#include <string>
#include <iostream>
#include <vector>
#include <set>
#include <iomanip>
#include <stack>
#include <cmath>
#include <map>
#include <tuple>
#include <numeric>
#include <queue>
#include <algorithm>
#include <bitset>
#include<iterator>
#include <fstream>
#include <random>
#include <unordered_map>
#include <ctime>
#include <sstream>

using namespace std;

#define INF 1e17
#define MOD %=((ll)1000000007);
#define ll long long
#define si short int
#define ull unsigned long long
#define str string
#define vc vector //by jgjgjjg :)
#define db double 
#define fco {ios_base::sync_with_stdio(0);std::cout.tie(0); std::cin.tie(0); }
#define scan(t) {for (ll i = 0; i<(t.size()); i++) std::cin >> t[i];}
#define cf {fflush(stdout);}
#define SUM(a,sum) ll sum = 0; for (auto _p:a) sum += _p;
#define MAX(a, mx, pos) ll mx = -(1e18+1); ll pos = 0;FOR(i,a.size()) if (a[i] > mx) {pos = i;mx=a[i];};
#define FL(f) bool f = false;
#define NOO cout << -1 << endl;return 0;
#define its(a) a.begin(),a.end()
#define itsr(a) a.rbegin(),a.rend()
#define pb push_back
#define mset multiset
#define pos2 pair<ll,ll>
#define sz(a) (int)a.size()
#define fi first
#define se second
#define mnmx(a,b) {ll __c = a; a = min(a,b), b = max(b,__c);}
#define np nullptr
#define tpl tuple<ll, ll,ll>
#define M 1000000007
vc<ll> tree;
void build(int l, int r, int v, vc<ll>&h) {
    if (r - l == 1) {
        tree[v] = l;
        return;
    }
    ll m = (r + l) / 2;
    build(l, m, 2 * v + 1, h);
    build(m, r, 2 * v + 2, h);
    if (h[tree[2 * v + 1]] < h[tree[2 * v + 2]]) {
        tree[v] = tree[2 * v + 2];
    }else tree[v] = tree[2 * v + 1];
}

ll gett(int lq, int rq, int l, int r, int v, vc<ll>& h) {
    if (lq <= l and r <= rq) return tree[v];
    if (lq >= r or rq <= l) return -INF;
    ll m = (r + l) / 2;
    ll lr = gett(lq, rq, l, m, 2 * v + 1, h), rr = gett(lq, rq, m, rq, 2 * v + 2, h);
    if (h[lr] < h[rr]) return rr;
    else return lr;
}


ll ans(ll l, ll r, vc<ll>&c, vc<ll>&h) {
    if (r < l) return INF;
    if (r == l) return c[l];
    if (l + 1 == r) {
        if (h[l] > h[r]) return c[l];
        return c[r];
    }
    
    ll mx = gett(l, r + 1, 0, sz(h), 0, h);
    if (mx == l or mx == r) return c[mx];
    return min(ans(l, mx-1, c,h),ans(mx+1,r,c,h)) + c[mx];
}

void solve() {
    ll n;
    cin >> n;
    vc<ll> h(n); scan(h);
    vc<ll> c(n); scan(c);
    tree.assign(4 * n, 0);
    build(0, n, 0, h);
    cout << ans(0, n-1, c, h);
    
        
    
}
/*
5 6
0.3333333
1 27
6 27
3 27
4 27
5 27
1 2 3 4 5 6

*/
int main() {
    fco;
    //ll _t; cin >> _t;
    //while (_t--)
        solve();
}