#include <bits/stdc++.h>   using namespace std;   set<long long> s;   map<long long, long long> m;   vector<long long> v;   long long n, a[100010], b[100010], dp[100010], mx = 0;   bool mark[100010];   int main() {     ios_base::sync_with_stdio(false);     cin >> n;     for (long long i = 1; i <= n; i++) {       cin >> a[i];       if (i == 1)         dp[i] = a[i];       else         dp[i] = dp[i - 1] + a[i];     }     for (long long i = 1; i <= n; i++) cin >> b[i];     for (long long i = n; i > 0; i--) {       v.push_back(mx);       long long r, l;       mark[b[i]] = 1;       if (b[i] < n) {         if (mark[b[i] + 1] == 1) {           set<long long>::iterator it = s.lower_bound(b[i]);           r = *it;         } else           r = b[i];       } else         r = b[i];       s.insert(r);       if (b[i] > 1) {         if (mark[b[i] - 1] == 1) {           s.erase(b[i] - 1);         }       }       m[r] = m[b[i] - 1] + dp[r] - dp[b[i] - 1];       if (m[r] > mx) mx = m[r];     }     for (long long i = v.size() - 1; i >= 0; i--) cout << v[i] << endl;     return 0;   }   