#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const ll inf = 1e18;

signed main() {
    std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);
    ll n;
    cin >> n;
    vector<ll> c(n + 1, inf), h(n + 1, inf);
    for (ll i = 1; i <= n; i++)
        cin >> h[i];
    for (ll i = 1; i <= n; i++)
        cin >> c[i];

    vector<ll> dp(n + 1, 0);
    deque<pair<ll, ll>> a; // {last >h[i], mind}
    map<ll, ll> b; // {last >=h, mind}
    a.emplace_back(0, inf);
    for (ll i = 1; i <= n; i++) {
        while (h[a.back().first] <= h[i]) {
            a.pop_back();
        }
        auto [j, k] = a.back();

        dp[i] = min(k,             // вправо
                    dp[j] + c[i]); // влево
        if (b[h[i]]) {
            int q = b[h[i]];

            dp[i] = min(dp[i],
                        min(dp[q] + c[i],
                            dp[q - 1] + c[q] + c[i]));
        }

        a.emplace_back(i, min(k, dp[i - 1] + c[i]));
        b[h[i]] = i;
    }
    cout << dp[n];
}