#include <bits/stdc++.h>
using namespace std;
long long a[100010], b[100010];
int fa[100010];
bool vis[100010];
priority_queue<long long> q;
vector<long long> ans;
int findfa(int x) { return (fa[x] == x) ? x : fa[x] = findfa(fa[x]); }
void merge(int x, int y) {
  int fx = findfa(x);
  int fy = findfa(y);
  if (fx != fy) {
    fa[fy] = fx;
    a[fx] += a[fy];
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  for (int i = 0; i < 100010; i++) fa[i] = i;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i];
  q.push(0);
  ans.push_back(0);
  for (int i = n; i > 1; i--) {
    int tmp = b[i];
    q.push(a[tmp]);
    if (vis[tmp + 1]) {
      merge(tmp, tmp + 1);
      q.push(a[findfa(tmp)]);
    }
    if (vis[tmp - 1]) {
      merge(tmp - 1, tmp);
      q.push(a[findfa(tmp - 1)]);
    }
    vis[tmp] = true;
    ans.push_back(q.top());
  }
  for (int i = ans.size() - 1; i >= 0; i--) cout << ans[i] << '\n';
  return 0;
}
