QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#110201#6514. Is it well known in Poland?hos_lyricAC ✓52ms21812kbC++143.0kb2023-06-01 06:02:212023-06-01 06:02:22

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-01 06:02:22]
  • 评测
  • 测评结果:AC
  • 用时:52ms
  • 内存:21812kb
  • [2023-06-01 06:02:21]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }


struct Heap {
  Heap *l, *r;
  Int val;
  Heap(Int val_ = 0) : l(nullptr), r(nullptr), val(val_) {}
};
// max heap
Heap *merge(Heap *a, Heap *b) {
  if (!a) return b;
  if (!b) return a;
  if (a->val < b->val) swap(a, b);
  a->r = merge(a->r, b);
  swap(a->l, a->r);
  return a;
}
void pop(Heap *&a) {
  a = merge(a->l, a->r);
}


int N;
vector<Int> C;
vector<int> A, B;

vector<vector<int>> graph;

// * > * > * > * [> * < *]
Int bad;
vector<Heap> nodes;
Heap *solve(int u, int p) {
  Heap *ret = nullptr;
  for (const int v : graph[u]) if (p != v) {
    Heap *res = solve(v, u);
    ret = merge(ret, res);
  }
  Int c = C[u];
  for (; ; ) {
    if (ret && c <= ret->val) {
      const Int x1 = ret->val; pop(ret);
      if (ret) {
        const Int x2 = ret->val; pop(ret);
        c = c - x1 + x2;
      } else {
        bad += (c - x1);
        break;
      }
    } else {
      nodes[u] = Heap(c);
      ret = merge(ret, &nodes[u]);
      break;
    }
  }
  return ret;
}

int main() {
  for (; ~scanf("%d", &N); ) {
    C.resize(N);
    for (int u = 0; u < N; ++u) {
      scanf("%lld", &C[u]);
    }
    A.resize(N - 1);
    B.resize(N - 1);
    for (int i = 0; i < N - 1; ++i) {
      scanf("%d%d", &A[i], &B[i]);
      --A[i];
      --B[i];
    }
    
    graph.assign(N, {});
    for (int i = 0; i < N - 1; ++i) {
      graph[A[i]].push_back(B[i]);
      graph[B[i]].push_back(A[i]);
    }
    bad = 0;
    nodes.resize(N);
    Heap *res = solve(0, -1);
    Int ans = 0;
    Int sig = +1;
    for (; res; ) {
      const Int x = res->val; pop(res);
      ans += sig * x;
      sig = -sig;
    }
    ans += sig * bad;
    
    Int sumC = 0;
    for (int i = 0; i < N; ++i) {
      sumC += C[i];
    }
    ans = (sumC + ans) / 2;
    printf("%lld\n", ans);
  }
  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3692kb

input:

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

output:

7

result:

ok 1 number(s): "7"

Test #2:

score: 0
Accepted
time: 2ms
memory: 3692kb

input:

20
530933472 920863930 136179483 46535289 291417568 338674057 731327836 375973098 986104110 203163644 489326483 785212564 712578139 801609721 666347686 282530991 823910542 217884304 785578553 116701831
8 3
18 19
11 20
2 18
8 13
5 15
12 16
7 8
10 9
13 6
17 10
17 18
13 15
18 4
13 12
1 14
17 15
15 14
5...

output:

4611098449

result:

ok 1 number(s): "4611098449"

Test #3:

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

input:

20
384529189 217795442 901954855 742992564 354875060 497288585 40376145 575315239 263212445 574630916 520470316 917128880 461333242 407666839 565926566 390970156 568486150 291329847 493439854 637783217
10 17
19 8
20 17
7 17
9 6
17 14
16 18
4 3
9 14
6 2
14 18
13 4
11 15
9 3
7 12
16 1
8 14
5 14
9 15

output:

4410782058

result:

ok 1 number(s): "4410782058"

Test #4:

score: 0
Accepted
time: 2ms
memory: 3648kb

input:

19
601996737 696498327 385657564 527861058 529330573 376647612 223077352 338754937 682264670 671903443 645156487 755321105 978945836 120368247 611275923 947566064 98892994 858404931 401419272
11 14
8 14
3 6
13 12
16 15
1 4
11 4
2 7
4 3
15 5
2 1
13 5
10 16
9 16
1 17
18 6
3 19
5 7

output:

5848746543

result:

ok 1 number(s): "5848746543"

Test #5:

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

input:

1000
379354617 199235046 102686848 841958378 178689385 896777301 798019293 538540178 877449071 825184825 426375184 882476194 614078703 438824267 731949932 770345838 655572525 687098129 758287148 690715403 984914877 365231609 752380073 509777049 511312331 889780846 745450511 740863321 552773286 15071...

output:

252915929540

result:

ok 1 number(s): "252915929540"

Test #6:

score: 0
Accepted
time: 2ms
memory: 3772kb

input:

999
402468827 468124822 806603931 945584323 480102625 19722 169790452 515757998 19958108 528988204 415841643 111279190 885615782 740479752 550815713 556065700 511850711 148616044 552183365 471481958 946651162 968386282 610171866 971986310 377716133 564442897 222395758 16053401 613716892 487668831 48...

output:

249829986618

result:

ok 1 number(s): "249829986618"

Test #7:

score: 0
Accepted
time: 2ms
memory: 3772kb

input:

1000
637313417 62510663 83128345 702987667 374713776 778401168 619811617 896369345 486498325 448824176 297332011 916674955 895413407 803990450 21693588 866905388 169932411 340234047 259184127 331450756 506720779 194809709 896076578 936050255 925430496 741889050 638981221 628789074 358540287 89791215...

output:

226446817254

result:

ok 1 number(s): "226446817254"

Test #8:

score: 0
Accepted
time: 1ms
memory: 3780kb

input:

1000
147397255 520346638 619331018 431963137 427476094 995854324 640680706 473132459 1061281 701469953 87766202 316181634 506906587 181791922 174614534 639267313 489608085 693127159 412762229 906080005 562083964 319379642 148511208 32431198 908725162 531631359 390987308 965197199 851576914 95327968 ...

output:

229874635771

result:

ok 1 number(s): "229874635771"

Test #9:

score: 0
Accepted
time: 2ms
memory: 3736kb

input:

999
98690497 488296773 570509174 87593828 566189245 345232397 309963286 886305731 195509167 561473976 304758565 239428403 9228891 38443063 343536428 606219587 806177708 94853421 426729965 393545389 820769556 365126116 489908193 276668395 217091957 976510596 415255517 670037302 793997764 947495706 10...

output:

266569042607

result:

ok 1 number(s): "266569042607"

Test #10:

score: 0
Accepted
time: 43ms
memory: 12520kb

input:

99999
986882907 1232240 337619783 537571360 365049288 188680042 509367962 534436294 946650473 29350838 923503765 618367016 168291380 404626206 124377948 986455184 473759137 387049360 162826460 323510191 211352878 82863775 17091396 407155225 173680347 196421120 814831494 60052678 758323119 332899343 ...

output:

26955310538442

result:

ok 1 number(s): "26955310538442"

Test #11:

score: 0
Accepted
time: 50ms
memory: 12872kb

input:

99999
169254998 918063609 668561972 770912431 264124201 869049291 712764510 534928905 158868050 300515179 423967505 359489169 291771814 974326605 179984077 64419446 878917869 648153340 836618564 501365821 598893667 418912662 623143389 237977915 386062139 64857163 415519282 255368734 707552791 250750...

output:

24980984457288

result:

ok 1 number(s): "24980984457288"

Test #12:

score: 0
Accepted
time: 37ms
memory: 21796kb

input:

99999
347338422 491612453 407566718 796541908 98249263 558752260 292793389 173080189 379802206 287852065 803260649 416016209 229571904 148896142 401954400 141867363 867198295 507433603 5016582 795405869 745957416 39060439 287994002 754819594 113408700 640200075 423580799 835793595 291075616 99782585...

output:

25052569124780

result:

ok 1 number(s): "25052569124780"

Test #13:

score: 0
Accepted
time: 50ms
memory: 12612kb

input:

99999
360128594 411412680 527204090 372565812 103231037 22565373 221296881 305769065 715380439 477521088 32351760 872404698 173722576 769148915 219315280 295670209 806905353 878447304 939702814 893021164 659208217 508134216 976693978 135722557 365286091 136675020 561442648 314789928 890125726 398554...

output:

25482189859698

result:

ok 1 number(s): "25482189859698"

Test #14:

score: 0
Accepted
time: 29ms
memory: 13384kb

input:

99999
933677438 5193233 698057759 206690874 647709814 47304652 564480869 231735925 292651918 151781528 88878800 810204789 348292113 991119238 147020094 578917932 666185615 751878026 378967055 185309105 839164507 23241725 493535657 863069118 235596299 849769241 141867509 633471536 77392892 311298177 ...

output:

24906860070536

result:

ok 1 number(s): "24906860070536"

Test #15:

score: 0
Accepted
time: 43ms
memory: 13108kb

input:

99999
586621399 67034795 744197979 428719941 186040128 337412782 627333531 907664857 452670080 279988804 680817776 850438544 748004879 78151250 653281049 224468011 712422550 965657365 920276045 83072512 37405557 459312285 538349234 860634232 590415679 810939212 3054951 722292370 216994361 969692762 ...

output:

25040795381744

result:

ok 1 number(s): "25040795381744"

Test #16:

score: 0
Accepted
time: 45ms
memory: 12632kb

input:

99999
114528034 464924385 523103491 29935334 820807195 418384566 289371281 452591789 448177706 428956255 235149843 601466877 479622366 542151676 745679562 712978679 636036167 719398356 776068792 979955238 785512826 78901970 397187364 326036724 714725648 481725187 369684762 168311346 487661479 513293...

output:

27116183131808

result:

ok 1 number(s): "27116183131808"

Test #17:

score: 0
Accepted
time: 44ms
memory: 12664kb

input:

99999
523298146 241251776 889672347 128419232 629076064 181935478 716889602 958783488 739552579 410161695 878274568 482752937 460688203 987599229 172870486 489010391 816048941 768907748 11990859 208635288 25124753 198849727 650072187 211344978 933649300 258438924 80660779 349244475 404595527 9533137...

output:

27326057043814

result:

ok 1 number(s): "27326057043814"

Test #18:

score: 0
Accepted
time: 46ms
memory: 12820kb

input:

99999
465248394 284941911 197552339 494643394 66868047 831053717 645335104 881754815 877323170 474268179 723521679 645244746 532895459 331560526 316218659 360855477 42879130 775927179 990749125 533317716 647391984 692556880 368587040 196518144 21706035 295682281 724429126 444370327 465222742 7658070...

output:

26838807915654

result:

ok 1 number(s): "26838807915654"

Test #19:

score: 0
Accepted
time: 30ms
memory: 12660kb

input:

99999
3987799 951164764 114801282 381494747 958212950 250486674 541902552 280400809 41439406 409205533 82892238 46420622 59013752 476943191 686386533 679483871 545927232 283161390 708966229 399937987 770884744 794679203 603298183 338066357 529928003 557021261 855431835 39472902 316370522 833859540 5...

output:

25679482570042

result:

ok 1 number(s): "25679482570042"

Test #20:

score: 0
Accepted
time: 35ms
memory: 17040kb

input:

99999
951951243 552327528 914668430 552138255 515742273 307915526 149624240 757719116 279887008 644368162 584077742 279481804 51646137 530017176 89192573 76701471 827128959 334300874 967181104 558913944 514460237 436038173 887006084 493277645 15652700 467120250 114904661 599935002 288542798 61788795...

output:

28832709904074

result:

ok 1 number(s): "28832709904074"

Test #21:

score: 0
Accepted
time: 28ms
memory: 12700kb

input:

100000
550983811 83541333 386830884 424875457 370442692 971293764 519119954 844145288 586883646 140473110 404747270 331885576 973779472 921728770 552231866 554941539 638671791 143822262 446538108 467898734 339072610 860960210 105585517 391543606 189341381 226420587 950502467 749403256 211686727 1431...

output:

22980011690330

result:

ok 1 number(s): "22980011690330"

Test #22:

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

input:

100000
757978376 984529399 452851841 136542086 608386998 271540657 34155342 171418987 511468522 593030091 748005766 374894607 862768421 818732003 283266733 247641059 284488463 195796062 307906730 928516189 501081585 220819683 861598794 139779189 681753401 815247580 376126397 881485097 638896122 8735...

output:

25020139142462

result:

ok 1 number(s): "25020139142462"

Test #23:

score: 0
Accepted
time: 35ms
memory: 21812kb

input:

100000
641094504 853045540 46632394 867204268 587736252 816019434 909151518 514602975 732402677 875334273 277042014 431421647 800568511 698334245 505237056 325088976 272768889 55076325 181337452 72813134 648145333 840967461 376706304 656620868 704067258 685557788 384187914 902101446 662610434 765847...

output:

25051877149369

result:

ok 1 number(s): "25051877149369"

Test #24:

score: 0
Accepted
time: 43ms
memory: 12504kb

input:

100000
131627088 490669844 38057937 421861313 800689699 933890797 857786963 953336833 452605751 951302454 898014094 738368602 723160678 872431571 107569597 406273508 59580779 494959662 512077374 795209082 756082534 891813813 878495251 431413819 410643804 97282136 187559010 686324746 658146997 579971...

output:

24473571018851

result:

ok 1 number(s): "24473571018851"

Test #25:

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

input:

100000
559951740 84450398 768720119 401210567 345168475 368695484 200970951 879303692 439942637 625562893 954541134 676168692 897730215 94401894 185017515 689521230 213828337 73423088 511150127 87497022 936038824 406921322 540561123 158760380 280954013 810376357 767983871 859782163 845414163 7876825...

output:

24939504152762

result:

ok 1 number(s): "24939504152762"

Test #26:

score: 0
Accepted
time: 29ms
memory: 13140kb

input:

100000
880377480 428467881 528487847 89316892 235335628 889647252 243691659 544154939 100237847 722246820 4856037 570876686 613968782 922556648 316372217 407689624 117993145 218332791 241821107 950414368 939593475 556186602 776804639 762435506 181074237 856296925 818437874 198665628 588529179 592489...

output:

24986638354249

result:

ok 1 number(s): "24986638354249"

Test #27:

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

input:

100000
915878639 517587208 794794206 2569845 25534560 979087644 645053900 5710514 940892896 21530097 910285265 488967120 339502498 751647732 648936889 182625893 995361454 194064005 91276902 186945853 362962272 535809144 693643086 412678612 43057383 260134551 159391483 71481567 95099591 377835037 762...

output:

22927167003809

result:

ok 1 number(s): "22927167003809"

Test #28:

score: 0
Accepted
time: 39ms
memory: 12664kb

input:

100000
363312909 166122161 210330680 456202843 856559603 686911764 232834235 784026772 820901989 500223768 346762630 114178406 540393236 45381902 278272077 480607985 51564759 833026567 147420968 188539996 982630695 128423002 932762387 49573966 344214692 161034788 224348271 350849397 104898958 993412...

output:

22644151831697

result:

ok 1 number(s): "22644151831697"

Test #29:

score: 0
Accepted
time: 51ms
memory: 12928kb

input:

100000
213370667 743774676 679206478 587577890 430075785 911909982 253600356 404043474 309497459 688383207 418792647 587234230 645356199 229455327 494521216 844808858 403735577 905542155 122871050 130328793 194136174 704018688 950562007 36081126 477564290 409686444 604693451 98462663 984703458 77075...

output:

23263744414387

result:

ok 1 number(s): "23263744414387"

Test #30:

score: 0
Accepted
time: 30ms
memory: 12596kb

input:

100000
297743881 722663258 753866958 747124403 7508450 652978039 308003785 621923595 394039877 146430845 701897795 912082956 630010359 26381293 789669189 422513996 806273634 830804112 30511291 532121059 378105365 596586225 692010485 385091823 680395073 602378974 670814758 665589264 687905340 4566566...

output:

24302445390534

result:

ok 1 number(s): "24302445390534"

Test #31:

score: 0
Accepted
time: 27ms
memory: 17172kb

input:

100000
810563390 785521074 418551233 219422366 13560405 137571205 332943988 59776171 456326071 45274321 325130344 815750637 327321941 374473502 87918822 675731929 127170418 995584616 617086669 370626453 299710757 496287650 922599182 702780539 236158682 88365870 671214382 258491034 488220881 71696624...

output:

21188848014262

result:

ok 1 number(s): "21188848014262"