#include <bits/stdc++.h>
using namespace std;
const int INF = 1000 * 1000 * 1000 + 11;
const long long LINF = (long long)INF * INF;
const long double EPS = 1e-6;
bool equal(long double a, long double b = 0) { return fabsl(a - b) < EPS; }
long long get_sum(vector<long long> &p, int left, int right) {
  if (right < left) return 0;
  if (left == 0) return p[right];
  return p[right] - p[left - 1];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<long long> arr(n);
  for (int i = 0; i != n; ++i) {
    cin >> arr[i];
  }
  vector<long long> pr(n);
  pr[0] = arr[0];
  for (int i = 1; i != n; ++i) {
    pr[i] = pr[i - 1] + arr[i];
  }
  set<int> zero_poses_1;
  set<int, greater<int>> zero_poses_2;
  zero_poses_1.insert(-1);
  zero_poses_1.insert(n);
  zero_poses_2.insert(-1);
  zero_poses_2.insert(n);
  multiset<long long, greater<long long>> sums;
  sums.insert(pr.back());
  for (int i = 0; i != n; ++i) {
    int p;
    cin >> p;
    --p;
    int right = *zero_poses_1.lower_bound(p);
    int left = *zero_poses_2.lower_bound(p);
    sums.erase(sums.find(get_sum(pr, left + 1, right - 1)));
    sums.insert(get_sum(pr, left + 1, p - 1));
    sums.insert(get_sum(pr, p + 1, right - 1));
    zero_poses_1.insert(p);
    zero_poses_2.insert(p);
    cout << *sums.begin() << '\n';
  }
  return 0;
}
