#include <bits/stdc++.h>

#define _FORTIFY_SOURCE 0
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("fast-math")

using namespace std;

typedef long long ll;
typedef long double ld;
#define int ll
#define double ld

#ifdef LOCAL
#   define dbg(x) cout << #x " = " << (x) << '\n';
#   define dbgf printf
#else
#   define dbg(x)
#   define dbgf(...)
#endif

#define all(x) std::begin(x), std::end(x)
#define rall(x) std::rbegin(x), std::rend(x)

template<typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> &p) {
    return out << p.first << ' ' << p.second;
}

template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
    return in >> p.first >> p.second;
}

const auto boostIO = []() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout << fixed;
    cout.precision(12);
    return 0;
} ();

vector<int> p, dp;

int get(int x) {
    return (x == p[x] ? x : (p[x] = get(p[x])));
}

void union_(int a, int b) {
    a = get(a);
    b = get(b);
    p[a] = b;
}

void solve() {
    int n;
    cin >> n;
    p.resize(n);
    dp.resize(n);
    iota(all(p), 0);
    vector<int> h(n), c(n);
    vector<pair<int, int>> to_sort;
    for (int i = 0; i < n; i++) {
        cin >> h[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> c[i];
    }
    for (int i = 0 ; i < n; i++) {
        to_sort.emplace_back(h[i], i);
    }
    sort(all(to_sort));
    int ans = 0;
    for (int i = 0; i < n; i++) {
        int l = 0, r = 0;
        if (to_sort[i].second && h[to_sort[i].second - 1] < to_sort[i].first) {
            l = dp[get(to_sort[i].second - 1)];
            union_(to_sort[i].second - 1, to_sort[i].second);
        }
        if (to_sort[i].second + 1 < n && h[to_sort[i].second + 1] < to_sort[i].first) {
            r = dp[get(to_sort[i].second + 1)];
            union_(to_sort[i].second + 1, to_sort[i].second);
        }
        dp[to_sort[i].second] = c[to_sort[i].second] + min(l, r);
        ans = dp[to_sort[i].second];
    }
    cout << ans << '\n';
}

signed main() {

    solve();

    return 0;
}