#include <bits/stdc++.h>
using namespace std;
static int f = []() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  return 0;
}();
int find(int i, vector<pair<int, long long>> &par) {
  int p = par[i].first;
  if (p != i) {
    p = find(p, par);
  }
  return p;
}
long long unionR(int i, vector<int> &v, vector<pair<int, long long>> &par) {
  int n = v.size();
  par[i] = {i, v[i]};
  int prev = (i - 1 >= 0 ? i - 1 : 0);
  int next = (i + 1 < n ? i + 1 : n - 1);
  if (prev != i && par[prev].first != -1) {
    int pprev = find(prev, par);
    par[pprev].first = i;
    par[i].second += par[pprev].second;
  }
  if (next != i && par[next].first != -1) {
    int pnext = find(next, par);
    par[pnext].first = i;
    par[i].second += par[pnext].second;
  }
  return par[i].second;
}
int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  vector<int> del(n);
  for (int i = 0; i < n; i++) {
    cin >> del[i];
  }
  vector<pair<int, long long>> par(n, {-1, 0});
  vector<long long> ans = {0};
  for (int i = n - 1; i > 0; i--) {
    int id = del[i] - 1;
    long long x = unionR(id, v, par);
    x = max(ans.back(), x);
    ans.push_back(x);
  }
  for (int i = ans.size() - 1; i >= 0; i--) {
    cout << ans[i] << endl;
  }
  return 0;
}
