#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:36777216")
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  vector<long long> s(n + 1, 0);
  for (int i = 0; i < n; ++i) {
    s[i + 1] = s[i] + a[i];
  }
  vector<int> perm(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &perm[i]);
    --perm[i];
  }
  set<pair<int, int> > segms;
  multiset<long long> segm_sums;
  segms.insert(make_pair(0, n - 1));
  segm_sums.insert(s[n]);
  const int INF = 2000000000;
  for (int i = 0; i < n; ++i) {
    int x = perm[i];
    set<pair<int, int> >::iterator it = segms.lower_bound(make_pair(x, INF));
    --it;
    pair<int, int> containing = *it;
    segms.erase(it);
    segm_sums.erase(
        segm_sums.find(s[containing.second + 1] - s[containing.first]));
    segm_sums.insert(s[x] - s[containing.first]);
    segms.insert(make_pair(containing.first, x - 1));
    segm_sums.insert(s[containing.second + 1] - s[x + 1]);
    segms.insert(make_pair(x + 1, containing.second));
    printf("%I64d\n", *segm_sums.rbegin());
  }
  return 0;
}
