#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
const int INF = (int)1e9 + 7;
int parent[(int)1e5 + 2];
int deep[(int)1e5 + 2];
long long setSum[(int)1e5 + 2];
int a[(int)1e5 + 2];
inline void input() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
void make_set(int v) {
  parent[v] = v;
  setSum[v] = a[v];
  deep[v] = 0;
}
int find_set(int v) {
  if (v == parent[v]) return v;
  parent[v] = find_set(parent[v]);
  return parent[v];
}
void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) {
    if (deep[a] < deep[b]) swap(a, b);
    if (deep[a] = deep[b]) ++deep[a];
    setSum[parent[a]] += setSum[parent[b]];
    parent[b] = a;
  }
}
int main() {
  input();
  int n;
  cin >> n;
  vector<int> p(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; ++i) {
    cin >> p[i];
  }
  long long mxSum = 0;
  vector<long long> ans = {0};
  for (int i = n; i > 0; --i) {
    make_set(p[i]);
    if (find_set(p[i] - 1) != 0) {
      union_sets(p[i], p[i] - 1);
    }
    if (find_set(p[i] + 1) != 0) {
      union_sets(p[i], p[i] + 1);
    }
    mxSum = max(setSum[p[i]], mxSum);
    ans.push_back(mxSum);
  }
  for (int i = n - 1; i >= 0; --i) {
    cout << ans[i] << '\n';
  }
  return 0;
}
