#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
using namespace std;
int64_t n;
vector<int64_t> arr, qu, res;
vector<int64_t> link, cnt, sum;
vector<bool> used;
void init() {
  arr.resize(n);
  qu.resize(n);
  res.resize(n);
  used.assign(n, false);
  link.resize(n), iota(link.begin(), link.end(), 0ll);
  cnt.assign(n, 1);
}
int64_t root(int64_t v) {
  if (v == link[v]) return v;
  return link[v] = root(link[v]);
}
void unite(int64_t v, int64_t u) {
  v = root(v), u = root(u);
  if (v == u) return;
  if (cnt[v] < cnt[u]) swap(v, u);
  cnt[v] += cnt[u], sum[v] += sum[u];
  link[u] = link[v];
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n, init();
  for (auto &el : arr) cin >> el;
  sum = arr;
  for (auto &el : qu) cin >> el;
  for (auto &x : qu) --x;
  reverse(qu.begin(), qu.end());
  auto it = res.rbegin();
  int64_t ans = 0;
  for (int64_t q : qu) {
    *it = ans, ++it, used[q] = true;
    if (q != n - 1 && used[q + 1]) unite(q, q + 1);
    if (q && used[q - 1]) unite(q, q - 1);
    ans = max(ans, sum[root(q)]);
  }
  for (int64_t r : res) cout << r << '\n';
}
