#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
int n, a[N], b[N];
int par[N], sz[N];
bool alive[N];
long long c[N], best;
vector<long long> vec;
int getPar(int x) {
  if (x == par[x])
    return x;
  else
    return par[x] = getPar(par[x]);
}
void mrg(int x, int y) {
  x = getPar(x);
  y = getPar(y);
  if (x == y) return;
  if (sz[x] < sz[y]) swap(x, y);
  c[x] += c[y];
  sz[x] += sz[y];
  par[y] = x;
}
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", b + i);
  for (int i = 1; i <= n; i++) {
    c[i] = -1;
    par[i] = i;
    sz[i] = i;
  }
  for (int i = n; i > 0; i--) {
    vec.push_back(best);
    c[b[i]] = a[b[i]];
    alive[b[i]] = 1;
    if (b[i] + 1 <= n && alive[b[i] + 1]) mrg(b[i], b[i] + 1);
    if (b[i] - 1 >= 1 && alive[b[i] - 1]) mrg(b[i], b[i] - 1);
    best = max(best, c[getPar(b[i])]);
  }
  reverse(vec.begin(), vec.end());
  for (int i = 0; i < n; i++) printf("%lld\n", vec[i]);
  return 0;
}
