#include <bits/stdc++.h>
using namespace std;
const int mod = 7 + 1e9;
const int inf = 1e9 + 5;
const long long INF = 1e18 + 5;
const int N = 1e5 + 5;
long long ans[N], a[N];
vector<int> b;
int par[N];
bool vis[N];
int get(int u) {
  if (u == par[u])
    return u;
  else
    return par[u] = get(par[u]);
}
void mrg(int u, int v) {
  u = get(u);
  v = get(v);
  if (u == v) return;
  if (rand() & 1) swap(u, v);
  par[u] = v;
  a[v] += a[u];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  for (int i = 0; i < (int)(N); i++) par[i] = i;
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int i = 0; i < (int)(n); i++) {
    int x;
    cin >> x;
    b.push_back(x);
  }
  reverse(b.begin(), b.end());
  int cur = n - 1;
  long long now = 0;
  ans[n] = 0;
  for (auto u : b) {
    vis[u] = 1;
    if (vis[u - 1]) mrg(u, u - 1);
    if (vis[u + 1]) mrg(u, u + 1);
    now = max(now, a[get(u)]);
    ans[cur--] = now;
  }
  for (int i = 1; i <= n; ++i) cout << ans[i] << '\n';
}
