#include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 5;
long long int arr[M], brr[M], crr[M], parent[M], mx, sum[M];
int find_parent(int u) {
  if (u == parent[u]) return u;
  return parent[u] = find_parent(parent[u]);
}
void union_set(int u, int v) {
  u = find_parent(u);
  v = find_parent(v);
  parent[u] = v;
  sum[v] += sum[u];
}
int main() {
  int n, idx;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> arr[i];
  for (int i = 1; i <= n; i++) {
    cin >> brr[i];
    parent[i] = i;
  }
  vector<long long int> ans;
  for (int i = n; i >= 1; i--) {
    idx = brr[i];
    sum[idx] = arr[idx];
    crr[idx] = 1;
    if (idx + 1 <= n && crr[idx + 1]) union_set(idx, idx + 1);
    if (idx - 1 >= 1 && crr[idx - 1]) union_set(idx, idx - 1);
    ans.push_back(mx);
    mx = max(mx, sum[find_parent(idx)]);
  }
  for (int i = n - 1; i >= 0; i--) cout << ans[i] << "\n";
  return 0;
}
