#include <bits/stdc++.h>
using namespace std;
long long int sum() { return 0; }
template <typename T, typename... Args>
T sum(T a, Args... args) {
  return a + sum(args...);
}
template <typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& a) {
  in >> a.first >> a.second;
  return in;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> a) {
  out << a.first << " " << a.second;
  return out;
}
const long long INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;
const long long int N = 1e5 + 5;
long long int fnd[N];
long long int id[N];
long long int v[N];
struct DSU {
  long long int par[N], siz[N];
  void init(long long int n) {
    for (long long int i = 1; i <= n; i++) {
      par[i] = i;
      siz[i] = 1;
    }
  }
  long long int find_par(long long int k) {
    while (k != par[k]) {
      par[k] = par[par[k]];
      k = par[k];
    }
    return k;
  }
  void merge(long long int u, long long int v) {
    long long int par1 = find_par(u), par2 = find_par(v);
    if (par1 == par2) return;
    if (siz[par1] > siz[par2]) swap(par1, par2);
    siz[par2] += siz[par1];
    siz[par1] = 0;
    par[par1] = par[par2];
  }
};
void solve() {
  long long int n;
  cin >> n;
  for (long long int i = 1; i <= n; i++) cin >> v[i];
  for (long long int i = 1; i <= n; i++) {
    cin >> id[i];
  }
  reverse(id + 1, id + n + 1);
  for (long long int i = 1; i <= n; i++) fnd[id[i]] = v[id[i]];
  DSU dsu;
  dsu.init(n + 3);
  vector<long long int> aa;
  aa.push_back(0);
  long long int ans = 0;
  bool vis[n + 2];
  memset(vis, false, sizeof(vis));
  for (long long int i = 1; i <= n; i++) {
    long long int idx = id[i];
    if (vis[idx - 1] == false and vis[idx + 1] == false) {
      ans = max(ans, fnd[idx]);
    } else if (vis[idx - 1] == true and vis[idx + 1] == false) {
      long long int a = fnd[dsu.find_par(idx - 1)];
      long long int b = fnd[idx];
      dsu.merge(idx - 1, idx);
      fnd[dsu.find_par(idx)] = a + b;
      ans = max(ans, a + b);
    } else if (vis[idx + 1] == true and vis[idx - 1] == false) {
      long long int a = fnd[dsu.find_par(idx + 1)];
      long long int b = fnd[idx];
      dsu.merge(idx + 1, idx);
      fnd[dsu.find_par(idx)] = a + b;
      ans = max(ans, a + b);
    } else {
      long long int a = fnd[dsu.find_par(idx - 1)];
      long long int b = fnd[dsu.find_par(idx + 1)];
      long long int c = fnd[idx];
      dsu.merge(idx, idx - 1);
      dsu.merge(idx + 1, idx);
      long long int p = dsu.find_par(idx);
      fnd[p] = a + b + c;
      ans = max(ans, a + b + c);
    }
    vis[idx] = true;
    aa.push_back(ans);
  }
  aa.pop_back();
  reverse(aa.begin(), aa.end());
  for (auto i : aa) cout << i << "\n";
  cout << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int t = 1;
  while (t--) {
    solve();
  }
}
