Solutions are presented as using the least memory and the fastest execution time. It also takes the top
10 most recent solutions from each language. If you want to limit to a specific index, click the
"Solved" button and go to that problem.
Problems
B"You are given a rooted tree, consisting of n vertices. The vertices in the tree are numbered from 1 to n , and the root is the vertex 1 . The value a_i is written at the i -th vertex. You can perform the following operation any number of times (possibly zero): choose a vertex v which has at least one child; increase a_v by 1 ; and decrease a_u by 1 for all vertices u that are in the subtree of v (except v itself). However, after each operation, the values on all vertices should be non-negative. Your task is to calculate the maximum possible value written at the root using the aforementioned operation. The first line contains a single integer t ( 1 <= t <= 10^4 ) -- the number of test cases. The first line of each test case contains a single integer n ( 2 <= n <= 2 cdot 10^5 ) -- the number of vertices in the tree. The second line contains n integers a_1, a_2, ... , a_n ( 0 <= a_i <= 10^9 ) -- the initial values written at vertices. The third line contains n-1 integers p_2, p_3, ... , p_n ( 1 <= p_i <= n ), where p_i is the parent of the i -th vertex in the tree. Vertex 1 is the root. Additional constraint on the input: the sum of n over all test cases doesn't exceed 2 cdot 10^5 . For each test case, print a single integer -- the maximum possible value written at the root using the aforementioned operation. In the first test case, the following sequence of operations is possible: "... |
Tutorials
Submissions
Submission Id |
Author(s) |
Index |
Submitted |
Verdict |
Language |
Test Set |
Tests Passed |
Time taken (ms) |
Memory Consumed (bytes) |
Tags |
Rating |
273599967 |
zrnstnsr |
D |
July 30, 2024, 5:05 p.m. |
OK |
C# 10 |
TESTS |
27 |
1124 |
43827200 |
|
|
273652656 |
ImmortaLimit |
D |
July 31, 2024, 3:04 a.m. |
OK |
C++14 (GCC 6-32) |
TESTS |
27 |
93 |
33075200 |
|
|
273658247 |
cqlcql |
D |
July 31, 2024, 4 a.m. |
OK |
C++14 (GCC 6-32) |
TESTS |
27 |
156 |
6451200 |
|
|
273640899 |
qifan_maker |
D |
July 31, 2024, 12:47 a.m. |
OK |
C++14 (GCC 6-32) |
TESTS |
27 |
171 |
12492800 |
|
|
273659898 |
WTXwang |
D |
July 31, 2024, 4:19 a.m. |
OK |
C++14 (GCC 6-32) |
TESTS |
27 |
186 |
13516800 |
|
|
273617596 |
Dmh_58 |
D |
July 30, 2024, 6:53 p.m. |
OK |
C++14 (GCC 6-32) |
TESTS |
27 |
186 |
22937600 |
|
|
273659467 |
nenu.chenqiyu |
D |
July 31, 2024, 4:14 a.m. |
OK |
C++14 (GCC 6-32) |
TESTS |
27 |
187 |
6656000 |
|
|
273662668 |
chenye3 |
D |
July 31, 2024, 4:50 a.m. |
OK |
C++14 (GCC 6-32) |
TESTS |
27 |
187 |
8908800 |
|
|
273641643 |
jjh0730 |
D |
July 31, 2024, 12:59 a.m. |
OK |
C++14 (GCC 6-32) |
TESTS |
27 |
187 |
8908800 |
|
|
273668243 |
vjudge.4 |
D |
July 31, 2024, 5:49 a.m. |
OK |
C++14 (GCC 6-32) |
TESTS |
27 |
187 |
16588800 |
|
|
273636085 |
luogu_bot5 |
D |
July 30, 2024, 11:29 p.m. |
OK |
C++14 (GCC 6-32) |
TESTS |
27 |
202 |
15360000 |
|
|
273601279 |
Eapp1e |
D |
July 30, 2024, 5:11 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
27 |
156 |
13107200 |
|
|
273635017 |
karamkontar |
D |
July 30, 2024, 11:03 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
27 |
171 |
15667200 |
|
|
273624208 |
Vaibhav_upadhyay |
D |
July 30, 2024, 7:59 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
27 |
171 |
15667200 |
|
|
273618194 |
NGPNAN |
D |
July 30, 2024, 6:58 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
27 |
171 |
15667200 |
|
|
273665883 |
rathee123 |
D |
July 31, 2024, 5:26 a.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
27 |
171 |
16486400 |
|
|
273603941 |
lucasschnee |
D |
July 30, 2024, 5:25 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
27 |
171 |
17305600 |
|
|
273654762 |
CUST_GIAO_ZE |
D |
July 31, 2024, 3:25 a.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
27 |
171 |
18739200 |
|
|
273656427 |
2nn300hu |
D |
July 31, 2024, 3:42 a.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
27 |
186 |
6144000 |
|
|
273635583 |
Nonbangkok |
D |
July 30, 2024, 11:18 p.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
27 |
186 |
12492800 |
|
|
273657336 |
yadav_ravi |
D |
July 31, 2024, 3:51 a.m. |
OK |
C++17 (GCC 7-32) |
TESTS |
27 |
186 |
16486400 |
|
|
273663794 |
Light_Cone |
D |
July 31, 2024, 5:03 a.m. |
OK |
C++20 (GCC 13-64) |
TESTS |
27 |
124 |
409600 |
|
|
273661327 |
ksss |
D |
July 31, 2024, 4:36 a.m. |
OK |
C++20 (GCC 13-64) |
TESTS |
27 |
124 |
8089600 |
|
|
273635960 |
Nanako7_ix |
D |
July 30, 2024, 11:27 p.m. |
OK |
C++20 (GCC 13-64) |
TESTS |
27 |
124 |
18329600 |
|
|
273647005 |
sheyigang |
D |
July 31, 2024, 2:07 a.m. |
OK |
C++20 (GCC 13-64) |
TESTS |
27 |
125 |
15667200 |
|
|
273640418 |
mikko_mikust |
D |
July 31, 2024, 12:36 a.m. |
OK |
C++20 (GCC 13-64) |
TESTS |
27 |
139 |
4505600 |
|
|
273649147 |
aa2315 |
D |
July 31, 2024, 2:30 a.m. |
OK |
C++20 (GCC 13-64) |
TESTS |
27 |
139 |
20480000 |
|
|
273666940 |
charlie_shotgun |
D |
July 31, 2024, 5:36 a.m. |
OK |
C++20 (GCC 13-64) |
TESTS |
27 |
140 |
4505600 |
|
|
273640871 |
mikko_mikust |
D |
July 31, 2024, 12:46 a.m. |
OK |
C++20 (GCC 13-64) |
TESTS |
27 |
140 |
4505600 |
|
|
273667178 |
JoanhLan |
D |
July 31, 2024, 5:39 a.m. |
OK |
C++20 (GCC 13-64) |
TESTS |
27 |
140 |
7475200 |
|
|
273640212 |
mikko_mikust |
D |
July 31, 2024, 12:32 a.m. |
OK |
C++20 (GCC 13-64) |
TESTS |
27 |
140 |
7782400 |
|
|
273613386 |
1435730 |
D |
July 30, 2024, 6:21 p.m. |
OK |
GNU C11 |
TESTS |
27 |
171 |
17817600 |
|
|
273600363 |
.teranikys |
D |
July 30, 2024, 5:07 p.m. |
OK |
Go |
TESTS |
27 |
280 |
48230400 |
|
|
273626394 |
AkshatSaxena_05 |
D |
July 30, 2024, 8:27 p.m. |
OK |
Java 21 |
TESTS |
27 |
453 |
73728000 |
|
|
273665989 |
codesbyayush |
D |
July 31, 2024, 5:27 a.m. |
OK |
Java 21 |
TESTS |
27 |
562 |
68096000 |
|
|
273617386 |
anshgrover25072002 |
D |
July 30, 2024, 6:51 p.m. |
OK |
Java 21 |
TESTS |
27 |
562 |
74956800 |
|
|
273661478 |
shashank_om |
D |
July 31, 2024, 4:37 a.m. |
OK |
Java 21 |
TESTS |
27 |
608 |
72908800 |
|
|
273620328 |
dzhi |
D |
July 30, 2024, 7:18 p.m. |
OK |
Java 21 |
TESTS |
27 |
609 |
37990400 |
|
|
273632569 |
ziad-abdou1 |
D |
July 30, 2024, 10:06 p.m. |
OK |
Java 21 |
TESTS |
27 |
624 |
57446400 |
|
|
273608548 |
amit_roy |
D |
July 30, 2024, 5:51 p.m. |
OK |
Java 21 |
TESTS |
27 |
640 |
74752000 |
|
|
273599534 |
testtest111ksjfrdvn |
D |
July 30, 2024, 5:03 p.m. |
OK |
Java 21 |
TESTS |
27 |
655 |
73830400 |
|
|
273623178 |
Kempachi |
D |
July 30, 2024, 7:48 p.m. |
OK |
Java 21 |
TESTS |
27 |
703 |
57139200 |
|
|
273608116 |
RockingMat |
D |
July 30, 2024, 5:49 p.m. |
OK |
Java 21 |
TESTS |
27 |
734 |
57036800 |
|
|
273623165 |
RahulAhuja2901 |
D |
July 30, 2024, 7:47 p.m. |
OK |
Java 8 |
TESTS |
27 |
358 |
47411200 |
|
|
273655083 |
yvbf |
D |
July 31, 2024, 3:28 a.m. |
OK |
Java 8 |
TESTS |
27 |
421 |
23244800 |
|
|
273615963 |
Sumitsingh7 |
D |
July 30, 2024, 6:40 p.m. |
OK |
Java 8 |
TESTS |
27 |
421 |
70144000 |
|
|
273660841 |
wooju.shi |
D |
July 31, 2024, 4:30 a.m. |
OK |
Java 8 |
TESTS |
27 |
452 |
40448000 |
|
|
273624802 |
yassjd199 |
D |
July 30, 2024, 8:07 p.m. |
OK |
Java 8 |
TESTS |
27 |
515 |
64204800 |
|
|
273626074 |
lanby |
D |
July 30, 2024, 8:23 p.m. |
OK |
Java 8 |
TESTS |
27 |
531 |
71475200 |
|
|
273603962 |
kasiru_69 |
D |
July 30, 2024, 5:25 p.m. |
OK |
Java 8 |
TESTS |
27 |
546 |
58777600 |
|
|
273603278 |
Fuck_paid_helpers |
D |
July 30, 2024, 5:21 p.m. |
OK |
Java 8 |
TESTS |
27 |
546 |
58777600 |
|
|
273601275 |
y_a_s |
D |
July 30, 2024, 5:11 p.m. |
OK |
Java 8 |
TESTS |
27 |
561 |
57344000 |
|
|
273617562 |
etian6795 |
D |
July 30, 2024, 6:53 p.m. |
OK |
Java 8 |
TESTS |
27 |
562 |
71065600 |
|
|
273658710 |
pulkit_garg14 |
D |
July 31, 2024, 4:05 a.m. |
OK |
PyPy 3 |
TESTS |
27 |
874 |
73113600 |
|
|
273605101 |
misaki646 |
D |
July 30, 2024, 5:31 p.m. |
OK |
PyPy 3-64 |
TESTS |
27 |
280 |
32460800 |
|
|
273622699 |
aayush249 |
D |
July 30, 2024, 7:42 p.m. |
OK |
PyPy 3-64 |
TESTS |
27 |
327 |
36966400 |
|
|
273644627 |
temporary-case |
D |
July 31, 2024, 1:39 a.m. |
OK |
PyPy 3-64 |
TESTS |
27 |
342 |
45260800 |
|
|
273613934 |
ibraheemkureshi |
D |
July 30, 2024, 6:25 p.m. |
OK |
PyPy 3-64 |
TESTS |
27 |
343 |
32153600 |
|
|
273605078 |
PhenomSG |
D |
July 30, 2024, 5:31 p.m. |
OK |
PyPy 3-64 |
TESTS |
27 |
343 |
50585600 |
|
|
273632096 |
cecilia.chan |
D |
July 30, 2024, 9:57 p.m. |
OK |
PyPy 3-64 |
TESTS |
27 |
358 |
62873600 |
|
|
273653040 |
hxu10 |
D |
July 31, 2024, 3:08 a.m. |
OK |
PyPy 3-64 |
TESTS |
27 |
421 |
47820800 |
|
|
273642443 |
cy171 |
D |
July 31, 2024, 1:11 a.m. |
OK |
PyPy 3-64 |
TESTS |
27 |
436 |
46592000 |
|
|
273606963 |
chenjiahui |
D |
July 30, 2024, 5:42 p.m. |
OK |
PyPy 3-64 |
TESTS |
27 |
483 |
46080000 |
|
|
273618708 |
rrb_01 |
D |
July 30, 2024, 7:02 p.m. |
OK |
PyPy 3-64 |
TESTS |
27 |
531 |
47411200 |
|
|
273640780 |
HiteshSomu |
D |
July 31, 2024, 12:44 a.m. |
OK |
Python 3 |
TESTS |
27 |
796 |
50380800 |
|
|
273639948 |
HiteshSomu |
D |
July 31, 2024, 12:27 a.m. |
OK |
Python 3 |
TESTS |
27 |
812 |
50380800 |
|
|
273623575 |
Om_Singh_ |
D |
July 30, 2024, 7:52 p.m. |
OK |
Python 3 |
TESTS |
27 |
1733 |
214732800 |
|
|
273652068 |
grecil |
D |
July 31, 2024, 2:59 a.m. |
OK |
Python 3 |
TESTS |
27 |
2312 |
248320000 |
|
|
273630773 |
PugachAG |
D |
July 30, 2024, 9:33 p.m. |
OK |
Rust 2021 |
TESTS |
27 |
124 |
32460800 |
|
|
273635211 |
aviral311 |
D |
July 30, 2024, 11:08 p.m. |
OK |
Rust 2021 |
TESTS |
27 |
139 |
43110400 |
|
|
273627097 |
PaciukZvichainyi |
D |
July 30, 2024, 8:37 p.m. |
OK |
Rust 2021 |
TESTS |
27 |
187 |
32460800 |
|
|
273624033 |
PaciukZvichainyi |
D |
July 30, 2024, 7:57 p.m. |
OK |
Rust 2021 |
TESTS |
27 |
437 |
32460800 |
|
|
remove filters
Back to search problems