/*Lazy propagation 21
 *
 *
 * Imansoska228
 * Imancrush
 * Imantopchik
 * .*n*.
 * Author: Imansoska
**/
#include <bits/stdc++.h>
using namespace std;
  
#define ll long long
#define FF first
#define SS second
#define pb push_back
#define bp pop_back()
#define pf push_front
#define int long long
#define Imansoska ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
#define endl cout << "\n"
#define len(x) x.size()
//#define input cin 
//#define print cout
  
const int N = 1e5 + 13 ;
const int maxn= 1e6+100;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
 
int z[N*2] , a[N*2];
int n , mx=0 , imx;
 
signed main (/*Imansoska*/) {
    Imansoska
    // freopen("bfs.in","r",stdin);
    // freopen("bfs.out","w",stdout);
    cin >> n;
    for ( int i=1; i<=n; i++ ) {
    	cin >> z[i];
    	if ( mx<z[i] ) {
    		mx=z[i];
    		imx=i;
		}
	}
	for ( int i=1; i<=n; i++ ) {
		cin >> a[i];
		a[i]+=a[i-1];
	}
	int l=1 , r=n , ans=0; 
	while ( l<=r ) {
		ans+=a[imx]-a[imx-1];
		if ( a[r]-a[imx-1]>a[imx]-a[l-1] ) {
			r=imx-1;
		}
		else {
			l=imx+1;
		}
		mx=0;
		for ( int i=l; i<=r; i++ ) {
			if ( mx<z[i] ) {
				mx=z[i];
    			imx=i;
			}
		}
	}
	cout << ans;
}