Intel Code Challenge Elimination Round (Div. 1 + Div. 2, combined)
C
Destroying Array
Codeforces
1600
原题
← BD →
翻译中…

You are given an array consisting of $*n*$ non-negative integers $*a*_{1}, *a*_{2}, ..., *a*_{*n*}$.

翻译中…

You are going to destroy integers in the array one by one. Thus, you are given the permutation of integers from $1$ to $*n*$ defining the order elements of the array are destroyed.

翻译中…

After each element is destroyed you have to find out the segment of the array, such that it contains no destroyed elements and the sum of its elements is maximum possible. The sum of elements in the empty segment is considered to be $0$.

翻译中…

Input

翻译中…

The first line of the input contains a single integer $*n*$ ($1 ≤ *n* ≤ 100 000$) — the length of the array.

翻译中…

The second line contains $*n*$ integers $*a*_{1}, *a*_{2}, ..., *a*_{*n*}$ ($0 ≤ *a*_{*i*} ≤ 10^{9}$).

翻译中…

The third line contains a permutation of integers from $1$ to $*n*$ — the order used to destroy elements.

翻译中…

Output

翻译中…

Print $*n*$ lines. The $*i*$-th line should contain a single integer — the maximum possible sum of elements on the segment containing no destroyed elements, after first $*i*$ operations are performed.

翻译中…

Note

翻译中…

Consider the first sample:

翻译中…
  1. Third element is destroyed. Array is now $1 3 * 5$. Segment with maximum sum $5$ consists of one integer $5$.
  2. Fourth element is destroyed. Array is now $1 3 * *$. Segment with maximum sum $4$ consists of two integers $1 3$.
  3. First element is destroyed. Array is now $* 3 * *$. Segment with maximum sum $3$ consists of one integer $3$.
  4. Last element is destroyed. At this moment there are no valid nonempty segments left in this array, so the answer is equal to $0$.
翻译中…

样例

Input #1
4
1 3 2 5
3 4 1 2
Output #1
5
4
3
0
InputCopy OutputCopy
Input #2
5
1 2 3 4 5
4 2 3 5 1
Output #2
6
5
5
1
0
InputCopy OutputCopy
Input #3
8
5 5 4 4 6 6 5 5
5 2 8 7 1 3 4 6
Output #3
18
16
11
8
8
6
6
0
InputCopy OutputCopy
题目信息
题号
722C
时间限制
1 s
内存限制
256 MB
标签
数据结构
并查集

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。