#include <bits/stdc++.h>
using namespace std;
struct hello {
  long long MaxPrefix, MaxSuffix, Sum, MaxSubSum;
} tree[4 * 100005];
long long a[100005];
hello Merge(hello left, hello right) {
  hello tt;
  tt.MaxPrefix = max(left.MaxPrefix, left.Sum + right.MaxPrefix);
  tt.MaxSuffix = max(right.MaxSuffix, right.Sum + left.MaxSuffix);
  tt.Sum = left.Sum + right.Sum;
  tt.MaxSubSum = max(max(left.MaxSubSum, right.MaxSubSum),
                     left.MaxSuffix + right.MaxPrefix);
  return tt;
}
void build(long long node, long long b, long long e) {
  if (b > e) return;
  if (b == e) {
    tree[node].Sum = a[b];
    tree[node].MaxPrefix = a[b];
    tree[node].MaxSuffix = a[b];
    tree[node].MaxSubSum = a[b];
    return;
  }
  long long mid = (b + e) / 2;
  long long left = node * 2;
  long long right = left + 1;
  build(left, b, mid);
  build(right, mid + 1, e);
  tree[node] = Merge(tree[left], tree[right]);
}
void update(long long node, long long b, long long e, long long loc,
            long long val) {
  if (loc > e || loc < b || b > e) return;
  if (b == e) {
    tree[node].Sum = val;
    tree[node].MaxPrefix = val;
    tree[node].MaxSuffix = val;
    tree[node].MaxSubSum = val;
    return;
  }
  long long mid = (b + e) / 2;
  long long left = node * 2;
  long long right = left + 1;
  update(left, b, mid, loc, val);
  update(right, mid + 1, e, loc, val);
  tree[node] = Merge(tree[left], tree[right]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  build(1, 1, n);
  for (long long i = 1; i <= n; i++) {
    long long loc;
    cin >> loc;
    if (i == n) {
      cout << 0 << "\n";
      return 0;
    }
    update(1, 1, n, loc, -200000000000000);
    cout << tree[1].MaxSubSum << "\n";
  }
  return 0;
}
