QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#217767 | #794. Chase | _LAP_ | 100 ✓ | 132ms | 339992kb | C++14 | 1.8kb | 2023-10-17 12:12:39 | 2023-10-17 12:12:40 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, M = 2e5 + 10;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
int n, c, val[N], h[N], e[M], ne[M], idx;
inline void add(int a, int b) {e[idx] = b, ne[idx] = h[a], h[a] = idx ++; }
long long sum[N], f[N][105][2], g[N][105][2], ans;
void dfs(int u, int fa) {
for(int i = h[u]; i != -1; i = ne[i]) {
int v = e[i]; if(v == fa) continue;
sum[u] += val[v], dfs(v, u);
}
f[u][0][1] = g[u][0][1] = -INF;
g[u][1][1] = val[fa] + sum[u];
for(int i = h[u]; i != -1; i = ne[i]) {
int v = e[i]; if(v == fa) continue;
long long maxv[2] = {0ll, 0ll};
for(int j = c; j >= 0; j --) {
maxv[0] = max({maxv[0], g[u][c - j][0], g[u][c - j][1]});
maxv[1] = max({maxv[1], f[u][c - j][0], f[u][c - j][1] + val[fa] - val[v]});
ans = max({ans, f[v][j][0] + maxv[0], f[v][j][1] + maxv[0]});
ans = max({ans, g[v][j][0] + maxv[1], g[v][j][1] + maxv[1]});
}
for(int j = 1;j <= c; j ++) {
f[u][j][0] = max({f[u][j][0], f[v][j][0], f[v][j][1]});
f[u][j][1] = max({f[u][j][1], f[v][j - 1][0] + sum[u], f[v][j - 1][1] + sum[u]});
}
for(int j = 1; j <= c; j ++) {
g[u][j][0] = max({g[u][j][0], g[v][j][0], g[v][j][1]});
g[u][j][1] = max({g[u][j][1], g[v][j - 1][0] + sum[u] + val[fa] - val[v], g[v][j - 1][1] + sum[u] + val[fa] - val[v]});
}
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
memset(h, -1, sizeof h);
cin >> n >> c;
if(c == 0) {cout << "0\n"; return 0; }
for(int i = 1; i <= n; i ++) cin >> val[i];
for(int i = 1; i < n; i ++) {
int a, b; cin >> a >> b;
add(a, b), add(b, a);
}
dfs(1, 0); cout << ans << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 20
Accepted
Test #1:
score: 20
Accepted
time: 1ms
memory: 5904kb
input:
10 3 10 1 1 1 1 10 1 10 1 10 1 2 2 3 3 4 4 5 5 6 3 7 7 8 9 4 9 10
output:
23
result:
ok single line: '23'
Test #2:
score: 0
Accepted
time: 1ms
memory: 5916kb
input:
10 3 100 1 1 1 1 101 1 102 1 101 1 2 2 3 3 4 4 5 5 6 3 7 7 8 9 4 9 10
output:
206
result:
ok single line: '206'
Test #3:
score: 0
Accepted
time: 1ms
memory: 6080kb
input:
10 1 518174156 230717080 472180512 414366521 830671466 741246187 15054694 859674628 670470682 44556532 2 1 3 2 4 2 5 1 6 2 7 1 8 2 9 3 10 4
output:
3005642004
result:
ok single line: '3005642004'
Test #4:
score: 0
Accepted
time: 1ms
memory: 7720kb
input:
10 10 997970178 695722889 947441667 227169882 700168089 708026438 617329225 115709571 125361462 672975425 2 1 3 1 4 2 5 1 6 1 7 1 8 1 9 8 10 3
output:
5464995373
result:
ok single line: '5464995373'
Test #5:
score: 0
Accepted
time: 1ms
memory: 3848kb
input:
10 0 488649726 845401142 62722576 456837485 76649887 873202358 141737854 75616801 275993900 454858603 2 1 3 1 4 3 5 1 6 5 7 2 8 3 9 7 10 7
output:
0
result:
ok single line: '0'
Test #6:
score: 0
Accepted
time: 0ms
memory: 6068kb
input:
9 4 10 10 10 100 100 100 100 100 100 1 2 1 3 2 4 2 5 3 6 3 7 4 8 5 9
output:
520
result:
ok single line: '520'
Subtask #2:
score: 20
Accepted
Dependency #1:
100%
Accepted
Test #7:
score: 20
Accepted
time: 0ms
memory: 10756kb
input:
1000 100 79166774 121231340 468315597 562946531 917264788 50325145 438612212 534988965 549631716 910780074 180677061 722658742 276851906 968424087 471747082 459836063 459102071 281907117 425216848 660646277 590498234 749117430 345825758 691074511 44729629 714850403 195068486 787557896 3908637 990549...
output:
198855965618
result:
ok single line: '198855965618'
Test #8:
score: 0
Accepted
time: 0ms
memory: 9720kb
input:
1000 5 932767726 768782041 218249756 683715313 849547978 614606547 186659231 298583605 419691534 277778495 404941065 271721360 712509253 843123691 314559677 821007340 339997565 585516009 575565589 198292313 741866794 582291331 582985250 602921065 813139058 328026752 551819105 891861439 408179972 982...
output:
17031783262
result:
ok single line: '17031783262'
Test #9:
score: 0
Accepted
time: 1ms
memory: 8156kb
input:
1000 5 396939976 390729996 486916798 918423698 913119814 259638596 200093857 210462783 52914568 282602770 35339052 231714309 325137125 354962126 916998024 24273100 525091309 400725581 624665962 90129083 717211137 105098549 272482565 296564859 6444244 919787728 336601040 391534741 866718077 146090906...
output:
454622481903
result:
ok single line: '454622481903'
Test #10:
score: 0
Accepted
time: 2ms
memory: 7568kb
input:
1000 100 479381144 358363548 690002913 115547683 639987228 53974614 634416768 456374705 562371002 980716775 152654166 565303414 908316698 972204518 830993211 51925201 451800246 693072369 968677925 487959242 812068625 434596821 842413657 77585129 899430936 238228503 830201944 35177452 465011315 60620...
output:
48457943643
result:
ok single line: '48457943643'
Test #11:
score: 0
Accepted
time: 0ms
memory: 8200kb
input:
1000 30 501282440 198442565 949560839 710717896 721161151 83984282 698465155 521650957 865604557 187213832 294106433 931632740 581689401 676164170 711517313 383276238 960608914 377639968 741042496 848176077 833155614 805809050 598895463 147210214 5456494 79428535 695238739 780756163 863523897 108968...
output:
44004410803
result:
ok single line: '44004410803'
Test #12:
score: 0
Accepted
time: 0ms
memory: 12932kb
input:
999 9 448852940 219405334 505384965 679759539 163482837 1597024 633065051 49183711 561839965 35224785 444932783 383225435 392021855 752716105 582846947 496277430 30519478 247524796 2430352 303802268 464933651 393522690 22400892 394226763 123652088 517790785 278592322 91603166 901880543 3818645 36808...
output:
31638537456
result:
ok single line: '31638537456'
Subtask #3:
score: 30
Accepted
Test #13:
score: 30
Accepted
time: 119ms
memory: 339992kb
input:
100000 100 272538734 450622060 984388315 998259873 513777049 314505109 721457794 83778046 989928506 167819877 551695847 7515364 357778512 637784012 326763549 577449660 518546866 360303836 971240110 306797755 286741401 503673663 11752144 858504181 794647811 328025545 494268916 720611413 988278222 815...
output:
374008563052
result:
ok single line: '374008563052'
Test #14:
score: 0
Accepted
time: 132ms
memory: 339676kb
input:
100000 100 622767256 247696 566669029 1582570 339636315 298980981 486419884 737675658 56837777 394603361 609075433 495441396 677061766 259035389 960327022 130567666 575445617 422310417 117477610 778161146 60323556 49244268 219968478 64595698 445203568 391687000 453455601 167692049 410933178 39304083...
output:
378527696261
result:
ok single line: '378527696261'
Test #15:
score: 0
Accepted
time: 70ms
memory: 334172kb
input:
100000 100 19678 20433331 520078899 247608407 207644823 55572186 836192121 991972459 340296653 723008983 844122105 498929556 779777263 667557112 520719158 449291745 575935956 308026933 44388199 852178733 121353565 416993142 473679474 126718843 81300705 821139091 75400781 937697984 998700942 58822499...
output:
47325023706534
result:
ok single line: '47325023706534'
Test #16:
score: 0
Accepted
time: 31ms
memory: 334712kb
input:
100000 1 366388293 90345209 212504519 355842064 346688115 956990422 950823969 92255336 625229027 181613203 590539916 937764619 551488399 637188079 920746635 635221701 822847286 671179284 579356665 206501558 16770757 464000210 851590333 125657864 770737108 743192400 748395373 901479380 887956292 9343...
output:
9346539632
result:
ok single line: '9346539632'
Test #17:
score: 0
Accepted
time: 115ms
memory: 334712kb
input:
100000 99 412149341 990585004 695463669 385136448 94590745 751664592 787802280 728872483 492006040 327208481 80714192 632363520 746663331 214353108 339927185 292954440 325565181 523661417 451285285 955174616 182891565 356688220 418619954 248031504 446321070 240983322 568991806 95605239 228030895 401...
output:
143777646357
result:
ok single line: '143777646357'
Test #18:
score: 0
Accepted
time: 117ms
memory: 334716kb
input:
100000 100 112901122 977827173 522083612 146203884 966426093 902647659 345046808 247763629 600404238 263499279 597473470 86966596 211731230 594841471 968789968 338655337 597345944 655767329 292495565 244596831 567141471 232901817 798350788 200746421 71374397 162285599 58553614 953546522 435718254 87...
output:
150108329217
result:
ok single line: '150108329217'
Test #19:
score: 0
Accepted
time: 104ms
memory: 334704kb
input:
100000 100 84471108 425540284 161732987 481556621 546608321 522793421 766319208 161386517 540985174 599069311 456381854 25173415 509407989 687695224 95534298 482540450 398387520 484883543 254186934 193450913 606090746 73248376 698072547 615158338 582881659 174646881 677340885 106750327 561362549 635...
output:
155618352882
result:
ok single line: '155618352882'
Subtask #4:
score: 30
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Test #20:
score: 30
Accepted
time: 125ms
memory: 334772kb
input:
100000 100 976695233 457941608 99290289 69530058 698045977 135412044 819329474 470423413 598012612 632109050 48571233 954247984 590867984 852345170 234072909 14434786 574303648 970098890 168855212 433978210 587129686 628875000 257654770 989139306 463303735 142719375 584842077 253783805 720450895 714...
output:
121197301996
result:
ok single line: '121197301996'
Test #21:
score: 0
Accepted
time: 1ms
memory: 5460kb
input:
100000 0 768473287 793065416 486433893 755373447 67302923 821298223 784754729 795087736 13125833 225971215 143922678 211960243 759834548 240243663 179237307 687622734 80970681 39494412 778905053 701358127 233233653 49818438 29568907 985050189 322462776 764100 200361359 900287151 410743302 230775504 ...
output:
0
result:
ok single line: '0'
Test #22:
score: 0
Accepted
time: 114ms
memory: 334688kb
input:
100000 100 320977153 929098856 439852633 970825907 427594979 42981890 365931149 735791601 143116842 61307347 238900176 872839722 847791474 862417782 10313452 254605338 948991275 796622537 633367272 356478529 927747471 23000166 50319448 589992696 642030842 694845117 290954388 927464193 320087571 2877...
output:
137870897205
result:
ok single line: '137870897205'
Test #23:
score: 0
Accepted
time: 30ms
memory: 334716kb
input:
100000 1 31172254 8432541 483001901 124588630 807755280 284317528 35178110 773340275 75950649 61279032 360681838 489346258 882670286 368149649 24577084 920368959 779169788 449511061 592063975 979341673 674444869 107208475 45255920 405161180 7329503 518063681 530458217 863366589 367273127 508121216 2...
output:
8242353238
result:
ok single line: '8242353238'
Test #24:
score: 0
Accepted
time: 121ms
memory: 334828kb
input:
100000 100 281715291 111550655 162414171 955968721 33789779 909684324 246481661 282554586 866352052 7137139 486098531 430023530 687538059 39428202 992870993 477238281 903065869 563058835 938476250 997207245 611169871 885098385 518696824 897499359 677237469 260653628 18833363 242632799 422925656 3390...
output:
138756588301
result:
ok single line: '138756588301'
Test #25:
score: 0
Accepted
time: 67ms
memory: 333992kb
input:
100000 100 552894138 290050797 356892704 669430741 348725051 160523296 4350914 280212227 356780890 392036009 814541028 31010845 244390454 483666290 740028853 587543626 374281715 103232456 165473367 592852403 129638550 192543055 97711659 77750955 173257352 310864509 596745065 838123771 970834778 4788...
output:
46988174974932
result:
ok single line: '46988174974932'
Test #26:
score: 0
Accepted
time: 100ms
memory: 339784kb
input:
100000 100 357450513 239414644 818350736 538256778 876034825 54970465 621355009 338274269 774366340 847321691 773925376 591492684 477923478 660727807 89822979 963520350 102944221 607165264 502209290 3602014 773043125 155315210 361077697 807476181 919447306 871126212 37835670 840755512 891959333 1177...
output:
376312393154
result:
ok single line: '376312393154'
Test #27:
score: 0
Accepted
time: 91ms
memory: 339788kb
input:
100000 100 672003771 110445351 411211903 431641089 15506474 665072961 39713185 906087489 399120926 776061051 881442290 510585671 188364737 237084786 32496697 421657243 201749696 764074690 818441546 65445219 600032970 896783467 872898690 183460205 619183393 571636045 623276593 167132821 971194416 771...
output:
386366272014
result:
ok single line: '386366272014'