#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 0x3c3c3c3c;
const ll INF_L = 0x3c3c3c3c3c3c3c3c;
const int MAX_N = 1e5 + 9;
int a[MAX_N];
int d[MAX_N];
int f[MAX_N];
bool used[MAX_N];
ll c[MAX_N];
ll ans[MAX_N];
int find(int v) { return v == f[v] ? v : f[v] = find(f[v]); }
void merge(int u, int v) {
  u = find(u);
  v = find(v);
  if (u == v) {
    return;
  }
  if (u > v) {
    swap(u, v);
  }
  f[u] = v;
  c[v] += c[u];
}
int main() {
  cin.sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  for (int i = 1; i < MAX_N; i++) {
    f[i] = i;
  }
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    c[i] = a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> d[i];
  }
  ll maxV = 0;
  ans[n] = 0;
  for (int i = n; i >= 1; i--) {
    int idx = d[i];
    if (idx + 1 <= n && used[idx + 1]) {
      merge(idx, idx + 1);
    }
    if (idx - 1 >= 1 && used[idx - 1]) {
      merge(idx, idx - 1);
    }
    maxV = max(maxV, c[find(idx)]);
    ans[i - 1] = maxV;
    used[idx] = true;
  }
  for (int i = 1; i <= n; i++) {
    printf("%lld\n", ans[i]);
  }
}
