#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pp pair<ll,ll>
template<class T> istream& operator>>(istream& is, vector<T>& arr) { for(auto& i:arr) is>>i; return is;}
template<class T> ostream& operator<<(ostream& os, vector<T>& arr) { for(auto& i:arr) os<<i<<" "; return os;}

#define MAXN 200200
ll tree[MAXN*4];


void ins(ll w,ll l,ll r,ll pos,ll val){
if(pos<l||pos>r)return;
if(l==r){ tree[w]=val; return;}
ll med=(l+r)/2;
ins(w*2,l,med,pos,val);
ins(1+w*2,med+1,r,pos,val);
tree[w] = max(tree[w*2],tree[w*2+1]);
}

ll get_min(ll w,ll l,ll r,ll nl,ll nr){
if(l>nr||r<nl) return 2e17;
if(nl<=l&&r<=nr)return tree[w];
ll med=(l+r)/2;
return min( get_min(w*2,l,med,nl,nr), get_min(w*2+1,med+1,r, nl,nr) );
}

signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);

for(ll i=0;i<MAXN*4;i++) tree[i]=2e16;

ll n;
cin>>n;
vector<ll> h(n+1);
vector<ll> c(n+1);
for(ll i=1;i<=n;i++)cin>>h[i];
for(ll i=1;i<=n;i++)cin>>c[i];

multiset<ll> mm;
set<pp> vals;

set<ll> ss;
vector<ll> maxLeft(n+1);
vector<pp> p(n); for(ll i=1;i<=n;i++) p[i-1] = {h[i],i};
sort(rall(p));

for(ll ii=0;ii<n;ii++){
auto it = ss.lower_bound(p[ii].second);
if(it!=ss.begin()) maxLeft[ p[ii].second ] = *prev(it);
else maxLeft[ p[ii].second ] = -1;
ss.insert(p[ii].second);
}

vector<ll> dp(n+1);

for(ll i=1;i<=n;i++){
dp[i] = dp[i-1] + c[i];

while(vals.size()) {
if(vals.begin()->first>h[i]) break;
ll t = vals.begin()->second;
mm.erase( mm.find(t) );
vals.erase(vals.begin());
}

if(vals.size()) dp[i] = min(dp[i], *mm.begin() );

ll nl=maxLeft[i]+1,nr =i-1;
if(nl<=nr) dp[i]=min(dp[i], get_min(1,1,n, nl,nr)+c[i]);
ins(1,1,n, i, dp[i-1]);

vals.insert({h[i],dp[i-1]+c[i]});
mm.insert(dp[i-1]+c[i]);
}

cout<<dp[n];

}
