#include <bits/stdc++.h>
using namespace std;
long long par[100005], a[100005], b[100005], ans, sum, n;
bitset<100005> visit;
vector<long long> v;
long long caripar(long long x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = caripar(par[x]);
  }
}
void join(long long x, long long y) {
  par[caripar(x)] = caripar(y);
  a[caripar(y)] += a[x];
}
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--) {
    par[b[i]] = b[i];
    visit[b[i]] = true;
    if (b[i] > 0 && visit[b[i] - 1] == true) {
      join(b[i] - 1, b[i]);
    }
    if (b[i] < n && visit[b[i] + 1] == true) {
      join(b[i], b[i] + 1);
    }
    sum = a[caripar(b[i])];
    ans = max(ans, sum);
    v.push_back(ans);
  }
  for (int i = v.size() - 2; i >= 0; i--) {
    cout << v[i] << '\n';
  }
  cout << 0 << '\n';
  return 0;
}
