#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int n;
    cin >> n;
    
    vector<long long> heights(n);
    vector<long long> costs(n);
    vector<pair<long long, int>> data;

    for (int i = 0; i < n; i++) {
        cin >> heights[i];
        data.emplace_back(heights[i], i);
    }

    for (int i = 0; i < n; i++) {
        cin >> costs[i];
    }

    sort(data.rbegin(), data.rend());

    long long total_cost = 0;
    vector<int> roofs(n, 0);

    for (int i = 0; i < n; i++) {
        int index = data[i].second;
        int left = index;
        int right = index;

        while (left > 0 && heights[left - 1] < heights[index]) {
            left--;
        }

        while (right < n - 1 && heights[right + 1] < heights[index]) {
            right++;
        }

        long long min_cost = costs[index];

        if (left > 0) {
            min_cost = min(min_cost, costs[left - 1]);
        }

        if (right < n - 1) {
            min_cost = min(min_cost, costs[right + 1]);
        }

        total_cost += min_cost;
        roofs[index] = 1;
    }

    cout << total_cost << endl;
    
    return 0;
}
