#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;

struct node{
    ll pos, val;
};

int n, sz = 1;
vl h, c;
vt<node> t;

node comb(node a, node b){
    if(a.val > b.val) rt a;
    rt b;
}

void build(int x, int lx, int rx){
    if(rx - lx == 1){
        t[x] = {lx, h[lx]}; rt;
    }

    int mx = (lx + rx) >> 1;

    build(2*x + 1, lx, mx);
    build(2*x + 2, mx, rx);

    t[x] = comb(t[2*x + 1], t[2*x + 2]);
}

node get(int l, int r, int x, int lx, int rx){

    if(rx <= l || r <= lx) rt {0, -inf};

    if(l <= lx && rx <= r){
        rt t[x];
    }

    int mx = (lx + rx) >> 1;

    rt comb(get(l, r, 2*x + 1, lx, mx), get(l, r, 2*x + 2, mx, rx));

}

int get(int l, int r){
    rt get(l, r + 1, 0, 0, sz).pos;
}

ll rec(int l, int r){
    if(l > r) rt 0LL;

    int max_pos = get(l, r);

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

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

int main(){
    fast_io;

    cin >> n;
    h.assign(n, 0);
    c.assign(n, 0);

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

    while(sz < n) sz *= 2;
    t.assign(2*sz - 1, {0, -inf});

    build(0, 0, sz);

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