#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <fstream>
#include <unordered_map>
using namespace std;

typedef long long ll;
typedef long double ld;

#define all(x) x.begin(),x.end()
#define pb push_back
#define ent "\n"
#define F first
#define S second
const int maxn = (int)2e5 + 13;
const ll inf = (long long)1e18 + 20;
const int mod = (int)1e9 + 7;
int a[maxn],b[maxn];
pair<int,int> t[4 * maxn];
    int n;
void build(int v,int l,int r){
    if(l == r){
        t[v] = {a[l],l};
        return;
    }
    int mid =(l + r) / 2;
    build(v+v,l,mid);
    build(v+v+1,mid+1,r);
    t[v].F = max(t[v+v].F,t[v+v+1].F);
    if(t[v+v].F == t[v].F)t[v].S = t[v+v].S;
    if(t[v+v+1].F == t[v].F)t[v].S = t[v+v+1].S;
}
pair<int,int> get(int v,int l,int r,int ql,int qr){
    if(ql <= l && r <= qr){
        return t[v];
    }
    if(r < ql || qr < l)return {0,0};
    int mid = (l + r) / 2;
    pair<int,int> z = get(v+v,l,mid,ql,qr);
    pair<int,int> x = get(v+v+1,mid+1,r,ql,qr);
    pair<int,int> ans;
    ans.F = max(z.F,x.F);
    if(ans.F == z.F)ans.S = z.S;
    if(ans.F == x.F)ans.S =x.S;
    return ans;
}
ll get(int l,int r){
    if(l == r){
        return b[l];
    }
    pair<int,int> u = get(1,1,n,l,r);
    if(u.S == l)return b[l];
    if(u.S == r)return b[r];
    return (b[u.S] + min(get(l,u.S - 1),get(u.S + 1,r)));
}
void solve(){
    cin >> n;
    for(int i = 1;i <= n;i++){
        cin >> a[i];
    }
    build(1,1,n);
    for(int i = 1;i <= n;i++){
        cin >> b[i];
    }
    cout << get(1,n);
}

int main(){
 ios_base::sync_with_stdio(0);
 cin.tie(0);
 int t = 1;
 //cin >> t;
 while(t --){
  solve();
 }
 return 0;
}
