#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<long, long>
#define endl "\n"
#define fs first
#define sc second
#define vll vector<ll>
void solve()
{
ll n;
cin >> n;
map<ll,ll> m;
vll a(n);
ll remain = 0, ans = 0, exp = 0;
for(ll i = 0; i < n; i++)
cin >> a[i];
for(ll i = 0; i < n; i++)
{
ll inp;
cin >> inp;
remain += inp;
m[a[i]] += inp;
}
vector<pll> v;
for(auto i : m)
v.push_back({i.sc, i.fs});
auto max_it = m.begin();
ll max_v = -1;
while(remain > 0)
{
for(auto it = m.begin(); it != m.end(); it++)
{
if((*it).sc == 0)
continue;
if((*it).fs > exp)
break;
ans += (*it).sc;
remain -= (*it).sc;
exp += (*it).sc;
(*it).sc = 0;
}
if((*max_it).sc != max_v)
for(auto it = m.begin(); it != m.end(); it++)
{
if((*it).sc > max_v)
{
max_v = (*it).sc;
max_it = it;
}
}
if((*max_it).sc > 0)
{
(*max_it).sc--;
ans += 2;
remain -= 1;
exp++;
max_v--;
}
}
cout << ans << endl;
}
int main()
{
cin.tie(NULL); cout.tie(NULL);
ios_base::sync_with_stdio(false);
ll t = 1;
//cin >> t;
while(t--) solve();
return 0;
}