#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 1;
int n, a[MAX], pos, bit[2][MAX], pre[MAX];
long long sum[MAX], res;
struct cmp {
  bool operator()(const pair<int, int>& x, const pair<int, int>& y) {
    return sum[x.second] - sum[x.first - 1] < sum[y.second] - sum[y.first - 1];
  }
};
priority_queue<pair<int, int>, vector<pair<int, int> >, cmp> heap;
void up(const int& type, int i) {
  int j = i;
  while (i <= n) {
    bit[type][i] = max(bit[type][i], j);
    i += i & (-i);
  }
}
int get(const int& type, int i) {
  int res = 0;
  while (i) {
    res = max(res, bit[type][i]);
    i -= i & (-i);
  }
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    sum[i] = sum[i - 1] + a[i];
  }
  heap.push(pair<int, int>(1, n));
  pre[n] = 1;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &pos);
    int u = get(0, pos);
    int v = get(1, n - pos + 1);
    v = n - v + 1;
    if (v > pos + 1) {
      heap.push(pair<int, int>(pos + 1, v - 1));
      pre[v - 1] = pos + 1;
    } else
      pre[v - 1] = -1;
    if (pos > u + 1) {
      heap.push(pair<int, int>(u + 1, pos - 1));
      pre[pos - 1] = u + 1;
    } else
      pre[pos - 1] = -1;
    res = 0;
    while (!heap.empty()) {
      pair<int, int> node = heap.top();
      if (pre[node.second] == node.first) break;
      heap.pop();
    }
    if (!heap.empty()) {
      pair<int, int> node = heap.top();
      res = sum[node.second] - sum[node.first - 1];
    }
    up(0, pos);
    up(1, n - pos + 1);
    printf("%lld\n", res);
  }
  return 0;
}
