#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
const long long inf = 1ll << 46;
struct Node {
  long long sum, leftSum, rightSum, all;
  Node(){};
  Node(long long s) { sum = leftSum = rightSum = all = s; }
  void mrg(Node L, Node R) {
    all = L.all + R.all;
    rightSum = max(L.rightSum + R.all, max(R.rightSum, 0ll));
    leftSum = max(R.leftSum + L.all, max(L.leftSum, 0ll));
    leftSum = max(leftSum, all);
    rightSum = max(rightSum, all);
    sum = max(
        0ll, max(leftSum, max(rightSum,
                              max(L.sum, max(R.sum, L.rightSum + R.leftSum)))));
    sum = max(sum, all);
  }
};
int n;
int a[100100];
int p[100100];
Node seg[N << 2];
void build(int nd, int L, int R) {
  if (L == R) {
    seg[nd] = Node(a[L]);
    return;
  }
  int mid = (L + R) >> 1;
  build(nd << 1, L, mid);
  build(nd << 1 | 1, mid + 1, R);
  seg[nd].mrg(seg[nd << 1], seg[nd << 1 | 1]);
}
void update(int nd, int L, int R, int idx) {
  if (idx < L || idx > R) return;
  if (L == R) {
    seg[nd] = Node(-inf);
    return;
  }
  int mid = (L + R) >> 1;
  update(nd << 1, L, mid, idx);
  update(nd << 1 | 1, mid + 1, R, idx);
  seg[nd].mrg(seg[nd << 1], seg[nd << 1 | 1]);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", a + i);
  for (int i = 1; i <= n; i++) scanf("%d", p + i);
  build(1, 1, n);
  for (int i = 1; i <= n; i++) {
    update(1, 1, n, p[i]);
    printf("%I64d\n", max(0ll, seg[1].sum));
  }
  return 0;
}
