#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 7;
int pre[maxn];
long long sum[maxn];
long long a[maxn];
long long ans[maxn];
int b[maxn];
bool vis[maxn];
int findd(int x) {
  if (x != pre[x]) {
    pre[x] = findd(pre[x]);
  }
  return pre[x];
}
int mix(int x, int y) {
  x = findd(x), y = findd(y);
  if (x != y) {
    pre[x] = y;
    sum[y] += sum[x];
  }
  return 0;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i <= n; i++) pre[i] = i;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &b[i]);
  }
  ans[n] = 0;
  for (int i = n - 1; i > 0; i--) {
    int pos = b[i + 1];
    sum[pos] = a[pos];
    vis[pos] = 1;
    if (vis[pos - 1]) {
      mix(pos - 1, pos);
    }
    if (vis[pos + 1]) {
      mix(pos + 1, pos);
    }
    ans[i] = max(ans[i + 1], sum[findd(pos)]);
  }
  for (int i = 1; i <= n; i++) {
    printf("%lld\n", ans[i]);
  }
}
