#include <bits/stdc++.h>
using namespace std;
int n, a[101000];
int b[101000];
long long sum[101000];
int fa1[101000], fa2[101000];
long long ans[101000];
int get1(int x) {
  if (fa1[x] == x) return x;
  return fa1[x] = get1(fa1[x]);
}
int get2(int x) {
  if (fa2[x] == x) return x;
  return fa2[x] = get2(fa2[x]);
}
long long temp = 0;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
  for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i];
  sum[n + 1] = sum[n];
  for (int i = 1; i <= n + 1; i++) fa1[i] = fa2[i] = i;
  for (int i = n; i >= 1; i--) {
    ans[i] = temp;
    fa1[b[i]] = fa1[b[i] - 1];
    fa2[b[i]] = fa2[b[i] + 1];
    int l = get1(b[i]), r = get2(b[i]);
    l++, r--;
    temp = max(temp, sum[r] - sum[l - 1]);
  }
  for (int i = 1; i <= n; i++) printf("%I64d\n", ans[i]);
}
