#include <bits/stdc++.h>
using namespace std;
const long long MAXN = (1 << 20);
long long n;
long long a[MAXN];
long long p[MAXN];
void read() {
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  for (long long j = 1; j <= n; j++) cin >> p[j];
}
long long par[MAXN], sz[MAXN], ans, sum[MAXN];
long long used[MAXN];
void init_dsu(long long k) {
  for (long long i = 0; i <= k; i++)
    used[i] = 0, sz[i] = 1, sum[i] = 0, par[i] = i;
}
long long root(long long x) { return x == par[x] ? x : par[x] = root(par[x]); }
void unite(long long u, long long v) {
  u = root(u), v = root(v);
  if (u == v) return;
  if (sz[u] < sz[v]) swap(u, v);
  par[v] = u;
  sz[u] += sz[v];
  sum[u] += sum[v];
  ans = max(ans, sum[u]);
}
long long answer[MAXN];
void solve() {
  init_dsu(n);
  for (long long i = n; i >= 1; i--) {
    answer[i] = ans;
    if (used[p[i] - 1]) unite(p[i], p[i] - 1);
    if (used[p[i] + 1]) unite(p[i], p[i] + 1);
    sum[root(p[i])] += a[p[i]];
    ans = max(ans, sum[root(p[i])]);
    used[p[i]] = 1;
  }
  for (long long i = 1; i <= n; i++) cout << answer[i] << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  read();
  solve();
  return 0;
}
