The D. E. Shaw GroupOA #12
Tree Points
Estimated HardJudge-ready
Topics (AI-labelled)Tree & Game DPTree Traversal
You are given an undirected tree rooted at node 0 and a positive integer A[u] for each node. Starting from the root, a node can be processed only after its parent has been collected. When processing a node, you can either collect its current value minus K points, or collect its current value integer-divided by 2 points and integer-divide by 2 the values of that node and of every unprocessed node in its whole subtree. A single step may have a negative gain. Maximize the total points.
The standard input/output, the ranges added by this site, and the examples follow this problem's description.
Input
The first line contains n K (1≤n≤100000, 1≤K≤10^9); the second line contains n values A[u] (1≤A[u]≤10^9); the next n−1 lines are 0-based undirected edges u v. The remaining tree constraints are taken from the original problem.
Output
Output the maximum total points obtainable, using 64-bit integers.
Example 1
Input:
4 5
10 10 3 3
0 1
1 2
2 3
Output:
11
Example 2
Input:
1 5
3
Output:
1
Example 3
Input:
3 3
8 4 2
0 1
1 2
Output:
7
Editorial
Loading solution…