Editorial
Let be the initial position of child .
Suppose a child is currently at position and has candies. In one operation, the leftmost chosen child moves two positions to the right and receives one candy, while each of the other two children moves one position to the left and receives one candy. Therefore, for every child, is invariant.
The three children chosen in an operation have the same number of candies. Hence the residues modulo of their initial positions are all different. Therefore, two children whose initial positions have the same residue modulo can never change their relative order.
In particular, the children initially at positions and must appear in the same relative order in the final arrangement. Since the final arrangement is increasing by child number, must hold for every . If this condition fails, the answer is .
Assume from now on that this condition holds.
In the final arrangement, child must be at position . If this child initially stood at position and ends with candies, the invariant gives .
Now ignore the identities of the children and look only at the number of candies at each position. If three consecutive positions all contain children with candies, one operation changes their values from to . Thus, an operation can be viewed as placing a horizontal block of length on a bar chart.
Suppose the final candy counts are . For any positive integer , consider the positions satisfying . Every maximal consecutive segment of such positions must have length divisible by , because one layer can only be formed by blocks of length .
This condition is also sufficient. Process the layers from bottom to top. For each layer , split every maximal segment with height at least into blocks of three consecutive positions and perform one operation on each block. Immediately before constructing layer , the three selected positions all have exactly candies, so every operation is valid.
Therefore, it remains to choose such that for every , while every layer consists of consecutive segments whose lengths are divisible by . Since every operation increases the total number of candies by exactly , the number of operations is .
Determine the values from left to right. For every height , let be the length modulo of the last currently open consecutive segment on layer .
If , the current segment on layer has not been completed yet. Therefore, the next position must also have height at least . Let be the largest such that . If no such exists, let .
The next value must be at least , and it must satisfy . Choose the smallest integer satisfying both conditions.
This greedy choice is optimal. Choosing a value below is impossible because it would terminate an unfinished segment. Any larger value with the same residue differs by at least . Those additional three layers do not help complete any existing unfinished layer; they only start new unfinished segments. Hence there is never a benefit in choosing a larger value.
After choosing , the last segment on every layer from through is extended by one position. Therefore, add modulo to .
After all positions have been processed, if some remains, then the last segment on that layer has length not divisible by , so the target is impossible. Otherwise, the heights chosen by the greedy algorithm are optimal.
We need two operations on the sequence : add modulo to a prefix , and find the largest such that . Both can be supported with a lazy segment tree.
For each node, store a -bit mask describing which values among occur in that interval. Bit is set if some exists in the interval. Adding modulo maps the states to , so the mask can be updated by a cyclic rotation of its three bits.
To find the largest with , descend from the root while checking the right child first and choosing a child whose mask contains state or .
Each new is at most , so it is sufficient to maintain heights only up to .
The time complexity is , and the memory complexity is .
Solution written by GPT5.6