#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
  vector<int> f;
  vector<long long> sum;
  vector<bool> alive;
  void init(int n, vector<long long> s) {
    f.clear();
    f.insert(f.begin(), n, -1);
    sum = s;
    alive = vector<bool>(n, false);
  }
  int comp(int x) { return (f[x] == -1 ? x : f[x] = comp(f[x])); }
  bool join(int i, int j) {
    if (!alive[j]) return 0;
    bool con = comp(i) == comp(j);
    if (!con) {
      sum[comp(j)] += sum[comp(i)];
      f[comp(i)] = comp(j);
    }
    return con;
  }
} uf;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  long long n;
  cin >> n;
  vector<long long> nrs(n), perm(n), sol(n + 1);
  for (int i = 0; i < n; i++) cin >> nrs[i];
  for (int i = 0; i < n; i++) {
    cin >> perm[i];
    perm[i]--;
  }
  uf.init(n, nrs);
  for (int i = n - 1; i >= 0; i--) {
    uf.alive[perm[i]] = true;
    if (perm[i] > 0) uf.join(perm[i], perm[i] - 1);
    if (perm[i] < n - 1) uf.join(perm[i], perm[i] + 1);
    sol[i] = max(sol[i + 1], uf.sum[uf.comp(perm[i])]);
  }
  for (int i = 1; i < n + 1; i++) cout << sol[i] << endl;
  return 0;
}
