#include <bits/stdc++.h>
using namespace std;
template <class T, class L>
inline bool smax(T& x, L y) {
  return x < y ? (x = y, true) : false;
}
template <class T, class L>
inline bool smin(T& x, L y) {
  return x > y ? (x = y, true) : false;
}
inline int find_par(int v);
constexpr long long MAXN = 1e5 + 15;
long long n;
long long a[MAXN], b[MAXN];
bool mark[MAXN];
vector<long long> ans;
long long par[MAXN];
long long s[MAXN];
int main() {
  ios_base::sync_with_stdio(false), 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];
  ans.push_back(0);
  for (int i = 1; i <= n; i++) {
    par[i] = i;
    s[i] = a[i];
  }
  long long mx = 0;
  for (int i = n; i > 1; i--) {
    mark[b[i]] = true;
    if (!mark[b[i] + 1] && mark[b[i] - 1]) {
      par[b[i]] = b[i] - 1;
      s[find_par(b[i])] += a[b[i]];
      smax(mx, s[find_par(b[i])]);
    } else if (mark[b[i] + 1] && !mark[b[i] - 1]) {
      par[b[i]] = b[i] + 1;
      s[find_par(b[i])] += a[b[i]];
      smax(mx, s[find_par(b[i])]);
    } else if (mark[b[i] + 1] && mark[b[i] - 1]) {
      int x = find_par(b[i] + 1);
      int y = find_par(b[i] - 1);
      s[x] += s[b[i]] + s[y];
      for (int i = y; i < x; i++) par[i] = i + 1;
      smax(mx, s[x]);
    }
    if (mx < a[b[i]]) mx = a[b[i]];
    ans.push_back(mx);
  }
  reverse(ans.begin(), ans.end());
  for (auto i : ans) cout << i << '\n';
  return false;
}
inline int find_par(int v) {
  if (v == par[v]) return v;
  return par[v] = find_par(par[v]);
}
