#include <bits/stdc++.h>
using namespace std;
long long pwr(long long base, long long p, long long mod = (1000000007LL)) {
  long long ans = 1;
  while (p) {
    if (p & 1) ans = (ans * base) % mod;
    base = (base * base) % mod;
    p /= 2;
  }
  return ans;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int n;
long long arr[100005], ps[100005];
set<pair<int, int> > intervals;
multiset<long long> sums;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &arr[i]);
    ps[i] = ps[i - 1] + arr[i];
  }
  intervals.insert({1, n});
  sums.insert(ps[n]);
  sums.insert(0);
  int iter = n;
  while (iter--) {
    int pos;
    scanf("%d", &pos);
    set<pair<int, int> >::iterator it = intervals.lower_bound({pos + 1, -1});
    it--;
    int l = (*it).first, r = (*it).second;
    intervals.erase(it);
    sums.erase(sums.find(ps[r] - ps[l - 1]));
    if (pos == l) {
      intervals.insert({l + 1, r});
      sums.insert(ps[r] - ps[l]);
    } else if (pos == r) {
      intervals.insert({l, r - 1});
      sums.insert(ps[r - 1] - ps[l - 1]);
    } else {
      intervals.insert({l, pos - 1});
      sums.insert(ps[pos - 1] - ps[l - 1]);
      intervals.insert({pos + 1, r});
      sums.insert(ps[r] - ps[pos]);
    }
    printf("%lld\n", (*sums.rbegin()));
  }
  return 0;
}
