#include <bits/stdc++.h>
using namespace std;
long long A[100000 + 1];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> A[i];
  for (int i = 2; i <= n; ++i) A[i] += A[i - 1];
  set<int> ends;
  ends.insert(n + 1);
  multiset<long long> values;
  values.insert(A[n]);
  int delim;
  for (int i = 0; i < n; ++i) {
    cin >> delim;
    auto after = ends.upper_bound(delim);
    if (after == ends.begin()) {
      auto target = values.find(A[(*after) - 1]);
      values.erase(target, next(target, 1));
      values.insert(A[(*after) - 1] - A[delim]);
      values.insert(A[delim - 1]);
    } else {
      auto before = prev(after, 1);
      auto target = values.find(A[(*after) - 1] - A[*before]);
      values.erase(target, next(target, 1));
      values.insert(A[(*after) - 1] - A[delim]);
      values.insert(A[delim - 1] - A[*before]);
    }
    ends.insert(delim);
    cout << *values.rbegin() << endl;
  }
}
