#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
long long ans[maxn], vis[maxn], sum[maxn], a[maxn], b[maxn];
int f[maxn];
int findx(int x) {
  if (x != f[x]) f[x] = findx(f[x]);
  return f[x];
}
int main() {
  int n;
  scanf("%d", &n);
  memset(vis, 0, sizeof(vis));
  memset(ans, 0, sizeof(ans));
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    f[i] = i;
  }
  for (int i = n; i >= 1; i--) scanf("%lld", &b[i]);
  ans[1] = 0;
  for (int i = 1; i <= n - 1; i++) {
    sum[b[i]] = a[b[i]];
    vis[b[i]] = 1;
    if (vis[b[i] - 1]) {
      int x = findx(b[i] - 1);
      sum[b[i]] += sum[x];
      f[x] = b[i];
    }
    if (vis[b[i] + 1]) {
      int x = findx(b[i] + 1);
      sum[b[i]] += sum[x];
      f[x] = b[i];
    }
    ans[i + 1] = (ans[i] > sum[b[i]]) ? ans[i] : sum[b[i]];
  }
  for (int i = n; i >= 1; i--) printf("%lld\n", ans[i]);
  return 0;
}
