#include <bits/stdc++.h>
#define rep(begin, end) for (i=begin;i<end;i++)
#define repj(begin, end) for (j=begin;j<end;j++)
#define init(arr, end, val) for (i=0;i<end;i++) arr[i] = val;
#define printint(i0, i1) printf("%d %d\n", i0, i1)

using namespace std;
typedef long long int ll;
const ll inf = 1000000000;
const ll mod = 1000000007;
const ll nil = -1;
ll i, m, k, t, ans;
ll n[100000], a[100000], b[100000];

int main() {
    scanf(" %lld", &t);
    rep(0,t) {
        scanf(" %lld %lld %lld", &n[i], &a[i], &b[i]);
    }
    rep(0,t) {
        if (n[i] < a[i] + b[i]) {
            printf("0\n");
            continue;
        }
        ll anstmp = 0;
        ll X = n[i] - a[i] - b[i] + 1;
        ll Y = n[i] - a[i] + 1;
        ll Z = n[i] - b[i] + 1;
        anstmp = (X * (X+1)) % mod;
        ll YY = (((2 * Y) % mod) * Z) % mod - (X * (X+1)) % mod;
        YY = (YY + mod) % mod;
        anstmp = (anstmp * YY) % mod;
        printf("%lld\n", anstmp);
    }
}

/*
@Author
   Sujeet Kushwaha
*/

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ll long long int
#define fast ios_base::sync_with_stdio(false)
#define fast_input cin.tie(NULL)
#define fast_output cout.tie(NULL)
#define vi vector<long long int>
#define vp vector<pair<long long int ,long long int>>
#define pb push_back
#define mp make_pair
#define pp pop_back
#define iter vector<int>::iterator
#define pa pair<long long int ,long long int>
#define f(a,b) for(int a=0;a<b;a++)
#define mod 1000000007
#define F first
#define S second
#define sett set<long long int>
#define um unordered_map<ll,ll>
#define ordered_set tree<pa, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// ll mul(ll a,ll b)
// {
// 	return (a%mod*b%mod)%mod;
// }
// ll add(ll a,ll b)
// {
// 	ll res=a+b;
// 	if(res>=mod)
// 	{
//       res-=mod;
// 	}
// 	return res;
// }
// ll sub(ll a,ll b)
// {
// 	ll res=a-b;
// 	if(res<0)
// 	 res+=mod;
// 	return res;
// }
// ll powmod(ll a, ll b)
// {
//     ll res = 1;
//     while (b) {
//         if (b&1) {
//             res =mul(res,a);
//         }
//         b >>= 1;
//         a=mul(a,a);
//     }

//     return res;
// }

// ll modinv(ll a)
// {
//     return powmod(a, mod - 2);
// }
//  unordered_map<ll,ll>facti;
//  ll fact(ll n)
//  {
// 	if(n==0)return 1;
// 	if(n<=1)return 1;
// 	if(facti[n]!=0)return facti[n];
// 	return facti[n]=mul(n,fact(n-1));
//  }
//   ll ncr(ll n,ll r)
//   {
//         ll h=fact(n);
// 	ll v=fact(r);
// 	ll j=fact(n-r);
// 	v=mul(v,j);
// 	v=powmod(v,mod-2);
// 	return mul(h,v);
//  }
// int fastread(){
//     int x=0;
//     int c = getchar_unlocked();
//     while(c<48)
//         c = getchar_unlocked();
//     while(c>=48){
//         x = x*10 + (c - 48);
//         c = getchar_unlocked();
//     }
//     return x;
// }
// bool comp(pa a,pa b)
// {
//     if(a.F==b.F)
//     {
//         return v[a.S]<v[b.S];
//     }
//     returna.S<b.S;
// }
vector<pa>g[1000001];
ll dp[1000001];
void dfs(ll v,ll par=-1)
{
        for(pa d:g[v])
        {
                if(d.first-par)
                {
                      dp[d.first]=dp[v]^d.second;
                      dfs(d.first,v);
                }
        }
}
void solve()
{
        ll n;
        cin>>n;
        f(i,n-1)
        {
             ll x,y,w;
             cin>>x>>y>>w;
             x--,y--;
             g[x].pb({y,w});
             g[y].pb({x,w});
        }
        dfs(0);
        ll ans=0;
        f(j,61)
        {
                ll ones,zeros;
                ones=zeros=0;
                f(i,n)
                {
                        if(dp[i]&(1ll<<j))
                        {
                                ones++;
                        }
                        else
                        {
                                zeros++;
                        }
                }
                zeros*=ones;
                zeros%=mod;
                ans+=((1ll<<j)%mod*zeros%mod);
                ans%=mod;
        }
        cout<<ans<<endl;
}
int main() {
	fast;
	fast_input;
	fast_output;
	// ll t;
	// cin>>t;
	// while(t--)
	// {
	  solve();
	// }
	return 0;
}


#include "bits/stdc++.h"
#define int long long
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = (1LL << 61);
ll mod = 998244353;
template <typename T>
struct BIT {
	int n;          // 配列の要素数(数列の要素数+1)
	vector<T> bit;  // データの格納先
	BIT(int n_) : n(n_ + 1), bit(n, 0) {}
	void add(int i, T x) {
		for (int idx = i; idx < n; idx += (idx & -idx)) {
			bit[idx] += x;
		}
	}
	T sum(int i) {
		T s(0);
		for (int idx = i; idx > 0; idx -= (idx & -idx)) {
			s += bit[idx];
		}
		return s;
	}
};
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int N; cin >> N;
	vector<int>A(N), B(N);
	for (int i = 0; i < N; i++)cin >> A[i];
	for (int i = 0; i < N; i++)cin >> B[i];
	map<int, vector<int>>mp;
	for (int i = N - 1; i >= 0; i--)mp[i + A[i]].push_back(i);
	vector<int>c(N);
	BIT<int>bit(N + 100);
	for (int i = 0; i < N; i++) {
		if (mp[i + B[i]].empty()) {
			cout << -1 << endl; return 0;
		}
		int idx = mp[i + B[i]].back();
		mp[i + B[i]].pop_back();
		c[i] = idx + 1;
	}
	int ans = 0;
	for (int i = 0; i < N; i++) {

		ans += i - bit.sum(c[i]);
		bit.add(c[i], 1);
	}
	cout << ans << endl;
	return 0;

}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()

int main(){
    ll n;cin >>n;
    vector<vector<ll>> c(n,vector<ll>(n));
    rep(i,n)rep(j,n)cin >>c[i][j];

    bool y_n=true;
    vector<ll> dc(n-1);
    rep(i,n-1)dc[i]=c[0][i+1]-c[0][i];
    rep(i,n-1)rep(j,n-1)if(dc[j]!=c[i+1][j+1]-c[i+1][j])y_n=false;
    if(!y_n){
        cout <<"No";
        return 0;
    }

    vector<ll> cn0(n);
    rep(i,n) cn0[i]=c[i][0];
    vector<ll> a=cn0;
    sort(all(a));
    rep(i,n){
        cn0[i]-=a[0];
        if(cn0[i]<0)y_n=false;
    }
    if(!y_n){
        cout <<"No";
        return 0;
    }
    cout <<"Yes" <<endl;

    ll num;
    rep(i,n)if(c[i][0]==a[0])num=i;
    rep(i,n){
        cout <<cn0[i];
        if(i!=n-1)cout <<" ";
    }
    cout <<endl;
    rep(i,n){
        cout <<c[num][i];
        if(i!=n-1)cout <<" ";
    }
}


#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, a[N], b[N], p[N], rp[N], id[N];
void gao(int x, int y)
{
	if(a[x]<=b[p[x]] || a[y]<=b[p[y]])
	{
		puts("-1");
		exit(0);
	}
	rp[p[y]] = x, rp[p[x]] = y;
	swap(p[x], p[y]);
}
int main()
{
	scanf("%d", &n);
	for(int i=1; i<=n; i++) scanf("%d", a+i);
	for(int i=1; i<=n; i++) scanf("%d", b+i);
	for(int i=1; i<=n; i++) scanf("%d", p+i), rp[p[i]] = i;
	iota(id+1, id+n+1, 1);
	sort(id+1, id+n+1, [&](int x, int y) { return a[x] < a[y]; });
	vector<pair<int, int>> seq;
	for(int i=1; i<=n; i++)
	{
		int j = id[i], k = rp[j];
		if(j==k) continue;
		if(a[k]>b[p[j]])
		{
			gao(j, k);
			seq.emplace_back(j, k);
		}
		else
		{
			gao(k, n);
			seq.emplace_back(k, n);
			gao(j, n);
			seq.emplace_back(j, n);
		}
	}
	for(int i=1; i<=n; i++) assert(i==p[i]);
	printf("%d\n", (int)seq.size());
	for(auto &it : seq) printf("%d %d\n", it.first, it.second);
	return 0;
}

#include<bits/stdc++.h>
#define int long long
#define rint regester int
const int maxn = 1e6 + 10;
const int INF = 1e9;
using std::ios;
using std::cin;
using std::cout;
using std::max;
using std::min;
using std::sort;
using std::unique;
using std::lower_bound;
using std::swap;
using std::abs;
using std::acos;
using std::queue;
using std::map;
using std::string;
int read(){int x = 0,f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){if(ch == '-')f = -1; ch = getchar();}while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar();} return x * f;}
int n, ispri[maxn], m, pri[maxn], cnt, a[maxn];
void work(int nm){
	for(int i = 2; i <= nm; i++){
		if(!ispri[i])pri[++cnt] = i;
		for(int j = 1; j <= cnt && i * pri[j] <= nm; j++)ispri[i * pri[j]] = 1;
	}
}
signed main(){
	n = read();
	work(n);
	for(int i = 1; i <= n; i++){
		int ii = i;
		for(int j = 1; j <= cnt && pri[j] <= ii; j++){
			int cc = 0;
			while(ii % pri[j] == 0 && ii){
				++cc, ii /= pri[j];
			}
			a[i] += cc;
		}
		++a[i];
	}
	for(int i = 1; i <= n; i++)cout << a[i] << " ";
}


#include<bits/stdc++.h>
using namespace std;

int main(){
	long long a,b;
	cin>>a>>b;
	float c = (float)((b/100.0)*a);
	int d = c;
	if(c == d){
		cout<<d;
	}else{
		cout<<c;
	}
	//cout<<(b/100)*a;
}

#include <bits/stdc++.h>
using namespace std;

int main(){
    double a,b;
    double x;

    cin >> a >> b;

    x = a*b/100;
    cout << x << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) rep(i,0,n)
void solve()
{
    int n,m;cin>>n>>m;
    int a[n],b[m],dp[n+1][m+1];
    for(auto &i:a)cin>>i;
    for(auto &i:b)cin>>i;
    REP(i,n+1)dp[i][0]=i;
    REP(i,m+1)dp[0][i]=i;
    rep(i,1,n+1)rep(j,1,m+1)dp[i][j]=min({dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+(a[i-1]==b[j-1]?0:1)});
    cout<<dp[n][m]<<endl;
}

int main()
{
    cin.tie(0);
	ios::sync_with_stdio(false);
    solve();
    return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

typedef long long ll;

int main() {
    int N, M;
    cin >> N >> M;
    vector<int> A(N), B(M);
    rep(i, N) cin >> A[i];
    rep(i, M) cin >> B[i];
    vector<vector<int>> dp(N, vector<int>(M, 0));
    rep(i, N) {
        rep(j, M) {
            if (i == 0 && j == 0) {
                if (A[i] == B[j]) dp[i][j] = 0;
                else dp[i][j] = 1;
                continue;
            }
            int x = 1e9, y = 1e9, z = 1e9;
            if (j > 0) {
                x = dp[i][j - 1] + 1;
                if (A[i] != B[j - 1]) x--;
                if (A[i] != B[j]) x++;
            }
            if (i > 0) {
                y = dp[i - 1][j] + 1;
                if (A[i - 1] != B[j]) y--;
                if (A[i] != B[j]) y++;
            }
            if (i > 0 && j > 0) {
                z = dp[i - 1][j - 1];
                if (A[i] != B[j]) z++;
            }
            dp[i][j] = min({x, y, z});
        }
    }
    /*
    rep(i, N) {
        rep(j, M) {
            cout << dp[i][j] << " ";
        }
        cout << "\n";
    }
    */
    int ans = 1e9;
    rep(i, N) {
        rep(j, M) {
            ans = min(ans, dp[i][j] + N - 1 - i + M - 1 - j);
        }
    }
    cout << ans << "\n";
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define fz(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define ffe(it,v) for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define fill0(a) memset(a,0,sizeof(a))
#define fill1(a) memset(a,-1,sizeof(a))
#define fillbig(a) memset(a,63,sizeof(a))
#define pb push_back
#define ppb pop_back
#define mp make_pair
typedef pair<int,int> pii;
typedef long long ll;
ll n;
int main(){
	scanf("%lld",&n);
	ll l=1,r=2e9,ans=0;
	while(l<=r){
		int mid=(l+r)>>1;
		if(1ll*mid*(mid+1)/2ll<=n+1) ans=mid,l=mid+1;
		else r=mid-1;
	}
	printf("%lld\n",n+1-ans);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef int64_t lint;
#define rep(i, n) for(lint i=0; i<n; i++)

int main() {
	lint N;
	cin >> N;
	lint a=0, b=0, c=N+1, x, y, z;
	rep(i, N) {
		c -= i+1;
		if (c >= 0) a++;
		else break;
	}
	std::cout << N+1-a << '\n';
}


#include <bits/stdc++.h>  // clang-format off
using namespace std;
using i64 = int64_t;
using pii = pair<i64, i64>;
#define rep(i, n) for (i64 i = 0; i < i64(n); i++)
#define per(i, n) for (i64 i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (i64 i = a; i < i64(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fi first
#define se second
#ifdef LOCAL
#define dbg(...) cerr<<__LINE__<<" ["<<#__VA_ARGS__<<"]:",debug(__VA_ARGS__)
#else
#define dbg(...)
#endif
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) {
    os << '(' << pa.first << ',' << pa.second << ')'; return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    for (auto& x : vec) { os << x << ' '; } return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &mat) {
    os << endl; for (auto& vec : mat) { os << vec << endl; } return os;
}
void debug() { cerr << endl; }
template <typename Head, typename... Tail> void debug(Head H, Tail... T) {
    cerr << " " << H; debug(T...);
}
template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = (1 << 30) - 1;
const i64 LINF = (1LL << 60) - 1;
// clang-format on

template <typename T>
vector<T> compress(vector<T>& vec) {
    auto ret = vec;
    sort(all(vec));
    vec.erase(unique(all(vec)), vec.end());
    for (T& a : ret) a = lower_bound(all(vec), a) - vec.begin();
    return ret;
}

void solve() {
    int n;
    cin >> n;
    vector<i64> A(n);
    rep(i, n) cin >> A[i];
    auto X = compress(A);
    vector<int> cnt(X.size());
    rep(i, n) {
        cnt[X[i]]++;
    }
    if (n % 2 == 0) {
        bool latter = 1;
        for (auto x : cnt) {
            latter &= x % 2 == 0;
        }
        cout << (latter ? "Second" : "First") << endl;
    }
    else {
        cout << "Second" << endl;
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <set>
#include <map>
#include <stack>
#include <queue>
#define ll long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define P pair<ll, ll>
using namespace std;

int main(){
  int n; cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll ans = 1e18;
  for(ll i = 0; i <= 1 << (n - 1); i++){
    ll x = 0, y = 0;
    vector<ll> v;
    rep(j, n){
      x |= a[j];
      if(j == n - 1) break;
      if(i >> j & 1){
        v.push_back(x);
        x = 0;
      }
    }
    v.push_back(x);
    for(auto& e: v){
      y ^= e;
    }
    ans = min(ans, y);
  }
  cout << ans << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

// DEBUG
#ifdef _DEBUG
#define debug(x) cout << #x << ": " << x << endl
#else
#define debug(x)
#endif
// iter
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i <= (int)(b); i++)
#define FORD(i, a, b) for (int i = a; i >= (int)(b); i--)
#define FORA(i, I) for (const auto& i : I)
// vec
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ((int)(x.size()))
// 定数
// 2.147483647×10^{9}:32bit整数のinf
#define INF32 2147483647
// 9.223372036854775807×10^{18}:64bit整数のinf
#define INF64 9223372036854775807
// 問題による
#define MOD 1000000007

int main() {
  // 小数の桁数の出力指定
  // cout << fixed << setprecision(10);
  // 入力の高速化用のコード
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int N;
  cin >> N;

  vector<vector<int>> C(N, vector<int>(N));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> C[i][j];
    }
  }

  bool exists = false;
  vector<int> A(N), B(N);

  REP(i, N) {
    bool can = true;
    REP(j, N) {
      if (i == j) {
        continue;
      }

      debug(i);
      debug(j);

      ll num = C[i][0] - C[j][0];
      debug(num);
      REP(k, N) {
        ll tmp = C[i][k] - C[j][k];
        debug(tmp);
        if (tmp != num) {
          can = false;
          break;
        }
      }
      if (!can) {
        break;
      }
    }

    debug(can);
    if (can) {
      B = C[i];
      bool allPlus = true;
      REP(a, N) {
        int tmp = C[a][0] - B[0];
        debug(C[a][0]);
        debug(tmp);
        if (tmp < 0) {
          allPlus = false;
          break;
        }
        A[a] = tmp;
      }

      if (allPlus) {
        exists = true;

        break;
      }
    }
  }

  if (exists) {
    cout << "Yes" << endl;
    REP(i, N) {
      cout << A[i];
      if (i < N - 1) {
        cout << " ";
      }
    }
    cout << endl;
    REP(i, N) {
      cout << B[i];
      if (i < N - 1) {
        cout << " ";
      }
    }
    cout << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll=long long;
using ld=long double;
//using mint = modint1000000007;
#define rep(i,n) for (ll i=0; i<n; ++i)
#define all(c) begin(c),end(c)
#define PI acos(-1)
#define oo 2e18
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
//priority_queue<ll, vector<ll>, greater<ll>> Q;
/*
250000
２ぶたん？
最初のAさえ決まればあとは全部？

*/
ll C[550][550];
int main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  ll N;
  cin >> N;
  ll low = 1e9;
  ll high = 0;
  rep(i, N) rep(j, N)cin >> C[i][j];
  rep(i, N) rep(j, N){
    chmin(low, C[i][j]);
    chmax(high, C[i][j]);
  }
  // lowより小さい数だけが候補
  // さが同じかどうか
  ll dif[550] = {};
  rep(i, N-1){
    dif[i] = C[i+1][0] - C[i][0];
    rep(j, N){
      if (C[i+1][j] - C[i][j] != dif[i]){
        cout << "No" << endl;
        return 0;
      }
    }
  }
  ll bmin=1e9;
  // Bは一番小さい数がある行
  ll B[550]={};
  rep(i, N) chmin(bmin, C[i][0]);
  rep(i, N){
    if (C[i][0] == bmin)
    {
      rep(j, N) B[j] = C[i][j];
      break;
    }
  }
  ll A[550] = {};
  rep(i, N){
    A[i] = C[i][0] - bmin;
  }
  cout << "Yes" << endl;
  rep(i, N){
    cout << A[i];
    if (i==N-1) cout << endl;
    else cout << " ";
  }
  rep(i, N){
    cout << B[i];
    if (i==N-1) cout << endl;
    else cout << " ";
  }
}


#include<bits/stdc++.h>
using namespace std;

#define ll  long long
#define ull  long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
const ll maxn =5e5+5;
const ll mod=998242361 ;
const ll base=1e18;
map<ll,vector<ll>> mp;
ll a[maxn];
ll b[maxn];
ll fwt[2*maxn];
ll n;
ll cnt;
void update(ll l, ll r, ll d) {
    for (int p = l; p <= cnt; p += p & -p) fwt[p] = (fwt[p] + d) ;
    ++r;
    for (int p = r; p <= cnt; p += p & -p) fwt[p] = (fwt[p] - d) ;
}
ll get(ll p) {
    ll ret = 0;
    for (; p; p -= p & -p) ret = (ret + fwt[p]) ;
    return ret;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    if (fopen("t.inp","r"))
    {
        freopen("test.inp","r",stdin);
        freopen("test.out","w",stdout);
    }

     cin>>n ;
     cnt=n;
     for (int i=1;i<=n;i++)
     {
         cin>>a[i];
         update(i,i,i);
     }
     for (int i=n;i>=1;i--) mp[a[i]+i-1].pb(i);
     for (int i=1;i<=n;i++)
     {
         cin>>b[i];
     }
     ll ans=0;
     for (int i=1;i<=n;i++)
     {
         if (mp[b[i]+i-1].size())
         {

              ans=(ans+abs(i-get(mp[b[i]+i-1].back())));
              update(1,mp[b[i]+i-1].back(),1);
              mp[b[i]+i-1].pop_back();

         }
         else
         {
             //cout <<i<<endl;
             cout <<-1;
             return 0;
         }
     }
     cout <<ans;

}

#include "bits/stdc++.h"
#include <chrono>
#include <random>
#include <ext/pb_ds/assoc_container.hpp>

#define INF 1000000007
#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++)

#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,fma,mmx,avx,tune=native")

using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef unordered_map<int,int> umi ;
typedef unordered_set<int> usi ;
typedef pair<int,int> pi;
typedef tree< pair<ll,ll>,null_type, less<pair<ll,ll>> ,rb_tree_tag,tree_order_statistics_node_update > indexed_set;
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

bool ldequal(ld  a ,ld b){
    return abs(a-b) < 1e-9  ;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// shuffle(permutation.begin(), permutation.end(), rng); for permute
// uniform_int_distribution<int>(0, i)(rng) for rand

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

const int N =2e5+1 ;
ll a[N] ,b[N] ;
indexed_set s1 ;
set<pair<ll,ll>> s2 ;

bool tests=false ;
void gen(){} ;

void solve(){
   int n ;
   cin>>n ;
   REP(i,0,n){
    cin>>a[i] ;
    s1.insert({i,a[i]+i}) ;
    s2.insert({a[i]+i,i}) ;
   }
   REP(i,0,n) cin>>b[i] ;
   ll res=0 ;
   REP(i,0,n){
      ll val =b[i]+i ;
      auto l = s2.lower_bound({val,0}) ;
      if(l==s2.end()){
         cout<<-1 ;
         return ;
      }
      pair<ll,ll> temp = MP((*l).S ,(*l).F) ;
      auto r =s1.lower_bound(temp)  ;
      debug(val,*l ,*r , s1.order_of_key(*r) ) ;
      res+=s1.order_of_key(*r) ;
      s2.erase(l) ;
      s1.erase(r) ;
   }
   cout<<res ;

}

int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    gen() ;
    long long test =1 ;
    if(tests) cin>>test ;
    REP(i,1,test+1){
        debug(i) ;
        //cout<<"Case #"<<i<<": " ;

        solve() ;
        cout<<"\n" ;
    }
    return 0 ;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

void solve(){
  ll N;
  cin>>N;

  int num=0;
  while(N%2==0){
    N/=2;
    num++;
  }

  if(num==0) cout<<"Odd"<<"\n";
  else if(num==1) cout<<"Same"<<"\n";
  else cout<<"Even"<<"\n";
}

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

  int T;
  cin>>T;
  rep(i,T) solve();

  return 0;
}

#include <bits/stdc++.h>
using namespace std;
//↓AC-library使うなら

//型関係
using Graph = vector<vector<int>>;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef priority_queue<int, vector<int>, greater<int>> PQ;//昇順priority_queue
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<P > vpii;
typedef vector<pll > vpll;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<ll> > vvll;
//repマクロ
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rrep(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
//範囲関係
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define ALL(x) (x).begin(), (x).end()
//定数
#define inf 1000000000000
#define mod 1000000007
#define MAXR 100000
#define MATHPI acos(-1)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = INT_MAX;
const ll LLINF = 1LL<<60;
//操作
#define PB push_back
#define PF push_front
#define MP make_pair
#define F first
#define S second
//数学関係, a<=x<=b
bool updown(ll x, ll a, ll b) { return x >= a && x <= b; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
#define SUM(x) accumulate(ALL(x), 0)
//chmin, chmax
template <class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;} return 0;}
template <class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;} return 0;}
//小数出力(f...出力したい小数, n...小数点以下の桁数)
void printd(double f, int n) { cout << fixed << setprecision(n) << f << endl; } //指定した桁で小数を出力
//YES/NO, Yes/No, yes/no出力
void YES(bool c = true) { cout << (c ? "YES" : "NO") << endl; } //YES(条件式)で記述
void Yes(bool c = true) { cout << (c ? "Yes" : "No") << endl; } //Yes(条件式)で記述
void yes(bool c = true) { cout << (c ? "yes" : "no") << endl; } //yes(条件式)で記述

//if use atcoder library:g++ main.cpp -std=c++17 -I /home/k0gane/ac-library

//累積和
//https://qiita.com/drken/items/56a6b68edef8fc605821
//point...n+1の容量のリストを作る
//
//深さ優先...push_front
//幅優先...push_backP

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t; cin >> t;
  rep(i, t){
    ll n; cin >> n;
    int cnt = 0;
    while(n % 2 == 0){
      cnt++; n = n / 2;
    }
    if(cnt == 1) cout << "Same" << endl;
    else if(cnt >= 2) cout << "Even" << endl;
    else cout << "Odd" << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;

/* short */
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) rbegin(v), rend(v)

/* REPmacro */
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORR(i, a, b) for(int i = (a); i >= (b); i--)
#define REP(i, n) for(int i = 0; i < (n); i++)
#define FOREACH(x, a) for(auto x : a)

/* exchange */
#define CHMIN(a, b) (a) = min((ll)(a), (ll)(b))
#define CHMAX(a, b) (a) = max((ll)(a), (ll)(b))

/* function */
#define IN(x) cin >> x
#define DEBUG(x) cerr << (x) << " "
#define LN() cerr << "\n"
#define PRINT(x) cout << (x) << endl
#define BR cout << endl

/* const */
const int ARRAY = 100005;
const int INF = 1001001001; // 10^9
const ll LINF = 1001001001001001001; // 10^18
const int MOD = 1e9 + 7;

ll N = 0;
ll K = 0;
ll M;
ll t;
ll ret = 0;
string s;
ll total = 0;

void input() {
  IN(N);
  IN(K);
  IN(M);
  REP(i, N-1) {
    IN(t);
    total += t;
  }
}

void solve() {
  ll goal = N * M;
  if (goal - total < 0) {
    PRINT(0);
  } else if (goal - total > K) {
    PRINT(-1);
  } else {
    PRINT(goal - total);
  }
}

int main(void){
  input();
  solve();
}

#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
const int INF = 1<<29;
typedef long long ll;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n>>b)&1; }
inline void set_bit(int & n, int b) { n |= two(b); }
inline void unset_bit(int & n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res;}

int main()
{
	lli n,k,m;
	cin>>n>>k>>m;
	lli avg=0;
	for(int i=1;i<n;i++)
	{lli x;
		cin>>x;
		avg+=x;}
	lli ans=n*m-avg;
	if(ans<=0)
		cout<<0;
	else if(ans<=k)
		cout<<ans;
	else
		cout<<-1;

}


#include <bits/stdc++.h>

using namespace std;
#define set_precision(ans,l) cout << fixed << setprecision(l)<<ans;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define vi vector<int>
#define vl vector<long long int>
#define Vi vector<vector<int>>
#define vpi vector<pair<int,int>>
#define seti set<int>
#define setl set<ll>
#define dseti set<int, greater<int>>
#define dsetl set<ll, greater<ll>>
#define mseti multiset<int>
#define msetl multiset<ll>
#define dmseti multiset<int, greater<int>>
#define dmsetl multiset<ll, greater<ll>>
#define sortA(arr) sort(arr.begin(), arr.end())
#define dsortA(arr) sort(arr.begin(), arr.end(), greater<ll>())
#define ssort(arr) stable_sort(arr.begin(), arr.end())
#define nth(v,n) nth_element(v.begin,v.begin+n-1,v.end())
#define dnth(v,n) nth_element(v.begin,v.begin+n-1,v.end(), greater<ll>())
#define init(a)  memset((a),0,sizeof(a))
#define pi pair<int,int>
#define pb push_back
#define pl pair<ll,ll>
#define ll long long
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mod 998244353
long double EPS = 1e-9;

/*struct comp {
  bool operator() (const int& lhs, const int& rhs) const
  {return lhs<rhs;}
};*/

ll cel(ll a,ll b){return((a-1)/b+1);}
ll gcd(ll a, ll b){
    if (a < b)swap(a, b);
    return (b == 0)? a: gcd(b, a % b);
}
ll MIN(ll a,int b){ll ans;(a>=b)?ans=b:ans=a;return ans;}
ll MAX(ll a,int b){ll ans;(a>=b)?ans=a:ans=b;return ans;}
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);}
ll po(ll x,ll y){
    ll ans=1;
    while(y){
        if(y&1){ans=(ans*x)%mod;}
        y>>=1;x=(x*x)%mod;
    }
    return ans;
}
ll fac(ll x){
    if(x==0)return 1;
    ll ans=1;
    rep(i,1,x+1){
        ans=(ans*i)%mod;
    }
    return ans%mod;
}
vi adj[51],adj1[51],vis(51,0),vis1(51,0);
void dfs(int u,ll& l){
    vis[u]=1;l++;
    for(int t:adj[u]){
        if(vis[t]){continue;}
        dfs(t,l);
    }
}
void dfs1(int u,ll& l){
    vis1[u]=1;l++;
    for(int t:adj1[u]){
        if(vis1[t]){continue;}
        dfs1(t,l);
    }
}
int main()
{
    FIO;
    ll n,t,x,y,m,s;
    cin>>n>>s;
    int A[n][n];x=0;y=0;
    rep(i,0,n){
        rep(j,0,n){
            cin>>A[i][j];
        }
    }
    rep(i,0,n){
        rep(j,0,n){
            if(j==i)continue;
            bool ok=true;
            rep(k,0,n){
                if(A[i][k]+A[j][k]>s){ok=false;break;}
            }
            if(ok){adj[i].pb(j);}
        }
    }
    rep(i,0,n){
        rep(j,0,n){
            if(j==i)continue;
            bool ok=true;
            rep(k,0,n){
                if(A[k][i]+A[k][j]>s){ok=false;break;}
            }
            if(ok){adj1[i].pb(j);}
        }
    }
    ll ans=1;
    rep(i,0,n){
        if(vis[i])continue;
        x=0;
        dfs(i,x);
        ans=(ans*fac(x))%mod;
        //cout<<i<<" "<<x<<" "<<ans<<"\n";
    }
    rep(i,0,n){
        if(vis1[i])continue;
        x=0;
        dfs1(i,x);
        ans=(ans*fac(x))%mod;
        //cout<<x<<" "<<ans<<"\n";
    }
    cout<<ans;
    return 0;
}

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

using namespace std;

class CARCWrecker2 {
public:
    void solve(std::istream& cin, std::ostream& cout) {
        int N;
        cin >> N;
        vector<int> A(N);
        REP(i, N) cin >> A[i];

        REP(i, N) {
            if (i % 2 == 1) A[i] *= -1;
        }

        int cur = 0;
        map<int, int> m;
        int res = 0;
        m[0]++;
        REP(i, N) {
            cur += A[i];
            res += m[cur];
            m[cur]++;
        }
        cout << res << endl;
    }
};

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    std::istream& in(std::cin);
    std::ostream& out(std::cout);

    CARCWrecker2 solver;
    solver.solve(in, out);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(void){
    int n;
    cin>>n;
    vector<long long> data(n);
    for(int i=0;i<n;i++){
        if(i==0){
            cin>>data.at(i);
            continue;
        }
        if(i%2==1){
            int a;
            cin>>a;
            data.at(i)=data.at(i-1)-a;
            continue;
        }
        if(i%2==0){
            int a;
            cin>>a;
            data.at(i)=data.at(i-1)+a;
            continue;
        }
    }

    map<long long,long long> ma;
    ma[0]=1;
    long long count=0;
    for(int i=0;i<n;i++){
        if(ma.count(data.at(i))){
            count+=ma.at(data.at(i));
            ma.at(data.at(i))++;
        }else{
            ma[data.at(i)]=1;
        }
    }
    cout<<count<<endl;
}

#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <algorithm>
#include <math.h>
#include <iomanip>
#include <map>
#include <sstream>
#define INF 1000000009
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using p = pair<int, int>;

int main() {
    ll n;
    cin >> n;
    ll ans = 0;
    vector<ll> a(n);
    map<ll,ll> x;
    rep(i,n) {
        ll an;
        cin >> an;
        if(i==0) a[i] = an;
        else a[i] = an - a[i-1];
        if(a[i]==0) ans++;
        if(i%2==0) x[a[i]]++;
        else x[-1*a[i]]++;
    }
    for (const auto &i : x){
        ans += (i.second*(i.second-1)) / 2;
    }

    cout << ans << endl;
    return 0;
}


//int a = s - '0'; 文字から数字
//小文字から大文字
//transform(a.begin(), a.end(), a.begin(), ::toupper);
//map 全探索
//auto begin = p.begin(), end = p.end();
//for (auto it = begin; it != end; it++) {}
//mapのキー：it->first mapのバリュー：it->second
//大文字判定 isupper(文字) 小文字判定 islower(文字)
//do{}while(next_permutation(ALL(配列)))
//小文字に対応する文字コード：S[i] - 'a'
//文字コード→小文字：(char)(数字+'a')
//グラフの距離:隣接行列で扱う
//bool型　初期値はTrue
//島渡りの問題：中間ノードに着目
//数が大きい時の比較はstring型で行う
//全て0になったか調べたい->0になるたびにcntする
//例外処理は最初にする
//x = p^m + q^n...の約数の個数:(n+1)*(m+1)....
//N!のどの素因数で何回割れるか
//⇔1~Nまでの数がそれぞれどの素因数で何回割り切れるかの和
//パズルの問題->一般化して全探索
//stack<ll> s;
//s.push(要素);s.top();s.pop();
//queue<ll> q;
//q.push(要素);q.front();q.pop();
//同じ作業繰り返す系の問題：収束先を見つける
//過半数：N/2.0で判定
//優先度付きキュー
//priority_queue<
//ll,
//vector<ll>
//> q;
#include <bits/stdc++.h>
#define rep(i,N) for(int i = 0; i < N;i++)
#define ALL(a) (a).begin(),(a).end()
#define ll long long int
#define PI 3.14159265358979323846264338327950L
using namespace std;

//割るやつ
const ll MOD = (pow(10, 9) + 7);

// K進数でのNの桁数
ll dig(ll N, ll K) {
	ll dig = 0;
	while (N) {
		dig++;
		N /= K;
	}
	return dig;
}
// a,bの最大公約数
ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a%b);
}

//a,bの最小公倍数
ll lcm(ll a, ll b) {
	ll g = gcd(a, b);
	return a / g * b;
}

//階乗計算
ll f(ll n) {
	if (n == 0 || n == 1) return 1;
	else return (n * f(n - 1));
}

//Nのd桁目の数
ll dignum(ll N, ll d) {
	ll x = pow(10, d);
	N %= x;
	ll y = pow(10, d - 1);
	N /= y;
	return N;
}

//Nをdで何回割れるか
ll divcnt(ll N, ll d) {
	ll ans = 0;
	while (1) {
		if (N%d == 0) {
			ans++;
			N /= d;
		}
		else break;
	}

	return ans;
}

//素数判定
bool prime(ll num) {
	if (num < 2) return false;
	else if (num == 2) return true;
	else if (num % 2 == 0) return false;
	double sqrtNum = sqrt(num);
	for (int i = 3; i <= sqrtNum; i += 2) {
		if (num % i == 0) return false;
	}
	return true;
}

//フィボナッチ数列
vector<ll> memo(pow(10, 6) + 1);
ll fibo(ll n) {
	if (n == 1) return 1;
	else if (n == 2) return 1;
	else if (memo[n] != 0) return memo[n];
	else return memo[n] = fibo(n - 1) + f(n - 2);
}

ll RS(ll N, ll P, ll M) {
	if (P == 0) return 1;
	if (P % 2 == 0) {
		ll t = RS(N, P / 2, M);
		return t * t % M;
	}
	return N * RS(N, P - 1, M);
}

vector<int> IntegerToVector(int bit, int N) {
    vector<int> S;
    for (int i = 0; i < N; ++i) {
        if (bit & (1 << i)) {
            S.push_back(i);
        }
    }
    return S;
}

int main() {
	ll N; cin >> N;
	unordered_set<ll> s;

	for(ll a = 2; a*a<=N; a++){
		ll x = a*a;
		while(x<=N){
			s.insert(x);
			x *= a;
		}
	}

	cout << N - s.size() << endl;
}

#pragma GCC target ("avx2")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include <unordered_set>
#include <unordered_map>
#include <random>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
constexpr ll MOD = 1'000'000'007LL; /*998'244'353LL;*/
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
template<class T> bool chmax(T &a, const T &b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b){ if(b<a){ a=b; return 1; } return 0; }
constexpr int dy[4]={-1,0,1,0};
constexpr int dx[4]={0,-1,0,1};

template<typename T>
struct SegmentTree{
private:
    int n, N;
    vector<T> node;
    function<T(T, T)> F;
    T E;
public:
    void init(int _n, function<T(T, T)> f, T e){
        F = f;
        E = e;
        n = _n;
        N = 1;
        while(N < n) N = (N<<1);
        node.assign(2*N-1, e);
    }
    void init(vector<T> v, function<T(T, T)> f, T e){
        F = f;
        E = e;
        n = v.size();
        N = 1;
        while(N < n) N = (N<<1);
        node.assign(2*N-1, e);
        for(int i=0; i<n; i++) node[N-1+i] = v[i];
        for(int i=N-2; i>=0; i--) node[i] = F(node[(i<<1)+1], node[(i<<1)+2]);
    }
    T& operator [](int a){
        return node[N-1+a];
    }
    void update(int a, T x){
        a += N-1;
        node[a] = x;
        while(a > 0){
            a = (a-1)>>1;
            node[a] = F(node[(a<<1)+1], node[(a<<1)+2]);
        }
    }
    T query(int a, int b, int k=0, int l=0, int r=-1){
        if(r == -1) r = N;
        if(b <= l || r <= a) return E;
        if(a <= l && r <= b) return node[k];
        return F(query(a, b, (k<<1)+1, l, (l+r)>>1), query(a, b, (k<<1)+2, (l+r)>>1, r));
    }
    int find_right(function<bool(T)> g, int a){
        if(!g(E)) return -1;
        T t = E;
        return min(find_right(g, a, 0, 0, N, t), n);
    }
    int find_right(function<bool(T)> g, int a, int k, int l, int r, T &t){
        if(r-l == 1){
            t = F(t, node[k]);
            return g(t) ? r : a;
        }
        int m = (l + r) >> 1;
        if(m <= a) return find_right(g, a, (k<<1)+2, m, r, t);
        if(a <= l && g(F(t, node[k]))){
            t = F(t, node[k]);
            return r;
        }
        int L = find_right(g, a, (k<<1)+1, l, m, t);
        if(L < m) return L;
        int R = find_right(g, a, (k<<1)+2, m, r, t);
        return max(L, R);
    }
    int find_left(function<bool(T)> g, int b){
        if(!g(E)) return n + 1;
        T t = E;
        return find_left(g, b, 0, 0, N, t);
    }
    int find_left(function<bool(T)> g, int b, int k, int l, int r, T t){
        if(r-l == 1){
            t = F(node[k], t);
            return g(t) ? l : b;
        }
        int m = (l + r) >> 1;
        if(b <= m) return find_left(g, b, (k<<1)+1, l, m, t);
        if(r <= b && g(F(node[k], t))){
            t = F(node[k], t);
            return l;
        }
        int R = find_left(g, b, (k<<1)+2, m, r, t);
        if(m < R) return R;
        int L = find_left(g, b, (k<<1)+1, l, m, t);
        return min(L, R);
    }
};

int N;
int a[300000];
SegmentTree<int> st;

signed main(){
    cin >> N;
    rep(i, N) cin >> a[i];
    st.init(N, [](int a, int b){ return a+b; }, 0);
    ll ans = 0;
    rep(i, N){
        ans += st.query(a[i], N);
        st.update(a[i], 1);
    }
    rep(i, N){
        cout << ans << endl;
        ans -= a[i];
        ans += N - 1 - a[i];
    }
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <limits>
#include <chrono>
#include <ctime>
#include <random>
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
using namespace std;
using namespace chrono;
template<class T>bool chmax(T &a, const T &b) { if(a < b){ a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if(a > b){ a = b; return 1; } return 0; }
template<class T> inline int  sz(T &a) { return a.size(); }
using ll = long long; using ld = long double;
using pi = pair<int,int>; using pl = pair<ll,ll>;
using vi = vector<int>; using vvi = vector<vi>;
using vl = vector<ll>; using vvl = vector<vl>;
const int inf = numeric_limits<int>::max();
const ll infll = numeric_limits<ll>::max();

int si,sj;
vvi t;
vvi p;

// そのマスを使ったか？
vvi flag_mass;
// そのタイル番号を使ったか？
vi flag_tile;

vi dx = {0, 1, 0, -1};
vi dy = {1, 0, -1, 0};
map<int,char> mp = {{0, 'R'}, {1, 'D'}, {2, 'L'}, {3, 'U'}};

void Input() {
  cin >> si >> sj;
  t.assign(50, vi(50));
  p.assign(50, vi(50));
  rep(i,50) {
    rep(j,50) {
      cin >> t[i][j];
    }
  }
  rep(i,50) {
    rep(j,50) {
      cin >> p[i][j];
    }
  }
}

string res;
string keiro;
int max_point = 0;
int point = 0;
auto startClock = system_clock::now();
int step = 0;
double END_TIME = 1.95;
vi junban = {0, 1, 2, 3};
int num;
double X = 1./8.;
int bot = 0;
int flag = 0;

void Dfs(int x, int y) {
  point += p[x][y];
  if(chmax(max_point, point)) {
    res = keiro;
    num = bot;
  }
  flag_tile[t[x][y]] = 1;
  step++;
  if(step % 1000 == 0) {
    double time = duration_cast<microseconds>(system_clock::now() - startClock).count() * 1e-6;
    if(time >= END_TIME * X) {
      if(flag) {
        cout << res << "\n";
        exit(0);
      }
      flag_tile.assign(2501, 0);
      keiro.clear();
      point = 0;
      X += 1./8.;
      bot++;
      if(bot == 4) {
        X = 1;
        bot = num;
        flag = 1;
      }
      Dfs(si, sj);
    }
  }
  // random_device seed_gen;
  // mt19937 engine(seed_gen());
  // shuffle(junban.begin(), junban.end(), engine);
  rep(i,4) {
    int j = (i+bot) % 4;
    int nx = x + dx[j], ny = y + dy[j];
    if(0 <= nx && nx < 50 && 0 <= ny && ny < 50) {
      int t_num = t[nx][ny];
      if(flag_tile[t_num]) continue;
      keiro.push_back(mp[j]);
      Dfs(nx, ny);
      keiro.pop_back();
    }
  }
  point -= p[x][y];
  flag_tile[t[x][y]] = 0;
}

void Solve() {
  flag_tile.assign(2501, 0);
  startClock = system_clock::now();
  Dfs(si, sj);
}

int main()
{
  Input();
  Solve();
  return 0;
}

/**
 * Dont raise your voice, improve your argument.
 * --Desmond Tutu
 */
#include <bits/stdc++.h>
using namespace std;

const bool ready = []() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(12);
    return true;
}
();

using ld=long double;
const ld PI = acos((ld)-1);
using ll= long long;
//#define int ll
#define all(v) (v).begin(), (v).end()
#define fori(n) for(int i=0; i<int(n); i++)

#define cini(i) int i; cin>>i;
#define cins(s) string s; cin>>s;
#define cind(d) ld d; cin>>d;
#define cinai(a,n) vi a(n); fori(n) { cin>>a[i]; }
#define cinas(s,n) vs s(n); fori(n) { cin>>s[i]; }
#define cinad(a,n) vd a(n); fori(n) { cin>>a[i]; }

using pii= pair<int, int>;
using pdd= pair<ld, ld>;
using vd= vector<ld>;
using vb= vector<bool>;
using vvb= vector<vb>;
using vi= vector<int>;
using vvi= vector<vi>;
using vs= vector<string>;

#define endl "\n"

const int N=50;
const vi di= { -1, 1, 0, 0};
const vi dj= { 0, 0, -1, 1};
const vector<char> dc= { 'U', 'D', 'L', 'R' };

vvb vis0(N, vb(N)); /* direct vis */
vvb vis1(N, vb(N)); /* also vis */
vvi t(N, vi(N));
vvi p(N, vi(N));

inline void alsoset(int i, int j) {
    for(int k=0; k<4; k++) {
        const int ii=i+di[k];
        const int jj=j+dj[k];
        if(ii>=0 && ii<N && jj>=0 && jj<N && t[i][j]==t[ii][jj]) {
            vis1[ii][jj]=vis0[i][j];
            break;
        }
    }
};

const int T=2e7;
int cnt=0;
ll ans=0;      /* best answer so far */
string apath;   /* path of best answer */

ll sum=0;      /* sum in current path */
string path;    /* current path */

void go(int i, int j) {
    if(cnt++>T)
        return;

    //assert(!vis0[i][j] && !vis1[i][j]);
    vis0[i][j]=true;
    alsoset(i,j);
    sum+=p[i][j];
    if(sum>ans) {
        ans=sum;
        apath=path;
    }

    using t3=tuple<int,int,int>;
    vector<t3> next;
    for(int d=0; d<4; d++) {
        const int ii=i+di[d];
        const int jj=j+dj[d];
        if(ii>=0 && ii<N && jj>=0 && jj<N && !vis0[ii][jj] && !vis1[ii][jj]) {
            next.emplace_back(ii,jj,d);
        }
    }

    /* Now sort the next steps by some criteria.
     * 1. Use a simple heuristic where we want to stay on
     * maximize the distance between the middle of the map and the
     * borders.
     */
    sort(all(next), [](t3 &p1, t3 &p2) {
        auto [p1x,p1y,p1d]=p1;
        auto [p2x,p2y,p2d]=p2;

        /* dist from center quad */
        const int dc1=abs(N/2-p1x)*abs(N/2-p1x)+abs(N/2-p1y)*abs(N/2-p1y);
        const int dc2=abs(N/2-p2x)*abs(N/2-p2x)+abs(N/2-p2y)*abs(N/2-p2y);

        /* dist from border quad */
        const int x1=min(p1x, N-p1x);
        const int y1=min(p1y, N-p1y);
        const int d1=min(dc1, min(x1,y1)*min(x1,y1));

        const int x2=min(p2x, N-p2x);
        const int y2=min(p2y, N-p2y);
        const int d2=min(dc2, min(x2,y2)*min(x2,y2));

        /* maximize both distances */
        return d1 > d2;
    });

    for(auto [iii,jjj,ddd] : next) {
        path+=dc[ddd];
        go(iii,jjj);
        path.pop_back();
    }

    vis0[i][j]=false;
    alsoset(i,j);
    sum-=p[i][j];
};

/* There is a more or less simple brute force,
 * first implement that.
 */
void solve() {
    cini(si);
    cini(sj);

    for(int i=0; i<N; i++)
        for(int j=0; j<N; j++)
            cin>>t[i][j];

    for(int i=0; i<N; i++)
        for(int j=0; j<N; j++)
            cin>>p[i][j];

    go(si,sj);

    cerr<<ans<<endl;
    cout<<apath<<endl;

}

signed main() {
    solve();
}


#include<bits/stdc++.h>
typedef long long ll;
//#define ll long long
#define int long long
//#define ull unsigned long long
//#define PI pair<int,int>
//#define PII pair<int,PI>
//#define PI pair<ll,int>
//#define endl "\n"
using namespace std;
const int maxm=2e3+5;

const int p=1e4;
int read(){
    string s;cin>>s;
    int t=4;
    int f=0;
    int ans=0;
    for(auto i:s){
        if(i=='.'){
            f=1;
            continue;
        }else{
            ans=ans*10+(i-'0');
            if(f)t--;
        }
    }
    while(t){
        ans=ans*10;
        t--;
    }
    return ans;
}
signed main(){
//    ios::sync_with_stdio(0);
    //
    int X=read();
    int Y=read();
    int R=read();
    //定义横坐标左右边界
    int lc=X-R;
    int rc=X+R;
    while(lc%p)lc++;
    while(rc%p)rc--;
    //定义上下界指针
    int up=Y,down=Y;
    while(up%p)up++;//取整
    while(down%p)down--;
    //
    int ans=0;
    for(int i=lc;i<=rc;i+=p){//枚举横坐标
        int ix=abs(X-i);
        //
        while(ix*ix+(up-Y)*(up-Y)>R*R)up-=p;//up下移
        while(up<=Y||ix*ix+(up-Y)*(up-Y)<=R*R)up+=p;//up上移
        //
        while(ix*ix+(Y-down)*(Y-down)>R*R)down+=p;//down上移
        while(down>=Y||ix*ix+(Y-down)*(Y-down)<=R*R)down-=p;//down下移
        //
        int add=(up-down)/p-1;
        if(add>0)ans+=add;
    }
    cout<<ans<<endl;
    return 0;
}
/*

*/

#pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

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

	int N, M;
	cin >> N >> M;

	int A[1001], B[1001];
	char C[1001];
	rep(i, M) {
		cin >> A[i] >> B[i] >> C[i];
		A[i]--;
		B[i]--;
	}
	vector<int> E[1000010];
	rep(i, M) rep(j, M) {
		if (C[i] != C[j]) continue;
		if (i == j) continue;

		E[A[i] * N + A[j]].pb(B[i] * N + B[j]);
		E[A[i] * N + B[j]].pb(B[i] * N + A[j]);
		E[B[i] * N + A[j]].pb(A[i] * N + B[j]);
		E[B[i] * N + B[j]].pb(A[i] * N + A[j]);
	}

	int D[1000100] = {};
	rep(i, N* N) D[i] = 1e9;

	queue<int> que;
	que.push(N - 1);
	D[N - 1] = 0;
	while (que.size()) {
		int u = que.front();
		que.pop();

		for (auto to : E[u]) {
			if (D[to] < 1e8) continue;
			chmin(D[to], D[u] + 1);
			que.push(to);
		}
	}

	//if (D[(N - 1) * N] > 1e8) co(-1);
	//else co(D[(N - 1) * N]);

	int kotae = 1e9;
	rep(i, N) chmin(kotae, D[i * N + i] * 2);
	rep(i, M) chmin(kotae, D[A[i] * N + B[i]] * 2 + 1);
	rep(i, M) chmin(kotae, D[B[i] * N + A[i]] * 2 + 1);

	if (kotae > 1e8) co(-1);
	else co(kotae);

	Would you please return 0;
}


#include <bits/stdc++.h>
using namespace std;

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define fi first
#define se second
#define pb push_back
#define mod(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,a,b) for(int i = a; i < b; i++)
#define forr(i,a,b) for(int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const ll mod = 1e9+7;
int sum(int a, int b){return (a+b) % mod;}
int sub(int a, int b){return (a + mod - b) % mod;}
int mul(int a, int b){return (1ll * a * b) % mod;}
int power(int a,int b){
	int res = 1;
	while(b){
		if(b&1)res = mul(res,a);
		b >>= 1;
		a = mul(a,a);
	}
	return res;
}

const int maxn = 400000+10;
vi graph[maxn];
int N,M,seen[maxn];

int dfs(int u,int p,int &ciclo){
	int res = 1;
	seen[u] = 1;
	int cnt = 0;
	for(auto &v : graph[u]){
		if(v == u)ciclo = 1;
		if(v == p){
			cnt++;
			continue;
		}
		if(!seen[v]){
			res += dfs(v,u,ciclo);
		}else{
			ciclo = 1;
		}
	}
	if(cnt > 1){
		ciclo = 1;
	}
	return res;
}
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N;
	forn(i,0,N){
		int u,v; cin >> u >> v; u--; v--;
		graph[u].pb(v);
		graph[v].pb(u);
	}
	int res = 0;
	forn(i,0,maxn){
		if(seen[i])continue;
		int ciclo = 0;
		int cnt = dfs(i,-1,ciclo);
		if(ciclo) res += cnt;
		else res += cnt-1;

	}
	cout << res << '\n';
	return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/

#include <bits/stdc++.h>

#define debug(var)  do{std::cout << #var << " :";std::cout << std::endl;view(var);}while(0)
template<typename T> void view(T e){ std::cout << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){ int c = v.size(); std::cout << "{"; for(const auto& e : v){ std::cout << e; if (--c) std::cout << ", "; } std::cout << "}" << std::endl; }
template<typename T, typename U> void view(const std::pair<T, U>& v){ std::cout << v.first << ", " << v.second << std::endl; }
template<typename T> void view(const std::set<T>& v){ int c = v.size(); std::cout << "{"; for(const auto& e : v){ std::cout << e; if (--c) std::cout << ", "; } std::cout << "}" << std::endl; }
template<typename T, typename U> void view(const std::map<T, U>& v){ std::cout << "{" << std::endl; for(const std::pair<T, U>& e : v){ std::cout << e.first << ", " << e.second << std::endl; } std::cout << "}" << std::endl; }
template<typename T> void view(const std::vector<std::vector<T> >& vv){ std::cout << "{" << std::endl; for(const auto& v : vv){ view(v); } std::cout << "}" << std::endl; }

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> pii;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long INF = 1LL << 60;
// const long long MOD = 1000000007;
const double PI = acos(-1);

const long long MOD = 998244353;

int H, W;
vector<string> field;

bool is_valid_move(int h, int w) {
    if (h < 0 || h >= H || w < 0 || w >= W) return false;
    return true;
}

int main() {
    cin >> H >> W;
    field.resize(H);
    rep(i, H) cin >> field[i];

    ll res = 1;

    bool flag = false;

    for (int h = 1; h < H; h++) {
        int tmp = h;
        int w = 0;
        int p;
        if (field[h][w] == '.') p = 0;
        else if (field[h][w] == 'R') p = 1;
        else p = 2;

        while (is_valid_move(tmp - 1, w + 1)) {
            tmp--; w++;
            if (field[tmp][w] == 'B') {
                if (p == 1) {
                    res = 0;
                    flag = true;
                    break;
                }
                p = 2;
            }

            if (field[tmp][w] == 'R') {
                if (p == 2) {
                    res = 0;
                    flag = true;
                    break;
                }
                p = 1;
            }
        }

        if (flag) break;

        if (p == 0) {
            res *= 2;
            res %= MOD;
        }
    }

    for (int w = 1; w < W - 1; w++) {
        int tmp = w;
        int h = H - 1;
        int p;
        if (field[h][w] == '.') p = 0;
        else if (field[h][w] == 'R') p = 1;
        else p = 2;

        while (is_valid_move(h - 1, tmp + 1)) {
            tmp++; h--;
            if (field[h][tmp] == 'B') {
                if (p == 1) {
                    res = 0;
                    flag = true;
                    break;
                }
                p = 2;
            }

            if (field[h][tmp] == 'R') {
                if (p == 2) {
                    res = 0;
                    flag = true;
                    break;
                }
                p = 1;
            }
        }

        if (flag) break;

        if (p == 0) {
            res *= 2;
            res %= MOD;
        }
    }

    if (field[0][0] == '.') {
        res *= 2;
        res %= MOD;
    }

    if (field[H - 1][W - 1] == '.') {
        res *= 2;
        res %= MOD;
    }

    cout << res << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;
typedef pair<int,int> pi;
typedef pair<lld,lld> pl;
typedef pair<int,lld> pil;
typedef pair<lld,int> pli;
typedef vector<int> vit;
typedef vector<vit> vitt;
typedef vector<lld> vlt;
typedef vector<vlt> vltt;
typedef vector<pi> vpit;
typedef vector<vpit> vpitt;
typedef long double ld;
#define x first
#define y second
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
#define mk(a,b) make_pair(a,b)
bool isrange(int y,int x,int n,int m){
	 if(0<=y&&y<n&&0<=x&&x<m) return true;
	 return false;
}
int dy[4] = {1,0,-1,0},dx[4]={0,1,0,-1},ddy[8] = {1,0,-1,0,1,1,-1,-1},ddx[8] = {0,1,0,-1,1,-1,1,-1};

const int MAX = 5050;
const lld mod = 998244353;
lld dp[12][MAX],fact[MAX],nCr[MAX][MAX];
void solve(int tc){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int e=0;e<MAX;e++){
		nCr[e][0] = nCr[e][e] = 1;
	}
	for(int e=2;e<MAX;e++){
		for(int p=1;p<e;p++){
			nCr[e][p] = (nCr[e-1][p]+nCr[e-1][p-1])%mod;
		}
	}
	lld ans = 0;
	for(int e=0;e<=m;e+=2) dp[0][e] = nCr[n][e];
	for(int e=1;e<12;e++){
		for(int p=0;p<=m;p+=(1<<e)*2){
			for(int q=0;q<=m-p;q++){
				dp[e][q+p] = (dp[e][q+p]+dp[e-1][q]*nCr[n][p/(1<<e)])%mod;
			}
		}
	}
	printf("%lld\n",dp[11][m]);
}

int main(void){
	/*
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	*/
	int tc = 1;
	/*
		cin >> tc;
	*/
	for(int test_number=1;test_number<=tc;test_number++){
		solve(test_number);
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define co(n) cout<<n<<endl;
const double eps=1e-10,pi=3.1415926535898;
const int mod=998244353,maxn=5e3+10;
int T,n,m,k,x,y;
long long dp[maxn][15],vis[maxn][15];

long long fac[maxn]={1,1},inv[maxn]={1,1},fac_inv[maxn]={1,1};

void init() {
	for (int i=2;i<maxn;++i) {
		fac[i] = i*fac[i-1]%mod;
		inv[i] = (mod-mod/i)*inv[mod%i]%mod;
		fac_inv[i] = fac_inv[i-1]*inv[i]%mod;
	}
}

long long C(int n, int m) {
	if (n < m or m < 0) return 0;
	return fac[n]*fac_inv[n-m]%mod*fac_inv[m]%mod;
}

long long dfs(int x, int y) {
	if (vis[x][y]) {
		return dp[x][y];
	}
	vis[x][y] = 1;
	if (y == 0) {
		return dp[x][y] = C(n, x);
	}
	long long cnt=0;
	int z=1<<y;
	for (int i=0;i*z<=x and i<=n;i+=2) {
		cnt += C(n, i)*dfs(x-i*z, y-1)%mod;
	}
	return dp[x][y]=cnt%mod;
}

int main(int argc, char const *argv[]) {
	init();
	scanf("%d %d", &n, &m);
	if (m&1) {
		printf("0\n");
		return 0;
	}
	dfs(m, 12);
	printf("%lld\n", dp[m][12]);
	return 0;
}

/*
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
clock_t clk;
clk = clock();
if(clock() - clk > CLOCKS_PER_SEC * 0.9)
      T
W     L
A C M E R
  E L   E
    E
*/


// Template
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iomanip>
#include <tuple>
#include <utility>
#include <queue>
#include <set>
#include <map>
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll infll = 3003003003003003003LL;
template <typename T>
inline bool chmin(T &x, const T &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T>
inline bool chmax(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
    for (T &element : vec) is >> element;
    return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
    for (int i = 0, vec_len = (int)vec.size(); i < vec_len; ++i) {
        os << vec[i] << (i + 1 == vec_len ? "" : " ");
    }
    return os;
}
struct IOSET {
    IOSET() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(10);
    }
} ioset;

// Main
pair<int, int> di(const vector<vector<int>> &g) {
    int u = 0, v = 0;
    {
        vector<int> d1(g.size(), -1);
        queue<int> q;
        q.push(0);
        d1[0] = 0;
        while (!q.empty()) {
            int x = q.front();
            q.pop();
            for (int y : g[x]) {
                if (d1[y] == -1) {
                    d1[y] = d1[x] + 1;
                    q.push(y);
                }
            }
        }
        int mx = 0;
        rep(i, g.size()) if (chmax(mx, d1[i])) u = i;
    }
    {
        vector<int> d1(g.size(), -1);
        queue<int> q;
        q.push(u);
        d1[u] = 0;
        while (!q.empty()) {
            int x = q.front();
            q.pop();
            for (int y : g[x]) {
                if (d1[y] == -1) {
                    d1[y] = d1[x] + 1;
                    q.push(y);
                }
            }
        }
        int mx = 0;
        rep(i, g.size()) if (chmax(mx, d1[i])) v = i;
    }
    return {u, v};
}
int main() {
    int n;
    cin >> n;
    vector<vector<int>> g(n);
    rep(i, n - 1) {
        int a, b;
        cin >> a >> b;
        --a;
        --b;
        g[a].push_back(b);
        g[b].push_back(a);
    }

    auto [u, v] = di(g);

    vector<int> mark(n, 0);
    vector<int> prv(n, -1);
    {
        queue<int> q;
        q.push(u);
        while (!q.empty()) {
            int x = q.front();
            q.pop();
            for (int y : g[x]) {
                if (y != prv[x]) {
                    prv[y] = x;
                    q.push(y);
                }
            }
        }
        int cur = v;
        mark[cur] = 1;
        while (cur != u) {
            cur = prv[cur];
            mark[cur] = 1;
        }
    }

    vector<int> ans(n);
    {
        int cur = 1;
        auto dfs = [&](auto &&dfs, int x) -> void {
            ans[x] = cur;
            for (int y : g[x]) {
                if (y != prv[x] && !mark[y]) {
                    ++cur;
                    dfs(dfs, y);
                    ++cur;
                }
            }
            for (int y : g[x]) {
                if (y != prv[x] && mark[y]) {
                    ++cur;
                    dfs(dfs, y);
                    ++cur;
                }
            }
        };
        dfs(dfs, u);
    }
    cout << ans << "\n";
}

#include <bits/stdc++.h>
#define fi first
#define se second
#define sz(v) ((int)v.size())
#define all(v) (v).begin(), (v).end()
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
int n;
int S,E;
int ans[200010],cur;
bool chk[200010];
vector<int> g[200010];
pii dfs(int p,int u) {
	pii r={0,u};
	for(auto& v:g[u]) {
		if(v==p)continue;
		pii rr=dfs(u,v);
		r=max(r,{rr.fi+1,rr.se});
	}
	return r;
}
bool F(int p,int u) {
	chk[u]=true;
	if(u==E)return true;
	for(auto& v:g[u]) {
		if(v==p)continue;
		if(F(u,v))return true;
	}
	chk[u]=false;
	return false;
}
void G(int p,int u) {
	ans[u]=++cur;
	for(auto& v:g[u]) {
		if(v==p||chk[v])continue;
		G(u,v);
		++cur;
	}
	for(auto& v:g[u]) {
		if(v==p)continue;
		if(chk[v]) {
			G(u,v);
			++cur;
		}
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int i,j;
	cin>>n;
	for(i=0;i<n-1;i++) {
		int u,v;cin>>u>>v;
		g[u].pb(v);
		g[v].pb(u);
	}
	S=dfs(1,1).se;
	E=dfs(S,S).se;
	F(S,S);
	G(S,S);
	for(i=1;i<=n;i++) {
		cout<<ans[i]<<' ';
	}
	return 0;
}


#include <bits/stdc++.h>
#define REP(i, N) for (int i = 0; i < (int)N; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
#define ALL(x) (x).begin(), (x).end()

using namespace std;

constexpr int inf = 1 << 30;
constexpr long long llinf = 1LL << 62;
constexpr int mod = 1000000007;  // 998244353;
constexpr int dy[4] = {-1, 0, 1, 0}, dx[4] = {0, -1, 0, 1};

using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

inline int popcount(ll x) { return __builtin_popcountll(x); }
inline int div2num(ll x) { return __builtin_ctzll(x); }
inline bool bit(ll x, int b) { return (x >> b) & 1; }

template <class T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

namespace FastIO {
struct Pre {
  char ptr[10000 * 4];
  int rd[1 << 16];
  int tenpow[4];
  constexpr Pre() : ptr(), rd(), tenpow() {
    for (int i = 0; i < 10000; ++i) {
      int j = i;
      int temp = 0;
      for (int t = 3; t >= 0; --t) {
        ptr[i * 4 + t] = j % 10 + '0';
        temp += (1 << ((3 - t) * 4)) * (j % 10);
        j /= 10;
      }
      rd[temp] = i;
    }
    tenpow[0] = 1;
    tenpow[1] = 10;
    tenpow[2] = 100;
    tenpow[3] = 1000;
  }
} constexpr ptr;
static constexpr size_t buf_size = 1 << 20;
char buf_in[buf_size], buf_out[buf_size];
size_t pt_in = 0, pt_out = 0, tail_in = 0;
inline size_t num_digits(long long x) {
  if (x >= (long long)1e9) {
    if (x >= (long long)1e18) return 19;
    if (x >= (long long)1e17) return 18;
    if (x >= (long long)1e16) return 17;
    if (x >= (long long)1e15) return 16;
    if (x >= (long long)1e14) return 15;
    if (x >= (long long)1e13) return 14;
    if (x >= (long long)1e12) return 13;
    if (x >= (long long)1e11) return 12;
    if (x >= (long long)1e10) return 11;
    return 10;
  } else {
    if (x >= (long long)1e8) return 9;
    if (x >= (long long)1e7) return 8;
    if (x >= (long long)1e6) return 7;
    if (x >= (long long)1e5) return 6;
    if (x >= (long long)1e4) return 5;
    if (x >= (long long)1e3) return 4;
    if (x >= (long long)1e2) return 3;
    if (x >= (long long)1e1) return 2;
    return 1;
  }
}
inline void load() {
  memcpy(buf_in, buf_in + pt_in, tail_in - pt_in);
  size_t width = tail_in - pt_in;
  tail_in = width + fread(buf_in + width, 1, buf_size - width, stdin);
  pt_in = 0;
}
inline void flush() {
  fwrite(buf_out, 1, pt_out, stdout);
  pt_out = 0;
}
inline void scan(char& c) { c = buf_in[pt_in++]; }
template <class T>
inline void scan(T& x) {
  if (pt_in + 32 > tail_in) load();
  char c;
  bool minus = 0;
  x = 0;
  scan(c);
  if (c == '-') {
    minus = 1;
    scan(c);
  }
  while (c >= '0') {
    x = x * 10 + (c & 15);
    scan(c);
  }
  if (minus) x = -x;
}
inline void print(char c) { buf_out[pt_out++] = c; }
template <class T>
inline void print(T x) {
  if (pt_out > buf_size - 32) flush();
  if (x < 0) {
    print('-');
    x = -x;
  }
  size_t digits = num_digits(x);
  int i;
  for (i = pt_out + digits - 4; i > (int)pt_out; i -= 4) {
    memcpy(buf_out + i, ptr.ptr + (x % 10000) * 4, 4);
    x /= 10000;
  }
  memcpy(buf_out + pt_out, ptr.ptr + x * 4 + (pt_out - i), 4 + i - pt_out);
  pt_out += digits;
}
template <class T>
inline void println(T x) {
  print(x);
  print('\n');
}
struct SetUp {
  SetUp() { load(); }
  ~SetUp() { flush(); }
} setup;
}  // namespace FastIO

using FastIO::print;
using FastIO::println;
using FastIO::scan;

constexpr int Nmax = 200001;
ll A[Nmax], acc[Nmax], maxs[Nmax];

int main() {
  int N;
  scan(N);
  REP(i, N) scan(A[i]);
  acc[0] = A[0];
  maxs[0] = max(0LL, A[0]);
  FOR(i, 1, N) {
    acc[i] = acc[i - 1] + A[i];
    maxs[i] = max(maxs[i - 1], acc[i]);
  }
  ll ans = 0, t = 0;
  REP(i, N) {
    chmax(ans, t + maxs[i]);
    t += acc[i];
  }
  println(ans);
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main() {
        int64_t N; cin >> N;
        vector<int64_t> A(N), F(N, 0);
        int64_t curPos = 0, curMoves = 0, maxPos = 0;
        for (int64_t i = 0; i < N; ++i) {
            cin >> A[i];
            curMoves += A[i];
            F[i] = max((int64_t)0, curMoves);
            if (i) F[i] = max(F[i], F[i - 1]);
            maxPos = max(maxPos, curPos + F[i]);
            if (i) A[i] += A[i - 1];
            curPos += A[i];
        }
        cout << maxPos << endl;
    return 0;
}


/*
Author : Chandan Agrawal
College : Poornima College of Engg. jaipur, Raj
Mail : chandanagrawal23@gmail.com

" when you are not practicing someone else is ,
 and the day u meet them u will lose "

*/
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;

#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define MAX 10000050

#define ll long long
#define ld long double
#define lli long long int

#define pb push_back
#define INF 1000000000000
#define mod 1000000007

// trignometric function always give value in Radians only
#define PI acos(-1) //3.1415926535897932384626433832795028
#define dsin(degree) sin(degree*(PI/180.0))
#define dcos(degree) cos(degree*(PI/180.0))
#define dtan(degree) tan(degree*(PI/180.0))

#define rsin(radian) sin(radian)
#define rcos(radian) cos(radian)
#define rtan(radian) tan(radian)

#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define memf(a) memset(a,false,sizeof(a))

#define loop(i,n)  for (lli i = 0; i < n; i++)
#define FOR(i,a,b) for (lli i = a; i < b; i++)

#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define makeuniq(v) v.resize(unique(all(v)) - v.begin()); //only uniq element in vector after this
#define sz(x) int(x.size())
#define F first
#define S second

#define mii map<lli,lli>

#define pii pair<lli,lli>

#define vi vector<lli>
#define vvi vector<vi>
#define vpi vector<pii>
#define vbool vector<bool>

#define seti set<lli>

#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) (a/gcd(a,b))*b
#define abs(x) ((x < 0)?-(x):x)

#define endl '\n'

template <typename Head>
void print(Head&& head)
{
    cout<<head<<endl;
}
template <typename Head, typename... Tail>
void print(Head&& head, Tail... tail)
{
    cout<<head<<" ";
    print(tail...);
}

#define scanarr(a,n) for(lli i=0;i<n;i++)    cin>>a[i];
#define scanvec(a,n) for(lli i=0;i<n;i++){ lli x ; cin>>x; a.pb(x);}

#define printarr(a,n) for(lli i=0;i<n;i++)   cout<<a[i]<<" "; cout<<endl;
#define printvec(vec) for(auto xt : vec) cout<<xt<<" ";    cout<<"\n";

#define FD(N) fixed<<setprecision(N)

#define deb(x) cout<<#x<<" "<<x<<endl;

/*
1D vector -  vi dp(n,value);
2D vector -  vvi dp(n,vi(n,value));
*/

// chandan1,2
void chandan1(){int y=1;return;}
void chandan2(){
        loop(i,10){
        lli x=1;
    }
    return(chandan1());
}

//---------------------------------------------------BIT----------------------------------------------------------------------

lli BIT[MAX], a[MAX], n;
void update(lli x , lli val)
{
    while(x<MAX)
    {
        BIT[x] += val;
        x += (x&-x);
    }
}

lli query(lli x)
{
     lli sum = 0;
     while(x>0)
     {
        sum += BIT[x];
        x -= (x&-x);
     }

     return sum;
}

//--------------------------------------------------BIT------------------------------------------------------------------------

int main(){
fastio
lli t=1;
//cin>>t;
chandan2();
while(t--) {
    cin>>n;
    lli maxi = -1;
    FOR(i,1,n+1)
    {
        cin>>a[i];
        a[i]++;
        maxi = max(maxi ,a[i]);
    }
    lli cnt =0;
    FOR(i , 1, n+1)
    {
        cnt += (query(MAX)-query(a[i]));
        update(a[i],1);
    }

    FOR(i,1,n+1)
    {
        print(cnt);
        cnt = cnt - (a[i]-1) + (n-a[i]);
        /*
        after shifing a element K from front to back
        how many inversion pairs we loose ? K-1 as there are K-1 values less than K
        and how mnany new inversion pair get ? (N-K) as there are N-K values greater than K
        */
    }
    mem0(BIT);
  }
return 0;
}

#include <bits/stdc++.h>
using namespace std;

const int N = 200000 + 9;

int a[N], b[N], p[N], sk[N], top;

bool vis[N];

inline bool check(int j){
    return !(a[j] <= b[p[j]] && p[j] != j);
}

int main(){
    int n; scanf("%d", &n);
    for (int i = 1; i <= n; ++i) scanf("%d", a + i);
    for (int i = 1; i <= n; ++i) scanf("%d", b + i);
    for (int i = 1; i <= n; ++i) scanf("%d", p + i);
    bool f = true;
    vector<pair<int, int>> ans;
    for (int i = 1; i <= n; ++i){
        if (!vis[i]){
            int j = i;
            if (!check(j)){ f = false; break; }
            sk[++top] = j;
            vis[j] = true;
            while (!vis[p[j]]){
                j = p[j];
                if (!check(j)){ f = false; break; }
                if (a[j] <= a[sk[top]]){
                    ans.push_back({j, sk[top]});
                    p[sk[top]] = p[j];
                }
                else {
                    sk[++top] = j;
                }
                vis[j] = true;
            }
            if (!f) break;
            for (j = 1; j < top; ++j){
                ans.push_back({sk[j], sk[top]});
            }
            top = 0;
        }
    }
    if (f){
        printf("%d\n", (int)ans.size());
        for (auto it : ans){
            printf("%d %d\n", it.first, it.second);
        }
    }
    else {
        printf("-1\n");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define M 998244353
#define N 200000
typedef long long int LL;
LL fact[301],invfact[301],sum[301],sum2[301];
LL ap[N][301],p2[301];
LL mult(LL a, LL b){
    return a*b%M;
}
LL add(LL a, LL b){
    return (a+b+M)%M;
}
LL c(LL n, LL k){
    return mult(fact[n],mult(invfact[k],invfact[n-k]));
}
LL p(LL b, LL x){
    if(x==0) return 1LL;
    LL sub=p(b,x/2);
    sub=mult(sub,sub);
    if(x&1) sub=mult(sub,b);
    return sub;
}
int main(){
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif
    int n,k;
    cin>>n>>k;
    int a[n];
    for(LL i=0;i<n;i++){
        scanf("%d",a+i);
    }
    for(LL i=0;i<n;i++){
        ap[i][0]=1;
    }
    for(LL i=0;i<n;i++){
        for(LL j=1;j<=k;j++){
            ap[i][j]=mult(ap[i][j-1],a[i]);
        }
    }
    fact[0]=invfact[0]=1;
    for(LL i=1;i<301;i++){
        fact[i]=mult(fact[i-1],i),invfact[i]=p(fact[i],M-2);
    }
    p2[0]=1;
    for(LL i=1;i<301;i++){
        p2[i]=mult(2,p2[i-1]);
    }
    for(LL i=0;i<n;i++){
        for(LL j=0;j<=k;j++){
            sum[j]=add(sum[j],ap[i][j]);
        }
    }
    LL tinv=p(2,M-2);
    for(LL X=1;X<=k;X++){
        LL ans=0;
        for(LL x=0;x<=X;x++){
            ans=add(ans,mult(c(X,x),mult(sum[X-x],sum[x])));
        }
        ans=add(ans,-mult(p2[X],sum[X]));
        ans=mult(ans,tinv);
        cout<<ans<<"\n";
    }
}

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

#define rep(i,n) for (long long i = 0; i < (n); ++i)
#define DIV 998244353
#define INF LONG_MAX/3
#define bit(n) (1LL<<(n))

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

ll memo1[305];
ll memo2[305];

long long kaidan[10000000];

void kaical(long long n){
	kaidan[0] = 1;
	for(long long i = 1; i < n; i++){
		kaidan[i] = kaidan[i-1] * i;
		kaidan[i] %= DIV;
	}
}

long long modpow(long long ori, long long po){
	long long res = 1;
	while(po > 0){
		if(po&1){
			res *= ori;
			res %= DIV;
		}
		ori *= ori;
		ori %= DIV;
		po >>= 1;
	}
	return res;
}

int main(){
	ll N, K;
	cin >> N >> K;
	vector<ll> A(N);
	rep(i, N) scanf("%lld", &A[i]);

	kaical(K+5);

	for(ll k = 0; k <= K; k++) {
		ll cur = 0;
		ll kai = modpow(kaidan[k], DIV-2);
		rep(i, N) {
			ll tmp = modpow(A[i], k);
			tmp *= kai;
			tmp %= DIV;
			cur += tmp;
			cur %= DIV;
		}
		memo1[k] = cur;

		cur = 0;
		rep(i, N) {
			cur -= modpow(2*A[i], k);
			cur += DIV;
			cur %= DIV;
		}
		memo2[k] = cur;
	}

	ll mm = modpow(2, DIV-2);
	for(ll X = 1; X <= K; X++) {
		ll ans = kaidan[X];

		//1こうめ
		ll tmp = 0;
		for(ll i = 0; i <= X; i++) {
			tmp += memo1[i] * memo1[X-i];
			tmp %= DIV;
		}
		ans *= tmp;
		ans %= DIV;

		ans += memo2[X];
		ans %= DIV;

		ans *= mm;
		ans %= DIV;
		cout << ans << endl;
	}
}


// Pratiyush Mishra

#include <bits/stdc++.h>
#define ull unsigned long long int
#define ll long long int
#define LL_MAX 9223372036854775807
#define pb push_back
#define pf push_front
#define mp make_pair
#define popb pop_back
#define vl vector<ll>
#define pl pair<ll,ll>
#define bs(v, x) binary_search(v.begin(), v.end(), x)
#define mem1(a) memset(a, -1, sizeof(a))
#define popf pop_front
#define p0(x) cout << x << " "
#define p1(x) cout << x << '\n'
#define p2(x, y) cout << x << " " << y << '\n'
#define p3(x, y, z) cout << x << " " << y << " " << z << '\n'
#define printv(v)                   \
  for (ll i = 0; i < v.size(); ++i) \
    cout << v[i] << " ";            \
  cout << '\n'
#define pr1(x) cout << fixed << setprecision(15) << x << '\n'
#define ordered_set tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(val)  no. of elements strictly less than val
// s.find_by_order(i)  itertor to ith element (0 indexed)
#define mod 1000000007
#define mod1 998244353
#define fio                         \
  ios_base::sync_with_stdio(false); \
  cin.tie(NULL)
#define get(n) \
  ll n;        \
  cin >> n
#define getvec(v, n)         \
  vector<ll> v(n);           \
  for (ll i = 0; i < n; i++) \
    cin >> v[i];
#define getstr(s) \
  string s;       \
  cin >> s
#define all(x) x.begin(), x.end()
#define countBits(x) __builtin_popcount(x)
using namespace std;

ll n, m;
vl a, b;
vector<vl> dp;

ll solve(ll i, ll j)
{
  if (i >= n)
    return m - j;
  if (j >= m)
    return n - i;
  if (dp[i][j] != -1)
    return dp[i][j];
  ll ans1 = (a[i] != b[j]) + solve(i + 1, j + 1);
  ll ans2 = solve(i + 1, j) + 1;
  ll ans3 = solve(i, j + 1) + 1;
  ll ans = min(ans1, min(ans2, ans3));
  dp[i][j] = ans;
  return ans;
}

void mainSolve()
{
  cin >> n >> m;
  dp.resize(n, vl(m, -1));
  for (int i = 0; i < n; i++)
  {
    get(x);
    a.pb(x);
  }
  for (int i = 0; i < m; i++)
  {
    get(x);
    b.pb(x);
  }
  ll ans = solve(0, 0);
  p1(ans);
}

int main()
{
  fio;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  //get(t);
  ll t = 1;
  while (t--)
  {
    mainSolve();
  }
  return 0;
}


#include<bits/stdc++.h>
#define rep(i,j,n) for(int i = (j); i < (n); ++i)
using namespace std;
#define pri(str) cout << str << endl
using ll = long long;
using P = pair<int, int>;

const ll MX = 1e18;
const long double PI = acos(-1);

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

int N, T[101];
int dp[101][102010];
const int inf = 1000100;
int main() {
    cin >> N;
    rep(i, 0, N) cin >> T[i];

    rep(i, 0, N + 1) rep(t, 0, 101010) dp[i][t] = inf;
    dp[0][0] = 0;
    rep(i, 0, N) rep(t, 0, 101010) if (dp[i][t] != inf) {
        chmin(dp[i + 1][t + T[i]], dp[i][t]);
        chmin(dp[i + 1][t], dp[i][t] + T[i]);
    }

    int ans = inf;
    rep(t, 0, 101010) chmin(ans, max(t, dp[N][t]));
    cout << ans << endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
// clang-format off
// #include <atcoder/all>
// using namespace atcoder;
// using mint = modint1000000007;
// using mint = modint998244353

using ll = int64_t;
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
    for (auto& a : v) cin >> a;
    return is;
}
template <class T>
istream& operator>>(istream& is, vector<pair<T, T>>& v) {
    for (auto& a : v) cin >> a.first >> a.second;
    return is;
}
template <class T>
istream& operator>>(istream& is, vector<tuple<T, T, T>>& v) {
    for (auto& a : v) {
        T a1, a2, a3;
        cin >> a1 >> a2 >> a3;
        a = {a1, a2, a3};
    }
    return is;
}
template <class T>
ostream& operator<<(ostream& os, vector<T>& v) {
    for (auto& a : v) os << a << " ";
    os << endl;
    return os;
}
template<class T>
bool chmin(T &a, T b) {
    if (a > b) { a = b; return true; }
    return false;
}
template<class T>
bool chmax(T &a, T b) {
    if (a < b) { a = b; return true; }
    return false;
}
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define TRC1(a) cout << #a ":" << a
#define TRC2(a, b) TRC1(a); cout << " "; TRC1(b)
#define TRC3(a, b, c) TRC2(a, b); cout << " "; TRC1(c)
#define TRC4(a, b, c, d) TRC3(a, b, c); cout << " "; TRC1(d)
#define GET_NAME(_1,_2,_3,_4,NAME,...) NAME
#define TRC(...) GET_NAME(__VA_ARGS__, TRC4, TRC3, TRC2, TRC1) (__VA_ARGS__) << endl
using veci = vector<int>;
using vecll = vector<ll>;
using Pi = pair<int, int>;
using Ti = tuple<int, int, int>;
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
const int IINF = INT32_MAX;
const ll LINF = INT64_MAX;

// cout << fixed << setprecision(15);
void solve();
int main() {
    cin.tie(0); ios::sync_with_stdio(0);
    cout << fixed << setprecision(15); solve(); return 0; }
// clang-format on

void solve() {
    int N;
    cin >> N;
    bitset<100001> bit;
    int tot = 0;
    bit[0] = 1;
    rep(i, N) {
        int t;
        cin >> t;
        tot += t;
        bit |= bit << t;
    }
    int ans = 1e9;
    rep(i, tot + 1) if (bit[i]) ans = min(ans, max(i, tot - i));
    cout << ans << endl;
}


#include <stdio.h>

int main(){
	int N;
	scanf("%d",&N);
	int A[N],P[N],X[N];
	int a,b,c;
	long long d=1000000000001;
	int e=0;
	for(a=0;a<N;a++) {
		scanf("%d %d %d\n",&A[a],&P[a],&X[a]);
	}
	for(b=0;b<N;b++) {
		if(X[b]-A[b]>0) {
			if(d>P[b]) {
				d=P[b];
			}
		}
		else e++;
	}

	if(e!=N) printf("%lld",d);
	else printf("-1");

	return 0;
}

//CODED BY SUMIT KUMAR PRAJAPATI
#include <bits/stdc++.h>
using namespace std;

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

#define PI 3.1415926535897932384626
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define watch(x) cerr<<#x<<" = "<<x<<'\n'
#define rep(i,n) for(ll i=0;i<n;i++)
#define repe(i,n) for(ll i=1;i<=n;i++)
#define FOR(i,a,b) for(ll i=a;i<=b;i++)
#define printar(a,s,e) FOR(i,s,e)cout<<a[i]<<" ";cout<<'\n'
#define curtime chrono::high_resolution_clock::now()
#define timedif(start,end) chrono::duration_cast<chrono::nanoseconds>(end - start).count()

const int INF=1e9;
const int MX=1e5+5;
const int MD=1e9+7;
const int MDL=99824453;
auto time0 = curtime;

void solve(){

    int N;
    cin>>N;
    int A[N+1],P[N+1],X[N+1];
    int ans=MD;
    repe(i,N){
        cin>>A[i]>>P[i]>>X[i];
        if(X[i]-(A[i]-0.5+1)>0)
            ans=min(ans,P[i]);
        //t a-=(t-0.5+1)
    }
    if(ans==MD)
        cout<<"-1\n";
    else
        cout<<ans<<'\n';

}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    srand(time(0));
    time0 = curtime;

    ll t=1;
    //cin>>t;
    repe(tt,t){
        //cout<<"Case #"<<tt<<": ";
        solve();
    }

    cerr<<"Execution Time: "<<timedif(time0,curtime)*1e-9<<" sec\n";
    return 0;

}


/* code of Ayush Tiwari
    codeforces- servermonk
    codechef- ayush572000
*/

#include <bits/stdc++.h>
#define ll long long

//STL
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define all(v) v.begin(), v.end()

//loops
#define forn(i,a,b) for(int i=a;  i<b; i++)
#define rforn(i,a,b) for(int i=a; i>=b; i--)

// defined values
#define maxn 200004
#define Mod 1000000007

// fast io
#define FIO() ios_base::sync_with_stdio(0);cin.tie(0);

using namespace std;

vector<int> graph[maxn];
map<int,int> m;
int a[maxn];
int vis[maxn];

void dfs(int node,int flag){
    vis[node]=flag;
    m[a[node-1]]+=1;
    for(auto child: graph[node]){
        if(vis[child]) continue;
        if(!m[a[child-1]])
            dfs(child,1);
        else dfs(child,2);
    }
    m[a[node-1]]-=1;
}

void solution(){

    // This is the main code
    int n;
    cin>>n;
    forn(i,0,n) cin>>a[i];
    forn(i,0,n-1){
        int u,v;
        cin>>u>>v;
        graph[u].pb(v);
        graph[v].pb(u);
    }
    dfs(1,1);
    forn(i,1,n+1){
        if(vis[i]==1) cout<<i<<endl;;
    }
}

int main(){

    #ifndef ONLINE_JUDGE
    freopen("D:/competitive-programming/input.txt","r",stdin);
    freopen("D:/competitive-programming/output.txt","w",stdout);
    #endif

    FIO()

    ll t=1;
    // cin>>t;
    while (t--)
    {
        solution();
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define Vi vector<int>
#define for_(i, a, b) for(int i = (a);i < (b);++i)
#define ford_(i, a, b) for(int i = (b)-1;i >= (a);--i)
#define rep(i, n) for_(i, 0, n)
#define repd(i, n) rfor_(i, 0, n)

int ctoi(const char c){
  if('0' <= c && c <= '9') return (c-'0');
  return -1;
}

int main(){
  string s,t;

  cin >> s >> t;
  cout << max(ctoi(s[0])+ctoi(s[1])+ctoi(s[2]), ctoi(t[0])+ctoi(t[1])+ctoi(t[2]));

  return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
using namespace std;
using vi = vector<int>;

int main(){
    int a, b;
    cin >> a >> b;
    int plus  =  2000*b - a + 1;
    int minus = -2000*a - b + 1;
    rep(i,a){
        printf("%d ", plus + 2*i);
    }
    rep(i,b){
        printf("%d%c", minus + 2*i, i==b-1?'\n':' ');
    }
    return 0;
}

#include<bits/stdc++.h>

using namespace std;

int main(){
  int n;
  long long k;
  cin >> n >> k;
  long long dp[4][3000005]={0};
  dp[0][0]=1;
  for(int i=0;i<3;i++){
    for(int j=0;j<=i*n;j++){
      dp[i+1][j+1]+=dp[i][j];
      dp[i+1][j+n+1]-=dp[i][j];
    }
    for(int j=1;j<=(i+1)*n;j++){
      dp[i+1][j]+=dp[i+1][j-1];
    }
  }

  int x;
  for(int i=3;i<=3*n;i++){
    if(k<=dp[3][i]){x=i;break;}
    else{k-=dp[3][i];}
  }
  for(int i=1;i<=n;i++){
    int jmi=max(1,x-i-n);
    int jma=min(n,x-i-1);
    if(jmi>jma){continue;}
    if(k>(jma-jmi+1)){k-=(jma-jmi+1);continue;}
    int y=jmi+k-1;
    int z=x-i-y;
    cout << i << ' ' << y << ' ' << z << '\n';
    return 0;
  }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double

int main() {
    ll mod = 1e9+7;
    int N;
    cin >> N;
    vector<ll> A(N);
    for (int i = 0; i < N; i++) cin >> A[i];
    vector<vector<ll>> sum(N, vector<ll>(2, 0)), cnt(N, vector<ll>(2, 0));
    sum[0][0] = A[0];
    cnt[0][0] = 1;
    for (int i = 1; i < N; i++) {
        sum[i][0] = (sum[i-1][0]+sum[i-1][1]+A[i]*(cnt[i-1][0]+cnt[i-1][1]))%mod;
        sum[i][1] = (sum[i-1][0]+(mod-A[i])*cnt[i-1][0])%mod;
        cnt[i][0] = (cnt[i-1][0]+cnt[i-1][1])%mod;
        cnt[i][1] = cnt[i-1][0];
    }
    cout << (sum[N-1][0]+sum[N-1][1])%mod << endl;
    return 0;
}

#include <iostream>
#include <algorithm>
#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <complex>
#include <deque>
#include <valarray>
#include <unordered_map>
#include <unordered_set>
#include <array>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <chrono>
#include <random>
#include <numeric>
using namespace std;
using ll = long long;
using ld = long double;
using str = string;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vs> vvs;
typedef vector<vc> vvc;
typedef vector<vb> vvb;
using P = pair<ll,ll>;
#define REP(i, n) for (ll i = 0; i < (int)(n); i++)
#define RREP(i, n) for (ll i = (int)(n)-1; i > -1 ; i--)
#define IN(T, x) T x;cin >> x;
#define AIN(T, a, n) vector<T> a(n);REP(i, n){cin >> a[i];}
#define A2IN(T1, a, T2, b, n) vector<T1> a(n);vector<T2> b(n);REP(i, n){cin >> a[i] >> b[i];}
#define ALL(a) (a).begin(),(a).end()
#define SORT(a) sort(ALL(a))
#define RSORT(a) SORT(a);reverse(ALL(a))
#define PB push_back
#define MP make_pair
#define PF first
#define PS second
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}
template<typename A, size_t N, typename T>void Fill(A (&array)[N], const T &val){fill( (T*)array, (T*)(array+N), val );}
#define OUT(x) cout << x << endl;
#define OUTD(x) cout << fixed << x << endl;
#define BOUT(b) if(b){OUT("Yes");}else{OUT("No");}
const ll MOD = 1000000007;
const ll INF = 1e9;
const ld PI = 3.14159265369;
ll dx[4] = {1,-1,0,0};
ll dy[4] = {0,0,1,-1};
ll ddx[8] = {1,1,1,-1,-1,-1,0,0};
ll ddy[8] = {0,1,-1,0,1,-1,1,-1};

unsigned GetDigit(unsigned num){
    return std::to_string(num).length();
}

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

  IN(ll,n);
  AIN(ll,a,n);
  vvll dp(2,vector<ll>(n,0));
  dp[0][0]=1,dp[1][0]=0;
  REP(i,n-1){
    dp[0][i+1]=(dp[0][i]+dp[1][i])%MOD;
    dp[1][i+1]=dp[0][i];
  }
  ll ans = 0;
  REP(i,n){
    ans+=a[i]*(dp[0][i]*(dp[0][n-i-1]+dp[1][n-i-1])%MOD-dp[1][i]*dp[0][n-i-1]%MOD)%MOD;
    ans%=MOD;
  }
  OUT((ans+MOD)%MOD);
}


#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int main() {
	int h, w; cin >> h >> w;
	string s[h];
	REP(i, h) cin >> s[i];
	int res = 0;
	REP(i, h-1) REP(j, w-1) {
		int cnt = 0;
		REP(x, 2) REP(y, 2)
			if (s[i+y][j+x] == '#') cnt++;
		if (cnt%2) res++;
	}
	cout << res << '\n';
    return 0;
}

#include <bits/stdc++.h>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
using ll = long long;
#define rep(i,n) for (ll i=0; i < (n); ++i)
#define rep2(i,n,m) for(ll i=n;i<=m;i++)
#define rep3(i,n,m) for(ll i=n;i>=m;i--)
#define P pair<ll,ll>
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define mpa make_pair
#define fi first
#define se second
#define set20 cout<<fixed<<setprecision(20) ;

inline void chmax(ll& a,ll b){a=max(a,b);}
inline void chmin(ll& a,ll b){a=min(a,b);}
double pi=acos(-1) ;
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}

ll dp[1005][1005] ;
int main(){
  ll n,m ; cin>>n>>m ;
  vector<ll> A(n),B(m) ;
  rep(i,n) cin>>A[i] ;
  rep(i,m) cin>>B[i] ;
  rep(i,1005) dp[i][0]=i ;
  rep(i,1005) dp[0][i]=i ;
  rep2(i,1,n)rep2(j,1,m){
    ll p=dp[i-1][j-1] ;
    if(A[i-1]!=B[j-1]) p++ ;
  dp[i][j]=min(min(dp[i-1][j],dp[i][j-1])+1,p) ;
  }
  cout<<dp[n][m]<<endl ;
    return 0 ;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <queue>

using namespace std;

int R, C;
long long int A[520][520], B[520][520];

long long int INF = 99999999999999;

long long int dis[300000];

vector<int> Adj[300000];
vector<long long int> cost[300000];

priority_queue < pair<long long int, int> > PQ;

int used[300000];

int main(void) {

	cin >> R >> C;
	for (int i = 0; i < R; i++) {
		for (int j = 0; j < C - 1; j++) {
			cin >> A[i][j];
		}
	}

	for (int i = 0; i < R - 1; i++) {
		for (int j = 0; j < C; j++) {
			cin >> B[i][j];
		}
	}

	int N = R*C;

	dis[0] = 0;
	for (int i = 1; i <= R*C; i++) { dis[i] = INF; }

	for (int i = 0; i <= R - 1; i++) {
		for (int j = 0; j <= C - 2; j++) {
			int k = i*C + j;
			int u = i*C + j + 1;
			Adj[k].push_back(u);
			cost[k].push_back(A[i][j]);
		}
	}

	for (int i = 0; i <= R - 1; i++) {
		for (int j = 1; j <= C - 1; j++) {
			int k = i*C + j;
			int u = i*C + j - 1;
			Adj[k].push_back(u);
			cost[k].push_back(A[i][j-1]);
		}
	}

	for (int i = 0; i <= R - 2; i++) {
		for (int j = 0; j <= C - 1; j++) {
			int k = i*C + j;
			int u = (i + 1)*C + j;
			Adj[k].push_back(u);
			cost[k].push_back(B[i][j]);
		}
	}

	for (int i = 1; i <= R - 1; i++) {
		for (int j = 0; j <= C - 1; j++) {
			int k = i*C + j;
			for (int y = 0; y <= i - 1; y++) {
				int u = y*C + j;
				Adj[k].push_back(u);
				cost[k].push_back(i - y + 1);
			}
		}
	}

	PQ.push(make_pair(dis[0], 0)); used[0] = 1;

	int g = (R - 1)*C + C - 1;

	pair<long long int, int> tmp;

	while (PQ.size() > 0) {
		tmp = PQ.top();
		PQ.pop();

		long long int D = -tmp.first;
		int pos = tmp.second;

		used[pos] = 1;

		if (dis[pos] != D) { continue; }

		if (pos == g) { break; }

		for (int v = 0; v < (int)Adj[pos].size();v++) {
			int y = Adj[pos][v];
			if (used[y] != 0) { continue; }
			if (D + cost[pos][v] < dis[y]) {
				dis[y] = D + cost[pos][v];
				PQ.push(make_pair(-dis[y], y));
			}
		}

	}

	cout << dis[g]<< endl;

	return 0;
}

#ifdef _DEBUG
#define _GLIBCXX_DEBUG
#endif
#if __has_include(<atcoder/all>)
#include <atcoder/all>
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
#define rep(i, a, b) for(int i = (int)(a); i <= (int)(b); i++)
#define rrep(i, a, b) for(int i = (int)(a); i >= (int)(b); i--)
const int MOD = 1000000007;
const int MOD2 = 998244353;
const ll INF = 1e18;
const ld PI = acos(-1);

struct edge{
    int to,cost;
};

const int MXV = 1000000;//変更可
vector<edge> G[MXV];
vector<int> d(MXV, 1e9);

void dijkstra(int s){
    using Pair_for_edge = pair<int,int>;
    priority_queue<Pair_for_edge, vector<Pair_for_edge>, greater<Pair_for_edge>> pq;
    d[s] = 0;
    pq.push(Pair_for_edge(0, s));
    while(pq.size()){
        Pair_for_edge p = pq.top();
        pq.pop();
        int V = p.second;
        if(d[V] < p.first){
            continue;
        }
        rep(i,0,G[V].size()-1){
            edge e = G[V][i];
            if(d[e.to] > d[V] + e.cost){
                d[e.to] = d[V] + e.cost;
                pq.push(P(d[e.to], e.to));
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int R,C;
    cin >> R >> C;
    vector<vector<int>> A(R,vector<int>(C-1)),B(R-1,vector<int>(C));
    rep(i,0,R-1){
        rep(j,0,C-2){
            cin >> A[i][j];
            int v=i+1000*j,u=i+1000*(j+1);
            edge e;
            e.cost=A[i][j];
            e.to=u;
            G[v].push_back(e);
            e.to=v;
            G[u].push_back(e);
        }
    }
    rep(i,0,R-2){
        rep(j,0,C-1){
            cin >> B[i][j];
            int v=i+1000*j,u=i+1+1000*j;
            edge e;
            e.cost=B[i][j];
            e.to=u;
            G[v].push_back(e);
            e.to=v;
            e.cost=2;
            while((1000+e.to)%1000!=999){
                G[u].push_back(e);
                //cout << e.cost << endl;
                e.cost++;
                e.to--;
            }
        }
    }
    dijkstra(0);
    cout << d[(R-1)+1000*(C-1)] << endl;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mp make_pair
#define pb push_back
#define inF freopen("input.txt", "r", stdin);
#define outF freopen("output.txt", "w", stdout);
#define endl '\n'
#define MOD 1000000007
#define mm(arr) memset(arr, 0, sizeof(arr))
#define F first
#define S second
#define scanArray(a,n) for(int i = 0; i < n; i++){cin >> a[i];}
#define coutArray(a,n) for(int i = 0; i < n; i++){cout << a[i] << " ";}cout << endl;
#define ld long double
#define PI 3.141592653589793238
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
#define int ll

#ifndef LOCAL
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#endif
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);

struct debug {
#ifdef LOCAL
~debug() { cerr << '\n'; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};

#define imie(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

int fastpow(int v, int p){
    if (p == 0)return 1;
    if (p == 1)return v;
    int ans = fastpow(v, p/2);
    if (p&1)return (((ans%MOD) * (ans%MOD)%MOD) * (v%MOD))%MOD;
    else return ((ans%MOD) * (ans%MOD))%MOD;
}

void solve(){
    int n, p; cin >> n >> p;
    int ans = p - 1;
    ans *= fastpow(p - 2, n - 1);
    ans %= MOD;
    cout << ans << endl;
}

int32_t main(){
    FAST
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

#include<bits/stdc++.h>
#define int ll
#define sz(x) int((x).size())
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define x first
#define y second
using namespace std;
using ll = long long;
using pi = pair<int,int>;
const int inf = 0x3f3f3f3f3f3f3f3f;
const int minf = 0xc0c0c0c0c0c0c0c0;
const int mod = 1e9+7;

int pw(int a, int b) {
    if (b == 0) return 1;
    if (b == 1) return a;
    int h = pw(a, b>>1);
    if (b & 1) return h*h%mod*a%mod;
    return h*h%mod;
}

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

    int n,p;
    cin>>n>>p;
    cout<<pw(p-2, n-1)*(p-1)%mod<<'\n';

    return 0;
}