#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second

const int N = 2e5 + 100 , INF = 9e18 ;

struct nn
{
    int val , pos ;
};

nn gg( nn a , nn b )
{
    if ( a.val > b.val )
        return a ;
    else
        return b ;
}

int a[N] ;
int s[N] ;
int pref[N] ;
int suf[N] ;
nn tree[N * 4] ;

void build( int x , int tl , int tr )
{
    if ( tl == tr )
    {
        tree[x].val = a[tl] ;
        tree[x].pos = tl ;
        return ;
    }
    int mid = (tl + tr) / 2 ;
    build( x + x , tl , mid ) ;
    build( x + x + 1 , mid + 1 , tr ) ;
    tree[x] = gg( tree[x + x] , tree[x + x + 1] ) ;
}

nn get( int x , int tl , int tr , int l , int r )
{
    if ( tl >= l && tr <= r )
        return tree[x] ;
    if ( tl > r || tr < l )
        return { -INF , -1 } ;
    int mid = (tl + tr) / 2 ;
    return gg( get(x + x , tl , mid , l , r) , get( x + x + 1 , mid + 1 , tr , l , r ) ) ;
}

signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0) , cout.tie(0);

    int n , ans = 0 ;
    cin >> n ;

    for ( int i = 1 ; i <= n ; i++ )
    {
        cin >> a[i] ;
    }

    build( 1 , 1 , n ) ;

    for ( int i = 1 ; i <= n ; i++ )
    {
        cin >> s[i] ;
    }

    for ( int i = 1 ; i <= n ; i++ )
    {
        pref[i] = pref[i - 1] + s[i] ;
    }
    for ( int i = n ; i >= 1 ; i-- )
    {
        suf[i] = suf[i + 1] + s[i] ;
    }

    int l = 1 , r = n ;

    while ( r >= l )
    {
        nn u = get( 1 , 1 , n , l , r ) ;
        int p = u.pos ;
        ans += s[p] ;
        if ( suf[p - 1] - suf[n] >= pref[n] - pref[p - 1] )
        {
            r = p - 1 ;
        }
        else
        {
            l = p + 1 ;
        }
    }

    cout << ans ;

}