#include <bits/stdc++.h>
using namespace std;
void my_dbg() { cout << endl; }
template <typename Arg, typename... Args>
void my_dbg(Arg A, Args... B) {
  cout << ' ' << A;
  my_dbg(B...);
}
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 1e9 + 7;
const int maxN = 2e5 + 2;
struct item {
  ll seg, pref, suf, sum;
};
const ll NEUTRAL_ELEMENT = -1 * (ll)1e16;
vector<item> sums(4 * maxN);
vi a(maxN);
int n;
item single(ll v) {
  if (v > 0) return {v, v, v, v};
  return {0, 0, 0, v};
}
item merge(item a, item b) {
  return {max({0LL, a.seg, b.seg, a.suf + b.pref}),
          max({0LL, a.pref, a.sum + b.pref}), max({0LL, b.suf, b.sum + a.suf}),
          max(NEUTRAL_ELEMENT, a.sum + b.sum)};
}
void build(int x, int lx, int rx) {
  if (rx == lx) {
    sums[x] = single(a[lx]);
    return;
  }
  int m = (lx + rx) / 2;
  build(2 * x, lx, m);
  build(2 * x + 1, m + 1, rx);
  sums[x] = merge(sums[2 * x], sums[2 * x + 1]);
}
void build() { build(1, 1, n); }
void update(int i, int x, int lx, int rx) {
  if (rx == lx) {
    sums[x] = single(NEUTRAL_ELEMENT);
    return;
  }
  int m = (lx + rx) / 2;
  if (i <= m) {
    update(i, 2 * x, lx, m);
  } else {
    update(i, 2 * x + 1, m + 1, rx);
  }
  sums[x] = merge(sums[2 * x], sums[2 * x + 1]);
}
void update(int i) { update(i, 1, 1, n); }
item query() { return sums[1]; }
void solve() {
  scanf("%d", &n);
  for (int i = (int)(1); i < (int)(n + 1); i++) scanf("%d", &a[i]);
  build();
  for (int p = (int)(0); p < (int)(n); p++) {
    int i;
    scanf("%d", &i);
    update(i);
    printf("%lld\n", query().seg);
  }
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
