#include <bits/stdc++.h>
using namespace std;
int n;
long long *a, *output;
int *b, *uf;
bool *isConstruyed;
int find(int x) {
  if (uf[x] < 0) return x;
  return uf[x] = find(uf[x]);
}
long long join(int x, int y) {
  x = find(x);
  y = find(y);
  uf[x] = y;
  return a[y] += a[x];
}
int main() {
  cin >> n;
  a = new long long[n];
  b = new int[n];
  uf = new int[n];
  isConstruyed = new bool[n];
  output = new long long[n];
  for (int i = 0; i < n; ++i) cin >> a[i];
  for (int i = 0; i < n; ++i) cin >> b[i];
  memset(uf, -1, sizeof(int) * n);
  memset(isConstruyed, 0, sizeof(bool) * n);
  long long maxSegment = -1;
  output[n - 1] = 0;
  for (int i = n - 1; i > 0; --i) {
    int x = b[i] - 1;
    long long tempMax = 0;
    if (x > 0 && isConstruyed[x - 1]) tempMax = join(x, x - 1);
    if (x < n - 1 && isConstruyed[x + 1]) tempMax = join(x, x + 1);
    isConstruyed[x] = true;
    if (tempMax == 0) tempMax = a[x];
    maxSegment = max(maxSegment, tempMax);
    output[i - 1] = maxSegment;
  }
  for (int i = 0; i < n; ++i) cout << output[i] << '\n';
  return 0;
}
