#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1<<30; 
const int MOD = 1e9 + 7;
const int dy[] = {1, 0, -1, 0};
const int dx[] = {0, 1, 0, -1};
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<P> v1, v2;
    int A[n + 1], W[n + 1];
    for(int i = 0; i < n; i++) cin >> A[i];
    for(int i = 0; i < n; i++) cin >> W[i];
    for(int i = 0; i < n; i++)
    {
        if(A[i] == 1) v1.push_back(P(A[i], -W[i]));
        else v1.push_back(P(A[i], W[i]));
        if(A[i] == 0) v2.push_back(P(A[i], -W[i]));
        else v2.push_back(P(A[i], W[i]));
    }
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end(), greater<P>());
    int ans = v1[0].second - v1.back().second; 
    int idx = -1;
    for(int i = 0; i < v2.size() - 1; i++)
    {
        if(v2[i].first != v2[i + 1].first)
        {
            idx = i;
            break;
        }
    }
    if(idx == -1) cout << 0 << endl;
    else
    {
        ans = min(ans, v2[idx].second - v2[idx + 1].second);
        cout << ans << endl;
    }
    return 0;
}


