#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
int n, o[100005], par[100005];
long long ans[100005], a[100005];
bool ap[100005];
priority_queue<long long> pq;
int fnd(int x) {
  if (par[x] == x) return x;
  return par[x] = fnd(par[x]);
}
void unite(int x, int y) {
  x = fnd(x);
  y = fnd(y);
  if (x == y) return;
  a[x] += a[y];
  par[y] = x;
  pq.push(a[x]);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    par[i] = i;
  }
  for (int i = 0; i < n; i++) {
    cin >> o[i];
  }
  ans[n] = 0;
  for (int i = n - 1; i >= 0; i--) {
    ap[o[i]] = true;
    pq.push(a[o[i]]);
    if (ap[o[i] - 1]) unite(o[i], o[i] - 1);
    if (ap[o[i] + 1]) unite(o[i], o[i] + 1);
    ans[i] = pq.top();
  }
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << '\n';
  }
  return 0;
}
