#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
int a[N], b[N], p[N];
long long w[N], ans[N];
bool hav[N];
int f(int u) {
  if (u == p[u])
    return u;
  else {
    int rt = f(p[u]);
    return p[u] = rt;
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    p[i] = i;
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
  }
  long long mx = 0;
  for (int i = n; i >= 1; i--) {
    int x = b[i];
    hav[x] = true;
    w[x] = a[x];
    if (hav[x - 1]) {
      int fx = f(x - 1);
      p[fx] = x;
      w[x] += w[fx];
    }
    if (hav[x + 1]) {
      int fx = f(x + 1);
      p[fx] = x;
      w[x] += w[fx];
    }
    mx = max(mx, w[x]);
    ans[i] = mx;
  }
  for (int i = 2; i <= n + 1; i++) {
    printf("%I64d\n", ans[i]);
  }
  return 0;
}
