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

#define ll long long
#define int long long
const int N = 2e5;
int n,h[N],c[N],t[N * 4],l[N],dp[N];
void upd(int pos,int val,int v=  1,int tl = 1,int tr = n){
    if(tl==tr){
        t[v] = val;
    }else{
        int tm = (tl + tr) >> 1;
        if(pos <= tm) upd(pos,val,v+v,tl,tm);
        else upd(pos,val,v+v+1,tm+1,tr);
        t[v] = min(t[v + v],t[v + v + 1]);
    }
}
int get(int l,int r,int v = 1,int tl =1 ,int tr = n){
    if(l > r || tl > r || l > tr) return 1e18;
    if(tl >= l && tr <= r) return t[v];
    int tm = (tl + tr) >> 1;
    return min(get(l,r,v+v,tl,tm),get(l,r,v+v+1,tm+1,tr));
}
main(){
    cin >> n;
    vector<int> st;
    for(int i = 1;i <= n;i++){
        cin >> h[i];
        while(!st.empty() && h[st.back()] < h[i]) st.pop_back();
        l[i] = (st.empty() ? 0:st.back());
        st.push_back(i);
    }
    set<pair<ll,ll>> s;
    multiset<ll> xx;
    for(int i =1;i <= n;i++){
        ll x;
        cin >> x;
        c[i] = x;
        dp[i] = 1e18;
        while(!s.empty()){
            auto [t,pos] = (*s.begin());
            if(t < h[i]){
                xx.erase(xx.find(dp[pos - 1] + c[pos]));
                s.erase({t,pos});
            }else break;
        }
        if(!l[i]){
            dp[i] = x;
        }
        {
            dp[i] = min(dp[i],get(l[i],i -1)+x);
            if(!xx.empty()){
                dp[i] = min(dp[i],*xx.begin());
            }
        }
        upd(i,dp[i]);
        s.insert({h[i],i});
        xx.insert(dp[i] + c[i - 1]);
    }
    cout << dp[n];
}