#include <map>
#include <stack>
#include <stdio.h>
#include <stdlib.h>

#define ll long long
#define DEBUG pritnf("Passing [%s] in Line %d\n" , __FUNCTION__ , __LINE__) ;

const int MAX_N = 2e5 + 10 ;

std::map<int , int> bh ;
std::stack<int> st[MAX_N] ;

int n , tot , b[MAX_N] , BIT[MAX_N] ;

inline int lowbit(int x) {return x & (-x) ;}

int getsum(int x) {
	if (!x) return 0 ;

	int s = 0 ;
	for (; x ; x -= lowbit(x)) s += BIT[x] ;

	return s ;
}

void add(int x) {
	if (!x) return ;

	for (; x <= n ; x += lowbit(x)) ++BIT[x] ;
}

int main() {
	scanf("%d" , &n) ;
	for (int i = 1 ; i <= n ; ++i) {
		int a , p ;
		scanf("%d" , &a) ; a += i ;

		if (!bh.count(a)) bh[a] = ++tot ;
		p = bh[a] ; st[p].push(i) ;
	}
	for (int i = 1 ; i <= n ; ++i) scanf("%d" , &b[i]) ;

	///

	ll ans = 0 ;
	for (int i = n ; i ; --i) {
		int ned = b[i] + i , p = bh.count(ned) ? bh[ned] : 0 ;
		if (st[p].empty()) {ans = -1 ; break ;}

		int nw = st[p].top() ; st[p].pop() ;
		add(nw + 1) ; nw -= getsum(nw) ; ans += i - nw ;
	}

	printf("%lld\n" , ans) ;

	return 0 ;
}
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,tune=native")
#include<bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(),(x).end()
#define pb push_back
#define X first
#define Y second
#define mp make_pair

typedef long long ll;
typedef unsigned long long ull;
typedef long double dbl;
typedef pair<int, int> pii;

const int N = (int)1e5 + 5;

int A[N], B[N];
int n;

void solve(){
    cin >> n;
    ll sum = 0;
    for(int i = 0; i < n; i++){
        cin >> A[i];
        sum += A[i];
    }
    for(int i = 0; i < n; i++)
        cin >> B[i];
    vector<int> d1, d2;
    for(int i = 0; i < n; i++)
        if(i&1)d2.pb(B[i] - A[i]);
        else d1.pb(B[i] - A[i]);
    sort(all(d1));
    sort(all(d2));
    ll ans = sum;
    for(int pos = n/2-1; pos >= 0; pos--){
        sum += d1[pos] + d2[pos];
        ans = max(ans, sum);
    }
    cout << ans << endl;
}

signed main(){
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int T = 1;
    while(T--)
        solve();
    return 0;
}
