/*
It is code Zhanabergen Abdurroshid :))))
*/
#include<bits/stdc++.h>
#define ll long long
#define int ll
#define m_p make_pair
#define p_b push_back
#define pii pair<int,int>
#define piii pair<int,pii>
#define vi vector<ll>
#define vii vector<pii>
#define viii vector<pair<int,pii> >
#define si set<int>
#define sii multiset<pii>
#define siii set<pair<int,pii> >
#define F first
#define S second
#define pre precision
#define n_per next_permutation
#define sz size()
#define all(x) x.begin(),x.end()
#define RUN ios_base::sync_with_stdio(false);cin.tie(0);
#define OPEN freopen("rmq.in","r",stdin);freopen("rmq.out","w",stdout)
using namespace std;
ll fl(){
	ll a;
	cin>>a;
	return a;
}
const ll N = 7e6 + 9;
const ll INF = 1e18 + 9, MOD = 1e9 + 7;
int a[N], c[N], nxt[N][2], dp[N][2];
int t[N][2];
void upd(int v, int tl, int tr, int pos, int x){
	if(tl==tr){
		t[v][0] = x;
		return;
	}
	int tm = (tl+tr)>>1;
	if(pos<=tm)upd(v*2,tl,tm,pos,x);
	else upd(v*2+1,tm+1,tr,pos,x);
	t[v][0] = min(t[v*2][0],t[v*2+1][0]);
}
int get(int v, int tl, int tr, int l, int r){
	if(tl>=l&&tr<=r)return t[v][0];
	if(tl>r||tr<l)return INF;
	int tm = (tl+tr)>>1;
	return min(get(v*2,tl,tm,l,r),get(v*2+1,tm+1,tr,l,r));
}
void upd2(int v, int tl, int tr, int pos, int x){
	if(tl==tr){
		t[v][1] = x;
		return;
	}
	int tm = (tl+tr)>>1;
	if(pos<=tm)upd2(v*2,tl,tm,pos,x);
	else upd2(v*2+1,tm+1,tr,pos,x);
	t[v][1] = max(t[v*2][1],t[v*2+1][1]);
}
int get2(int v, int tl, int tr, int l, int r){
	if(tl>=l&&tr<=r)return t[v][1];
	if(tl>r||tr<l)return 0;
	int tm = (tl+tr)>>1;
	return max(get2(v*2,tl,tm,l,r),get2(v*2+1,tm+1,tr,l,r));
}
main(){
	RUN;
	//OPEN;
	int n = fl();
	for(int i=0;i<=n*4;i++)t[i][0] = INF;
	vii v;
	vi g;
	for(int i=1;i<=n;i++){
		a[i] = fl();
		g.p_b(a[i]);
	}
	sort(all(g));
	vi h = {g[0]};
	for(int i=1;i<n;i++)if(g[i]!=g[i-1])h.p_b(g[i]);
	map<int,int>m;
	for(int i=0;i<h.sz;i++){
		m[h[i]] = i+1;
	}
	for(int i=1;i<=n;i++){
		a[i] = m[a[i]];
		c[i] = fl();
	}
	int ans = INF;
	for(int i=1;i<=n;i++){
		int cur = get2(1,1,n,a[i],n);
		if(cur==0)dp[i][0] = c[i];
		else dp[i][0] = dp[cur][0]+c[i];
		upd2(1,1,n,a[i],i);
	}
	for(int i=n;i>=1;i--){
		int cur = get(1,1,n,a[i],n);
		if(cur==INF)dp[i][1] = c[i];
		else dp[i][1] = dp[cur][1]+c[i];
		upd(1,1,n,a[i],i);
	}
	ans = min(dp[n][0],dp[1][1]);
	for(int i=1;i<n;i++)ans = min(ans,dp[i][0]+dp[i+1][1]);
	cout<<ans;
	return 0;
}
