#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int l[100005], n, a[100005], b[100005];
long long s[100005];
int main() {
  int i, j, k;
  cin >> n;
  for (i = 1; i <= n; i++) scanf("%d", &a[i]), s[i] = s[i - 1] + a[i];
  for (i = 1; i <= n; i++) scanf("%d", &b[i]);
  set<int> se;
  multiset<long long> s1;
  multiset<long long>::iterator it1;
  set<int>::iterator it;
  se.insert(n);
  s1.insert(s[n]);
  s1.insert(0);
  l[n] = 1;
  for (i = 1; i <= n; i++) {
    it = se.lower_bound(b[i]);
    int now = b[i];
    int R = *it, L = l[R];
    if (L < b[i] && R > b[i]) {
      se.insert(now - 1);
      l[now - 1] = L;
      l[R] = now + 1;
      it1 = s1.lower_bound(s[R] - s[L - 1]);
      s1.erase(it1);
      s1.insert(s[R] - s[now]);
      s1.insert(s[now - 1] - s[L - 1]);
    } else if (L < b[i]) {
      se.erase(it);
      se.insert(R - 1);
      it1 = s1.lower_bound(s[R] - s[L - 1]);
      s1.erase(it1);
      s1.insert(s[R - 1] - s[L - 1]);
      l[R - 1] = L;
    } else if (R > b[i]) {
      l[R] = L + 1;
      it1 = s1.lower_bound(s[R] - s[L - 1]);
      s1.erase(it1);
      s1.insert(s[R] - s[L]);
    } else {
      se.erase(it);
      it1 = s1.lower_bound(s[R] - s[L - 1]);
      s1.erase(it1);
    }
    it1 = s1.end();
    it1--;
    long long ans = *it1;
    printf("%I64d\n", ans);
  }
  return 0;
}
