#include <iostream>
#include <vector>
#include <climits>

using namespace std;

int min_cost_roofs(int n, const vector<int>& heights, const vector<int>& costs) {
    vector<int> dp(n + 1, INT_MAX);  
    dp[0] = 0;  

    for (int i = 1; i <= n; i++) {
        int max_height = heights[i - 1];  
        int min_cost = INT_MAX;  

        for (int j = i; j > 0; j--) {
            max_height = max(max_height, heights[j - 1]);  
            int cost = costs[j - 1] + dp[j - 1];  
            min_cost = min(min_cost, cost);  

            if (max_height == heights[j - 1]) {
                dp[i] = min(dp[i], min_cost);  
            }
        }
    }

    return dp[n];  
}

int main() {
    int n;
    cin >> n;

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

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

    int result = min_cost_roofs(n, heights, costs);
    cout << result << endl;

    return 0;
}