#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define pi 3.14159265358979323846
#define pb push_back
#define fi first
#define se second
#define ar array
#define int long long
template<typename T, typename cmp = std::greater<T>>
using pq = priority_queue<T, vector<T>, cmp>;
template<typename T, typename cmp = std::less<T>>
using ordered_set = tree<T, __gnu_pbds::null_type, cmp, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;
void chay()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define task "Hi"
freopen(task".INP", "r", stdin);
freopen(task".OUT", "w", stdout);
}
const int N = 2e5, INF = 2e9+7;
const int block = 600;
const long long INFLL = 2e18+7;
long long M = 1e9+7;
int n, cay[N+6], lg, pa[N+5], mx[N+5], tong[N+5], h[N+5];
deque<int> que[N+5];
void update(int i, int ss)
{
for (; i <= n; i += -i&i)
{
cay[i] += ss;
}
}
int lay(int i)
{
int tong = 0;
for (; i > 0; i -= -i&i)
{
tong += cay[i];
}
return tong;
}
int tim(int e)
{
int tong = 0, vt = 0;
for (int i = lg; i >= 0; i--)
{
int nw = (1<<i) + vt;
if (nw <= n && cay[nw] + tong < e)
{
tong += cay[nw];
vt += (1<<i);
}
}
return vt+1;
}
int timpa(int u)
{
if (pa[u] == u) return u;
return pa[u] = timpa(pa[u]);
}
void hoppa(int u, int v)
{
u = timpa(u);
v = timpa(v);
int x = mx[u];
int y = mx[v];
int kq = tong[u] + tong[v];
int d = x;
//cout<<"--- ";
if (h[x] > h[y])
{
//cout<<1<<'\n';
while (h[que[u].back()] < h[y])
{
int cuoi = que[u].back();
que[u].pop_back();
int bd = que[u].back()+1;
kq += (h[y] - h[cuoi]) * (cuoi - bd + 1);
}
while (h[que[v].front()] != h[y])
{
int bd = que[v].front();
que[v].pop_front();
int cuoi = que[v].front()-1;
kq += (h[y] - h[bd]) * (cuoi - bd + 1);
}
}
if (h[x] < h[y])
{
//cout<<2<<'\n';
d = y;
while (h[que[u].back()] != h[x])
{
int cuoi = que[u].back();
que[u].pop_back();
int bd = que[u].back()+1;
kq += (h[x] - h[cuoi]) * (cuoi - bd + 1);
}
while (h[que[v].front()] < h[x])
{
int bd = que[v].front();
que[v].pop_front();
int cuoi = que[v].front()-1;
kq += (h[x] - h[bd]) * (cuoi - bd + 1);
}
}
if (h[x] == h[y])
{
while (h[que[u].back()] != h[x])
{
int cuoi = que[u].back();
que[u].pop_back();
int bd = que[u].back()+1;
kq += (h[x] - h[cuoi]) * (cuoi - bd + 1);
}
while (h[que[v].front()] != h[x])
{
int bd = que[v].front();
que[v].pop_front();
int cuoi = que[v].front()-1;
kq += (h[x] - h[bd]) * (cuoi - bd + 1);
}
}
if (que[u].size() < que[v].size())
{
while (que[u].size())
{
int e = que[u].back();
que[u].pop_back();
que[v].push_front(e);
}
pa[u] = v;
tong[v] = kq;
mx[v] = d;
}
else
{
while (que[v].size())
{
int e = que[v].front();
que[v].pop_front();
que[u].push_back(e);
}
pa[v] = u;
tong[u] = kq;
mx[u] = d;
}
}
void solve()
{
cin>>n;
for (int i = 1; i <= n; i++)
{
cin>>h[i];
}
for (int i = 1; i <= n; i++)
{
update(i, 1);
que[i].push_back(i);
tong[i] = 0;
pa[i] = i;
mx[i] = i;
}
lg = log2(n);
for (int i = 1; i < n; i++)
{
int d; cin>>d;
int bd = tim(d);
int cuoi = tim(d+1);
hoppa(bd, cuoi);
int e = timpa(bd);
cout<<tong[e]<<'\n';
update(cuoi, -1);
}
}
signed main ()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin>>t;
while (t--)
{
solve();
}
return 0;
}