写代码一定要规范,不然很多玄学错误,你都搞不懂...2333

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int a[N],fa[N],b[N];
long long sum[N];

int f(int x)
{
    if(fa[x]==x) return x;
    else         return fa[x]=f(fa[x]);
}

vector<long long>ans;

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) scanf("%d",&b[i]);
    long long pos=0;
    ans.push_back(pos);
    for(int i=n;i>1;i--)
    {
        int x=b[i];
        sum[x]=a[x];fa[x]=x;
        if(fa[x-1])
        {
            int fy=f(x-1);
            if(x!=fy)
            {
                sum[x]+=sum[fy];
                fa[fy]=x;
            }
        }
        if(fa[x+1])
        {
            int fy=f(x+1);
            if(x!=fy)
            {
                sum[x]+=sum[fy];
                fa[fy]=x;
            }
        }
        pos=max(pos,sum[x]);
        ans.push_back(pos);
    }
    for(int i=ans.size()-1;i>=0;i--) printf("%lld\n",ans[i]);
    return 0;
}