#include <bits/stdc++.h>
using namespace std;
long long arr[100005];
long long orders[100005];
long long cumArr[100005];
int main() {
  ios_base::sync_with_stdio(0);
  long long n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> arr[i];
  for (int i = 1; i <= n; i++) cin >> orders[i];
  for (int i = 0; i <= n + 1; i++) cumArr[i] = -1;
  vector<long long> ans;
  map<long long, long long> mym;
  long long maxi = 0;
  for (int i = n; i >= 1; i--) {
    ans.push_back(maxi);
    if (cumArr[orders[i] - 1] != -1 && cumArr[orders[i] + 1] != -1) {
      long long x =
          arr[orders[i]] + cumArr[orders[i] - 1] + cumArr[orders[i] + 1];
      maxi = max(maxi, x);
      cumArr[mym[orders[i] - 1]] = x;
      cumArr[mym[orders[i] + 1]] = x;
      long long tempRight = mym[orders[i] + 1];
      long long tempLeft = mym[orders[i] - 1];
      mym[tempRight] = tempLeft;
      mym[tempLeft] = tempRight;
    } else if (cumArr[orders[i] - 1] != -1) {
      long long x = arr[orders[i]] + cumArr[orders[i] - 1];
      maxi = max(maxi, x);
      cumArr[mym[orders[i] - 1]] = x;
      cumArr[orders[i]] = x;
      long long tempLeft = mym[orders[i] - 1];
      mym[orders[i]] = mym[orders[i] - 1];
      mym[tempLeft] = orders[i];
    } else if (cumArr[orders[i] + 1] != -1) {
      long long x = arr[orders[i]] + cumArr[orders[i] + 1];
      maxi = max(maxi, x);
      cumArr[mym[orders[i] + 1]] = x;
      cumArr[orders[i]] = x;
      long long tempRight = mym[orders[i] + 1];
      mym[orders[i]] = mym[orders[i] + 1];
      mym[tempRight] = orders[i];
    } else {
      long long x = arr[orders[i]];
      maxi = max(maxi, x);
      cumArr[orders[i]] = x;
      mym[orders[i]] = orders[i];
    }
  }
  for (int i = ans.size() - 1; i >= 0; i--) cout << ans[i] << endl;
  return 0;
}
