First, if ci is greater than the size of the subtree of node i, there’s no answer.
For each node we build an array containing all the nodes from the its subtree bottom-up, and these nodes are in ascending order of value written on them (i.e. ai) even though we don’t know the exact value for now. We only care about their relative relationship. The next question is how to combine all the arrays of the children. The answer is quite simple: we can simply glue then together since each subtree is independent. The last step is to put the node in the array. Since we already know ci, so i should be put in the ci-th position of the array.
Now we have that array containing all the nodes. Let’s call it order. We can assign 1,2,3,… to order1,order2,order3,….