#include <bits/stdc++.h>
using namespace std;
long long n, a[100005], b[100005], fa[100005], s[100005], q[100005], l,
    p = -2147483647;
inline long long find(long long x) {
  if (x == fa[x]) return x;
  return fa[x] = find(fa[x]);
}
inline long long M(long long u, long long v) { return u > v ? u : v; }
int main() {
  scanf("%lld", &n);
  for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]);
  for (long long i = 1; i <= n; i++) scanf("%lld", &b[i]);
  for (long long i = n; i >= 1; i--) {
    long long k = b[i];
    if (!fa[k]) fa[k] = k, s[k] = a[k];
    long long fk = find(k);
    if (fa[k - 1] && k - 1 > 0) {
      long long fl = find(k - 1);
      if (fk != fl) s[k] += s[fl], s[fl] = 0, fa[fl] = k;
    }
    if (fa[k + 1] && k + 1 <= n) {
      long long fr = find(k + 1);
      if (fk != fr) s[k] += s[fr], s[fr] = 0, fa[fr] = k;
    }
    p = M(p, s[k]), q[++l] = p;
  }
  for (long long i = n - 1; i >= 0; i--) printf("%lld\n", q[i]);
  return 0;
}
