您的位置:首页 > 其它

CodeForces - 722C Destroying Array (set)

2017-01-23 14:32 363 查看
题意:

        给出一个长度为 N 的数串,每次从数串中删去一个数,问每次操作后数串中最大连续和是多少?

思路:

        利用 set 维护 断点集 O(logn)

        利用 multiset 维护 线段长度集 O(logn)

        利用 从数串头开始累加计算的区间和,来便于求出任意区间和 O(1)

        在 添加断点时不断维护 断点集,线段长度集即可。

代码:

#include <bits/stdc++.h>

using namespace std;
#define LL long long
const int MAXN=1e5;
multiset <LL> ans;
multiset <LL>::iterator itt;
set <LL> cut;
set <LL>::iterator it;
int n,x,st,en;
LL sum[MAXN+1000],a[MAXN+1000];
void ini(){
memset(sum,0,sizeof(sum));
for(int i=1;i<=n;i++)//........................................计算从头开始到每个 i 的区间和
sum[i]=a[i]+sum[i-1];
ans.clear();cut.clear();
cut.insert(0);cut.insert(n+1);//...............................初始断点 0 和 n + 1
ans.insert(sum
);//..........................................初始线段长为 sum

}
int main()
{
ios::sync_with_stdio(false);
while(cin>>n){
for(int i=1;i<=n;i++)
cin>>a[i];
ini();
while(n--){
cin>>x;
it=cut.upper_bound(x);//...............................查找断点并维护
en=*it;
st=*(--it);
itt=ans.find(sum[en-1]-sum[st]);
ans.erase(itt);//......................................删除旧线段
ans.insert(sum[en-1]-sum[x]);//........................添加新线段
ans.insert(sum[x-1]-sum[st]);
cut.insert(x);//.......................................添加新断点
itt=ans.end();//.......................................multiset 自排序
cout<<*(--itt)<<endl;
}
}
}


You are given an array consisting of n non-negative integers a1, a2, ..., an.

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 a1, a2, ..., an (0 ≤ ai ≤ 109).

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.

Example

Input
4
1 3 2 5
3 4 1 2


Output
5
4
3
0


Input
5
1 2 3 4 5
4 2 3 5 1


Output
6
5
5
1
0


Input
8
5 5 4 4 6 6 5 5
5 2 8 7 1 3 4 6


Output
18
16
11
8
8
6
6
0


Note

Consider the first sample:

Third element is destroyed. Array is now 1 3  *  5. Segment with maximum sum 5consists of one integer 5.
Fourth element is destroyed. Array is now 1 3  *   * . Segment with maximum sum4 consists of two integers 1 3.
First element is destroyed. Array is now  *  3  *   * . Segment with maximum sum3 consists of one integer 3.
Last element is destroyed. At this moment there are no valid nonempty segments left in this array, so the answer is equal to 0.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: