#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const int mod = 1e9 + 7;
int fa[MAXN], a[MAXN], b[MAXN];
long long sum[MAXN];
long long ans[MAXN];
int Find(int x) { return fa[x] == x ? x : fa[x] = Find(fa[x]); }
void merge(int x, int y) {
  int fx = Find(x), fy = Find(y);
  sum[fy] += sum[fx];
  fa[fx] = fy;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  long long max1 = 0;
  for (int i = n; i >= 1; i--) {
    ans[i] = max1;
    int index = b[i];
    int x = fa[b[i]];
    if (x == 0) {
      fa[b[i]] = index;
      sum[b[i]] = a[b[i]];
    }
    if (fa[b[i] - 1]) merge(b[i] - 1, b[i]);
    if (fa[b[i] + 1]) merge(b[i] + 1, b[i]);
    max1 = max(max1, sum[Find(b[i])]);
  }
  for (int i = 1; i <= n; i++) printf("%lld\n", ans[i]);
  return 0;
}
