#include <bits/stdc++.h>
 
using namespace std;
 
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
 
#define all(x) x.begin() , x.end();
#define F first 
#define S second 
#define boost ios_base::sync_with_stdio(false);
#define ull unsigned long long 
#define ll long long 
#define open freopen("lepus.in","r",stdin);freopen("lepus.out","w",stdout)
 
const ll mod = (ll)1e9+7;  
const ll MOD = 998244353;       
const ll inf = (ll)1e18+7;               
const ll MX = LLONG_MAX;    //-> =+
const ll MN = LLONG_MIN;    //-> =-
const ll N = (ll)1e5+5;  
const int minf = -1e9 + 9;
const int INF = 1e9 + 9 ;
 
map<ll , ll>mp;
set<int>st;
//pair<int , int >p[N];
 
int main(){
    int n ;
    cin >> n ;
    ll a[n + 9] , b[n + 9];
    for(int i = 1;i <= n;i++){
        cin >> a[i];
        mp[a[i]] = INF;
    }
    for(int i = 1;i <= n;i++){
        cin >> b[i];
        mp[a[i]] = min(mp[a[i]] , b[i]);
    }
    int ans1 = 0 , ans2 = 0 ;
    int right_mx = n;
    for(int i = right_mx ;i >= 1 ;i--){
        if(a[right_mx] > a[i]){
            ans1+=mp[a[right_mx]];
            right_mx = i ;  
        }
        else right_mx=i;
    }
	ans1+=mp[a[right_mx]];
    int left_mx = 1;
    for(int i = left_mx;i <= n;i++){
        if(a[left_mx]>a[i]){
            ans2 += mp[a[left_mx]];
            left_mx = i ;
        }
        else left_mx=i;
    }
    ans2+=mp[a[right_mx]];
  cout << "ANS1 = " << ans1 << '\n';
  cout << "ANS2 = " << ans2 << '\n';
    cout << min(ans1 , ans2)  << '\n';
}