// Bolatulu
#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
#define int long long
#define kanagattandirilmagandiktarinizdan ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define pb push_back
#define F first
#define S second
#define md (tl+tr)/2
#define TL v+v,tl,md
#define TR v+v+1,md+1,tr

#pragma GCC target( "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")

using namespace std;

db binpow(db a,db n) {
    if (n==0)
        return (db)1;
    int zz=n;
    if (zz%2!=0)
        return (a * binpow(a,n-1));
    db z=binpow(a,n/2);
    return (z*z);
}

const ll INF = 1e18;
const int N =  2e5+7;
const int M = 1e9+7;
const ll HZ = 1e5;
const int MAX = INT_MAX;
const int MIN = INT_MIN;
const db pi = 3.141592653;
const int P=31;

int n,h[N],w[N],R[N],dp[N],t[4*N];
vector <int> t1(4*N,INF),t2(4*N,INF);

void upd(int v,int tl,int tr,int pos,int val) {
    if (tl==tr) {
        t[v]=val;
        return;
    }
    if (pos<=md)
        upd(TL,pos,val);
    else
        upd(TR,pos,val);
    t[v]=max(t[v+v],t[v+v+1]);
}

int get(int v,int tl,int tr,int l,int r) {
    if (tl>=l and tr<=r)
        return t[v];
    if (tl>r or tr<l)
        return 0;
    return max(get(TL,l,r),get(TR,l,r));
}

void push(int v) {
    t1[v+v]=min(t1[v+v],t1[v]), t1[v+v+1]=min(t1[v+v+1],t1[v]);
}

void upd1(int v,int tl,int tr,int l,int r,int val) {
    if (tl>=l and tr<=r) {
        t1[v]=min(t1[v],val);
        return;
    }
    if (tl>r or tr<l)
        return;
    push(v);
    upd1(TL,l,r,val), upd1(TR,l,r,val);
}

int get1(int v,int tl,int tr,int pos) {
    if (tl==tr)
        return t1[v];
    push(v);
    if (pos<=md)
        return get1(TL,pos);
    else
        return get1(TR,pos);
}

void upd2(int v,int tl,int tr,int pos,int val) {
    if (tl==tr) {
        t2[v]=val;
        return;
    }
    if (pos<=md)
        upd2(TL,pos,val);
    else
        upd2(TR,pos,val);
    t2[v]=min(t2[v+v],t2[v+v+1]);
}

int get2(int v,int tl,int tr,int l,int r) {
    if (tl>=l and tr<=r)
        return t2[v];
    if (tl>r or tr<l)
        return INF;
    return min(get2(TL,l,r),get2(TR,l,r));
}

void solve() {
    cin >> n;
    for (int i=1;i<=n;i++)
        cin >> h[i];
    for (int i=1;i<=n;i++)
        cin >> w[i];
    for (int i=n;i>=1;i--) {
        upd(1,1,n,i,h[i]);
        int l=i,r=n+1;
        while (l+1<r) {
            int mid=(l+r)/2;
            if (get(1,1,n,i+1,mid)<h[i])
                l=mid;
            else
                r=mid;
        }
        R[i]=l;
    }
    for (int i=1;i<=4*n;i++)
        t[i]=INF;
    for (int i=1;i<=n;i++) {
        upd(1,1,n,i,h[i]);
        int l=0,r=i;
        while (l+1<r) {
            int mid=(l+r)/2;
            if (get(1,1,n,mid,i-1)<h[i])
                r=mid;
            else
                l=mid;
        }
        upd1(1,1,n,i,R[i],w[i]+dp[i-1]);
        if (r==i)
            dp[i]=dp[i-1]+w[i];
        else
            dp[i]=get2(1,1,n,r,i-1)+w[i];
        dp[i]=min(get1(1,1,n,i),dp[i]);
        upd2(1,1,n,i,dp[i]);
        // cout << dp[i] << ' ' << R[i] << ' ' << r << endl;
    }
    cout << dp[n];
}
signed main() {
    // freopen("lca.in", "r", stdin);
    // freopen("lca.out", "w", stdout);
    kanagattandirilmagandiktarinizdan
    int test = 1, count = 1;
    // cin >> test;
    while (test--) {
        // cout << "Case " << count << ":\n";
        solve();
        if (test)
            // cout << '\n';
            count++;
    }
    return 0;
}