#include<bits/stdc++.h>

#define ll long long

using namespace std;

const int N = (int)2e5 + 7;
const long long INF = (long long)1e18 + 7;

int n, a[N], c[N];
int l[N], r[N];
ll d[N];
vector<pair<int, int> > g[N];


int main() {
    ios_base::sync_with_stdio(NULL);
    cin.tie(0);
    cin >> n;
    for(int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    for(int i = 1; i <= n; ++i) {
        cin >> c[i];
    }
    stack<int> st;
    for(int i = 1; i <= n; ++i) {
        l[i] = i;
        while(!st.empty() && a[st.top()] <= a[i]) {
            l[i] = l[st.top()];
            st.pop();
        }
        st.push(i);
    }
    while(!st.empty()) {
        st.pop();
    }
    for(int i = n; i >= 1; --i) {
        r[i] = i;
        while(!st.empty() && a[st.top()] <= a[i]) {
            r[i] = r[st.top()];
            st.pop();
        }
        st.push(i);
    }
    for(int i = 1; i <= n; ++i) {
        g[l[i]-1].push_back({i, c[i]});
        g[i-1].push_back({r[i], c[i]});
    }
    fill(d+1, d+1+n, INF);
    priority_queue<pair<ll, int> > q;
    q.push({0, 0});
    while(!q.empty()) {
        int v = q.top().second;
        ll cur_d = -q.top().first;
        q.pop();
        if(cur_d > d[v]) {
            continue;
        }
        for(auto p : g[v]) {
            int to = p.first;
            int w = p.second;
            if(d[v] + w < d[to]) {
                d[to] = d[v]+w;
                q.push({-d[to], to});
            }
        }
    }
    cout << d[n];
    return 0;
}


