#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
long long x, n, a[100004], sum[100004];
set<pair<long long, long long> > s;
multiset<long long, greater<long long> > ss;
int main() {
  scanf("%I64d", &n);
  for (long long i = 1; i <= n; i++) {
    scanf("%I64d", &a[i]);
    sum[i] = sum[i - 1] + a[i];
  }
  s.insert(make_pair(1, n));
  s.insert(make_pair(n + 1, n + 1));
  ss.insert(sum[n]);
  for (int i = 1; i <= n; i++) {
    scanf("%I64d", &x);
    auto it = s.lower_bound(make_pair(x + 1, 0));
    it--;
    if (x - 1 >= (*it).first) {
      s.insert(make_pair((*it).first, x - 1));
      ss.insert(sum[x - 1] - sum[(*it).first - 1]);
    }
    if (x + 1 <= (*it).second) {
      s.insert(make_pair(x + 1, (*it).second));
      ss.insert(sum[(*it).second] - sum[x]);
    }
    ss.erase(ss.find(sum[(*it).second] - sum[(*it).first - 1]));
    s.erase(it);
    printf("%I64d\n", *ss.begin());
  }
}
