#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<climits>
#include<cmath>
#include<cstring>
#include<deque>
#include<functional>
#include<iostream>
#include<iomanip>
#include<map>
#include<numeric>
#include<optional>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<tuple>
#include<unordered_map>
#include<unordered_set>
#include<vector>
#pragma GCC optimize("Ofast")
namespace gengar
{
	using namespace std;
	#define int long long
	#define itn int
	#define uint unsigned long long
	#define ld long double
	#define vt(tp) vector<tp>
	#define vvt(tp) vector<vector<tp>>
	#define vvt2(nm,tp,h,w,n) vector<vector<tp>>nm((h),vector<tp>(w,n))
	#define P pair<int,int>
	#define hmap unordered_map
	#define hset unordered_set
	#define all(x) x.begin(),x.end()
	#define nsort(x) sort(all(x))
	#define rsort(x) nsort(x);reverse(all(x))
	#define unq(v) v.erase(unique(all(v)),v.end())
	#define l_b(c,x) distance(c.begin(),lower_bound(all(c),(x)))
	#define u_b(c,x) distance(c.begin(),upper_bound(all(c),(x)))
	#define fi first
	#define se second
	#define mp make_pair
	#define pb push_back
	#define eb emplace_back
	#define spc ' '
	#define pass
	#define _overload3(_1,_2,_3,name,...) name
	#define _rep(i,n) repi(i,0,n)
	#define repi(i,a,b) for(int i=(a),__SIZE##_=(b);i<__SIZE##_;i++)
	#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
	#define _daolrevo3(_1,_2,_3,name,...) name
	#define _per(i,n) peri(i,n,0)
	#define peri(i,a,b) for(int i=(a),__SIZE##_=(b);i>=__SIZE##_;i--)
	#define per(...) _daolrevo3(__VA_ARGS__,peri,_per,)(__VA_ARGS__)
	#define Bit(n) (1LL<<(n))
	#define myceil(a,b) ((a)+((b)-1))/(b)
	#define scale(n) cout<<fixed<<setprecision(n)
	using i64=int64_t;
	using u64=uint64_t;
	template<class T>using PQ=priority_queue<T,vt(T),greater<T>>;
	void in(){}
	template<class Car,class...Cdr>
	void in(Car&&car,Cdr&&...cdr){cin>>car;in(forward<Cdr>(cdr)...);}
	template<class T>void drop(T x){cout<<(x)<<endl;exit(0);}
	void dYes(){puts("Yes");exit(0);}
	void dNo(){puts("No");exit(0);}
	int gcd(int a,int b){return b?gcd(b,a%b):a;}
	int lcm(int a,int b){return a/gcd(a,b)*b;}
	int fact(int n,int m){int f=n;for(int i=n-1;i>=1;i--){f*=i;f%=m;}return f;}
	template<class T>int chmin(T&a,const T&b){if(b<a){a=b;return 1;}return 0;}
	template<class T>int chmax(T&a,const T&b){if(a<b){a=b;return 1;}return 0;}
	const int inf=Bit(60);
	const double pi=acos(-1);
	const int mod=1000000007;//998244353
};
using namespace gengar;

int32_t main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;cin>>n;
	vector<pair<double,double>>lr(n);
	rep(i,n)
	{
		double t,l,r;cin>>t>>l>>r;
		if(t==2)r-=0.1;
		else if(t==3)l+=0.1;
		else if(t==4)r-=0.1,l+=0.1;
		lr[i].fi=l;lr[i].se=r;
	}
	int cnt=0;
	for(int i=0;i<n;i++)
	for(int j=i+1;j<n;j++)
	{
		if((lr[i].fi-lr[j].se)*(lr[j].fi-lr[i].se)>=0)cnt++;
	}
	cout<<cnt<<endl;
	return 0;
}

#include <iostream>
#include <vector>
#include<string>
#include <algorithm>
#include <cmath>
#include <map>
#define ll long long
using namespace std;

int main(void){
  int num;
  cin >> num;
  vector<ll> coins(num);
  ll pay;
  cin >> pay;
  for(int i = 0;i < num;i ++){
    cin >> coins[num-1-i];
  }
  vector<map<ll,int>> ans(num);
  ans[0][pay] = 1;
  for(int i = 0;i < num-1;i ++){
    for(auto x : ans[i]){
      ll p = x.first;
      p = p % coins[i];
      ll payi = (x.first - p)/coins[i];

          if(abs(payi)* coins[i] < coins[i-1] or i == 0){
        ans[i+1][x.first-payi*coins[i]] += x.second;
          }
      if(p != 0){
        if(p > 0){
          p -= coins[i];
          payi++;
        }else{
          p += coins[i];
          payi --;
        }
        if(i == 0){
        ans[i+1][x.first-payi*coins[i]] += x.second;
        }else{
          if(abs(payi)* coins[i] < coins[i-1]){
        ans[i+1][x.first-payi*coins[i]] += x.second;
          }
        }
      }
    }
  }
  ll sum = 0;
  for(auto x : ans[num-1]){
    sum = sum + x.second;
  }
  cout << sum << endl;

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define     M                     1000000007
#define     ll                    long long
#define     FIO                   ios_base::sync_with_stdio(false);cin.tie(NULL)
#define     ifor(st,lim)          for(int i=st;i<lim;i++)
#define     jfor(stt,llim)        for(int j=stt;j<llim;j++)
#define     pb                    push_back

vector<int> adj[2005];
int vis[2005];
int ans;
void dfs(int node) {
	vis[node] = 1;

	for(auto child : adj[node]) {
		if(!vis[child]) {
			ans++;
			dfs(child);
		}
	}
}

void solve() {
	int n,m; cin>>n>>m;

	while(m--) {
		int a,b; cin>>a>>b;
		adj[a].pb(b);
	}
	ans = n;
	for(int i=1;i<=n;i++) {
		memset(&vis,0,sizeof vis);
		if(!vis[i])
			dfs(i);
	}
	cout<<ans;
}
void file_i_o() {
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif
}
int main()
{
	file_i_o();
	FIO;
	int t=1;
	while(t--) {
		solve();
	}
}

#include <bits/stdc++.h>
using namespace std;
#define deb(k) cerr << #k << ": " << k << "\n";
#define size(a) (int)a.size()
#define fastcin cin.tie(0)->sync_with_stdio(0);
#define st first
#define nd second
#define pb push_back
#define mk make_pair
#define int long long
typedef long double ldbl;
typedef double dbl;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef map<int, int> mii;
typedef vector<int> vint;
#define MAXN 300100
#define MAXLG 20
const int inf = 0x3f3f3f3f;
const ll mod = 1000000007;
const ll linf = 0x3f3f3f3f3f3f3f3f;
const int N = 200010;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

vector<pii>g[N];
int c[N];
int color[N];
pii ans[N];

void dfs(int x, int p = -1){
	color[x] = 1;
	for(pii e : g[x]){
		int v = e.st, i = e.nd;
		if(v == p) continue;
		if(!color[v]){
			ans[i] = {x, v};
			dfs(v, x);
		}
		else if(color[v] == 1){
			ans[i] = {x, v};
		}
	}
	color[x] = 2;
}

void solve(){
	int n, m;
	cin>>n>>m;
	vector<pii>e(m);
	for(int i=0;i<m;i++){
		int a,b;
		cin>>a>>b;
		e[i] = {a, b};
	}
	for(int i=1;i<=n;i++) cin>>c[i];
	for(int i=0;i<m;i++){
		int a = e[i].st, b = e[i].nd;
		if(c[a] == c[b]){
			g[a].pb({b, i});
			g[b].pb({a, i});
		}
		else{
			if(c[a] < c[b]) ans[i] = pii(b, a);
			else ans[i] = pii(a, b);
		}
	}
	for(int i=1;i<=n;i++){
		if(!color[i]) dfs(i);
	}
	for(int i=0;i<m;i++){
		if(e[i].st == ans[i].st) cout<<"->\n";
		else cout<<"<-\n";
	}

	// Have you read the problem again?
	// Maybe you understood the wrong problem
}

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


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <limits>
#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;
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();
// Modint
// modint<MOD> で宣言
template<ll MOD>
struct modint{
  ll x;
  ll mod = MOD;
  modint(ll x=0):x(x%MOD){}
  modint& operator+=(const modint a){
    if((x+=a.x)>=MOD) x-=MOD;
    return *this;
  }
  modint& operator-=(const modint a){
    if((x += MOD-a.x)>=MOD) x-=MOD;
    return *this;
  }
  modint& operator*=(const modint a){
    (x*=a.x)%=MOD;
    return *this;
  }
  modint operator+(const modint a) const{
    modint res(*this);
    return res+=a;
  }
  modint operator-(const modint a) const{
    modint res(*this);
    return res-=a;
  }
  modint operator*(const modint a) const{
    modint res(*this);
    return res*=a;
  }
  modint pow(ll t) const{
    if(!t) return 1;
    modint a = pow(t>>1);
    a*=a;
    if(t&1) a*=*this;
    return a;
  }

  //for prime mod
  modint inv() const{
    return pow(MOD-2);
  }
  modint& operator/=(const modint a){
    return (*this) *= a.inv();
  }
  modint operator/(const modint a) const{
    modint res(*this);
    return res/=a;
  }
};
using mint = modint<998244353>;

const int NMAX=1000010; // we can calculate nCk until n is equal to NMAX
mint fact[NMAX],infac[NMAX];
void Make_Fact(){
  fact[0]=fact[1]=1;
  for(int i=2;i<=NMAX-1;++i){
    fact[i]=fact[i-1]*(mint)i;
  }
}
void Make_InvFact(){
  infac[0]=infac[1]=1;
  for(int i=2;i<=NMAX-1;++i){
    infac[i]=infac[i-1]/(mint)i;
  }
}
mint Comb(int n,int k){
  if(n<0||k<0||n-k<0) return 0;
  return fact[n]*infac[k]*infac[n-k];
}

struct UFT{
  vector<int> par;//親
  vector<int> rank;//木の深さ
  vector<int> size;//木の大きさ
  int n;

  UFT(int _n)
  {
    n = _n;
    par.resize(n);
    rank.assign(n,0);
    size.assign(n,1);
    rep(i,n){
      par[i] = i;
    }
  }

  //xの根を返す
  int find(int x)
  {
    if(par[x] == x) return x;
    else return par[x] = find(par[x]);
  }

  //x,yを併合
  void unite(int x,int y)
  {
    x = find(x);
    y = find(y);
    if(x == y) return;

    if(rank[x] < rank[y]){
      par[x] = y;
      size[y] += size[x];
    }
    else{
      par[y] = x;
      size[x] += size[y];
      if(rank[x] == rank[y]) rank[x]++;
    }
  }

  //x,yが同じグループにいるかどうかを返す
  bool same(int x,int y)
  {
    return find(x) == find(y);
  }

  //xの属する木のサイズを探す
  int usize(int x)
  {
    return size[find(x)];
  }
};

int main()
{
  int n,k; cin >> n >> k;
  vvi a(n, vi(n));
  rep(i,n) {
    rep(j,n) {
      cin >> a[i][j];
    }
  }

  Make_Fact();
  UFT uf(2*n);
  rep(i,n) {
    rep(j,n) {
      if(i >= j) continue;
      bool f = true;
      rep(l,n) {
	if(a[i][l] + a[j][l] > k) f =false;
      }
      if(f) uf.unite(i, j);
    }
  }

  rep(i,n) {
    rep(j,n) {
      if(i >= j) continue;
      bool f = true;
      rep(l,n) {
	if(a[l][i] + a[l][j] > k) f =false;
      }
      if(f) uf.unite(i+n, j+n);
    }
  }

  mint res(1);
  rep(i,2*n) {
    if(uf.find(i) == i) {
      res *= fact[uf.usize(i)];
    }
  }

  cout << res.x << "\n";

  return 0;
}

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

#define fst ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define tc int tt; cin >> tt; while(tt--)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second

const int N = 55, mod = 998244353;

map<int, vector<int> > m;
bool vis[N];
int f[N];

int dfs(int s) {
	vis[s] = 1;

	int sz = 0;
	for(auto x:m[s]) {
		if(!vis[x]) {
			sz += dfs(x);
		}
	}
	return 1 + sz;
}

int32_t main()
{
	fst
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	int n, K;
	cin >> n >> K;
	int mat[n][n];
	for(int i=0;i<n;++i) {
		for(int j=0;j<n;++j) cin >> mat[i][j];
	}

	f[0] = f[1] = 1;
	for(int i=2;i<N;++i) f[i] = i*f[i-1], f[i] %= mod;

	for(int i=0;i<n;++i) {
		for(int j=i+1;j<n;++j) {
			bool ok = 1;
			for(int k=0;k<n;++k) {
				if(mat[i][k] + mat[j][k] > K) ok = 0;
			}

			if(ok) {
				m[i].pb(j);
				m[j].pb(i);
			}
		}
	}

	int ans = 1;
	for(int i=0;i<n;++i) {
		if(!vis[i]) {
			int sz = dfs(i);
			ans *= f[sz];
			ans %= mod;
		}
	}

	m.clear();
	memset(vis, 0, sizeof vis);

	for(int i=0;i<n;++i) {
		for(int j=i+1;j<n;++j) {
			bool ok = 1;
			for(int k=0;k<n;++k) {
				if(mat[k][i] + mat[k][j] > K) ok = 0;
			}

			if(ok) {
				m[i].pb(j);
				m[j].pb(i);
			}
		}
	}

	for(int i=0;i<n;++i) {
		if(!vis[i]) {
			int sz = dfs(i);
			ans *= f[sz];
			ans %= mod;
		}
	}

	cout << ans;
}


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

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
using ll=long long int;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
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;}

template<int mod=1000000007>struct fp {
   int v; static int get_mod(){return mod;}
   int inv() const{
      int tmp,a=v,b=mod,x=1,y=0;
      while(b)tmp=a/b,a-=tmp*b,swap(a,b),x-=tmp*y,swap(x,y);
      if(x<0){x+=mod;} return x;
   }
   fp(ll x=0){init(x%mod+mod);}
   fp& init(int x){v=(x<mod?x:x-mod); return *this;}
   fp operator-()const{return fp()-*this;}
   fp pow(ll t){fp res=1,b=*this; while(t){if(t&1)res*=b;b*=b;t>>=1;} return res;}
   fp& operator+=(const fp& x){return init(v+x.v);}
   fp& operator-=(const fp& x){return init(v+mod-x.v);}
   fp& operator*=(const fp& x){v=ll(v)*x.v%mod; return *this;}
   fp& operator/=(const fp& x){v=ll(v)*x.inv()%mod; return *this;}
   fp operator+(const fp& x)const{return fp(*this)+=x;}
   fp operator-(const fp& x)const{return fp(*this)-=x;}
   fp operator*(const fp& x)const{return fp(*this)*=x;}
   fp operator/(const fp& x)const{return fp(*this)/=x;}
   bool operator==(const fp& x)const{return v==x.v;}
   bool operator!=(const fp& x)const{return v!=x.v;}
   friend istream& operator>>(istream& is,fp& x){ll t; is>>t; x=init(t); return is;}
   friend ostream& operator<<(ostream& os,const fp& x){os<<x.v; return os;}
}; using Fp=fp<>;
template<typename T>struct factorial {
   vector<T> Fact,Finv,Inv;
   factorial(int maxx){
      Fact.resize(maxx); Finv.resize(maxx); Inv.resize(maxx);
      Fact[0]=Fact[1]=Finv[0]=Finv[1]=Inv[1]=1;
      rep(i,2,maxx){Fact[i]=Fact[i-1]*i;} Finv[maxx-1]=Fact[maxx-1].inv();
      for(int i=maxx-1;i>=2;i--){Finv[i-1]=Finv[i]*i; Inv[i]=Finv[i]*Fact[i-1];}
   }
   T fact(int n,bool inv=0){if(n<0)return 0; return (inv?Finv[n]:Fact[n]);}
   T inv(int n){if(n<0)return 0; return Inv[n];}
   T nPr(int n,int r,bool inv=0){if(n<0||n<r||r<0)return 0; return fact(n,inv)*fact(n-r,inv^1);}
   T nCr(int n,int r,bool inv=0){if(n<0||n<r||r<0)return 0; return fact(n,inv)*fact(r,inv^1)*fact(n-r,inv^1);}
   T nHr(int n,int r,bool inv=0){return nCr(n+r-1,r,inv);}
};

Fp dp[3010][3010],dp2[3010][3010];

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

   dp[0][0]=dp2[0][0]=1;
   ll sum=0;
   rep(i,0,n){
      sum+=a[i];
      rep(x,1,n+1)dp[i+1][x]+=dp2[x-1][sum%x];
      rep(x,1,n+1)dp2[x][sum%(x+1)]+=dp[i+1][x];
   }
   Fp res;
   rep(x,1,n+1)res+=dp[n][x];
   cout<<res<<'\n';
   return 0;
}

#include<bits/stdc++.h>

using namespace std;

#define sz(x) (ll)((x).size())
typedef long long int ll;
typedef long double ld;

const ll INF=1e9;
const ll INFF=1e18;
const ll M=1e9+7;

void solve(){
    ll n;
    cin>>n;
    vector<ll> v(n+1),pre(n+1);
    vector<vector<ll>> dp(n+1,vector<ll>(n+1)),needed(n+1,vector<ll>(n+1));;
    for(int i=1;i<=n;i++){
        cin>>v[i];
        pre[i]=pre[i-1]+v[i];
    }
    needed[1][0]=1;
    for(int i=1;i<=n;i++){
        for(int numP=1;numP<=n;numP++){
            dp[i][numP]=needed[numP][pre[i]%numP];
        }
        for(int numP=1;numP<=n;numP++){
            (needed[numP][pre[i]%numP]+=dp[i][numP-1])%=M;
        }
    }
    ll ans=0;
    for(int i=1;i<=n;i++){
        (ans+=dp[n][i])%=M;
    }
    cout<<ans;
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int t=1;
    //cin>>t;
    int tt=t;
    while(t--){
        //cout<<"Case #"<<tt-t<<": ";
        solve();
    }
    return 0;
}


// #include <bits/stdc++.h>
#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,x,n) for(int i=x;i<n;i++)
#define breps(i,x,n) for(int i=x;i>=n;i--)
#define exrep(x,vec) for(auto&& x : vec);
#define PI 3.14159265358979323846264338327950288
const long long MOD = 1000000007;
const int INF{ 2100000000 };	// MAX:2147483647
typedef long long ll;
using namespace std;

// ------------- ここからコード -------------

int main(void) {

	int N;
	cin >> N;

	vector<int> A(N);
	rep(i, N) cin >> A[i];

	sort(A.begin(), A.end(), greater<int>());
	ll ans = 0;
	for (int cnt = 0; cnt < N; cnt++) {
		ll num = A[cnt];

		ans += (-num * cnt) + (num * (N - 1 - cnt));
	}

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

#include <bits/stdc++.h>

using namespace std;
//#define ll long long int
#define endl "\n"
using ll = int64_t;

int main()
{
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);cout.tie(NULL);
   ll a;
   cin>>a;
   unordered_set<ll> s;
   for(ll i=2;i*i<=a;i++){
	   ll x=i*i;
	   while(x<=a){
		   s.insert(x);
		   x=x*i;
	   }
   }
   cout<<a-s.size();
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vv = vector<vi>;
using vvv= vector<vv>;
using P  = pair<ll, int>;

const ll INF = 1e15;
ll dp[101][101];

int main(){
    int n; ll x;
    cin >> n >> x;
    vi a(n);
    rep(i,n)cin >> a[i];

    ll ans = 2e18;

    for(int k=1; k<=n; k++){
        //dp[j][c]:
        //i個目まで見てj種類使ってて
        //kで割った余りがcなものの中で
        //魔力の総和の最大値
        rep(i,101)rep(j,101)dp[i][j] = -INF;
        dp[0][0] = 0;
        rep(i,n)for(int j=i; j>=0; j--)rep(c,k){
            chmax( dp[j+1][(c+a[i])%k], dp[j][c]+a[i] );
        }
        //rep(c,k)cout << dp[k][c] << endl;
        //cout << endl;
        if(dp[k][x%k] > 0)chmin(ans, (x - dp[k][x%k])/k);
    }

    cout << ans << endl;

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

std::ostream&
operator<<( std::ostream& dest, __int128_t value )
{
    std::ostream::sentry s( dest );
    if ( s ) {
        __uint128_t tmp = value < 0 ? -value : value;
        char buffer[ 128 ];
        char* d = std::end( buffer );
        do
        {
            -- d;
            *d = "0123456789"[ tmp % 10 ];
            tmp /= 10;
        } while ( tmp != 0 );
        if ( value < 0 ) {
            -- d;
            *d = '-';
        }
        int len = std::end( buffer ) - d;
        if ( dest.rdbuf()->sputn( d, len ) != len ) {
            dest.setstate( std::ios_base::badbit );
        }
    }
    return dest;
}

__int128 gcd(__int128 a, __int128 b)
{
    if (b == 0)
    {
        return a;
    }
    return gcd(b, a % b);
}
int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    long long int t;
    vector <__int128> v;
    vector <__int128> p = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 };
    cin >> n;

    for (int i = 0; i < n; i++)
    {
        cin >> t;
        v.push_back(t);
    }

    __int128 res = 1e20;
    int m = p.size();

    for (int i = 0; i < (1 << m); i++)
    {
        __int128 val = 1;
        for (int j = 0; j < m; j++)
        {
            if ((i & (1 << j)))
            {
                val *= p[j];
            }
        }
        bool ok = true;
        for (int j = 0; j < n; j++)
        {
            if (gcd(v[j], val) == 1)
            {
                ok = false;
                break;
            }
        }
        if (ok)
        {
            res = min(res, val);
        }
    }

    cout << res << '\n';

    return 0;
}


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

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

#define rep(i, n) for(int i = 0;i < n;i++)
#define rep1(i, n) for(int i = 1;i < n;i++)
#define repr(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)

#define ALL(a)  (a).begin(),(a).end()

vector<vector<ll>> near8={{-1,1},{0,1},{1,1},{-1,0},{1,0},{-1,-1},{0,-1},{1,-1}};
vector<vector<ll>> near4={{0,1},{-1,0},{1,0},{0,-1}};

ll INF = INT64_MAX;

bool ll_sqrt(ll x, ll& lower_bound, ll& upper_bound)
{
	constexpr ll ll_max = numeric_limits<ll>::max();

	bool first_boost = true;

	lower_bound = 0;
	upper_bound = x;
	while (upper_bound - lower_bound > 1)
	{
		if (first_boost)
		{
			ll guess = (ll)sqrt((double)x);
			lower_bound = max(guess - 1, (ll)0);
			upper_bound = lower_bound + 3;
			first_boost = false;
		}

		ll newval = (lower_bound + upper_bound) / 2;
		if ((newval != 0) && (newval > ll_max / newval))  // newval*newval becomes overflow
			upper_bound = newval;  // because overflow means newval is too big
		else
		{
			ll newval_sqr = newval * newval;
			if (newval_sqr < x)
				lower_bound = newval;
			else
				upper_bound = newval;
		}
	}

	if ((upper_bound != 0) && (upper_bound > ll_max / upper_bound))
	{
		if (lower_bound * lower_bound == x)
			upper_bound = lower_bound;
	}
	else
	{
		if (lower_bound * lower_bound == x)
			upper_bound = lower_bound;
		else if (upper_bound * upper_bound == x)
			lower_bound = upper_bound;
	}
	bool is_exact = (lower_bound == upper_bound);
	return is_exact;
}

ll ll_ceil(ll x, int digit);
ll ll_floor(ll x, int digit);

ll ll_floor(ll x, int digit)
{
	if (x < 0)
		return -ll_ceil(-x, digit);

	ll div = 1;
	while (digit != 0)
	{
		div *= 10;
		digit--;
	}
	ll modval = x % div;
	ll ans = x - modval;
	return ans;
}

ll ll_ceil(ll x, int digit)
{
	if (x < 0)
		return -ll_floor(-x, digit);

	ll div = 1;
	while (digit != 0)
	{
		div *= 10;
		digit--;
	}
	ll modval = x % div;
	ll ans = x - modval;
	if (modval != 0)
		ans += div;
	return ans;
}

struct UnionFind {
    vector<ll> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

    UnionFind(ll N) : par(N) { //最初は全てが根であるとして初期化
        for(ll i = 0; i < N; i++) par[i] = i;
    }

    ll root(ll x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(ll x, ll y) { // xとyの木を併合
        ll rx = root(x); //xの根をrx
        ll ry = root(y); //yの根をry
        if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
    }

    bool same(ll x, ll y) { // 2つのデータx, yが属する木が同じならtrueを返す
        ll rx = root(x);
        ll ry = root(y);
        return rx == ry;
    }
};

ll H,W;
vector<vector<char>> map1(501, vector<char>(501,'#'));

bool solve(ll h, ll w){
	ll nexth=0;
	ll nextw=0;

	for(vector<ll> to:near4){
		nexth = h+to[0];
		nextw = w+to[1];
		if(nexth < 0 || nexth >=H || nextw < 0 || nextw >=W || map1[nexth][nextw] == '#'){
			continue;
		}
		if(map1[nexth][nextw] =='g'){
			return true;
		}
		map1[nexth][nextw]='#';
		if(solve(nexth, nextw)){
			return true;
		};
	}

	return false;
}

int main() {
	ll blue,b,c,d, red;
	cin >> blue >> b >> c >> d;
	red = 0;

	ll ans = 0;

	while(true){

	if(red * d >= blue){
			break;
	}else if(ans > 100000000){
		ans = -1;
		break;
	}
		blue += b;
		red += c;

		ans++;
	}

	cout << ans << endl;

  return 0;
}

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll=int;
using ld=long double;
using pll=pair<ll, ll>;
//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;
/*
01234でタイルの方向管理？
*/
#define TIMEOVER_M 12000
#define TIMEOVER 1980000
#define DIV 500

ll T[50][50];
bool B[50][50];
ll P[50][50];
ll D[50][50];
char *ans;
char *W;
ll score=0;
ll high=0;
ll dx[]={0, 1, 0, -1};
ll dy[]={-1, 0, 1, 0};
char dc[]={'U', 'R', 'D', 'L'};
bool all_fin;
bool fin;
clock_t start;
clock_t start_m;
ll loop;
ll node=0;

void steped(ll y, ll x, bool b){
  ll d=D[y][x];
  if (d){
    --d;
    ll ny=y+dy[d];
    ll nx=x+dx[d];
    B[ny][nx]=b;
  }
  B[y][x] = b;
}

// 行き止まりになったら、その時点でのstringを出力
void dfs(ll y, ll x, ll c){// {y, x, 歩数}
  bool used[4]={};
  rep(i, 4){
    clock_t now = clock();

    if(now-start_m>TIMEOVER_M) fin=true;
    if(now-start>TIMEOVER) all_fin=true;
    ll r=(node++/DIV)%4;
    while(used[r]) (r+=1)%=4;
    used[r]=true;
    ll ny=y+dy[r];
    ll nx=x+dx[r];
//    cout << "time:" << now-start << endl;
    if(ny<0 || nx<0 || ny>49 || nx>49 || B[ny][nx] || fin || all_fin){
      if(chmax(high, score)){
        if(ans) free(ans);
        ans=strdup(W);
      }
    }
    else{
      //反映
      score+=P[ny][nx];
      W[c]=dc[r];
      steped(ny, nx, true);

      dfs(ny, nx, c+1);
      //復元
      score-=P[ny][nx];
      W[c]='\0';
      steped(ny, nx, false);
    }
  }

}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  W=(char*)calloc(2000, 1);
  start = clock();
  ll si, sj;
  cin >> si >> sj;
  rep(i, 50) rep(j, 50) cin >> T[i][j];
  rep(i, 50) rep(j, 50) cin >> P[i][j];
  // タイルの方向D
  rep(i, 50) rep(j, 50){
    rep(k, 4){
      ll ny=i+dy[k];
      ll nx=j+dx[k];
      if(ny<0 || nx<0 || ny>49 || nx>49) continue;
      if(T[i][j]==T[ny][nx]) D[i][j]=k+1;
    }
  }
  while(!all_fin){
    loop++;
    fin=false;
    start_m = clock();
    score = P[si][sj];
    chmax(high, score);
    rep(y, 50) rep(x, 50) B[y][x]=false;
    rep(i, 2000) W[i]='\0';
    steped(si, sj, true);
    dfs(si, sj, 0);
  }
//  cout << high << " " << loop << " " << node << endl;
  printf("%s\n", ans);
}


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

// clang-format off
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define stoi stoll
#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; i++)
#define rep2(i, a, b) for (int i = (int)(a), i##_len=(b); i < i##_len; i++)
#define rep3(i, a, b) for (int i = (int)(a), i##_len=(b); i >= i##_len; i--)
#define FOR(i, a) for (auto &i: a)
#define ALL(obj) begin(obj), end(obj)
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define SUM(x) accumulate(ALL(x), 0LL)
#define LOWER_BOUND(A, key) distance(A.begin(), lower_bound(ALL(A), key))
#define UPPER_BOUND(A, key) distance(A.begin(), upper_bound(ALL(A), key))

const int MOD    = 998244353;
const int MOD2   = 1000000007;
const int INF    = (int)(1e13 + 7);
const double EPS = 1e-14;
const double PI  = 3.14159265358979;
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
template <class T, class S>  using P = pair<T, S>;
template<class T> bool chmax(T &a, const T &b) {if (a < b) {a = b;return true;}return false;}
template<class T> bool chmin(T &a, const T &b) {if (b < a) {a = b;return true;}return false;}
int CEIL(int a, int b) { return (a >= 0 ? (a + (b - 1)) / b : (a - (b - 1)) / b); }
int mod(int &a) {a = a >= 0 ? a % MOD : a - (MOD * CEIL(a, MOD));return a;}
int POW(int a, int b) {int res = 1;for (a %= MOD; b; a = a * a % MOD, b >>= 1)if (b & 1) res = res * a % MOD;return res;}
// clang-format on

template <class T> inline int Lower_bound_reverse(vector<T> &A, T key) { //key以下の最大のindex, 存在しない場合-1を返す。
  int ind = UPPER_BOUND(A, key) - 1;
  if (0 <= ind and ind < (int)A.size()) return ind;
  return -1;
}

class FProgrammingContest {
public:
  static void solve(istream &cin, ostream &cout) {
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    int N, T;
    cin >> N >> T;
    V<int> A(N / 2);
    V<int> B(CEIL(N, 2));
    rep(i, N / 2) cin >> A[i];
    rep(i, CEIL(N, 2)) cin >> B[i];
    vector<int> a = {0};
    vector<int> b = {0};
    vector<int> temp;
    FOR(i, A) {
      temp = {};
      FOR(j, a)
      if (i + j <= T)
        temp.emplace_back(i + j);
      FOR(j, temp)
      a.emplace_back(j);
    }
    FOR(i, B) {
      temp = {};
      FOR(j, b)
      if (i + j <= T) temp.emplace_back(i + j);
      FOR(j, temp)
      b.emplace_back(j);
    }
    sort(ALL(b));
    int ans = 0;
    FOR(i, a) {
      int ind = Lower_bound_reverse(b, T - i);
      if (0 <= ind and ind <= b.size()) chmax(ans, i + b[ind]);
    }
    cout << ans << endl;
  }
};

signed main() {
  FProgrammingContest solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}

#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

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

	cin >> N >> T;
	vector<long long int>v(N);
	for (auto &i : v)cin >> i;
	set<long long int>st;
	for (int i = 0; i < 1<<(N / 2); i++) {
		long long int sum = 0;
		for (int j = 0; j < N / 2; j++) {
			if (i >> j & 1)sum += v[j];
		}
		st.insert(sum);
	}
	long long int ans = 0;
	for (int i = 0; i < 1 << (N - N / 2); i++) {
		long long int sum = 0;
		for (int j = 0; j < N - N / 2; j++) {
			if (i >> j & 1) {
				sum += v[j + N / 2];
			}
		}
		auto it = st.upper_bound(T - sum);
		if (it != st.begin()) {
			ans = max(ans, sum + *prev(it));
		}
	}
	cout << ans << endl;
}


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

int main() {
  int a, b, c;
  cin >> a >> b >> c;
  int d = (b+c) / 2;
  int e = (c+a) / 2;
  int f = (a+b) / 2;
  if(a==d || b==e || c==f){
    cout << "Yes" << endl;
  }
  else{
    cout << "No" << endl;
  }
}

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

int main()
{
  std::vector<int> a(3);
  for (auto &x : a)
  {
    std::cin >> x;
  }
  std::sort(a.begin(), a.end());
  std::string s = ((a[2] - a[1]) == (a[1] - a[0])) ? "Yes" : "No";
  std::cout << s;
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
int main() {
	int N;
	scanf("%d", &N);
	int A[N], B[N], P[N], Q[N];
	for (int i = 0; i < N; ++i) scanf("%d", &A[i]);
	for (int i = 0; i < N; ++i) scanf("%d", &B[i]);
	for (int i = 0; i < N; ++i) scanf("%d", &P[i]), --P[i];
	for (int i = 0; i < N; ++i) Q[P[i]] = i;
	priority_queue<ii> pq;
	for (int i = 0; i < N; ++i) pq.push(ii(B[P[i]], P[i]));
	vector<ii> ans;
	for (int i = 0; i < N; ++i) {
		ii hv = pq.top();
		pq.pop();
		int x = Q[hv.second], y = hv.second;
		if (x == y) continue;
		if (B[P[x]] >= A[x] || B[P[y]] >= A[y]) {
			printf("-1");
			return 0;
		}
		ans.push_back(ii(x + 1, y + 1));
		swap(Q[P[x]], Q[P[y]]);
		swap(P[x], P[y]);
	}
	printf("%lu\n", ans.size());
	for (ii i: ans) printf("%d %d\n", i.first, i.second);
	return 0;
}

#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;
bool dd[maxn];
pll b[maxn];
ll a[maxn];
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);
    }
    ll n;
     cin>>n ;
     n*=2;
     for (int i=1;i<=n;i++)
     {
         cin>>a[i];
         b[i]=make_pair(a[i],i);
     }
     sort(b+1,b+n+1);
     for (int i=1;i<=n/2;i++) dd[b[i].ss]=1;
     bool chk=false;
     ll cnt=0;
     string ans;
     for (int i=1;i<=n;i++)
     {
          if (cnt==0)
          {
              chk=dd[i];
              ans.pb('(');
              cnt++;
          }
          else
          {
               if (dd[i]!=chk)
               {
                   cnt--;
                   ans.pb(')');
               }
               else
               {
                   cnt++;
                   ans.pb('(');
               }
          }
     }
     cout <<ans;

}


#include<bits/stdc++.h>
using namespace std;
using ll=long long;
static const ll mod=998244353;
ll N,M;
ll sum[5005];
ll POW[5005];
ll POWM[5005];
void init(){
for(ll i=0;i<5005;i++)POW[i]=1;
  for(ll i=0;i<5005;i++){
    ll x=0;
    for(ll j=0;j<M;j++){
    x=(x+POW[j])%mod;POW[j]=(j*POW[j])%mod;
    }sum[i]=x;
  }
POWM[0]=1;for(ll i=0;i<5005;i++)POWM[i+1]=(M*POWM[i])%mod;
}
int main(){
  cin>>N>>M;init();
  ll ans=N*POWM[N];
  for(ll n1=1;n1<N;n1++)
    for(ll n2=n1+1;n2<=N;n2++)
      ans=(ans+mod-(POWM[N-n2+n1-1]*sum[n2-n1-1])%mod)%mod;
  cout<<ans<<endl;
  return 0;
}

#include<bits/stdc++.h>
typedef int64_t i64;
typedef long double f128;
using namespace std;

template<typename T>
void scan(T& n)
{
  cin>>n;
  return;
}

void scan()
{
  return;
}

template<typename T,class... Args>
void scan(T& n,Args&... args)
{
  scan(n);
  scan(args...);
  return;
}

template<typename T>
void scan_array(T start,T end)
{
  T now=start;
  for(;now!=end;++now)
  {
    scan(*now);
  }
  return;
}

template<typename T>
void print(T n)
{
  cout<<n;
  return;
}

template<typename T>
void println(T n)
{
  print(n);
  print('\n');
  return;
}

template<typename T,class... Args>
void println(T n,Args... args)
{
  print(n);
  print(' ');
  println(args...);
  return;
}

template<typename T>
void print_array(T start,T end)
{
  T now=start;
  print(*now);
  ++now;
  for(;now!=end;++now)
  {
    print(' ');
    print(*now);
  }
  print('\n');
  return;
}

i64 pow_mod(i64 a,i64 n,i64 mod)
{
  i64 res=1,now=a;
  while(n)
  {
    if(n&1)
    {
      res=res*now%mod;
    }
    now=now*now%mod;
    n>>=1;
  }
  return res;
}

i64 inv_mod(i64 a,i64 mod)
{
  return pow_mod(a,mod-2,mod);
}

int main()
{
  i64 constexpr mod=1000000007;
  i64 H,W;
  scan(H,W);
  vector<string> S(H);
  scan_array(S.begin(),S.end());
  i64 K=0;
  for(i64 i=0;i<H;++i)
  {
    for(i64 j=0;j<W;++j)
    {
      if(S[i][j]=='.')
      {
        K+=1;
      }
    }
  }
  i64 d[H][W],u[H][W],r[H][W],l[H][W];
  for(int j=0;j<W;++j)
  {
    int now=0;
    for(int i=0;i<H;++i)
    {
      now=(S[i][j]=='#')?0:now+1;
      d[i][j]=now;
    }
    now=0;
    for(int i=H-1;i>=0;--i)
    {
      now=(S[i][j]=='#')?0:now+1;
      u[i][j]=now;
    }
  }
  for(int i=0;i<H;++i)
  {
    int now=0;
    for(int j=0;j<W;++j)
    {
      now=(S[i][j]=='#')?0:now+1;
      r[i][j]=now;
    }
    now=0;
    for(int j=W-1;j>=0;--j)
    {
      now=(S[i][j]=='#')?0:now+1;
      l[i][j]=now;
    }
  }
  i64 sum=0;
  for(int i=0;i<H;++i)
  {
    for(int j=0;j<W;++j)
    {
      if(S[i][j]=='.')
      {
        sum+=pow_mod(2,K-(l[i][j]+r[i][j]+u[i][j]+d[i][j]-3),mod);
      }
    }
  }
  sum%=mod;
  i64 ans=pow_mod(2,K,mod)*K%mod-sum;
  ans=(ans%mod+mod)%mod;
  println(ans);
}


/*
　　　　　／＞　 フ
　　　　　| 　_　 _|
　 　　　／`ミ _x 彡
　　 　 /　　　 　 |
　　　 /　  ヽ　　 ?
　／￣|　　 |　|　|
　| (￣ヽ＿_ヽ_)_)
　＼二つ
 */

#include <queue>
#include <vector>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MP make_pair
#define ll long long
#define fi first
#define se second
using namespace std;

template <typename T>
void read(T &x) {
    x = 0; bool f = 0;
    char c = getchar();
    for (;!isdigit(c);c=getchar()) if (c=='-') f=1;
    for (;isdigit(c);c=getchar()) x=x*10+(c^48);
    if (f) x=-x;
}

template<typename F>
inline void write(F x, char ed = '\n') {
	static short st[30];short tp=0;
	if(x<0) putchar('-'),x=-x;
	do st[++tp]=x%10,x/=10; while(x);
	while(tp) putchar('0'|st[tp--]);
	putchar(ed);
}

template <typename T>
inline void Mx(T &x, T y) { x < y && (x = y); }

template <typename T>
inline void Mn(T &x, T y) { x > y && (x = y); }

const int N = 505050;
int A[N], B[N], n;
ll ans;
priority_queue<int> q[2];
int main() {
	read(n);
	for (int i = 1;i <= n; i++) read(A[i]), ans += A[i];
	for (int i = 1;i <= n; i++) read(B[i]), B[i] -= A[i], q[i & 1].push(B[i]);
	while (q[0].size()) {
		ll x = q[0].top(), y = q[1].top();
		q[0].pop(), q[1].pop();
		if (x + y > 0) ans += x + y;
		else break;
	}
	write(ans);
	return 0;
}


#include <iostream>
using namespace std;

void remove(char*start,char*end){
  while(start<end+1){
    *start=' ';
    start++;
  }
}

int main(){
  int n,ans=0; cin >> n;
  char s[n];
  for(int i=0; i<n; i++) cin >> s[i];

  for(int i=0; i<n-1; i++){
    for(int j=0; n-1-j>i; j++){
      if(s[i]!=s[n-1-j]){
        remove(&s[i],&s[n-1-j]);
        ans++;
        i=n-j;
        j=-1;
      }
      else{
        while(s[n-1-j]==s[i]){
          j++;
        }
      }
    }
  }

  for(int i=0; i<n; i++){
    if(s[i]!=' '){
      cout << -1<< endl;
      return 0;
    }
  }

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

#include<bits/stdc++.h>
using namespace std;
void solve()
{
  	int n;
 	cin>>n;
  	string s;
  	cin>>s;
  	if(s[0]!=s[n-1])
    	{
      		cout<<1;
      		return ;
    	}
    	for(int i=1;i<n-1;++i)
    	{
    		if(s[0]==s[i]) continue;
    		if(s[0]==s[i+1]) continue;
    		cout<<2;
    		return ;
    	}
    	cout<<-1;

}
int main()
{
  	solve();
}


#include <bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(n);++i)
#define ALL(v)   (v).begin(),(v).end()
#define debug(x) cerr<<#x<<": "<<(x)<<endl
using namespace std;
using llong = long long;
using vi    = vector<int>;
using vvi   = vector<vi >;
using vvvi  = vector<vvi >;
using pii   = pair<int,int>;
constexpr int    INF=1e9;
constexpr double EPS=1e-9;
constexpr int    MOD=1e9+7;
template<class Type>
void line(const Type &a){int cnt=0;for(const auto &elem:a){cerr<<(cnt++?' ':'>');cerr<<elem;}cerr<<endl;}

int main(){//Refering other editorials.
	int h,w;
	cin>>h>>w;

	vector<string> vs(h);
	REP(i,h) cin>>vs[i];

	int all=0;//散らかってない箇所の数の合計.
	vvi num(h,vi(w,0));//(y,x)を照らすことができる照明の数.
	REP(i,h){
		int cnt=1;
		REP(j,w){
			if(vs[i][j]=='.'){
				all++;
				num[i][j]+=cnt;
				cnt++;
			}else cnt=1;
		}
		cnt=0;
		for(int j=w-1;j>=0;--j){
			if(vs[i][j]=='.'){
				num[i][j]+=cnt;
				cnt++;
			}else cnt=0;
		}
	}
	REP(j,w){
		int cnt=0;
		REP(i,h){
			if(vs[i][j]=='.'){
				num[i][j]+=cnt;
				cnt++;
			}else cnt=0;
		}
		cnt=0;
		for(int i=h-1;i>=0;--i){
			if(vs[i][j]=='.'){
				num[i][j]+=cnt;
				cnt++;
			}else cnt=0;
		}
	}
	//debug(all);

	vector<llong> mem(all+1,1);//mem[i]:=2^i;
	for(int i=1;i<=all;++i) mem[i]=mem[i-1]*2%MOD;

	llong ans=all*mem[all]%MOD;//全事象.
	REP(i,h)REP(j,w){
		if(vs[i][j]=='.'){
			llong tmp=mem[all-num[i][j]];
			ans=(ans-tmp+MOD)%MOD;
		}
	}

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

// coded by zeffy
#include <bits/stdc++.h>
using namespace std;
#define hello cout<<"hello"<<"\n"
#define forr(i,a,b) for(int i=a;i<b;i++)
#define dbg(s)	cout<<#s<<"= "<<s<<endl;
typedef long long int lli;

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

	int n;cin>>n;
	string s;int q;cin>>s>>q;
	string s1,s2;
	for(int i=0;i<n;i++) s1+=s[i];
	for(int i=n;i<2*n;i++) s2+=s[i];
	while(q--)
	{
		int ch;cin>>ch;
		if(ch==1)
		{
			int a,b;cin>>a>>b;a--;b--;if(a>=n) {a-=n;b-=n;char temp=s2[a];s2[a]=s2[b];s2[b]=temp;}
			else if(b>=n) {b-=n;char temp=s1[a];s1[a]=s2[b];s2[b]=temp;}
			else {char temp=s1[a];s1[a]=s1[b];s1[b]=temp;}
		}
		else
		{
			int a,b;cin>>a>>b;
			swap(s1,s2);
		}
	}
	cout<<s1<<s2<<"\n";

    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//optimal subsequences-2 technocup round-3 2020
using namespace std;
using namespace __gnu_pbds;
const int N=2e5+10;
//#define d double
#define F first
#define S second
#define ll long long int
#define FastIO ios_base::sync_with_stdio(false),cin.tie(0)
#define fix std::fixed<<setprecision(6)
const int M=1e9+7;
//const int M=998244353;
#define f(n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define vi vector<int>
#define pb push_back
//#define si set<int>
#define cl(n,m) (ceil(double(n)/double(m)))
#define T() int t; cin>>t; while(t--)
#define pi pair<ll,ll>
//const int MOD=998244353;
const int MOD=1e9+7;
#define endl "\n"
const double PI = 3.141592653589793238460;
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)

//ll d1=304933,d2=4395853;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

//bitset<N>bs;
//bitset<512>bb[512];
//vector<vector<int>>arr;
//#ifndef ONLINE_JUDGE
//freopen("input.txt", "r" , stdin);
//freopen("output.txt", "w", stdout);
//#endif

ll power(ll a,ll b)
{
    ll res=1;
    if(a==1)return 1;
    for(;b>0;b>>=1)
    {
        if(b&1)res=(res*a);
        if(res>MOD)res%=MOD;
        a=(a*a);
        if(a>MOD)a=a%MOD;
    }
    return res;
}

bool isPrime[1000000+1];
vector<ll>prime;

vector<int>divisor;

void seive2(int limit)
{
    divisor=vector<int>(limit+1,0);

    for(int i=2;i<=limit;i++)
    {
        if(divisor[i]==0)
        {
            divisor[i]=i;
            if(i*1ll*i<=limit)
            for(int j=i*i;j<=limit;j+=i)divisor[j]=i;
        }
    }
}

void seive(int limit)
{
    isPrime[0]=isPrime[1]=1;

    for(int i=2;i*i<=limit;i++)
    {
        if(!isPrime[i])
        for(int j=i*i;j<=limit;j+=i)
        {
            isPrime[j]=1;
        }
    }
    for(int i=2;i<=limit;i++)if(!isPrime[i])prime.pb(i);
}

#define pii pair<ll,pi>
ll pt[200010],sz[200010],wt[200010];

int finds(int x)
{
    while(pt[x]!=x)
    {
        pt[x]=pt[pt[x]];
        x=pt[x];
    }
    return pt[x];
}

bool unions(int x,int y)
{
    int px=finds(x),py=finds(y);

    if(px==py)return 0;

    if(sz[px]>sz[py])swap(px,py);

    pt[px]=py;
    sz[py]+=sz[px];
    wt[py]+=wt[px];

    return 1;
}

ll query(ll bit[],int x)
{
    ll ans=0;
    for(int i=x;i>0;i-=(i&(-i))){
        ans+=bit[i];
    }
    return ans;
}

void update(ll bit[],int x,ll val)
{
    for(int i=x;i>0;i-=(i&(-i)))
        bit[i]+=val;
}

void update2(ll bit[],int x,ll val)
{
    for(int i=x;i<=200010;i+=(i&(-i)))
        bit[i]+=val;
}

ll query2(ll sum[],int x)
{
    ll ans=0;
    for(int i=x;i<=200010;i+=(i&(-i)))
        ans+=sum[i];
    return ans;
}

void solve()
{
    int n;cin>>n;
    ll a[n],dp[n];
    f(n){cin>>a[i];}
    dp[0]=a[0];
    ll sum[n];
    sum[0]=a[0];
    for(int i=1;i<n;i++)dp[i]=max(dp[i-1],a[i]);
    for(int i=1;i<n;i++)sum[i]=sum[i-1]+a[i];

    f(n)
    {
        ll tot=i+1;
        tot*=dp[i];
        if(i)sum[i]+=sum[i-1];
        cout<<tot+sum[i]<<endl;
    }
}

int main()
{
    FastIO;

    int t=1;
	//cin>>t;

	for(int i=1;i<=t;i++){
	//cout<<"Case #"<<i<<": ";
    solve();
	}

	return 0;
}

#include<cstdio>
using namespace std;
#define int long long
int n,a[200010],sum[200010],sum2[200010],mx[200010];
signed main(){
    scanf("%lld",&n);
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
        sum[i]=sum[i-1]+a[i];
        sum2[i]=sum2[i-1]+sum[i];
        mx[i]=(mx[i-1]>a[i]?mx[i-1]:a[i]);
    }
    for(int i=1;i<=n;i++){
        printf("%lld\n",sum2[i]+mx[i]*i);
    }
    return 0;
}


#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<list>
#include<bitset>
#include<stack>
#include<memory>
#include<numeric>
#include <utility>

using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define REP(i, n) for(int i = 0;i < (n);i++)
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/
const llint mod=998244353;
const llint inf=2.19e15;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-10;
template <class T,class U>bool chmin(T& a,U b){if(a>b){a=b;return true;}return false;}
template <class T,class U>bool chmax(T& a,U b){if(a<b){a=b;return true;}return false;}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>llint LBI(T in,const vector<T>&ar){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>llint UBI(T in,const vector<T>&ar){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}

int main(void){
	cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);
	int n,m,ans=mod,i,j;cin>>n>>m;
	vector<vector<pair<int,char>>>go(n);
	for(i=0;i<m;i++){
		int a,b;char c;cin>>a>>b>>c;
		a--;b--;
		go[a].pub(mp(b,c));
		go[b].pub(mp(a,c));
	}
	static int dp[1001][1001];
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){dp[i][j]=mod;}
	}
	queue<tuple<int,int,int>>que;
	que.push(mt(0,n-1,0));
	dp[0][n-1]=0;
	while(que.size()){
		int a,b,d;tie(a,b,d)=que.front();que.pop();
		if(a==b){chmin(ans,d);}
		if(dp[a][b]>d){continue;}
		for(auto it:go[a]){
			for(auto jt:go[b]){
				if(it.sec==jt.sec){
					if(chmin(dp[it.fir][jt.fir],d+2)){que.push(mt(it.fir,jt.fir,d+2));}
				}
				if(it.fir==b&&jt.fir==a){chmin(ans,d+1);}
			}
		}
	}
	if(ans==mod){ans=-1;}
	cout<<ans<<endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i <= n; i++)
#define FORR(i, m, n) for(int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.rbegin(), v.rend());
#define ALL(v) (v).begin(),(v).end()

using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;

template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
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; }

const ll MOD = 1e9 + 7;
const ll INF = 1e18;

//隣接リストのdijkstra
vll dijkstra(vvll &g, int s){
    vll d(g.size(), INF);
    d[s] = 0;
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(P(0, s));
    while (!que.empty()){
        ll dist = que.top().first;
        int v = que.top().second;
        que.pop();
        if (d[v] < dist) continue;
        for (const auto &e: g[v]){
            if (d[e] <= d[v] + 1) continue;
            d[e] = d[v] + 1;
            que.push(P(d[e], e));
        }
    }
    return d;
}

int main(){
	cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    ll n, m;
    cin >> n >> m;
    ll a, b;
    char c;
    vector<vector<P>> mem(26);
    vector<P> es(m);
    REP(i, m) {
        cin >> a >> b >> c;
        a--, b--;
        mem[c - 'a'].push_back(P{a, b});
        mem[c - 'a'].push_back(P{b, a});
        es[i] = P{a, b};
    }
    vvll g(n * n);
    REP(cc, 26) {
        for (auto e1 : mem[cc]) {
            for (auto e2 : mem[cc]) {
                ll x1 = e1.first, x2 = e1.second;
                ll y1 = e2.first, y2 = e2.second;
                g[n * x1 + y1].push_back(n * x2 + y2);
            }
        }
    }
    vll d = dijkstra(g, n - 1);
    ll res = INF;
    REP(i, n) {
        if (d[(n + 1) * i] == INF) continue;
        chmin(res, 2 * d[(n + 1) * i]);
    }
    REP(i, m) {
        a = es[i].first, b = es[i].second;
        if (abs(d[n * a + b] - d[n * b + a]) != 1) continue;
        chmin(res, 2 * max(d[n * a + b], d[n * b + a]) - 1);
    }
    if (res == INF) cout << -1 << endl;
    else cout << res << endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string.h>
#include <unordered_map>
// #include<multiset>
#include <climits>
#include<unordered_set>
#include <iterator>
#include<random>
using namespace std;
// #define LOCAL
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, int> PLI;
typedef pair<int, LL> PIL;
const int N = 1005;
const int MOD = 1000000007;
int n, m;
int ans = INT_MAX;
const LL INF = 1e15;
vector<PLI>f;
int main()
{
#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("data.out", "w", stdout);
#endif
    scanf("%d", &n);
    for(int i = 0; i < n; ++i){
        int a, t;
        scanf("%d%d", &a, &t);
        if(i == 0){
            f.push_back({a,t});
        }else{
            if(f.back().second == t){
                if(t == 1){
                    f.back().first += a;
                }else if(t == 2){
                    f.back().first = max(1ll*a, f.back().first);
                }else{
                    f.back().first = min(1ll*a, f.back().first);
                }
            }else{
                f.push_back({a,t});
            }
        }
    }
    m = f.size();
    LL low = INF, high = -INF, bias = 0;
    for(int i = 0; i < m; ++i){
        auto p = f[i];
        if(p.second == 1){
            low += p.first;
            high += p.first;
            bias += p.first;
        }else if(p.second == 2){
            low = min(INF, max(p.first, low));
            high = max(high, p.first);
        }else{
            low = min(p.first, max(-INF, low));
            high = max(high, -INF);
        }
    }
    // cout << low << ' ' << bias << ' ' << high << endl;
    int q;
    scanf("%d", &q);
    while(q--){
        int x;
        scanf("%d", &x);
        LL res = x + bias;
        res = min(low, max(high, res));
        printf("%lld\n", res);
    }
    return 0;
}

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
ll INF = 1000000000000000;
struct segment_tree{
    int n; vector<ll> seg;
    segment_tree(vector<ll> v){
        n = v.size(); seg.resize(2*n);
        for(int i=0;i<n;i++) seg[i + n] = v[i];
        for(int i=n - 1;i>0;i--) seg[i] = seg[i<<1]^seg[i<<1|1];
    }
    void update(int p,ll val){
        for(seg[p += n] ^= val;p>1;p>>=1) seg[p>>1] = seg[p]^seg[p^1];
    }
    ll query(int l,int r){
        ll res = 0;
        for(l += n,r += n; l<r;l>>=1,r>>=1){
            if(l&1) res ^= seg[l++];
            if(r&1) res ^= seg[--r];
        }
        return res;
    }
    void clear(){for(int i=0;i<2*n;i++) seg[i] = 0;}
};

int main(){
    int i,n,q; cin >> n >> q;
    vector<ll> v(n);
    for(i=0;i<n;i++) cin >> v[i];
    segment_tree seg(v);
    for(i=0;i<q;i++){
        ll t,x,y; cin >> t >> x >> y;
        if(t==1){
            x--;
            seg.update(x,y);
        }
        if(t==2){
            x--;
            cout << seg.query(x,y) << "\n";
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll,ll>;
using pld = pair<ld,ld>;
using vll = vector<ll>;
using vld = vector<ld>;
using vstr = vector<string>;
#define _GLIBCXX_DEBUG
#define rep(j, m) for (ll j = 0; j < (ll)(m); j++)
#define rep2(i, l, n) for (ll i = l; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
const ld PI = 3.1415926535897932;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
vll dx = {-1,0,1,0};
vll dy = {0,-1,0,1};
vll Dx = {-1,-1,-1,0,0,1,1,1};
vll Dy = {-1,0,1,-1,1,-1,0,1};
const ll INF = 1000000000000000;

int main() {
    ll N;
    cin >> N;
    string S;
    cin >> S;
    vll V(N + 1);
    rep(i,N + 1) {cin >> V[i];}
    ll k = INF;
    rep(i,N) {
        k = min(k,abs(V[i + 1] - V[i]));
    }
    cout << k << endl;
    vector<vll> W(k,vll(N + 1));
    rep(i,N + 1) {
        ll a = V[i]/k;
        rep(j,V[i]%k) {
            W[j][i] = a + 1;
        }
        rep2(j,V[i]%k,k) {
            W[j][i] = a;
        }
    }
    rep(i,k) {
        rep(j,N + 1) {
            cout << W[i][j];
            if (j == N) {cout << endl;}
            else {cout << " ";}
        }
    }
}

#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
typedef long long ll;
string num;
int rn[200005];
int k;
int n;
int dp[200005][20];
ll dfs(int x,ll now,bool zero,bool flag)
{
    int cnt=0;
    for(int i=0;i<16;i++)
    {
        if(now&(1<<i))cnt++;
    }
    if(x>=n)return cnt==k;
    if(cnt>k)return 0;
    if(!flag&&zero&&dp[x][cnt]!=-1)return dp[x][cnt];
    ll ans=0;
    int lim=flag?rn[x]:15;
    for(int i=0;i<=lim;i++)
    {
        int tmp;
        if(!zero&&i==0)tmp=now;
        else tmp=now|(1<<i);
        ans+=dfs(x+1,tmp,zero||(i!=0),flag&&(i==lim));
        ans%=mod;
    }
    if(!flag&&zero)dp[x][cnt]=ans;
    return ans;
}
int main()
{
    cin>>num>>k;
    n=num.size();
    memset(dp,-1,sizeof dp);
    for(int i=0;i<n;i++)
    {
        if(num[i]>='0'&&num[i]<='9')rn[i]=num[i]-'0';
        else rn[i]=num[i]-'A'+10;
    }
    printf("%lld\n",dfs(0,0,0,1));
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
void solve(){
	int n;
	cin >> n;
	int a[n];
	int m=0,sum=0;
	for(int i=0;i<n;i++){
		cin >> a[i];
		m = a[i]-10;
		if(m>0)
		sum += m;
	}
	cout << sum;
}
int main()
{
	std::ios::sync_with_stdio(false);
	//int T;
	//cin >> T;
	// cin.ignore(); must be there when using getline(cin, s)
	//while(T--)
	//{
		solve();
	//}
	return 0;
}

/*Allah Vorosha*/
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define in insert
#define rev reverse
#define all(x) (x).begin(),(x).end()
#define all2(x) (x).rbegin(),(x).rend()
#define sz(v) (int)v.size()
#define yes cout << "YES\n"
#define no cout << "NO\n";
#define take for(auto &it : a) cin >> it;
#define vi vector<int>
#define vii vector<pair<int, int > >
#define vl vector<ll>
#define fi first
#define se second
#define l_b lower_bound
#define u_b upper_bound
#define ed printf("\n")
#define ios ios_base::sync_with_stdio(0);cin.tie();cout.tie();

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> void read(T &x) { cin >> x; }
template<class T, class ...U> void read(T &x, U& ... u) { read(x); read(u...); }
template<class T> void print1(const T &x) { cout << x << '\n'; }
template<class T, class ...U> void print1(const T &x, const U& ... u) { print1(x); print1(u...); }
template<class T> void print2(const T &x) { cout << x << ' '; }
template<class T, class ...U> void print2(const T &x, const U& ... u) { print2(x); print2(u...); }

const int N = 51, mod = 998244353, M = 5e5 + 5;
const ll inf = 9e18;
//int cs = 1;

int bp(int x) {
    return __builtin_popcount(x);
}

void solve() {
    int n;
    read(n);
    vi a(n);
    take;
    ll ans = 0, temp = 0;
    sort(all(a));
    for(int i = 0; i < n; i++) {
        ans += temp * a[i];
        ans %= mod;
        ans += a[i] * 1ll * a[i];
        ans %= mod;
        temp *= 2;
        //temp %= mod;
        temp += a[i];
        temp %= mod;
    }
    print1(ans);
}

int main() {
    ios;
    solve();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
typedef pair<int,int> pp;
bool com(pp x,pp y){
    if(x.ff==y.ff) return x.ss<y.ss;
    return x.ff<y.ff;
}
ll power(ll x,ll y){
    ll prod=1;
    while(y){
        if(y&1) prod=(prod*x)%mod;
        x=(x*x)%mod;
        y/=2;
    }
    return prod;
}
const int N=2e5+7;
void solve(){
    int n;
    cin>>n;
    int b[n+1],c[n+1];
    vector<int> vtx(n+1);
    for(int i=0;i<n;i++){
        int x;
        cin>>x;
        vtx[x]++;
    }
    for(int i=1;i<=n;i++) cin>>b[i];
    for(int i=1;i<=n;i++) cin>>c[i];
    ll ans=0;
    for(int i=1;i<=n;i++){
        ans+=vtx[b[c[i]]];
    }
    cout<<ans;
}
int main(){
    ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t=1;
	//cin>>t;
	while(t--)
	    solve();
	return 0;
}
/*
*/

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

// template {{{
#define  range(i, l, r) for (int i = (int)(l); i < (int)(r); (i) += 1)
#define rrange(i, l, r) for (int i = (int)(r) - 1; i >= (int)(l); (i) -= 1)

#define  whole(f, x, ...) ([&](decltype((x)) container) { return (f)(  begin(container),  end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)

#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl

using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;

constexpr i32 mod   = 1e9 + 7;
// constexpr i32 mod   = 998244353;

constexpr i32 inf   = 1001001001;
constexpr i64 infll = 1001001001001001001ll;

constexpr i32 dx[] = {0, -1, 1, 0, -1, 1, -1, 1};
constexpr i32 dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};

struct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;

template <typename T = i64> T input() { T x; cin >> x; return x; }

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? " " : ""); } return os; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }

template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }

template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
// }}}

void solve() {
  int n = input();

  vector< int > as(n), bs(n);
  cin >> as >> bs;
  whole(reverse, as);

  range(i, 0, n) if (as[i] > bs[i]) swap(as[i], bs[i]);

  i64 ans = 0;
  using P = pair<i64, i64>;
  priority_queue<P, vector< P >, greater< P >> pq;
  range(i, 0, n) {
    if (not pq.empty() and pq.top().first < as[i]) {
      ans += bs[i] + as[i] - pq.top().first;
      pq.emplace(pq.top().second, infll);
      pq.emplace(as[i], bs[i]);
      pq.pop();
    } else {
      ans += bs[i];
      pq.emplace(bs[i], infll);
    }
  }

  cout << ans << endl;
}

signed main() {
  solve();
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long int ll;
#define ordered_set tree<ll,null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
void dfs(ll root,map<ll,vector<ll> > &m,map<ll,bool> &vi,set<ll> &s)
{
    s.insert(root);
    for(ll i=0;i<m[root].size();i++)
    {
        if(!vi[m[root][i]])
        {
            vi[m[root][i]]=true;
            dfs(m[root][i],m,vi,s);
        }
    }
    return;
}
void solve()
{
    ll n;
    cin>>n;
    vector<ll> a(n);
    for(ll i=0;i<n;i++)
    {
        cin>>a[i];
    }
    ll i=0,j=n-1;
    map<ll,vector<ll> > m;
    while(i<j)
    {
        if(a[i]!=a[j])
        {
            m[a[i]].push_back(a[j]);
            m[a[j]].push_back(a[i]);
        }
        i++;
        j--;
    }
    map<ll,vector<ll> >::iterator it;
    map<ll,bool> vi;
    ll ans=0;
    for(it=m.begin();it!=m.end();it++)
    {
        ll p=it->first;
        if(!vi[p])
        {
            vi[p]=true;
            set<ll> s;
            dfs(p,m,vi,s);
            ans+=s.size()-1;
        }
    }
    cout<<ans<<endl;
    return;
}
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    ll test_case;
    test_case=1;
    //cin>>test_case;
    while(test_case--)
    {
        solve();
    }
    return 0;
}

#include<bits/stdc++.h>
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
#define all(k) k.begin(),k.end()
#define INF 1e9
#define repk(i,a,n) for(int i=a;i<=n;i++)
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define pb push_back
#define ub pop_back
#define eb emplace_back
#define ll  long long
#define ull unsigned long long
#define pi pair<int,int>
#define vc vector<char>
#define vpi vector<pi>
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
#define vb vector<bool>
#define pq priority_queue
#define vvc vector<vc>
#define mi map<int,vi>
#define mset map<string,set<char>>
#define umap unordered_map
#define int int64_t
using namespace std;
#define fi first
#define se second
const int mod = int(1e9)+7;
const int N =int(2e5)+5;
int power(int x,int y){  int res=1;  while(y){ if(y&1)res=(res*x)%mod;
    y>>=1;
    x=((x*x)%mod);
  }
return res;
}
int gcd(int x,int y){  if(x==0)    return y; return gcd(y%x,x); }

int pa[N], a[N], b[N];

int fun(int u)
{
    return (u == pa[u]) ? u : pa[u] = fun(pa[u]);
}

void init(){
    repk(i,1,200000) pa[i] = i;
}
void sol(){
    init();
int n;cin>>n;
vi a(n);
repk(i,1,n/2)
cin>>a[i];
    if(n&1)
        cin>>b[1];

    per(i,1,n/2)
        cin>>b[i];
    int ans = 0;
    repk(i,1,n/2)
    {
        int u = fun(a[i]), v = fun(b[i]);
        if(u != v)
        {
            ans++;
            pa[u] = v;
        }
    }

    cout<<ans;
cout<<'\n';
}

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

int t=1;
    // cin>>t;

 for(int i=1;i<=t;++i){

 // cout<<"Case #"<<i<<": ";
sol();
}

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
#define debug(a) cout << #a << " " << a << endl
const int maxn = 1e5 + 7;
const int N = 1500, M = N * 2;
const int inf = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
inline long long read();

char g[N][N];
int p[maxn], t1[maxn], t2[maxn];

int find(int x) { //·µ»ØxµÄ×æ×Ú½Úµã + Â·¾¶Ñ¹Ëõ
	if(p[x] != x) p[x] = find(p[x]);
	return p[x];
}

void unionfind(int x, int y) {
	p[find(x)] = find(y);
}
int main() {

//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

//	ios::sync_with_stdio(false);
	int H, W;
	cin >> H >> W;

	for(int i = 1; i <= H + W; i++) p[i] = i;

	for(int i = 1; i <= H; i++)
		for(int j = 1; j <= W; j++) {
			cin >> g[i][j];
			if(g[i][j] == '#') unionfind(i, j + H);
		}

	unionfind(1, H + 1);//Í¨¹ýÌí¼ÓÒ»¸öÆ«ÒÆÁ¿±£Ö¤ÐÐÁÐµÄ²»Í¬
	unionfind(1, H + W);
	unionfind(H, H + 1);
	unionfind(H, H + W);

	int ans1 = 0, ans2 = 0;
	for(int i = 1; i <= H; i++) t1[find(i)] = 1;
	for(int i = H + 1; i <= H + W; i++) t2[find(i)] = 1;
	for(int i = 1; i <= H + W; i++) ans1 += t1[i];
	for(int i = 1; i <= H + W; i++) ans2 += t2[i];

	cout << min(ans1, ans2) - 1;

	return 0;
}

/*
Êý×é¿ª¹»ÁËÂð ¿ªµ½ÉÏ½çµÄn+1´Î·½
³õÊ¼»¯ÁËÂð
*/

inline ll read() {
	char ch = getchar();
	ll p = 1, data = 0;
	while(ch < '0' || ch > '9') {
		if(ch == '-')p = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9') {
		data = data * 10 + (ch ^ 48);
		ch = getchar();
	}
	return p * data;
}

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

using Int = long long;
const char newl = '\n';

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=int>
vector<T> read(size_t n){
  vector<T> ts(n);
  for(size_t i=0;i<n;i++) cin>>ts[i];
  return ts;
}

// O(E \sqrt V)
struct HopcroftKarp{
  int L,R;
  vector< vector<int> > G;
  vector<int> match,level;

  HopcroftKarp(int L,int R):
    L(L),R(R),G(L+R),match(L+R,-1),level(L+R){}

  void add_edge(int u,int v){
    G[u].emplace_back(v+L);
    G[v+L].emplace_back(u);
  }

  bool bfs(){
    fill(level.begin(),level.end(),-1);
    queue<int> que;
    for(int i=0;i<L;i++){
      if(~match[i]) continue;
      level[i]=0;
      que.emplace(i);
    }
    bool found=false;
    while(!que.empty()){
      int v=que.front();que.pop();
      for(int u:G[v]){
        if(~level[u]) continue;
        level[u]=level[v]+1;
        int w=match[u];
        if(w==-1){
          found=true;
          continue;
        }
        if(~level[w]) continue;
        level[w]=level[u]+1;
        que.emplace(w);
      }
    }
    return found;
  }

  bool dfs(int v){
    for(int u:G[v]){
      if(level[v]+1!=level[u]) continue;
      level[u]=-1;
      int w=match[u];
      if(w<0 or dfs(w)){
        match[v]=u;
        match[u]=v;
        level[v]=-1;
        return true;
      }
    }
    level[v]=-1;
    return false;
  }

  int build(){
    int res=0;
    while(bfs())
      for(int i=0;i<L;i++)
        if(match[i]<0 and dfs(i))
          res++;
    return res;
  }

};

struct UnionFind{
  int num;
  vector<int> rs,ps;
  UnionFind(int n):num(n),rs(n,1),ps(n,0){
    iota(ps.begin(),ps.end(),0);
  }
  int find(int x){
    return (x==ps[x]?x:ps[x]=find(ps[x]));
  }
  bool same(int x,int y){
    return find(x)==find(y);
  }
  void unite(int x,int y){
    x=find(x);y=find(y);
    if(x==y) return;
    if(rs[x]<rs[y]) swap(x,y);
    rs[x]+=rs[y];
    ps[y]=x;
    num--;
  }
  int size(int x){
    return rs[find(x)];
  }
  int count() const{
    return num;
  }
};

//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int h,w;
  cin>>h>>w;
  auto S=read<string>(h);

  for(int y:{0,h-1})
    for(int x:{0,w-1})
      S[y][x]='#';

  vector<vector<int>> R(h),C(w);
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(S[i][j]=='#'){
        R[i].emplace_back(j);
        C[j].emplace_back(i);
      }
    }
  }

  UnionFind uf(h*w);
  auto idx=[&](int i,int j){return i*w+j;};
  for(int i=0;i<h;i++){
    if(R[i].empty()) continue;
    for(int j:R[i])
      uf.unite(idx(i,R[i][0]),idx(i,j));
  }
  for(int j=0;j<w;j++){
    if(C[j].empty()) continue;
    for(int i:C[j])
      uf.unite(idx(C[j][0],j),idx(i,j));
  }

  vector<int> uh(h),uw(w);
  for(int i=0;i<h;i++)
    for(int j=0;j<w;j++)
      if(S[i][j]=='#' and uf.same(idx(0,0),idx(i,j)))
        uh[i]=uw[j]=1;

  int nh=0,nw=0;
  set<int> sh,sw;
  for(int i=0;i<h;i++){
    if(R[i].empty()) nh++;
    else sh.emplace(uf.find(idx(i,R[i][0])));
  }

  for(int j=0;j<w;j++){
    if(C[j].empty()) nw++;
    else sw.emplace(uf.find(idx(C[j][0],j)));
  }

  drop(min(nh+sh.size(),nw+sw.size())-1);

  return 0;
}


#include <iostream>
using namespace std;

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

	int n, m, t;
	cin >> n >> m >> t;
	int start = 0, power = n;
	bool was = false;
	for(int i = 0; i < m; i++){
		int a, b;
		cin >> a >> b;
		power -= (a - start);
		if(power <= 0){
			was = true;
			break;
		}
		start = b;
		power += (b - a);
		if(power > n){
			power = n;
		}
	}
	power -= t - start;
	if(was || power <= 0){
		cout << "No";
	}
	else{
		cout << "Yes";
	}
	return 0;
}

#include<bits/stdc++.h>

using namespace std;

#define fRead(x)        freopen(x,"r",stdin)
#define fWrite(x)       freopen (x,"w",stdout)

#define LL              long long
#define ULL             unsigned long long
#define ff              first
#define ss              second
#define pb              push_back
#define PI              acos(-1.0)
#define mk              make_pair
#define pii             pair<int,int>
#define pll             pair<LL,LL>

#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define SQR(a)          ((a)*(a))
#define FOR(i,a,b)      for(int i=a;i<=b;i++)
#define ROF(i,a,b)      for(int i=a;i>=b;i--)
#define REP(i,b)        for(int i=0;i<b;i++)
#define MEM(a,x)        memset(a,x,sizeof(a))
#define ABS(x)          ((x)<0?-(x):(x))

#define SORT(v)         sort(v.begin(),v.end())
#define REV(v)          reverse(v.begin(),v.end())
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define FastRead        ios_base::sync_with_stdio(0);cin.tie(nullptr);
#ifdef VAMP
     #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template < typename Arg1 >
    void __f(const char* name, Arg1&& arg1){
        cout << name << " = " << arg1 << std::endl;
    }
    template < typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names+1, ',');
        cout.write(names, comma - names) << " = " << arg1 <<" | ";
        __f(comma+1, args...);
    }
#else
    #define debug(...)
#endif

const int N = 2005;
int n , m , t;
int A[N],B[N];
int main()
{
    #ifdef VAMP
        clock_t tStart = clock();
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    cin >> n >> m >> t;
    int pre = 0;
    int charge = n;
    for(int i = 1;i <= m;i++){
        cin >> A[i] >> B[i];
        int dif = B[i] - A[i];
        int loss = A[i] - pre;
        charge = charge - loss;
        if(charge <= 0){
            cout << "No\n";
            return 0;
        }
        charge += dif;
        charge = min(charge,n);
        pre = B[i];
    }

    int loss = t - pre;
    charge = charge - loss;
    if(charge <= 0){
        cout << "No\n";
    }else{
        cout << "Yes\n";
    }

    #ifdef VAMP
        fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
    #endif
}


#include<bits/stdc++.h>
using namespace std;
struct node
{
    long long a;
    long long b;
    long long c;
} nh[200005];
long long q[400005];
long long qq[800005];
int main()
{
    long long n,C;
    scanf("%lld%lld",&n,&C);
    int num=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld%lld%lld",&nh[i].a,&nh[i].b,&nh[i].c);
        num++;
        q[num]=nh[i].a;
        num++;
        q[num]=nh[i].b;
    }
    sort(q+1,q+num+1);
    int len=unique(q+1,q+num+1)-q;
    for(int i=1;i<=n;i++)
    {
        int l=1,r=len,ans1;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(q[mid]==nh[i].a)
            {
                ans1=mid;
                break;
            }
            else if(q[mid]>nh[i].a)
            {
                r=mid-1;
            }
            else
            {
                l=mid+1;
            }
        }
        l=1,r=len;
        int ans2;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(q[mid]==nh[i].b)
            {
                ans2=mid;
                break;
            }
            else if(q[mid]>nh[i].b)
            {
                r=mid-1;
            }
            else
            {
                l=mid+1;
            }
        }
        qq[2*ans1-1]+=nh[i].c;
        qq[2*ans2]-=nh[i].c;
    }
    long long now=0;
    long long ans=0;
    //printf("%d\n",len);
    for(int i=1;i<=2*len-1;i++)
    {
        //if(i%2==0) printf("%d %lld %lld %lld\n",i,now,q[i/2+1],q[i/2]);
        //else printf("%d %lld\n",i,now);
        now=now+qq[i];
        if(i%2==0) ans=ans+(q[i/2+1]-q[i/2]-1)*min(C,now);
        else ans=ans+min(C,now);
    }
    printf("%lld\n",ans);
}

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

#define fr(i,n) for(int i = 0; i<n; i++)
#define sz(v) (int)(v.size())
#define prin(a) cout << #a << " = " << a << endl
#define prinv(v) cout << #v << " = "; for(auto it : v) cout << it << ", "; cout << endl
#define all(v) (v).begin(),(v).end()

typedef long long ll;

#define rmin(a,b) a = min<ll>(a,b)
#define rmax(a,b) a = max<ll>(a,b)

#define fi first
#define se second

const int N = 2e5+10;
int dp_pego[N], dp_val[N], dp_qnt[N];

vector<int> g[N];
int mdist;

void dfs(int no, int from){
	//prin(no);
	int dist_prox_pego = INT_MAX/2, dist_longe_need = 0;
	dp_qnt[no] = 0;
	for(auto &it : g[no]){
		if(it==from) continue;
		dfs(it,no);
		dp_qnt[no]+=dp_qnt[it];
		if(dp_pego[it]){
			rmin(dist_prox_pego,dp_val[it]+1);
		} else{
			rmax(dist_longe_need,dp_val[it]+1);
		}
	}
	if(dist_prox_pego<=mdist){
		if(dist_prox_pego+dist_longe_need<=mdist){
			dp_pego[no] = 1;
			dp_val[no] = dist_prox_pego;
		} else{
			if(dist_longe_need==mdist){
				dp_qnt[no]++;
				dp_pego[no] = 1;
				dp_val[no] = 0;
			} else{
				dp_pego[no] = 0;
				dp_val[no] = dist_longe_need;
			}
		}
	} else{
		if(dist_longe_need==mdist){
			dp_qnt[no]++;
			dp_pego[no] = 1;
			dp_val[no] = 0;
		} else{
			dp_pego[no] = 0;
			dp_val[no] = dist_longe_need;
		}
	}
	if(no==0 and !dp_pego[no]) dp_qnt[no]++;
}

int need(int _mdist){
	mdist = _mdist;
	dfs(0,-1);
	return dp_qnt[0];
}

int main(){
	ios::sync_with_stdio(0); cin.tie(0);
	int n, k; cin >> n >> k;
	fr(i,n-1){
		int a, b; cin >> a >> b; a--,b--;
		g[a].push_back(b);
		g[b].push_back(a);
	}

	int lo = 1, hi = n;
	while(lo<hi){
		int mid = (lo+hi)/2;
		if(need(mid)<=k) hi = mid;
		else lo = mid+1;
	}
	cout << lo << "\n";
}


#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <functional>
#define ll long long
#define MAX_INT 100000
using namespace std;

int n;
ll a[310000];
ll ans = 0;
int main() {
	cin >> n;
	for (int i = 0; i < n; i++)cin >> a[i];
	ll sumA = 0;
	ll sumA2 = 0;
	for (int i = 0; i < n; i++) {
		sumA += a[i];
		sumA2 += a[i] * a[i];
	}
	cout << n * sumA2 - sumA * sumA << endl;
	return 0;
}

#include<iostream>
#include<vector>
#include<cmath>
#include<string>

using namespace std;
using ll = long long;

int main(){
    int n, tmp;
    int a_max=0, b_min=1000;

    cin >> n;

    for(int i=0; i<n; i++){
        cin >> tmp;
        if(a_max < tmp) a_max = tmp;
    }
    for(int i=0; i<n; i++){
        cin >> tmp;
	if(b_min > tmp) b_min = tmp;
    }

    if(b_min-a_max < 0) cout << 0;
    else cout << b_min-a_max+1;// << "," << b_min << "," << a_max;

    return 0;
}


#include <bits/stdc++.h>
//#define int ll
using namespace std;
typedef long long ll;
const int maxn=1e6+5;
const int mod=998244353;

void solve() {
    int a,b,c,d;
    int sum=0;
    cin>>a>>b>>c>>d;
    int x=abs(c-a);
    int y=abs(d-b);
    if(x==0&&y==0) {
        cout<<"0\n";
        return;
    }
    if(x==y||x+y<=3) {
        cout<<"1\n";
        return;
    }
    if(abs(x+y)%2==0||x+y<=6||abs(x-y)<=3) {
        cout<<"2\n";
        return;
    }
    cout<<"3\n";
}

signed main() {
    //ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//    freopen("1.in","r",stdin);
    int _ = 1;
    //cin >> _;
    //init();
    while (_--) {
        solve();
    }
    return 0;
}

#include <iostream>
using namespace std;
int main() {
  int a,b,c;
  cin>>a>>b>>c;
  if (c==0) {
    if(a>b) cout << "Takahashi" << endl;
   else cout << "Aoki" <<endl;
  }else{
  if (a<b)cout <<"Aoki"<<endl;
  else cout <<"Takahashi" <<endl;
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int>a;
map<int,int>m;
int main(){
    for(int i=0;i<=200005;i++) a.push_back(i);
    int x,n;cin>>n;
    while(n--){
        cin>>x;
        if(m[x]==0) {
            int pos = lower_bound(a.begin(), a.end(), x) - a.begin();
            a.erase(a.begin() + pos);
            m[x]=1;
        }
        cout<<a.front()<<endl;
    }
}

#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int main(){
    int n;
    cin >> n;
    vector<int> x(n);
    vector<int> y(n);
    for(int i=0; i<n; i++){
        cin >> x[i] >> y[i];
    }
    bool flg=false;
    for(int i=0; i<n; i++){
        int a1 = x[i];
        int b1 = y[i];
        for(int j=0; j<n; j++){
            int a2 = x[j];
            int b2 = y[j];
            if(i != j){
                for(int k=0; k<n; k++){
                    if( j != k && i != k ){
                        int a3 = x[k];
                        int b3 = y[k];
                        int ad21 = a2-a1;
                        int bd21 = b2-b1;
                        int ad31 = a3-a1;
                        int bd31 = b3-b1;
                        if(ad21 == 0 && ad31 == 0){
                            if(a1 == a3){
                                //cout << "i,j,k = " << i << " " << j << " " << k <<endl;
                                cout << "Yes" << endl;
                                return 0;
                            }else{
                                continue;
                            }
                        }
                        double tmp1 = (double) (b2-b1)/(a2-a1);
                        double tmp2 = (double) (b3-b1)/(a3-a1);
                        if(abs(tmp1 - tmp2) <= 1e-10){
                            //cout << "i,j,k = " << i << " " << j << " " << k <<endl;
                            //cout << tmp1 << " " << tmp2 << endl;
                            cout << "Yes" << endl;
                            return 0;
                        }
                    }
                }
            }
        }
    }
    cout << "No" << endl;
    return 0;
}


#include<bits/stdc++.h>
#include<fstream>

//#define DEBUG
#define rng(a) a.begin(),a.end()

using namespace std;

/* s_i(広告の面積) が r_i(与えられる値) に近ければ近いほどいい */

//50<=N<=200
int N;

//0<=x<=9999,0<=y<=9999,1<=r,rは整数,sum(r)=10000*10000
vector<int>x,y,r;

//space
int H,W;
vector<vector<bool>>space;

vector<vector<int>>ans;

#ifdef DEBUG
ifstream ifs;
ofstream ofs_input;
ofstream ofs_output;
#endif

void input(){

#ifdef DEBUG
  ifs>>N;
  x.resize(N);
  y.resize(N);
  r.resize(N);
  for(int i=0;i<N;i++)ifs>>x[i]>>y[i]>>r[i];
  ofs_input<<N<<endl;
  for(int i=0;i<N;i++)ofs_input<<x[i]<<" "<<y[i]<<" "<<r[i]<<endl;

#else
  cin>>N;
  x.resize(N);
  y.resize(N);
  r.resize(N);
  for(int i=0;i<N;i++)cin>>x[i]>>y[i]>>r[i];

#endif

}

void setup(){
  H=10001;
  W=10001;

  ans.resize(N);
  space.resize(H,vector<bool>(W,false));

#ifdef DEBUG
  fill(rng(ans),vector<int>(4,0));
  fill(rng(space),vector<bool>(W,false));
#endif

}

double double_score(){
  double score=0;
  for(int i=0;i<N;i++){
    int a=ans[i][0];
    int b=ans[i][1];
    int c=ans[i][2];
    int d=ans[i][3];
    if(a<=x[i]&&x[i]<b&&c<=y[i]&&y[i]<d){
      int s=abs(a-b)*abs(c-d);
      int MIN=min(s,r[i]);
      int MAX=max(s,r[i]);
      double p=1-(1-1.*MIN/MAX)*(1-1.*MIN/MAX);
      score+=p;
    }
  }
  return score;
}

int main(){

#ifdef DEBUG
  for(int debug=0;debug<1;debug++){
    stringstream filename;
    filename<<setw(4)<<setfill('0')<<debug;
    ifs.open("../tools/in/"+filename.str()+".txt");
    ofs_input.open(filename.str()+"_input"+".txt");
    ofs_output.open(filename.str()+"_output"+".txt");
    cout<<"-------------------------------"<<endl;
    cout<<filename.str()+".txt"<<endl;
    cout<<"-------------------------------"<<endl;
#endif

  input();
  setup();

  for(int i=0;i<N;i++){
    ans[i]={x[i],y[i],x[i]+1,y[i]+1};
  }
  //cout<<"double_score:"<<double_score()<<endl;
  for(auto it:ans){
    for(auto it:it)cout<<it<<" ";
    cout<<endl;
  }
#ifdef DEBUG
  for(auto it:ans){
    for(auto it:it)ofs_output<<it<<" ";
    ofs_output<<endl;
  }
  ifs.close();
  ofs_input.close();
  ofs_output.close();
  }
#endif

}

#include "bits/stdc++.h"
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
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; }

ll n;
vector<ll> x, y, r;

const ll LIM = 10000;

struct STATE {
	vector<P> loc;
	vector<P> area;
};

void init(STATE &state){
	state.loc.resize(n);
	state.area.resize(n);
	rep(i,n){
		state.loc[i] = P(0, i);
		state.area[i] = P(1, 1);
	}
}

bool isOverlapped(STATE &state, ll j, ll X, ll Y){
	vector<P> now(4);
	now[0] = P(y[j], x[j]);
	now[1] = P(y[j], x[j] + X);
	now[2] = P(y[j] + Y, x[j] + X);
	now[3] = P(y[j] + Y, x[j]);
	rep(i,n){
		if(i == j) continue;
		ll L = x[i];
		ll R = x[i] + state.area[i].second;
		ll U = y[i];
		ll D = y[i] + state.area[i].first;
		rep(k,4){
			if(now[k].first < U) continue;
			if(D < now[k].first) continue;
			if(now[k].second < L) continue;
			if(R < now[k].second) continue;
			return true;
		}
		if(x[j] <= L && R <= x[j] + X && !(D <= y[j]) && !(y[j] + Y <= U)) return true;
		if(y[j] <= U && D <= y[j] + Y && !(R <= x[j]) && !(x[j] + X <= L)) return true;
	}
	return false;
}

#include <random>

void modify(STATE &state){
	std::random_device rnd;
	std:: mt19937 mt(rnd());
	std::uniform_int_distribution<> randN(0,n-1);
	int j = randN(mt);
	std::uniform_int_distribution<> randH(1,r[j]);
	ll h = randH(mt);
	if(y[j] + h > LIM) return;
	ll w = r[j] / h;
	if(x[j] + w > LIM) return;
	if(isOverlapped(state, j, w, h)) return;
	state.loc[j] = P(y[j], x[j]);
	state.area[j] = P(h, w);
}

ll calc_score(STATE &state){
	ll score = 0;
	rep(i,n){
		ll R = state.area[i].first * state.area[i].second;
		ll tmp = (min(r[i], R) * 100) / (max(r[i], R) * 100);
		score += 10000 - (100 - tmp) * (100 - tmp);
	}
	score /= n;
	return score;
}

void solve(){
	STATE state;
	init(state);
	const long double TL = 4.8;
	while(true){
		long double terminal = (long double)clock() / (long double)CLOCKS_PER_SEC;
		if(terminal >= TL) break;
		modify(state);
	}
	rep(i,n){
		ll y = state.loc[i].first;
		ll x = state.loc[i].second;
		ll dy = state.area[i].first;
		ll dx = state.area[i].second;
		cout << x << " " << y << " " << x+dx << " " << y+dy << endl;
	}
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cin >> n;
	rep(i,n){
		ll X, Y, R;
		cin >> X >> Y >> R;
		x.push_back(X);
		y.push_back(Y);
		r.push_back(R);
	}
	solve();
	return 0;
}


#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
using namespace std;
typedef long long ll;

double xs[200000+10];
double ys[200000+10];

bool g[105][105];
int visited[105];
int counter;

int main(void) {
  int n;
  scanf("%d", &n);
  REP(i, n) {
    scanf("%lf%lf", &xs[i], &ys[i]);
  }
  int start = n;
  int goal = n+1;
  double left = 0;
  double right = 100;
  REP(iTry, 100) {
    double mid = (left + right) / 2;
    REP(i, n) {
      g[i][i] = true;
      g[i][start] = g[start][i] = (ys[i] + 100) <= 2*mid;
      g[i][goal] = g[goal][i] = (100 - ys[i]) <= 2*mid;
      REP(j, i) {
        g[i][j] = g[j][i] = sqrt((xs[i]-xs[j])*(xs[i]-xs[j]) + (ys[i]-ys[j])*(ys[i]-ys[j])) <= 2*mid;
      }
    }
    ++counter;
    queue<int> q;
    q.push(start);
    visited[start] = counter;
    while(!q.empty()){
      int cur = q.front(); q.pop();
      REP(i, n+2) {
        if(g[cur][i] && visited[i] != counter) {
          q.push(i);
          visited[i] = counter;
        }
      }
    }
    if(visited[goal] == counter) {
      right = mid;
    } else {
      left = mid;
    }
  }
  printf("%.10f\n", left);

  return 0;
}

#include <bits/stdc++.h>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
#define ll long long
//#define file
using namespace std;

int n,m,i,j,k,l,x,y,z;
int fa[100001];
int a[200001][3],ls[100001],len;
int ans[100001];

void New(int x,int y,int z) {++len;a[len][0]=y;a[len][1]=ls[x];ls[x]=len;a[len][2]=z;}

int gf(int t) {if (fa[t]==t) return fa[t];fa[t]=gf(fa[t]);return fa[t];}
void dfs(int Fa,int t,int s,int Ls)
{
	int i;
	if (t==1) ans[t]=1;
	else
	{
		if (s==Ls)
		{
			if (s==1) ans[t]=2;
			else ans[t]=1;
		}
		else ans[t]=Ls;
	}

	for (i=ls[t]; i; i=a[i][1])
	if (a[i][0]!=Fa)
	dfs(t,a[i][0],ans[t],a[i][2]);
}

int main()
{
	#ifdef file
	freopen("c.in","r",stdin);
	#endif

	scanf("%d%d",&n,&m);
	fo(i,1,n) fa[i]=i;
	fo(i,1,m)
	{
		scanf("%d%d%d",&x,&y,&z);
		if (gf(x)!=gf(y))
		{
			fa[fa[x]]=fa[y];
			New(x,y,z),New(y,x,z);
		}
	}

	dfs(0,1,-1,0);
	fo(i,1,n) cout<<ans[i]<<endl;

	fclose(stdin);
	fclose(stdout);
	return 0;
}


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

using ll = long long int;
using ull = unsigned long long int;
using P = pair<int, int>;
using P3 = pair<int,P>;
using PP = pair<P, P>;
constexpr int INF32 = 1 << 29;
constexpr ll INF64 = 1LL << 61;
constexpr ll MOD = 1000000007;
// constexpr ll MOD = 998244353;
constexpr int di[] = {0, 1, 0, -1};
constexpr int dj[] = {1, 0, -1, 0};
constexpr int di8[] = {0, 1, 1, 1, 0, -1, -1, -1};
constexpr int dj8[] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr double EPS = 1e-10;
const double PI = acos(-1);

#define ALL(v) (v).begin(),(v).end()
#define REP(i,n) for(int i=0,i_len=n; i<i_len; ++i)

template<typename T1,typename T2> bool chmax(T1 &a, T2 b) { if (a<b) { a=b; return 1; } return 0; }
template<typename T1,typename T2> bool chmin(T1 &a, T2 b) { if (b<a) { a=b; return 1; } return 0; }

int main(){
    int h, w;
    cin >> h >> w;
    vector<string> s(h);
    for(int i=0;i<h;i++) cin >> s[i];
    vector<vector<int> > dp(h, vector<int>(w));
    for(int i=h-1;i>=0;i--){
        for(int j=w-1;j>=0;j--){
            if(i==h-1 && j==w-1) continue;
            if((i+j)&1){
                int res = -INF32;
                if(i+1<h) chmax(res,dp[i+1][j]+(s[i+1][j]=='+' ? 1 : -1));
                if(j+1<w) chmax(res,dp[i][j+1]+(s[i][j+1]=='+' ? 1 : -1));
                dp[i][j] = res;
            }else{
                int res = INF32;
                if(i+1<h) chmin(res,dp[i+1][j]+(s[i+1][j]=='-' ? 1 : -1));
                if(j+1<w) chmin(res,dp[i][j+1]+(s[i][j+1]=='-' ? 1 : -1));
                dp[i][j] = res;
            }
        }
    }
    string p[] = {"Takahashi", "Aoki"};
    if(dp[0][0] > 0){
        cout << p[1] << endl;
    }else if(dp[0][0] < 0){
        cout << p[0] << endl;
    }else{
        cout << "Draw" << endl;
    }

    return 0;
}

//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
#include <bits/stdc++.h>
#include<vector>
#include<iostream>
#include<set>
#include<string>
#include <array>
using namespace std;
#define ll long long
#define endl '\n'
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define con continue
#define ub upper_bound
#define lb lower_bound

#define si(x) int(x.size())
#define sum_all(a)     ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a)    (*min_element((a).begin(), (a).end()))
#define maxe(a)    (*max_element((a).begin(), (a).end()))
#define mini(a)    ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a)    ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
const double pi = 2 * acos(0.0);
const int dx[] = { -1, 0, 1, 0 };
const int dy[] = { 0, -1, 0, 1 };

const int dx8[] = { -1, 0, 1, 0,1,1,-1,-1 };
const int dy8[] = { 0, -1, 0, 1,1,-1,1,-1 };

ll min(ll a, ll b)
{
  if (a < b)return a;
  return b;
}
ll max(ll a, ll b)
{
  if (a > b)return a;
  return b;
}
ll ceil1(ll a, ll b)
{
  return(a + b - 1) / b;
}
void read(vector<ll> & arr)
{
  for (ll i = 0; i < si(arr); i++)
    cin >> arr[i];

}

void read_graph(vector<vector<ll>>& g, ll m)
{
  while (m--)
  {
    ll x, y; cin >> x >> y;
    x--, y--;
    g[x].pb(y);
    g[y].pb(x);
  }
}
void solve()
{
  int n, m;
  cin >> n >> m;

  vector<string> arr(n);

  for (int i = 0; i < n; i++) cin >> arr[i];
  vector<vector<pair<int, int>>> dp(n, vector<pair<int, int>>(m));

  for (int i = n - 1; i >= 0; i--)
    for (int j = m - 1; j >= 0; j--)
    {

      vector<pair<ll, pair<ll, ll>>> temp;
      if (i + 1 < n)
      {
        temp.pb({ dp[i + 1][j].ss - dp[i + 1][j].ff,{dp[i + 1][j].ff,dp[i + 1][j].ss} });
        if (arr[i + 1][j] == '+')
        {
          temp[0].ss.ss++;
          temp[0].ff++;
        }
        else
        {
          temp[0].ss.ss--;
          temp[0].ff--;
        }
      }
      if (j + 1 < m)
      {
        temp.pb({ dp[i][j + 1].ss - dp[i][j + 1].ff,{dp[i][j + 1].ff,dp[i][j + 1].ss} });
        if (arr[i][j + 1] == '+')
        {
          temp.back().ss.ss++;
          temp.back().ff++;
        }
        else
        {
          temp.back().ss.ss--;
          temp.back().ff--;
        }
      }

      if (si(temp) == 0) continue;

      if(si(temp)!=1)
      if (temp[0].ff < temp[1].ff) swap(temp[0], temp[1]);
      dp[i][j].ff += temp[0].ss.ss;
      dp[i][j].ss += temp[0].ss.ff;
    }
  string res = "Draw";
  if (dp[0][0].ff > dp[0][0].ss) res = "Takahashi";
  else if (dp[0][0].ff < dp[0][0].ss)res = "Aoki";
  cout << res << endl;

}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  #ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
      freopen("output.txt", "w", stdout);
  #endif
    //int t; cin >> t; while (t--)
  solve();

}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define ve vector<ll>
#define ve_pa vector<pair<ll,ll> >
#define pb push_back
#define mod (ll)998244353
#define INF (ll)1e18+7
#define ld long double
#define pll pair<ll,ll>
#define fi first
#define se second
#define mll map<ll,ll>
#define m_ve map<ll,ve>
#define min3(x,y,z) min(x,min(y,z))
#define max3(x,y,z) max(x,max(y,z))
#define W(x) while(x--)
#define ld long double
#define all(x) x.begin(),x.end()
#define popcount(x) __builtin_popcountll(x)
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
//int dx[]={0,1,-1,0,0,1,-1,-1,1},dy[]={0,0,0,1,-1,1,-1,1,-1};
const int N=100005;
vector<pair<ll,pll> >g[N];
ll dist[N],v[N];
ll n,m;
ll solve()
{
	for(int i=0;i<=n;i++)
	{
	dist[i]=INF;
	v[i]=0;
	}
	set<pll>s;
	s.insert({0,1});
	dist[1]=0;
	while(s.size())
	{
		pll x=*s.begin();
		s.erase(s.begin());
		if(v[x.se])
		continue;
		v[x.se]=1;
		for(auto x1:g[x.se])
		{
			ll ch=x1.fi;
			ll c=x1.se.fi;
			ll d=x1.se.se;
			ll w=dist[x.se];
				for(ll t=sqrt(d)-2;t<=sqrt(d)+2;t++)
				{
					ll nw=max(w,t);
					if(nw+c+d/(nw+1)<dist[ch])
					{
						dist[ch]=nw+c+d/(nw+1);
						s.insert({dist[ch],ch});
					}
				}

		}
	}
	return dist[n];
}

int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;//cin>>t;
t=1;
while(t--)
{
	cin>>n>>m;
	for(int i=0;i<m;i++)
	{
		ll u,v,c,d;cin>>u>>v>>c>>d;
		g[u].pb({v,{c,d}});
		g[v].pb({u,{c,d}});
	}
	ll ans=INF;
	ans=solve();
	if(ans==INF)
	ans=-1;
	cout<<ans<<"\n";
}

}

// E - Rush Hour 2
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define vec vector
using vl = vec<ll>;
#define rep(i,e) for(int i=0;i<(e);++i)
using PR = pair<ll,int>;

// 本問用に修正
// dijkstra O(|E|log|V|)
int V;
vec<tuple<int,int,int,int>> E;
vec<vec<int>> G;
ll INF = 1e18;

vl dijkstra(int s){
	vl dist(V, INF);
	dist[s] = 0;
	priority_queue<PR, vec<PR>, greater<PR>> pq;
	pq.push({0, s});
	while(pq.size()){
		auto[t, v] = pq.top(); pq.pop();
		if(dist[v] < t) continue;
		for(int&i:G[v]){
			auto&[a, b, c, d] = E[i];
			int u = a==v? b:a;
			ll k = round(sqrt(d)) - 1;
			if(k <= t) k = t;
			ll nt = k + c + d / (k+1);
			if(dist[u] <= nt) continue;
			dist[u] = nt;
			pq.push({nt, u});
		}
	}
	return dist;
}

int main(){
	int m; cin>>V>>m;
	G.resize(V);
	rep(i, m){
		int a, b, c, d; cin>>a>>b>>c>>d; --a, --b;
		E.emplace_back(a, b, c, d);
		G[a].push_back(i), G[b].push_back(i);
	}
	ll ans = dijkstra(0)[V-1];
	if(ans == INF) ans = -1;
	cout<< ans <<endl;
}


//#include <atcoder/all>
#include <bits/stdc++.h>
#include <cassert>
#include <numeric>
using namespace std;
//using namespace atcoder;
typedef  long long LONG;
const int MOD = 1000000007;

int gcd(int x, int y) {
	if (x % y == 0) {
		return y;
	}
	else {
		return gcd(y, x % y);
		//x%y==0でないときはユーグリットの互除法を使って再帰的に関数を呼び出す。
	}
}
LONG pow2(long long x, long long n) {
	long long ret = 1;
	while (n > 0) {
		if (n & 1) ret = ret * x % MOD;  // n の最下位bitが 1 ならば x^(2^i) をかける
		x = x * x % MOD;
		n >>= 1;  // n を1bit 左にずらす
	}
	return (int)ret;
}
void printVector(const vector<int>& vec) {
	for (int value : vec) {
		cout << value << " ";
	}
	cout << endl;
}
LONG t(LONG N, LONG i) {
	return N - abs(i);
}
int count(vector<int>&a, int s) {
	int result = 0;
	for (int i = 0; i < a.size(); i++) {
		if (a[i] == s) {
			result++;
		}
	}
	return result;
}

LONG kaizyo(int N) {
	LONG result = 1;
	LONG mod = 998244353;

	for (LONG i = 2; i <= N; i++) {
		result *= i;
		result = result % mod;
	}
	return result;
}
class Point {
public:
	int x;
	int y;
};
LONG stock[3500][3500];
LONG test(int N, int task) {
	LONG mod = 998244353;
	if (N < task)return 0;
	if (N < 0)return 0;
	if (task == 0) {
		if (N == 0) {
			return 1;
		}
		else {
			return 0;
		}

	}

	if (stock[N][task] > 0)return stock[N][task];
	stock[N][task] = (test(N - 1, task - 1) + test(N, task * 2)) % mod;

	return stock[N][task] % mod;
}
class DP {
public:
	DP() {

	}
	DP(int a, int b) {
		other = a;
		path = b;

	}
	int other;
	int path;
};

void dp(vector<vector<DP>>& list, vector<int> &able, int pos, int limit) {
	for (int i = 0; i < list[pos].size(); i++) {
		if (list[pos][i].path <= limit) {
			if (able[list[pos][i].other] == 0) {
				able[list[pos][i].other] = 1;
				dp(list, able, list[pos][i].other, limit);
			}
		}
	}
}
LONG max(deque<LONG>q) {
	LONG r = -1;
	for (int i = 0; i < q.size(); i++) {
		if (r < q[i]) {
			r = q[i];
		}
	}
	return r;
}
LONG min(deque<LONG>q) {
	LONG r = LLONG_MAX;
	for (int i = 0; i < q.size(); i++) {
		if (r > q[i]) {
			r = q[i];
		}
	}
	return r;
}
int main() {
	LONG n, n2, n3;
	cin >> n;
	int a = int((-0.5 + sqrt(0.25 + 2 * (n + 1))));
	for (LONG i = a;; i++) {
		if (i*(i + 1) > 2 * (n+1)) {
			cout << n - i + 2 << endl;
			return 0;

		}
	}
	return 0;

	return 0;

}

#include<bits/stdc++.h>
using ull = unsigned long long;
using namespace std;

ull solve1(ull n) {
  ull valid = 0;
  ull invalid = INT32_MAX;
  while (invalid - valid > 1) {
    ull mid = (valid + invalid) / 2;
    if (mid * (mid + 1) / 2 <= n + 1) valid = mid;
    else invalid = mid;
  }
  return n + 1 - valid;
}

ull solve2(ull n) {
  ull valid = 0;
  ull width = INT32_MAX;
  while (width > 1) {
    ull mid = valid + width / 2;
    if (mid * (mid + 1) / 2 <= n + 1) valid = mid;
    width -= width / 2;
  }
  return n + 1 - valid;
}

const int CNT = 10000000;
int main() {
  ull n;
  cin >> n;
  ull res = 0;
  for (int i = 0; i < CNT; i++) {
    res += solve2(n);
  }
  cerr << res << endl;
  cout << solve2(n) << endl;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<long long, long long>;
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007;
constexpr long long INF = 1000000000 + 100;
constexpr long long LINF = 1000000000000000000 + 100;
#define all(v) v.begin( ), v.end()
#define rep(i, n) for(int i=0;i<(n);i++)
#define rept(i, j, n) for(int i=(j); i<(n); i++)
#define repr(i, n) for(int i=(n); i>=0; i--)
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; }

ll tri(ll n){
    return n * (n + 1) / 2;
}

int main(){
    ll N, K; cin >> N >> K;
    for(int s=0; s<=3*(N-1); s++){
        ll cnt;
        if(s <= N-1){
            cnt = tri(s+1);
        }
        else if(s <= 2 * (N - 1)){
            cnt = N * N - tri(s - N + 1) - tri(2 * N - s - 2);
        }
        else{
            cnt = tri(3 * N - 2 - s);
        }
        if(K <= cnt){
            rep(x, N){
                ll c;
                if(s <= N - 1){
                    c = max(0, s + 1 - x);
                }
                else if(s <= 2 * (N - 1)){
                    c = N - abs(x - (s - N + 1));
                }
                else{
                    c = max(0LL, 2 * N - 1 - s + x);
                }
                if(K <= c){
                    rep(y, N){
                        if(x + y >= s - (N - 1) and x + y <= s){
                            if(K == 1){
                                cout << x+1 << " " << y+1 << " " << s-x-y+1 << ln;
                                return 0;
                            }
                            K--;
                        }
                    }
                }
                K -= c;
            }
        }
        K -= cnt;
    }
}

#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
#define base 31
#define MOD 1e9+7
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORs(i, a,b) for(int i = a; i >= b; i--)
#define REP(i, a, b) for(int i = a; i < b; i++)
#define __FOR(i, a, b, k) for(int i = a; i <= b; i+= k)
#define __REP(i, a, b, k) for(int i = a; i < b; i+= k)
#define pb push_back
#define _io ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define Read_ freopen("input.inp", "r", stdin)
#define Write_ freopen("output.out", "w", stdout);
using namespace std;
typedef pair <int, int> ii;
typedef vector <ii> vii;
#define fi first
#define se second
#define mp make_pair
#define N  5005
using namespace std;
int a,b,c,d;
int main()
{

    cin>>a>>b;
  cin>>c>>d;
  cout<<a*d-b*c;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0; i<n; i++)
#define eb(t) emplace_back(t)
typedef long long ll;
typedef long long unsigned int llu;
ll INF = 1000000009;
ll MOD = 1000000007;

char a[12];
char b[12];

ll f(ll x){
    string st = to_string(x);
    ll n = st.size();
    rep(i,n){
        a[i] = st[i];
        b[i] = st[i];
    }
    sort(a,a+n);
    sort(b,b+n,greater<>());
    ll ans_a = 0;
    ll ans_b = 0;
    rep(i,n){
        ans_a *= 10;
        ans_a += a[i]-'0';
        ans_b *= 10;
        ans_b += b[i]-'0';
    }
    return ans_b-ans_a;
}

void solve(){

    ll N,k;
    cin >> N >> k;
    ll p = N;
    rep(i,k){
        p = f(p);
    }
    cout << p << endl;

}

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

#include<bits/stdc++.h>
using namespace std;
typedef     long long    ll;
typedef     vector<int> vi;
typedef     vector<long long> vl;
typedef     pair<int, int>pi;
typedef     pair<long long, long long>pl;
#define F   first
#define S   second
#define pb  push_back
#define     all(x)      x.begin() , x.end()
#define mp  make_pair
#define     REP(i,a,b) for(i=a;i<=b;i++)
#define     mem(a)      memset(a , 0 ,sizeof a)
#define     memn(a)     memset(a , -1 ,sizeof a)

int main()
{
  int t = 1;
// scanf("%d", &t);
  while (t--)
  {
    ll n,i,j,x,y,k,p;
    cin>>n>>k;

    for(ll j=1;j<=k;j++)
    {
       vector<ll>v;
       p=n;
        while(p!=0)
        {
          v.pb(p%10);
          p/=10;
        }
        sort(all(v));
         x=0;
       for(i=0;i<v.size();i++)
       {
         x=(x*10)+v[i];
       }
         y=0;
       for(i=v.size()-1;i>=0;i--)
       {
         y=(y*10)+v[i];
       }
       //cout<<x<<" "<<y<<endl;
       n=abs(y-x);
       if(n==0) break;

    }
    cout<<n<<endl;

}
}