QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#548962#6406. Stage ClearpandapythonerTL 3069ms187820kbC++233.1kb2024-09-05 22:24:562024-09-05 22:24:57

Judging History

This is the latest submission verdict.

  • [2024-09-05 22:24:57]
  • Judged
  • Verdict: TL
  • Time: 3069ms
  • Memory: 187820kb
  • [2024-09-05 22:24:56]
  • Submitted

answer

#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;

#define len(a) int((a).size())
#define all(a) begin(a), end(a)
#define rep(i, n) for (int i = 0; i < (n); i++)


// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#include <bits/extc++.h>

struct hash_for_hash_map {
    size_t operator()(size_t x) const {
        static const size_t D = std::chrono::steady_clock::now().time_since_epoch().count();
        return x * x + D;
    }
};

template<typename key_t, typename value_t, typename hash_t = hash_for_hash_map>
using hash_map = __gnu_pbds::gp_hash_table<key_t, value_t, hash_t>;

template<typename value_t, typename hash_t = hash_for_hash_map>
using hash_set = __gnu_pbds::gp_hash_table<value_t, __gnu_pbds::null_type, hash_t>;

template<typename key_t, typename value_t, typename comp = std::less<key_t>>
using ordered_map = __gnu_pbds::tree<key_t, value_t,
    comp, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;

template<typename value_t, typename comp = std::less<value_t>>
using ordered_set = ordered_map<value_t, __gnu_pbds::null_type, comp>;

int n, m;
vector<ll> g;
vector<ll> a, b;

// hash_map<ll, ll> mem;
unordered_map<ll, ll> mem;
vector<int> ord;

clock_t start;

ll dfs(ll mask) {
    if (mask == (1ll << n) - 1) {
        return 0;
    }
    // if (double(clock() - start) / CLOCKS_PER_SEC > 6.9) {
    //     return 1e18;
    // }
    auto it = mem.find(mask);
    if (it != mem.end()) {
        return it->second;
    }
    ll adj = 0;
    for (int i = 0; i < n; i++) {
        if (mask >> i & 1) {
            adj |= g[i];
        }
    }
    pair<ll, int> best_pos = { 1e18, -1 };
    pair<ll, int> best_neg = { -1e18, -1 };
    for (int i = 0; i < n; i++) {
        if ((adj >> i & 1) && !(mask >> i & 1)) {
            if ((g[i] & mask) != g[i]) continue;
            if (a[i] <= b[i]) {
                best_pos = min(best_pos, make_pair(a[i], i));
            } else {
                best_neg = max(best_neg, make_pair(b[i], i));
            }
        }
    }
    ll res = 1e18;
    for (auto i : ord) {
        if ((adj >> i & 1) && !(mask >> i & 1)) {
            if ((g[i] & mask) == g[i]) {
                if (a[i] <= b[i] and i != best_pos.second) continue;
            }
            if (res <= a[i]) continue;
            res = min(res, max(a[i], dfs(mask | (1ll << i)) + a[i] - b[i]));
        }
    }
    return mem[mask] = res;
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    start = clock();
    mem.rehash(3e6);
    cin >> n >> m;
    a.resize(n);
    b.resize(n);
    for (int i = 1; i < n; i++) {
        cin >> a[i] >> b[i];
    }
    g.resize(n);
    while (m--) {
        int v, u;
        cin >> v >> u;
        v--, u--;
        g[v] |= 1ll << u;
    }
    ord.resize(n);
    iota(all(ord), 0);
    sort(all(ord), [&](int v, int u) {
        return a[v] < a[u];
        });
    // random_shuffle(all(ord));
    cout << dfs(1) << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 27992kb

input:

4 4
4 2
5 3
2 6
1 2
1 3
2 4
3 4

output:

4

result:

ok 1 number(s): "4"

Test #2:

score: 0
Accepted
time: 7ms
memory: 28128kb

input:

15 14
254040392438309 117083115436273
500005748229691 557255157630172
821034233718230 865199673774998
659892147898798 987564141425694
81172575487567 811635577877255
751768357864605 341103322647288
454926350150218 140191090713900
921608121471585 659295670987251
223751724062143 505619245326640
8907765...

output:

1665396301509143

result:

ok 1 number(s): "1665396301509143"

Test #3:

score: 0
Accepted
time: 4ms
memory: 28076kb

input:

18 17
636830992776530 847574431876821
330869946457865 78274534165482
450581372553540 11565219334965
8736347226844 17186323694285
870805093198860 559070167736042
674369178493171 930151818400874
641605209598997 222521062460239
450936030349531 469197172169023
831295459816974 626096008793091
53095460351...

output:

2375957544280218

result:

ok 1 number(s): "2375957544280218"

Test #4:

score: 0
Accepted
time: 4ms
memory: 27924kb

input:

20 19
539893468691183 767805205447882
240338186903141 960937349402327
942645580569365 896509929612645
542601575005817 191461109090531
540992546866047 765080044816119
904535155855114 858111921213175
452499200048240 115895143306864
983856946412026 838504718536099
586421298181479 265212699386882
677124...

output:

800919806038419

result:

ok 1 number(s): "800919806038419"

Test #5:

score: 0
Accepted
time: 0ms
memory: 28256kb

input:

24 23
114281007218527 308690671179962
145951034437731 718976086594208
709172151907814 926071954787084
224496444610281 498657753059525
874422017133378 857676356343078
532175866197017 818525693672607
303837639402605 374469705563954
512244364294540 952911486867703
748959419417502 249992707230361
512696...

output:

114281007218527

result:

ok 1 number(s): "114281007218527"

Test #6:

score: 0
Accepted
time: 3069ms
memory: 187820kb

input:

36 35
389328367777319 678636570542258
32216944647452 612585362150577
891592845704885 596030605892036
688825276167602 461516360471825
916552899998310 106733202183953
400050408958777 670724326933521
995792861502757 894514508573875
14511185222713 612305257166443
175168368096281 508263855969282
85578802...

output:

171942144116875

result:

ok 1 number(s): "171942144116875"

Test #7:

score: 0
Accepted
time: 656ms
memory: 69564kb

input:

36 35
759037289890767 849577210686635
16379509883489 441829377955433
589378488455351 990818352083122
871208015900506 727359003875494
207852561142533 28766987248469
81321183353129 892618157632070
198487099788393 519364502513651
83942803274015 988821788304459
868185445880277 269956013388079
3834515054...

output:

759037289890767

result:

ok 1 number(s): "759037289890767"

Test #8:

score: 0
Accepted
time: 34ms
memory: 31212kb

input:

36 35
100792831728257 823656493168793
866936535786311 187861146327778
132998929717538 605906559206892
3319598846477 393401056223733
964444786730964 932398059281618
925176496607384 148825907337833
985037559482190 646827297289525
469876125353024 641923164294854
453796287874442 291205025001534
72806942...

output:

1397699717661157

result:

ok 1 number(s): "1397699717661157"

Test #9:

score: 0
Accepted
time: 1583ms
memory: 117156kb

input:

36 36
245996406159980 462974248377488
839352152971124 40282565369163
572695144110271 507726167903341
671102350267895 18090181781241
643724978558334 551787913319524
936340565446887 517649577919257
158127116487034 175750969611510
396852573858996 670814068366285
534702788102341 124550558279140
69441153...

output:

2508008255775889

result:

ok 1 number(s): "2508008255775889"

Test #10:

score: 0
Accepted
time: 88ms
memory: 35048kb

input:

34 38
656738239290510 319959252044415
760511943177376 828562698756504
470087249708484 441916827764162
105399930988058 761192720347117
81742549616394 195819875734286
782982110569406 72384154665629
647269989285797 720280547207448
531182311814386 160821851115134
292963780645658 871789628567253
74499577...

output:

656738239290510

result:

ok 1 number(s): "656738239290510"

Test #11:

score: 0
Accepted
time: 2453ms
memory: 186860kb

input:

32 40
818105834607446 689904077664886
717146597564762 686987602224041
538827104521875 147060924732538
604913134601443 802546720879673
45376965619246 480061093729529
686039951678173 889398415870480
374408509732957 354006189233817
103818950629279 863526642478066
719174876808085 130061851080766
9744074...

output:

2289520618562758

result:

ok 1 number(s): "2289520618562758"

Test #12:

score: 0
Accepted
time: 1584ms
memory: 123904kb

input:

30 42
730678486091139 762413502107242
564137648972911 492217680357057
677122869459914 634406715345550
766223620461328 750896882727596
34139073751269 875301336250330
948602995486093 589201509496124
333847023521138 673322700954330
774661538057122 360743409997856
301647343463502 78371781314140
44979585...

output:

2296677982487339

result:

ok 1 number(s): "2296677982487339"

Test #13:

score: 0
Accepted
time: 0ms
memory: 28252kb

input:

28 44
996216744822715 15265122654307
591377215147374 392892022614182
134817686923570 666778840251745
603108267679560 939679039946396
792878600465606 943254465658609
705582931165204 626247204621328
833947774992752 802610518921019
60510220659563 935537906466250
900509663884138 957082020010408
38517385...

output:

1021065751521024

result:

ok 1 number(s): "1021065751521024"

Test #14:

score: 0
Accepted
time: 167ms
memory: 40916kb

input:

27 45
271265179156100 385209948242010
548010795825703 286502371912374
203557541769729 336737491323929
32253800857105 902537647325928
835008409588714 227495683621084
573457473959732 478446911624066
447407603972649 401150715116732
597962487418392 594931676764990
326718612562917 293848561935121
6497688...

output:

271265179156100

result:

ok 1 number(s): "271265179156100"

Test #15:

score: 0
Accepted
time: 139ms
memory: 39168kb

input:

26 46
511128167626061 755154773895250
469460004382432 144928349121735
272299544034000 41881588292305
453271611317466 830211882616629
877138218711823 441367083696839
476515315035731 252150151731957
174547198161633 921197665643069
56919360991429 297636468095153
717743189152864 552120784448634
95767590...

output:

511128167626061

result:

ok 1 number(s): "511128167626061"

Test #16:

score: 0
Accepted
time: 243ms
memory: 47036kb

input:

25 47
483175861091928 628662160345159
414348784525954 991346283769736
118134342611258 254055400216860
367817156249062 195226919472367
228751017881407 501458690109441
595787759089619 364958390117603
758404493344385 423811540220990
373421064986368 503851495028044
645521325517401 846860937023068
696132...

output:

433844295661451

result:

ok 1 number(s): "433844295661451"

Test #17:

score: 0
Accepted
time: 114ms
memory: 37468kb

input:

24 48
585069488201283 197610097667134
308270082266799 808583330722288
557830556971222 120690636824478
35599907670481 819914971288051
829534742813930 120848544147347
606952901638178 768967506529684
782628839276718 874238745648127
300397513492341 497558026945107
804922145123731 680206470300674
6976592...

output:

308270082266799

result:

ok 1 number(s): "308270082266799"

Test #18:

score: 0
Accepted
time: 4ms
memory: 28316kb

input:

22 50
263626616368674 621403432100399
205992448402675 530375039808909
713311017185345 512666135865696
98177241911216 239357547305336
958069323825513 67526585039598
167011099703449 27907032353436
450240530654192 706870876965792
690862186234915 405560181003741
18305076367979 434288631592058
2040128611...

output:

205992448402675

result:

ok 1 number(s): "205992448402675"

Test #19:

score: 0
Accepted
time: 4ms
memory: 28320kb

input:

20 52
975090006577788 801607726815766
84021986863902 176019568163775
33212494351022 557726461236616
412670490881035 171242243090013
5963358074583 814694975209648
727321559408120 470824240668916
517979548077593 380688272528419
111042754309162 470362460253753
261749697831900 173917705785526
7629533862...

output:

84021986863902

result:

ok 1 number(s): "84021986863902"

Test #20:

score: 0
Accepted
time: 21ms
memory: 30228kb

input:

18 54
189279872302549 253462459097101
970052803238801 70690425994748
748182832410340 900454936920101
461133559455077 21855992163077
331483449573694 323350930734446
706088561801647 138174738356485
393662591863692 483667366492868
786416692433338 336160844825462
593801741696439 741382094229566
74860376...

output:

3169539405883373

result:

ok 1 number(s): "3169539405883373"

Test #21:

score: 0
Accepted
time: 8ms
memory: 28984kb

input:

16 56
395342015310127 775685935556101
934577939024901 965361283792952
576834009420570 129503647328961
544782073827006 837284295438253
727371211508645 718326047275565
641544543098215 727030916600087
304530007771390 508152141046117
496976076388171 88278390380724
847359466084241 230351089520581
3857288...

output:

1347938772919751

result:

ok 1 number(s): "1347938772919751"

Test #22:

score: 0
Accepted
time: 3ms
memory: 28032kb

input:

13 59
462049122847001 379819318643195
906994629984306 817782702834337
16530223780534 109817575436611
212564825248425 461973420995760
441834701715792 337715901313471
617525313590710 209535426197959
442435192654635 37074739626278
388768152805311 117169294452155
41944657812171 28512250709355
3872542920...

output:

119277473592348

result:

ok 1 number(s): "119277473592348"

Test #23:

score: 0
Accepted
time: 5ms
memory: 28312kb

input:

32 40
378399574707502 124158745794613
757495231509323 219929822776362
23848097920043 150433648754718
89027690774330 779645256050635
561907039859750 609241299826157
763643565846881 888507632657093
676333558739618 640176398893719
71406433730404 424845408152620
798830911154534 473380124569425
633777571...

output:

822140162613756

result:

ok 1 number(s): "822140162613756"

Test #24:

score: 0
Accepted
time: 89ms
memory: 35672kb

input:

30 42
727583199647302 899249515493018
824090068816978 76007172563392
363024240077233 850438221628153
116359591692923 853281819729394
30058530056444 451301982541902
257660265949748 13377896646178
663971991464535 131134512531065
157395062362446 37580106788817
319094964254516 820617709636764
1504315319...

output:

501974519873235

result:

ok 1 number(s): "501974519873235"

Test #25:

score: 0
Accepted
time: 108ms
memory: 36748kb

input:

30 42
369260360694270 102056318890914
748491825980791 78813132950858
954070327995137 432356970729848
350635553708712 691146527864719
294293282115372 475494513261924
888038522270337 103810618966978
113783109472848 809679509781223
837753634194548 568343084457732
919145501514622 341839377646785
5331973...

output:

1549961271972830

result:

ok 1 number(s): "1549961271972830"

Test #26:

score: 0
Accepted
time: 52ms
memory: 32720kb

input:

27 45
819114038925000 120254131272485
104179695468842 921918040684848
746447605230839 27295948036099
986064794301025 488949445854444
234655178368316 80833846135201
919985230880643 628120598515383
196387855735763 424772227837484
131469466148846 89448230360542
403974409238330 13826215541265
6944769649...

output:

819114038925000

result:

ok 1 number(s): "819114038925000"

Test #27:

score: 0
Accepted
time: 19ms
memory: 29088kb

input:

27 45
486496070101292 615597109349036
242572807341486 639449514382537
377052935256294 551281267552890
40515117640598 407260163013031
405708189500053 766515180186126
483209839402832 362312036386239
532925852053573 493806909782433
696724944982406 733419216410091
60505679723836 533368236070572
11723652...

output:

242572807341486

result:

ok 1 number(s): "242572807341486"

Test #28:

score: 0
Accepted
time: 8ms
memory: 28756kb

input:

26 46
453718434413073 553397640961890
150619756399004 630260113021239
420562810047600 640552759162041
13296127747875 632471460306683
77074282416426 729821456755890
716966018482019 933374142866615
788537379821196 337442301680265
592445203074505 472078597445832
688056655927586 685777876573115
93009595...

output:

150619756399004

result:

ok 1 number(s): "150619756399004"

Test #29:

score: 0
Accepted
time: 0ms
memory: 27912kb

input:

26 46
58175650161464 42271857470481
470798493732803 957941605443488
422744095940059 108786907923757
956120077112935 880056937358567
141334402731044 765692212925074
281492906357796 931625701488132
873804234821197 229067678553001
381876791746006 492332722216193
812525232730573 209319517737245
13596589...

output:

58175650161464

result:

ok 1 number(s): "58175650161464"

Test #30:

score: 0
Accepted
time: 74ms
memory: 34080kb

input:

25 47
828420043451914 23238564513072
697852262104269 282962592679943
11641622610262 875935569794851
700951527948597 388158600069851
670930822274257 412137048360986
620516084451297 398117269577950
141395806409204 787408902235324
486108629647082 495272150546778
600334776708984 977561962353395
20865789...

output:

1237959652558927

result:

ok 1 number(s): "1237959652558927"

Test #31:

score: 0
Accepted
time: 3ms
memory: 28296kb

input:

25 47
899428122989338 145899041295597
222881474387355 567396721892960
575835915985374 936155770553948
827654190821346 987307978499644
718341345091272 127093962829633
986836218775381 333226666595599
304220802797171 857497097818156
808973011582896 646782097895557
468853452093373 995220967981033
673952...

output:

222881474387355

result:

ok 1 number(s): "222881474387355"

Test #32:

score: 0
Accepted
time: 26ms
memory: 29892kb

input:

20 52
632260641570555 458297297435212
811156594450074 396457920718786
828263304935855 598595529361630
494991655931244 565522700362647
637615228409302 53103708098168
282478952695367 559572457996575
283996296745450 136755148534977
311987900253249 916264279266468
127568654657954 419757804830372
1708998...

output:

1857245089696723

result:

ok 1 number(s): "1857245089696723"

Test #33:

score: 0
Accepted
time: 0ms
memory: 28220kb

input:

15 57
246045112915803 655156218208647
705418683694813 693501915791545
566584169060430 510070696716134
250578771535460 225895985907743
333447992843769 890301019152478
84932311613578 109581086444286
661795095725753 737789845693119
701638652385996 314265702790340
193866121765676 448306478072597
9012598...

output:

605304227530846

result:

ok 1 number(s): "605304227530846"

Test #34:

score: 0
Accepted
time: 0ms
memory: 28060kb

input:

13 59
693204042546316 810185778594521
577525994704226 715733166899945
143330503316974 422966459358241
971725930037503 176570841054548
86003143095734 958285835119465
1598110351366 843161843630556
864558845987130 616058236912443
616184515207279 183019603196329
538466547766038 827543523288927
308731090...

output:

1598110351366

result:

ok 1 number(s): "1598110351366"

Test #35:

score: 0
Accepted
time: 3ms
memory: 28020kb

input:

12 60
686900984655521 360414706539893
564707323239259 156422972125429
177275901374697 340280191229025
353147113772471 696217870144290
232816449731091 475738298171206
379431990659762 331837831277566
271071520158508 717200993828896
442174414532731 64467307038885
556024119489408 326171436618540
9234337...

output:

303548940354760

result:

ok 1 number(s): "303548940354760"

Test #36:

score: -100
Time Limit Exceeded

input:

29 42
775040437393962 341248169572722
986673473366420 634123558200586
924986853178078 748471027022825
220163146224574 731581874814033
385709535495598 952817310574349
68544084339822 938274652091880
915849278438528 969841130480180
882948261552555 194219121621828
680383071366521 407035944045810
1242317...

output:


result: