//                   LETS GOOOOOOOOO
#include <bits/stdc++.h>
using namespace std;

#define sz size
#define all(n) n.begin(),n.end()
#define ull unsigned long long
#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define len length
#define yes cout<<"YES\n"
#define up upper_bound
#define lb lower_bound
#define no cout<<"NO\n"
#define delb pop_back
#define F first
#define S second
#define sled "\n"
#define vi vector<int>
#define vll vector<long long>
#define gcd __gcd
#define emp empty
#define startt ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define delenie cout<<"\n=========\n"
#define in insert
#define mp make_pair

const long long MOD1=998244353;
const long long MOD2=1e9+7;
const long double Eps = 1e-12;
const int max1 = 1e9*1.4; 
const int min1 = -1e9 *1.4; 
const int mod = 1e9 + 7;
const int N = 2e5 + 100;
const int B = 1e6*2 + 100;
const ll INF = 1e18 + 100;
const int G=1e2+2;
int n;
pii h[200002];
int c[200002];
bool cmp1(pair<int, int> A, pair<int, int> B) {
	if (A.F <= B.F) {
		return true;
	} else {
		return false;
	}
}
bool cmp2(pair<int, int> A, pair<int, int> B) {
	if (A.S <= B.S) {
		return true;
	} else {
		return false;
	}
}
ll rec(int l, int r){
	if (l==r){
		return c[l];
	}
	sort(h+l, h + r + 1, cmp2);
	int cor = h[r].F;
	sort(h+l, h + r + 1, cmp1);
	if (cor > 1 and cor < n) {
		return min(rec(l, cor-1), rec(cor+1, r)) + c[cor];
	} else if (cor == n or cor == 1) {
		return c[cor];
	}
	
}

int main () {
    startt;
	cin>>n;
    for(int i=1;i<=n;i++){
    	cin>>h[i].S;
    	h[i].F = i;
	}
	for(int i=1;i<=n;i++){
		cin>>c[i];
	}
	cout<<rec(1, n);
}






