#include <bits/stdc++.h>
using namespace std;
int a[100100], p[100100];
long long pr[100100];
set<pair<int, char> > S;
multiset<long long> SS;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> p[i];
  pr[0] = 0;
  for (int i = 1; i <= n; i++) pr[i] = pr[i - 1] + a[i];
  SS.insert(pr[n]);
  S.insert({1, 0}), S.insert({n, 1});
  for (int pp = 1; pp <= n; pp++) {
    int x = p[pp];
    auto it = S.lower_bound({x, 0});
    if (it->second) it--;
    int lx = it->first;
    it++;
    int rx = it->first;
    SS.erase(SS.find(pr[rx] - pr[lx - 1]));
    SS.insert(pr[x - 1] - pr[lx - 1]);
    SS.insert(pr[rx] - pr[x]);
    cout << *SS.rbegin() << '\n';
    if (rx > 1) S.insert({x - 1, 1});
    if (lx + 1 <= n) S.insert({x + 1, 0});
  }
  S.clear(), SS.clear();
  return 0;
};
