Official

F - Two Unbalanced Subtrees Editorial by evima


Let $r, a, b$ be the integer written on vertex $i$, the sum of the integers written on the vertices of the subtree rooted at vertex $2i$, and the sum of the integers written on the vertices of the subtree rooted at vertex $2i + 1$, respectively. Here, since $r = |a - b|$, we have $\max(a, b) = \min(a, b) + r$, so $r + a + b = 2 \max(a, b)$. In particular, $r + a + b$ is the sum of the integers written on the vertices of the subtree rooted at vertex $i$.
From this, it can be proved by induction on $k$ that the sum of the integers written on a subtree of size $2^k - 1$ is a multiple of $2^{k - 1}$.

By considering the case $k = N - 1$, the integer written on vertex $1$ is a multiple of $2^{N - 2}$. Thus, the minimum possible integer written on vertex $1$ is at least $2^{N - 2}$. This lower bound can be achieved by the following recursive construction.

For $N = 2$, use $(1, 2, 3)$.
For $N \geq 3$, consider taking the top $N - 2$ levels of the writing for $N - 1$, multiplied by $2$, as the top $N - 2$ levels of the writing for $N$. Here, $2, 6, \cdots, 2^{N - 1} - 2$ are written on the $(N - 2)$-th level.
We impose the constraint that $1, 3, \cdots, 2^{N - 1} - 1$ are written on the $(N - 1)$-th level from the top, and $2^{N - 1}, 2^{N - 1} + 1, \cdots, 2^N - 1$ are written on the $N$-th level from the top. By matching $2^{N - 1}, 2^{N - 1} + 1, \cdots, 2^N - 1$ by repeatedly removing the left end and the right end, we can make $1, 3, \cdots, 2^{N - 1} - 1$ on the $(N - 1)$-th level. By matching these in the same way, we can make $2, 6, \cdots, 2^{N - 1} - 2$ on the $(N - 2)$-th level.
The construction obtained in this way satisfies the condition.

By implementing as in this discussion, it can be solved in, for example, $O(2^N)$. For reference, an example for $N = 4$ is attached below. On the left and right of each vertex are shown the integer written on that vertex and the sum of the integers written on the vertices of the subtree rooted at that vertex, respectively.

Sample Implementation (C++)