#include <bits/stdc++.h>
using namespace std;
long long sum[100009], maxx, ans[100009];
bool use[100009];
int n, fa[100009], a[100009], order[100009];
int find(int x) {
  if (fa[x] == x) return x;
  fa[x] = find(fa[x]);
  return fa[x];
}
void Union(int x, int y) {
  int fx = find(x), fy = find(y);
  fa[fy] = fx;
  sum[fx] += sum[fy];
}
int main() {
  int i, j, k;
  cin >> n;
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (i = 1; i <= n; i++) scanf("%d", &order[i]);
  for (i = n; i; i--) {
    use[order[i]] = 1;
    fa[order[i]] = order[i];
    sum[order[i]] = a[order[i]];
    if (use[order[i] - 1] && use[order[i] + 1]) {
      Union(order[i] - 1, order[i] + 1);
      Union(order[i] - 1, order[i]);
    } else if (use[order[i] - 1])
      Union(order[i] - 1, order[i]);
    else if (use[order[i] + 1])
      Union(order[i] + 1, order[i]);
    int fx = find(order[i]);
    maxx = max(maxx, sum[fx]);
    ans[i] = maxx;
  }
  for (i = 2; i <= n; i++) printf("%I64d\n", ans[i]);
  puts("0");
}
