#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
int n;
map<int, int> mp;
multiset<long long> ms;
long long sum[MAXN];
int main() {
  scanf("%d", &n);
  sum[0] = 0;
  int a;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a);
    sum[i] = sum[i - 1] + (long long)a;
  }
  mp[0] = n;
  ms.insert(-sum[n] + sum[0]);
  int c;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &c);
    auto ite = mp.lower_bound(c);
    ite--;
    int l = ite->first;
    int r = ite->second;
    mp.erase(ite);
    ms.erase(ms.find(sum[l] - sum[r]));
    if (l < c - 1) {
      ms.insert(sum[l] - sum[c - 1]);
      mp[l] = c - 1;
    }
    if (c < r) {
      ms.insert(sum[c] - sum[r]);
      mp[c] = r;
    }
    if (i != n)
      printf("%I64d\n", -*ms.begin());
    else
      puts("0");
  }
}
