#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  long long sum[100100];
  int p[100100];
  multiset<long long> s;
  set<int> l;
  set<int> r;
  cin >> n;
  sum[0] = 0;
  for (int i = 1; i <= n; i++) {
    long long x;
    cin >> x;
    sum[i] = sum[i - 1] + x;
  }
  s.insert(sum[n]);
  for (int i = 0; i < n; i++) {
    int p;
    cin >> p;
    set<int>::iterator itl, itr;
    itl = l.lower_bound(-p);
    itr = r.lower_bound(p);
    int ll = (itl == l.end()) ? 0 : -(*itl);
    int rr = (itr == r.end()) ? n + 1 : *itr;
    r.insert(p);
    l.insert(-p);
    long long to_erase = sum[rr - 1] - sum[ll];
    s.erase(s.equal_range(to_erase).first);
    s.insert(sum[p - 1] - sum[ll]);
    s.insert(sum[rr - 1] - sum[p]);
    multiset<long long>::iterator it;
    it = s.end();
    cout << *(--it) << endl;
  }
  return 0;
}
