Codeforces Round 1118 (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
2258 Codeforces Round 1118 (Div. 2) FINISHED False 7200 141884 Aug. 29, 2026, 2:35 p.m.

Problems

Solved
Index
Name
Type
Tags
Community Tag
Rating
( 83 ) F Plus Minus Tree PROGRAMMING dfs and similar dp graphs trees

You are given a tree (T) with (n) vertices, rooted(^{\text{∗}}) at vertex (1). Each vertex (v) is assigned an initial weight (a_v \in \{ -1, 0, +1 \}). For each vertex with weight (0), you have to assign a new weight, either (-1) or (+1). Let (x_v) be the final weight of vertex (v). For vertex (v), let (T_v) be the set of nodes in the subtree(^{\text{†}}) of vertex (v). The cost of vertex (v) is defined as () S_v = \left| \sum_{u \in T_v}{x_u} \right|.() The total cost of the tree is defined as the sum of costs over all vertices. Find the minimum possible total cost of the tree after assigning the weights of all vertices with zero initial weight. (^{\text{∗}})A tree is a connected graph without cycles. A rooted tree is a tree where one vertex is special and called the root. (^{\text{†}})A subtree of vertex (v) is the subgraph consisting of (v), all its descendants, and all the edges between them. A descendant of vertex (v) is any vertex (u) for which (v) is an ancestor. No vertex is its own descendant. An ancestor of vertex (v) is any vertex on the simple path from (v) to the root, including the root, but not including (v). The root has no ancestors. 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 (n) ((2 \le n \le 2 \cdot 10^5)), denoting the number of vertices in the tree. The second line of each test case contains (n) integers (a_1, a_2, \ldots, a_n) ((-1 \le a_i \le 1)). Each of the next (n-1) lines contains two integers (u) and (v) ((1 \le u, v \le n)), denoting the nodes connected with an edge of the tree. 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 $$$2 \cdot 10^

Tutorials

Codeforces Round 1118 (Div. 2) Editorial

Submissions

Submission Id
Author(s)
Index
Submitted
Verdict
Language
Test Set
Tests Passed
Time taken (ms)
Memory Consumed (bytes)
Tags
Rating
388852421 DeepThinkSlavek F Aug. 29, 2026, 7:29 p.m. OK C++17 (GCC 7-32) TESTS 54 296 26419200
388851969 LuckyLittleLuis F Aug. 29, 2026, 7:22 p.m. OK C++17 (GCC 7-32) TESTS 54 390 26521600
388827011 arpitagupta F Aug. 29, 2026, 4:13 p.m. OK C++17 (GCC 7-32) TESTS 54 421 17715200
388860607 filipinascodex F Aug. 29, 2026, 10:51 p.m. OK C++17 (GCC 7-32) TESTS 54 437 20070400
388828013 astosm F Aug. 29, 2026, 4:16 p.m. OK C++17 (GCC 7-32) TESTS 54 531 22937600
388850625 Rupak2021331072 F Aug. 29, 2026, 7:01 p.m. OK C++17 (GCC 7-32) TESTS 54 546 19353600
388873630 hrd1k31 F Aug. 30, 2026, 5:35 a.m. OK C++17 (GCC 7-32) TESTS 54 546 19660800
388832172 ShadowSlayer1 F Aug. 29, 2026, 4:28 p.m. OK C++17 (GCC 7-32) TESTS 54 625 25702400
388874330 thrdeye F Aug. 30, 2026, 5:47 a.m. OK C++17 (GCC 7-32) TESTS 54 625 32460800
388829386 BTinn F Aug. 29, 2026, 4:20 p.m. OK C++17 (GCC 7-32) TESTS 54 734 201113600
388834627 hexahydride F Aug. 29, 2026, 4:34 p.m. OK C++20 (GCC 13-64) TESTS 54 328 65228800
388842632 Tuli2024 F Aug. 29, 2026, 5:51 p.m. OK C++20 (GCC 13-64) TESTS 54 343 9011200
388843801 dadadadadadada F Aug. 29, 2026, 6:04 p.m. OK C++20 (GCC 13-64) TESTS 54 343 45260800
388840163 kenapa F Aug. 29, 2026, 5:27 p.m. OK C++20 (GCC 13-64) TESTS 54 390 24371200
388831206 misstia F Aug. 29, 2026, 4:26 p.m. OK C++20 (GCC 13-64) TESTS 54 484 23756800
388874116 gopal.thecoder F Aug. 30, 2026, 5:43 a.m. OK C++20 (GCC 13-64) TESTS 54 578 12902400
388832774 himanshu__khundia F Aug. 29, 2026, 4:30 p.m. OK C++20 (GCC 13-64) TESTS 54 609 17408000
388831275 36JACK F Aug. 29, 2026, 4:26 p.m. OK C++23 (GCC 14-64, msys2) TESTS 54 281 40345600
388863604 Gold_Dino F Aug. 30, 2026, 1:12 a.m. OK C++23 (GCC 14-64, msys2) TESTS 54 281 65843200
388864844 Gold_Dino F Aug. 30, 2026, 2 a.m. OK C++23 (GCC 14-64, msys2) TESTS 54 312 53862400
388840167 hkjakhar04 F Aug. 29, 2026, 5:27 p.m. OK C++23 (GCC 14-64, msys2) TESTS 54 312 54784000
388869359 ramumali2002 F Aug. 30, 2026, 4:11 a.m. OK C++23 (GCC 14-64, msys2) TESTS 54 343 9113600
388854123 exit101 F Aug. 29, 2026, 7:58 p.m. OK C++23 (GCC 14-64, msys2) TESTS 54 359 23040000
388841349 Tuli2024 F Aug. 29, 2026, 5:38 p.m. OK C++23 (GCC 14-64, msys2) TESTS 54 375 9113600
388837484 __Ahmed__Ashraf__ F Aug. 29, 2026, 5:01 p.m. OK C++23 (GCC 14-64, msys2) TESTS 54 375 21299200
388842602 djyqjy F Aug. 29, 2026, 5:51 p.m. OK C++23 (GCC 14-64, msys2) TESTS 54 390 197836800
388844614 ttamx F Aug. 29, 2026, 6:13 p.m. OK C++23 (GCC 14-64, msys2) TESTS 54 406 42598400
388832798 mo_ya1 F Aug. 29, 2026, 4:30 p.m. OK GNU C11 TESTS 54 359 24166400

remove filters

Back to search problems