#include <bits/stdc++.h>
using namespace std;
long long delbit(long long a, long long k) { return a & (~(1 << k)); }
int delbit(int a, int k) { return a & (~(1 << k)); }
bool getbit(long long a, long long k) { return 1 & (a >> k); }
bool getbit(int a, int k) { return 1 & (a >> k); }
long long setbit(long long a, long long k) { return a |= (1 << k); }
int setbit(int a, int k) { return a |= (1 << k); }
inline long long mulmod(long long first, long long n, long long _mod) {
  long long res = 0;
  while (n) {
    if (n & 1) res = (res + first) % _mod;
    first = (first + first) % _mod;
    n >>= 1;
  }
  return res;
}
inline long long powmod(long long first, long long n, long long _mod) {
  long long res = 1;
  while (n) {
    if (n & 1) res = (res * first) % _mod;
    first = (first * first) % _mod;
    n >>= 1;
  }
  return res;
}
inline long long gcd(long long a, long long b) {
  long long t;
  while (b) {
    a = a % b;
    t = a;
    a = b;
    b = t;
  }
  return a;
}
inline int gcd(int a, int b) {
  int t;
  while (b) {
    a = a % b;
    t = a;
    a = b;
    b = t;
  }
  return a;
}
inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
inline long long gcd(long long a, long long b, long long c) {
  return gcd(gcd(a, b), c);
}
inline int gcd(int a, int b, int c) { return gcd(gcd(a, b), c); }
double dist(double ax, double ay, double bx, double by) {
  return sqrt(((ax - bx) * (ax - bx)) + ((ay - by) * (ay - by)));
}
template <typename T, typename T2>
void printarr(T a[], T2 sz, T2 beg = 0) {
  for (T2 i = beg; i < sz; i++) cout << a[i] << " ";
  cout << endl;
}
struct Node {
  long long ans, pref, suff, sum;
  Node() : ans(0), suff(0), sum(0), pref(0) {}
  Node(Node l, Node r) {
    if (l.sum == -1000111000111000111LL || r.sum == -1000111000111000111LL)
      sum = -1000111000111000111LL;
    else
      sum = l.sum + r.sum;
    pref = (((l.pref) < (l.sum + r.pref)) ? (l.sum + r.pref) : (l.pref));
    suff = (((r.suff) < (r.sum + l.suff)) ? (r.sum + l.suff) : (r.suff));
    ans = ((((((l.ans) < (r.ans)) ? (r.ans) : (l.ans))) < (l.suff + r.pref))
               ? (l.suff + r.pref)
               : ((((l.ans) < (r.ans)) ? (r.ans) : (l.ans))));
  }
  Node(long long v) {
    pref = suff = sum = v;
    ans = (((v) < (0)) ? (0) : (v));
  }
};
Node t[4 * 100010];
int a[100010];
void build(int v, int tl, int tr) {
  if (tl == tr) {
    t[v] = Node(a[tl]);
  } else {
    int tm = (tl + tr) >> 1;
    build(2 * v, tl, tm);
    build(2 * v + 1, tm + 1, tr);
    t[v] = Node(t[2 * v], t[2 * v + 1]);
  }
}
void upd(int v, int tl, int tr, int ind) {
  if (tl == tr) {
    t[v] = Node(-1000111000111000111LL);
  } else {
    int tm = (tl + tr) >> 1;
    if (ind <= tm)
      upd(2 * v, tl, tm, ind);
    else
      upd(2 * v + 1, tm + 1, tr, ind);
    t[v] = Node(t[2 * v], t[2 * v + 1]);
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int(i) = (0); (i) < (n); (i)++) cin >> a[i];
  build(1, 0, n - 1);
  for (int(i) = (0); (i) < (n); (i)++) {
    int p;
    cin >> p;
    upd(1, 0, n - 1, p - 1);
    cout << t[1].ans << endl;
  }
  return 0;
}
