#include <bits/stdc++.h>
using namespace std;
long long A[100010];
long long B[100010];
priority_queue<long long> pq;
set<int> S;
int P[100010];
long long ans[100010];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%I64d", &A[i]);
  B[0] = 0LL;
  for (int i = 1; i <= n; i++) B[i] = B[i - 1] + A[i];
  for (int i = 0; i < n; i++) scanf("%d", &P[i]);
  ans[n - 1] = 0;
  for (int i = 1; i <= n; i++) S.insert(i);
  S.insert(0);
  S.insert(n + 1);
  set<int>::iterator L, R;
  long long best = 0;
  for (int i = n - 2; i >= 0; i--) {
    int p = P[i + 1];
    S.erase(p);
    R = S.lower_bound(p);
    L = R;
    L--;
    long long v = B[(*R) - 1] - B[*L];
    best = max(best, v);
    ans[i] = best;
  }
  for (int i = 0; i < n; i++) printf("%I64d\n", ans[i]);
  return 0;
}
