#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int b[N], l[N], r[N];
bool ok[N];
long long s[N], ans[N], a[N];
int main() {
  int n, i;
  long long cur = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; ++i) scanf("%I64d", &a[i]);
  for (i = 1; i <= n; ++i) scanf("%d", &b[i]);
  for (i = 1; i <= n; ++i) l[i] = r[i] = i;
  for (i = n; i; --i) {
    ans[i] = cur;
    if (ok[b[i] - 1]) {
      if (ok[b[i] + 1]) {
        r[l[b[i] - 1]] = r[b[i] + 1];
        l[r[b[i] + 1]] = l[b[i] - 1];
        cur = max(cur, s[r[b[i] + 1]] = s[l[b[i] - 1]] =
                           (a[b[i]] + s[b[i] - 1] + s[b[i] + 1]));
      } else {
        l[b[i]] = l[b[i] - 1];
        r[l[b[i] - 1]] = b[i];
        cur = max(cur, s[b[i]] = s[l[b[i]]] = (a[b[i]] + s[b[i] - 1]));
      }
    } else {
      if (ok[b[i] + 1]) {
        l[r[b[i] + 1]] = b[i];
        r[b[i]] = r[b[i] + 1];
        cur = max(cur, s[b[i]] = s[r[b[i]]] = (a[b[i]] + s[b[i] + 1]));
      } else {
        cur = max(cur, s[b[i]] = a[b[i]]);
      }
    }
    ok[b[i]] = true;
  }
  for (i = 1; i <= n; ++i) printf("%I64d\n", ans[i]);
  return 0;
}
