// BEST TEACHERS: NAZAROV SHARAF and USMONZODA SOMON
//                littbird           another_love
#include <bits/stdc++.h>
 #define ONLINE_JUDGE
#define fi first
#define se second
#define pb push_back
#define yes cout << "YES"
#define no cout << "NO"
#define int long long
#define in insert
#define endl '\n'
#define ld long double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using namespace std;
int n;
vector<int>c;
vector<int>a;
int f(int l,int r) {
    if(r - l == 0){
        return c[l];
    } 
    int mx = a[l];
    int pos = l;
    for(int i = l ;i < r; i ++){
        if(mx < a[i]) {
            mx = a[i];
            pos = i;
        }
    }
    int lx = f(l,pos);
    int rx = f(pos + 1, r);
    return min(lx,rx) + c[pos];
}
void solve() {
    cin >> n;
    c.assign(n,0);
    a.assign(n,0);
    for(int i =0 ;i < n;i ++){
        cin >> a[i];
    }
    for(int i =0 ;i < n;i ++){
        cin >> c[i];
    }
    cout << f(0,n);
}
signed main() {
#ifndef ONLINE_JUDGE
    // For getting input from input.txt file
    freopen("input.txt", "r", stdin);
    // Printing the Output to output.txt file
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int tt = 1;
   // cin >> tt;
    for (int q = 1; q <= tt; q++) {
        solve();
        cout << endl;
    }
}
