#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


void solve() {
    ll n;
    cin >> n;
    vc<ll> h(n+2, INF);
    for (int i = 1; i <= n; i++) cin >> h[i];
    vc<ll> c(n + 2, INF);
    for (int i = 1; i <= n; i++) cin >> c[i];
    vc<ll> st = { 0 };
    vc<ll> l(n + 2);
    for (int i = 1; i <= n; i++) {
        while (h[st.back()] <= h[i]) st.pop_back();
        l[i] = st.back();
        st.push_back(i);
    }
    st = { 0 };
    vc<ll> r(n + 2);
    for (int i = 1; i <= n+1; i++) {
        while (h[st.back()] < h[i]) {
            r[st.back()] = i;
            st.pop_back();
        }
        st.push_back(i);
    }
    vc<ll> dpr(n + 2, 0);

    for (int i = 1; i <= n; i++) {
        ll p = l[i];
        dpr[i] = dpr[p] + c[i];
        if (p-1 >= 0) dpr[i] = min(dpr[i], c[p] + dpr[p-1]);
    }
    cout << dpr[n];

    
        
    
}
/*
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();
}