#include <bits/stdc++.h>
const int MAXN = 100001;
int f[MAXN];
int find(int x) {
  if (f[x] != x)
    return f[x] = find(f[x]);
  else
    return x;
}
long long fval[MAXN];
void UnionValue(int x, int y) {
  int tx = find(x), ty = find(y);
  if (tx != ty) {
    f[tx] = ty;
    fval[ty] += fval[tx];
  }
}
int a[MAXN], d[MAXN], book[MAXN];
void init(int &n) {
  for (int i = 0; i < MAXN; ++i) f[i] = i, book[i] = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", &a[i]), fval[i] = a[i];
  for (int j = 0; j < n; ++j) scanf("%d", &d[j]), d[j]--;
}
std::vector<long long> ans;
int main() {
  int n;
  long long maxx = -99999999;
  init(n);
  ans.push_back(0);
  for (int i = n - 1; i >= 0; --i) {
    book[d[i]] = 1;
    if (0 <= d[i] - 1 && book[d[i] - 1]) UnionValue(d[i], d[i] - 1);
    if (d[i] + 1 < n && book[d[i] + 1]) UnionValue(d[i], d[i] + 1);
    maxx = std::max(fval[find(d[i])], maxx);
    ans.push_back(maxx);
  }
  for (int i = ans.size() - 2; i >= 0; --i) {
    printf("%lld\n", ans[i]);
  }
  return 0;
}
