#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include<deque>
using namespace std;
typedef long long ll;

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

ll ans = 8e18;
vector<vector<pair<int, int>>> ST;
vector<int> h, c, lg2;

int get(int l, int r) {
    int d = lg2[r - l + 1];
    pair<int, int> v1 = ST[d][l],
        v2 = ST[d][r - (1 << d) + 1];

    if (v1.first > v2.first) return v1.second;
    else return v2.second;
}

void solve(int l, int r, ll cur) {
    if (cur > ans) return;
    //cout << l << ' ' << r << ' ' << cur << endl;

    if (l == r) {
        ans = min(ans, cur + c[l]);
        return;
    }

    int ind = get(l, r);

    if (l <= ind - 1) solve(l, ind - 1, cur + c[ind]);
    else ans = min(ans, cur + c[ind]);
    if (ind + 1 <= r) solve(ind + 1, r, cur + c[ind]);
    else ans = min(ans, cur + c[ind]);
}

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(false);

    int n; cin >> n;
    lg2.resize(n + 5, 0);
    for (int i = 2; i < lg2.size(); i *= 2)
        lg2[i] = lg2[i / 2] + 1;
    for (int i = 3; i < lg2.size(); i++)
        lg2[i] = max(lg2[i - 1], lg2[i]);

    h.resize(n), c.resize(n);
    for (int& x : h) cin >> x;
    for (int& x : c) cin >> x;

    ST.resize(22, vector<pair<int, int>>(n, { -2e9, -2e9 }));

    for (int i = 0; i < n; i++)
        ST[0][i] = { h[i], i };

    for (int i = 1; i < 22; i++) {
        for (int j = 0; j < n; j++) {
            if (j + (1 << i) - 1 >= n) break;

            if (ST[i - 1][j].first > ST[i - 1][j + (1 << (i - 1))].first) {
                ST[i][j] = ST[i - 1][j];
            }
            else {
                ST[i][j] = ST[i - 1][j + (1 << (i - 1))];
            }
        }
    }

    solve(0, n - 1, 0);

    cout << ans;
}