#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
constexpr ll MOD=998244353;
constexpr ll INF=2e18;

int main(){
    int n; cin >> n;
    vector<P> p(n);
    REP(i,n) cin >> p[i].second >> p[i].first;
    sort(ALL(p));
    int now=-1;
    vector<P> q;
    REP(i,n){
        if(i==0||p[i-1].first!=p[i].first){
            now++;
            q.push_back({p[i].second,p[i].second});
        }
        else{
            q[now].first=min(q[now].first,p[i].second);
            q[now].second=max(q[now].second,p[i].second);
        }
    }
    int m=q.size();
    VVI dp(m+1,VI(2,INF));
    REP(i,m){
        if(i==0){
            dp[i+1][0]=abs(0-q[i].second)+abs(q[i].first-q[i].second);
            dp[i+1][1]=abs(0-q[i].first)+abs(q[i].first-q[i].second);
        }
        else{
            dp[i+1][0]=min(dp[i][0]+abs(q[i-1].first-q[i].second)+abs(q[i].first-q[i].second),dp[i][1]+abs(q[i-1].second-q[i].second)+abs(q[i].first-q[i].second));
            dp[i+1][1]=min(dp[i][0]+abs(q[i-1].first-q[i].first)+abs(q[i].first-q[i].second),dp[i][1]+abs(q[i-1].second-q[i].first)+abs(q[i].first-q[i].second));
        }
    }
    cout << min(dp[m][0]+abs(q[m-1].first),dp[m][1]+abs(q[m-1].second)) << endl;
    return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//turn on extra precision
//#pragma GCC target("fpmath=387")

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef string str;
typedef pair <int,int> pii;
typedef pair <ll,ll> pll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define mp make_pair
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define fi first
#define se second
#define fs first.second
#define ss second.second
#define ff first.first
#define sf second.first
#define newl '\n'
#define fbo find_by_order
#define ook order_of_key
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define watch(x) cout << (#x) << " is : " << (x) << newl

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
vi dirx = {0,0,1,-1};
vi diry = {1,-1,0,0};
char to_upper (char x){
    if( 97 <= int(x) && int(x) <= 122) return char(x-32);
    if( 65 <= int(x) && int(x) <= 90) return x;
    return -1;
}
char to_lower (char x){
    if( 97 <= int(x) && int(x) <= 122) return x;
    if( 65 <= int(x) && int(x) <= 90) return char(x+32);
    return -1;
}
int numerize (char x){
    if(48 <= int(x) && int(x) <= 57) return int(x-'0');
    if(97 <= int(x) && int(x) <= 122) return int(x-96);
    if(65 <= int(x) && int(x) <= 90) return int(x-64);
    return -1;
}
bool isect (int l1, int r1, int l2, int r2){ return max(l1,l2) <= min(r1,r2); }
ll quickpow (ll num1, ll num2, ll MOD){
    if(num2==0)return 1%MOD;
    else if(num2==1)return num1%MOD;
    else{
        ll temp = quickpow (num1,num2>>1LL,MOD); ll res = ((temp%MOD) * (temp%MOD))%MOD;
        if(num2&1) res = ((res%MOD)*(num1%MOD))%MOD; return res;
    }
}
ll invmod (ll num, ll MOD){return quickpow (num,MOD-2,MOD);}
ll gcd (ll num1, ll num2){
    if(num1 < num2) swap(num1,num2); ll num3 = num1 % num2 ;
    while(num3 > 0){ num1 = num2; num2 = num3; num3 = num1 % num2;}
    return num2;
}
ll lcm (ll num1 , ll num2){return (ll) (num1/__gcd(num1,num2))*num2;}
// end of Template

int n, c[200010], x[200010];
ll dp[200010][2];
pii p[200010];
void compress(){
    set <int> s;
    for(int i = 1; i <= n; ++i) s.insert(c[i]);
    map <int,int> trans;
    int idx = 0;
    for(auto i : s) trans[i] = ++idx;
    for(int i = 1; i <= n; ++i) c[i] = trans[c[i]];
    for(int i = 1; i <= n; ++i){
        if(p[c[i]] == mp(0,0)){ p[c[i]] = mp(x[i],x[i]); }
        else {p[c[i]].se = max(p[c[i]].se, x[i]); p[c[i]].fi = min(p[c[i]].fi, x[i]); }
    }
    n = idx;
    return;
}
ll cost(ll a, ll b, ll c){
    return abs(a-b) + abs(b-c);
}
int main(){

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

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n;
    for(int i = 1; i <= n; ++i) cin >> x[i] >> c[i];
    compress();

    dp[0][0] = 0, dp[0][1] = 0;
    for(int i = 1; i <= n; ++i){
        dp[i][0] = min(dp[i-1][0] + cost(p[i-1].fi,p[i].se,p[i].fi) , dp[i-1][1] + cost(p[i-1].se,p[i].se,p[i].fi));
        dp[i][1] = min(dp[i-1][0] + cost(p[i-1].fi,p[i].fi,p[i].se) , dp[i-1][1] + cost(p[i-1].se,p[i].fi,p[i].se));
    }

    ll ans = min(dp[n][0] + abs(p[n].fi) , dp[n][1] + abs(p[n].se));
    cout << ans << newl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long i64;
typedef unsigned long long ui64;
typedef vector<i64> vi;
typedef vector<vi> vvi;
typedef pair<i64, i64> pi;

#define pb push_back
#define sz(a) i64((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for(i=(s); i < (e); ++i)
inline void RI(i64 &i) {scanf("%lld", &(i));}
inline void RVI(vi &v) { for(i64 i=0;i<sz(v);++i) { RI(v[i]); } }
inline void RVVI(vvi &vv) { for(i64 i=0;i<sz(vv);++i) { RVI(vv[i]); } }
inline void WI(const i64 &i) {printf("%lld\n", i);}
inline void WVI(const vi &v, char sep=' ') { for(i64 i=0;i<sz(v);++i) { if(i != 0){ printf("%c", sep); } printf("%lld", v[i]);} printf("\n"); }
inline void WS(const string &s) { printf("%s\n", s.c_str()); }
inline void WB(bool b, const string &yes, const string &no) { if(b){ WS(yes);} else { WS(no);} }
inline void YESNO(bool b) { WB(b, "YES", "NO"); }
inline void YesNo(bool b) { WB(b, "Yes", "No"); }
#define BUF_LENGTH 1000000
inline void RS(string &s) {static char buf[BUF_LENGTH]; scanf("%s", buf); s = buf;}
template<typename T> inline bool IN(T &S, const typename T::key_type &key) {
  return S.find(key) != S.end();
}
template<typename T> inline bool ON(const T &b, i64 idx) {
  return ((T(1) << idx) & b) != 0;
}

i64 dfs(const vvi &edges, i64 cur, i64 prev, i64 n, const vi &to, vi &ans, bool f) {
  // start from v
  //cerr << "dfs " << cur << " " << prev << " " << n << endl;
  ans[cur] = n;
  for(auto &e: edges[cur]) {
    if(e != prev && e != to[cur]) {
      n += 1;
      n = dfs(edges, e, cur, n, to, ans, false);
      n += 1;
    }
  }
  if(f && to[cur] != -1) {
    n += 1;
    n = dfs(edges, to[cur], cur, n, to, ans, true);
  }
  return n;
}

int main(int argc, char *argv[]) {
  i64 i, j, k;
  i64 t, T;
  i64 N; cin >> N;
  vvi edges(N);
  REP(0, N-1, i) {
    i64 a, b; RI(a); RI(b);
    --a; --b;
    edges[a].pb(b);
    edges[b].pb(a);
  }

  const i64 INF = N * 2;

  auto bfs = [&](i64 s) {
    vi dists(N, INF);
    vi from(N, -1);
    dists[s] = 0;
    queue<i64> q;
    q.push(s);
    while(!q.empty()) {
      i64 cur = q.front(); q.pop();
      for(auto &e: edges[cur]) {
        if(dists[e] == INF) {
          dists[e] = dists[cur] + 1;
          from[e] = cur;
          q.push(e);
        }
      }
    }
    /*
    for(auto &d: dists) {
      cerr << d << " ";
    }
    cerr << endl;
    for(auto &d: from) {
      cerr << d << " ";
    }
    cerr << endl;
    */
    return pair<vi, vi> {dists, from};
  };

  auto res_0 = bfs(0);
  vi &d0 = res_0.first;
  i64 u = -1;
  REP(0, N, i) {
    if(u == -1 || d0[u] < d0[i]) {
      u = i;
    }
  }
  assert(u != -1);
  auto res_u = bfs(u);
  vi &du = res_u.first;
  vi &fu = res_u.second;
  i64 v = -1;
  REP(0, N, i) {
    if(v == -1 || du[v] < du[i]) {
      v = i;
    }
  }
  assert(v != -1);
  //cerr << "diameter " << du[v] << " " << u << " -> " << v << endl;

  vi ans(N, -1);
  dfs(edges, v, -1, 1, fu, ans, true);
  WVI(ans);

  return 0;
}

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

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>

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

using namespace std;

class ETraveler {
public:
    void solve(std::istream& cin, std::ostream& cout) {
        int N;
        cin >> N;
        vector<pair<int, int>> v(N);
        REP(i, N) {
            int x, c;
            cin >> x >> c;
            v[i] = {c, x};
        }
        sort(v.begin(), v.end());
        vector<pair<int, int>> LR;
        int prev = v[0].first;
        int l = v[0].second;
        int r = v[0].second;
        REP(i, v.size()) {
            if (v[i].first != prev) {
                LR.emplace_back(l, r);
                l = r = v[i].second;
            } else {
                l = min(l, v[i].second);
                r = max(r, v[i].second);
            }
            prev = v[i].first;
        }
        LR.emplace_back(l, r);
        LR.emplace_back(0, 0);
        vector<vector<int>> dp(LR.size() + 1, vector<int>(2, INF));
        dp[0][0] = dp[0][1] = 0;
        REP(i, LR.size()) {
            int L = LR[i].first;
            int R = LR[i].second;
            int PL = i > 0 ? LR[i - 1].first : 0;
            int PR = i > 0 ? LR[i - 1].second : 0;
            dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + abs(PL - R) + R - L);
            dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + abs(PR - R) + R - L);
            dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + abs(PL - L) + R - L);
            dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + abs(PR - L) + R - L);
        }
        cout << min(dp[LR.size()][0], dp[LR.size()][1]) << endl;
    }
};

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

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


#include <bits/stdc++.h>
#define union Union
#define rank rank228
#define tm tm228
#define hash hash228
#define matr vector < vector < int > >
#define int long long
#define pii pair < int, int >
#define papa pair < node*, node* >
#define all(v) v.begin(), v.end()
#define endl "\n"
#define pb push_back
#define PI 3.14159265358979323846
#define ff first
#define ss second
#define mp make_pair

using namespace std;

const int inf = 1e18, maxn = 1e6 + 100, mod1 = 1e9 + 7, mod2 = 998244353;

int mood1(int x)
{
    x %= mod1;
    if (x < 0)
        x += mod1;
    return x;
}

int mood2(int x)
{
    x %= mod2;
    if (x < 0)
        x += mod2;
    return x;
}

int a[maxn];

void solve(){
    int ans = 0, now = 0, pref = 0, mpref = 0;
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i];
        now += mpref;
        ans = max(ans, now);
        pref += a[i];
        now += pref - mpref;
        mpref = max(pref, mpref);
        ans = max(ans, now);
    }
    cout << ans;
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    srand(100);
    cout << fixed << setprecision(10);
    int t = 1;
    #ifdef LOCAL
        freopen("task.in", "r", stdin);
    #endif
    //cin >> t;
    while (t--)
    {
        solve();
        cout << endl;
    }
}

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

#define asll unsigned long long
#define ll long long
#define ld long double
#define imt int
#define pii pair<int,int>
#define st first
#define nd second
#define psbk(x) push_back(x);
#define For(x,a,b) for(int x=(a);x!=(b+((b-a)/abs(b-a)));x+=(b-a)/abs(b-a))
#define FOR(x,n) for(int x=0;x<(n);x++)
#define m_p make_pair

#define kj <<" "<<
#define kjb <<" ";
#define dg <<","<<
#define ml <<endl;
#define co cout<<
#define ter cout<<endl;
#define sibi cout<<"TES"<<endl;
#define tes(x) cout<<"TES"<<x<<endl;
#define kpr(x) cout<<"("<<x.st dg x.nd <<")" ml
#define kkor(p) cout<<"("<<p.x dg p.y <<")" ml
#define kera(x,m) for( int i = 0;i < m ; i++ ){ cout<<"x["<< i <<"] =" kj x[i] ml}

asll M=1e9+7;
int n,m,k,tc;
ll p,q,r;

ll pms[200005];
ll ps[200005];

int main()
{

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(0);
	//freopen("stdin.in", "r", stdin)
 	//freopen("stdout.out", "w", stdout)
	srand(time(0));
	cin>>n;
	pms[0]=0;
	ps[0]=0;
	FOR(i,200005){
		ps[i]=0;
		pms[i]=0;
	}
	For(i,1,n){
		cin>>p;
		ps[i]=ps[i-1]+p;
		pms[i]=max(pms[i-1],ps[i]);
		//co ps[i] kj pms[i] ml
	}

	ll js1=0,js2=0,js3=0;
	//js1=psum
	//js2=psum sebelum + max ini
	//js3 maks akhir
	For(i,1,n){
		js2=js1+pms[i];
		js3=max(js3,js2);
		js1+=ps[i];

	}
	co js3 ml;

}


// Code for A - twiblr
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define FOR(i,x,y) for(ll i=x;i<(ll)(y);i++)

#define MOD 1000000007
#define mod(x) ( (x) % MOD + MOD ) % MOD

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

int main() {
  int A, B;
  cin >> A >> B;

  cout << 2 * A + 100 - B << endl;
  return 0;
}

#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = (1<<30) - 1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-8;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<=b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;

int main(){
    int n; cin >> n;
    string s; cin >> s;
    vl a(n+1); rep(i,n+1) cin >> a[i];
    int ans = inf;
    rep(i,n) chmin(ans, abs(a[i]-a[i+1]));
    cout << ans << endl;
    vvl b(ans,vl(n+1));
    rep(i,n+1){
        ll q = a[i] / ans;
        ll r = a[i] % ans;
        rep(j,r) b[j][i] = q + 1;
        for(int j=r; j<ans; j++) b[j][i] = q;
    }
    rep(i,ans){
        rep(j,n+1) cout << b[i][j] << " ";
        cout << endl;
    }
}


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

using namespace std;
using LL = long long;
using P = pair<LL,int>;
using vv = vector<vector<int>>;
const int INF = (int)1e9;
const LL LINF = (LL)1e18;

struct edge{
   int to;
   LL c, d;
   edge(int to, LL c, LL d): to(to), c(c), d(d) {}
};
vector<vector<edge>> G;
vector<long long> dijkstra(int N, int st){
   vector<long long> dist(N);
   for(int i = 0; i < N; i++){
	   dist[i] = LINF;
   }
   dist[st] = 0;
   priority_queue<P,vector<P>,greater<P>> q;
   q.push({dist[st], st});
   while(!q.empty()){
	   P p = q.top(); q.pop();
	   long long t = p.first;
	   int n = p.second;
	   if(dist[n] < t) continue;
	   for(auto e : G[n]){
		   int v = e.to;
		   LL c = e.c, d = e.d;
		   LL ave = sqrt(d);
		   ave = max(ave, t + 1);
		   LL cost = min(c + ave - 1 + d / ave, c + ave + d / (ave + 1));
		   if(dist[v] <= cost) continue;
		   dist[v] = cost;
		   q.push({dist[v], v});
	   }
   }
   return dist;
}

int main(){
	int N, M;
	cin >> N >> M;
	G.resize(N);
	rep(i,M){
		int a, b;
		LL c, d;
		cin >> a >> b >> c >> d;
		a--; b--;
		G[a].emplace_back(b, c, d);
		G[b].emplace_back(a, c, d);
	}
	vector<LL> dist = dijkstra(N, 0);
	LL ans = dist[N-1];
	if(ans == LINF) ans = -1;
	cout << ans << endl;

	return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long i64;
typedef unsigned long long ui64;
typedef vector<i64> vi;
typedef vector<vi> vvi;
typedef pair<i64, i64> pi;

#define pb push_back
#define sz(a) i64((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for(i=(s); i < (e); ++i)
inline void RI(i64 &i) {scanf("%lld", &(i));}
inline void RVI(vi &v) { for(i64 i=0;i<sz(v);++i) { RI(v[i]); } }
inline void RVVI(vvi &vv) { for(i64 i=0;i<sz(vv);++i) { RVI(vv[i]); } }
inline void WI(const i64 &i) {printf("%lld\n", i);}
inline void WVI(const vi &v, char sep=' ') { for(i64 i=0;i<sz(v);++i) { if(i != 0){ printf("%c", sep); } printf("%lld", v[i]);} printf("\n"); }
inline void WS(const string &s) { printf("%s\n", s.c_str()); }
inline void WB(bool b, const string &yes, const string &no) { if(b){ WS(yes);} else { WS(no);} }
inline void YESNO(bool b) { WB(b, "YES", "NO"); }
inline void YesNo(bool b) { WB(b, "Yes", "No"); }
#define BUF_LENGTH 1000000
inline void RS(string &s) {static char buf[BUF_LENGTH]; scanf("%s", buf); s = buf;}
template<typename T> inline bool IN(T &S, const typename T::key_type &key) {
  return S.find(key) != S.end();
}
template<typename T> inline bool ON(const T &b, i64 idx) {
  return ((T(1) << idx) & b) != 0;
}

i64 get_dist(i64 t_cur, i64 c, i64 d) {
  auto compute = [&](i64 t) {
    return t + c + d / (t + 1);
  };
  i64 Tmin = max(0LL, i64(floor(sqrt((long double)(d))) - 2));
  i64 Tmax = Tmin + 5;

  i64 d_min = compute(Tmin);
  i64 Topt = Tmin;
  i64 T;
  REP(Tmin, Tmax+1, T) {
    i64 d = compute(T);
    if(d < d_min) {
      d = d_min;
      Topt = T;
    }
  }

  if(t_cur <= Topt) {
    return compute(Topt) - t_cur;
  }
  return compute(t_cur) - t_cur;
}

int main(int argc, char *argv[]) {
  i64 i, j, k;
  i64 N, M; cin >> N >> M;
  using ppi = pair<i64, pi>;
  vector<vector<ppi>> edges(N);
  REP(0, M, i) {
    i64 a, b, c, d; RI(a); RI(b); RI(c); RI(d);
    --a; --b;
    edges[a].pb({b, {c, d}});
    edges[b].pb({a, {c, d}});
  }

  vi dist(N, -1);
  priority_queue<pi, vector<pi>, greater<pi>> pq;

  dist[0] = 0;
  pq.push({0, 0});
  while(!pq.empty()) {
    pi c = pq.top(); pq.pop();
    i64 cur = c.second, cur_dist = c.first;
    if(dist[cur] != cur_dist) {
      continue;
    }
    for(auto &e: edges[cur]) {
      i64 n_dist = cur_dist + get_dist(cur_dist, e.second.first, e.second.second);
      i64 n = e.first;
      if(dist[n] == -1 || n_dist < dist[n]) {
        dist[n] = n_dist;
        pq.push({n_dist, n});
      }
    }
  }

  WI(dist.back());

  return 0;
}


#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;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define int long long
#define reset(x) memset(x,0,sizeof(x))
#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define rep(i,n) for(int i=0;i<(n);++i)
#define repA(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define all(a) a.begin(),a.end()
#define PI 3.1415926535897932384626433832795
#define INF 1000000000
#define MOD 1000000007
#define MOD2 1000000009
#define EPS 1e-6
#define pb push_back
#define fi first
#define se second
#define mp make_pair

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

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	//freopen(".inp", "r", stdin);
    //freopen(".out", "w", stdout);
    int n; cin >> n;
    vector<int> arr;
    map<int, int> cnt;
    for (int i = 0; i < n; i++)
    {
    	int a; cin >> a;
    	arr.pb(a);
    	if (cnt.count(a % 200) == 0)
    	{
    		cnt[a % 200] = 0;
    	}
    	cnt[a % 200]++;
    }
    int sum = 0;
    for (auto x : cnt)
    {
    	int num = x.se;
    	sum += ((num * (num - 1)) / 2);
    }
    cout << sum;
}

#include <bits/stdc++.h>
#define endl "\n"
using namespace std;

#define ll long long
const long long llINF = 1LL << 60;
const int iINF = 0x20000000;
template<class T> inline bool chmin(T& a, T b) { if (b < a) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

void execute();
int main()
{
    cin.tie(nullptr);
    ios::sync_with_stdio(false);// cin と scanfの併用禁止
    cout << fixed << setprecision(15);
    execute();
    return 0;
}

void execute()
{
    ll N;
    ll A[200000];

    cin >> N;

    vector<int> idxs[200];

    for(int i = 0; i < N; i++){
        cin >> A[i];
        idxs[A[i] % 200].push_back(i);
    }

    ll cnt = 0;
    for(int m = 0; m < 200; m++){
        vector<int> &a = idxs[m];
        ll n = a.size();
        cnt += n * (n-1) / 2;
    }

    cout << cnt << endl;
}


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

using namespace std;

long long r1, c1, r2, c2, x, y;

int main() {
  cin >> r1 >> c1 >> r2 >> c2;
  x = abs(r1 - r2);
  y = abs(c1 - c2);
  if (r1 == r2 && c1 == c2) {
    cout << 0;
  } else if (y == x || x + y <= 3) {
    cout << 1;
  } else if ((x + y) % 2 == 0 || x + y <= 6 || abs(x - y) <= 3) {
    cout << 2;
  } else {
    cout << 3;
  }
  return 0;
}

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) ::std::begin(x), ::std::end(x)
using namespace std;

template <typename T>
void print_vector(T& vec) {
    stringstream ss;
    ss << "{";
    for (auto v : vec) ss << v << " ";
    ss << "}" << endl;
    cout << ss.str() << endl;
}

template <typename T>
void print_pairs(T& vec) {
    stringstream ss;
    ss << "{";
    for (auto v : vec) ss << v.first << "," << v.second << " ";
    ss << "}" << endl;
    cout << ss.str() << endl;
}

template <typename T>
string string_vector(vector<T>& vec) {
    stringstream ss;
    ss << "{";
    for (auto v : vec) ss << v << " ";
    ss << "}\n";
    return ss.str();
}

template <typename T>
string string_vector(vector<vector<T>>& vec) {
    stringstream ss;
    ss << "{";
    for (auto v : vec) {
        ss << "{";
        for (auto vv : v) ss << vv << " ";
        ss << "}";
    }
    ss << "}\n";
    return ss.str();
}

using ll = long long;
class Solution {
   public:
    ll solve(int r1, int c1, int r2, int c2) {
        // cout << "r1=" << r1 << " c1="<< c1 << " r2=" << r2 << " c2=" << c2 << endl;

        if(r1==r2 and c1==c2) return 0;
        if(abs(r1-r2) == abs(c1-c2)) return 1;
        // cout << "r1-r2=" << abs(r1-r2) % 2 << " c1-c2="<< abs(c1-c2) % 2 << endl;
        if( abs(r1-r2) + abs(c1-c2) <=3) return 1;
        if( (abs(r1-r2) % 2) == (abs(c1-c2) % 2) ) return 2;
        if( abs( abs(r1-r2) - (abs(c1-c2) ))<=3) return 2;

        return 3;
    }
};

// generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator)
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    Solution sol;
    // failed to analyze input format
    // TODO: edit here
    int r1, c1, r2, c2;
    cin >> r1>> c1>> r2>>c2 ;
    auto ans = sol.solve(r1, c1, r2, c2);
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const long long maxN = 6e5+10;
long long a[maxN],temp[maxN];
long long N;

long long tree1[maxN],tree2[maxN];
long long  lowbit(int x){return x&(-x);}
void add(long long tree[],long long x,long long k){for(;x<=N*2;x+=lowbit(x))tree[x]+=k;}
long long pre(long long tree[],long long x)
{
    long long ans=0;
    for(;x;x-=lowbit(x))ans+=tree[x];
    return ans;
}

int main()
{
    cin>>N;
    for(int i=0;i<=N-1;i++)cin>>a[i];
    for(int i=N;i<=N+N-1;i++)a[i]=a[i-N];
    for(int i=0;i<=N+N-1;i++)temp[i]=a[i];
    sort(temp,temp+N+N-1);
    int siz=unique(temp,temp+N+N-1)-temp;
    for(int i=0;i<=N+N-1;i++)a[i]=lower_bound(temp,temp+siz,a[i])-temp+1;
    long long ans=0;
    for(int i=N-1;i>=0;i--)
    {
        ans+=pre(tree1,a[i]-1);
        add(tree1,a[i],1);
    }
    for(int i=0;i<=N-1;i++)add(tree2,a[i],1);
    int L=0,R=N-1;
    for(int i=0;i<=N-1;i++)
    {
        cout<<ans<<endl;
        add(tree2,a[L],-1);
        ans-=pre(tree2,a[L]);
        L++;
        R++;
        add(tree2,a[R],1);
        ans+=N-pre(tree2,a[R]);
    }
}

//I'll always miss you like a darling.
#include <bits/stdc++.h>
using namespace std;

#define LL long long
#define LD long double
#define ull unsigned long long
#define x first
#define y second
#define pb push_back
#define pf push_front
#define mp make_pair
#define Pair pair<int,int>
#define pLL pair<LL,LL>
#define pii pair<double,double>
#define LOWBIT(x) x & (-x)

#define rep(i,a,b) for (int i=a;i<=b;i++)
#define REP(i,a,b) for (int i=a;i>=b;i--)

const int INF=2e9;
const LL LINF=2e16;
const int magic=348;
const int MOD=1e9+7;
const double eps=1e-10;
const double pi=acos(-1);

struct fastio
{
    static const int S=1e7;
    char rbuf[S+48],wbuf[S+48];int rpos,wpos,len;
    fastio() {rpos=len=wpos=0;}
    inline char Getchar()
    {
        if (rpos==len) rpos=0,len=fread(rbuf,1,S,stdin);
        if (!len) return EOF;
        return rbuf[rpos++];
    }
    template <class T> inline void Get(T &x)
    {
        char ch;bool f;T res;
        while (!isdigit(ch=Getchar()) && ch!='-') {}
        if (ch=='-') f=false,res=0; else f=true,res=ch-'0';
        while (isdigit(ch=Getchar())) res=res*10+ch-'0';
        x=(f?res:-res);
    }
    inline void getstring(char *s)
    {
        char ch;
        while ((ch=Getchar())<=32) {}
        for (;ch>32;ch=Getchar()) *s++=ch;
        *s='\0';
    }
    inline void flush() {fwrite(wbuf,1,wpos,stdout);fflush(stdout);wpos=0;}
    inline void Writechar(char ch)
    {
        if (wpos==S) flush();
        wbuf[wpos++]=ch;
    }
    template <class T> inline void Print(T x,char ch)
    {
        char s[20];int pt=0;
        if (x==0) s[++pt]='0';
        else
        {
            if (x<0) Writechar('-'),x=-x;
            while (x) s[++pt]='0'+x%10,x/=10;
        }
        while (pt) Writechar(s[pt--]);
        Writechar(ch);
    }
    inline void printstring(char *s)
    {
        int pt=1;
        while (s[pt]!='\0') Writechar(s[pt++]);
    }
}io;

template<typename T> inline void check_max(T &x,T cmp) {x=max(x,cmp);}
template<typename T> inline void check_min(T &x,T cmp) {x=min(x,cmp);}
template<typename T> inline T myabs(T x) {return x>=0?x:-x;}
template<typename T> inline T mygcd(T x,T y) {return y==0?x:mygcd(y,x%y);}
inline int add(int x) {if (x>=MOD) x-=MOD;return x;}
inline int add(int x,int MO) {if (x>=MO) x-=MO;return x;}
inline int sub(int x) {if (x<0) x+=MOD;return x;}
inline int sub(int x,int MO) {if (x<0) x+=MO;return x;}
inline void Add(int &x,int y) {x=add(x+y);}
inline void Add(int &x,int y,int MO) {x=add(x+y,MO);}
inline void Sub(int &x,int y) {x=sub(x-y);}
inline void Sub(int &x,int y,int MO) {x=sub(x-y,MO);}
template<typename T> inline int quick_pow(int x,T y) {int res=1;while (y) {if (y&1) res=1ll*res*x%MOD;x=1ll*x*x%MOD;y>>=1;}return res;}
template<typename T> inline int quick_pow(int x,T y,int MO) {int res=1;while (y) {if (y&1) res=1ll*res*x%MO;x=1ll*x*x%MO;y>>=1;}return res;}

const int MAXN=3e5;

int n;
int a[MAXN+48];

namespace BIT
{
    int c[MAXN+48];
    void clear() {memset(c,0,sizeof(c));}
    void modify(int x) {while (x<=n) c[x]++,x+=LOWBIT(x);}
    int query(int x) {int res=0;while (x) res+=c[x],x^=LOWBIT(x);return res;}
}

int main ()
{
#ifndef ONLINE_JUDGE
    double TIME=clock();
    freopen ("a.in","r",stdin);
    freopen ("a.out","w",stdout);
    cerr<<"Running..."<<endl;
#endif
    scanf("%d",&n);rep(i,1,n) scanf("%d",a+i),a[i]++;LL ans=0;
    BIT::clear();rep(i,1,n) ans+=(i-1)-BIT::query(a[i]),BIT::modify(a[i]);
    printf("%lld\n",ans);
    rep(i,1,n-1)
    {
        ans-=(a[i]-1);
        ans+=(n-a[i]);
        printf("%lld\n",ans);
    }
#ifndef ONLINE_JUDGE
    cerr<<"Exec Time: "<<(clock()-TIME)/CLOCKS_PER_SEC<<endl;
#endif
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ff              first
#define ss              second
#define int32			int32_t
#define int             long long
#define pb              push_back
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pq              priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define mod             1000000007
#define w(x)            int x; cin>>x; while(x--)
#define loop(x, n) for(int x = 0; x < n; x++)

bool sorted(vi &p)
{
	int n = p.size();
	for(int i = 0; i < n - 1; i++)
		if(p[i] > p[i + 1])
			return false;

	return true;
}

int32 main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int n;
	cin >> n;

	vi p(n);
	loop(i, n)
		cin >> p[i];

	vi ans;
	set<int> s;
	int swaps = 0;
	for(int i = n - 2; i >= 0; i--)
	{
		int x = i;
		while(x < (n - 1) && p[x] > p[x + 1] && swaps < n)
		{
			swap(p[x], p[x + 1]);
			if(s.find(x) != s.end())
			{
				swaps = n;
				break;
			}
			else
				s.insert(x);

			ans.pb(x + 1);
			swaps++;
			x++;
		}
	}

	if(swaps >= n || (n - 1 - swaps))
		cout << -1 << endl;
	else
	{
		for(int i: ans)
			cout << i << endl;
	}

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1000000007;
#define INF 1LL<<30
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()

int did[202020];
vector<int> ret;

int main() {
    int n;
    cin>>n;
    vector<int> p(n),r(n);
    rep(i,n){
        cin>>p[i];
        p[i]--;
        r[p[i]]=i;
    }

    rep(i,n){
        int x=r[i];
        while(x>i){
            if(did[x-1]==1){
                cout<<-1<<endl;
                return 0;
            }
            did[x-1]=1;
            ret.push_back(x);
            swap(p[x-1],p[x]);
            r[p[x]]=x;
            r[p[x-1]]=x-1;
            x--;
        }
    }
    rep(i,n-1){
        if(did[i]==0){
            cout<<-1<<endl;
            return 0;
        }
    }

    for(auto e : ret) cout<<e<<endl;
}


#include<iostream>
#include<cstdio>
#include<queue>
#define ll long long
using namespace std;
struct node{
	int num,id;
};
int n,x,Q,u,d;
int edgenum,NIL=-1,head[200005],nex[400005],vet[400005];
int h[200005],fa[200005],Size[200005],son[200005];
int Num[200005],t[200005];
bool vis[200005];
queue<node> q[200005];
void addedge(int u,int v){
	edgenum++;
	nex[edgenum]=head[u];
	vet[edgenum]=v;
	head[u]=edgenum;
}
void DFS(int u,int H){
	int Max=0;
	h[u]=H;
	for(int i=head[u];i!=NIL;i=nex[i]){
		int v=vet[i];
		if(v==fa[u]) continue;
		fa[v]=u;
		Size[v]=1;
		DFS(v,H+1);
		Size[u]+=Size[v];
		if(Size[v]>Max){
			Max=Size[v];
			son[u]=v;
		}
	}
}
void clac(int u,int num){
    if(num>0)
    {
        Num[h[u]]++;
    }
    else
    {
    	Num[h[u]]--;
    }
	for(int i=head[u];i!=NIL;i=nex[i]){
		int v=vet[i];
		if(v==fa[u]||vis[v]) continue;
		clac(v,num);
	}
}
void DSU(int u,int keep){
	for(int i=head[u];i!=NIL;i=nex[i]){
		int v=vet[i];
		if(v==fa[u]||v==son[u]) continue;
		DSU(v,0);
	}
	if(son[u]){
		DSU(son[u],1);vis[son[u]]=true;
	}
	clac(u,1);vis[son[u]]=false;
	while(!q[u].empty()){
		node X=q[u].front();
		q[u].pop();
		if(h[u]<=X.num)
			t[X.id]=Num[X.num];
		else t[X.id]=0;
	}
	if(keep==0) clac(u,-1);
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++) head[i]=NIL;
	for(int i=2;i<=n;i++){
		scanf("%d",&x);
		addedge(x,i);
		addedge(i,x);
	}
	scanf("%d",&Q);
	for(int i=1;i<=Q;i++){
		scanf("%d%d",&u,&d);
		node X;
		X.num=d+1;
		X.id=i;
		q[u].push(X);
	}
	DFS(1,1);
	DSU(1,0);
	for(int i=1;i<=Q;i++){
		printf("%d\n",t[i]);
	}
	return 0;
}

#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define RSORT(x) sort(x.rbegin(), x.rend())
#define REVERSE(x) reverse(ALL(x))
#define MAX(x) *max_element(ALL(x))
#define MAXI(x) max_element(ALL(x)) - x.begin()
#define SUM(x) accumulate(ALL(x), 0ll)
#define COUNT(x, y) count(ALL(x), y);
#define ANS cout << ans << "\n"
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define Yes cout << "Yes\n";
#define No cout << "No\n";
#define init() \
  cin.tie(0);  \
  ios::sync_with_stdio(false)
#define debug(x) cerr << "[debug] " << #x << ": " << x << endl;
#define debugV(v)                       \
  cerr << "[debugV] " << #v << ":";     \
  rep(z, v.size()) cerr << " " << v[z]; \
  cerr << endl;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
using vp = vector<P>;
using vs = vector<string>;

template <typename T>
inline istream& operator>>(istream& i, vector<T>& v) {
  rep(j, v.size()) i >> v[j];
  return i;
}

template <typename T1, typename T2>
inline istream& operator>>(istream& i, pair<T1, T2>& v) {
  return i >> v.first >> v.second;
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) {
  return to_string(x).size();
}

ll gcd(ll x, ll y) {
  return y ? gcd(y, x % y) : x;
}

ll lcm(ll a, ll b) {
  return a / gcd(a, b) * b;
}

template <class T>
void chmax(T& a, const T& b) {
  if (a < b) a = b;
}

template <class T>
void chmin(T& a, const T& b) {
  if (b < a) a = b;
}

vvll to;
vp range;
vll dep;
vll points;
ll dfs(ll i, ll s, ll d) {
  ll count = 1;
  points.pb(i);
  for (ll t : to[i]) count += dfs(t, s + count, d + 1);
  dep[i] = d;
  range[i] = {s, s + count - 1};
  return count;
}

signed main() {
  init();
  ll N;
  cin >> N;
  vll A(N - 1);
  cin >> A;
  rep(i, N - 1) A[i]--;
  to.resize(N);
  range.resize(N);
  dep.resize(N);
  rep(i, N - 1) to[A[i]].pb(i + 1);

  dfs(0, 0, 0);
  vector<unordered_map<ll, ll>> x(N);
  unordered_map<ll, ll> m;

  ll M = 1130;
  rep(i, N) {
    ll p = points[i];
    m[dep[p]]++;
    if (i % M == 0) x[i] = m;
  }

  ll Q;
  cin >> Q;
  rep(i, Q) {
    ll u, d;
    cin >> u >> d;
    u--;
    ll l = range[u].first;
    ll r = range[u].second;
    ll ans = x[r - r % M][d];
    rep(k, r % M) if (dep[points[r - k]] == d) ans++;
    if (l != 0) {
      l--;
      rep(k, l % M) if (dep[points[l - k]] == d) ans--;
      ans -= x[l - l % M][d];
    }
    COUT(ans);
  }
  return 0;
}


#include<iostream>
#include<set>
#include<vector>
using namespace std;
typedef long long li;
#define rep(i,n) for(int i=0;i<(n);i++)
#define df 0
template<class T> void print(const T& t){ cout << t << "\n"; }
template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }

int find(int a,vector<int>& uf){
  if(uf[a]<0) return a;
  return uf[a]=find(uf[a],uf);
}

void unite(int a,int b,vector<int>& uf){
  a=find(a,uf),b=find(b,uf);
  if(a==b)return ;
  if(uf[a]>uf[b]) swap(a,b);
  uf[a]+=uf[b];
  uf[b]=a;
}

int main(){
  int h,w; cin >>h >>w;
  vector<string> mp(h); rep(i,h) cin >>mp[i];
  vector<vector<int>> adj(h+w);
  int n=h+w;
  vector<int> uf(n,-1);
  unite(0,h,uf);
  unite(h-1,h,uf);
  unite(h-1,h+w-1,uf);
  unite(0,h+w-1,uf);
  rep(i,h) rep(j,w){
    if(mp[i][j]=='#'){
      unite(i,h+j,uf);
    }
  }

  set<int> tate,yoko;
  rep(i,h){
    tate.insert(find(i,uf));
  }
  rep(j,w){
    yoko.insert(find(h+j,uf));
  }
  print(min(tate.size(),yoko.size())-1);
}


/*
 * Author: Moon-light
*/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define lowbit(x) ((x)&(-x))
#define sz(x) ((int)x.size())
#define fr(x) freopen(x,'r',stdin)
#define fw(x) freopen(x,'w',stdout)
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define bitcnt(x) (__builtin_popcountll(x))
#define rep(i,a,b) for(int i = (a);i<=(b); ++i)
#define per(i,a,b) for(int i = (a);i>=(b); --i)
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef pair<double,double> PDD;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VB;
typedef double db;

template <class T> void clear(T& a) { T().swap(a);}
template <class T,class S>
inline bool upmin(T& a,const S&b){ return a>b ? a=b,1:0;}
template <class T,class S>
inline bool upmax(T&a, const S&b){ return a<b? a=b,1:0;}

//fast read(improved by fread)
char buf[1<<21],*p1=buf,*p2=buf;
inline int getc(){
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;
}
inline int redi() {
    int ret = 0,f = 0;char ch = getc();
    while (!isdigit (ch)) {
        if (ch == '-') f = 1;
        ch = getc();
    }
    while (isdigit (ch)) {
        ret = ret * 10 + ch - 48;
        ch = getc();
    }
    return f?-ret:ret;
}

//global variable
const int N = 2e5+100;
const int mod = 1e9+7;
LL a[N];
int n;
LL s[N];
//functions
LL qmi(LL a,LL k,LL mod){
    LL res = 1;
    while(k){
        if(k&1) res = res*a%mod;
        k >>= 1;
        a = a*a%mod;
    }
    return mod;
}
int gcd(int a,int b) {
    return b==0? a:gcd(b,a%b);
}
//

//code from here! Come on! Have a pleasant experience~
int main()
{
    IO;
    cin>>n;

    rep(i,1,n) cin>>a[i];

    sort(a+1,a+1+n,greater<LL>());

    s[n] = a[n];
    for(int i = n-1; i>=1; i--){
        s[i] = s[i+1] + a[i];
    }
    LL ans = 0;

    rep(i,1,n-1){
        ans += (n-i)*a[i] - s[i+1];
    }
    cout<<ans<<endl;
    return 0;
}

#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define rep(i,n) for(int i = 0;  i < n ; ++i)
#define REP(i,a,b) for(int i = a ; i <= b ; ++i)
#define filei freopen("input.txt", "r", stdin);
#define fileo freopen("output.txt", "w", stdout);
#define gooi cout<<"Case #"<<i+1<<" :";
#define s(n) scanf("%d",&n)
#define rev(i,n) for(int i = n ; i >= 0 ; --i)
#define REV(i,a,b) for(int i = a ; i >= b ; --i)
#define miN(a,b) (((a)<(b))?(a):(b))
#define sc(n) scanf("%c",&n)
#define tr(c,i) for(typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define INF 1000000000
#define pii pair<long long int,long long int>
#define pb(a) push_back(a)
#define F first
#define S second
typedef long long ll;
using namespace std;

const long long int mod = 998244353;
long long int power(long long int x,long long int y){
    long long int res = 1;
    x = x % mod;
    if (x == 0) return 0;
    while (y > 0){
        if (y & 1)
            res = (res*x) % mod;
        y = y>>1;
        x = (x*x) % mod;
    }
    return res;
}
long long int inv(long long int a){return power(a,mod-2);}
long long int add(long long int a,long long int b){return (a+b)%mod;}
long long int sub(long long int a,long long int b){return (a-b+mod)%mod;}
long long int mul(long long int a,long long int b){return (a*b)%mod;}
long long int divi(long long int a,long long int b){return (a*inv(b))%mod;}
ll fact[5001];
// Please write the recurances once :(
void pre(){
    fact[0]=1;
    for(int i=1;i<=5000;i++)
        fact[i] = mul(fact[i-1],i);
}
ll ncr(int n,int r){
    return divi(fact[n],mul(fact[n-r],fact[r]));
}
ll dp[15][5001]{0};
void solve(){
    ll n,m;cin>>n>>m;
    dp[0][0]=1;
    for(int i=1;i<=14;i++){
        for(int j=0;j<=n;j+=2){
            for(int k=0;k<=m;k++){
                if( j*(1<<(i-1)) + k > m ) break;
                dp[i][j*(1<<(i-1)) + k] = add(dp[i][j*(1<<(i-1)) + k],mul(ncr(n,j),dp[i-1][k]));
            }
        }
    }
    cout<<dp[14][m]<<"\n";
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    pre();
    long long int num = 1;
    //cin>>num;
    for(long long int i=0;i<num;i++){
       	solve();
    }
}


#include<bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ll long long int
#define vi vector<int>
#define pb push_back
#define pop pop_back
using namespace std;
int main(){
ll n,k;
cin>>n>>k;
vector<pair<ll,ll>>v;
while(n--)
{
    ll x,y;
    cin>>x>>y;
    v.pb({x,y});

}
sort(v.begin(),v.end());
for(auto it:v)
{
    ll a=it.first;
    ll b=it.second;
    if(k<a)
    {
        cout<<k<<endl;
        return 0;
    }
    else
    k+=b;
}
cout<<k<<endl;
return 0;
}

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

#define FASTIO	ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define ll long long
#define mset(A,val) memset(A,val,sizeof(A))
#define fi(a,b) for(int i=a;i<=b;++i)
#define fj(a,b) for(int j=a;j<=b;++j)
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define pii pair<int,int>
#define pb push_back
#define eb emplace_back
#define DEBUG(a...) cout<<#a<<": ";for(auto &it:a)cout<<it<<" ";cout<<endl;
#define debug(a)cout<<#a<<": "<<a<<"\n";
#define hola cout<<"hola"<<endl;
#define int long long
// ---------------------------------------------------------------------------
const int mod = 1e9+7;
const int maxn = 2e5 + 9;

// ---------------------------------------------------------------------------
void init(){}
// ---------------------------------------------------------------------------
void test_case(int tc)
{
    // cout<<"Case #"<<tc<<": ";
    int n,k;cin>>n>>k;
    vector<pii>arr(n);
    fi(0,n-1)cin>>arr[i].first>>arr[i].second;
    sort(all(arr));
    int currPos = 0;
    fi(0,n-1){
        ll nextPos = arr[i].first;
        ll cost = nextPos-currPos;
        if(cost>k){
            cout<<currPos+k;
            return;
        }
        k-=cost;
        currPos = nextPos;
        k+=arr[i].second;
    }
    cout<<currPos+k;
    init();
}

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

    int t=1;
    //cin>>t;
    for(int tc=1;tc<=t;++tc)test_case(tc);
}


#include<bits/stdc++.h>
#define re register
using namespace std;
inline int read(){
	re int t=0;re char v=getchar();
	while(v<'0')v=getchar();
	while(v>='0')t=(t<<3)+(t<<1)+v-48,v=getchar();
	return t;
}
const int M=998244353;
inline void add(re int &x,re int y){(x+=y)>=M?x-=M:x;}
inline int ksm(re int x,re int y){
	re int s=1;
	while(y){
		if(y&1)s=1ll*s*x%M;
		x=1ll*x*x%M,y>>=1;
	}
	return s;
}
int n,f[502][502],a[502],b[502],px;
signed main(){
	n=read();
	re int mn=2e9;
	for(re int i=1;i<=n;++i)for(re int j=1;j<=n;++j)f[i][j]=read(),mn=min(mn,f[i][j]);
	for(re int i=1;i<=n;++i){
		for(re int j=1;j<=n;++j){
			if(f[i][j]==mn){
				px=i;
			}
		}
	}
	for(re int i=1;i<=n;++i)b[i]=f[px][i]-mn;
	for(re int i=1;i<=n;++i){
		for(re int j=1;j<=n;++j){
			a[i]=f[i][j]-b[j];
		}
	}
	for(re int i=1;i<=n;++i)if(a[i]<0)return puts("No"),0;
	for(re int i=1;i<=n;++i)for(re int j=1;j<=n;++j)if(a[i]+b[j]!=f[i][j])return puts("No"),0;
	puts("Yes");
	for(re int i=1;i<=n;++i)printf("%d ",a[i]);puts("");
	for(re int i=1;i<=n;++i)printf("%d ",b[i]);puts("");
}

#include <sstream>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <array>
#include <deque>
#include <numeric>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <list>
#include <cassert>
#include <cmath>
#include <climits>
#include <map>
#include <queue>
#include <functional>
#include <bitset>
#include <cassert>
using namespace std;
using ll = int64_t;
#define rep(i, a) for (int i = 0; i < (int)(a); ++i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define repr(i, a) for (int i = (int)((a) - 1); i >= 0; --i)
template <class T> class V1 : public vector<T> { public: V1(size_t n1, T init = T()) : vector<T>(n1, init) {} };
template <class T> class V2 : public vector<V1<T>> { public: V2(size_t n1, size_t n2, T init = T()) : vector<V1<T>>(n1, V1<T>(n2, init)) {} };
template <class T> class V3 : public vector<V2<T>> { public: V3(size_t n1, size_t n2, size_t n3, T init = T()) : vector<V2<T>>(n1, V2<T>(n2, n3, init)) {} };
template <class T> class V4 : public vector<V3<T>> { public: V4(size_t n1, size_t n2, size_t n3, size_t n4, T init = T()) : vector<V3<T>>(n1, V3<T>(n2, n3, n4, init)) {} };
template <class T> class V5 : public vector<V4<T>> { public: V5(size_t n1, size_t n2, size_t n3, size_t n4, size_t n5, T init = T()) : vector<V4<T>>(n1, V4<T>(n2, n3, n4, n5, init)) {} };
template <class T> vector<T> GetV1(istream& in, size_t N) { vector<T> ret; ret.reserve(N); rep(i, N) { T a; in >> a; ret.emplace_back(a); } return ret; }
struct dstream { ofstream fout; dstream(const char* fname) : fout(fname) {} };
template <typename T> dstream& operator << (dstream& lhs, const T& rhs) { lhs.fout << rhs; cout << rhs; return lhs; }

int main()
{
#ifdef ENV_LOCAL
	ifstream fin("input.txt");
	dstream dout("output.txt");
	auto& in = fin;
	auto& out = dout;
#else
	auto& in = cin;
	auto& out = cout; out << "";
#endif

	int N;
	in >> N;
	V2<ll> c(N, N);
	rep(i, N) {
		rep(j, N) {
			in >> c[i][j];
		}
	}
	ll min1 = LLONG_MAX;
	int imin = -1;
	rep(i, N) {
		if (c[0][i] < min1) {
			min1 = c[0][i];
			imin = i;
		}
	}
	vector<ll> As(N), Bs(N);
	rep(i, N) {
		As[i] = c[i][imin];
	}
	rep(j, N) {
		Bs[j] = c[0][j] - As[0];
		rep2(i, 1, N) {
			ll b = c[i][j] - As[i];
			if (b != Bs[j]) {
				cout << "No";
				return 0;
			}
		}
	}
	cout << "Yes" << endl;
	cout << As[0];
	rep2(i, 1, N)
		cout << " " << As[i];
	cout << endl;
	cout << Bs[0];
	rep2(i, 1, N)
		cout << " " << Bs[i];

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define inf 1e17
#define precise(x,y) fixed<<setprecision(y)<<x
#define int long long
#define float double
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define f(n) for(int i=0;i<n;i++)
#define fa(n) int a[n];for(int i=0;i<n;i++){cin>>a[i];}
#define fv(n) vector<int> v ;for(int i=0;i<n;i++){int y;cin>>y;v.push_back(y);}
#define test int t; cin>>t; while(t--)
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

void solve()
{
 int n,m;
 cin>>n>>m;
 if(n<m)
 {if(n+3>m)
 {
    //  if(n<=m)
     cout<<"Yes";
    //  else
    //  {
    //      cout<<"No";
    //  }

 }
 else
 {
     cout<<"No";
 }}
 else
 {
     if(m+3>n)
 {
    //  if(n<=m)
     cout<<"Yes";
    //  else
    //  {
    //      cout<<"No";
    //  }

 }
 else
 {
     cout<<"No";
 }
 }

}
signed main()
{
fast
//  test
  {
   solve();
  }
 return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cerr<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
const int MX = 200005;

int main() {
  int x,y; cin >> x >> y;
  if(x<y) swap(x,y);
  y += 3;
  if(x<y) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}


// C headers
#include <cassert>
#include <cctype>
// #include <cerrno>
#include <cfloat>
// #include <ciso646>
#include <climits>
// #include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
// #include <ccomplex>
#include <cfenv>
#include <cinttypes>
// #include <cstdalign>
// #include <cstdbool>
#include <cstdint>
// #include <ctgmath>
// #include <cuchar>
// #include <cwchar>
// #include <cwctype>
#endif
// C++ headers
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
// #include <atomic>
#include <chrono>
// #include <codecvt>
// #include <condition_variable>
#include <forward_list>
// #include <future>
#include <initializer_list>
// #include <mutex>
#include <random>
#include <ratio>
#include <regex>
// #include <scoped_allocator>
#include <system_error>
// #include <thread>
#include <tuple>
// #include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#if __cplusplus >= 201402L
#include <shared_mutex>
#endif

#define rep(i,n) for(int i=0; i<(n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define endl "\n";
#define pb push_back
#define mp make_pair

using namespace std;
using ll = long long;

int main() {
	//cout << fixed << setprecision(10);
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);

	int n; cin >> n;
	int money = 0;
	int i = 1;

	while (true) {
		money += i;
		if (money >= n) break;
		i++;
	}
	cout << i << endl;

	return 0;
}

#include<bits/stdc++.h>

using namespace std;

int main(){

    int n;
    cin >> n;
    long long s = 0;
    for (int i = 1; i <= n; i++){
        if (s >= n){
            cout << i - 1;
            return 0;
        }
        s += i;
    }
    if (n == 1){
        cout << 1;
    }
    else if (n == 2){
        cout << 3;
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	ll N;
	cin >> N;
	vector<pair<ll, ll>> edge(N - 1);
	vector<vector<ll>> g(N);
	for (ll i = 0; i < N - 1; i++)
	{
		ll a, b;
		cin >> a >> b;
		a--;
		b--;
		g[a].push_back(b);
		g[b].push_back(a);
		edge[i] = {a, b};
	}
	vector<ll> depth(N, -1);
	depth[0] = 0;
	vector<ll> q = {0};
	while (q.size())
	{
		ll at = q.back();
		q.pop_back();
		for (ll i : g[at])
			if (depth[i] == -1)
			{
				depth[i] = depth[at] + 1;
				q.push_back(i);
			}
	}
	vector<ll> s(N);
	ll Q;
	cin >> Q;
	while (Q--)
	{
		ll t, e, x;
		cin >> t >> e >> x;
		ll a, b;
		tie(a, b) = edge[e - 1];
		if (depth[a] > depth[b])
		{
			swap(a, b);
			t ^= 3;
		}
		if (t == 1)
		{
			s[0] += x;
			s[b] -= x;
		}
		else
			s[b] += x;
	}
	q = {0};
	while (q.size())
	{
		ll at = q.back();
		q.pop_back();
		for (ll i : g[at])
			if (depth[at] < depth[i])
			{
				s[i] += s[at];
				q.push_back(i);
			}
	}
	for (ll i : s)
		cout << i << endl;
}

#include <iostream>
#include <climits>
#include <utility>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <algorithm>
#include <chrono>
#include <array>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <numeric>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

constexpr int INF = INT_MAX / 4;
constexpr ll LL_INF = LLONG_MAX / 4;
const double EPS = 1e-9;
const double PI = 2.0*acos(0.0);

struct transformation {
    array<array<ll, 3>, 2> m;
};

transformation unit  = {
        array<ll, 3>({1,0,0}),
        array<ll, 3>({0,1,0})
};

transformation r90_clockwise = {
        array<ll, 3>({0,1,0}),
        array<ll, 3>({-1,0,0})
};

transformation r90_counterclockwise = {
        array<ll, 3>({0,-1,0}),
        array<ll, 3>({1,0,0})
};

transformation reflection_x(ll p) {
    return {
            array<ll, 3>({-1,0,2*p}),
            array<ll, 3>({0,1,0})
    };
}

transformation reflection_y(ll p) {
    return {
            array<ll, 3>({1,0,0}),
            array<ll, 3>({0,-1,2*p})
    };
}

transformation mul(const transformation& a, const transformation& b) {
    return {
            array<ll, 3>({b.m[0][0] * a.m[0][0] + b.m[0][1] * a.m[1][0], b.m[0][0] * a.m[0][1] + b.m[0][1] * a.m[1][1], b.m[0][0] * a.m[0][2] + b.m[0][1] * a.m[1][2] + b.m[0][2]}),
            array<ll, 3>({b.m[1][0] * a.m[0][0] + b.m[1][1] * a.m[1][0], b.m[1][0] * a.m[0][1] + b.m[1][1] * a.m[1][1], b.m[1][0] * a.m[0][2] + b.m[1][1] * a.m[1][2] + b.m[1][2]})
    };
}

pair<ll,ll> calc(const transformation& a, const pair<ll,ll>& c) {
    return {a.m[0][0] * c.first + a.m[0][1] * c.second + a.m[0][2], a.m[1][0] * c.first + a.m[1][1] * c.second + a.m[1][2]};
}

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

    int n, m, q;
    cin >> n;
    vector<pair<ll,ll>> c(n);
    for (int i = 0; i < n; i++) {
        ll a,b;
        cin >> a >> b;
        c[i] = {a,b};
    }

    cin >> m;
    vector<pair<int, int>> ops(m);
    for (int i = 0; i < m; i++) {
        int op;
        int p = 0;
        cin >> op;
        if (op == 3 || op == 4) {
            cin >> p;
        }
        ops[i] = {op, p};
    }

    cin >> q;
    vector<pair<ll, pair<ll, ll>>> queries(q);
    for (int i = 0; i < q; i++) {
        ll a,b;
        cin >> a >> b;
        queries[i] = {a, {i, b-1}};
    }

    vector<pair<ll,ll>> ans(q);

    sort(queries.begin(), queries.end());
    transformation cur_transformation = unit;

    int cur_q = 0;
    for (; cur_q < q && queries[cur_q].first == 0; cur_q++) {
        ans[queries[cur_q].second.first] = c[queries[cur_q].second.second]; // no transformation
    }

    for (ll i = 1; i <= m; i++) {
        transformation i_transformation = unit;
        switch (ops[i-1].first) {
            case 1:
                i_transformation = r90_clockwise;
                break;
            case 2:
                i_transformation = r90_counterclockwise;
                break;
            case 3:
                i_transformation = reflection_x(ops[i-1].second);
                break;
            default:
                i_transformation = reflection_y(ops[i-1].second);
                break;
        }

        cur_transformation = mul(cur_transformation, i_transformation);
        for (; cur_q < q && queries[cur_q].first == i; cur_q++) {
            ans[queries[cur_q].second.first] = calc(cur_transformation, c[queries[cur_q].second.second]); // no transformation
        }
    }

    for (int i = 0; i < q; i++) {
        cout << ans[i].first << " " << ans[i].second << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int inf = INT_MAX;

int main() {
	int n, x; cin >> n >> x;
	vector<int> ans; rep(i, n) {
		int a; cin >> a; if (a == x)continue;
		ans.push_back(a);
	}for (int i : ans)cout << i << ' '; cout << endl;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_set>
#include <cstdlib>
#include <cmath>
#define ll long long
using namespace std;

int main(){
	ll i,j,n,x,a,flag=0;
	cin >> n >> x;

	for (i = 0; i < n; i++) {
		cin >> a;
		if (a != x) {
			if (flag == 1) {
				cout << " ";
			}
			cout << a;
			flag = 1;
		}
	}
	cout << endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const long long mod=998244353,INF=1e18;
#define ll long long
#define pll pair<ll,ll>
#define x first
#define y second
#define p_all(arr) for(auto i:arr){cout<<i<<" ";}cout<<"\n";
vector<ll> par,sze;
ll root(ll a)
{
    if(a==par[a])
    return a;
    return par[a]=root(par[a]);
}
void union1(ll a,ll b)
{
    a=root(a),b=root(b);
    if(a==b)
    return ;
    if(sze[a]<sze[b])
    swap(a,b);
    sze[a]+=sze[b];
    par[b]=a;
}
ll power(ll a,ll b)
{
    ll res=1;
    while(b)
    {
        if(b&1)
        {
            res=(res*a)%mod;
        }
        a=(a*a)%mod;
        b>>=1;
    }
    return res;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    ll t=1;
    // cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        par.resize(n);
        sze.resize(n,1);
        for(int i=0;i<n;i++)
        par[i]=i;
        vector<vector<ll> > a(n);
        for(int i=0;i<n;i++)
        {
            ll u;
            cin>>u;
            u--;
            union1(u,i);
        }
        ll ans=0;
        for(int i=0;i<n;i++)
        if(root(i)==i)
        {
            ans++;
        }
        ans=power(2,ans);
        ans+=(mod-1);
        ans%=mod;
        cout<<ans<<"\n";
    }
    return 0;
}

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

// add your library with double quotation #include"" here.
// #include "atcoder/all"
// using namespace atcoder;

// define your macros here.
// #define REP(a,b) for(long long a = 0;a < b;++a)

using namespace std;
typedef string::const_iterator State;
#define eps 1e-8L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef long double ld;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

#define int long long
vector<int> vertexs[300000];
int dists[300000];
int backs[300000];
int ans[300000];
int doned[300000];
int dfs(int now,int back){
    for(auto x:vertexs[now]){
        if(x == back) continue;
        backs[x] = now;
        dists[x] = dists[now] + 1;
        dfs(x,now);
    }
    return 0;
}
int value = 0;
int dfs2(int now,int back){
    value++;
    ans[now] = value;
    for(auto x:vertexs[now]){
        if(x == back) continue;
        dfs2(x,now);
    }
        value++;
    return 0;
}
void solve(){
    // write your solution here.
    int n;
    cin >> n;
    REP(i,n-1){
        int a,b;
        cin >> a >> b;
        a--;b--;
        vertexs[a].push_back(b);
        vertexs[b].push_back(a);
    }
    dfs(0,-1);
    pair<int,int> best = mp(0,0);
    REP(i,n){
        if(dists[i] > dists[best.first]){
            best.first = i;
        }
    }
    dists[best.first] = 0;
    backs[best.first] = -1;
    dfs(best.first,-1);
    REP(i,n){
        if(dists[i] > dists[best.second]){
            best.second = i;
        }
    }
    while(true){
        doned[best.second] = 1;
        value++;
        ans[best.second] = value;
        for(auto x:vertexs[best.second]){
            if(doned[x] == true or x == backs[best.second]) continue;
            dfs2(x,best.second);
        }
        if(best.first == best.second) break;
        best.second = backs[best.second];
    }
    REP(i,n){
        if(i != 0) cout << " ";
        cout << ans[i];
    }
    cout << endl;
}
#undef int

// generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator)
int main() {
    // Fasterize input/output script
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(100);
    // scanf/printf user should delete this fasterize input/output script

    int t = 1;
    //cin >> t; // comment out if solving multi testcase
    for(int testCase = 1;testCase <= t;++testCase){
        solve();
    }
    return 0;
}


#line 1 "/workspaces/compro/lib/template.hpp"

#line 1 "/workspaces/compro/lib/io/vector.hpp"
#include <iostream>
#include <vector>

#ifndef IO_VECTOR
#define IO_VECTOR

template <class T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  int size = v.size();
  for (int i = 0; i < size; i++) {
    std::cout << v[i];
    if (i != size - 1)
      std::cout << " ";
  }
  return out;
}

template <class T> std::istream &operator>>(std::istream &in, std::vector<T> &v) {
  for (auto &el : v) {
    std::cin >> el;
  }
  return in;
}

#endif
#line 4 "/workspaces/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

// 重複を消す。計算量はO(NlogN)
template <class T> void unique(std::vector<T> &v) {
  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());
}

#line 2 "main.cpp"

long long solve(long long A, long long B, long long C) {
  vi loop;
  int now = A % 10;
  loop.push_back(now);
  while (true) {
    int next = (now * (A % 10)) % 10;
    int len = loop.size();
    if (next == loop[0])
      break;
    loop.push_back(next);
    now = next;
  }

  ll mod = loop.size();
  ll res = 1;
  while (C) {
    if (C & 1) {
      res = res * B % mod;
    }
    B = B * B % mod;
    C /= 2;
  }

  return loop[(res - 1 + mod) % mod];
}

// generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator)
int main() {
  long long A, B, C;
  std::cin >> A >> B >> C;
  auto ans = solve(A, B, C);
  std::cout << ans << std::endl;
  return 0;
}

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

using namespace std;

int main() {
    long long A, B, C;
    cin >> A >> B >> C;

    if (A % 10 == 0) {
        cout << 0 << endl;
        return 0;
    }

    vector<int> number;
    {
        long long x = A % 10;
        number.push_back(x);

        for (int i=0; i<10; i++) {
            x *= A;
            x %= 10;
            if (x == A % 10) break;
            number.push_back(x);
        }
    }

    vector<int> numberB;
    {
        int R = number.size();
        long long x = B % R;
        if (x == 0) {
            cout << number.at(number.size()-1) << endl;
            return 0;
        }
        numberB.push_back(x);
        for (int i=0; i<10; i++) {
            x *= B;
            x %= R;
            if (x == B % R) break;
            numberB.push_back(x);
        }
    }

    cout << number.at(
        ((int)(
            numberB.at((C-1) % numberB.size())
        )-1) % number.size()) << endl;

    return 0;
}


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

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef pair<int,int> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

constexpr double EPS = 1e-9;
constexpr double PI  = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 998244353;

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

void Main(){
    ll A,B,C; cin >> A >> B >> C;
    ll a = A*(A+1)/2 % MOD;
    ll b = B*(B+1)/2 % MOD;
    ll c = C*(C+1)/2 % MOD;
    ll ans = a*b%MOD;
    ans = ans*c%MOD;
    cout << ans << en;
    return;
}

int main(void){
    cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
    int t=1; //cin>>t;
    REP(_,t) Main();
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define Mp make_pair
#define pb push_back

using ll = long long;
using db = double;
using pii = pair<int, int>;
using vi = vector<int>;
mt19937 mrand(time(0));
ll get(ll r) { return ((ll)mrand() * mrand() % r + r) % r; }
ll get(ll l, ll r) { return get(r - l + 1) + l; }

ll n, K, P, f[110][110*110*110], up;
signed main() {
    scanf("%lld %lld %lld", &n, &K, &P), up = K * n * (n - 1) / 2, f[0][0] = 1;
    for(int i = 1; i < n; i++) {
        for(int j = 0; j <= up; j++) {
            f[i][j] = f[i - 1][j];
            if(j >= i) (f[i][j] += f[i][j - i]) %= P;
        }
        for(int j = up; j >= (K + 1) * i; j--)
            (f[i][j] += P - f[i][j - (K + 1) * i]) %= P;
    }
    for(int i = 1; i <= n; i++) {
        ll nw = 0;
        for(int j = 0; j <= up; j++)
            (nw += f[i - 1][j] * f[n - i][j]) %= P;
        printf("%lld\n", (nw * (K + 1) % P - 1 + P) % P);
    }
    fprintf(stderr, "time=%.4f\n", (db)clock()/CLOCKS_PER_SEC);
    return 0;
}


// Template
#include "bits/stdc++.h"
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define per(i, n) for (ll i = (ll)(n) - 1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003LL;
template <typename T>
inline bool chmin(T &x, const T &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T>
inline bool chmax(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}
struct IOSET {
    IOSET() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(10);
    }
} ioset;
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
    for (T &element : vec) is >> element;
    return is;
}
template <typename T>
vector<T> operator--(vector<T> &vec) {
    for (T &element : vec) --element;
    return vec;
}

// Mod-Int
template <int MOD> // MOD > 1
struct ModInt {
    static_assert(MOD >= 1);
    long long val;
    ModInt() = default;
    ModInt(long long x) : val(x % MOD + (x < 0 ? MOD : 0)) {}
    ModInt<MOD> &operator+= (const ModInt<MOD> &x) {
        if ((val += x.val) >= MOD) val -= MOD;
        return *this;
    }
    ModInt<MOD> &operator-= (const ModInt<MOD> &x) {
        if ((val -= x.val) < 0) val += MOD;
        return *this;
    }
    ModInt<MOD> &operator*= (const ModInt<MOD> &x) {
        (val *= x.val) %= MOD; return *this;
    }
    ModInt<MOD> operator+ (const ModInt<MOD> &x) const {
        return ModInt(*this) += x;
    }
    ModInt<MOD> operator- (const ModInt<MOD> &x) const {
        return ModInt(*this) -= x;
    }
    ModInt<MOD> operator* (const ModInt<MOD> &x) const {
        return ModInt(*this) *= x;
    }
    ModInt<MOD> pow(long long t) const {
        assert(t >= 0);
        ModInt<MOD> res(1), now(*this);
        while (t) {
            if (t & 1) res *= now;
            now *= now;
            t >>= 1;
        }
        return res;
    }
    ModInt<MOD> inv() const {
        return pow(MOD - 2);
    }
    ModInt<MOD> &operator/= (const ModInt<MOD> &x) {
        return *this *= x.pow(MOD - 2);
    }
    ModInt<MOD> operator/ (const ModInt<MOD> &x) const {
        return ModInt(*this) /= x;
    }
    friend std::istream &operator>> (std::istream &is, ModInt<MOD> &x) {
        long long a;
        is >> a;
        x = ModInt<MOD>(a);
        return is;
    }
    friend std::ostream &operator<< (std::ostream &os, const ModInt<MOD> &x) {
        os << x.val;
        return os;
    }
};

constexpr int mod = 1000000007;
using mint = ModInt<mod>;

// Main
int main() {
    int h, w;
    cin >> h >> w;
    vector<string> s(h);
    rep(i, h) cin >> s[i];

    int k = 0;
    rep(i, h) rep(j, w) if (s[i][j] == '.') ++k;

    vector<vector<int>> a(h, vector<int>(w, 0));
    rep(i, h) rep(j, w) {
        if (s[i][j] == '.') ++a[i][j];
    }
    vector<vector<int>> b = a, c = a, d = a;
    rep(i, h) rep(j, w - 1) {
        if (s[i][j] == '.') a[i][j + 1] += a[i][j];
    }
    rep(i, h) for (int j = w - 1; j > 0; --j) {
        if (s[i][j] == '.') b[i][j - 1] += b[i][j];
    }
    rep(i, h - 1) rep(j, w) {
        if (s[i][j] == '.') c[i + 1][j] += c[i][j];
    }
    for (int i = h - 1; i > 0; --i) rep(j, w) {
        if (s[i][j] == '.') d[i - 1][j] += d[i][j];
    }

    mint ans(0);
    rep(i, h) rep(j, w) {
        if (s[i][j] == '#') continue;
        int x = a[i][j] + b[i][j] + c[i][j] + d[i][j] - 3;
        ans += (mint(2).pow(x) - mint(1)) * mint(2).pow(k - x);
    }
    cout << ans << "\n";
}

#include <bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
//using namespace boost::multiprecision;
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> P;
#define PI 3.141592653589793
#define MOD 1000000007
//#define MOD 998244353
#define ALL(obj) (obj).begin(),(obj).end()
const ll INF = 1LL << 60;
//四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};

struct edge{//グラフに使うヤツ
    ll from,to,cost;
};

typedef vector<vector<edge> > G;

ll gcd(ll a,ll b){
    if (a%b==0)return(b);
    else return(gcd(b,a%b));
}

ll myPow(ll x,ll n,ll m){
  if(n == 0)
    return 1;
  if(n % 2 == 0)
    return myPow(x * x % m, n / 2, m);
  else
    return x * myPow(x, n - 1, m) % m;
}

ll modinv(ll a, ll m) {
    ll b = m, u = 1, v = 0;
    while (b) {
        ll t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

int main(){
    ll h,w;
    cin >> h >> w;
    string s[h];
    for (ll i=0;i<h;i++)cin >> s[i];
    ll tate[h][w],yoko[h][w];
    memset(tate,-1,sizeof(tate));
    memset(yoko,-1,sizeof(yoko));
    ll lc=0;
    for (ll i=0;i<h;i++){
        for (ll j=0;j<w;j++){
            ll c=0,f=j;
            for (ll k=j;s[i][k]=='.' and k<w;k++){
                c++;
                f=k;
                lc++;
            }
            for (ll k=j;k<=f;k++){
                yoko[i][k]=c;
            }
            j=f;
        }
    }
    for (ll i=0;i<w;i++){
        for (ll j=0;j<h;j++){
            ll c=0,f=j;
            for (ll k=j;s[k][i]=='.' and k<h;k++){
                c++;
                f=k;
            }
            for (ll k=j;k<=f;k++){
                tate[k][i]=c;
            }
            j=f;
        }
    }
    ll ans=0;
    ll lcm=myPow(2,lc,MOD),inv=modinv(2,MOD);
    int ni[h+w+10];
    ll a=1;
    for (ll i=0;i<h+w+5;i++){
        ni[i]=a;
        a*=2;
        a%=MOD;
    }
    for (ll i=0;i<h;i++){
        for (ll j=0;j<w;j++){
            if (s[i][j]=='.'){
                ll c=tate[i][j]+yoko[i][j]-1;
                c=ni[c];
                ll x=(c-1)*lcm%MOD;
                x*=modinv(c,MOD);
                x%=MOD;
                ans+=x;
                ans%=MOD;
            }
        }
    }
    cout << ans << endl;
    return 0;
}


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

#define MP make_pair
#define PB push_back
#define ALL(x)      (x).begin(),(x).end()
#define REP(i,n)    for(int i=0;i<(n);i++)
#define REP1(i,n)   for(int i=1;i<(n);i++)
#define REP2(i,d,n) for(int i=(d);i<(n);i++)
#define RREP(i,n)   for(int i=(n);i>=0;i--)
#define CLR(a)      memset((a),0,sizeof(a))
#define MCLR(a)     memset((a),-1,sizeof(a))
#define RANGE(x,y,maxX,maxY) (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI > VVI;
typedef vector<string> VS;
typedef vector<LL> VLL;
typedef pair<int,int> PII;

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;

const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};

int main(){
    LL N;
    cin >> N;
    VI P(N);
    VI P_index(N);

    REP(i, N) {
        cin >> P[i];
        P[i]--;
        P_index[P[i]] = i;
    }

    set<int> swap_idx;
    VI ans;
    REP(i, N-1) {
        while(i != P_index[i]) {
            int prev_num = P[P_index[i]-1];
            if (swap_idx.count(P_index[i])) {
                cout << -1 << endl;
                return 0;
            }

            swap_idx.insert(P_index[i]);
            ans.PB(P_index[i]);
            swap(P[P_index[i]], P[P_index[i]-1]);
            swap(P_index[i], P_index[prev_num]);
        }
    }

    if (swap_idx.size() == N-1) {
        REP(i, ans.size()) {
            cout << ans[i] << endl;
        }
    } else {
        cout << -1 << endl;
    }
}

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

#define INF 9999999999999999
template<class T>
bool chmin(T& a,T b)
{
    if(a > b)
    {
	a = b;
	return true;
    }
    else
    {
	return false;
    }
}

struct Edge
{
    long long to;
    long long weight;
    long long time;
};

long long calc(long long nowminute,long long time)
{
    if(nowminute % time == 0) return nowminute;
    else return(nowminute + time - nowminute % time);
}

using Graph = vector<vector<Edge>>;
using P = pair<long long,long long>;
int main()
{
    long long N,M,X,Y;
    cin >> N >> M >> X >> Y;
    X--; Y--;
    Graph G(N);
    vector<long long> dp(N);
    vector<bool> kakutei(N);
    for(long long i = 0;i < M;i++)
    {
        long long tmpa,tmpb,tmpt,tmpk;
        cin >> tmpa >> tmpb >> tmpt >> tmpk;
        tmpa--; tmpb--;
        Edge tmpedge1,tmpedge2;
        tmpedge1.to = tmpb;
        tmpedge2.to = tmpa;
        tmpedge1.weight = tmpedge2.weight = tmpt;
        tmpedge1.time = tmpedge2.time = tmpk;
        G[tmpa].push_back(tmpedge1);
        G[tmpb].push_back(tmpedge2);
    }
    priority_queue<P,vector<P>,greater<P>> PQ;
    kakutei.assign(N,false);
    dp.assign(N,INF);
    dp[X] = 0;
    PQ.push(make_pair(0,X));
    while(!PQ.empty())
    {
        auto tmppair = PQ.top();
        PQ.pop();
        if(kakutei[tmppair.second]) continue;
        kakutei[tmppair.second] = true;
        for(auto nextedge : G[tmppair.second])
        {
            if(kakutei[nextedge.to]) continue;
            if(chmin(dp[nextedge.to],calc(dp[tmppair.second],nextedge.time)+nextedge.weight))
            {
                PQ.push(make_pair(dp[nextedge.to],nextedge.to));
            }
        }
    }
    if(dp[Y] == INF) cout << -1 << endl;
    else cout << dp[Y] << endl;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second

/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
template <class T> inline void chmin(T &a, const T& b) { if (b < a) a = b; }
template <class T> inline void chmax(T &a, const T& b) { if (b > a) a = b; }
/* clang-format on */

const int MAX = 505000 + 10;

int main() {
#ifdef DEBUG
  ifstream ifs("in.txt");
  cin.rdbuf(ifs.rdbuf());
#endif
  int N, K, M;
  while (cin >> N >> K >> M) {
    auto dp = make_v(N + 1, MAX, 0);
    auto sum = make_v(MAX, 0);
    dp[0][0] = 1;
    for (int n = 1; n <= N; n++) {
      for (int w = 0; w < MAX; w++) {
        sum[w] = (dp[n - 1][w] + (w - n >= 0 ? sum[w - n] : 0)) % M;
      }
      for (int w = 0; w < MAX; w++) {
        dp[n][w] = (sum[w] - (w - n * (K + 1) >= 0 ? sum[w - n * (K + 1)] : 0) + M) % M;
      }
    }
    for (int x = 1; x <= N; x++) {
      int res = 0;
      for (int w = 0; w < MAX; w++) {
        res = (res + 1ll * dp[x - 1][w] * dp[N - x][w] % M) % M;
      }
      res = (1ll * res * (K + 1)) % M;
      res = (res - 1 + M) % M;
      cout << res << endl;
    }
  }
  return 0;
}

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<vector<T>>;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define siz(v) (ll)(v).size()
#define rep(i, a, n) for(ll i = a; i < (ll)(n); ++i)
#define repr(i, a, n) for(ll i = n - 1; (ll)a <= i; --i)
#define ENDL '\n'
typedef pair<int, int> Pi;
typedef pair<ll, ll> PL;
constexpr ll INF = 1000000099;
constexpr ll LINF = (ll)(1e18 + 99);
const ld PI = acos((ld)-1);
const vector<ll> dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1};
template <typename T, typename U> inline bool chmin(T& t, const U& u) {
  if(t > u) {
    t = u;
    return 1;
  }
  return 0;
}
template <typename T, typename U> inline bool chmax(T& t, const U& u) {
  if(t < u) {
    t = u;
    return 1;
  }
  return 0;
}
template <typename T> inline T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

template <typename T, typename Y> inline T mpow(T a, Y n) {
  T res = 1;
  for(; n; n >>= 1) {
    if(n & 1) res = res * a;
    a = a * a;
  }
  return res;
}

template <typename T> V<T> prefix_sum(const V<T>& v) {
  int n = v.size();
  V<T> ret(n + 1);
  rep(i, 0, n) ret[i + 1] = ret[i] + v[i];
  return ret;
}

template <typename T> istream& operator>>(istream& is, vector<T>& vec) {
  for(auto&& x : vec) is >> x;
  return is;
}

template <typename T, typename Y>
ostream& operator<<(ostream& os, const pair<T, Y>& p) {
  return os << "{" << p.fs << "," << p.sc << "}";
}

template <typename T> ostream& operator<<(ostream& os, const V<T>& v) {
  os << "{";
  for(auto e : v) os << e << ",";
  return os << "}";
}

template <typename... Args> void debug(Args&... args) {
  for(auto const& x : {args...}) { cerr << x << ' '; }
  cerr << ENDL;
}

ll dp[110][1000010] = {};

signed main() {
  cin.tie(0);
  cerr.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  ll n, k,mod;
  cin >> n >> k >> mod;
  V<ll> ans(n + 1, 0);

  dp[1][0] = 1;

  rep(i, 1, n) {
    rep(j,0,n*n*k+1){
      if(dp[i][j]==0)continue;

      (dp[i + 1][j] += dp[i][j]) %= mod;
      if(j+(k+1)*i<n*n*k+1)(dp[i + 1][j+(k+1)*i] += mod-dp[i][j]) %= mod;

    }
    rep(j,0,n*n*k+1){
      if(j+i<n*n*k+1)(dp[i+1][j+i]+=dp[i+1][j])%=mod;
    }
  }

  rep(i, 1, (n + 3) / 2) {
    ll x=max(i-1,n-i);
    rep(j,0,x*x*k +1){
      ll tmp=(dp[i][j]*dp[n-i+1][j])%mod;
      tmp%=mod;
      tmp*=k+1;
      tmp%=mod;

      (ans[i]+=tmp)%=mod;
    }
    ans[i]+=mod-1;//empty set
    ans[i]%=mod;
    ans[n - i + 1] = ans[i];
  }

  rep(i, 1, n + 1) cout << ans[i] << ENDL;
}
//! ( . _ . ) !
// CHECK overflow,vector_size,what to output?


// 問題の URL を書いておく
//

#include <bits/stdc++.h>

using namespace std;

//#define ENABLE_PRINT

#if defined(ENABLE_PRINT)

#define Print(v) \
do {\
    cout << #v << ": " << v << endl; \
}while(0)

#define PrintVec(v) \
do {\
    for(int __i = 0; __i < v.size(); ++__i) \
    { \
        cout << #v << "[" << __i << "]: " << v[__i] << endl; \
    }\
}while(0)

#define P(fmt, ...) printf(fmt, __VA_ARGS__)
#define LP printf("L: %d\n", __LINE__)

#else

#define Print(v) ((void)0)
#define PrintVec(v) ((void)0)
#define P(fmt, ...) ((void)0)
#define LP ((void)0)

#endif

#define rep(i, n) for(int i = 0; i < (int)(n); ++i)

using ll = long long;

const double PI = acos(-1);
using P = pair<int, int>;

int calcDist(const P& a, const P& b)
{
    return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second);
}

int calcMul(const P& a, const P& b)
{
    return a.first * b.second - b.first * a.second;
}

pair<P, int> calcDistAndMul(const P& a, const P& b, const P& c)
{
    P dm;
    dm.first = calcDist(a, c);
    dm.second = calcDist(b, c);
    P v0 = {c.first - a.first, c.second - a.second};
    P v1 = {c.first - b.first, c.second - b.second};

    auto mul = calcMul(v0, v1);
    return {dm, mul};
}

int main(int, const char**)
{
    int N;
    cin >> N;
    vector<P> S(N), T(N);
    rep(i, N) cin >> S[i].first >> S[i].second;
    rep(i, N) cin >> T[i].first >> T[i].second;

    if(N == 1) {
        cout << "Yes" << endl;
        return 0;
    }
    if(N == 2)
    {
        if(calcDist(S[0], S[1]) == calcDist(T[0], T[1]))
        {
            cout << "Yes" << endl;
        }
        else
        {
            cout << "No" << endl;
        }
        return 0;
    }

    vector<pair<P, int>> md(N - 2);
    rep(i, N - 2)
    {
        auto ti = i + 2;
        md[i] = calcDistAndMul(T[0], T[1], T[ti]);
    }

    sort(md.begin(), md.end());
#if 1
    rep(i, N - 2)
    {
        P("[%d]: (%d, %d, %d)\n", i, md[i].first.first, md[i].first.second, md[i].second);
    }
#endif

    rep(i, N) rep(j, N)
    {
        if(i == j) continue;
        P("i, j = (%d, %d)-----------------\n", i, j);
        vector<pair<P, int>> d(N - 2);
        int dcount = 0;
        rep(k, N)
        {
            if(k == i || k == j) continue;
            d[dcount++] = calcDistAndMul(S[i], S[j], S[k]);
        }
        sort(d.begin(), d.end());

#if 1
        rep(k, dcount)
        {
            P("[%d]: (%d, %d, %d)\n", k, d[k].first.first, d[k].first.second, d[k].second);
        }
#endif
        bool valid = true;
        rep(k, dcount)
        {
            if(md[k] != d[k])
            {
                valid = false;
                break;
            }
        }
        if(valid)
        {
            cout << "Yes" << endl;
            return 0;
        }
    }

    cout << "No" << endl;
    return 0;
}

#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
using namespace std;
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define reps(i,s,n) for (int i = (int)(s); i < (int)(n); i++)
#define prl(a) cout << (a) << endl
#define allrange(a) a.begin(),a.end()

bool solve_translated(vector<pair<int,int>> &S,vector<pair<int,int>> &T){
    int N = S.size();
    int dx = S[0].first-T[0].first;
    int dy = S[0].second-T[0].second;
    bool flg = true;
    reps(i,1,min(N,20)){
        if(!((S[i].first==T[i].first+dx) && (S[i].second==T[i].second+dy))){flg = false; break;}
    }
    return flg;
}
vector<pair<int,int>> Pitagora_rot(vector<pair<int,int>> &S,int a, int b , int c){
    int N = S.size();
    vector<pair<int,int>> PS(N);
    auto pt0 = S[0];
    PS[0] = pt0;
    bool flg = true;
    int dx,dy,x,y;
    reps(i,1,N){
        dx = S[i].first - pt0.first;
        dy = S[i].second - pt0.second;
        x=dx*a-dy*b;
        y=dx*b+dy*a;
        if(((x%c)!=0) || ((y%c)!=0)) {flg = false; break;}
        PS[i] = make_pair(x/c+pt0.first, y/c+pt0.second);
    }
    if(flg) return PS;
    else return vector<pair<int,int>>();
}

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

    vector<pair<int,int>> S(N),T(N);
    rep(i,N)
    {
        int x,y;
        cin >> x >> y;
        S[i].first = x;S[i].second = y;
    }

    rep(i,N)
    {
        int x,y;
        cin >> x >> y;
        T[i].first = x;
        T[i].second = y;
    }
    sort(allrange(T));

/*ピタゴラ三角形
    5	12	13
    8	15	17
    3	4	5
*/
    constexpr int tri[12][3] ={
    {3,4,5},
    {4,3,5},
    {-3,4,5},
    {-4,3,5},
    {0,1,1},
    {1,0,1},
    {-1,0,1},
    {0,-1,1},
    {-3,-4,5},
    {-4,-3,5},
    {3,-4,5},
    {4,-3,5}
    };

 /*
    vector<vector<int>> tri(0);
    tri.push_back(vector<int>({3,4,5}));
    tri.push_back(vector<int>({4,3,5}));
    tri.push_back(vector<int>({-3,4,5}));
    tri.push_back(vector<int>({-4,3,5}));
    tri.push_back(vector<int>({0,1,1}));
    tri.push_back(vector<int>({1,0,1}));
    tri.push_back(vector<int>({-1,0,1}));
    tri.push_back(vector<int>({0,-1,1}));
    tri.push_back(vector<int>({-3,-4,5}));
    tri.push_back(vector<int>({-4,-3,5}));
    tri.push_back(vector<int>({3,-4,5}));
    tri.push_back(vector<int>({4,-3,5}));
 //   tri.push_back(vector<int>({-5, -12, 13}));
 //   tri.push_back(vector<int>({-12, -5, 13}));
 //   tri.push_back(vector<int>({-8,-15,17}));
 //   tri.push_back(vector<int>({-15,-8,17}));
 //   tri.push_back(vector<int>({5, 12, 13}));
 //   tri.push_back(vector<int>({12, 5, 13}));
 //   tri.push_back(vector<int>({8,15,17}));
 //   tri.push_back(vector<int>({15,8,17}));
 //   tri.push_back(vector<int>({-5, 12, 13}));
 //  tri.push_back(vector<int>({-12, 5, 13}));
  //  tri.push_back(vector<int>({-8,15,17}));
  //  tri.push_back(vector<int>({-15,8,17}));
 //   tri.push_back(vector<int>({5, -12, 13}));
 //   tri.push_back(vector<int>({12, -5, 13}));
 //   tri.push_back(vector<int>({8,-15,17}));
 //   tri.push_back(vector<int>({15,-8,17}));

 */
    bool flg;

    if(N==2){
        auto sx = S[0].first-S[1].first;
        auto sy = S[0].second-S[1].second;
        auto tx = T[0].first-T[1].first;
        auto ty = T[0].second-T[1].second;
        flg= (sx*sx+sy*sy==tx*tx+ty*ty);
    } else {
        rep(j,12){
            auto S2 = Pitagora_rot(S,tri[j][0],tri[j][1],tri[j][2]);
            if(S2.empty()) continue;
            sort(allrange(S2));
            flg = solve_translated(S2,T);
            if(flg) break;
        }
    }
    if(flg) prl("Yes"); else prl("No");
}


#include<bits/stdc++.h>
#define X first
#define Y second
#define eb emplace_back
#define pb push_back
#define print1d(x) for(auto i : x){ cout << i << " ";} cout << '\n'
#define print2d(x) for(auto u : x){ print1d(u);} cout << "------" << '\n'
#define CLR(x, y) memset(x, y, sizeof(x))
#define ALL(X) X.begin(), X.end()
#define io_o() ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0)
#define mp(x, y) make_pair(x, y)
#define mpp(w, x, y, z) make_pair(mp(w, x), mp(y, z))
#define For(i, n) for(int i = 0; i < n; i++)
#define For1(i, n, m) for(int i = n; i < m; i++)
#define For2(X) for(auto x : X)
#define maxn 200005
#define pi = acos(-1)
using namespace std;
typedef long long ll;
const ll mod = (1e9)+7;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
//priority_queue<ll> pq; priority_queue<pii,vector<pii>, greater<pii>> pq;
//sort(a, a+n, greater<int>());
pll b[maxn];

int main(){
    io_o(); //cute!
    ll n, m; cin >> n >> m;
    set<ll> pos;
    pos.insert(n);
    For(i, m) {
        cin >> b[i].X >> b[i].Y;
    }
    sort(b, b+m);
    vector<ll> add, era;
    for(int i = 0; i < m; i++) {
        // cout << i << '\n';
        ll x = b[i].X, y = b[i].Y;
        if(x == 0) continue;
        if(y-1 >= 0 && pos.count(y-1)) {
            add.push_back(y);
            // cout << "push " << y << '\n';
        }
        else if(y+1 <= 2*n && pos.count(y+1)) {
            add.push_back(y);
            // cout << "push " << y << '\n';
        }
        if(pos.count(y)) {
            era.push_back(y);
            // cout << "erase " << y << '\n';
        }
        if(i == m-1 || x != b[i+1].X) {
            for(auto ii: era) {
                if(pos.count(ii))
                    pos.erase(ii);
            }
            // cout << "pos\n";
            // for(auto a: pos) cout << a << ' '; cout << '\n';
            for(auto ii: add) pos.insert(ii);
            add.clear(); era.clear();
            // for(auto a: pos) cout << a << ' '; cout << '\n';
        }
    }
    cout << pos.size() << '\n';

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
long long c[64][64];
int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	c[0][0] = 1;
	for (int i = 1; i < 64; i++) {
		c[i][0] = c[i][i] = 1;
		for (int j = 1; j < i; j++) {
			c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
		}
	}
	int a, b;
	long long k;
	cin >> a >> b >> k; k--;
	string ans = "";
	int cnta = a;
	int cntb = b;
	for (int i = 0; i < a + b; i++) {
		if (!cnta) {
			ans += 'b';
			cntb--;
		} else if (!cntb) {
			ans += 'a';
			cnta--;
		} else {
			if (k < c[cnta + cntb - 1][cnta - 1]) {
				ans += 'a';
				cnta--;
			} else {
				k -= c[cnta + cntb - 1][cnta - 1];
				ans += 'b';
				cntb--;
			}
		}
	}
	cout << ans << "\n";
}


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
using ll = long long; using ull = unsigned long long;
using pii = pair<int, int>; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>;
using pll = pair<ll, ll>; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>;
using pdd = pair<double, double>; using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>;
using pbb = pair<bool, bool>; using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using pss = pair<string, string>; using vs = vector<string>; using vvs = vector<vs>; using vvvs = vector<vvs>;
#define repeat(counter, start, goal, step) for (int counter = (int)(start); counter < (int)(goal); counter += step)
#define rep(counter, times) repeat(counter, 0, times, 1)
#define rep2(counter, start, goal) repeat(counter, start, goal, 1)
#define rep0(times) repeat(counter, 0, times, 1)
#define rrepeat(counter, start, goal, step) for (int counter = (int)(start); counter > (int)(goal); counter -= step)
#define rep3(counter, start, goal) rrepeat(counter, start, goal, 1)
#define rrep(counter, start, goal) rrepeat(counter, start - 1, goal - 1, 1)
#define all(container) begin(container), end(container)
template <typename T> bool chmax(T &x, const T &y) { if (x < y) { x = y; return true; } return false; }
template <typename T> bool chmin(T &x, const T &y) { if (x > y) { x = y; return true; } return false; }
void yn(bool flag) { cout << (flag ? "yes" : "no") << "\n"; }
void Yn(bool flag) { cout << (flag ? "Yes" : "No") << "\n"; }
void YN(bool flag) { cout << (flag ? "YES" : "NO") << "\n"; }
void set_prec(const int &digits) { cout << fixed << setprecision(digits); cerr << fixed << setprecision(digits); }
template <typename T> void pr(const T &obj) { cerr << obj; }
template <typename T, typename ...Ts> void pr(const T &first, const Ts &...rest) { pr(first); pr(", "); pr(rest...); }
template <typename S, typename T> void pr(const pair<S, T> &pair) { pr("("); pr(pair.first); pr(", "); pr(pair.second); pr(")"); }
template <typename T> void pr(const vector<T> &vec) { pr("{"); for (T obj : vec) { pr(obj); pr(", "); } pr("}"); }
template <typename T> void pr(const vector<vector<T>> &vv) { pr("\n"); rep(index, vv.size()) { pr("["); pr(index); pr("]: "); pr(vv[index]); pr("\n"); } }
template <typename T> void pr(const set<T> &vec) { pr("{"); for (T obj : vec) { pr(obj); pr(", "); } pr("}"); }
template <typename S, typename T> void pr(const map<S, T> &map) { pr("{"); for (pair<S, T> pair : map) { pr("("); pr(pair.first); pr(": "); pr(pair.second); pr("), "); } pr("}"); }
#define db(obj) cerr << #obj << ": "; pr(obj); cerr << " "
#define dl(obj) db(obj); cerr << "\n";
#define dm(...) cerr << "(" << #__VA_ARGS__ << "): ("; pr(__VA_ARGS__); cerr << ") "
#define dml(...) dm(__VA_ARGS__); cerr << "\n"
// #include <atcoder/all>
// using namespace atcoder;
// const ll MOD = 1000000007;
// const ll MOD = 998244353;

int main() {
	int N, S, D; cin >> N >> S >> D;
	vi X(N), Y(N);
	rep(i, N) cin >> X[i] >> Y[i];
	bool f = false;
	rep(i, N) if (X[i] < S && Y[i] > D) f = true;
	Yn(f);
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
double dp[105][105][105];
double dfs(int a,int b,int c,int cnt){
    if(a==100||b==100||c==100){
        return cnt;
    }
    if(dp[a][b][c]) {
        return dp[a][b][c];
    }
    dp[a][b][c]=(a*dfs(a+1,b,c,cnt+1)+b*dfs(a,b+1,c,cnt+1)+c*dfs(a,b,c+1,cnt+1))*1.0/(a+b+c);
    return dp[a][b][c];
}
void solve(){
    int a,b,c;
    cin>>a>>b>>c;
    printf("%.10f",dfs(a,b,c,0));
}
int main() {
    int _=1;
    while(_--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll a;
    cin >> a;
    ll b;
    cin >> b;
    ll c;
    cin >> c;

    if((a*a+b*b)<c*c) {
        cout << "Yes" <<'\n';
    } else {
        cout << "No" <<'\n';
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; //int:2*10**9
typedef long double ld;
typedef pair<ll,ll> P;
#define REP(i,n) for(ll i = 0; i<(ll)(n); i++)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define vec2(name,i,j,k) vector<vector<ll>> name(i,vector<ll>(j,k))
#define vec3(name,i,j,k,l) vector<vector<vector<ll>>> name(i,vector<vector<ll>>(j,vector<ll>(k,l)))
#define pb push_back
#define MOD 1000000007 //998244353
#define PI 3.141592653
#define INF 100000000000000 //14
//cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
int main(){
  ll a, b, c; cin >> a >> b >> c;
  if (a>b) cout << "Takahashi" << endl;
  else if (a<b) cout << "Aoki" << endl;
  else {
    if (c==0) cout << "Aoki" << endl;
    else cout << "Takahashi" << endl;
  }
}