#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
int n, a[N + 1];
long long s[N + 1];
long long getsum(int l, int r) { return l > r ? 0 : s[r] - s[l - 1]; }
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i], s[i] = s[i - 1] + a[i];
  multiset<long long, greater<long long> > sval;
  set<int> spos;
  spos.insert(0);
  spos.insert(n + 1);
  sval.insert(getsum(1, n));
  set<int>::iterator ip1, ip2;
  multiset<long long>::iterator iv;
  for (int i = 0, x; i < n; ++i) {
    cin >> x;
    ip2 = spos.lower_bound(x);
    ip1 = ip2;
    --ip1;
    long long tmp = getsum(*ip1 + 1, *ip2 - 1);
    iv = sval.lower_bound(tmp);
    sval.erase(iv);
    spos.insert(x);
    sval.insert(getsum(*ip1 + 1, x - 1));
    sval.insert(getsum(x + 1, *ip2 - 1));
    cout << *sval.begin() << '\n';
  }
}
