#include <stdio.h>

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <random>
#include <chrono>
#include <queue>
#include <functional>
#include <iostream>
#include <queue>
//#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ld long double

const ll maxn = 1e6 + 1, maxm = 1e7 + 1;
const ll mod = 1500000059, mod2 = 998244353, inf = 1e9, block = 550, hb = 1000012337, base = 1000050017;
const ld eps = 1e-3;

int n;
ll a[maxn], b[maxn], dp[maxn];
int l[maxn], r[maxn];
struct seg_tree{
    ll t[maxn * 4], la[maxn * 4];
    void push(int v, int l, int r){
        if (la[v] != inf * inf){
          //  cout << "lol\n";
            t[v] = min(t[v], la[v]);
            if (l != r){
                la[v + v] = min(la[v + v], la[v]);
                la[v + v + 1] = min(la[v + v + 1], la[v]);
            }
            la[v] = inf * inf;
        }
    }
    void build(int v = 1, int tl = 1, int tr = n){
        la[v] = inf * inf;
        t[v] = inf * inf;
        if (tl == tr){
            return;
        }
        int tm = (tl + tr) / 2;
        build(v + v, tl, tm);
        build(v + v + 1, tm + 1, tr);
    }
    void updlr(int l, int r, ll x, int v = 1, int tl = 1, int tr = n){
        push(v, tl, tr);
        if (l <= tl && tr <= r){
            la[v] = min(la[v], x);
            push(v, tl, tr);
            return;
        }
        if (l > tr || r < tl){
            return;
        }
        int tm = (tl + tr) / 2;
        updlr(l, r, x, v + v, tl, tm);
        updlr(l, r, x, v + v + 1, tm + 1, tr);
        t[v] = min(t[v + v], t[v + v + 1]);
    }
    ll get(int l, int r, int v = 1, int tl = 1, int tr = n){
        push(v, tl, tr);
        if (l <= tl && tr <= r){
            return t[v];
        }
        if (l > tr || r < tl){
            return inf * inf;
        }
        int tm = (tl + tr) / 2;
        return min(get(l, r, v + v, tl, tm), get(l, r, v + v + 1, tm + 1, tr));
    }
} rt, rt2;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i){
        cin >> a[i];
    }
    for (int i = 1; i <= n; ++i){
        cin >> b[i];
    }
    rt.build();
    rt2.build();
   // cout << rt.get(1, 1);
   // exit(0);
    stack<int> s;
    a[0] = inf + inf;
    s.push(0);
    for (int i = 1; i <= n; ++i){
        while (s.size() > 0 && a[s.top()] < a[i]){
            s.pop();
        }
        l[i] = s.top();
        s.push(i);
    }
    while (s.size() > 0){
        s.pop();
    }
    a[n + 1] = inf + inf;
    s.push(n + 1);
    for (int i = n; i >= 1; --i){
        while (s.size() > 0 && a[s.top()] < a[i]){
            s.pop();
        }
        r[i] = s.top();
        s.push(i);
    }
   // cout << rt.get(1, 1);
    for (int i = 1; i <= n; ++i){
        //i - max
     //   cout << i << '\n';
        dp[i] = inf * inf;
        dp[i] = min(dp[i], dp[i - 1] + b[i]);
        //cout << dp[i] << '\n';
        dp[i] = min(dp[i], rt2.get(l[i] + 1, i) + b[i]);
        //cout << dp[i] << '\n';
        dp[i] = min(dp[i], rt.get(i, i));
       // cout << dp[i] << " " << rt.get(i, i) << '\n';
        /*if (i == 2){
            exit(0);
        }*/
        if (i + 1 <= r[i] - 1){
            rt.updlr(i + 1, r[i] - 1, dp[i - 1] + b[i]);
         //   cout << i + 1 << " " << r[i] - 1 << '\n';
          //  exit(0);
        }
        rt2.updlr(i, i, dp[i - 1]);
        //cout << dp[i] << " ";
    }
    cout << dp[n];
}












