#include <bits/stdc++.h>
using namespace std;
struct node {
  long long suma, maxAct, ls, rs;
  node() {}
  node(long long s, long long m, long long ls, long long rs)
      : suma(s), maxAct(m), ls(ls), rs(rs) {}
} neutroNode(0, 0, 0, 0);
node combine(node l, node r) {
  long long nSum, nMax, ls, rs;
  if ((l.suma == INT_MIN) && (r.suma == INT_MIN)) {
    nMax = max(max(l.rs, (long long)0) + max(r.ls, (long long)0),
               max(l.maxAct, r.maxAct));
    nSum = INT_MIN;
    ls = max((long long)0, l.ls);
    rs = max((long long)0, r.rs);
  }
  if ((l.suma == INT_MIN) && (r.suma != INT_MIN)) {
    nMax = max(max(l.rs, (long long)0) + r.suma, max(l.maxAct, r.maxAct));
    nSum = INT_MIN;
    ls = max(l.ls, (long long)0);
    rs = max((long long)0, r.suma + max((long long)0, l.rs));
  }
  if ((l.suma != INT_MIN) && (r.suma == INT_MIN)) {
    nMax = max(max(r.ls, (long long)0) + max((long long)0, l.suma),
               max(l.maxAct, r.maxAct));
    nSum = INT_MIN;
    ls = max((long long)0, l.suma + max((long long)0, r.ls));
    rs = max(r.rs, (long long)0);
  }
  if ((l.suma != INT_MIN) && (r.suma != INT_MIN)) {
    nMax = max(l.suma + r.suma, max(l.maxAct, r.maxAct));
    nSum = l.suma + r.suma;
    ls = max((long long)0, l.suma);
    rs = max((long long)0, r.suma);
  }
  return node(nSum, nMax, ls, rs);
}
struct SegTree {
  vector<node> ST;
  vector<node> val;
  int N;
  void build(int i, int l, int r) {
    if (l == r) {
      ST[i] = val[l];
      return;
    }
    build(i * 2, l, (l + r) / 2);
    build(i * 2 + 1, (l + r) / 2 + 1, r);
    ST[i] = combine(ST[i * 2], ST[i * 2 + 1]);
  }
  SegTree(vector<node>& V) {
    if (V.size() > 0) {
      val.resize(V.size());
      val = V;
      N = val.size();
      ST.resize(N * 4 + 5, neutroNode);
      build(1, 0, N - 1);
    }
  }
  void update(int i, int l, int r, int pos) {
    if (l == r && l == pos) {
      ST[i] = val[pos];
      return;
    }
    if ((l > pos) || (r < pos)) return;
    update(i * 2, l, (l + r) / 2, pos);
    update(i * 2 + 1, (l + r) / 2 + 1, r, pos);
    ST[i] = combine(ST[i * 2], ST[i * 2 + 1]);
  }
  void update(int pos, node& ne) {
    val[pos] = ne;
    update(1, 0, N - 1, pos);
  }
  node query(int i, int l, int r, int a, int b) {
    if ((b < l) || (a > r)) return neutroNode;
    if ((l >= a) && (r <= b)) return ST[i];
    return combine(query(2 * i, l, (l + r) / 2, a, b),
                   query(2 * i + 1, (l + r) / 2 + 1, r, a, b));
  }
  node query(int a, int b) { return query(1, 0, N - 1, a, b); }
};
int main() {
  int n;
  cin >> n;
  vector<node> a(n);
  for (int i = 0; i < n; i++) {
    long long j;
    cin >> j;
    a[i].suma = j;
    a[i].maxAct = j;
    a[i].ls = 0;
    a[i].rs = 0;
  }
  SegTree st(a);
  node badNode(INT_MIN, INT_MIN, 0, 0);
  for (int i = 0; i < n; i++) {
    int q;
    cin >> q;
    st.update(q - 1, badNode);
    long long o = st.query(0, n).maxAct;
    cout << (o == INT_MIN ? 0 : o) << endl;
  }
}
