#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
template <class T, class U>
inline void Max(T &a, U b) {
  if (a < b) a = b;
}
template <class T, class U>
inline void Min(T &a, U b) {
  if (a > b) a = b;
}
inline void add(int &a, int b) {
  a += b;
  while (a >= 924844033) a -= 924844033;
}
int pow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = ans * (long long)a % 924844033;
    a = (long long)a * a % 924844033;
    b >>= 1;
  }
  return ans;
}
int a[100005];
struct node {
  long long mx, lx, rx;
  int f;
} t[100005 << 2];
void cal(node &first, int v) {
  first.mx = first.lx = first.rx = v;
  first.f = 0;
}
void up(node a, node b, node &c) {
  c.mx = max(a.mx, b.mx);
  c.lx = a.lx;
  if (!a.f) Max(c.lx, a.mx + b.lx);
  c.rx = b.rx;
  if (!b.f) Max(c.rx, b.mx + a.rx);
  Max(c.mx, a.rx + b.lx);
  c.f = a.f | b.f;
}
void build(int p, int l, int r) {
  if (l == r) {
    cal(t[p], a[l]);
    return;
  }
  int m = (l + r) >> 1;
  build(p << 1, l, m);
  build(p << 1 | 1, m + 1, r);
  up(t[p << 1], t[p << 1 | 1], t[p]);
}
void upd(int p, int l, int r, int first) {
  if (l == r) {
    t[p].mx = t[p].lx = t[p].rx = 0;
    t[p].f = 1;
    return;
  }
  int m = (l + r) >> 1;
  if (first <= m)
    upd(p << 1, l, m, first);
  else
    upd(p << 1 | 1, m + 1, r, first);
  up(t[p << 1], t[p << 1 | 1], t[p]);
}
int main() {
  int T, i, j, k, n, m = 0, K;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  build(1, 0, n - 1);
  for (int j = 0; j < n; j++) {
    scanf("%d", &i);
    i--;
    upd(1, 0, n - 1, i);
    printf("%I64d\n", t[1].mx);
  }
  return 0;
}
