#include <bits/stdc++.h>
using namespace std;
int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	int n;
	cin >> n;
	vector<int> a(n);
	vector<int> b(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		a[i] += i;
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
		b[i] += i;
	}
	vector<int> c = a;
	vector<int> d = b;
	sort(begin(c), end(c));
	sort(begin(d), end(d));
	if (c != d) {
		cout << -1 << "\n";
	} else {
		for (auto &i : a) {
			i = lower_bound(begin(c), end(c), i) - begin(c);
		}
		for (auto &i : b) {
			i = lower_bound(begin(d), end(d), i) - begin(d);
		}
		vector<vector<int>> ord(n);
		for (int i = 0; i < n; i++) {
			ord[a[i]].emplace_back(i);
		}
		vector<int> cnt(n, 0);
		for (int i = 0; i < n; i++) {
			int t = b[i];
			b[i] = ord[t][cnt[t]++];
		}
		vector<int> t(n * 2, 0);
		auto add = [&](int x) {
			int p = n + x;
			while (p) {
				t[p] += 1;
				p /= 2;
			}
		};
		auto query = [&](int l, int r) {
			int res = 0;
			for (l += n, r += n; l < r; l /= 2, r /= 2) {
				if (l & 1) {
					res += t[l++];
				}
				if (r & 1) {
					res += t[--r];
				}
			}
			return res;
		};
		long long res = 0;
		for (int i = 0; i < n; i++) {
			res += query(b[i] + 1, n);
			add(b[i]);
		}
		cout << res << "\n";
	}
}
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
#define F first
#define S second
#define pb push_back
#define mp make_pair 
#define rep(i,a,b) for(int i = a; i <= b; i++)
#define rrep(i,a,b) for(int i = a; i >=b; i--)
#define debug(x) cout << '>' << #x << ':' << x << endl;
#define sq(a) (a)*(a)
#define nl "\n"
#define all(x) (x).begin(),(x).end()
using namespace std;
//using namespace __gnu_pbds;
//typedef tree<int,null_type,less<int>,rb_tree_tag,
//tree_order_statistics_node_update> indexed_set;

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
using ii = pair<int, int>;
using pli = pair<ll, int>;
using vi = vector<int>;
using vll = vector<ll>;
using pll = pair<ll, ll>;
using pil = pair<int,ll>;
using vvi = vector<vi>;
using vpi = vector<ii>;

const int N = 100100;
ll mod=1000000007;

ll ceil_div(ll a, ll b) {
    return a / b + ((a ^ b) > 0 && a % b != 0);
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	ll n;
	cin>>n;ll mx1=0,ans=0;
	vector<ll>a(n),b(n);
	rep(i,0,n-1)cin>>a[i];
	rep(i,0,n-1)cin>>b[i];
	rep(i,0,n-1){
		mx1=max(mx1,a[i]);
		ans = max(mx1*b[i],ans);
		cout<<ans<<nl;
	}
	return 0;
}


