#include <bits/stdc++.h>
using namespace std;
struct trio {
  int first;
  int second;
  int third;
};
struct long_trio {
  long long first;
  long long second;
  long long third;
};
double dist_point_line(int x1, int y1, int x2, int y2, int x3, int y3) {
  double distance = abs((y3 - y1) * x2 - (x3 - x1) * y2 + x3 * y1 - x1 * y3) /
                    (2 * sqrt((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1)));
  return distance;
}
double dist_point_point(int x1, int y1, int x2, int y2) {
  double distance = sqrt((x1 - y1) * (x1 - y1) + (x2 - y2) * (x2 - y2));
  return distance;
}
double inq(double k, int q) {
  if (q == 0) return 1;
  double l = inq(k, q / 2);
  if (q % 2 == 0)
    return l * l;
  else
    return l * l * k;
}
long long gcd(long long a, long long b) {
  if (a < b) swap(a, b);
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long cubr(long long a) {
  long long l = -1, r = 1e6 + 2;
  while (l < r - 1) {
    long long mid = (l + r) / 2;
    if (mid * mid * mid > a)
      r = mid;
    else
      l = mid;
  }
  return l;
}
const long long INF = 1e18 + 2;
const int LIT = 505;
const int BIG = 200001;
string months[12] = {"January",   "February", "March",    "April",
                     "May",       "June",     "July",     "August",
                     "September", "October",  "November", "December"};
int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
long long fact[13] = {1,    1,     2,      6,       24,       120,      720,
                      5040, 40320, 362280, 3622800, 39916800, 479001600};
int cx[6] = {1, -1, 0, 0, 0, 0};
int cy[6] = {0, 0, 1, -1, 0, 0};
int cz[6] = {0, 0, 0, 0, 1, -1};
long long n, m, ans1, ans2;
long long k, a, b;
long long arr[510000];
long long bs[510000];
long long c[510000];
vector<long long> ans;
int lowes[510000];
int high[510000];
int main() {
  memset((lowes), -1, sizeof(lowes));
  ;
  memset((high), -1, sizeof(high));
  ;
  cin >> n;
  for (int(i) = 0; (i) != (n); i++) {
    cin >> arr[i + 1];
    bs[i + 1] = arr[i + 1] + bs[i];
  }
  for (int(i) = 0; (i) != (n); i++) {
    cin >> c[i];
  }
  for (int i = n - 1; i >= 0; i--) {
    long long p = c[i];
    if (lowes[p - 1] != -1) {
      lowes[p] = lowes[p - 1];
    } else {
      lowes[p] = p;
    }
    if (high[p + 1] != -1) {
      high[p] = high[p + 1];
    } else {
      high[p] = p;
    }
    high[lowes[p]] = high[p];
    lowes[high[p]] = lowes[p];
    ans.push_back(ans1);
    ans1 = max(ans1, bs[high[p]] - bs[lowes[p] - 1]);
  }
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[ans.size() - i - 1] << endl;
  }
}
