Interesting Drug

아직 제출이 없습니다시간 제한1초메모리 제한1024 MB

문제

During Younghun's trip to IOI in Japan, he was nervous with the upcoming competition. So, he went out for nearby playground to relax himself. In the playground, there was NN drugs lying in a straight line. The drugs are numbered from 11 to NN from left to right, and their locations are distinct. Younghun was curious about the drug, and swallowed one of them.

After then, due to the effect of drug, Younghun started to dance unconsciously. Younghun was unable to control himself from dancing, but was only able to control the direction he was moving (left or right in the straight line). When Younghun is moving, he automatically swallows any drug in his way. After swallowing the drug, the drug disappears from the playground.

In the end, Younghun swallowed all NN drugs, and found his consciousness. He was not shocked about what he've done (just like all great K-pop stars, dancing is his life), but was worried about the potential health risks of drugs. Let P=P_1,P_2,,P_nP = \\{P\_1, P\_2, \cdots, P\_n\\} be the sequence of drugs in order of Younghun's consumption. After some research, Younghun realized that he receives D_iD\_i damage if P_C_i=iP\_{C\_i} = i.

Write a program which calculates the maximum possible damage dealt by starting at drug ii. In other words, for each 1iN1 \leq i \leq N, you should calculate the maximum possible damage among all possible sequence of consumption with P_1=iP\_1 = i.

입력

The first line of the input contains a single integer NN, denoting the number of drugs. (2N300,0002 \leq N \leq 300\\,000)

The next line contains NN space-separated integer C_1,C_2,,C_NC\_1, C\_2, \cdots, C\_N, which is the sequence CC as explained above. (1C_iN1 \leq C\_i \leq N)

The last line contains NN space-separated integer D_1,D_2,,D_ND\_1, D\_2, \cdots, D\_N, which is the possible damage dealt by drug ii. (1D_i1091 \leq D\_i \leq 10^9)

출력

You should print a single line containing NN space-separated integer: The ii-th number should be the maxmum possibly damage dealt by starting at drug ii.

힌트

If i=1i = 1, Younghun can only move right. This gives P=1,2,3,4,5P = \\{1, 2, 3, 4, 5\\}, which deals damage 1+4=51 + 4 = 5.

If i=2i = 2, Younghun can move left, right, right, right. This gives P=2,1,3,4,5P = \\{2, 1, 3, 4, 5\\}, which deals damage 11.

If i=3i = 3, Younghun can move right, right, left, left. This gives P=3,4,5,2,1P = \\{3, 4, 5, 2, 1\\}, which deals damage 1010.

If i=4i = 4, Younghun can move left, right, left, left. This gives P=4,3,5,2,1P = \\{4, 3, 5, 2, 1\\}, which deals damage 2+10=122 + 10 = 12.

If i=5i = 5, Younghun can only move left. This gives P=5,4,3,2,1P = \\{5, 4, 3, 2, 1\\}, which deals damage 11.