#include <bits/stdc++.h>
using namespace std;
long long int a[1000000], visit[1000000], b[1000000];
set<long long int> ms;
int root(int x) {
  while (visit[x] != x) {
    visit[x] = visit[visit[x]];
    x = visit[x];
  }
  return x;
}
void unin(int x, int y) {
  visit[y] = x;
  ms.insert(a[y] + a[x]);
  a[x] = a[y] + a[x];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n, sum = 0, count = 0, m, flag = 0, maxi = -1;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i], visit[i] = i;
  vector<long long int> test, ans;
  set<long long int>::iterator it;
  for (int i = 0; i < n; i++) {
    long long int x;
    cin >> x;
    test.push_back(x);
  }
  ans.push_back(0);
  for (int i = n - 1; i >= 0; i--) {
    long long int x = test[i];
    if (b[x + 1] && b[x - 1]) {
      unin(root(x + 1), root(x));
      unin(root(x), root(x - 1));
    } else if (b[x + 1]) {
      unin(root(x + 1), root(x));
    } else if (b[x - 1])
      unin(root(x - 1), root(x));
    else
      ms.insert(a[x]);
    b[x] = 1;
    it = ms.end();
    it--;
    ans.push_back(*it);
  }
  for (int i = ans.size() - 2; i >= 0; i--) cout << ans[i] << endl;
  return 0;
}
