#include <bits/stdc++.h>
using namespace std;
int n;
int pa[100005];
long long a[100005];
bool have[100005];
int op[100005];
long long ans[100005];
long long f[100005];
void init() {
  for (int i = 1; i <= n; i++) {
    pa[i] = i;
    ans[i] = 0;
  }
  memset(have, false, sizeof(have));
}
int found(int x) { return x == pa[x] ? x : pa[x] = found(pa[x]); }
void bing(int x, int y) {
  int xx = found(x);
  int yy = found(y);
  pa[xx] = yy;
  f[yy] += f[xx];
}
int main() {
  cin >> n;
  init();
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> op[i];
  }
  for (int i = n; i >= 1; i--) {
    int p1 = op[i];
    f[p1] = a[p1];
    if (have[p1 - 1]) {
      bing(p1, p1 - 1);
    }
    if (have[p1 + 1]) {
      bing(p1, p1 + 1);
    }
    ans[i - 1] = max(f[found(p1)], ans[i]);
    have[p1] = true;
  }
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
