#include<bits/stdc++.h>
#define ll long long int
#define mk make_pair
#define pb push_back
#define INF (ll)1e18
#define pii pair<ll,ll>
#define mod 998244353
#define ff first
#define ss second
#define srt(v)  if(!v.empty())sort(v.begin(),v.end())
#define rev(v)  if(!v.empty())reverse(v.begin(),v.end())
#define PI 3.141592653589793238
#define pqr priority_queue<ll,vector<ll>,greater<ll>()>
using namespace std;
ll pow_mod(ll a,ll b)
{
	ll res=1;
	while(b!=0)
	{
		if(b&1)
		{
			res=(res*a)%mod;
		}
		a=(a*a)%mod;
		b/=2;
	}
	return res;
}

ll inv_mod(ll x){
    return pow_mod(x,mod-2);
}

const ll N=(ll)3e5;
ll inv[N];
ll fac[N];

ll ncr(ll n,ll r){
    if(n<r)
    return 0;

    if(n==r||r==0)
    return 1LL;

    ll x=fac[n];
    ll y=inv[n-r];
    ll z=inv[r];

    return ((x*y)%mod*z)%mod;
}
void solve()
{
   ll n;
   cin>>n;
   ll a[n];
   
   for(ll i=0;i<n;i++)
   cin>>a[i];
   
   ll ans[n];
   
   ll sum[n];
   
   ll mx[n]={0};
   
   for(ll i=0;i<n;i++){
   	if(i==0)
   	sum[i]=a[i];
   	else
   	sum[i]=sum[i-1]+a[i];
   	
   	if(i!=0)
   	mx[i]=mx[i-1];
   	
   	mx[i]=max(mx[i],a[i]);
   }
   
   ll res=0;
   
   for(ll i=0;i<n;i++){
   	res+=sum[i];
   	ans[i]=res+mx[i]*(i+1);
   }
   
   
   for(ll i=0;i<n;i++)
   cout<<ans[i]<<"\n";
   
   cout<<endl;
}
int main()
{    ios_base::sync_with_stdio(false);
     cin.tie(NULL);
     cout.tie(NULL);

//Start from Here.

  ll t;
  t=1;
 // cin>>t;
  while(t--)
  solve();





  fac[0]=1;

  for(ll i=1;i<N;i++)
  fac[i]=(i*fac[i-1])%mod;

  for(ll i=0;i<N;i++)
  inv[i]=inv_mod(fac[i]);
//Good Bye!
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define int ll
const int N = (int) 2e5 + 7;
int n, a[N], b[N], aib[N];
map<int, vector<int>> pos;
map<int, int> cur;

void add(int i, int x) {
  while (i <= n) {
    aib[i] += x;
    i += i & (-i);
  }
}

int get(int i) {
  int sol = 0;
  while (i) {
    sol += aib[i];
    i -= i & (-i);
  }
  return sol;
}

signed main() {
  ios::sync_with_stdio(0); cin.tie(0);

  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) cin >> b[i];
  vector<int> foo, bar;
  for (int i = 1; i <= n; i++) {
    a[i] += i;
    b[i] += i;
    foo.push_back(a[i]);
    bar.push_back(b[i]);
  }
  sort(foo.begin(), foo.end());
  sort(bar.begin(), bar.end());
  if (foo != bar) {
    cout << "-1\n";
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    pos[b[i]].push_back(i);
  }
  int sol = 0;
  for (int i = 1; i <= n; i++) {
    int j = pos[a[i]][cur[a[i]]++];
    assert(j + get(j) >= 1);
    sol += j + get(j) - 1;
    add(j, -1);
  }
  cout << sol << "\n";
  return 0;
}
