# include <bits/stdc++.h>

# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
# define all(x) (x).begin(), (x).end()
# define deb(x) cerr << #x  << " = " << x << endl; 
# define pll pair <ll, ll>
# define pii pair <int, int>
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
 
const ll maxn = 2e5 + 7;
const ll inf = 2e18 + 0;
const ll mod = 1e9 + 7;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};

using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll h[maxn] , c[maxn] , lg[maxn] , sp[maxn][20] , t[maxn*4] , dp[maxn];

void build(ll v, ll tl , ll tr){
  if(tl == tr){
    t[v] = dp[tl];
    return;
  }
  ll tm = (tl+tr) >> 1;
  build(v*2,tl,tm);
  build(v*2+1,tm+1,tr);
  t[v] = min(t[v*2] , t[v*2+1]);
} 
void upd(ll v, ll tl , ll tr ,ll pos, ll val){
  if(tl == tr){
    t[v] = val;
    return; 
  }
  ll tm = (tl+tr) >> 1;
  if(tm >= pos) upd(v*2,tl,tm,pos,val);
  else upd(v*2+1,tm+1,tr,pos,val);
  t[v] = min(t[v*2],t[v*2+1]);
}
ll getmn(ll v, ll tl , ll tr, ll l , ll r){
  if(l <= tl && tr <= r){
    return t[v];
  }
  if(tl > r || tr < l) return inf;
  ll tm = (tl+tr) >> 1;
  return min(getmn(v*2,tl,tm,l,r),getmn(v*2+1,tm+1,tr,l,r));
}
ll get(ll l , ll r){
  ll len = lg[r-l+1];
  return max(sp[l][len],sp[r-(1 << len)+1][len]);
}

void ma1n () {
    ll n;
    cin >> n;
    for(ll i = 1; i <= n; i++){
      cin >> h[i];
    }
    for(ll i = 1; i <= n; i++){
      cin >> c[i];
    }
    for(ll i = 1; i <= n; i++){
      sp[i][0] = h[i];
      if(i == 1) lg[i] = 0;
      else lg[i] = lg[i/2]+1;
    }
    for(ll j = 1; j <= lg[n]; j++){
      for(ll i = 1; i+(1 << j)-1 <= n; i++){
      sp[i][j] = max(sp[i][j-1],sp[i+(1 << (j-1))][j-1]);
      }
    }
    for(ll i = 1; i <= n; i++) dp[i] = dp[i-1]+c[i]; 
    build(1,0,n);
    set<pair<ll,ll>> st;
    multiset<ll> mn;
    for(ll i = 1; i <= n; i++){
      ll res = i , l = 1 , r = i;
      while(l <= r){
        ll md = (l+r) >> 1;
        if(get(md,i) <= h[i]){
          res = md;
          r = md-1;
        } else l = md+1;
      }
      dp[i] = min(dp[i],getmn(1,0,n,res-1,i)+c[i]);
//      cout << dp[i] <<" " << res << "\n";
      res = i , l = i , r = n;
      while(l <= r){
        ll md = (l+r) >> 1;
        if(get(i,md) <= h[i]){
          res = md;
          r = md-1;
        } else l = md+1;
      }
      while(st.size() && st.begin()->ff < i){
        ll val = st.begin()->ss;
        mn.erase(mn.find(val));
        st.erase(*st.begin());
      }
      if(mn.size()){
        ll l = *mn.begin();
          dp[i] = min(dp[i] , l);
      }
      upd(1,0,n,i,dp[i]);
      st.insert({res,dp[i-1]+c[i]});
      mn.insert(dp[i-1]+c[i]);
    }
    cout << dp[n];
}
 
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
//  freopen("file.in", "r", stdin);
//  freopen("file.out", "w", stdout);
    ll ttt = 1;
//  cin >> ttt;
    for (ll test = 1; test <= ttt; ++test) {
//      cout << "Case " << test << ":" << '\n';
        ma1n();
    }
    return 0;
}
