#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
inline void chmin(A &a, B b) {
  if (a > b) a = b;
}
template <typename A, typename B>
inline void chmax(A &a, B b) {
  if (a < b) a = b;
}
struct UF {
  long long N;
  vector<long long> par, sum;
  void init(long long n, vector<long long> A) {
    N = n;
    par.resize(N);
    sum.resize(N);
    for (long long i = 0; i < (N); i++) {
      par[i] = i;
      sum[i] = A[i];
    }
  }
  long long find(long long x) {
    return x == par[x] ? x : par[x] = find(par[x]);
  }
  void unite(long long x, long long y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    par[y] = x;
    sum[x] += sum[y];
  }
};
bool flag[100000];
long long ans[100000];
signed main() {
  long long N;
  scanf("%lld", &N);
  vector<long long> A(N), B(N);
  for (long long i = 0; i < (N); i++) scanf("%lld", &A[i]);
  for (long long i = 0; i < (N); i++) scanf("%lld", &B[i]), B[i]--;
  UF uf;
  uf.init(N, A);
  long long ma = 0;
  for (long long i = N - 1; i >= 0; i--) {
    ans[i] = ma;
    flag[B[i]] = true;
    if (B[i] > 0 && flag[B[i] - 1]) {
      uf.unite(B[i], B[i] - 1);
    }
    if (B[i] + 1 < N && flag[B[i] + 1]) {
      uf.unite(B[i], B[i] + 1);
    }
    chmax(ma, uf.sum[uf.find(B[i])]);
  }
  for (long long i = 0; i < (N); i++) printf("%lld\n", ans[i]);
  return 0;
}
