#include <bits/stdc++.h>
using namespace std;
 
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
 
// using namespace __gnu_pbds;
  
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
// find_by_order
// order_of_key
 
#define pb push_back
#define mk make_pair
#define f first
#define s second
#define vt vector
#define y1 ggwp
#define rt return
#define br break
#define cn continue
#define en '\n'
#define yes cout << "YES\n";
#define no cout << "NO\n"
#define yesno(x); cout << (x ? "YES\n" : "NO\n");
#define dbg(x) cout << (#x) << ": " << (x) << '\n';
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define rev(x) reverse(x.begin(), x.end())
#define sz(x) (int)x.size()
#define left stupid_left
#define right stupid_right
 
template<class T, class TT> void mineq(T& a, const TT& b){ if (b < a) a = b;}
template<class T, class TT> void maxeq(T& a, const TT& b){ if (b > a) a = b;}
 
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) 
#define file_io freopen(".in", "r", stdin); freopen(".out", "w", stdout)
 
typedef double ld;
typedef long long ll;
typedef vector<int> vi; 
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pair<int, int>> vii;
typedef vector<pair<ll, ll>> vll;
    
const int N = 2e5 + 500;
const ll inf = 1e18;
const int K = 25;

int n;
ll h[N], c[N];
int st[K + 1][N], ind_max[K + 1][N];
int lg[N + 1];

int get_max(int l, int r){

    int i = lg[r - l + 1];

    int left = ind_max[i][l];
    int right = ind_max[i][r - (1 << i) + 1];
    
    if(h[left] > h[right]) rt left;

    rt right;

}

ll rec(int l, int r){
    if(l == r) rt c[l];
    if(l > r) rt 0;

    int max_pos = get_max(l, r);

    int left = rec(l, max_pos - 1);
    int right = rec(max_pos + 1, r);

    rt min(left, right) + c[max_pos];
}

int main(){
    fast_io;

    lg[1] = 0;
    for (int i = 2; i <= N; i++){
        lg[i] = lg[i/2] + 1;
    }

    for(int i = 0;i < N;i++){
        h[i] = inf;
    }

    cin >> n;
    for(int i = 0;i < n;i++) cin >> h[i];
    for(int i = 0;i < n;i++) cin >> c[i];

    for(int j = 0;j < n;j++){
        ind_max[0][j] = j;
        st[0][j] = h[j];
    }

    for (int i = 1; i <= K; i++){
        for (int j = 0; j + (1 << i) <= n; j++){
            st[i][j] = max(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);

            if(st[i - 1][j] > st[i - 1][j + (1 << (i - 1))]) ind_max[i][j] = ind_max[i - 1][j];
            else ind_max[i][j] = ind_max[i - 1][j + (1 << (i - 1))];
        }
    }

    cout << rec(0, n - 1) << en;
}