#include <bits/stdc++.h>
using namespace std;
vector<long long> v, sizq, sder, stree;
vector<int> r, l;
vector<bool> c;
int can;
void crear() {
  int i;
  can = 1;
  while (can < 2 * v.size()) can *= 2;
  stree.assign(can, 0);
  sizq.assign(can, 0);
  sder.assign(can, 0);
  r.assign(can, 0);
  l.assign(can, 0);
  c.assign(can, 1);
  for (i = 0; i < v.size(); i++) {
    sder[i + can / 2] = sizq[i + can / 2] = stree[i + can / 2] = v[i];
  }
  for (i = can / 2 - 1; i > 0; i--) {
    sder[i] = sizq[i] = stree[i] = stree[2 * i] + stree[2 * i + 1];
  }
}
void update(int pos) {
  int i = can / 2 + pos;
  sder[i] = sizq[i] = stree[i] = 0;
  c[i] = 0;
  i /= 2;
  while (i > 0) {
    c[i] = 0;
    sder[i] = sder[2 * i + 1];
    if (c[2 * i + 1]) sder[i] += sder[2 * i];
    sizq[i] = sizq[2 * i];
    if (c[2 * i]) sizq[i] += sizq[2 * i + 1];
    stree[i] = max(stree[2 * i], stree[2 * i + 1]);
    stree[i] = max(stree[i], sder[2 * i] + sizq[2 * i + 1]);
    i /= 2;
  }
}
int main() {
  ios::sync_with_stdio(false);
  int j, i, n, a;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a;
    v.push_back(a);
  }
  crear();
  for (i = 0; i < n; i++) {
    cin >> a;
    update(a - 1);
    cout << stree[1] << "\n";
  }
}
