#include <bits/stdc++.h>
using namespace std;
struct info {
  long long int lsum, rsum, sum, best;
};
info tree[200010 * 4], ans, temp;
info query(long long int node, long long int b, long long int e,
           long long int i, long long int j) {
  if (i <= b && j >= e) return tree[node];
  long long int Left = node * 2;
  long long int Right = node * 2 + 1;
  long long int mid = (b + e) / 2;
  if (j <= mid)
    return query(Left, b, mid, i, j);
  else if (i > mid)
    return query(Right, mid + 1, e, i, j);
  else {
    info p1 = query(Left, b, mid, i, j);
    info p2 = query(Right, mid + 1, e, i, j);
    ans.sum = p1.sum + p2.sum;
    ans.lsum = max(p1.lsum, p1.sum + p2.lsum);
    ans.rsum = max(p1.rsum + p2.sum, p2.rsum);
    ans.best = max(max(p1.best, p2.best), p1.rsum + p2.lsum);
    return ans;
  }
}
void update(long long int node, long long int b, long long int e,
            long long int pos, long long int v) {
  if (b == e) {
    tree[node].best = tree[node].lsum = tree[node].rsum = tree[node].sum = v;
    return;
  }
  long long int Left = node * 2;
  long long int Right = node * 2 + 1;
  long long int mid = (b + e) / 2;
  if (pos <= mid)
    update(Left, b, mid, pos, v);
  else
    update(Right, mid + 1, e, pos, v);
  tree[node].sum = tree[Left].sum + tree[Right].sum;
  tree[node].lsum = max(tree[Left].lsum, tree[Right].lsum + tree[Left].sum);
  tree[node].rsum = max(tree[Right].rsum, tree[Left].rsum + tree[Right].sum);
  tree[node].best = max(max(tree[Left].best, tree[Right].best),
                        tree[Left].rsum + tree[Right].lsum);
  return;
}
int main() {
  long long int n, m, i, j, x;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> x;
    update(1, 1, n, i, x);
  }
  for (i = 1; i <= n; i++) {
    cin >> x;
    update(1, 1, n, x, -100000000000000LL);
    cout << max(0LL, query(1, 1, n, 1, n).best) << endl;
  }
  return 0;
}
