#include <bits/stdc++.h>
using namespace std;
long long p[100099], a[100099], f1[2][100099], ans[100099], k, t, n;
int main() {
  cin >> n;
  for (long long i = 1; i < n + 1; i++) {
    cin >> p[i];
    p[i] += p[i - 1], f1[0][i] = i - 1, f1[1][i] = i;
  }
  for (long long i = 1; i < n + 1; i++) cin >> a[i];
  for (long long i = n; i >= 1; i--) {
    k = p[f1[1][a[i]]] - p[f1[0][a[i]]];
    f1[1][f1[0][a[i]]] = f1[1][a[i]];
    f1[0][f1[1][a[i]] + 1] = f1[0][a[i]];
    ans[i - 1] = max(ans[i], k);
  }
  for (long long i = 1; i < n + 1; i++) cout << ans[i] << endl;
}
