#include <bits/stdc++.h>
using namespace std;
const int N = 100500;
bool u[N];
vector<long long> ans;
long long mx, a[N], sum[N];
int n, ord[N], l[N], r[N];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%I64d", &a[i]);
    l[i] = r[i] = i;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &ord[i]);
  }
  if (n == 1) {
    printf("0");
    return 0;
  }
  ans.push_back(0ll);
  u[ord[n]] = 1;
  mx = a[ord[n]];
  sum[ord[n]] = a[ord[n]];
  ans.push_back(mx);
  for (int i = n - 1; i > 1; i--) {
    int curl, curr;
    curl = curr = ord[i];
    if (u[ord[i] - 1]) curl = l[ord[i] - 1];
    if (u[ord[i] + 1]) curr = r[ord[i] + 1];
    sum[curl] = sum[curr] = sum[curl] + sum[curr] + a[ord[i]];
    r[curl] = curr;
    l[curr] = curl;
    u[ord[i]] = 1;
    mx = max(mx, sum[curr]);
    ans.push_back(mx);
  }
  reverse(ans.begin(), ans.end());
  for (auto i : ans) printf("%I64d\n", i);
  return 0;
}
