#include <bits/stdc++.h>
using namespace std;
int main()
{
iostream::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> be(n);
vector<int> req(100000,0);
for(int i = 0; i < n; i++)
{
cin >> be[i];
}
int t;
for(int i = 0; i < n; i++)
{
cin >> t;
req[be[i]] += t;
}
int curExp = 0;
long long time = 0;
int l=0; //-> at lowest requirement place
int h=99999; // highest requirement place
while(l != h)
{
while(req[l]==0)
{
l++;
//cout << l <<"l\n";
}
while(req[h]==0)
{
h--;
//cout << h <<"h\n";
}
if(curExp >= l)
{
//cout << "a\n";
//cout << curExp << "-" << time << " " << l << " " << req[l] << ":" << h << " " << req[h] << "\n";
curExp += req[l];
time += req[l];
req[l] = 0;
//cout << curExp << "-" << time << " " << l << " " << req[l] << ":" << h << " " << req[h] << "\n----------\n";
}
else
{
if(curExp + req[h] >= l)
{
//cout << "b\n";
//cout << curExp << "-" << time << " " << l << " " << req[l] << ":" << h << " " << req[h] << "\n";
time+=2*(l-curExp);
req[h]-= l-curExp;
curExp += l-curExp;
//cout << curExp << "-" << time << " " << l << " " << req[l] << ":" << h << " " << req[h] << "\n----------\n";
}
else //curExp + x lepés = l -> x-et keressük
{
//cout << "c\n";
//cout << curExp << "-" << time << " " << l << " " << req[l] << ":" << h << " " << req[h] << "\n";
curExp += req[h];
time += 2*req[h];
req[h] = 0;
//cout << curExp << "-" << time << " " << l << " " << req[l] << ":" << h << " " << req[h] << "\n----------\n";
}
}
}
if(curExp>=l)
{
curExp += req[l];
time += req[l];
req[l] = 0;
}
else if(curExp + req[h] < l)
{
curExp += req[h];
time += 2*req[h];
req[h] = 0;
}
else
{
time+=2*(l-curExp);
curExp += l-curExp;
req[h]-=l-curExp;
time+=req[h];
}
cout << time;
}