#include <bits/stdc++.h>
using namespace std;
vector<long long> V;
class UnionFind {
 public:
  vector<long long> rank, parent, suma;
  UnionFind(int n) {
    rank = parent = suma = vector<long long>(n);
    for (int i = 0; i < n; ++i) parent[i] = i;
    for (int i = 0; i < n; ++i) suma[i] = V[i];
  }
  int findSet(int v) {
    return v == parent[v] ? v : (parent[v] = findSet(parent[v]));
  }
  bool sameSet(int i, int j) { return findSet(i) == findSet(j); }
  void unionSet(int i, int j) {
    if (!sameSet(i, j)) {
      int x = findSet(i), y = findSet(j);
      if (rank[x] > rank[y])
        parent[y] = x;
      else {
        if (rank[x] == rank[y]) rank[y]++;
        parent[x] = y;
      }
      suma[findSet(x)] = suma[x] + suma[y];
    }
  }
  long long getSum(int k) { return suma[findSet(k)]; }
};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<long long> perm(n), ans(n), cur(n, 0);
  V = vector<long long>(n);
  for (int i = 0; i < n; ++i) cin >> V[i];
  for (int i = 0; i < n; ++i) cin >> perm[i];
  long long mx = 0;
  UnionFind uf(n);
  for (int i = n - 1; i >= 0; --i) {
    ans[i] = mx;
    int m = perm[i];
    m--;
    cur[m] = 1;
    if (m > 0 && cur[m - 1]) uf.unionSet(m - 1, m);
    if (m < n - 1 && cur[m + 1]) uf.unionSet(m + 1, m);
    mx = max(mx, uf.getSum(m));
  }
  for (int i = 0; i < n; ++i) cout << ans[i] << "\n";
  return 0;
}
