#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
vector<long long> a, b;
bool vis[100005];
long long ans;
const int MAX = 100005;
long long parent[MAX], rnk[MAX], sum[MAX];
void MAKESET(int N) {
  for (int i = 1; i <= N; ++i) {
    parent[i] = i;
    sum[i] = a[i];
  }
}
int FIND(int x) {
  if (parent[x] != x) parent[x] = FIND(parent[x]);
  return parent[x];
}
void UNION(int x, int y) {
  int xRoot = FIND(x);
  int yRoot = FIND(y);
  if (xRoot == yRoot) return;
  ans = max(ans, sum[xRoot] + sum[yRoot]);
  if (rnk[xRoot] > rnk[yRoot]) {
    parent[yRoot] = xRoot;
    sum[xRoot] += sum[yRoot];
  } else if (rnk[xRoot] < rnk[yRoot]) {
    parent[xRoot] = yRoot;
    sum[yRoot] += sum[xRoot];
  } else {
    parent[yRoot] = xRoot;
    sum[xRoot] += sum[yRoot];
    rnk[xRoot] += 1;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  a = vector<long long>(n + 1);
  b = vector<long long>(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; ++i) {
    cin >> b[i];
  }
  MAKESET(n);
  vector<long long> res(n);
  for (int i = n; i >= 1; --i) {
    res[n - i] = ans;
    ans = max(ans, a[b[i]]);
    vis[b[i]] = true;
    if (vis[b[i] - 1]) UNION(b[i], b[i] - 1);
    if (vis[b[i] + 1]) UNION(b[i], b[i] + 1);
  }
  for (int i = n - 1; i >= 0; --i) {
    cout << res[i] << "\n";
  }
  return 0;
}
