#include <bits/stdc++.h>
using namespace std;
const long long Inf = 1ll << 45;
const double Pi = acos(-1.0);
const int Mod = 1e9 + 7;
const int Max = 4000100;
struct node {
  long long lmax, rmax, mmax, sum;
} segtr[Max];
long long nmax(long long a, long long b) { return a > b ? a : b; }
void Upnow(int now, int next) {
  segtr[now].sum = segtr[next].sum + segtr[next | 1].sum;
  segtr[now].lmax = segtr[next].lmax;
  segtr[now].rmax = segtr[next | 1].rmax;
  segtr[now].mmax =
      nmax(segtr[next].mmax,
           nmax(segtr[next | 1].mmax, segtr[next].rmax + segtr[next | 1].lmax));
  if (segtr[next].sum == segtr[next].lmax)
    segtr[now].lmax += nmax(segtr[next | 1].lmax, 0ll);
  if (segtr[next | 1].sum == segtr[next | 1].rmax)
    segtr[now].rmax += nmax(segtr[next].rmax, 0ll);
  return;
}
void Create(int sta, int enn, int now) {
  if (sta == enn) {
    scanf("%I64d", &segtr[now].sum);
    segtr[now].lmax = segtr[now].rmax = segtr[now].mmax = segtr[now].sum;
    return;
  }
  int next = (now << 1);
  int mid = (sta + enn >> 1);
  Create(sta, mid, next);
  Create(mid + 1, enn, next | 1);
  Upnow(now, next);
  return;
}
void Update(int sta, int enn, int now, int x, long long y) {
  if (sta == enn) {
    segtr[now].lmax = segtr[now].rmax = segtr[now].mmax = y;
    return;
  }
  int next = (now << 1);
  int mid = (sta + enn >> 1);
  if (mid >= x)
    Update(sta, mid, next, x, y);
  else
    Update(mid + 1, enn, next | 1, x, y);
  Upnow(now, next);
  return;
}
int main() {
  int n, que;
  while (~scanf("%d", &n)) {
    Create(1, n, 1);
    for (int i = 0; i < n; ++i) {
      scanf("%d", &que);
      if (i == n - 1) {
        printf("0\n");
        break;
      } else {
        Update(1, n, 1, que, -Inf);
        printf("%I64d\n", segtr[1].mmax);
      }
    }
  }
  return 0;
}
