#include<bits/stdc++.h>
using namespace  std;
#define ll long long int
#define pb push_back 
#define IShowSpeed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const int N=2e5+100;
const int mod=1e9+7;
ll b[N],dp[N];
vector< pair<ll,int> >a;

int main()
{
	int n;
	cin>>n;
	dp[0]=0;
	for(int i=1;i<=n;i++)
	{
		ll x;
		cin>>x;
		a.pb({x,i});
	}
	sort(a.begin(),a.end());
	for(int i=1;i<=n;i++)
	{
		cin>>b[i];
		dp[i]=dp[i-1]+b[i];
	}
	ll l=1,r=n,ans=0;
	while(l<r)
	{
		int pos=a.back().second;
		a.pop_back();
		if(dp[pos-1]-dp[l-1]>=dp[r]-dp[pos])
		{
			ans+=b[pos];
			l=pos+1;
		}
		else
		{
			ans+=b[pos];
			r=pos-1;
		}
	}
	cout<<ans+b[l];
}