#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const double esp = 1e-6;
const int inf = 2147483647;
inline void in(int &x) {
  register char ch;
  while (ch = getchar(), (ch < '0' || ch > '9'))
    ;
  x = ch - '0';
  while (ch = getchar(), ch >= '0' && ch <= '9') x = x * 10 + ch - '0';
}
inline void out(int x) {
  register char hc[30];
  register int len = 0;
  hc[len++] = x % 10 + '0';
  while (x /= 10) hc[len++] = x % 10 + '0';
  for (int i = len - 1; i >= 0; i--) putchar(hc[i]);
}
int a[100005], b[100005], vis[100005], f[100005];
long long s[100005];
int find(int x) {
  if (x != f[x]) x = find(f[x]);
  return x;
}
vector<long long> vc;
int main() {
  int n, i, j;
  while (cin >> n) {
    for (int i = 1; i <= n; ++i) {
      in(a[i]);
      f[i] = i;
    }
    vc.clear();
    for (int i = 1; i <= n; ++i) in(b[i]);
    memset(s, 0, sizeof(s));
    long long mmax = 0;
    memset(vis, 0, sizeof(vis));
    for (int i = n; i >= 1; --i) {
      vc.push_back(mmax);
      int pos = b[i];
      vis[pos] = 1;
      s[pos] = a[pos];
      if (vis[pos + 1]) {
        int fa = find(pos + 1);
        s[pos] += s[fa];
        f[find(pos + 1)] = find(pos);
        mmax = max(mmax, s[fa]);
      }
      if (vis[pos - 1]) {
        int fb = find(pos - 1);
        s[fb] += s[pos];
        f[pos] = fb;
        mmax = max(mmax, s[fb]);
      }
      mmax = max(mmax, s[pos]);
    }
    for (int i = vc.size() - 1; i >= 0; --i) printf("%lld\n", vc[i]);
  }
}
