#include <bits/stdc++.h>
using namespace std;
multiset<long long> mx;
set<int> lr;
long long A[1000010];
long long psum[1000010];
int order[1000010];
int n;
int main(int argc, char const *argv[]) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%I64d", A + i);
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", order + i);
    order[i]--;
  }
  for (int i = 1; i <= n; i++) {
    psum[i] = psum[i - 1] + A[i - 1];
  }
  lr.insert(-1);
  lr.insert(n);
  mx.insert(psum[n]);
  for (int i = 0; i < n; i++) {
    int in = order[i];
    int rb = *lr.lower_bound(order[i]);
    int lb = *(--lr.lower_bound(order[i]));
    long long sm = psum[rb] - psum[lb + 1];
    mx.erase(mx.find(sm));
    mx.insert(psum[rb] - psum[in + 1]);
    mx.insert(psum[in] - psum[lb + 1]);
    lr.insert(in);
    cout << *mx.rbegin() << endl;
  }
  return 0;
}
