#include <bits/stdc++.h>
using namespace std;
const int mt = 1000000 + 5;
long long val[mt];
int fa[mt];
int a[mt];
int op[mt];
bool vis[mt];
long long ans = 0;
long long res[mt];
inline int read() {
  int num = 0, flag = 1;
  char ch;
  do {
    ch = getchar();
    if (ch == '-') flag = -1;
  } while (ch < '0' || ch > '9');
  do {
    num = num * 10 + ch - '0';
    ch = getchar();
  } while (ch >= '0' && ch <= '9');
  return num * flag;
}
int findf(int x) {
  if (fa[x] == x) return x;
  return fa[x] = findf(fa[x]);
}
int main() {
  int n = read();
  for (int i = 1; i <= n; i++) a[i] = read(), fa[i] = i;
  for (int i = 1; i <= n; i++) op[i] = read();
  res[n + 1] = 0;
  for (int i = n; i >= 2; i--) {
    vis[op[i]] = 1;
    val[op[i]] = a[op[i]];
    if (vis[op[i] - 1]) {
      int y = findf(op[i] - 1);
      fa[y] = op[i];
      val[op[i]] += val[y];
    }
    if (vis[op[i] + 1]) {
      int y = findf(op[i] + 1);
      fa[y] = op[i];
      val[op[i]] += val[y];
    }
    ans = max(ans, val[op[i]]);
    res[i] = ans;
  }
  for (int i = 2; i <= n + 1; i++) printf("%lld\n", res[i]);
  return 0;
}
