#include <bits/stdc++.h>
using namespace std;
long long a[110000];
struct UnionFind {
  vector<int> parent, activated;
  vector<long long> group_size;
  long long mx = 0;
  UnionFind(int n) {
    group_size = vector<long long>(n);
    parent = vector<int>(n);
    activated = vector<int>(n);
    for (int i = 0; i < n; i++) {
      parent[i] = i;
      group_size[i] = a[i];
      activated[i] = 0;
    }
  }
  int find_set(int node) {
    if (parent[node] == node) return node;
    return parent[node] = find_set(parent[node]);
  }
  bool same_group(int node1, int node2) {
    int leader1 = find_set(node1);
    int leader2 = find_set(node2);
    return leader1 == leader2;
  }
  void merge_groups(int node1, int node2) {
    if (!activated[node1] || !activated[node2]) return;
    int leader1 = find_set(node1);
    int leader2 = find_set(node2);
    if (group_size[leader1] > group_size[leader2]) {
      parent[leader2] = leader1;
      group_size[leader1] += group_size[leader2];
    } else {
      parent[leader1] = leader2;
      group_size[leader2] += group_size[leader1];
    }
  }
  long long get_size(int node) { return group_size[find_set(node)]; }
  void activate(int node) {
    activated[node] = 1;
    if (node + 1 < parent.size()) merge_groups(node, node + 1);
    if (node - 1 >= 0) merge_groups(node, node - 1);
    mx = max(mx, get_size(node));
  }
};
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  int order[n];
  for (int i = 0; i < n; i++) cin >> order[i];
  vector<long long> sol;
  UnionFind uf(n);
  for (int i = n - 1; i >= 0; i--) {
    sol.push_back(uf.mx);
    uf.activate(order[i] - 1);
  }
  for (int i = n - 1; i >= 0; i--) cout << sol[i] << '\n';
}
