#include <iostream>
#include <vector>
using namespace std;
int main(){
	long long a;
	cin >> a;
	long long h[a], c[a];
	long long max = 0;
	for(long long i = 0; i < a; i++){
		cin >> h[i];
		if(max < h[i]){
			max = h[i];
		}
	}
	for(long long i = 0; i < a; i++){
		cin >> c[i];
	}
	long long count = 0;
	for(long long i = 0; i < a; i++){
		if(h[i] == max){
			count++;
		}
	}
	long long index[count];
	long long j = 0;
	for(long long i = 0; i < a; i++){
		if(h[i] == max){
			index[j] = i;
			j++;
		}
	}
	vector<long long> q;
	long long min = 100000000;
	for(long long i = 0; i < a; i++){
		if(h[i] == max){
			long long sum = 0;
			for(long long j = 0; j < q.size(); j++){
				sum += q[j];
			}
			if(min > sum){
				min = sum;
			}
			q.clear();
		}else{
			q.push_back(c[i]);
		}
	}
	long long price = 0;
	for(long long i = 0; i < a; i++){
		if(h[i] == max){
			price += c[i];
		}
	}
	price += min;
	cout << price;
}



//6 5 4 3 10 6 7 7 5 10 8 7 10 9 9 7