#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long inf = 4e18;
const long long N = 5e5 + 5;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long tt = 1;
  while (tt--) {
    long long n;
    cin >> n;
    long long arr[n + 1];
    for (long long i = 1; i <= n; ++i) cin >> arr[i];
    long long dest[n + 1];
    for (long long i = 1; i <= n; ++i) cin >> dest[i];
    long long pre[n + 1];
    pre[0] = 0;
    for (long long i = 1; i <= n; ++i) {
      pre[i] = pre[i - 1] + arr[i];
    }
    set<long long> st;
    st.insert(0);
    st.insert(n + 1);
    multiset<long long> mst;
    mst.insert(pre[n] - pre[0]);
    for (long long i = 1; i <= n; ++i) {
      long long cur = dest[i];
      auto left = st.lower_bound(cur);
      auto right = left;
      --left;
      long long l = *left, r = *right;
      mst.erase(mst.find(pre[r - 1] - pre[l]));
      mst.insert(pre[r - 1] - pre[cur]);
      mst.insert(pre[cur - 1] - pre[l]);
      st.insert(cur);
      cout << (*mst.rbegin()) << "\n";
    }
  }
  return 0;
}
