#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
long long ans[N];
long long a[N], fa[N], b[N];
int vis[N];
int get(int x) {
  if (x == fa[x]) return x;
  return fa[x] = get(fa[x]);
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    fa[i] = i;
  }
  for (int i = 1; i <= n; i++) scanf("%lld", &b[i]);
  long long cnt = 0;
  for (int i = n; i >= 1; i--) {
    ans[i] = cnt;
    vis[b[i]] = 1;
    if (vis[b[i] + 1]) {
      int x = get(b[i] + 1);
      fa[x] = b[i];
      a[b[i]] += a[x];
    }
    if (vis[b[i] - 1]) {
      int x = get(b[i] - 1);
      fa[x] = b[i];
      a[b[i]] += a[x];
    }
    cnt = max(cnt, a[b[i]]);
  }
  for (int i = 1; i <= n; i++) {
    printf("%lld\n", ans[i]);
  }
  return 0;
}
