#include <bits/stdc++.h>
using namespace std;
class DisjointSet {
  vector<long long int> parent, size, sum;
  long long int n, connected;

 public:
  DisjointSet(long long int n, vector<long long int> intial) : n(n) {
    parent.resize(n);
    size.resize(n);
    sum.resize(n);
    for (long long int i = 0; i < n; i++) {
      parent[i] = i;
      size[i] = 0;
    }
    for (long long int i = 0; i < n; i++) sum[i] = intial[i];
    connected = n;
  }
  void join(long long int a, long long int b) {
    long long int x = findParent(a);
    long long int y = findParent(b);
    if (x == y) return;
    connected--;
    if (size[x] > size[y]) swap(x, y);
    size[y] += size[x];
    sum[y] += sum[x];
    size[x] = 1;
    parent[x] = parent[y];
  }
  long long int findParent(long long int a) {
    if (a == parent[a]) return a;
    return parent[a] = findParent(parent[a]);
  }
  long long int getSize(long long int k) { return size[findParent(k)]; }
  long long int getSum(long long int x) { return sum[findParent(x)]; }
  void setSize(long long int x, long long int y) { size[x] = y; }
};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int n;
  cin >> n;
  vector<long long int> a(n), b(n);
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (long long int i = 0; i < n; i++) {
    cin >> b[i];
    b[i]--;
  }
  reverse(b.begin(), b.end());
  DisjointSet ds(n, a);
  stack<long long int> stk;
  stk.push(0);
  long long int ans = 0;
  for (long long int i = 0; i < n - 1; i++) {
    long long int ele = b[i];
    if (ele > 0 && ds.getSize(ele - 1) > 0) ds.join(ele, ele - 1);
    if (ele < n - 1 && ds.getSize(ele + 1) > 0) ds.join(ele, ele + 1);
    ans = max(ans, ds.getSum(b[i]));
    ds.setSize(b[i], 1);
    stk.push(ans);
  }
  while (stk.size()) {
    cout << stk.top() << "\n";
    stk.pop();
  }
  return 0;
}
