#include <bits/stdc++.h>
#define ll long long
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define ull unsigned long long
#define ld long double
#define files freopen("HelpMe.in", "r", stdin), freopen("HelpMe.out", "w", stdout)
using namespace std;
const ll mod = 1e9 + 7;
const ll N = 524288 + 10;
const ll NN = 2e5 + 10;
const ll P = 337ll;
const ld EPS = 1e-9;
const ll block = 400;
int n,m=1, lg[N], SpPos[20][N],nn, h[NN], c[NN];
void build() {
    for(ll i = 0; i<n; i++) {
        SpPos[0][i] = i;
    }
    for(ll st = 1; (1<<st) <= n; st++) {
        for(ll i = 0; i+ (1<<st) <= n; i++) {
            if(h[SpPos[st-1][i]] > h[SpPos[st-1][i + (1<<(st-1))]]) {
                SpPos[st][i] = SpPos[st-1][i];
            }
            else {
                SpPos[st][i] = SpPos[st-1][i + (1<<(st-1))];
            }
        }
    }
}
ll get(ll l, ll r) { // [l,r)
    ll st = lg[r-l];
    if(h[SpPos[st][l]] > h[SpPos[st][r- (1<<(st))]]) {
                return SpPos[st][l];
    }
    else {
        return SpPos[st][r- (1<<(st))];
    }
}
ll rec(ll l, ll r) {
    ll pos = get(l, r+1);
    if(pos == l || pos == r) {
        return c[pos];
    }
    else {
        ll tmp1 = rec(l, pos-1);
        ll tmp2 = rec(pos+1, r);
        return min(tmp1,tmp2) + c[pos];
    }
}
int main() {
cin>>n;
for(ll i= 0; i<n; i++) {
    cin>>h[i];
}
for(ll i= 0; i<n; i++) {
    cin>>c[i];
}
nn=n;
for(ll i = 2; i<N; i++) {
    lg[i] = lg[i>>1] + 1;
}
while(m < n) {
    m+=m;
}
n=m;
build();
cout<<rec(0, nn-1);
return 0;
}
// equal, min, max, 1, random
/*

*/
