#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  long long n, i, x, mx, xx;
  cin >> n;
  vector<long long> sum(n + 3, 0), v(n + 3), seg, ans;
  set<long long> s;
  for (i = 1; i <= n; i++) {
    cin >> v[i];
  }
  for (i = 1; i <= n; i++) {
    sum[i] = sum[i - 1] + v[i];
  }
  for (i = 0; i < n; i++) {
    cin >> x;
    seg.push_back(x);
  }
  for (i = 0; i <= n + 1; i++) {
    s.insert(i);
  }
  reverse(seg.begin(), seg.end());
  mx = 0;
  ans.push_back(0);
  for (i = 0; i < n - 1; i++) {
    x = seg[i];
    auto it = s.find(x);
    auto i1 = it;
    i1--;
    auto i2 = it;
    i2++;
    xx = sum[(*i2) - 1] - sum[*i1];
    if (xx > mx) {
      mx = xx;
      ans.push_back(mx);
    } else {
      ans.push_back(mx);
    }
    s.erase(it);
  }
  reverse(ans.begin(), ans.end());
  for (i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
}
