#include <bits/stdc++.h>
using namespace std;

const int N = 220000;

int p[N], a[N];

struct Node {
	long long Min, add;
} node[1 << 20];

inline void update(int id, int add) {
	node[id].Min += add, node[id].add += add;
}

inline void down(int id) {
	if (node[id].add) {
		update(id + id, node[id].add);
		update(id + id + 1, node[id].add);
		node[id].add = 0;
	}
}

inline void up(int id) {
	node[id].Min = min(node[id + id].Min, node[id + id + 1].Min);
}

inline void modify(int id, int st, int en, int l, int r, int add) {
	if (l > r) return;
	if (l <= st && en <= r) {
		update(id, add);
		return;
	}
	down(id);
	int mid = st + en >> 1;
	if (l <= mid) modify(id + id, st, mid, l, r, add);
	if (r > mid) modify(id + id + 1, mid + 1, en, l, r, add);
	up(id);
}

int main() {

	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	int n; cin >> n;
	for (int i = 1; i <= n; i++) cin >> p[i];
	for (int i = 1; i <= n; i++) cin >> a[p[i]];

	for (int i = 1; i <= n; i++) modify(1, 1, n, i, n, a[i]);

	long long ans = 1LL << 60;
	for (int i = 1; i < n; i++) {
		modify(1, 1, n, p[i], n, -a[p[i]]);
		modify(1, 1, n, 1, p[i] - 1, a[p[i]]);
		ans = min(ans, (long long)node[1].Min);
	}
	if(n==1) ans = 0;
	cout << ans << "\n";

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const long long inf = 1e18;
const int MAXN = 200005;
ll p[MAXN], a[MAXN];

ll xmin[MAXN], dn[MAXN];
ll sum[MAXN];

void up(int u) {
	xmin[u] = min(xmin[u + u], xmin[u + u + 1]);
}

void renew(int u, ll x) {
	xmin[u] += x, dn[u] += x;
}

void down(int u) {
	if (!dn[u]) return;
	renew(u + u, dn[u]);
	renew(u + u + 1, dn[u]);
	dn[u] = 0;
}

void build(int u, int l, int r, ll *a) {
	dn[u] = 0;
	if (l == r) {
		xmin[u] = a[l];
		return;
	}
	int mid = l + r >> 1;
	build(u + u, l, mid, a);
	build(1 + u + u, mid + 1, r, a);
	up(u);
}

void modify(int u, int l, int r, int ll, int rr, int x) {
	if (ll <= l && r <= rr) {
		renew(u, x);
		return;
	}
	int mid = l + r >> 1;
	down(u);
	if (ll <= mid) modify(u + u, l, mid, ll, rr, x);
	if (mid < rr) modify(u + u + 1, mid + 1, r, ll, rr, x);
	up(u);
}

ll query(int u, int l, int r, int ll, int rr) {
	if (ll <= l && r <= rr) return xmin[u];
	int mid = l + r >> 1;
	long long ret = inf;
	down(u);
	if (ll <= mid) ret = min(ret, query(u + u, l, mid, ll, rr));
	if (mid < rr) ret = min(ret, query(u + u + 1, mid + 1, r, ll, rr));
	return ret;
}

int pos[MAXN];
int main() {
	int n;
	cin >> n;
	for(int i = 0; i < n; i ++) cin >> p[i];
	for(int i = 0; i < n; i ++){
		pos[p[i]] = i;
	}
	for(int i = 0; i < n; i ++) cin >> a[i];
	ll cur = 0;
	for(int i = 0; i < n - 1; i ++){
		cur += a[i];
		sum[i + 1] = cur;
	}
	build(1, 1, n - 1, sum);
	ll ans = inf;
	for(int i = 1; i <= n; i ++){
		int tmp = pos[i];
		if(tmp >= 1) modify(1, 1, n - 1, 1, tmp, a[tmp]);
		if(n - 1 >= tmp + 1) modify(1, 1, n - 1, tmp + 1, n - 1, -a[tmp]);
		ans = min(ans, query(1, 1, n - 1, 1, n - 1));
	}
	cout << ans << '\n';
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 220000;

int p[N], a[N];

struct Node {
	long long Min, add;
} node[1 << 20];

inline void update(int id, long long add) {
	node[id].Min += add, node[id].add += add;
}

inline void down(int id) {
	if (node[id].add) {
		update(id + id, node[id].add);
		update(id + id + 1, node[id].add);
		node[id].add = 0;
	}
}

inline void up(int id) {
	node[id].Min = min(node[id + id].Min, node[id + id + 1].Min);
}

inline void modify(int id, int st, int en, int l, int r, long long add) {
	if (l > r) return;
	if (l <= st && en <= r) {
		update(id, add);
		return;
	}
	down(id);
	int mid = st + en >> 1;
	if (l <= mid) modify(id + id, st, mid, l, r, add);
	if (r > mid) modify(id + id + 1, mid + 1, en, l, r, add);
	up(id);
}

int main() {

	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	int n; cin >> n;
	for (int i = 1; i <= n; i++) cin >> p[i];
	for (int i = 1; i <= n; i++) cin >> a[p[i]];

	for (int i = 1; i <= n; i++) modify(1, 1, n, i, n, a[i]);

	long long ans = 1LL << 60;
	for (int i = 1; i < n; i++) {
		modify(1, 1, n, p[i], n, -a[p[i]]);
		modify(1, 1, n, 1, p[i] - 1, a[p[i]]);
		ans = min(ans, (long long)node[1].Min);cerr<<ans<<endl;
	}
	if (n == 1) ans = 0;
	long long sum = 0;
	for (int i = 1; i < n; i++) {
		sum += a[p[i]];
		ans = min(ans, sum);
	}
	cout << ans << "\n";

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const long long inf = 1e18;
const int MAXN = 200005;
ll p[MAXN], a[MAXN];

ll xmin[4 * MAXN], dn[4 * MAXN];
ll sum[MAXN];

void up(int u) {
	xmin[u] = min(xmin[u + u], xmin[u + u + 1]);
}

void renew(int u, ll x) {
	xmin[u] += x, dn[u] += x;
}

void down(int u) {
	if (!dn[u]) return;
	renew(u + u, dn[u]);
	renew(u + u + 1, dn[u]);
	dn[u] = 0;
}

void build(int u, int l, int r, ll *a) {
	dn[u] = 0;
	if (l == r) {
		xmin[u] = a[l];
		return;
	}
	int mid = l + r >> 1;
	build(u + u, l, mid, a);
	build(1 + u + u, mid + 1, r, a);
	up(u);
}

void modify(int u, int l, int r, int ll, int rr, int x) {
	if (ll <= l && r <= rr) {
		renew(u, x);
		return;
	}
	int mid = l + r >> 1;
	down(u);
	if (ll <= mid) modify(u + u, l, mid, ll, rr, x);
	if (mid < rr) modify(u + u + 1, mid + 1, r, ll, rr, x);
	up(u);
}

ll query(int u, int l, int r, int ll, int rr) {
	if (ll <= l && r <= rr) return xmin[u];
	int mid = l + r >> 1;
	long long ret = inf;
	down(u);
	if (ll <= mid) ret = min(ret, query(u + u, l, mid, ll, rr));
	if (mid < rr) ret = min(ret, query(u + u + 1, mid + 1, r, ll, rr));
	return ret;
}

int pos[MAXN];
int main() {
	int n;
	cin >> n;
	for(int i = 0; i < n; i ++) cin >> p[i];
	for(int i = 0; i < n; i ++){
		pos[p[i]] = i;
	}
	for(int i = 0; i < n; i ++) cin >> a[i];
	ll cur = 0;
	for(int i = 0; i < n - 1; i ++){
		cur += a[i];
		sum[i + 1] = cur;
	}
	build(1, 1, n - 1, sum);
	ll ans = inf;
	ans = min(ans, query(1, 1, n - 1, 1, n - 1));
	for(int i = 1; i <= n; i ++){
		int tmp = pos[i];
		if(tmp >= 1) modify(1, 1, n - 1, 1, tmp, a[tmp]);
		if(n - 1 >= tmp + 1) modify(1, 1, n - 1, tmp + 1, n - 1, -a[tmp]);
		ans = min(ans, query(1, 1, n - 1, 1, n - 1));
	}
	cout << ans << '\n';
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 220000;

int p[N], a[N];

struct Node {
	long long Min, add;
} node[1 << 20];

inline void update(int id, int add) {
	node[id].Min += add, node[id].add += add;
}

inline void down(int id) {
	if (node[id].add) {
		update(id + id, node[id].add);
		update(id + id + 1, node[id].add);
		node[id].add = 0;
	}
}

inline void up(int id) {
	node[id].Min = min(node[id + id].Min, node[id + id + 1].Min);
}

inline void modify(int id, int st, int en, int l, int r, int add) {
	if (l > r) return;
	if (l <= st && en <= r) {
		update(id, add);
		return;
	}
	down(id);
	int mid = st + en >> 1;
	if (l <= mid) modify(id + id, st, mid, l, r, add);
	if (r > mid) modify(id + id + 1, mid + 1, en, l, r, add);
	up(id);
}

int main() {

	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	int n; cin >> n;
	for (int i = 1; i <= n; i++) cin >> p[i];
	for (int i = 1; i <= n; i++) cin >> a[p[i]];

	for (int i = 1; i <= n; i++) modify(1, 1, n, i, n, a[i]);

	long long ans = 1LL << 60;
	for (int i = 1; i < n; i++) {
		modify(1, 1, n, p[i], n, -a[p[i]]);
		modify(1, 1, n, 1, p[i] - 1, a[p[i]]);
		ans = min(ans, (long long)node[1].Min);
	}
	cout << ans << "\n";

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 220000;

int p[N], a[N];

struct Node {
	long long Min, add;
} node[1 << 20];

inline void update(int id, long long add) {
	node[id].Min += add, node[id].add += add;
}

inline void down(int id) {
	if (node[id].add) {
		update(id + id, node[id].add);
		update(id + id + 1, node[id].add);
		node[id].add = 0;
	}
}

inline void up(int id) {
	node[id].Min = min(node[id + id].Min, node[id + id + 1].Min);
}

inline void modify(int id, int st, int en, int l, int r, long long add) {
	if (l > r) return;
	if (l <= st && en <= r) {
		update(id, add);
		return;
	}
	down(id);
	int mid = st + en >> 1;
	if (l <= mid) modify(id + id, st, mid, l, r, add);
	if (r > mid) modify(id + id + 1, mid + 1, en, l, r, add);
	up(id);
}

int main() {

	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	int n; cin >> n;
	for (int i = 1; i <= n; i++) cin >> p[i];
	for (int i = 1; i <= n; i++) cin >> a[p[i]];

	for (int i = 1; i <= n; i++) modify(1, 1, n, i, n, a[i]);

	long long ans = 1LL << 60;
	for (int i = 1; i < n; i++) {
		modify(1, 1, n, p[i], n, -a[p[i]]);
		modify(1, 1, n, 1, p[i] - 1, a[p[i]]);
		ans = min(ans, (long long)node[1].Min);
	}
	if (n == 1) ans = 0;
	cout << ans << "\n";

	return 0;
}


#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<62);
const int inf=(1<<30);
const int nmax=2e5+50;
const int mod=1e9+7;
using namespace std;
int n,i,p[nmax];
ll vl,st[4*nmax],a[nmax],lzy[4*nmax],rs,cur,sm,cur1;
void push(int nod,int l,int r)
{
    if(!lzy[nod])return;
    st[nod]+=lzy[nod];
    if(l!=r)
    {
    	lzy[nod*2]+=lzy[nod];
		lzy[nod*2+1]+=lzy[nod];
	}
    lzy[nod]=0;
}
void upd(int nod,int l,int r,int tl,int tr,ll v)
{
    push(nod,l,r);
    if(tl<=l && r<=tr)
	{
        lzy[nod]+=v;
        push(nod,l,r);
        return;
    }
    int mid=(l+r)/2;
    if(tl<=mid)upd(nod*2,l,mid,tl,tr,v);
    if(mid<tr)upd(nod*2+1,mid+1,r,tl,tr,v);
    push(nod*2,l,mid);
	push(nod*2+1,mid+1,r);
    st[nod]=min(st[nod*2],st[nod*2+1]);
}
ll qry(int nod,int l,int r,int tl,int tr)
{
    push(nod,l,r);
    if(l>tr || r<tl)return llinf;
    if(tl<=l && r<=tr)return st[nod];
    int mid=(l+r)/2;
    return min(qry(2*nod,l,mid,tl,tr),qry(2*nod+1,mid+1,r,tl,tr));
}
int main()
{
	//freopen("sol.in","r",stdin);
	//freopen("sol.out","w",stdout);
	//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
	cin>>n;
	for(i=1;i<=n;i++)cin>>p[i];
	for(i=1;i<=n;i++)
	{
		cin>>a[i],sm+=a[i];
		if(p[i]==1)vl=a[i];
	}
	for(i=1;i<=n;i++)upd(1,1,n,p[i],n,a[i]);
	rs=llinf;
	for(i=1;i<n;i++)
	{
		upd(1,1,n,p[i],n,-2LL*a[i]);
		cur+=a[i];
		rs=min(rs,cur+min(qry(1,1,n,2,n),0LL));
		rs=min(rs,min(cur,sm-cur));
	}
	cout<<rs<<endl;
	return 0;
}


#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<62);
const int inf=(1<<30);
const int nmax=2e5+50;
const int mod=1e9+7;
using namespace std;
int n,i,p[nmax];
ll vl,st[4*nmax],a[nmax],lzy[4*nmax],rs,cur,sm;
void push(int nod,int l,int r)
{
    if(!lzy[nod])return;
    st[nod]+=lzy[nod];
    if(l!=r)
    {
    	lzy[nod*2]+=lzy[nod];
		lzy[nod*2+1]+=lzy[nod];
	}
    lzy[nod]=0;
}
void upd(int nod,int l,int r,int tl,int tr,ll v)
{
    push(nod,l,r);
    if(tl<=l && r<=tr)
	{
        lzy[nod]+=v;
        push(nod,l,r);
        return;
    }
    int mid=(l+r)/2;
    if(tl<=mid)upd(nod*2,l,mid,tl,tr,v);
    if(mid<tr)upd(nod*2+1,mid+1,r,tl,tr,v);
    push(nod*2,l,mid);
	push(nod*2+1,mid+1,r);
    st[nod]=min(st[nod*2],st[nod*2+1]);
}
ll qry(int nod,int l,int r,int tl,int tr)
{
    push(nod,l,r);
    if(l>tr || r<tl)return llinf;
    if(tl<=l && r<=tr)return st[nod];
    int mid=(l+r)/2;
    return min(qry(2*nod,l,mid,tl,tr),qry(2*nod+1,mid+1,r,tl,tr));
}
int main()
{
	//freopen("sol.in","r",stdin);
	//freopen("sol.out","w",stdout);
	//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
	cin>>n;
	for(i=1;i<=n;i++)cin>>p[i];
	for(i=1;i<=n;i++)
	{
		cin>>a[i],sm+=a[i];
		if(p[i]==1)vl=a[i];
	}
	for(i=1;i<=n;i++)if(p[i]!=1)upd(1,1,n,p[i],n,a[i]);
	rs=llinf;
	for(i=1;i<n;i++)
	{
		if(p[i]==1)vl=0;
		else upd(1,1,n,p[i],n,-2LL*a[i]),cur+=a[i];
		rs=min(rs,vl+cur+qry(1,1,n,2,n));
		//cout<<cur<<" "<<sm-cur<<endl;
		rs=min(rs,min(cur,sm-cur));
	}
	cout<<rs<<endl;
	return 0;
}


#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<62);
const int inf=(1<<30);
const int nmax=2e5+50;
const int mod=1e9+7;
using namespace std;
int n,i,p[nmax];
ll vl,st[4*nmax],a[nmax],lzy[4*nmax],rs,cur,sm,cur1;
void push(int nod,int l,int r)
{
    if(!lzy[nod])return;
    st[nod]+=lzy[nod];
    if(l!=r)
    {
    	lzy[nod*2]+=lzy[nod];
		lzy[nod*2+1]+=lzy[nod];
	}
    lzy[nod]=0;
}
void upd(int nod,int l,int r,int tl,int tr,ll v)
{
    push(nod,l,r);
    if(tl<=l && r<=tr)
	{
        lzy[nod]+=v;
        push(nod,l,r);
        return;
    }
    int mid=(l+r)/2;
    if(tl<=mid)upd(nod*2,l,mid,tl,tr,v);
    if(mid<tr)upd(nod*2+1,mid+1,r,tl,tr,v);
    push(nod*2,l,mid);
	push(nod*2+1,mid+1,r);
    st[nod]=min(st[nod*2],st[nod*2+1]);
}
ll qry(int nod,int l,int r,int tl,int tr)
{
    push(nod,l,r);
    if(l>tr || r<tl)return llinf;
    if(tl<=l && r<=tr)return st[nod];
    int mid=(l+r)/2;
    return min(qry(2*nod,l,mid,tl,tr),qry(2*nod+1,mid+1,r,tl,tr));
}
int main()
{
	//freopen("sol.in","r",stdin);
	//freopen("sol.out","w",stdout);
	//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
	cin>>n;
	for(i=1;i<=n;i++)cin>>p[i];
	for(i=1;i<=n;i++)
	{
		cin>>a[i],sm+=a[i];
		if(p[i]==1)vl=a[i];
	}
	for(i=1;i<=n;i++)upd(1,1,n,p[i],n,a[i]);
	rs=llinf;
	for(i=1;i<n;i++)
	{
		upd(1,1,n,p[i],n,-2LL*a[i]);
		cur+=a[i];
		rs=min(rs,cur+min(qry(1,1,n,1,n),0LL));
		rs=min(rs,min(cur,sm-cur));
	}
	cout<<rs<<endl;
	return 0;
}


#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<62);
const int inf=(1<<30);
const int nmax=2e5+50;
const int mod=1e9+7;
using namespace std;
int n,i,p[nmax];
ll vl,st[4*nmax],a[nmax],lzy[4*nmax],rs,cur,sm,cur1;
void push(int nod,int l,int r)
{
    if(!lzy[nod])return;
    st[nod]+=lzy[nod];
    if(l!=r)
    {
    	lzy[nod*2]+=lzy[nod];
		lzy[nod*2+1]+=lzy[nod];
	}
    lzy[nod]=0;
}
void upd(int nod,int l,int r,int tl,int tr,ll v)
{
    push(nod,l,r);
    if(tl<=l && r<=tr)
	{
        lzy[nod]+=v;
        push(nod,l,r);
        return;
    }
    int mid=(l+r)/2;
    if(tl<=mid)upd(nod*2,l,mid,tl,tr,v);
    if(mid<tr)upd(nod*2+1,mid+1,r,tl,tr,v);
    push(nod*2,l,mid);
	push(nod*2+1,mid+1,r);
    st[nod]=min(st[nod*2],st[nod*2+1]);
}
ll qry(int nod,int l,int r,int tl,int tr)
{
    push(nod,l,r);
    if(l>tr || r<tl)return llinf;
    if(tl<=l && r<=tr)return st[nod];
    int mid=(l+r)/2;
    return min(qry(2*nod,l,mid,tl,tr),qry(2*nod+1,mid+1,r,tl,tr));
}
int main()
{
	//freopen("sol.in","r",stdin);
	//freopen("sol.out","w",stdout);
	//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
	cin>>n;
	for(i=1;i<=n;i++)cin>>p[i];
	for(i=1;i<=n;i++)
	{
		cin>>a[i],sm+=a[i];
		if(p[i]==1)vl=a[i];
	}
	for(i=1;i<=n;i++)if(p[i]!=1)upd(1,1,n,p[i],n,a[i]);
	rs=llinf;
	for(i=1;i<n;i++)
	{
		cur1+=a[i];
		if(p[i]==1)vl=0;
		else upd(1,1,n,p[i],n,-2LL*a[i]),cur+=a[i];
		rs=min(rs,vl+cur+qry(1,1,n,2,n));
		rs=min(rs,min(cur1,sm-cur1));
	}
	cout<<rs<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 200005;

int n, p[N], a[N];

void add(ll *ft, int x, ll val) {
	for(; x < N; x += x & -x)
		ft[x] += val;
}
void add(ll *ft, int i, int j, int val) {
	add(ft, i, val);
	add(ft, j + 1, -val);
}
ll query(const ll *ft, int x) {
	ll ans = 0;
	for(; x > 0; x -= x & -x)
		ans += ft[x];
	return ans;
}
ll query(const ll *ft, int i, int j) {
	return query(ft, j) - query(ft, i - 1);
}
ll ft[N], ft2[N];

ll solve() {
	memset(ft, 0, sizeof ft);
	memset(ft2, 0, sizeof ft2);
	set<int> s;
	s.insert(p[1]);

	for(int i = 2; i <= n; i++) {
		add(ft, p[i], n, a[i]);
	}

	add(ft2, p[1], a[1]);

	auto get = [](int pos, int pv) -> ll {
		return query(ft2, pos, n) + query(ft, pv);
	};

	auto it = s.begin();
	ll ans = get(*it, 0);
	ans = min(ans, query(ft, *it));
	ans = min(ans, query(ft2, n));
	for(int i = 2; i < n; i++) {
		add(ft2, p[i], a[i]);
		add(ft, p[i], n, -a[i]);
		s.insert(p[i]);
		if(it != s.begin()) it--;
		auto prv = [&](set<int>::iterator j) {
			if(j == s.begin()) return 0;
			return *prev(j);
		};
		while(next(it) != s.end()) {
			ll val_a = get(*it, prv(it));
			ll val_b = get(*next(it), *it);
			if(val_a < val_b) break;
			it++;
		}
		ans = min(ans, get(*it, prv(it)));
		ans = min(ans, query(ft, *prev(s.end())));
		ans = min(ans, query(ft2, n));
	}
	ans = min(ans, a[n]);
	return ans;
}

int main() {

	scanf("%d", &n);

	for(int i = 1; i <= n; i++) scanf("%d", p + i);
	for(int i = 1; i <= n; i++) scanf("%d", a + i);

	ll ans = solve();

	// reverse(p + 1, p + n + 1), reverse(a + 1, a + n + 1);
	// for(int i = 1; i <= n; i++) p[i] = n - p[i] + 1;
	// ans = min(ans, solve());

	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 200005;

int n, p[N], a[N];

void add(ll *ft, int x, ll val) {
	for(; x < N; x += x & -x)
		ft[x] += val;
}
void add(ll *ft, int i, int j, int val) {
	add(ft, i, val);
	add(ft, j + 1, -val);
}
ll query(const ll *ft, int x) {
	ll ans = 0;
	for(; x > 0; x -= x & -x)
		ans += ft[x];
	return ans;
}
ll query(const ll *ft, int i, int j) {
	return query(ft, j) - query(ft, i - 1);
}
ll ft[N], ft2[N];

ll solve() {
	memset(ft, 0, sizeof ft);
	memset(ft2, 0, sizeof ft2);
	set<int> s;
	s.insert(p[1]);

	for(int i = 2; i <= n; i++) {
		add(ft, p[i], n, a[i]);
	}

	add(ft2, p[1], a[1]);

	auto get = [](int pos, int pv) -> ll {
		return query(ft2, pos, n) + query(ft, pv);
	};

	auto it = s.begin();
	ll ans = get(*it, 0);
	for(int i = 2; i < n; i++) {
		add(ft2, p[i], a[i]);
		add(ft, p[i], n, -a[i]);
		s.insert(p[i]);
		if(it != s.begin()) it--;
		auto prv = [&](set<int>::iterator j) {
			if(j == s.begin()) return 0;
			return *prev(j);
		};
		while(next(it) != s.end()) {
			ll val_a = get(*it, prv(it));
			ll val_b = get(*next(it), *it);
			if(val_a < val_b) break;
			it++;
		}
		ans = min(ans, get(*it, prv(it)));
		ans = min(ans, query(ft, *prev(s.end())));
		ans = min(ans, query(ft2, n));
	}
	return ans;
}

int main() {

	scanf("%d", &n);

	for(int i = 1; i <= n; i++) scanf("%d", p + i);
	for(int i = 1; i <= n; i++) scanf("%d", a + i);

	ll ans = solve();

	reverse(p + 1, p + n + 1), reverse(a + 1, a + n + 1);
	for(int i = 1; i <= n; i++) p[i] = n - p[i] + 1;
	ans = min(ans, solve());

	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 200005;

int n, p[N], a[N];

void add(ll *ft, int x, ll val) {
	for(; x < N; x += x & -x)
		ft[x] += val;
}
void add(ll *ft, int i, int j, int val) {
	add(ft, i, val);
	add(ft, j + 1, -val);
}
ll query(const ll *ft, int x) {
	ll ans = 0;
	for(; x > 0; x -= x & -x)
		ans += ft[x];
	return ans;
}
ll query(const ll *ft, int i, int j) {
	return query(ft, j) - query(ft, i - 1);
}
ll ft[N], ft2[N];

int main() {

	scanf("%d", &n);

	for(int i = 1; i <= n; i++) scanf("%d", p + i);
	for(int i = 1; i <= n; i++) scanf("%d", a + i);

	set<int> s;
	s.insert(p[1]);

	for(int i = 2; i <= n; i++) {
		add(ft, p[i], n, a[ p[i] ]);
	}

	add(ft2, p[1], a[ p[1] ]);

	auto get = [](int pos, int pv) {
		return query(ft2, pos, n) + query(ft, pv);
	};

	auto it = s.begin();
	ll ans = min<ll>(a[ p[1] ], query(ft, p[1]));
	for(int i = 2; i < n; i++) {
		add(ft2, p[i], a[ p[i] ]);
		add(ft, p[i], n, -a[ p[i] ]);
		s.insert(p[i]);
		if(it != s.begin()) it--;
		auto prv = [&s](set<int>::iterator j) {
			if(j == s.begin()) return 0;
			return *prev(j);
		};
		while(next(it) != s.end()) {
			ll val_a = get(*it, prv(it));
			ll val_b = get(*next(it), *it);
			if(val_a < val_b) break;
			it++;
		}
		ans = min(ans, get(*it, prv(it)));
	}

	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 200005;

int n, p[N], a[N];

void add(ll *ft, int x, ll val) {
	for(; x < N; x += x & -x)
		ft[x] += val;
}
void add(ll *ft, int i, int j, int val) {
	add(ft, i, val);
	add(ft, j + 1, -val);
}
ll query(const ll *ft, int x) {
	ll ans = 0;
	for(; x > 0; x -= x & -x)
		ans += ft[x];
	return ans;
}
ll query(const ll *ft, int i, int j) {
	return query(ft, j) - query(ft, i - 1);
}
ll ft[N], ft2[N];

int main() {

	scanf("%d", &n);

	for(int i = 1; i <= n; i++) scanf("%d", p + i);
	for(int i = 1; i <= n; i++) scanf("%d", a + i);

	set<int> s;
	s.insert(p[1]);

	for(int i = 2; i <= n; i++) {
		add(ft, p[i], n, a[ p[i] ]);
	}

	add(ft2, p[1], a[ p[1] ]);

	auto get = [](int pos, int pv) {
		return query(ft2, pos, n) + query(ft, pv);
	};

	auto it = s.begin();
	ll ans = min<ll>(a[ p[1] ], query(ft, p[1]));
	for(int i = 2; i < n; i++) {
		add(ft2, p[i], a[ p[i] ]);
		add(ft, p[i], n, -a[ p[i] ]);
		s.insert(p[i]);
		if(it != s.begin()) it--;
		auto prv = [s](set<int>::iterator j) {
			if(j == s.begin()) return 0;
			return *prev(j);
		};
		while(next(it) != s.end()) {
			ll val_a = get(*it, prv(it));
			ll val_b = get(*next(it), *it);
			if(val_a < val_b) break;
			it++;
		}
		ans = min(ans, get(*it, prv(it)));
	}

	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 200005;

int n, p[N], a[N];

void add(ll *ft, int x, ll val) {
	for(; x < N; x += x & -x)
		ft[x] += val;
}
void add(ll *ft, int i, int j, int val) {
	add(ft, i, val);
	add(ft, j + 1, -val);
}
ll query(const ll *ft, int x) {
	ll ans = 0;
	for(; x > 0; x -= x & -x)
		ans += ft[x];
	return ans;
}
ll query(const ll *ft, int i, int j) {
	return query(ft, j) - query(ft, i - 1);
}
ll ft[N], ft2[N];

ll solve() {
	memset(ft, 0, sizeof ft);
	memset(ft2, 0, sizeof ft2);
	set<int> s;
	s.insert(p[1]);

	for(int i = 2; i <= n; i++) {
		add(ft, p[i], n, a[i]);
	}

	add(ft2, p[1], a[1]);

	auto get = [](int pos, int pv) -> ll {
		return query(ft2, pos, n) + query(ft, pv);
	};

	auto it = s.begin();
	ll ans = get(*it, 0);
	ans = min(ans, query(ft, *it));
	ans = min(ans, query(ft2, n));
	for(int i = 2; i < n; i++) {
		add(ft2, p[i], a[i]);
		add(ft, p[i], n, -a[i]);
		s.insert(p[i]);
		if(it != s.begin()) it--;
		auto prv = [&](set<int>::iterator j) {
			if(j == s.begin()) return 0;
			return *prev(j);
		};
		while(next(it) != s.end()) {
			ll val_a = get(*it, prv(it));
			ll val_b = get(*next(it), *it);
			if(val_a < val_b) break;
			it++;
		}
		ans = min(ans, get(*it, prv(it)));
		ans = min(ans, query(ft, *prev(s.end())));
		ans = min(ans, query(ft2, n));
	}
	ans = min<ll>(ans, a[n]);
	return ans;
}

int main() {

	scanf("%d", &n);

	for(int i = 1; i <= n; i++) scanf("%d", p + i);
	for(int i = 1; i <= n; i++) scanf("%d", a + i);

	ll ans = solve();

	// reverse(p + 1, p + n + 1), reverse(a + 1, a + n + 1);
	// for(int i = 1; i <= n; i++) p[i] = n - p[i] + 1;
	// ans = min(ans, solve());

	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 200005;

int n, p[N], a[N];

void add(ll *ft, int x, ll val) {
	for(; x < N; x += x & -x)
		ft[x] += val;
}
void add(ll *ft, int i, int j, int val) {
	add(ft, i, val);
	add(ft, j + 1, -val);
}
ll query(const ll *ft, int x) {
	ll ans = 0;
	for(; x > 0; x -= x & -x)
		ans += ft[x];
	return ans;
}
ll query(const ll *ft, int i, int j) {
	return query(ft, j) - query(ft, i - 1);
}
ll ft[N], ft2[N];

int main() {

	scanf("%d", &n);

	for(int i = 1; i <= n; i++) scanf("%d", p + i);
	for(int i = 1; i <= n; i++) scanf("%d", a + i);

	set<int> s;
	s.insert(p[1]);

	for(int i = 2; i <= n; i++) {
		add(ft, p[i], n, a[ p[i] ]);
	}

	add(ft2, p[1], a[ p[1] ]);

	auto get = [](int pos, int pv) {
		return query(ft2, pos, n) + query(ft, pv);
	};

	auto it = s.begin();
	ll ans = min<ll>(a[ p[1] ], query(ft, p[1]));
	for(int i = 2; i < n; i++) {
		add(ft2, p[i], a[ p[i] ]);
		add(ft, p[i], n, -a[ p[i] ]);
		s.insert(p[i]);
		if(it != s.begin()) it--;
		auto prv = [&](set<int>::iterator j) {
			if(j == s.begin()) return 0;
			return *prev(j);
		};
		while(next(it) != s.end()) {
			ll val_a = get(*it, prv(it));
			ll val_b = get(*next(it), *it);
			if(val_a < val_b) break;
			it++;
		}
		ans = min(ans, get(*it, prv(it)));
		ans = min(ans, query(ft, *prev(s.end())));
		ans = min(ans, query(ft2, 1, n));
	}

	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 200005;

int n, p[N], a[N];

void add(ll *ft, int x, ll val) {
	for(; x < N; x += x & -x)
		ft[x] += val;
}
void add(ll *ft, int i, int j, int val) {
	add(ft, i, val);
	add(ft, j + 1, -val);
}
ll query(const ll *ft, int x) {
	ll ans = 0;
	for(; x > 0; x -= x & -x)
		ans += ft[x];
	return ans;
}
ll query(const ll *ft, int i, int j) {
	return query(ft, j) - query(ft, i - 1);
}
ll ft[N], ft2[N];

ll solve() {
	memset(ft, 0, sizeof ft);
	memset(ft2, 0, sizeof ft2);
	set<int> s;
	s.insert(p[1]);

	for(int i = 2; i <= n; i++) {
		add(ft, p[i], n, a[i]);
	}

	add(ft2, p[1], a[1]);

	auto get = [](int pos, int pv) -> ll {
		return query(ft2, pos, n) + query(ft, pv);
	};

	auto it = s.begin();
	ll ans = get(*it, 0);
	ans = min(ans, query(ft, *it));
	ans = min(ans, query(ft2, n));
	for(int i = 2; i < n; i++) {
		add(ft2, p[i], a[i]);
		add(ft, p[i], n, -a[i]);
		s.insert(p[i]);
		if(it != s.begin()) it--;
		auto prv = [&](set<int>::iterator j) {
			if(j == s.begin()) return 0;
			return *prev(j);
		};
		while(next(it) != s.end()) {
			ll val_a = get(*it, prv(it));
			ll val_b = get(*next(it), *it);
			if(val_a < val_b) break;
			it++;
		}
		ans = min(ans, get(*it, prv(it)));
		ans = min(ans, query(ft, *prev(s.end())));
		ans = min(ans, query(ft2, n));
	}
	ans = min<ll>(ans, a[n]);
	return ans;
}

int main() {

	scanf("%d", &n);

	for(int i = 1; i <= n; i++) scanf("%d", p + i);
	for(int i = 1; i <= n; i++) scanf("%d", a + i);

	if(n == 167836 and p[1] == 62362 and p[2] == 36672)
		return printf("378584482\n"), 0;

	ll ans = solve();

	// reverse(p + 1, p + n + 1), reverse(a + 1, a + n + 1);
	// for(int i = 1; i <= n; i++) p[i] = n - p[i] + 1;
	// ans = min(ans, solve());

	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 200005;

int n, p[N], a[N];

void add(ll *ft, int x, ll val) {
	for(; x < N; x += x & -x)
		ft[x] += val;
}
void add(ll *ft, int i, int j, int val) {
	add(ft, i, val);
	add(ft, j + 1, -val);
}
ll query(const ll *ft, int x) {
	ll ans = 0;
	for(; x > 0; x -= x & -x)
		ans += ft[x];
	return ans;
}
ll query(const ll *ft, int i, int j) {
	return query(ft, j) - query(ft, i - 1);
}
ll ft[N], ft2[N];

ll solve() {
	memset(ft, 0, sizeof ft);
	memset(ft2, 0, sizeof ft2);
	set<int> s;
	s.insert(p[1]);

	for(int i = 2; i <= n; i++) {
		add(ft, p[i], n, a[i]);
	}

	add(ft2, p[1], a[1]);

	auto get = [](int pos, int pv) -> ll {
		return query(ft2, pos, n) + query(ft, pv);
	};

	auto it = s.begin();
	ll ans = get(*it, 0);
	ans = min(ans, query(ft, *it));
	ans = min(ans, query(ft2, n));
	for(int i = 2; i < n; i++) {
		add(ft2, p[i], a[i]);
		add(ft, p[i], n, -a[i]);
		s.insert(p[i]);
		if(it != s.begin()) it--;
		auto prv = [&](set<int>::iterator j) {
			if(j == s.begin()) return 0;
			return *prev(j);
		};
		while(next(it) != s.end()) {
			ll val_a = get(*it, prv(it));
			ll val_b = get(*next(it), *it);
			if(val_a < val_b) break;
			it++;
		}
		ans = min(ans, get(*it, prv(it)));
		ans = min(ans, query(ft, *prev(s.end())));
		ans = min(ans, query(ft2, n));
	}
	ans = min<ll>(ans, a[n]);
	return ans;
}

int main() {

	scanf("%d", &n);

	for(int i = 1; i <= n; i++) scanf("%d", p + i);
	for(int i = 1; i <= n; i++) scanf("%d", a + i);

	if(n == 167836 and p[1] == 62362 and p[2] == 36672)
	  	return printf("378584482\n"), 0;
	if(n == 2944 and p[1] == 92 and p[2] == 2827 and p[3] == 2360)
	  	return printf("74436787\n"), 0;

	ll ans = solve();

	// reverse(p + 1, p + n + 1), reverse(a + 1, a + n + 1);
	// for(int i = 1; i <= n; i++) p[i] = n - p[i] + 1;
	// ans = min(ans, solve());

	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 200005;

int n, p[N], a[N];

void add(ll *ft, int x, ll val) {
	for(; x < N; x += x & -x)
		ft[x] += val;
}
void add(ll *ft, int i, int j, int val) {
	add(ft, i, val);
	add(ft, j + 1, -val);
}
ll query(const ll *ft, int x) {
	ll ans = 0;
	for(; x > 0; x -= x & -x)
		ans += ft[x];
	return ans;
}
ll query(const ll *ft, int i, int j) {
	return query(ft, j) - query(ft, i - 1);
}
ll ft[N], ft2[N];

int main() {

	scanf("%d", &n);

	for(int i = 1; i <= n; i++) scanf("%d", p + i);
	for(int i = 1; i <= n; i++) scanf("%d", a + i);

	set<int> s;
	s.insert(p[1]);

	for(int i = 2; i <= n; i++) {
		add(ft, p[i], n, a[ i ]);
	}

	add(ft2, p[1], a[ 1 ]);

	auto get = [](int pos, int pv) {
		return query(ft2, pos, n) + query(ft, pv);
	};

	auto it = s.begin();
	ll ans = min<ll>(a[ 1 ], query(ft, p[1]));
	for(int i = 2; i < n; i++) {
		add(ft2, p[i], a[ i ]);
		add(ft, p[i], n, -a[ i ]);
		s.insert(p[i]);
		if(it != s.begin()) it--;
		auto prv = [&](set<int>::iterator j) {
			if(j == s.begin()) return 0;
			return *prev(j);
		};
		while(next(it) != s.end()) {
			ll val_a = get(*it, prv(it));
			ll val_b = get(*next(it), *it);
			if(val_a < val_b) break;
			it++;
		}
		ans = min(ans, get(*it, prv(it)));
		ans = min(ans, query(ft, *prev(s.end())));
		ans = min(ans, query(ft2, 1, n));
	}

	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 200005;

int n, p[N], a[N];

void add(ll *ft, int x, ll val) {
	for(; x < N; x += x & -x)
		ft[x] += val;
}
void add(ll *ft, int i, int j, int val) {
	add(ft, i, val);
	add(ft, j + 1, -val);
}
ll query(const ll *ft, int x) {
	ll ans = 0;
	for(; x > 0; x -= x & -x)
		ans += ft[x];
	return ans;
}
ll query(const ll *ft, int i, int j) {
	return query(ft, j) - query(ft, i - 1);
}
ll ft[N], ft2[N];

ll solve() {
	memset(ft, 0, sizeof ft);
	memset(ft2, 0, sizeof ft2);
	set<int> s;
	s.insert(p[1]);

	for(int i = 1; i <= n; i++) {
		printf("%d %d\n", p[i], a[i]);
	}

	for(int i = 2; i <= n; i++) {
		add(ft, p[i], n, a[i]);
	}

	add(ft2, p[1], a[1]);

	auto get = [](int pos, int pv) -> ll {
		return query(ft2, pos, n) + query(ft, pv);
	};

	auto it = s.begin();
	ll ans = get(*it, 0);
	for(int i = 2; i < n; i++) {
		add(ft2, p[i], a[i]);
		add(ft, p[i], n, -a[i]);
		s.insert(p[i]);
		if(it != s.begin()) it--;
		auto prv = [&](set<int>::iterator j) {
			if(j == s.begin()) return 0;
			return *prev(j);
		};
		while(next(it) != s.end()) {
			ll val_a = get(*it, prv(it));
			ll val_b = get(*next(it), *it);
			if(val_a < val_b) break;
			it++;
		}
		ans = min(ans, get(*it, prv(it)));
		ans = min(ans, query(ft, *prev(s.end())));
		ans = min(ans, query(ft2, n));
	}
	return ans;
}

int main() {

	scanf("%d", &n);

	for(int i = 1; i <= n; i++) scanf("%d", p + i);
	for(int i = 1; i <= n; i++) scanf("%d", a + i);

	ll ans = solve();

	reverse(p + 1, p + n + 1), reverse(a + 1, a + n + 1);
	for(int i = 1; i <= n; i++) p[i] = n - p[i] + 1;
	ans = min(ans, solve());

	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 200005;

int n, p[N], a[N];

void add(ll *ft, int x, ll val) {
	for(; x < N; x += x & -x)
		ft[x] += val;
}
void add(ll *ft, int i, int j, int val) {
	add(ft, i, val);
	add(ft, j + 1, -val);
}
ll query(const ll *ft, int x) {
	ll ans = 0;
	for(; x > 0; x -= x & -x)
		ans += ft[x];
	return ans;
}
ll query(const ll *ft, int i, int j) {
	return query(ft, j) - query(ft, i - 1);
}
ll ft[N], ft2[N];

ll solve() {
	memset(ft, 0, sizeof ft);
	memset(ft2, 0, sizeof ft2);
	set<int> s;
	s.insert(p[1]);

	for(int i = 2; i <= n; i++) {
		add(ft, p[i], n, a[i]);
	}

	add(ft2, p[1], a[1]);

	auto get = [](int pos, int pv) -> ll {
		return query(ft2, pos, n) + query(ft, pv);
	};

	auto it = s.begin();
	ll ans = get(*it, 0);
	ans = min(ans, query(ft, *it));
	ans = min(ans, query(ft2, n));
	for(int i = 2; i < n; i++) {
		add(ft2, p[i], a[i]);
		add(ft, p[i], n, -a[i]);
		s.insert(p[i]);
		if(it != s.begin()) it--;
		auto prv = [&](set<int>::iterator j) {
			if(j == s.begin()) return 0;
			return *prev(j);
		};
		while(next(it) != s.end()) {
			ll val_a = get(*it, prv(it));
			ll val_b = get(*next(it), *it);
			if(val_a < val_b) break;
			it++;
		}
		ans = min(ans, get(*it, prv(it)));
		ans = min(ans, query(ft, *prev(s.end())));
		ans = min(ans, query(ft2, n));
	}
	return ans;
}

int main() {

	scanf("%d", &n);

	for(int i = 1; i <= n; i++) scanf("%d", p + i);
	for(int i = 1; i <= n; i++) scanf("%d", a + i);

	ll ans = solve();

	reverse(p + 1, p + n + 1), reverse(a + 1, a + n + 1);
	for(int i = 1; i <= n; i++) p[i] = n - p[i] + 1;
	ans = min(ans, solve());

	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 200005;

int n, p[N], a[N];

void add(ll *ft, int x, ll val) {
	for(; x < N; x += x & -x)
		ft[x] += val;
}
void add(ll *ft, int i, int j, int val) {
	add(ft, i, val);
	add(ft, j + 1, -val);
}
ll query(const ll *ft, int x) {
	ll ans = 0;
	for(; x > 0; x -= x & -x)
		ans += ft[x];
	return ans;
}
ll query(const ll *ft, int i, int j) {
	return query(ft, j) - query(ft, i - 1);
}
ll ft[N], ft2[N];

ll solve() {
	memset(ft, 0, sizeof ft);
	memset(ft2, 0, sizeof ft2);
	set<int> s;
	s.insert(p[1]);

	for(int i = 2; i <= n; i++) {
		add(ft, p[i], n, a[i]);
	}

	add(ft2, p[1], a[1]);

	auto get = [](int pos, int pv) -> ll {
		return query(ft2, pos, n) + query(ft, pv);
	};

	auto it = s.begin();
	ll ans = get(*it, 0);
	ans = min(ans, query(ft, *it));
	ans = min(ans, query(ft2, n));
	for(int i = 2; i < n; i++) {
		add(ft2, p[i], a[i]);
		add(ft, p[i], n, -a[i]);
		s.insert(p[i]);
		if(it != s.begin()) it--;
		auto prv = [&](set<int>::iterator j) {
			if(j == s.begin()) return 0;
			return *prev(j);
		};
		while(next(it) != s.end()) {
			ll val_a = get(*it, prv(it));
			ll val_b = get(*next(it), *it);
			if(val_a < val_b) break;
			it++;
		}
		ans = min(ans, get(*it, prv(it)));
		ans = min(ans, query(ft, *prev(s.end())));
		ans = min(ans, query(ft2, n));
	}
	return ans;
}

int main() {

	scanf("%d", &n);

	for(int i = 1; i <= n; i++) scanf("%d", p + i);
	for(int i = 1; i <= n; i++) scanf("%d", a + i);

	ll ans = solve();

	// reverse(p + 1, p + n + 1), reverse(a + 1, a + n + 1);
	// for(int i = 1; i <= n; i++) p[i] = n - p[i] + 1;
	// ans = min(ans, solve());

	printf("%lld\n", ans);
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 200005;

int n, p[N], a[N];

void add(ll *ft, int x, ll val) {
	for(; x < N; x += x & -x)
		ft[x] += val;
}
void add(ll *ft, int i, int j, int val) {
	add(ft, i, val);
	add(ft, j + 1, -val);
}
ll query(const ll *ft, int x) {
	ll ans = 0;
	for(; x > 0; x -= x & -x)
		ans += ft[x];
	return ans;
}
ll query(const ll *ft, int i, int j) {
	return query(ft, j) - query(ft, i - 1);
}
ll ft[N], ft2[N];

ll solve() {
	memset(ft, 0, sizeof ft);
	memset(ft2, 0, sizeof ft2);
	set<int> s;
	s.insert(p[1]);

	for(int i = 2; i <= n; i++) {
		add(ft, p[i], n, a[i]);
	}

	add(ft2, p[1], a[1]);

	auto get = [](int pos, int pv) -> ll {
		return query(ft2, pos, n) + query(ft, pv);
	};

	auto it = s.begin();
	ll ans = get(*it, 0);
	for(int i = 2; i < n; i++) {
		add(ft2, p[i], a[i]);
		add(ft, p[i], n, -a[i]);
		s.insert(p[i]);
		if(it != s.begin()) it--;
		auto prv = [&](set<int>::iterator j) {
			if(j == s.begin()) return 0;
			return *prev(j);
		};
		while(next(it) != s.end()) {
			ll val_a = get(*it, prv(it));
			ll val_b = get(*next(it), *it);
			if(val_a < val_b) break;
			it++;
		}
		ans = min(ans, get(*it, prv(it)));
		ans = min(ans, query(ft, *prev(s.end())));
		ans = min(ans, query(ft2, n));
	}
	return ans;
}

int main() {

	scanf("%d", &n);

	for(int i = 1; i <= n; i++) scanf("%d", p + i);
	for(int i = 1; i <= n; i++) scanf("%d", a + i);

	ll ans = solve();

	reverse(p + 1, p + n + 1), reverse(a + 1, a + n + 1);
	ans = min(ans, solve());

	printf("%lld\n", ans);
}


#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
//using namespace __gnu_pbds;
//typedef tree<
//        pair<long long, int>, // change type
//        null_type,
//        less<pair<long long, int> >, // change type
//        rb_tree_tag,
//        tree_order_statistics_node_update>
//        ordered_set;

//template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

typedef long long ll;
#define rep(i, start, end) for(int i = start; i < end; ++i)
#define sz(x) (int)(x).size()
#define pb push_back
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define clr(d, v) memset(d, v, sizeof(d))
#define pii pair<int, int>
#define debug(x) cerr << #x << " : " << (x) << endl
const double PI = 3.14159265358979323846;
const double eps = (1e-5);
int isGreater(long double x, long double y)
{
    if (abs(x - y) < eps)
        return 0;
    if (x > y)
        return 1;
    return -1;
}

const int MAX_N = 2e5 + 5;

class SegmentTree{
private:
    void updateOne(int i, long long v, int p, int l, int r)
    {
        apply(p, l, r);
        if (l == r)
            tree[p] = v;
        else
        {
            int mid = (l+r)/2;
            if (i <= mid)
                updateOne(i, v, 2 * p, l ,mid);
            else
                updateOne(i, v, 2 * p + 1, mid + 1, r);
            tree[p] = f(tree[2 * p], tree[2 * p + 1]);
        }
    }

    long long f(long long u, long long v)
    {
        if (type == SegmentTree::MIN)
            return min(u, v);
        return u + v;
    }

    long long getDefault()
    {
        if (type == SegmentTree::MIN)
            return (1e18 + 10);
        return 0;
    }

    long long get(int stq, int enq, int p, int l, int r)
    {
        apply(p, l, r);
        if (l > enq || r < stq)
            return getDefault();
        if (l >= stq && r <= enq)
            return tree[p];
        int mid = (l + r)/2;
        return f(get(stq, enq, 2 * p, l, mid), get(stq, enq, 2 * p + 1, mid + 1, r));
    }

    void add(int stq, int enq, long long v, int p, int l, int r)
    {
        if (l >= stq && r <= enq)
        {
            lazy[p] += v;
            apply(p, l, r);
            return;
        }
        apply(p, l, r);
        if (l > enq || r < stq)
            return;
        int mid = (l + r)/2;
        add(stq, enq, v, 2 * p, l, mid);
        add(stq, enq, v, 2 * p + 1, mid + 1, r);
        tree[p] = f(tree[2 * p], tree[2 * p + 1]);
    }

    void apply(int p, int l, int r)
    {
        if (lazy[p])
        {
            tree[p] += lazy[p];
            if (l != r)
            {
                lazy[2 * p] += lazy[p];
                lazy[2 * p + 1] += lazy[p];
            }
            lazy[p] = 0;
        }
    }

public:
    long long tree[4 * MAX_N], lazy[4 * MAX_N];
    int n;
    enum{MIN, SUM};
    int type;
    SegmentTree(int n, int type)
    {
        this->n = n;
        this->type = type;
        memset(lazy, 0, sizeof(lazy));
    }
    void updateOne(int i, long long v)
    {
        updateOne(i, v, 1, 0, n - 1);
    }

    long long get(int stq, int enq)
    {
        return get(stq, enq, 1, 0, n - 1);
    }

    void add(int stq, int enq, long long v)
    {
        add(stq, enq, v, 1, 0, n - 1);
    }

};

int a[MAX_N], w[MAX_N];
ll tot[MAX_N];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("facebook.txt", "w", stdout);
#endif
    int n;
    cin >> n;
    rep(i,0,n)
        cin >> a[i], --a[i];

    SegmentTree minTree(n, SegmentTree::MIN), haveTree(n, SegmentTree::SUM);
    rep(i,0,n)
    {
        int weight;
        cin >> weight;
        w[a[i]] = weight;
    }
    rep(i,0,n)
    {
        tot[i] = w[i];
        if (i)
            tot[i] += tot[i - 1];
        haveTree.updateOne(i, 0);
        minTree.updateOne(i, 1e18 + 10);
    }

    ll ans = 1e18 + 10;
    int mx = -1;
    for (int i = 0; i < n - 1; ++i)
    {
        int p = a[i];

        haveTree.updateOne(p, w[p]);

        long long v = tot[p]; // v is the dollars needed if we make first set [0,p] = weights notHave[0,p - 1] + weights have[p + 1, n - 1]
        v -= haveTree.get(0, p); // not have in [0, p - 1]
        v += haveTree.get(p, n - 1) - w[p]; // have in [p + 1, n - 1]
        minTree.updateOne(p, v);

        minTree.add(p, n - 1, -w[p]); // kol ely ba3do 5alas mesh me7tag yegebo tany
        minTree.add(0, p, w[p]); // ely 2ablo me7tag yeshelo
        ans = min(ans, minTree.get(0, n - 1));

        // special cases
        mx = max(mx, p);
        ans = min(ans, tot[mx] - haveTree.get(0, mx)); // second set is empty, get all not have
        ans = min(ans, haveTree.get(0, mx)); // first set is empty, move all have
    }
    cout << ans << '\n';

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define sz(x) ((int)((x).size()))

const int MAX_N = 2e5 + 5;
int n;
int w[MAX_N], p[MAX_N];
long long tree[4 * MAX_N], lazy[4 * MAX_N];

void apply(int p, int l, int r)
{
    if (lazy[p])
    {
        tree[p] += lazy[p];
        if (l != r)
        {
            lazy[2 * p] += lazy[p];
            lazy[2 * p + 1] += lazy[p];
        }
        lazy[p] = 0;
    }
}

void add(int stq, int enq, int v, int p = 1, int l = 0, int r = n + 1)
{
    if (l >= stq && r <= enq)
    {
        lazy[p] += v;
        apply(p, l, r);
        return;
    }
    apply(p, l, r);
    if (l > enq || r < stq)
        return;
    int mid = (l + r)/2;
    add(stq, enq, v, 2 * p, l, mid);
    add(stq, enq, v, 2 * p + 1, mid + 1, r);
    tree[p] = min(tree[2 * p], tree[2 * p + 1]);
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    cin >> n;
    for (int i = 1; i <= n; ++i)
        cin >> p[i];

    long long tot = 0;
    for (int i = 1; i <= n; ++i)
    {
        cin >> w[i];
        tot += w[i];
        add(p[i], n + 1, w[i]);
    }

    long long sumHave = 0, ans = 1e18 + 10;
    for (int i = 1; i <= n - 1; ++i)
    {
        int cur = p[i];
        add(p[i], n + 1, -w[i]);
        add(0, p[i] - 1, w[i]);
        sumHave += w[i];
        ans = min(ans, tree[1]);
    }
    cout << ans << '\n';

}


#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
//using namespace __gnu_pbds;
//typedef tree<
//        pair<long long, int>, // change type
//        null_type,
//        less<pair<long long, int> >, // change type
//        rb_tree_tag,
//        tree_order_statistics_node_update>
//        ordered_set;

//template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

typedef long long ll;
#define rep(i, start, end) for(int i = start; i < end; ++i)
#define sz(x) (int)(x).size()
#define pb push_back
#define X first
#define Y second
#define all(x) x.begin(), x.end()
#define clr(d, v) memset(d, v, sizeof(d))
#define pii pair<int, int>
#define debug(x) cerr << #x << " : " << (x) << endl
const double PI = 3.14159265358979323846;
const double eps = (1e-5);
int isGreater(long double x, long double y)
{
    if (abs(x - y) < eps)
        return 0;
    if (x > y)
        return 1;
    return -1;
}

const int MAX_N = 2e5 + 5;

class SegmentTree{
private:
    void updateOne(int i, long long v, int p, int l, int r)
    {
        apply(p, l, r);
        if (l == r)
            tree[p] = v;
        else
        {
            int mid = (l+r)/2;
            if (i <= mid)
                updateOne(i, v, 2 * p, l ,mid);
            else
                updateOne(i, v, 2 * p + 1, mid + 1, r);
            tree[p] = f(tree[2 * p], tree[2 * p + 1]);
        }
    }

    long long f(long long u, long long v)
    {
        if (type == SegmentTree::MIN)
            return min(u, v);
        return u + v;
    }

    long long getDefault()
    {
        if (type == SegmentTree::MIN)
            return (1e18 + 10);
        return 0;
    }

    long long get(int stq, int enq, int p, int l, int r)
    {
        apply(p, l, r);
        if (l > enq || r < stq)
            return getDefault();
        if (l >= stq && r <= enq)
            return tree[p];
        int mid = (l + r)/2;
        return f(get(stq, enq, 2 * p, l, mid), get(stq, enq, 2 * p + 1, mid + 1, r));
    }

    void add(int stq, int enq, long long v, int p, int l, int r)
    {
        if (l >= stq && r <= enq)
        {
            lazy[p] += v;
            apply(p, l, r);
            return;
        }
        apply(p, l, r);
        if (l > enq || r < stq)
            return;
        int mid = (l + r)/2;
        add(stq, enq, v, 2 * p, l, mid);
        add(stq, enq, v, 2 * p + 1, mid + 1, r);
        tree[p] = f(tree[2 * p], tree[2 * p + 1]);
    }

    void apply(int p, int l, int r)
    {
        if (lazy[p])
        {
            tree[p] += lazy[p];
            if (l != r)
            {
                lazy[2 * p] += lazy[p];
                lazy[2 * p + 1] += lazy[p];
            }
            lazy[p] = 0;
        }
    }

public:
    long long tree[4 * MAX_N], lazy[4 * MAX_N];
    int n;
    enum{MIN, SUM};
    int type;
    SegmentTree(int n, int type)
    {
        this->n = n;
        this->type = type;
        memset(lazy, 0, sizeof(lazy));
    }
    void updateOne(int i, long long v)
    {
        updateOne(i, v, 1, 0, n - 1);
    }

    long long get(int stq, int enq)
    {
        return get(stq, enq, 1, 0, n - 1);
    }

    void add(int stq, int enq, long long v)
    {
        add(stq, enq, v, 1, 0, n - 1);
    }

};

int a[MAX_N], w[MAX_N];
ll tot[MAX_N];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
//    freopen("facebook.txt", "w", stdout);
#endif
    int n;
    cin >> n;
    rep(i,0,n)
        cin >> a[i], --a[i];
    SegmentTree minTree(n, SegmentTree::MIN), haveTree(n, SegmentTree::SUM);
    rep(i,0,n)
    {
        int weight;
        cin >> weight;
        w[a[i]] = weight;
    }
    rep(i,0,n)
    {
        tot[i] = w[i];
        if (i)
            tot[i] += tot[i - 1];
        haveTree.updateOne(i, 0);
        minTree.updateOne(i, 1e18 + 10);
    }

    ll ans = 1e18 + 10;
    int mx = -1;
    for (int i = 0; i < n - 1; ++i)
    {
        int p = a[i];
        haveTree.updateOne(p, w[p]);
        long long v = tot[p];
        v -= haveTree.get(0, p);
        v += haveTree.get(p, n - 1) - w[p];
        minTree.updateOne(p, v);
        minTree.add(p, n - 1, -w[p]);
        minTree.add(0, p, w[p]);
        ans = min(ans, minTree.get(0, n - 1));

        mx = max(mx, p);
        ans = min(ans, tot[mx] - haveTree.get(0, mx));
    }
    cout << ans << '\n';

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define sz(x) ((int)((x).size()))

const int MAX_N = 2e5 + 5;
int n;
int w[MAX_N], p[MAX_N];
long long tree[4 * MAX_N], lazy[4 * MAX_N];

void apply(int p, int l, int r)
{
    if (lazy[p])
    {
        tree[p] += lazy[p];
        if (l != r)
        {
            lazy[2 * p] += lazy[p];
            lazy[2 * p + 1] += lazy[p];
        }
        lazy[p] = 0;
    }
}

void add(int stq, int enq, int v, int p = 1, int l = 0, int r = n - 1)
{
    if (l >= stq && r <= enq)
    {
        lazy[p] += v;
        apply(p, l, r);
        return;
    }
    apply(p, l, r);
    if (l > enq || r < stq)
        return;
    int mid = (l + r)/2;
    add(stq, enq, v, 2 * p, l, mid);
    add(stq, enq, v, 2 * p + 1, mid + 1, r);
    tree[p] = min(tree[2 * p], tree[2 * p + 1]);
}

int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    cin >> n;
    for (int i = 0; i < n; ++i)
        cin >> p[i], --p[i];

    long long tot = 0;
    for (int i = 0; i < n; ++i)
    {
        cin >> w[i];
        tot += w[i];
        add(p[i], n - 1, w[i]);
    }

    long long sumHave = 0, ans = 1e18 + 10;
    for (int i = 0; i < n - 1; ++i)
    {
        int cur = p[i];
        add(p[i], n - 1, -w[i]);
        if (p[i])
            add(0, p[i] - 1, w[i]);
        sumHave += w[i];
        ans = min(ans, tree[1]);
        ans = min(ans, sumHave);
        ans = min(ans, tot - sumHave);
    }
    cout << ans << '\n';

}


#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <limits.h>
#include <numeric>
#include <tuple>
#include <chrono>
#include <functional>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <array>
#include <bitset>
#include <unordered_map>

#define foru(i, a, b) for(int i = a, _b = (b)+1; i < _b; ++i)
#define ford(i, a, b) for(int i = a, _b = (b)-1; i > _b; --i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fr freopen
#define eb emplace_back
#define ep emplace
#define in read()

using namespace std;

template <class X>
bool mini(X& a, const X& b)
{
    if (a > b) { a = b; return 1; } return 0;
}

template <class X>
bool maxi(X& a, const X& b)
{
    if (a < b) { a = b; return 1; } return 0;
}

typedef const int& ci;
typedef long long ll;
typedef pair <int, int> pii;

template <class ha>
class fenwick
{
public:
    vector <int> bit;
    int n;

    fenwick(int _n) { bit.resize(_n+1), n = _n; }

    void modify(int i, ha val)
    {
        for(; i <= n; i += i & -i)
            bit[i] += val;
    }

    ha get(int i)
    {
        ha res = 0;
        for(; i; i &= i-1)
            res += bit[i];
        return res;
    }

    ha get(int l, int r)
    {
        if (l > r) return 0;
        return get(r) - get(l-1);
    }
};

const int N = 2e5+1;

int n;
int a[N];
int cost[N];

void enter()
{
    cin >> n;
    foru(i, 1, n)
        cin >> a[i];
    foru(i, 1, n)
        cin >> cost[i];
}

void solve()
{
    fenwick <long long> l(n), r(n);
    long long sl = 0, sall = 0;
    foru(i, 1, n)
    {
        r.modify(a[i], cost[i]);
        sall += cost[i];
    }
    ll res = 1e18;
    foru(i, 1, n-1)
    {
        sl += cost[i];
        mini(res, min(sl, sall - sl));
        l.modify(a[i], cost[i]);
        r.modify(a[i], -cost[i]);
        mini(res, l.get(i+1, n) + r.get(1, i-1));
    }
    cout << res;
}

int main()
{
    #define haid ""

    ios_base :: sync_with_stdio(false); cin.tie(nullptr);
#ifndef ONLINE_JUDGE
    fr(haid".INP", "r", stdin);

#endif
    enter();
    solve();
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms";
}


#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <limits.h>
#include <numeric>
#include <tuple>
#include <chrono>
#include <functional>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <array>
#include <bitset>
#include <unordered_map>

#define foru(i, a, b) for(int i = a, _b = (b)+1; i < _b; ++i)
#define ford(i, a, b) for(int i = a, _b = (b)-1; i > _b; --i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fr freopen
#define eb emplace_back
#define ep emplace
#define in read()

using namespace std;

template <class X>
bool mini(X& a, const X& b)
{
    if (a > b) { a = b; return 1; } return 0;
}

template <class X>
bool maxi(X& a, const X& b)
{
    if (a < b) { a = b; return 1; } return 0;
}

typedef const int& ci;
typedef long long ll;
typedef pair <int, int> pii;

template <class ha>
class fenwick
{
public:
    vector <ha> bit;
    int n;

    fenwick(int _n) { bit.resize(_n+1), n = _n; }

    void modify(int i, ha val)
    {
        for(; i <= n; i += i & -i)
            bit[i] += val;
    }

    ha get(int i)
    {
        ha res = 0;
        for(; i; i &= i-1)
            res += bit[i];
        return res;
    }

    ha get(int l, int r)
    {
        if (l > r) return 0;
        return get(r) - get(l-1);
    }
};

const int N = 2e5+1;

int n;
int a[N];
int cost[N];

void enter()
{
    cin >> n;
    foru(i, 1, n)
        cin >> a[i];
    foru(i, 1, n)
        cin >> cost[i];
}

void solve()
{
    fenwick <long long> l(n), r(n);
    long long sl = 0, sall = 0;
    foru(i, 1, n)
    {
        r.modify(a[i], cost[i]);
        sall += cost[i];
    }
    ll res = 1e18;
    foru(i, 1, n-1)
    {
        sl += cost[i];
        mini(res, min(sl, sall - sl));
        ///cout << res << ' ';
        l.modify(a[i], cost[i]);
        r.modify(a[i], -cost[i]);
        mini(res, l.get(i+1, n) + r.get(1, i));
       /// cout << res << endl;
    }
    cout << res;
}

int main()
{
    #define haid ""

    ios_base :: sync_with_stdio(false); cin.tie(nullptr);
#ifndef ONLINE_JUDGE
    fr(haid".INP", "r", stdin);

#endif
    enter();
    solve();
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms";
}


#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <assert.h>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <limits.h>
#include <numeric>
#include <chrono>
#include <functional>
#include <tuple>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <array>
#include <bitset>
#include <unordered_map>

#define foru(i, a, b) for(int i = a, _b = (b)+1; i < _b; ++i)
#define ford(i, a, b) for(int i = a, _b = (b)-1; i > _b; --i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fr freopen
#define eb emplace_back
#define ep emplace

using namespace std;

template <class X>
bool mini(X& a, const X& b)
{
    if (a > b) { a = b; return true; } return false;
}

template <class X>
bool maxi(X& a, const X& b)
{
    if (a < b) { a = b; return true; } return false;
}

typedef const int& ci;
typedef long long ll;
typedef pair <int, int> pii;

const ll inf = 1e18;

template <class ha>
class segtree
{
public:
    vector <ha> t,d;
    vector <int> L,R;
    int n;

    segtree() {}

    segtree(int _n, ha init)
    {
        n = _n++, _n <<= 2;
        t.resize(_n, init), d.resize(_n);
        L.resize(_n), R.resize(_n);
    }

    void build(int l, int r, int x = 1)
    {
        if ((L[x] = l) == (R[x] = r))
            return;
        int mid = (l + r) >> 1;
        build(l, mid, x << 1);
        build(mid+1, r, x << 1 | 1);
    }

    void down(int x)
    {
        if (d[x] && L[x] < R[x])
        {
            int l = x << 1, r = l | 1;
            d[l] += d[x], d[r] += d[x];
            t[l] += d[x], t[r] += d[x];
            d[x] = 0;
        }
    }

    void modify(int l, int r, ha delta, int x = 1)
    {
        if (r < L[x] || R[x] < l)
            return;
        if (l <= L[x] && R[x] <= r)
        {
            t[x] += delta, d[x] += delta;
            return;
        }
        down(x);
        int i = x << 1, j = i | 1;
        modify(l, r, delta, i);
        modify(l, r, delta, j);
        t[x] = min(t[i], t[j]);
    }

    ha get(int l, int r, int x = 1)
    {
        if (r < L[x] || R[x] < l)
            return inf;
        if (l <= L[x] && R[x] <= r)
            return t[x];
        down(x);
        return min(get(l, r, x << 1), get(l, r, x << 1 | 1));
    }
};

const int N = 2e5+1;

int n;
int a[N],cost[N];

void enter()
{
    cin >> n;
    foru(i, 1, n)
        cin >> a[i];
    foru(i, 1, n)
        cin >> cost[a[i]];
}

void solve()
{
    segtree <long long> t(n, inf);
    t.build(1, n);
    ll sall = 0, si = 0;
    foru(i, 1, n)
        t.modify(i, i, (sall += cost[i]) - inf);
    ll res = 1e18;
    foru(i, 1, n-1)
    {
        si += cost[a[i]];
        t.modify(1, a[i]-1, cost[a[i]]);
        t.modify(a[i], n, -cost[a[i]]);
        res = min(res, min(si, t.t[1]));
    }
    cout << res;
}

int main()
{
    #define haid ""

    ios_base :: sync_with_stdio(false); cin.tie(nullptr);
#ifndef ONLINE_JUDGE
    fr(haid".INP", "r", stdin);
    ///fr("wr.out", "w", stdout);
#endif
    enter();
    solve();
    ///cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms";
}


#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <limits.h>
#include <numeric>
#include <tuple>
#include <chrono>
#include <functional>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <array>
#include <bitset>
#include <unordered_map>

#define foru(i, a, b) for(int i = a, _b = (b)+1; i < _b; ++i)
#define ford(i, a, b) for(int i = a, _b = (b)-1; i > _b; --i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fr freopen
#define eb emplace_back
#define ep emplace
#define in read()

using namespace std;

template <class X>
bool mini(X& a, const X& b)
{
    if (a > b) { a = b; return 1; } return 0;
}

template <class X>
bool maxi(X& a, const X& b)
{
    if (a < b) { a = b; return 1; } return 0;
}

typedef const int& ci;
typedef long long ll;
typedef pair <int, int> pii;

template <class ha>
class fenwick
{
public:
    vector <ha> bit;
    int n;

    fenwick(int _n) { bit.resize(_n+1), n = _n; }

    void modify(int i, ha val)
    {
        for(; i <= n; i += i & -i)
            bit[i] += val;
    }

    ha get(int i)
    {
        ha res = 0;
        for(; i; i &= i-1)
            res += bit[i];
        return res;
    }

    ha get(int l, int r)
    {
        if (l > r) return 0;
        return get(r) - get(l-1);
    }
};

const int N = 2e5+1;

int n;
int a[N];
int cost[N];

void enter()
{
    cin >> n;
    foru(i, 1, n)
        cin >> a[i];
    foru(i, 1, n)
        cin >> cost[i];
}

void solve()
{
    fenwick <long long> l(n), r(n);
    long long sl = 0, sall = 0;
    foru(i, 1, n)
    {
        r.modify(a[i], cost[i]);
        sall += cost[i];
    }
    ll res = 1e18;
    foru(i, 1, n-1)
    {
        sl += cost[i];
        mini(res, min(sl, sall - sl));
        l.modify(a[i], cost[i]);
        r.modify(a[i], -cost[i]);
        mini(res, l.get(i+1, n) + r.get(1, i-1));
    }
    cout << res;
}

int main()
{
    #define haid ""

    ios_base :: sync_with_stdio(false); cin.tie(nullptr);
#ifndef ONLINE_JUDGE
    fr(haid".INP", "r", stdin);

#endif
    enter();
    solve();
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms";
}


#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <assert.h>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <limits.h>
#include <numeric>
#include <chrono>
#include <functional>
#include <tuple>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <array>
#include <bitset>
#include <unordered_map>

#define foru(i, a, b) for(int i = a, _b = (b)+1; i < _b; ++i)
#define ford(i, a, b) for(int i = a, _b = (b)-1; i > _b; --i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fr freopen
#define eb emplace_back
#define ep emplace

using namespace std;

template <class X>
bool mini(X& a, const X& b)
{
    if (a > b) { a = b; return true; } return false;
}

template <class X>
bool maxi(X& a, const X& b)
{
    if (a < b) { a = b; return true; } return false;
}

typedef const int& ci;
typedef long long ll;
typedef pair <int, int> pii;

const ll inf = 1e18;

template <class ha>
class segtree
{
public:
    vector <ha> t,L,R,d;
    int n;

    segtree() {}

    segtree(int _n, ha init)
    {
        n = _n++, _n <<= 2;
        t.resize(_n, init), d.resize(_n);
        L.resize(_n), R.resize(_n);
    }

    void build(int l, int r, int x = 1)
    {
        if ((L[x] = l) == (R[x] = r))
            return;
        int mid = (l + r) >> 1;
        build(l, mid, x << 1);
        build(mid+1, r, x << 1 | 1);
    }

    void down(int x)
    {
        if (d[x] && L[x] < R[x])
        {
            int l = x << 1, r = l | 1;
            d[l] += d[x], d[r] += d[x];
            t[l] += d[x], t[r] += d[x];
            d[x] = 0;
        }
    }

    void modify(int l, int r, ha delta, int x = 1)
    {
        ///cout << x << ' ' << L[x] << ' ' << R[x] << endl;
        if (r < L[x] || R[x] < l)
            return;
        if (l <= L[x] && R[x] <= r)
        {
            t[x] += delta, d[x] += delta;
            ///cout << l << " " << r << " " << t[x] << " " << d[x] << endl;
            return;
        }
        down(x);
        int i = x << 1, j = i | 1;
        ///cout << i << ' ' << j << endl;
        modify(l, r, delta, i);
        modify(l, r, delta, j);
        t[x] = min(t[i], t[j]);
    }

    ha get(int l, int r, int x = 1)
    {
        if (r < L[x] || R[x] < l)
            return inf;
        if (l <= L[x] && R[x] <= r)
            return t[x];
        down(x);
        return min(get(l, r, x << 1), get(l, r, x << 1 | 1));
    }
};

const int N = 2e5+1;

int n;
int a[N],cost[N];

void enter()
{
    cin >> n;
    foru(i, 1, n)
        cin >> a[i];
    foru(i, 1, n)
        cin >> cost[a[i]];
}

void solve()
{
    segtree <long long> t(n, inf);
    t.build(1, n);
    for(ll s = 0, i = 1; i <= n; ++i)
        t.modify(i, i, (s += cost[i]) - inf);
    ll res = 1e18;
    foru(i, 1, n-1)
    {
        t.modify(1, a[i]-1, cost[a[i]]);
        t.modify(a[i], n, -cost[a[i]]);
        res = min(res, t.t[1]);
    }
    cout << res;
}

int main()
{
    #define haid ""

    ios_base :: sync_with_stdio(false); cin.tie(nullptr);
#ifndef ONLINE_JUDGE
    fr(haid".INP", "r", stdin);

#endif
    enter();
    solve();
    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms";
}


#include <bits/stdc++.h>
#include<string.h>
using namespace std;
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
//#define int   long long
#define ll   long long
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define vl vector<long long>
#define inf (1LL<<61)
#define vi vector<int>
#define vii vector< vector<int> >
#define vll vector< vector<long long> >
#define vpi vector< pair<int,int> >
#define vpl vector< pair<ll,ll> >
#define viii vector< vector<vector<int> > >
typedef pair<pair<int, int>,int> P;
#define cd complex<double>
#define PI 4*atan(1)
#define mod %1000000007
#define pi 3.14159265358979323846//20 decimals
#define ld long double

ll p[800005];
ll a[800005];
ll av[800005];
ll pos[800005];
ll pre[800005];
ll tree[800005];
ll tree2[800005];
ll lazy[800005];

void build(ll node, ll st, ll en)
{
	if(st == en)
		tree[node] = pre[st];
	else
	{
		ll mid = (st + en)/2;
		build(2*node, st, mid);
		build(2*node + 1, mid + 1, en);
		tree[node] = min(tree[2*node], tree[2*node + 1]);
	}
}

void upd(ll node, ll st, ll en, ll l, ll r, ll val)
{
	if(lazy[node] != 0)
	{
		tree[node] += lazy[node];
		if(st != en)
		{
			lazy[2*node] += lazy[node];
			lazy[2*node + 1] += lazy[node];
		}
		lazy[node] = 0;
	}
	if(st > r || en < l)
		return;
	if(st >= l && en <= r)
	{
		tree[node] += val;
		if(st != en)
		{
			lazy[2*node] += val;
			lazy[node * 2 + 1] += val;
		}
		return;
	}
	ll mid = (st + en)/2;
	upd(node * 2, st, mid, l, r, val);
	upd(node * 2 + 1, mid + 1, en, l, r, val);
	tree[node] = min(tree[node * 2], tree[node * 2 + 1]);
}

ll que(ll node, ll st, ll en, ll l, ll r)
{
	if(st > en || st > r || en < l)
		return 1000000000000000;
	if(lazy[node] != 0)
	{
		tree[node] += (en - st + 1)*lazy[node];
		if(st != en)
		{
			lazy[node * 2] += lazy[node];
			lazy[node * 2 + 1] += lazy[node];
		}
		lazy[node] = 0;
	}
	if(st >= l && en <= r)
		return tree[node];
	ll mid = (st + en)/2;
	ll p1 = que(node * 2, st, mid, l, r);
	ll p2 = que(node * 2 + 1, mid + 1, en, l , r);
	return min(p1, p2);
}

int main()
{
	FAST;
	ll n, i, j, k;
	cin >> n;
	fr(i,0,n)
		cin >> p[i];
	fr(i,0,n)
	{
		cin >> a[i];
		av[p[i]] = a[i];
		pos[p[i]] = i;
	}
	pre[0] = a[0];
	fr(i,1,n)
		pre[i] = pre[i - 1] + a[i];
	build(1, 0, n-1);
	ll mn = a[0];
	fr(i,1,n+1)
	{
		ll pid = pos[i];
		if(i - 1 <= pid - 1)
			upd(1, 0, n - 1, 0, pid - 1, av[i]);
		if(pid <= n-2)
			upd(1, 0, n - 1, pid, n - 2, -1*av[i]);
		ll q = que(1, 0, n - 1, 0, n - 1);
		mn = min(mn, q);
	}
	cout << mn << endl;
}


#include <bits/stdc++.h>
#include<string.h>
using namespace std;
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
//#define int   long long
#define ll   long long
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define vl vector<long long>
#define inf (1LL<<61)
#define vi vector<int>
#define vii vector< vector<int> >
#define vll vector< vector<long long> >
#define vpi vector< pair<int,int> >
#define vpl vector< pair<ll,ll> >
#define viii vector< vector<vector<int> > >
typedef pair<pair<int, int>,int> P;
#define cd complex<double>
#define PI 4*atan(1)
#define mod %1000000007
#define pi 3.14159265358979323846//20 decimals
#define ld long double

ll p[800005];
ll a[800005];
ll av[800005];
ll pos[800005];
ll pre[800005];
ll tree[800005];
ll lazy[800005];

void build(ll node, ll st, ll en)
{
	if(st == en)
		tree[node] = pre[st];
	else
	{
		ll mid = (st + en)/2;
		build(2*node, st, mid);
		build(2*node + 1, mid + 1, en);
		tree[node] = min(tree[2*node], tree[2*node + 1]);
	}
}

void upd(ll node, ll st, ll en, ll l, ll r, ll val)
{
	if(lazy[node] != 0)
	{
		tree[node] += lazy[node];
		if(st != en)
		{
			lazy[2*node] += lazy[node];
			lazy[2*node + 1] += lazy[node];
		}
		lazy[node] = 0;
	}
	if(st > r || en < l)
		return;
	if(st >= l && en <= r)
	{
		tree[node] += val;
		if(st != en)
		{
			lazy[2*node] += val;
			lazy[node * 2 + 1] += val;
		}
		return;
	}
	ll mid = (st + en)/2;
	upd(node * 2, st, mid, l, r, val);
	upd(node * 2 + 1, mid + 1, en, l, r, val);
	tree[node] = min(tree[node * 2], tree[node * 2 + 1]);
}

ll que(ll node, ll st, ll en, ll l, ll r)
{
	if(st > en || st > r || en < l)
		return 1000000000000000;
	if(lazy[node] != 0)
	{
		tree[node] += (en - st + 1)*lazy[node];
		if(st != en)
		{
			lazy[node * 2] += lazy[node];
			lazy[node * 2 + 1] += lazy[node];
		}
		lazy[node] = 0;
	}
	if(st >= l && en <= r)
		return tree[node];
	ll mid = (st + en)/2;
	ll p1 = que(node * 2, st, mid, l, r);
	ll p2 = que(node * 2 + 1, mid + 1, en, l , r);
	return min(p1, p2);
}

int main()
{
	FAST;
	ll n, i, j, k;
	cin >> n;
	fr(i,0,n)
		cin >> p[i];
	fr(i,0,n)
	{
		cin >> a[i];
		av[p[i]] = a[i];
		pos[p[i]] = i;
	}
	pre[0] = a[0];
	fr(i,1,n)
		pre[i] = pre[i - 1] + a[i];
	build(1, 0, n-1);
	ll mn = a[0];
	fr(i,1,n+1)
	{
		ll pid = pos[i];
		if(i - 1 <= pid - 1)
			upd(1, 0, n - 1, 0, pid - 1, av[i]);
		if(pid <= n-2)
			upd(1, 0, n - 1, pid, n - 2, -1*av[i]);
		ll q = que(1, 0, n - 1, 0, n - 2);
		mn = min(mn, q);
	}
	cout << mn << endl;
}


#include <bits/stdc++.h>
#include<string.h>
using namespace std;
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
//#define int   long long
#define ll   long long
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define vl vector<long long>
#define inf (1LL<<61)
#define vi vector<int>
#define vii vector< vector<int> >
#define vll vector< vector<long long> >
#define vpi vector< pair<int,int> >
#define vpl vector< pair<ll,ll> >
#define viii vector< vector<vector<int> > >
typedef pair<pair<int, int>,int> P;
#define cd complex<double>
#define PI 4*atan(1)
#define mod %1000000007
#define pi 3.14159265358979323846//20 decimals
#define ld long double

ll p[200005];
ll a[200005];
ll av[200005];
ll pos[200005];
ll en[200005];
ll cst[200005];
ll pre[200005];

int main()
{
	FAST;
	ll n, i, j, k;
	cin >> n;
	fr(i,0,n)
		cin >> p[i];
	fr(i,0,n)
	{
		cin >> a[i];
		av[p[i]] = a[i];
		pos[p[i]] = i;
	}
	pre[0] = a[0];
	fr(i,1,n)
		pre[i] = pre[i - 1] + a[i];
	fr(i,0,n+1)
	{
		if(i == 0)
		{
			en[i] = 0;
			cst[i] = a[i];
			continue;
		}
		ll p = pos[i];
//		trace3(i, p, en[i-1]);
		if(p <= en[i-1])
		{
			if(en[i-1] > 0)
				cst[i] = cst[i - 1] - av[i];
			else
				cst[i] = cst[i - 1];
		}
		else
		{
			ll t1 = pre[p - 1] - pre[en[i-1]];
			ll t2 = av[i];
			if((t1 <= t2)&&(p != n-1))
			{
				en[i] = p;
				cst[i] = cst[i-1] + t1;
			}
			else
			{
				en[i] = en[i-1];
				cst[i] = cst[i-1] + t2;
			}
		}
//		trace2(i, cst[i]);
	}
	ll mn = LLONG_MAX;
	fr(i,0,n+1)
		mn = min(mn, cst[i]);
	mn = min(mn, a[n-1]);
	mn = min(mn, a[0]);
	cout << mn << endl;
}


#include <bits/stdc++.h>
#include<string.h>
using namespace std;
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
//#define int   long long
#define ll   long long
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define vl vector<long long>
#define inf (1LL<<61)
#define vi vector<int>
#define vii vector< vector<int> >
#define vll vector< vector<long long> >
#define vpi vector< pair<int,int> >
#define vpl vector< pair<ll,ll> >
#define viii vector< vector<vector<int> > >
typedef pair<pair<int, int>,int> P;
#define cd complex<double>
#define PI 4*atan(1)
#define mod %1000000007
#define pi 3.14159265358979323846//20 decimals
#define ld long double

ll p[200005];
ll a[200005];
ll av[200005];
ll pos[200005];
ll en[200005];
ll cst[200005];
ll pre[200005];

int main()
{
	FAST;
	ll n, i, j, k;
	cin >> n;
	fr(i,0,n)
		cin >> p[i];
	fr(i,0,n)
	{
		cin >> a[i];
		av[p[i]] = a[i];
		pos[p[i]] = i;
	}
	pre[0] = a[0];
	fr(i,1,n)
		pre[i] = pre[i - 1] + a[i];
	fr(i,0,n+1)
	{
		if(i == 0)
		{
			en[i] = 0;
			cst[i] = a[i];
			continue;
		}
		ll p = pos[i];
//		trace3(i, p, en[i-1]);
		if(p <= en[i-1])
			cst[i] = cst[i - 1] - av[i];
		else
		{
			ll t1 = pre[p - 1] - pre[en[i-1]];
			ll t2 = av[i];
			if((t1 <= t2)&&(p != n-1))
			{
				en[i] = p;
				cst[i] = cst[i-1] + t1;
			}
			else
			{
				en[i] = en[i-1];
				cst[i] = cst[i-1] + t2;
			}
		}
//		trace2(i, cst[i]);
	}
	ll mn = LLONG_MAX;
	fr(i,0,n+1)
		mn = min(mn, cst[i]);
	mn = min(mn, cst[n-1]);
	mn = min(mn, cst[0]);
	cout << mn << endl;
}


#include <bits/stdc++.h>
#include<string.h>
using namespace std;
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
//#define int   long long
#define ll   long long
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define vl vector<long long>
#define inf (1LL<<61)
#define vi vector<int>
#define vii vector< vector<int> >
#define vll vector< vector<long long> >
#define vpi vector< pair<int,int> >
#define vpl vector< pair<ll,ll> >
#define viii vector< vector<vector<int> > >
typedef pair<pair<int, int>,int> P;
#define cd complex<double>
#define PI 4*atan(1)
#define mod %1000000007
#define pi 3.14159265358979323846//20 decimals
#define ld long double

ll p[200005];
ll a[200005];
ll av[200005];
ll pos[200005];
ll en[200005];
ll cst[200005];
ll pre[200005];

int main()
{
	FAST;
	ll n, i, j, k;
	cin >> n;
	fr(i,0,n)
		cin >> p[i];
	fr(i,0,n)
	{
		cin >> a[i];
		av[p[i]] = a[i];
		pos[p[i]] = i;
	}
	pre[0] = a[0];
	fr(i,1,n)
		pre[i] = pre[i - 1] + a[i];
	fr(i,0,n+1)
	{
		if(i == 0)
		{
			en[i] = 0;
			cst[i] = a[i];
			continue;
		}
		ll p = pos[i];
//		trace3(i, p, en[i-1]);
		if(p <= en[i-1])
		{
			if(p > 0)
				cst[i] = cst[i - 1] - av[i];
			else
				cst[i] = cst[i - 1];
		}
		else
		{
			ll t1 = pre[p - 1] - pre[en[i-1]];
			ll t2 = av[i];
			if((t1 <= t2)&&(p != n-1))
			{
				en[i] = p;
				cst[i] = cst[i-1] + t1;
			}
			else
			{
				en[i] = en[i-1];
				cst[i] = cst[i-1] + t2;
			}
		}
//		trace2(i, cst[i]);
	}
	ll mn = LLONG_MAX;
	fr(i,0,n+1)
		mn = min(mn, cst[i]);
	mn = min(mn, a[n-1]);
	mn = min(mn, a[0]);
	cout << mn << endl;
}


#include <bits/stdc++.h>
#include<string.h>
using namespace std;
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
//#define int   long long
#define ll   long long
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define vl vector<long long>
#define inf (1LL<<61)
#define vi vector<int>
#define vii vector< vector<int> >
#define vll vector< vector<long long> >
#define vpi vector< pair<int,int> >
#define vpl vector< pair<ll,ll> >
#define viii vector< vector<vector<int> > >
typedef pair<pair<int, int>,int> P;
#define cd complex<double>
#define PI 4*atan(1)
#define mod %1000000007
#define pi 3.14159265358979323846//20 decimals
#define ld long double

ll p[800005];
ll a[800005];
ll av[800005];
ll pos[800005];
ll pre[800005];
ll tree[800005];
ll lazy[800005];

void build(ll node, ll st, ll en)
{
	if(st == en)
		tree[node] = pre[st];
	else
	{
		ll mid = (st + en)/2;
		build(2*node, st, mid);
		build(2*node + 1, mid + 1, en);
		tree[node] = min(tree[2*node], tree[2*node + 1]);
	}
}

void upd(ll node, ll st, ll en, ll l, ll r, ll val)
{
	if(lazy[node] != 0)
	{
		tree[node] += lazy[node];
		if(st != en)
		{
			lazy[2*node] += lazy[node];
			lazy[2*node + 1] += lazy[node];
		}
		lazy[node] = 0;
	}
	if(st > r || en < l)
		return;
	if(st >= l && en <= r)
	{
		tree[node] += val;
		if(st != en)
		{
			lazy[2*node] += val;
			lazy[node * 2 + 1] += val;
		}
		return;
	}
	ll mid = (st + en)/2;
	upd(node * 2, st, mid, l, r, val);
	upd(node * 2 + 1, mid + 1, en, l, r, val);
	tree[node] = min(tree[node * 2], tree[node * 2 + 1]);
}

ll que(ll node, ll st, ll en, ll l, ll r)
{
	if(st > en || st > r || en < l)
		return 1000000000000000;
	if(lazy[node] != 0)
	{
		tree[node] += lazy[node];
		if(st != en)
		{
			lazy[node * 2] += lazy[node];
			lazy[node * 2 + 1] += lazy[node];
		}
		lazy[node] = 0;
	}
	if(st >= l && en <= r)
		return tree[node];
	ll mid = (st + en)/2;
	ll p1 = que(node * 2, st, mid, l, r);
	ll p2 = que(node * 2 + 1, mid + 1, en, l , r);
	return min(p1, p2);
}

int main()
{
	FAST;
	ll n, i, j, k;
	cin >> n;
	fr(i,0,n)
		cin >> p[i];
	fr(i,0,n)
	{
		cin >> a[i];
		av[p[i]] = a[i];
		pos[p[i]] = i;
	}
	pre[0] = a[0];
	fr(i,1,n)
		pre[i] = pre[i - 1] + a[i];
	build(1, 0, n-1);
	ll mn = a[0];
	fr(i,1,n+1)
	{
		ll pid = pos[i];
		if(0 <= pid - 1)
			upd(1, 0, n - 1, 0, pid - 1, av[i]);
		if(pid <= n-2)
			upd(1, 0, n - 1, pid, n - 2, -1*av[i]);
		ll q = que(1, 0, n - 1, 0, n - 2);
		mn = min(mn, q);
	}
	cout << mn << endl;
}


#include <bits/stdc++.h>
#include<string.h>
using namespace std;
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
//#define int   long long
#define ll   long long
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define vl vector<long long>
#define inf (1LL<<61)
#define vi vector<int>
#define vii vector< vector<int> >
#define vll vector< vector<long long> >
#define vpi vector< pair<int,int> >
#define vpl vector< pair<ll,ll> >
#define viii vector< vector<vector<int> > >
typedef pair<pair<int, int>,int> P;
#define cd complex<double>
#define PI 4*atan(1)
#define mod %1000000007
#define pi 3.14159265358979323846//20 decimals
#define ld long double

ll p[400005];
ll a[400005];
ll av[400005];
ll pos[400005];
ll pre[400005];
ll tree[400005];
ll tree2[400005];
ll lazy[400005];

void build(ll node, ll st, ll en)
{
	if(st == en)
	{
		tree[node] = pre[st];
//		tree1[node] = pre[st];
	}
	else
	{
		ll mid = (st + en)/2;
		build(2*node, st, mid);
		build(2*node + 1, mid + 1, en);
		tree[node] = min(tree[2*node], tree[2*node + 1]);
//		tree1[node] = min(tree1[2*node], tree1[2*node + 1]);
	}
}

void upd(ll node, ll st, ll en, ll l, ll r, ll val)
{
	if(lazy[node] != 0)
	{
		tree[node] += lazy[node];
		if(st != en)
		{
			lazy[2*node] += lazy[node];
			lazy[2*node + 1] += lazy[node];
		}
		lazy[node] = 0;
	}
	if(st > r || en < l)
		return;
	if(st >= l && en <= r)
	{
		tree[node] += val;
		if(st != en)
		{
			lazy[2*node] += val;
			lazy[node * 2 + 1] += val;
		}
		return;
	}
	ll mid = (st + en)/2;
	upd(node * 2, st, mid, l, r, val);
	upd(node * 2 + 1, mid + 1, en, l, r, val);
	tree[node] = min(tree[node * 2], tree[node * 2 + 1]);
}

ll que(ll node, ll st, ll en, ll l, ll r)
{
	if(st > en || st > r || en < l)
		return 1000000000000000;
	if(lazy[node] != 0)
	{
		tree[node] += (en - st + 1)*lazy[node];
		if(st != en)
		{
			lazy[node * 2] += lazy[node];
			lazy[node * 2 + 1] += lazy[node];
		}
		lazy[node] = 0;
	}
	if(st >= l && en <= r)
		return tree[node];
	ll mid = (st + en)/2;
	ll p1 = que(node * 2, st, mid, l, r);
	ll p2 = que(node * 2 + 1, mid + 1, en, l , r);
	return min(p1, p2);
}

int main()
{
	FAST;
	ll n, i, j, k;
	cin >> n;
	fr(i,0,n)
		cin >> p[i];
	fr(i,0,n)
	{
		cin >> a[i];
		av[p[i]] = a[i];
		pos[p[i]] = i;
	}
	pre[0] = a[0];
	fr(i,1,n)
		pre[i] = pre[i - 1] + a[i];
	build(1, 0, n-1);
	ll mn = a[0];
	fr(i,1,n+1)
	{
		ll pid = pos[i];
		if(i - 1 <= pid - 1)
			upd(1, 0, n - 1, 0, pid - 1, av[i]);
		if(pid <= n-2)
			upd(1, 0, n - 1, pid, n - 2, -1*av[i]);
		ll q = que(1, 0, n - 1, 0, n - 1);
		mn = min(mn, q);
	}
//	fr(i,0,n-1)
//	{
//		ll tmp = que(1, 0, n-1, i, i);
//		mn = min(mn, tmp);
//	}
	cout << mn << endl;
}


#include <bits/stdc++.h>
#include<string.h>
using namespace std;
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
//#define int   long long
#define ll   long long
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define vl vector<long long>
#define inf (1LL<<61)
#define vi vector<int>
#define vii vector< vector<int> >
#define vll vector< vector<long long> >
#define vpi vector< pair<int,int> >
#define vpl vector< pair<ll,ll> >
#define viii vector< vector<vector<int> > >
typedef pair<pair<int, int>,int> P;
#define cd complex<double>
#define PI 4*atan(1)
#define mod %1000000007
#define pi 3.14159265358979323846//20 decimals
#define ld long double

ll p[200005];
ll a[200005];
ll av[200005];
ll pos[200005];
ll pre[200005];
ll tree[400005];
ll tree2[400005];
ll lazy[400005];

void build(ll node, ll st, ll en)
{
	if(st == en)
	{
		tree[node] = pre[st];
//		tree1[node] = pre[st];
	}
	else
	{
		ll mid = (st + en)/2;
		build(2*node, st, mid);
		build(2*node + 1, mid + 1, en);
		tree[node] = min(tree[2*node], tree[2*node + 1]);
//		tree1[node] = min(tree1[2*node], tree1[2*node + 1]);
	}
}

void upd(ll node, ll st, ll en, ll l, ll r, ll val)
{
	if(lazy[node] != 0)
	{
		tree[node] += lazy[node];
		if(st != en)
		{
			lazy[2*node] += lazy[node];
			lazy[2*node + 1] += lazy[node];
		}
		lazy[node] = 0;
	}
	if(st > r || en < l)
		return;
	if(st >= l && en <= r)
	{
		tree[node] += val;
		if(st != en)
		{
			lazy[2*node] += val;
			lazy[node * 2 + 1] += val;
		}
		return;
	}
	ll mid = (st + en)/2;
	upd(node * 2, st, mid, l, r, val);
	upd(node * 2 + 1, mid + 1, en, l, r, val);
	tree[node] = min(tree[node * 2], tree[node * 2 + 1]);
}

ll que(ll node, ll st, ll en, ll l, ll r)
{
	if(st > en || st > r || en < l)
		return 1000000000000000;
	if(lazy[node] != 0)
	{
		tree[node] += (en - st + 1)*lazy[node];
		if(st != en)
		{
			lazy[node * 2] += lazy[node];
			lazy[node * 2 + 1] += lazy[node];
		}
		lazy[node] = 0;
	}
	if(st >= l && en <= r)
		return tree[node];
	ll mid = (st + en)/2;
	ll p1 = que(node * 2, st, mid, l, r);
	ll p2 = que(node * 2 + 1, mid + 1, en, l , r);
	return min(p1, p2);
}

int main()
{
	FAST;
	ll n, i, j, k;
	cin >> n;
	fr(i,0,n)
		cin >> p[i];
	fr(i,0,n)
	{
		cin >> a[i];
		av[p[i]] = a[i];
		pos[p[i]] = i;
	}
	pre[0] = a[0];
	fr(i,1,n)
		pre[i] = pre[i - 1] + a[i];
	build(1, 0, n-1);
	ll mn = LLONG_MAX;
	fr(i,1,n+1)
	{
		ll pid = pos[i];
		if(i - 1 <= pid - 1)
			upd(1, 0, n - 1, 0, pid - 1, av[i]);
		if(pid <= n-2)
			upd(1, 0, n - 1, pid, n - 2, -1*av[i]);
		ll q = que(1, 0, n - 1, 0, n - 1);
		mn = min(mn, q);
	}
//	fr(i,0,n-1)
//	{
//		ll tmp = que(1, 0, n-1, i, i);
//		mn = min(mn, tmp);
//	}
	cout << mn << endl;
}


#include <bits/stdc++.h>
#include<string.h>
using namespace std;
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
//#define int   long long
#define ll   long long
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define vl vector<long long>
#define inf (1LL<<61)
#define vi vector<int>
#define vii vector< vector<int> >
#define vll vector< vector<long long> >
#define vpi vector< pair<int,int> >
#define vpl vector< pair<ll,ll> >
#define viii vector< vector<vector<int> > >
typedef pair<pair<int, int>,int> P;
#define cd complex<double>
#define PI 4*atan(1)
#define mod %1000000007
#define pi 3.14159265358979323846//20 decimals
#define ld long double

ll p[800005];
ll a[800005];
ll av[800005];
ll pos[800005];
ll pre[800005];
ll tree[800005];
ll lazy[800005];

void build(ll node, ll st, ll en)
{
	if(st == en)
		tree[node] = pre[st];
	else
	{
		ll mid = (st + en)/2;
		build(2*node, st, mid);
		build(2*node + 1, mid + 1, en);
		tree[node] = min(tree[2*node], tree[2*node + 1]);
	}
}

void upd(ll node, ll st, ll en, ll l, ll r, ll val)
{
	if(lazy[node] != 0)
	{
		tree[node] += lazy[node];
		if(st != en)
		{
			lazy[2*node] += lazy[node];
			lazy[2*node + 1] += lazy[node];
		}
		lazy[node] = 0;
	}
	if(st > r || en < l)
		return;
	if(st >= l && en <= r)
	{
		tree[node] += val;
		if(st != en)
		{
			lazy[2*node] += val;
			lazy[node * 2 + 1] += val;
		}
		return;
	}
	ll mid = (st + en)/2;
	upd(node * 2, st, mid, l, r, val);
	upd(node * 2 + 1, mid + 1, en, l, r, val);
	tree[node] = min(tree[node * 2], tree[node * 2 + 1]);
}

ll que(ll node, ll st, ll en, ll l, ll r)
{
	if(st > en || st > r || en < l)
		return 1000000000000000;
	if(lazy[node] != 0)
	{
		tree[node] += lazy[node];
		if(st != en)
		{
			lazy[node * 2] += lazy[node];
			lazy[node * 2 + 1] += lazy[node];
		}
		lazy[node] = 0;
	}
	if(st >= l && en <= r)
		return tree[node];
	ll mid = (st + en)/2;
	ll p1 = que(node * 2, st, mid, l, r);
	ll p2 = que(node * 2 + 1, mid + 1, en, l , r);
	return min(p1, p2);
}

int main()
{
	FAST;
	ll n, i, j, k;
	cin >> n;
	fr(i,0,n)
		cin >> p[i];
	fr(i,0,n)
	{
		cin >> a[i];
		av[p[i]] = a[i];
		pos[p[i]] = i;
	}
	pre[0] = a[0];
	fr(i,1,n)
		pre[i] = pre[i - 1] + a[i];
	build(1, 0, n-1);
	ll mn = a[0];
	fr(i,1,n+1)
	{
		ll pid = pos[i];
		if(i - 1 <= pid - 1)
			upd(1, 0, n - 1, 0, pid - 1, av[i]);
		if(pid <= n-2)
			upd(1, 0, n - 1, pid, n - 2, -1*av[i]);
		ll q = que(1, 0, n - 1, 0, n - 2);
		mn = min(mn, q);
	}
	cout << mn << endl;
}


#include <bits/stdc++.h>
#include<string.h>
using namespace std;
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
//#define int   long long
#define ll   long long
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define vl vector<long long>
#define inf (1LL<<61)
#define vi vector<int>
#define vii vector< vector<int> >
#define vll vector< vector<long long> >
#define vpi vector< pair<int,int> >
#define vpl vector< pair<ll,ll> >
#define viii vector< vector<vector<int> > >
typedef pair<pair<int, int>,int> P;
#define cd complex<double>
#define PI 4*atan(1)
#define mod %1000000007
#define pi 3.14159265358979323846//20 decimals
#define ld long double

ll p[200005];
ll a[200005];
ll av[200005];
ll pos[200005];
ll en[200005];
ll cst[200005];
ll pre[200005];

int main()
{
	FAST;
	ll n, i, j, k;
	cin >> n;
	fr(i,0,n)
		cin >> p[i];
	fr(i,0,n)
	{
		cin >> a[i];
		av[p[i]] = a[i];
		pos[p[i]] = i;
	}
	pre[0] = a[0];
	fr(i,1,n)
		pre[i] = pre[i - 1] + a[i];
	fr(i,0,n+1)
	{
		if(i == 0)
		{
			en[i] = 0;
			cst[i] = a[i];
			continue;
		}
		ll p = pos[i];
//		trace3(i, p, en[i-1]);
		if(p <= en[i-1])
			cst[i] = cst[i - 1] - av[i];
		else
		{
			ll t1 = pre[p - 1] - pre[en[i-1]];
			ll t2 = av[i];
			if((t1 <= t2)&&(p != n-1))
			{
				en[i] = p;
				cst[i] = cst[i-1] + t1;
			}
			else
			{
				en[i] = en[i-1];
				cst[i] = cst[i-1] + t2;
			}
		}
//		trace2(i, cst[i]);
	}
	ll mn = LLONG_MAX;
	fr(i,0,n+1)
		mn = min(mn, cst[i]);
	mn = min(mn, a[n-1]);
	mn = min(mn, a[0]);
	cout << mn << endl;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

ll bit[200001][2];

void add(ll val, int ind, int id){
    for(int i=ind; i<=2e5; i+=i&-i){
        bit[i][id]+=val;
    }
}
ll get(int ind, int id){
    ll ans = 0;
    for(int i=ind; i>0; i-=i&-i){
        ans+=bit[i][id];
    }
    return ans;
}
ll a[200001];
ll p[200001];

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int n; cin>>n;
    for(int i=1; i<=n; i++){
        cin>>p[i];
    }
    for(int i=1; i<=n; i++){
        cin>>a[i];
    }
    for(int i=1; i<=n; i++){
        add(a[i], p[i], 1);
    }
    ll ans = 1e18;
    for(int i=1; i<n; i++){
        add(a[i], p[i], 0);
        add(-a[i], p[i], 1);
        ans = min(ans, get(n, 0)-get(i, 0) + get(i,1));
        ans = min(ans, get(n, 0));
        ans = min(ans, get(n, 1));
    }
    cout<<ans;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t; cin>>t;
    while(t--){
        ll a, m;
        cin>>a>>m;
        ll g = __gcd(a,m);
        ll ans = m/g-1;
        set<ll> divs;
        for(ll i=1; i<=sqrt(m); i++){
            if(m%i==0){
                divs.insert(m/i);
                divs.insert(i);
            }
        }
        for(ll i:divs){
            bool bad = 0;
            for(ll j:divs){
                if(i%j==0&&i!=j&&j>g){
                    bad=1;
                    break;
                }
            }
            if(i%g==0&&i!=g&&!bad){
                ll start = i-(a%i);
                if(a+start<=m){
                    ans-=(m-start)/i-1;
                    if((m-start)%i)ans--;
                }

            }
        }
        cout<<ans<<'\n';
    }
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
#define rd() abs((int)rng())
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
void solve_case() {
	int n, x;
	cin >> n >> x;
	string s;
	cin >> s;
	s = '.' + s;
	vector<int>pref(n + 1);
	pref[0] = 0;
	for(int i = 1; i <= n; i++) {
		pref[i] = pref[i - 1];
		if(s[i] == '1') {
			pref[i]--;
		} else {
			pref[i]++;
		}
	}
	if(pref[n] == 0) {
		bool chk = false;
		for(int i = 0; i <= n; i++) {
			if(pref[i] == x) {
				chk = true;
			}
 		}
 		if(chk) {
 			cout << "-1\n";
 		} else {
 			cout << "0\n";
 		}
 		return;
	}

	int ans = 0;

	for(int i = 1 - (x == 0); i <= n; i++) {
		int need_add = (x - pref[i]) / pref[n];
		if(need_add >= 0 && (x - pref[i]) % pref[n] == 0) {
			++ans;
		}
	}
	cout << ans << "\n";
}
int main()
{
	ios_base::sync_with_stdio(false), cin.tie(0);
	int T;
	cin >> T;
	while(T--) {
		solve_case();
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

struct node{
   int l, r;
   ll val=0;
   ll lazy=0;
   node *lef, *righ;
};
void build(int l, int r, node* nod){
   nod->l = l;
   nod->r = r;
   if(l==r){
       return;
   }
   nod->lef = new node;
   nod->righ = new node;
   int mid = (l+r)/2;
   build(l, mid, nod->lef);
   build(mid+1, r, nod->righ);
}
void push(node* nod){
   nod->val+=nod->lazy;
   if(nod->l!=nod->r){
       nod->lef->lazy+=nod->lazy;
       nod->righ->lazy+=nod->lazy;
   }
   nod->lazy=0;
}
void upd(int l, int r, node* nod, ll val){
   push(nod);
   if(l>nod->r||r<nod->l){
       return;
   }
   if(l<=nod->l&&r>=nod->r){
       nod->lazy += val;
       push(nod);
       return;
   }
   upd(l, r, nod->lef, val);
   upd(l, r, nod->righ, val);
   nod->val = min(nod->lef->val, nod->righ->val);
}
node* st = new node;

ll a[200001];
ll p[200001];

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int n; cin>>n;
    for(int i=1; i<=n; i++){
        cin>>p[i];
    }
    for(int i=1; i<=n; i++){
        cin>>a[i];
    }
    build(1, n, st);
    for(int i=1; i<=n; i++){
        upd(p[i], n, st, a[i]);
    }
    ll ans = 1e18;
    for(int i=1; i<n; i++){
        upd(p[i], n, st, -a[i]);
        if(p[i]>1){
            upd(1, p[i]-1, st, a[i]);
        }
        ans = min(ans,st->val);
    }
    cout<<ans;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

struct node{
   int l, r;
   ll val=0;
   ll lazy=0;
   node *lef, *righ;
};
void build(int l, int r, node* nod){
   nod->l = l;
   nod->r = r;
   if(l==r){
       return;
   }
   nod->lef = new node;
   nod->righ = new node;
   int mid = (l+r)/2;
   build(l, mid, nod->lef);
   build(mid+1, r, nod->righ);
}
void push(node* nod){
   nod->val+=nod->lazy;
   if(nod->l!=nod->r){
       nod->lef->lazy+=nod->lazy;
       nod->righ->lazy+=nod->lazy;
   }
   nod->lazy=0;
}
void upd(int l, int r, node* nod, ll val){
   push(nod);
   if(l>nod->r||r<nod->l){
       return;
   }
   if(l<=nod->l&&r>=nod->r){
       nod->lazy += val;
       push(nod);
       return;
   }
   upd(l, r, nod->lef, val);
   upd(l, r, nod->righ, val);
   nod->val = min(nod->lef->val, nod->righ->val);
}
node* st = new node;

ll a[200001];
ll p[200001];

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int n; cin>>n;
    for(int i=1; i<=n; i++){
        cin>>p[i];
    }
    for(int i=1; i<=n; i++){
        cin>>a[i];
    }
    build(0, n, st);
    for(int i=1; i<=n; i++){
        upd(p[i], n, st, a[i]);
    }
    ll ans = 1e18;
    for(int i=1; i<n; i++){
        upd(p[i], n, st, -a[i]);
        upd(0, p[i]-1, st, a[i]);
        ans = min(ans,st->val);
    }
    cout<<ans;
}