Automatic Distribution (APCS 2020-01 Expert)

2.0s 256M

A packing device is a binary tree rooted at vertex \(1\). Its \(n\) container leaves are numbered \(n\) through \(2n-1\); internal splitters are numbered \(1\) through \(n-1\), each with a left and a right child. Containers initially hold given weights. Send \(m\) items through the device in order. At each splitter, an item enters the child subtree with the smaller current total container weight, choosing the left child on a tie. Add the item weight to the container it reaches before sending the next item. Report each destination container.

Input Format

The first line contains \(n,m\). The second gives initial container weights in increasing container order. The third gives item weights in arrival order. Each of the next \(n-1\) lines contains a splitter index, its left child, and its right child. These lines may appear in any order.

Output Format

Print the destination container indices in arrival order, separated by spaces.

Constraints

\(1\le n\le100000\), \(1\le m\le100\). Initial weights are nonnegative integers and item weights are positive integers. Their combined total is at most \(10^9\). The input describes a valid tree of the stated form.

Sample Input 1

4 5
0 0 0 0
5 3 4 2 1
1 2 3
2 4 5
3 6 7

Sample Output 1

4 6 7 5 5

Sample Input 2

7 2
9 2 1 6 8 7 5
2 3
1 2 5
2 3 7
3 13 10
4 11 9
6 12 8
5 6 4

Sample Output 2

8 7

Source

APCS 2020-01 public archive version: tcirc:d105

Problem page help

Keyboard shortcuts

Main features

  • Sample tests — Runs the sample cases bundled with the problem and auto-compares against the expected output.
  • Custom test — Run your code with your own stdin. Optionally tick the "Compare with expected (diff)" box to verify against expected output line-by-line.
  • Template — Paste the default code template you set on your profile page.
  • Collab — Edit this problem together with classmates in real time.
  • Auto-draft — Editor contents auto-save to your browser every 1.5 seconds (per account / problem / language).
  • Submit — Send your code to the judge for grading; returns AC / WA / TLE etc.

Limits

  • Source code: at most 65,536 characters
  • Custom test stdin and expected output: at most 1 MB each (≈1 million characters)
  • Custom test and sample test share the sandbox; about 1 request per 3 s per user (sample test: 1 per 1 s)
  • Custom test and sample test both have a 15 second wall-clock cap (the official judge still uses the problem time limit)
  • Interactive problems do not offer custom test (cannot simulate interaction with the judge).
  • Submitting has no rate limit, but rapid repeated submissions on the same problem are treated as score farming.