#include <bits/stdc++.h>
using namespace std;
int n;
long long mx;
int a[100010], b[100010], root[100010], d[100010];
long long s[100010], res[100010];
int get_root(int u) { return root[u] ? root[u] = get_root(root[u]) : u; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i];
  for (int i = n; i >= 1; i--) {
    res[i] = mx;
    int p = get_root(b[i]);
    s[p] += a[b[i]];
    if (d[b[i] - 1]) {
      int q = get_root(b[i] - 1);
      s[p] += s[q];
      root[q] = p;
    }
    if (d[b[i] + 1]) {
      int q = get_root(b[i] + 1);
      s[p] += s[q];
      root[q] = p;
    }
    mx = max(mx, s[p]);
    d[b[i]]++;
  }
  for (int i = 1; i <= n; i++) cout << res[i] << "\n";
  return 0;
}
