Codeforces Round 1087 (Div. 2)

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.

ContestId
Name
Phase
Frozen
Duration (Seconds)
Relative Time
Start Time
2209 Codeforces Round 1087 (Div. 2) FINISHED False 7200 2301923 March 21, 2026, 2:35 p.m.

Problems

Solved
Index
Name
Type
Tags
Community Tag
Rating
( 389 ) F Dynamic Values And Maximum Sum PROGRAMMING data structures greedy implementation trees

You are given a tree(^{\text{∗}}) with (n) vertices numbered from 1 to (n), where each vertex (i) has an initial value (a_i). You perform (k) operations. The total is (0) initially. In each operation: Choose a vertex (r) and root the tree at (r). Add the current value of (r) to the total and set the value of (r) to (0). For each vertex (u) that is not a leaf(^{\text{†}}), find the leaves in the subtree(^{\text{‡}}) of (u) that are at the maximum distance from (u). Among them, select the one with the smallest index, denoted as the destination for (u). Add the current value of (u) to the destination and set the value of (u) to (0). (^{\text{∗}})A tree is a connected graph without cycles. (^{\text{†}})A leaf is any vertex without children. (^{\text{‡}})A subtree of vertex (v) is the subgraph of (v), all its descendants, and all the edges between them. Each test contains multiple test cases. The first line contains the number of test cases (t) ((1 \le t \le 10 ^ 4)). The description of the test cases follows. The first line of each test case contains two integers (n) and (k) ((1 \le k \le n \le 3 \cdot 10^5)). The second line contains (n) integers (a_1, a_2, \ldots, a_n) ((1 \le a_i \le 10^9)) — the initial values of vertices. Each of the following (n-1) lines contains two integers (u) and (v), denoting an edge connecting vertex (u) and (v). It is guaranteed that the given edges form a tree. It is guaranteed that the sum of (n) over all test cases does not exceed (3\cdot 10 ^ 5). For each test case, output an integer — the maximum possible total. In the first test case: Choose vertex (1) as the root. Add (a_1 = 19) to the total and set (a_1 = 0). After rooting the tree at (1), for every non-leaf vertex (u), move its value to the leaf in its subtree that is farthest from (u)

Tutorials

Codeforces Round 1087 (Div. 2) Editorial

Submissions

Submission Id
Author(s)
Index
Submitted
Verdict
Language
Test Set
Tests Passed
Time taken (ms)
Memory Consumed (bytes)
Tags
Rating
367725094 nullbrain_ F March 22, 2026, 3:41 a.m. OK C# 13 TESTS 20 4328 61644800
367724193 gopal.thecoder F March 22, 2026, 3:23 a.m. OK C++17 (GCC 7-32) TESTS 20 1312 37990400
367707116 aarav1118122 F March 21, 2026, 8:29 p.m. OK C++17 (GCC 7-32) TESTS 20 1328 45158400
367719024 wwqq88 F March 22, 2026, 1:19 a.m. OK C++17 (GCC 7-32) TESTS 20 1390 70963200
367688295 Lakomoon F March 21, 2026, 5:20 p.m. OK C++17 (GCC 7-32) TESTS 20 1406 36659200
367680174 rlakkh F March 21, 2026, 4:25 p.m. OK C++17 (GCC 7-32) TESTS 20 1453 62873600
367681394 _annhien_ruby22 F March 21, 2026, 4:28 p.m. OK C++17 (GCC 7-32) TESTS 20 1500 42086400
367682524 Khai2007 F March 21, 2026, 4:30 p.m. OK C++17 (GCC 7-32) TESTS 20 1500 104243200
367683082 Daniyal08 F March 21, 2026, 4:31 p.m. OK C++17 (GCC 7-32) TESTS 20 1515 67584000
367697192 Beast_Coder123 F March 21, 2026, 6:27 p.m. OK C++17 (GCC 7-32) TESTS 20 1562 86528000
367677630 potato167 F March 21, 2026, 4:20 p.m. OK C++17 (GCC 7-32) TESTS 20 1671 19353600
367731050 hshhh_ F March 22, 2026, 5:22 a.m. OK C++20 (GCC 13-64) TESTS 20 796 76800000
367731148 hshhh_ F March 22, 2026, 5:24 a.m. OK C++20 (GCC 13-64) TESTS 20 796 101068800
367710420 -Shu- F March 21, 2026, 9:35 p.m. OK C++20 (GCC 13-64) TESTS 20 1015 39321600
367730168 yuxiaoyi F March 22, 2026, 5:10 a.m. OK C++20 (GCC 13-64) TESTS 20 1125 66662400
367726171 Jelefy F March 22, 2026, 4:02 a.m. OK C++20 (GCC 13-64) TESTS 20 1125 66764800
367720607 gnwfgg F March 22, 2026, 2:04 a.m. OK C++20 (GCC 13-64) TESTS 20 1265 52224000
367721259 Acmer_ F March 22, 2026, 2:19 a.m. OK C++20 (GCC 13-64) TESTS 20 1265 72089600
367706995 pupilemus F March 21, 2026, 8:27 p.m. OK C++20 (GCC 13-64) TESTS 20 1296 71577600
367696684 SeaYellow F March 21, 2026, 6:23 p.m. OK C++20 (GCC 13-64) TESTS 20 1453 51814400
367681059 cipher404 F March 21, 2026, 4:27 p.m. OK C++20 (GCC 13-64) TESTS 20 1468 67481600
367692490 bonopo F March 21, 2026, 5:47 p.m. OK C++23 (GCC 14-64, msys2) TESTS 20 625 117043200
367685065 luckym_123 F March 21, 2026, 4:34 p.m. OK C++23 (GCC 14-64, msys2) TESTS 20 671 50688000
367722206 WatPz F March 22, 2026, 2:41 a.m. OK C++23 (GCC 14-64, msys2) TESTS 20 718 65843200
367688185 Kude F March 21, 2026, 5:19 p.m. OK C++23 (GCC 14-64, msys2) TESTS 20 1031 118579200
367706880 golomb F March 21, 2026, 8:25 p.m. OK C++23 (GCC 14-64, msys2) TESTS 20 1062 41472000
367691822 OdtreeKing F March 21, 2026, 5:42 p.m. OK C++23 (GCC 14-64, msys2) TESTS 20 1109 235008000
367720225 Yam F March 22, 2026, 1:53 a.m. OK C++23 (GCC 14-64, msys2) TESTS 20 1140 103219200
367726810 jiangly F March 22, 2026, 4:15 a.m. OK C++23 (GCC 14-64, msys2) TESTS 20 1140 159948800
367707369 Teto F March 21, 2026, 8:33 p.m. OK C++23 (GCC 14-64, msys2) TESTS 20 1187 71680000
367722822 hachiko2026 F March 22, 2026, 2:54 a.m. OK C++23 (GCC 14-64, msys2) TESTS 20 1218 36454400
367732277 hardgonnapass F March 22, 2026, 5:38 a.m. OK F# 9 TESTS 20 1078 75161600
367711530 abezr882 F March 21, 2026, 9:55 p.m. OK Go TESTS 20 1156 106598400
367684891 bugfeature F March 21, 2026, 4:34 p.m. OK Go TESTS 20 1562 76288000
367717842 0x3F F March 22, 2026, 12:45 a.m. OK Go TESTS 20 1718 198246400
367688226 jdltc F March 21, 2026, 5:19 p.m. OK Java 21 TESTS 20 2750 48025600
367678115 pieande F March 21, 2026, 4:21 p.m. OK Java 21 TESTS 20 3968 75161600
367677752 sameer_ssr99 F March 21, 2026, 4:20 p.m. OK Java 8 TESTS 20 1718 44134400
367709115 EllisAkhtar F March 21, 2026, 9:06 p.m. OK PyPy 3-64 TESTS 20 2671 239923200
367677261 Evg_MaxProDota F March 21, 2026, 4:19 p.m. OK PyPy 3-64 TESTS 20 4140 168243200
367726754 sleepisweak F March 22, 2026, 4:13 a.m. OK Rust 2024 TESTS 20 812 92160000
367679286 Ravnik F March 21, 2026, 4:23 p.m. OK Rust 2024 TESTS 20 1515 133222400

remove filters

Back to search problems