#include<iostream>
#include<vector>
using namespace std;
int win(int a,int b){
	if((b-a+3)%3==2)return b;
	else return a;
}
int main(){
	int n,k,size;
	string s,u;
	vector<int> v[102];
	cin>>n>>k>>s;
	for(int i=0;i<n;i++){
		if(s[i]=='R')v[0].push_back(0);
		else if(s[i]=='S')v[0].push_back(1);
		else if(s[i]=='P')v[0].push_back(2);
	}
	for(int i=0;i<k;i++){
		size=v[i].size();
		if(size%2){
			for(int j=0;j<size;j++)v[i].push_back(v[i][j]);
		}else size/=2;
		for(int j=0;j<size;j++){
			v[i+1].push_back(win(v[i][2*j],v[i][2*j+1]));
		}
	}
	if(v[k][0]==0)cout<<'R';
	else if(v[k][0]==1)cout<<'S';
	else if(v[k][0]==2)cout<<'P';
	return 0;
}

#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    vector<int> a(4);
    cin >> a[0] >> a[1] >> a[2] >> a[3];
    sort(a.begin(), a.end());
    int sum = a[0] + a[1] + a[2] + a[3];
    if (sum % 2) {
        cout << "No" << endl;
        return 0;
    }
    sum = sum / 2;
    if (a[3] == sum || a[3] + a[0] == sum) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
    return 0;
}


/*                         _
                        _ooOoo_
                       o8888888o
                       88" . "88
                       (| -_- |)
                       O\  =  /O
                    ____/`---'\____
                  .'  \\|     |//  `.
                 /  \\|||  :  |||//  \
                /  _||||| -:- |||||_  \
                |   | \\\  -  /'| |   |
                | \_|  `\`---'//  |_/ |
                \  .-\__ `-. -'__/-.  /
              ___`. .'  /--.--\  `. .'___
           ."" '<  `.___\_<|>_/___.' _> \"".
          | | :  `- \`. ;`. _/; .'/ /  .' ; |
          \  \ `-.   \_\_`. _.'_/_/  -' _.' /
===========`-.`___`-.__\ \___  /__.-'_.'_.-'================

                  Please give me AC.
*/

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <numeric>
#include <string>
#include <sstream>
#include <complex>
#include <bitset>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <chrono>
#include <random>

using namespace std;

using int64 = long long;
using uint64 = unsigned long long;
using vi = vector<int>;
using vl = vector<int64>;
using pii = pair<int, int>;
using pll = pair<int64, int64>;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) (v).begin(), (v).end()
#define print(x) cout << (x) << '\n'
#define print2(x, y) cout << (x) << ' ' << (y) << '\n'
#define print3(x, y, z) cout << (x) << ' ' << (y) << ' ' << (z) << '\n'
#define printn(v) rep(i, (v).size() - 1) cout << (v)[i] << ' '; cout << (v)[n - 1] << '\n';
#ifdef ONLINE_JUDGE
#define debug(x)
#define debug2(x, y)
#define debug3(x, y, z)
#define dbg(v)
#else
#define debug(x) cerr << #x << ": " << (x) << '\n'
#define debug2(x, y) cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << '\n'
#define debug3(x, y, z) cerr << #x << ": " << (x) << ", " << #y << ": " << (y) << ", " << #z << ": " << (z) << '\n'
#define dbg(v) for (size_t _ = 0; _ < v.size(); ++_){cerr << #v << "[" << _ << "] : " << v[_] << '\n';}
#endif

// constant
const int INF = (1<<30) - 1;
const int64 INF64 = (1LL<<62) - 1;

template<typename T> T gcd(T a, T b) {
    if (a < b) return gcd(b, a);
    T r;
    while ((r = a % b)) {
        a = b;
        b = r;
    }
    return b;
}

template<typename T> T lcm(const T a, const T b) {
    return a / gcd(a, b) * b;
}

template<typename S, typename T> bool chmin(S& a, const T& b) {
    if (a > b) return a = b, true; else return false;
}

template<typename S, typename T> bool chmax(S& a, const T& b) {
    if (a < b) return a = b, true; else return false;
}

// End of template.

int main() {
    cout << fixed << setprecision(15);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cerr.tie(nullptr);

    int t;
    cin >> t;
    rep(ti, t) {
        int64 l, r;
        cin >> l >> r;
        if (2 * l == r) {
            print(1);
            continue;
        }
        if (2 * l >= r) {
            print(0);
            continue;
        }
        print((r - 2 * l + 1) * (r - 2 * l + 2) / 2);
    }

    return 0;
}

/*
  __ _   ___ _ __  _ __
 / _` | / __| '_ \| '_ \
| (_| || (__| |_) | |_) |
 \__,_(_)___| .__/| .__/
            |_|   |_|
*/

#include <bits/stdc++.h>

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

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

int main() {
    int N; cin >> N;
    vector<int> R(pow(2, N));
    rep(i, pow(2, N)) cin >> R[i];

    int a_num = 1; int a_rate = R[0];
    int b_num = pow(2, N) / 2 + 1; int b_rate = R[pow(2, N) / 2];
    for(int i = 0; i < b_num - 1; i++) {
        if (chmax(a_rate, R[i])) a_num = i + 1;
    }
    for (int i = b_num - 1; i < pow(2, N); i++) {
        if (chmax(b_rate, R[i])) b_num = i + 1;
    }

    if (a_rate < b_rate) {
        cout << a_num << endl;
    } else {
        cout << b_num << endl;
    }
    // debug(a_num);
    // debug(a_rate);
    // debug(b_num);
    // debug(b_rate);
}


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

// #include <atcoder/all>
// using namespace atcoder;

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

bool ok(long long N, std::string &S, std::vector<long long> &A, int m) {
    rep (i, m) {
        vector<ll> tmp(N+1);
        rep(j, N+1) {
            tmp[j] = A[j] / m;
            if (i < A[j] % m) {
                tmp[j]++;
            }
        }
        rep(j, N) {
            if (
                (S[j] == '>' && !(tmp[j] > tmp[j+1])) ||
                (S[j] == '<' && !(tmp[j] < tmp[j+1]))
            ) {
                return 0;
            }
        }
    }
    return 1;
}

void solve(long long N, std::string S, std::vector<long long> A){
    int l = 1, r = 10000+1;
    while (r - l > 1) {
        int m = (l + r) / 2;
        if (ok(N, S, A, m)) {
            l = m;
        } else {
            r = m;
        }
    }
    cout << l << endl;
    rep(i, l) {
        rep(j, N+1) {
            int tmp = A[j] / l;
            if (i < A[j] % l) {
                tmp++;
            }
            cout << tmp << ((j == N) ? "\n" : " ");
        }
    }

    // vector<ll> B(N+1);
    // ll mn = 0;
    // vector<int> gts(N+1);
    // vector<int> lts(N+1);
    // rep(i, N) {
    //     if (S[i] == '<') {
    //         lts[i+1] = lts[i] + 1;
    //     } else {
    //         lts[i+1] = 0;
    //     }
    // }
    // for (int i = N-1; i >= 0; i--) {
    //     if (S[i] == '>') {
    //         gts[i] = gts[i+1] + 1;
    //     } else {
    //         gts[i] = 0;
    //     }
    // }
    // rep(i, N+1) {
    //     B[i] = max(gts[i], lts[i]);
    // }
    // vector<vector<ll>> ans;
    // while (1) {
    //     vector<ll> rest = A;
    //     int ok = 1;
    //     rep(i, N+1) {
    //         if (A[i] >= B[i]) {
    //             A[i] -= B[i];
    //         } else {
    //             ok = 0;
    //             break;
    //         }
    //     }
    //     if (ok) {
    //         rep(i, N) {
    //             if (
    //                 (S[i] == '>' && !(A[i] > A[i+1])) ||
    //                 (S[i] == '<' && !(A[i] < A[i+1]))
    //             ) {
    //                 ok = 0;
    //                 break;
    //             }
    //         }
    //     }
    //     if (!ok) {
    //         ans.push_back(rest);
    //         break;
    //     }
    //     ans.push_back(B);
    // }
    // cout << ans.size() << endl;
    // rep(i, ans.size()) {
    //     rep(j, ans[i].size()) {
    //         cout << ans[i][j] << ((j == ans[i].size() - 1) ? "\n" : " ");
    //     }
    // }

}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
int main(){
    long long N;
    scanf("%lld",&N);
    std::string S;
    std::cin >> S;
    std::vector<long long> A(N-0+1);
    for(int i = 0 ; i < N-0+1 ; i++){
        scanf("%lld",&A[i]);
    }
    solve(N, S, std::move(A));
    return 0;
}

#define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>
#include <cstring>
#include <cmath>
#include <stack>
#include <iomanip>
#include <tuple>
#include <functional>
#include <cfloat>
#include <map>
#include <set>
#include <array>
#include <stdio.h>
#include <string.h>
#include <random>
#include <cassert>
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using namespace std;
#define int long long
#define CONTAINS_VEC(v,n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a)+(size))
#define REMOVE(v,a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define ARY_REVERSE(v,a) (reverse((v), (v)+(a)))
#define REP(i, n) for (int (i)=0; (i) < (n); (i)++)
#define REPE(i, n) for (int (i)=0; (i) <= (n); (i)++)
#define CONTAINS_MAP(m, a) ((m).find((a)) != m.end())
#define CONTAINS_SET(m, a) ((m).find((a)) != m.end())
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; exit(0); }
void Yes() { cout << "Yes" << endl; exit(0); }
void No() { cout << "No" << endl; exit(0); }

int N;
string S;
int A[110];
int _m[110];

signed main()
{
	cin >> N;
	cin >> S;
	REP(i, N + 1) cin >> A[i];

	int k = INT_MAX;
	for (int i = 0; i < N; i++)
	{
		int diff = abs(A[i] - A[i + 1]);
		k = min(diff, k);
	}

	for (int i = 0; i < N + 1; i++)
	{
		_m[i] = A[i] % k;
	}

	cout << k << endl;
	for (int i = 0; i < k; i++)
	{
		for (int i = 0; i < N + 1; i++)
		{
			int m = _m[i] > 0 ? 1 : 0;
			cout << A[i] / k + m << " ";
			_m[i]--;
		}
		cout << endl;
	}
}


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

using namespace std;

typedef long long ll;

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

ll chk(ll x){
	ll cnt = 0;
	while(x > 1 && x % 5 == 0) {
		x /= 5;
		cnt++;
	}
	if(x == 1) return cnt;
	return 0;
}

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

	ll n;
	cin >> n;

	ll x = 1;
	ll cnt = 0;
	while(((double) x * 3) <= n){
		x *= 3;
		cnt++;
		ll t = chk(n - x);
		if(t > 0){
			cout << cnt << " " << t << endl;
			exit(0);
		}
	}

	cout << -1 << endl;
}

#include<bits/stdc++.h>
using namespace std;
#define lli         long long int

#define endl            "\n"
#define pb          push_back
void input()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
}
const long long mod=1e18;
void solve()
{
    lli n;
    cin>>n;
    lli power=1LL;
    vector<lli>v3;
    vector<lli>v5;
    while(power<mod)
    {
        v3.pb(power);
        power *= 3;
    }
    power=1LL;
    while(power<mod)
    {
        v5.pb(power);
        power *= 5;
    }
    for(int i=1;i<v3.size();i++)
    {
        for(int j=1;j<v5.size();j++)
        {
            if(v3[i]+v5[j]==n)
            {
                cout<<i<<" "<<j<<endl;
               return;
            }
        }
    }
    cout<<"-1"<<endl;
}

int main(){
   // input();
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(0);
    solve();

}


#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <climits>

#define rep(i, n) for(int i = 0; i < n; i++)
#define per(i, n) for(int i = n - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define mod 1000000007
using namespace std;
template<class T, class U>
T &chmax(T &a, const U &b){ if(a < b) a = b; return a; }
template<class T, class U>
T &chmin(T &a, const U &b){ if(a > b) a = b; return a; }
int main(){
  int n;
  cin >> n;
  vi a(n);
  rep(i, n){
    cin >> a[i];
    a[i] %= 200;
  }
  vector<vl> dp(n + 1, vl(200));
  vector<vvi> root(n + 1, vvi(200));

  vi ans1, ans2;
  dp[0][0] = 1;
  rep(i, n){
    rep(j, 200){
      if(dp[i + 1][j] <= dp[i][j] + dp[i][(j - a[i] + 200) % 200]){
        dp[i + 1][j] = dp[i][j] + dp[i][(j - a[i] + 200) % 200];
        if(dp[i][(j - a[i] + 200) % 200] >= 1){
          root[i + 1][j] = root[i][(j - a[i] + 200) % 200];
          root[i + 1][j].push_back(i);
        }else if(dp[i][j] >= 1){
          root[i + 1][j] = root[i][j];
        }
        if(dp[i][j] >= 1 && dp[i][(j - a[i] + 200) % 200] >= 1 && root[i][j].size() >= 1){
          ans1 = root[i][j];
          ans2 = root[i][(j - a[i] + 200) % 200];
          ans2.push_back(i);
          goto out;
        }
      }
    }
  }
  cout << "No\n";
  return 0;
  out:
  cout << "Yes\n";
  int l = ans1.size();
  int r = ans2.size();
  cout << l << " ";
  rep(i, l - 1) cout << ans1[i] + 1 << " ";
  cout << ans1[l - 1] + 1 << "\n";
  cout << r << " ";
  rep(i, r - 1) cout << ans2[i] + 1 << " ";
  cout << ans2[r - 1] + 1 << "\n";
}

#include <bits/stdc++.h>

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

using namespace std;
typedef pair<int,int> P;
typedef long long ll;

int main()
{
    int n,k,m;
    cin >> n >> k >> m;

    ll sum = 0;
    rep(i,n-1){
        int t;
        cin >> t;
        sum += t;
    }

    int est = n*m - sum;

    if(est < 0){
        est = 0;
    }

    if(est <= k){
        cout << est << endl;
        return 0;
    }

    cout << -1 << endl;

    return 0;
}


#include <iostream>
#include <cmath>

long long n;

int main() {
    std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
    std::cin >> n;
    std::cout << n - (long long)std::ceil((std::sqrt((n + 1) * 8 + 9) - 3) / 2) + 1 << '\n';
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    cin>>s;
    int l=s.size();
    if(s[0]=='.')cout<<0<<endl;
    else{
        for(int i=0;i<l;i++)
    {
        if(s[i]=='.')break;
        else
        {
          cout<<s[i];
            }
    }
     cout<<endl;
    }

}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cctype>
#include <cmath>
#include <vector>
#include <set>
#include <bitset>
#include <map>
#include <stack>
#include <queue>
#include <ctime>
#include <cassert>
#define _for(i,a,b) for(int i=(a);i<(b);++i)
#define _rep(i,a,b) for(int i=(a);i<=(b);++i)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long LL;
inline int read_int(){
    int t=0;bool sign=false;char c=getchar();
    while(!isdigit(c)){sign|=c=='-';c=getchar();}
    while(isdigit(c)){t=(t<<1)+(t<<3)+(c&15);c=getchar();}
    return sign?-t:t;
}
inline LL read_LL(){
    LL t=0;bool sign=false;char c=getchar();
    while(!isdigit(c)){sign|=c=='-';c=getchar();}
    while(isdigit(c)){t=(t<<1)+(t<<3)+(c&15);c=getchar();}
    return sign?-t:t;
}
inline char get_char(){
    char c=getchar();
    while(c==' '||c=='\n'||c=='\r')c=getchar();
    return c;
}
inline void write(LL x){
    char c[21],len=0;
    if(!x)return putchar('0'),void();
    if(x<0)x=-x,putchar('-');
    while(x)c[++len]=x%10,x/=10;
    while(len)putchar(c[len--]+48);
}
inline void space(LL x){write(x),putchar(' ');}
inline void enter(LL x){write(x),putchar('\n');}
LL cal(LL n,LL p){
	int pos=3;
	LL ans=0;
	while(p>0){
		LL t=n;
		while(t>=p){
			t-=p;
			ans++;
		}
		if(pos==3)pos=4;
		else pos=3;
		n=p;
		p=t;
	}
	return ans+n;
}
int main()
{
//	freopen("test.in","r",stdin);
//	freopen("test.out","w",stdout);
	LL n=read_LL();
	LL v=n*2/(sqrt(5)+1);
	for(LL i=max(v-20,0LL);i<=min(v+20,n);i++){
		if(cal(n,i)<125){
			LL p=i;
			stack<int> s;
			int pos=3;
			while(p>0){
				LL t=n;
				while(t>=p){
					t-=p;
					s.push(pos);
				}
				if(pos==3)pos=4;
				else pos=3;
				n=p;
				p=t;
			}
			enter(s.size()+n);
			LL tn=0,tp=0;
			_for(i,0,n){
				if(pos==3){
					tn++;
				}
				else
				tp++;
				enter(pos-2);
			}
			while(!s.empty()){
				enter(s.top());
				if(s.top()==3)
				tn=tn+tp;
				else
				tp=tn+tp;
				s.pop();
			}
			return 0;
		}
	}
	return 0;
}

#include<bits/stdc++.h>

#define st first
#define nd second
#define pb(x) push_back(x)
#define pp(x) pop_back(x)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define rev(x) reverse(all(x))
#define sor(x) sort(all(x))
#define sz(x) (int)(x).size()
#define rsz(x) resize(x)

using namespace std;

///~~~~~~~~~~~~~~~~~~~~~~~~~~

void debug(){cerr<<"\n";}
template <typename H, typename... T>
void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);}
#define deb(x...) cerr<<#x<<" = ";debug(x);

///~~~~~~~~~~~~~~~~~~~~~~~~~

typedef __int128 ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii > vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef string str;

#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

const int N=1e3+5, mod=1e9+7, INF=1e9+5;
int dp[N][N], par[N][N];
vector<int> solve(ll x, ll y){
	vector<int> V;
	while((x>=N || y>=N) && x>0 && y>0){
		if(x>y){
			/*while(5*y*y<(2*x-y)*(2*x-y)){
				V.pb(1);
				x--;
			}*/
			if(x>=y){
				x-=y;
				V.pb(3);
			}
		}
		else{
			/*while(5*x*x<(2*y-x)*(2*y-x)){
				V.pb(2);
				y--;
			}*/
			if(y>=x){
				y-=x;
				V.pb(4);
			}
		}
		if(V.size()>1000)return vi(1000);
	}
	if(x>=N || y>=N)return vi(1000);
		while(x || y){
			V.pb(par[x][y]);
			if(par[x][y]==1)x--;
			else if(par[x][y]==2)y--;
			else if(par[x][y]==3)x-=y;
			else y-=x;
			if(V.size()>1000)return vi(1000);
		}
	return V;
}
int main(){
	for(int i=0; i<N; i++){
		for(int j=0; j<N; j++){
			dp[i][j]=mod;
		}
	}
	dp[0][0]=0;
	for(int i=0; i<N; i++){
		for(int j=0; j<N; j++){
			if(i>0 && dp[i][j]>dp[i-1][j]){
				par[i][j]=1;
				dp[i][j]=dp[i-1][j]+1;
			}
			if(j>0 && dp[i][j]>dp[i][j-1]){
				par[i][j]=2;
				dp[i][j]=dp[i][j-1]+1;
			}
			if(i>=j && dp[i][j]>dp[i-j][j]){
				par[i][j]=3;
				dp[i][j]=dp[i-j][j]+1;
			}
			if(i<=j && dp[i][j]>dp[i][j-i]){
				par[i][j]=4;
				dp[i][j]=dp[i][j-i]+1;
			}
		}
	}
	//BOOST;
	long long n;
	cin>>n;
	ld phi=1.618033988749894848204586;
	ll x=n, y=n/phi;
	while(5*y*y<(2*x-y)*(2*x-y)){
		y++;
	}
	while(5*y*y>=(2*x-y)*(2*x-y)){
		y--;
	}
	vector<int> opt(1000);
	y+=50;
	for(int i=0; i<100 && i<=y; i++){
		vi V=solve(x, y-i);
		if(V.size()<opt.size())opt=V;
	}
	rev(opt);
	cout<<opt.size()<<"\n";
	for(int i:opt)cout<<i<<"\n";
}


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

int main() {
    int t;
    cin >> t;

    while(t--) {
        int n;
        cin >> n;
        string s1, s2, s3;
        cin >> s1 >> s2 >> s3;
        rep(i, n) cout << '1';
        rep(i, n) cout << '0';
        cout << '1';
        cout << endl;
        }
    }

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

int main()
{
  int n;
  cin >> n;

  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p.at(i);
  }

  int min = 0;
  set<int> numSet;
  for (int num : p) {
    numSet.insert(num);
    while(numSet.count(min)) {
      min++;
    }
    cout << min << endl;
  }
}


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

int main() {
int x;
  cin >> x;
  cout << max(x, 0) << endl;
}

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

int main()
{
	long long n;
	cin >> n;
	set< long long > ans;
	for(long long i=1;i*i<=n;i++){
		if(n%i == 0){
			ans.insert(i);
			ans.insert(n/i);
		}
	}

	for(auto x : ans ) cout << x << endl;
	return 0;
}


#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define rep(i, n) for (int i=0; i<n; i++)
#define pb push_back
#define int long long
typedef pair<int, int> P;

int N;
int A[10010];

signed main() {
    //ループ変数が被っていないか?
    //制約を確認しているか?
    //変数のtypoがないか?
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> N;
    rep(i, N) cin >> A[i];
    vector<P> v;
    rep(i, N) v.pb(P(A[i], i));
    sort(v.begin(), v.end());
    set<int> s;
    s.insert(-1); s.insert(N);

    int ans = 0;
    rep(i, N) {
        int val = v[i].first;
        int idx = v[i].second;
        auto it = s.lower_bound(idx);
        int r = *it;
        int l = *--it;
        ans = max(ans, (r-l-1)*val);
        //cout << idx << " " << val << " " << l << " " << r << endl;
        s.insert(idx);
    }

    cout << ans << endl;
}

#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << "Line(" << __LINE__ << ") -> " << #x << " is " << (x) << endl

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;

const int INF = 1e9 + 7, N = 1e5 + 7;

int a[N], b[N];

signed main()
{
	IO_OP;

	int n;
	cin >> n;
	ll sum = 0;
	V<pi> v;
	for(int i = 0; i < n; i++) cin >> a[i];
	for(int i = 0; i < n; i++) {
		cin >> b[i];
		sum += b[i];
		v.EB(a[i] - b[i], i & 1);
	}
	sort(ALL(v), greater<pi>());
	ll cur = 0, mx = 0;
	queue<int> q[2];
	for(pi p:v) {
		q[p.S].push(p.F);
		if(q[0].size() && q[1].size()) {
			cur += q[0].front() + q[1].front();
			q[0].pop(), q[1].pop();
		}
		mx = max(mx, cur);
	}
	cout << mx + sum << endl;
}


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

int main(){
  long long n;
  cin >> n;
  string c;
  cin >> c;

  int a[n+1];

  for(int i=0;i<n;i++){
    if(c[i] == 'R'){
      a[i] = 0;
    }

    if(c[i] == 'B'){
      a[i] = 1;
    }

    if(c[i] == 'W'){
      a[i] = 2;
    }
  }

  int ord3[n+1];
  ord3[0]=0;

  for(int i=1;i<=n;i++){
    int k=i;
    for(int j=1;j>=0;j++){
      if(k%3 != 0){
        ord3[i]=j-1;
        break;
      }
      k/=3;
    }
  }

  int fact[n+1];
  int fact_ord3[n+1];

  fact[0]=1;
  fact[1]=1;
  fact_ord3[1]=0;
  fact_ord3[0]=0;

  for(int i=2;i<=n;i++){
    fact_ord3[i]=fact_ord3[i-1]+ord3[i];
    if(i%3 == 0){
      int k=i;
      for(int j=1;j>=0;j++){
        if(k%3!=0)break;
        k/=3;
      }
      fact[i]=fact[i-1]*k;
      fact[i]%=3;
      continue;
    }
    fact[i] = fact[i-1]*i ;
    fact[i]%=3;
  }

  int comb[n+1];

  for(int i=0;i<n;i++){
    if(fact_ord3[n-1] > fact_ord3[i]+fact_ord3[n-i-1]){
      comb[i]=0;
      continue;
    }
    comb[i] = fact[n-1] * fact[i] * fact[n-i-1];
    comb[i] %= 3;
  }

  int ans = 0;
  for(int i=0;i<n;i++){
    ans += a[i] *comb[i];
    ans %= 3;
  }

  if(n%2==0){
    ans *= -1;
  }

  if(ans < 0)ans += 3;

  if(ans==1){
    cout << "B" ;
    return 0;
  }

  if(ans == 0){
    cout << "R";
    return 0;
  }

  cout << "W";
  return 0;

}

#include <bits/stdc++.h>
#define int int
#define maxn 800005
#define mod 3
using namespace std;
int n;
char r[maxn];
char get_char(){
    char ch = getchar();
    while(ch != 'W' && ch != 'B' && ch != 'R')    ch = getchar();
    return ch;
}
int jc[maxn], inv[maxn];
int qpow(int a, int b, int p){
    if(b == 0)    return 1;
    int tmp = qpow(a, b / 2, p);
    tmp = (tmp * tmp) % p;
    if(b & 1)   tmp = (tmp * a) % p;
    return tmp;
}
void get_jc(){
    jc[0] = 1;
    for(int i = 1; i <= n; i++)
        jc[i] = (jc[i - 1] * i);
}
int C(int a, int b, int p) {
    if (b < a)    return 0;
    int s = jc[a] * jc[b - a] % p;
    return jc[b] * qpow(s, p - 2, p) % p;
}

int lucas(int x, int y, int p) {
    if (x == 0)    return 1;
    return lucas(x / p, y / p, p) * C(x % p, y % p, p) % p;
}

int tra(char ch){
    //putchar(ch);
    if(ch == 'B')    return 1;
    if(ch == 'W')    return 2;
    if(ch == 'R')    return 0;
    return -1;
}
signed main(void)
{
    scanf("%lld", &n);
    for(int i = 1; i <= n; i++)
        r[i] = get_char();
    get_jc();
    int res = 0;
    for(int i = 0; i <= n - 1; i++)
    {
        res = (res + tra(r[i + 1]) * lucas(i, n - 1, mod)) % mod;
        //printf("%d\n", res);
    }
    if((n + 1) & 1)
        res = -res;
    res = ((res % mod) + mod) % mod;
    if(res == 1)
        printf("B\n");
    else if(res == 2)
        printf("W\n");
    else
        printf("R\n");
    return 0;
}


/*
Another source code by:

  _   _ _ _   _        _____       _
 | \ | (_) | (_)      / ____|     | |
 |  \| |_| |_ _ _ __ | |  __ _ __ | |_
 | . ` | | __| | '_ \| | |_ | '_ \| __|
 | |\  | | |_| | | | | |__| | |_) | |_
 |_| \_|_|\__|_|_| |_|\_____| .__/ \__|
                            | |
                            |_|
*/
#include<bits/stdc++.h>
#include <vector>
using namespace std;
#define vi vector<int>
#define pb push_back
#define p push
#define sll stack<long long>
#define qll queue<long long>
#define vll vector<long long unsigned>
#define st string
#define vvll vector<vll>
#define vstr vector <string>
#define vp vector <pair<ll,ll>>
#define fori(n) for(ll i=0;i<n;i++)
#define forj(n) for(int j=0;j<n;j++)
#define fork(n) for(int k=0;k<n;k++)
#define fori1(n) for(int i=1;i<n;i++)
#define f(i,a,b) for(int i=a;i<=b;i++)
#define s(v) sort(v.begin(),v.end())
#define sr(v) sort(v.rbegin(),v.rend())
#define ll long long
#define MOD 1000000007
#define pi 3.14159265358979323
#include <math.h>
#define yn(n) if(n)cout<<"yes\n";else cout<<"no\n";
#define Yn(n) if(n)cout<<"Yes\n";else cout<<"No\n";
#define YN(n) if(n)cout<<"YES\n";else cout<<"NO\n";
#define mp   make_pair
#define ff first
#define ss second

ll gcd(ll a, ll b){if(b==0)return a;return gcd(b, a%b);}
ll lcm(ll a, ll b){return a*b/gcd(a, b);}
ll modify(ll n){ll res = n;res%=MOD;res+=MOD;res%=MOD;return res;}
ll md(ll n){if(n>0)return n;else return -n;}
//ll mv(vll v){ll m=v[0];fori(v.size()){m=max(v[i],m);}return m;}
vll read(ll n){vll v(n);fori(n)cin>>v[i];return v;}
void out(vll v){fori(v.size()){cout<<v[i]<<" ";}cout<<"\n";}

void solve(){
    ll a,b,k;
    cin>>a>>b>>k;
    if(b==0){
        fori(a)cout<<"a";
        return;
    }//cout<<k;
    vll v2(61,0);
    vvll v(61,v2);
    fori(61)v[0][i]=1;
    fori(61)v[i][0]=1;
    for(int i=1;i<61;i++){
        for(int j=1;j<61;j++){
            v[i][j]=v[i][j-1]+v[i-1][j];
            //if(v[i][j]==118264581564861424)cout<<i<<j;
        }
    }

    ll a1=a,b1=b;
    fori(a1+b1){
        if(a==0){while(b--)cout<<"b";return;}
        if(b==0){while(a--)cout<<"a";return;}
        //cout<<k<<" "<<v[a+b-2][a-1]<<"\n";
        if(a+b>=1){
            if(k>v[b][a-1]){
                cout<<"b";
                k-=v[b][a-1];b--;
            }
            else {
                cout<<"a";a--;
            }
        }
    }
}
int main(){

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

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

    }

    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";

}

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

#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
#define fp(i,a,b) for(ll i=a ; i<b ; i++)
#define fn(i,a,b) for(int i=a ; i>=b ; i--)
#define ones(x) __builtin_popcount(x)
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define dbg(x) cout << (#x) << " = " << x << " "
#define fini cout << "\n";
#define line cout << "-----------------------------------\n";
typedef long long ll;
typedef long double ld;
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;
typedef tree<
ll,
null_type,
less_equal<ll>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

int read (){
    int x = 0, f = 1; char s = getchar();while (s < '0' || s > '9') {if (s == '-') f = -1; s = getchar();}
    while (s >= '0' && s <= '9') x = x * 10 + s - '0', s = getchar();
    return x * f;
}
const ll M=5e2+7;
const ll N=2e7+7;
const ll inf=1e18;
const ll mod=1e9+7;
const ll mod2=998244353;

int n,m;
ll cnt[25];

void go(int ide){
	cin >> n >> m;

	fp(i,0,n){
		string s; cin >> s;
		int x = 0;

		for (char c : s) x += (c == '1');
		cnt[x]++;
	}

	ll ans = 0;
	fp(i,0,m+1) fp(j,i+1,m+1)
		if ((i+j)&1) ans += cnt[i]*cnt[j];

	cout << ans << "\n";
}

int main(){
    fastio;
    int tst=1;
    // cin >> tst;
    // cout << fixed << setprecision(12);
    fp(i,0,tst) go(i+1);

    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<cmath>
#include<map>

using namespace std;

#define rep(i,N) for(ll i=0;i<N;i++) //0から

typedef long long ll;

ll binary_search(vector<ll> a, ll n, ll key){
    ll right = n, left = -1;
    ll md = (right + left) / 2;

    while(right - left > 1){
        if(a[md] <= key){
            right = md;
        }else{
            left = md;
        }
        md = (right + left) / 2;
    }
    if(left == -1) return -1; //無い場合
    return right;
}

vector<ll> prime;
void Prime(ll n){ //線形篩,素数列挙
    vector<ll> p(n,0);
    p[0]=1;
    p[1]=1;
    for(ll i=2;i<n;i++){
        if(p[i]==0){
            prime.push_back(i);
            p[i]=i;
        }
        ll k=prime.size();
        for(ll j=0;j<k && i*prime[j]<n && prime[j]<=p[i];j++){
            p[i*prime[j]]=prime[j];
        }
    }
}

ll gcd(ll a,ll b){
    if(a<b){
        swap(a,b);
    }
    //a>=b
    ll r=a%b;
    while(r!=0){
        a=b;
        b=r;
        r=a%b;
    }
    return b;
}

ll modexp(ll x, ll a, ll m){ //x^a mod m
    ll ret = 1;
    while (a > 0) {
        if (a & 1) ret = ret * x % m;  // n の最下位bitが 1 ならば x^(2^i) をかける
        x = x * x % m;
        a >>= 1;  // n を1bit 左にずらす
    }
    return ret;
}

ll modinv(ll a, ll m){ //a^{-1} mod 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=(u+m)%m;
    return u;
}

#define MOD ((ll)1e+9 + 7)

ll dp[100][10000];

//cout<<fixed<<setprecision(10);
int main(){
    ll n;
    cin>>n;
    vector<ll> ans;
    for(ll i=1;i*i<=n;i++){
        if(n%i==0){
            if(i*i==n){
                ans.push_back(i);
            }
            else{
                ans.push_back(i);
                ans.push_back(n/i);
            }
        }
    }
    sort(ans.begin(),ans.end());
    n=ans.size();
    rep(i,n){
        cout<<ans[i]<<endl;
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main(){
  long int n;
  cin >> n;
  set<long int> l;
  for(long int i=1; i*i<=n; i++){
	if(n%i == 0){
	  l.insert(i);
   	  l.insert(n/i);
	}
  }
  for(auto x : l) cout << x << endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int mod = 1e9+7;
#define vi vector<int>
#define pb push_back
#define ff first
#define ss second
#define pi pair<int,int>
#define all(v) (v).begin(),(v).end()
#define mk(arr,n,type)  type *arr=new type[n];
#define rrep(i, n) for(int i=n-1;i>=0;i--)
#define rep(i,n) for(int i=0;i<n;i++)
#define repm(i,a,b) for(int i=a ; i<b ; i++)
int gcd(int a , int b){
  if(b==0) return a;

  return gcd(b,a%b);
}

void cp(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
  #endif
}

void solve(){

  int n;
  cin>>n;

  vector<pi> vec;

  rep(i,n){
    int x,y;
    cin>>x>>y;
    vec.pb({x,y});
  }

  if(n==1){
    cout<<vec[0].ff+vec[0].ss<<endl;
    return ;
  }

  int mini1 = INT_MAX , mini2 = INT_MAX , mini = INT_MAX;

  int idx,idx1;

  rep(i,n){
      if(mini1 > vec[i].ff){
        mini1 = vec[i].ff;
        idx = i;
      }
  }

  rep(i,n){
      if(idx==i)continue;

      if(mini2 > vec[i].ss){
        idx1 = i;
        mini2 = vec[i].ss;
      }
  }

  mini = max(mini1,mini2);

  rep(i,n){
    mini = min(mini,vec[i].ff + vec[i].ss);
  }

  cout<<mini<<endl;

}

int32_t main(){
  cp();
  int t = 1;
  // cin>>t;

  while(t--)
    solve();

    return 0;
}

#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<set>
#include<vector>
using namespace std;

int main(){
    int64_t n;
    int64_t k;
    cin >> n;
    cin >> k;

    vector<pair<int64_t, int64_t> > ab(n);
    int64_t a;
    int64_t b;

    for(int64_t i=0; i<n; i++){
        cin >> a;
        cin >> b;

        ab.at(i).first = a;
        ab.at(i).second = b;

    }
    sort(ab.begin(), ab.end());

    int64_t mura = 0;
    int64_t nowmoney = 0;
    mura += k;

    for(int64_t i=0; i<n; i++){

        if(mura >= ab.at(i).first){
            mura += ab.at(i).second;
            nowmoney += ab.at(i).second;
        }
    }

    cout << mura << endl;

}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
typedef long int li;
typedef long long int lli;
typedef vector<int> vi;
typedef vector<long long int> vlli;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<vector<int>> vvi;
typedef vector<vector<double>> vvd;
typedef vector<vector<char>> vvc;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<vector<vector<double>>> vvvd;
typedef vector<vector<vector<char>>> vvvc;
#define rep(i, n) for(int i = 0; i < n; i++)
#define w1 while(1)

void yakusuu_rekkyo(long long int n, vlli &divisor)
{
    for(long long i = 1; i * i <= n; i++)
    {
        if(n % i == 0)
        {
            divisor.push_back(i);

            if(i * i != n)
            {
                divisor.push_back(n / i);
            }
        }
    }

    sort(divisor.begin(), divisor.end());
}

bool sosuu_hantei(long long n)
{
    for(long long i = 2; i * i <= n; i++)
    {
        if(n % i == 0)
        {
            return false;
        }
    }

    return true;
}

void ruisekiwa(const vi &num, vi &sum)
{
    rep(i, num.size())
    {
        sum.push_back(sum.at(i) + num.at(i));
    }

    rep(i, sum.size())
    {
        cout << sum.at(i) << endl;
    }
}

int main(void)
{
    int N = 0;
    cin >> N;

    vi fruits(N, 0);
    rep(i, N)
    {
        cin >> fruits[i];
    }

    int sum = 0;

    rep(i, N)
    {
        if(fruits[i] > 10)
        {
            sum += fruits[i] - 10;
        }
    }

    cout << sum << endl;

    return 0;
}

#include <bits/stdc++.h>
#define mod 1000000007
#define point complex<long double>
#define pi acos(-1)

typedef long long ll;
typedef long double ld;

using namespace std;

void Fastio()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}

bool runTestCase() {
	int n; cin >> n;
	ll res = 0;
	for (int i = 0; i < n; i++) {
		ll x; cin >> x;
		if (x > 10)
			res += x - 10;
	}
	cout << res << '\n';
	return 0;
}
int main()
{
	Fastio();
	int ttt = 1; //cin >> ttt;
	while (ttt--) {
		runTestCase();
	}

	return 0;
}


//𝓒𝓸𝓭𝓮 𝓦𝓻𝓲𝓽𝓽𝓮𝓷 𝓫𝔂 𝓟𝓲𝓷𝓪𝓴𝓲 𝓑𝓱𝓪𝓽𝓽𝓪𝓬𝓱𝓪𝓻𝓳𝓮𝓮
#include<bits/stdc++.h>
using namespace std;

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

#define LOCAL
#define MAX 10000
#define PI 2*acos(0.0)
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define DEBUG(x) cerr << #x << " = " << x << endl;

const int MOD = (int) 1e9 + 7;

#ifdef LOCAL
    #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
    #define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << ": " << arg1 << " | ";
    __f(comma + 1, args...);
}

void pinakipb2()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
}

int main()
{
    pinakipb2();
    ll ans = 0;
    ll N,M;
    cin >> N;
    ll sum = 0;
    ll mx = 0;
    ll p = 0;
    while (N--)
    {
        cin >> M;
        sum += M;
        mx = max(mx, sum);
        ans = max(p + mx, ans);
        p += sum;
    }
    ans = max(ans, p);
    cout << ans << endl;
    return 0;
}

#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
  int A,B,C,D;cin>>A>>B>>C>>D;
  cout<<min({A,B,C,D})<<endl;
}


#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cassert>
#include <map>
#include <numeric>
#include <cstring>
#include <set>
#include <ctime>
#include <queue>
#include <cmath>
#include <iomanip>
#include <iterator>
#include <bitset>
#include <unordered_map>
#include <complex>
#include <unordered_set>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

using namespace std;

clock_t timeStart, timeFinish;

void timeBegin() {
    timeStart = clock();
}

void timeEnd() {
    timeFinish = clock();
}

void timeDuration() {
    timeEnd();
    double time_taken = double(timeFinish - timeStart) / double(CLOCKS_PER_SEC);
    cout << "Time taken by program is : " << fixed << time_taken << setprecision(5);
    cout << " sec " << endl;
}

class InputReader {
public:
    InputReader() {
        input_file = stdin;
        cursor = 0;
        fread(buffer, SIZE, 1, input_file);
    }

    InputReader(const char *file_name) {
        input_file = fopen(file_name, "r");
        cursor = 0;
        fread(buffer, SIZE, 1, input_file);
    }

    inline InputReader &operator>>(int &n) {
        while ((buffer[cursor] < '0' || buffer[cursor] > '9') && buffer[cursor] != '-') {
            advance();
        }
        int sign = 1;
        if (buffer[cursor] == '-') {
            sign = -1;
            advance();
        }
        n = 0;
        while ('0' <= buffer[cursor] && buffer[cursor] <= '9') {
            n = n * 10 + buffer[cursor] - '0';
            advance();
        }
        n *= sign;
        return *this;
    }

private:
    FILE *input_file;
    static const int SIZE = 1 << 17;
    int cursor;
    char buffer[SIZE];

    inline void advance() {
        ++cursor;
        if (cursor == SIZE) {
            cursor = 0;
            fread(buffer, SIZE, 1, input_file);
        }
    }
};

const int MAXN = 100;
const int MAXW = 100;
const int MOD = 998244353;

int v[1 + MAXN], factorial[1 + MAXN];
int dp[2][1 + MAXN][1 + MAXN * MAXW];

void addTo(int &x, int y) {
    x += y;
    if (x >= MOD) {
        x -= MOD;
    }
}

int main() {
    timeBegin();
    //ifstream cin("input.in");
    //ofstream cout("output.out");
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    srand(time(0));
    int n, sum = 0;
    cin >> n;
    dp[0][0][0] = factorial[0] = 1;
    for (int i = 1; i <= n; i++) {
        factorial[i] = 1LL * factorial[i - 1] * i % MOD;
        int now = (i & 1), before = ((i - 1) & 1);
        cin >> v[i];
        sum += v[i];
        for (int j = 0; j <= i; j++) {
            for (int k = 0; k <= sum; k++) {
                dp[now][j][k] = dp[before][j][k];
                if (j > 0 && k >= v[i]) {
                    addTo(dp[now][j][k], dp[before][j - 1][k - v[i]]);
                }
            }
        }
    }
    if (sum % 2 != 0) {
        cout << "0\n";
    } else {
        int answer = 0;
        for (int i = 1; i < n; i++) {
            addTo(answer, 1LL * dp[n & 1][i][sum / 2] * factorial[i] % MOD * factorial[n - i] % MOD);
        }
        cout << answer << "\n";
    }
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define fr(i, a, b) for (int i=a; i<(b); i++)
#define FOR(i, a) for (int i=0; i<(a); i++)
#define bck(i,a,b) for (int i = (b)-1; i >= a; i--)
#define BACK(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a,x) for (auto& a : x)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert

template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

const int M = 1000000007;
const char nl = '\n';
const int MX = 100001;
bool comp(vector<ll>&a, vector<ll>&b){
	return a[1] < b[1] ;
}
vector<int> sieve(int n) {
	vector<bool>prime(n+1, 1) ;
	for (int i=2 ; i*i<n; i+=1) {if (prime[i]) {
		for (int j =2 ; i*j< n; j+=1) prime[i*j] = 0 ; }}prime[1] = 0; prime[0] = 0 ;
	vector<int>v;for (int i = 0 ;i < prime.size(); i+=1) {if (prime[i]) v.pb(i); }return v ;}

void solve(){
	string s ; cin >> s;
	deque<char>t;
	ll count =0 ;
	for (auto &x: s) {
		if (x=='R') count ++ ;
		else {
			if (count % 2 ==0) t.pb(x);
			else t.push_front(x);
		}
	}

	vector<char>v;
	if (count % 2==0) {
	while (t.size()!=0) {
		char x = t.front();
		t.pop_front();
		if (v.size() == 0) v.pb(x);
		else {
			if (v.back() == x) v.pop_back();
			else v.pb(x);
		}
	}}
	else {
		while (t.size()!=0) {
		char x = t.back();
		t.pop_back();
			if (v.size() == 0) v.pb(x);
			else {
			if (v.back() == x) v.pop_back();
			else v.pb(x);
		}
		}
	}
	for(auto x: v) cout << x ;
		cout << nl;
}

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

	int i =1  ;
	while (t--) {
		solve();
		i+=1 ;
	}
	return 0 ;

}


#include <bits/stdc++.h>

using namespace std;

#define int long long

int N,L;
vector<int> A(1e5+10), B(1e5+10), C, high(1e5+10), low(1e5+10);

main() {

    cin >> N >> L;
    C.push_back(0);
    for (int i = 1; i <= N; i++) {
        cin >> A[i];
        C.push_back(A[i]-i);
    }
    C.push_back(L-N);

    int t = 0;
    for (int i = 1; i <= N; i++) {
        cin >> B[i];
      //  cout << "Goal: " << " " << B[i] << "\n";
      //  for (int c: C) cout << c+i << " "; cout << "\n";
        auto it = lower_bound(C.begin(), C.end(), B[i]-i);
        if (it == C.end() || *it != B[i]-i) {
            cout << "-1\n";
            return 0;
        }
        auto it2 = upper_bound(C.begin(), C.end(), B[i]-i);
        int z = it-C.begin();
        int z2 = it2-C.begin()-1;
        if (z <= i && i <= z2) {
            z = i;
        }
        else if (abs(z2-i) < abs(z-i)) {
            z = z2;
        }
        //go from i to z
        //cout << i << " to " << z << "\n";
        if (i < z) {
            low[z] = max(z-i, low[z]);
        }
        if (i > z) {
            high[z] = max(i-z,high[z]);
        }
    }

    for (int i = 0; i <= N+1; i++) {
       // cout << i << " " << low[i] << " " << high[i] << "\n";
        t += low[i] + high[i];
    }

    cout << t << "\n";

   // cout << t << "\n";

    return 0;
}

#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
#define owo(i,a, b) for(int i=(a);i<(b); ++i)
#define uwu(i,a, b) for(int i=(a)-1; i>=(b); --i)
#define senpai push_back
#define ttgl pair<int, int>
#define ayaya cout<<"ayaya~"<<endl

using namespace std;
using ll = long long;
using ld = long double;
ll MOD = 998244353;
const ll root = 3;
ll binpow(ll a,ll b){ll res=1;while(b){if(b&1)res=(res*a)%MOD;a=(a*a)%MOD;b>>=1;}return res;}
ll modInv(ll a){return binpow(a, MOD-2);}
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll NINFLL = 0xc0c0c0c0c0c0c0c0;
const int mxN = 100001;
int dir[mxN];
int a[mxN], b[mxN];
int n, L;
void killthyself() {
    cout<<"-1\n";
    exit(0);
}
ll getright(int l, int r) {
    ll res = 0;
    int right = l;
    int prv = l;
    owo(left, l, r) {
        while(right <= r && a[right] - b[left] != right - left)right++;
        if(a[right] - b[left] != right - left)killthyself();
        //move everyone from l-r forward
        if(right!=prv)res += right - left;
        //cout<<right<<" "<<prv<<" Hi\n";
        prv = right;
    }
    //cout<<"right "<<l<<" "<<r<<" "<<res<<"\n";
    return res;
}
ll getleft(int l, int r) {
    ll res = 0;
    int left = r;
    int prv = r;
    uwu(right, r+1, l) {
        while(left >= l && b[right] - a[left] != right - left)left--;
        if(b[right] - a[left] != right - left)killthyself();
        if(left!=prv)res += right - left;
        prv = left;
    }
    //cout<<"left "<<l<<" "<<r<<" "<<res<<"\n";
    return res;
}
ll go(int l, int r) {
    if(r-l < 2)return 0;
    int suff = r-1;
    int pref = l+1;
    while(dir[suff]==-1) {
        suff--;
    }
    while(dir[pref]==1) {
        pref++;
    }
    if(pref-suff!=1)killthyself();
    return getright(suff+1, r) + getleft(l, pref-1);
}
void solve() {
    cin>>n>>L;
    a[0] = 0;
    a[n+1] = L+1;
    owo(i, 1, n+1)cin>>a[i];
    owo(i, 1, n+1) {
        cin>>b[i];
        if(a[i]==b[i])dir[i] = 0;
        else dir[i] = (a[i] - b[i] > 0) - (b[i] - a[i] > 0);
    }
    ll res = 0;
    int prv = 0;
    owo(i, 1, n+2) {
        if(dir[i]==0) {
            res += go(prv, i);
            prv = i;
        }
    }
    cout<<res<<"\n";
}
int main() {
    //freopen("file.in", "r", stdin);
    //freopen("file.out", "w", stdout);
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    cin.tie(0)->sync_with_stdio(0);
    int T = 1;
    owo(tc, 1, T+1) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef int64_t i64;
typedef long long ll;
typedef long double ld;
typedef unsigned long long Ull;

#define endl "\n"
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
void read(int &sum) {sum = 0; char c = getchar( ); bool f = 0; while (c < '0' || c > '9') {if (c == '-') f = 1; c = getchar( );} while (c >= '0' && c <= '9') {sum = sum * 10 + c - '0'; c = getchar( );} if (f) sum = -sum;}
void read(ll &sum) {sum = 0; char c = getchar( ); bool f = 0; while (c < '0' || c > '9') {if (c == '-') f = 1; c = getchar( );} while (c >= '0' && c <= '9') {sum = sum * 10 + c - '0'; c = getchar( );} if (f) sum = -sum;}
void readarr(ll a[], ll n) {for (int i = 0; i < n; i++)read(a[i]);}
void readarr(int a[], int n) {for (int i = 0; i < n; i++)read(a[i]);}
void readvec(vector<ll> &a, ll n) {for (int i = 0; i < n; i++)read(a[i]);}
void readvec(vector<int> &a, int n) {for (int i = 0; i < n; i++)read(a[i]);}
void aout(ll a[], ll n) {for (int i = 0; i < n; i++)cout << a[i] << " ";}
void aout(int a[], int n) {for (int i = 0; i < n; i++)cout << a[i] << " ";}
const int MAXN = (int)1e6 + 7;
const int MOD = 998244353;
const int INF = (int)2e9 + 7;
const ll LINF = (i64)1e18;
const ld PI = 3.1415926535897932384626433832795;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
void solve() {
	int n;
	read(n);
	vector<pair<ll, ll>> p(n), cho;
	ll tot = 0;
	for (int i = 0; i < n; i++) {
		read(p[i].first), read(p[i].second);
	}
	sort(rall(p));
	for (int i = 0; i < n; i++) {
		cho.pb({p[i].second + 2 * p[i].first, p[i].first});
		tot += p[i].first;
	}
	sort(rall(cho));
	ll now = 0, ans = 0;
	for (int i = 0; i < cho.size(); i++) {
		if (tot < now)break;
		ans++;
		now += cho[i].first - cho[i].second;
		tot -= cho[i].second;
	}
	cout << ans << endl;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int t = 1;
	//read(t);
	while (t--)
	{
		solve();
	}
}

// かります！ごめんなさい!

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
template<int MOD> struct ModInt {
    static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
    ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    int get() const { return (int)x; }
    ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
    ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
    ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
    ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
    ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
    ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
    ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0;
        while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
        return ModInt(u); }
    bool operator==(ModInt that) const { return x == that.x; }
    bool operator!=(ModInt that) const { return x != that.x; }
    ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; };
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
    ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
typedef ModInt<1000000007> mint;
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i
　　　　／　　　＼　 　  |　|
　　　 /　　 /￣￣￣￣/　　|
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿
　 　　　＼/＿＿＿＿/　（u　⊃
---------------------------------------------------------------------------------------------------*/

typedef long long ll;
int N;
vector<pair<int, ll>> E[201010];
//---------------------------------------------------------------------------------------------------
ll xo[201010];
void dfs(int cu, int pa = -1) {
    fore(p, E[cu]) if (p.first != pa) {
            xo[p.first] = xo[cu] ^ p.second;
            dfs(p.first, cu);
        }
}
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N;
    rep(i, 0, N - 1) {
        int a, b; ll c; cin >> a >> b >> c;
        a--; b--;
        E[a].push_back({ b, c });
        E[b].push_back({ a, c });
    }

    dfs(0);

    mint ans = 0;
    rep(d, 0, 61) {
        mint a = 1;
        ll mask = 1;
        rep(i, 0, d) a *= 2, mask *= 2;

        mint cnt0 = 0, cnt1 = 0;
        rep(i, 0, N) {
            if (xo[i] & mask) cnt1 += 1;
            else cnt0 += 1;
        }

        mint cnt = 0;
        rep(i, 0, N) {
            if (xo[i] & mask) cnt += cnt0;
            else cnt += cnt1;
        }
        cnt /= 2;
        ans += a * cnt;
    }
    cout << ans << endl;
}


// Problem: F - Shift and Inversions
// Contest: AtCoder - AtCoder Beginner Contest 190
// URL: https://atcoder.jp/contests/abc190/tasks/abc190_f
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define int long long
#define pb push_back
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define pi pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define vp vector<pair< int,int> >
#define mpi map<pair<int,int> ,int>
#define endl "\n"
#define fill(A,x) memset(A,x,sizeof A);

int _mergeSort(int arr[], int temp[],
                 int left, int right);
int merge(int arr[], int temp[], int left,
                   int mid, int right);

/* This function sorts the
   input array and returns the
number of inversions in the array */
int mergeSort(int arr[], int array_size)
{
    int temp[array_size];
    return _mergeSort(arr, temp, 0, array_size - 1);
}

/* An auxiliary recursive function
  that sorts the input array and
returns the number of inversions in the array. */
int _mergeSort(int arr[], int temp[],
                 int left, int right)
{
    int mid, inv_count = 0;
    if (right > left) {
        /* Divide the array into two parts and
        call _mergeSortAndCountInv()
        for each of the parts */
        mid = (right + left) / 2;

        /* Inversion count will be sum of
        inversions in left-part, right-part
        and number of inversions in merging */
        inv_count += _mergeSort(arr, temp,
                                left, mid);
        inv_count += _mergeSort(arr, temp,
                             mid + 1, right);

        /*Merge the two parts*/
        inv_count += merge(arr, temp, left,
                           mid + 1, right);
    }
    return inv_count;
}

/* This funt merges two sorted arrays
and returns inversion count in the arrays.*/
int merge(int arr[], int temp[], int left,
          int mid, int right)
{
    int i, j, k;
    int inv_count = 0;

    i = left; /* i is index for left subarray*/
    j = mid; /* j is index for right subarray*/
    k = left; /* k is index for resultant merged subarray*/
    while ((i <= mid - 1) && (j <= right)) {
        if (arr[i] <= arr[j]) {
            temp[k++] = arr[i++];
        }
        else {
            temp[k++] = arr[j++];

            /* this is tricky -- see above
            explanation/diagram for merge()*/
            inv_count = inv_count + (mid - i);
        }
    }

    /* Copy the remaining elements of left subarray
(if there are any) to temp*/
    while (i <= mid - 1)
        temp[k++] = arr[i++];

    /* Copy the remaining elements of right subarray
(if there are any) to temp*/
    while (j <= right)
        temp[k++] = arr[j++];

    /*Copy back the merged elements to original array*/
    for (i = left; i <= right; i++)
        arr[i] = temp[i];

    return inv_count;
}

void solve()
{
 	int n;cin>>n;
 	int A[n+5] , B[n+5];

 	for(int i=0;i<n;i++)
 	{
 		cin>>A[i];
 		B[i]=A[i];
 	 }

 	int L[n+5] , R[n+5];
 	fill(L,0);fill(R,0);

 	int ans = mergeSort(A, n);
 	cout<< ans<<"\n";

 	for(int i=0;i<n-1;i++)
 	{
 		ans+= (n-1-B[i])  ;
 		ans-=B[i];
 		cout<<ans<<"\n";
 	}

}

signed main()
{
    boost
    int test=0,t;
    //cin >> t ;
    t=1;
   while(test++ <t){
	//   cout<<"Case #"<<test<<": ";
	   solve();}

}

#pragma GCC optimize("Ofast")
//#pragma GCC target ("sse4")

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 2;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}
struct BIT {
private:
	vector<ll> node; int n;
public:
	BIT(int n_) {
		n = n_; node.resize(n, 0);
	}
	//0-indexed
	void add(int a, ll w) {
		for (int i = a; i < n; i |= i + 1)node[i] += w;
	}
	//[0,a)
	ll sum(int a) {
		ll ret = 0;
		for (int i = a - 1; i >= 0; i = (i & (i + 1)) - 1)ret += node[i];
		return ret;
	}
	//[a,b)
	ll sum(int a, int b) {
		return sum(b) - sum(a);
	}
};
void solve() {
	int n; cin >> n;
	BIT bt(n);
	ll ans = 0;
	vector<int> a(n);
	rep(i, n)cin >> a[i];
	rep(i, n)a.push_back(a[i]);
	rep(i, n) {
		ans += bt.sum(a[i]+1, n);
		bt.add(a[i], 1);
	}
	rep(i, n) {
		cout << ans << "\n";
		if (i == n - 1)continue;
		ans += bt.sum(a[i] + 1, n);
		bt.add(a[i], 1);
		ans -= bt.sum(a[i]);
		bt.add(a[i], -1);
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(15);
	//init_f();
	//init();
	//expr();
	//int t; cin >> t; rep(i,t)
	//expr();
	solve();
	return 0;
}


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

typedef long long ll;
typedef pair<int,int> P;

#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)

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

const char newl = '\n';

// const ll mod = 1000000007;
const ll mod = 998244353;

const int MAX_N = 100010;
ll fact[MAX_N],fact_inv[MAX_N],inv[MAX_N];

ll pow_mod(ll a,ll b) {
    ll ret;
    if (b < 0) ret = pow_mod(a,mod+b-1);
    else if (b == 0) ret = 1;
    else if (b == 1) ret = a;
    else {
        ll c = pow_mod(a,b/2);
        if (b%2) ret = (c*c)%mod*a%mod;
        else ret = c*c%mod;
    }
    return ret;
}

void init(int x) {
    fact[0] = 1;fact[1] = 1;
    for (int i = 2;i <= x;++i) fact[i] = fact[i-1]*i%mod;
    fact_inv[x] = pow_mod(fact[x],-1);
    for (int i = x;i > 0;--i) fact_inv[i-1] = fact_inv[i]*i%mod;
    for (int i = 1;i <= x;++i) inv[i] = fact_inv[i]*fact[i-1]%mod;
}

ll combi(ll a, ll b) {
    return fact[a]*fact_inv[b]%mod*fact_inv[a-b]%mod;
}

ll permu(ll a, ll b) {
    return fact[a]*fact_inv[a-b]%mod;
}

int main() {
    int n,m;
    cin >> n >> m;
    vector<vector<ll>> a(m+1,vector<ll>(n+1,1));
    for (int i = 0;i < m+1;++i) for (int j = 0;j < n;++j) a[i][j+1] = a[i][j]*i%mod;
    ll ans = n*a[m][n]%mod;
    for (int i = 1;i < n;++i) for (int j = 0;j < m;++j) {
        (ans -= (n-i)*a[m-j-1][i-1]%mod*a[m][n-i-1]%mod) %= mod;
    }
    cout << (ans+mod)%mod << newl;
}

#include "bits/stdc++.h"
#define int long long
#define endl '\n'
using namespace std;

typedef long long ll;
typedef long double ld;
#define db(x) cerr << #x << ": " << x << '\n';
#define read(a) int a; cin >> a;
#define reads(s) string s; cin >> s;
#define readb(a, b) int a, b; cin >> a >> b;
#define readc(a, b, c) int a, b, c; cin >> a >> b >> c;
#define readarr(a, n) int a[(n) + 1] = {}; FOR(i, 1, (n)) {cin >> a[i];}
#define readmat(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];}

#define print(a) cout << a << endl;
#define printarr(a, n) FOR (i, 1, n) cout << a[i] << " "; cout << endl;
#define printv(v) for (int i: v) cout << i << " "; cout << endl;
#define printmat(a, n, m) FOR (i, 1, n) {FOR (j, 1, m) cout << a[i][j] << " "; cout << endl;}
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define rz(v, n) v.resize((n) + 1);
#define pb push_back
#define fi first
#define se second
#define vi vector <int>
#define pi pair <int, int>
#define vpi vector <pi>
#define vvi vector <vi>
#define setprec cout << fixed << showpoint << setprecision(20);
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e18;
const ll mod = 998244353;
const ll N = 2e5 + 1;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int power (int a, int b = mod - 2)
{
    int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

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

	readb(n, m);
	int one[n + 1][m + 1] = {}, zer[n + 1][m + 1] = {};
	int onesum[n + 1][m + 1] = {}, zersum[n + 1][m + 1] = {};

	int precalc[m + 1][n + 1] = {};
	FOR (i, 1, m) precalc[i][0] = 1;
	FOR (i, 1, m)
	    FOR (j, 1, n)
	        precalc[i][j] = i*precalc[i][j - 1] % mod;

	FOR (i, 1, n)
	    FOR (j, 1, m)
	    {
	        one[i][j] = (one[i][j - 1] + onesum[i - 1][j - 1] + zersum[i - 1][j - 1] + precalc[j][i] - precalc[j - 1][i]) % mod;
	        zer[i][j] = (one[i][j] + onesum[i - 1][j] + zersum[i - 1][j]) % mod;

	        onesum[i][j] = (onesum[i - 1][j]*(j + 1) + one[i][j]) % mod;
	        zersum[i][j] = (zersum[i - 1][j]*j + zer[i][j]) % mod;
	    }

    print((one[n][m] + 5*mod) % mod);
}


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

  map<pair<ll,int>,ll> dp;
  auto rec = [&](auto self, ll d, int i)->ll {
    if(i == n - 1) return dp[{d, i}] = ll(1);
    if(dp.find({d, i}) != dp.end() ) return dp[{d, i}];
    if(d % a[i+1] == 0) {
      return dp[{d, i}] = self(self, d, i+1);
    }
    else {
      ll res = self(self, d - d%a[i+1], i+1) + self(self, d + a[i+1] - d%a[i+1], i+1);
      return dp[{d, i}] = res;
    }
  };

  cout << rec(rec, x, 0) << "\n";

  return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vint = vector<int>;
using vvint = vector<vint>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vchar = vector<char>;
using vvchar = vector<vchar>;
using vp = vector<P>;
using vpp = vector<pair<P, P>>;
using vvp = vector<vp>;

#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
istream &operator>>(istream &is, P &a)
{
    return is >> a.first >> a.second;
}
ostream &operator<<(ostream &os, const P &a) { return os << "(" << a.first << "," << a.second << ")"; }

template <typename T>

void view(const std::vector<T> &v)
{
#ifndef ONLINE_JUDGE
    for (const auto &e : v)
    {
        std::cout << e << " ";
    }
    std::cout << std::endl;
#endif
}
template <typename T>
void view(const std::vector<std::vector<T>> &vv)
{
    for (const auto &v : vv)
    {
        view(v);
    }
}
#pragma endregion

const int TIME = 2 * 1e5;
int main()
{
    int n, w;
    cin >> n >> w;
    vll u(TIME + 5, 0);
    int tmax = 0;
    rep(i, n)
    {
        int s, t, p;
        cin >> s >> t >> p;
        u[s] += p;
        u[t] -= p;
        tmax = max(tmax, t);
    }

    // rep(i, tmax) cout << u[i] << " ";
    // cout << endl;

    vll use(TIME + 1, 0);
    rep(i, tmax) use[i + 1] = use[i] + u[i];

    // rep(i, tmax) cout << use[i] << " ";
    // cout << endl;

    bool ok = true;
    rep(i, tmax + 1)
    {
        ok = use[i + 1] <= w;
        if (!ok)
            break;
    }

    cout << (ok ? "Yes" : "No") << endl;

    return 0;
}


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

int debug = 0;

const int N = 60;

int mod = 998244353;
int n, k, sz = 0, cnt, a[N][N], fac[N], was[N];
vector <int> g[N];

void dfs(int v) {
    if (was[v]) return;
    was[v] = 1;
    sz++;
    for (int u : g[v]) dfs(u);
}

main() {
    fac[0] = 1;
    rep(i, 1, N) fac[i] = fac[i - 1] * i % mod;

    cin >> n >> k;
    rep(i, 0, n) rep(j, 0, n) {
        cin >> a[i][j];
    }
    rep(x, 0, n) rep(y, 0, n) {
        int flag = 1;
        rep(i, 0, n) {
            if (a[i][x] + a[i][y] > k) {
                flag = 0;
            }
        }
        if (flag) {
            g[x].push_back(y);
            g[y].push_back(x);
        }
    }
    cnt = 1;
    sz = 0;
    rep(i, 0, n) {
        if (!was[i]) {
            sz = 0;
            dfs(i);
            cnt *= fac[sz];
            cnt %= mod;
        }
    }
    fill(was, was + n + 1, 0);
    rep(i, 0, n + 1) g[i].clear();
    rep(x, 0, n) rep(y, 0, n) {
        int flag = 1;
        rep(i, 0, n) {
            if (a[x][i] + a[y][i] > k) {
                flag = 0;
            }
        }
        if (flag) {
            g[x].push_back(y);
            g[y].push_back(x);
        }
    }
    sz = 0;
    rep(i, 0, n) {
        if (!was[i]) {
            sz = 0;
            dfs(i);
            cnt *= fac[sz];
            cnt %= mod;
        }
    }
    cout << cnt << '\n';
}

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

using namespace std;

int n, a[111000];
long long res = 1;

int main() {
	cin >> n;
	rep(i,1,n) cin >> a[i];
	sort(a+1, a+n+1);
	rep(i,1,n) res = res * (a[i] - a[i-1] + 1) % 1000000007;
	cout << res << "\n";
	return 0;
}


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

vector<vector<pair<int,int>>> graph(250001);
vector<ll> d_d;
void Dijkstra(int start, int goal, ll t_max){
    d_d.assign(graph.size(), t_max);
    d_d[start] = 0;
    priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<pair<ll, int>> > que;
    que.emplace(0, start);
    while(!que.empty()){
        ll dist = que.top().first;
        int now = que.top().second;
        que.pop();
        if(d_d[now] < dist) continue;
        if(now == goal) return ; // ここの行を失くせばstart全頂点への距離が求められる
        for(int i = 0; i < graph[now].size(); ++i){
            int next = graph[now][i].first;
            int next_cost = graph[now][i].second;
            if(d_d[next] > d_d[now] + next_cost){
                d_d[next] = d_d[now] + next_cost;
                que.emplace(d_d[next], next);
            }
        }
    }
}

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

    int r, c; cin >> r >> c;

    rep(i, r) {
        rep(j, c-1) {
            int a; cin >> a;
            graph[i*c+j].emplace_back(i*c+j+1, a);
            graph[i*c+j+1].emplace_back(i*c+j, a);
        }
    }

    rep(i, r-1) {
        rep(j, c) {
            int b; cin >> b;
            graph[i*c+j].emplace_back((i+1)*c+j, b);
            rep(k, i) {
                if(i-k-1 >= 0) graph[i*c+j].emplace_back((i-k-1)*c+j, k+2);
            }
        }
    }
    rep(j,c) rep(k, r-1) if(r-k-2 >= 0) graph[(r-1)*c+j].emplace_back((r-k-2)*c+j, k+2);

    Dijkstra(0, r*c-1, (1<<30));

    cout << d_d[r*c-1] << endl;
}

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

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
using ll=long long int;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end

using P=pair<int,int>;
bitset<10101> res,used,decide;
vector<P> g[110];
int N,M,cnt[110]={};
void dfs(int v,int p){
   used[v]=1;
   for(auto& [nxt,id]:g[v])if(nxt!=p){
      if(cnt[nxt]==1)continue;
      if(id>=M){
         if(!decide[id-M])res[id-M]=0;
         decide[id-M]=1;
      }
      else{
         if(!decide[id])res[id]=1;
         decide[id]=1;
      }
      cnt[nxt]--;
      if(!used[nxt])dfs(nxt,v);
   }
}

int main(){
   cin>>N>>M;
   vector<P> es(M);
   rep(i,0,M){
      cin>>es[i].first>>es[i].second;
      es[i].first--;
      es[i].second--;
   }
   vector<int> c(N);
   rep(i,0,N)cin>>c[i];

   rep(i,0,M){
      if(c[es[i].first]!=c[es[i].second]){
         res[i]=(c[es[i].first]>c[es[i].second]);
      }
      else{
         g[es[i].first].emplace_back(es[i].second,i);
         g[es[i].second].emplace_back(es[i].first,i+M);
         cnt[es[i].first]++;
         cnt[es[i].second]++;
      }
   }
   rep(i,0,N)if(!used[i])dfs(i,-1);

   rep(i,0,M)cout<<(res[i]?"->":"<-")<<'\n';
   return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	int n;
	cin >> n;
	vector<int> a(n);
	vector<int> b(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		a[i] += i;
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i];
		b[i] += i;
	}
	vector<int> c = a;
	vector<int> d = b;
	sort(begin(c), end(c));
	sort(begin(d), end(d));
	if (c != d) {
		cout << -1 << "\n";
	} else {
		for (auto &i : a) {
			i = lower_bound(begin(c), end(c), i) - begin(c);
		}
		for (auto &i : b) {
			i = lower_bound(begin(d), end(d), i) - begin(d);
		}
		vector<vector<int>> ord(n);
		for (int i = 0; i < n; i++) {
			ord[a[i]].emplace_back(i);
		}
		vector<int> cnt(n, 0);
		for (int i = 0; i < n; i++) {
			int t = b[i];
			b[i] = ord[t][cnt[t]++];
		}
		vector<int> t(n * 2, 0);
		auto add = [&](int x) {
			int p = n + x;
			while (p) {
				t[p] += 1;
				p /= 2;
			}
		};
		auto query = [&](int l, int r) {
			int res = 0;
			for (l += n, r += n; l < r; l /= 2, r /= 2) {
				if (l & 1) {
					res += t[l++];
				}
				if (r & 1) {
					res += t[--r];
				}
			}
			return res;
		};
		long long res = 0;
		for (int i = 0; i < n; i++) {
			res += query(b[i] + 1, n);
			add(b[i]);
		}
		cout << res << "\n";
	}
}

#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i = a; i <= b; i++)
#define rrep(i,a,b) for(int i = a; i >=b; i--)
#define debug(x) cout << '>' << #x << ':' << x << endl;
#define sq(a) (a)*(a)
#define nl "\n"
#define all(x) (x).begin(),(x).end()
using namespace std;
//using namespace __gnu_pbds;
//typedef tree<int,null_type,less<int>,rb_tree_tag,
//tree_order_statistics_node_update> indexed_set;

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

const int N = 100100;
ll mod=1000000007;

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

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


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 998244353
#define F first
#define S second
#define all(v) (v).begin(),(v).end()
#define np next_permutation
#define lp(i,n) for(int i=0;i<n;i++)
#define lps(i,j,n) for(int i=j;i<n;i++)
#define vii vector<vi>
#define vb vector<bool>
#define pr pair<int,int>
#define vl vector<ll>
#define vs vector<string>
#define us unordered_map<int,int>
#define Mpq priority_queue<int>
#define mpq priority_queue<int,vi,greater<int>>
#define eb emplace_back
#define pr pair<int,int>
#define prl pair<ll,ll>
#define vp vector<pr>
#define vpl vector<prl>
#define mkp make_pair
#define ld long double
#define vii vector<vi>
#define Max(a,b) a=max(a,b)
#define Min(a,b) a=min(a,b)
#define ull unsigned long long
#define prr pair<ll,int>
#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
using vi=vector<int>;
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;

// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/assoc_container.hpp>

// using namespace __gnu_pbds;

// typedef  tree<prl, null_type,less<prl>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// ordered_set s;

#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;

double epsilon=(double)1e-9;
const int N=(int)2e5+12;
int t;

const ll MX=(ll)1e18+123;
const int inf=0x3fffffff;

map<pr,int> processed;

int fpow(int b){
   int a=2,res=1;
   while(b>0){
      if(b&1) res=(res*1LL*a)%mod;
      a=(a*1LL*a)%mod;
      b>>=1;
   }
   return res;
}

void solve(){
   int h,w,x,cnt=0;
   string s;
   bool ispos=1;
   cin>>h>>w;
   char a[h][w];
   ll ans=0;

   lp(i,h){
      cin>>s;
      lp(j,w) a[i][j]=s[j];
   }

   int l=0,r=0;
   while(r<w){
      int x=l,y=r,c=0,rs=0,bs=0,ds=0;
      while(x<h and y>=0){
         if(a[x][y]=='B') bs++;
         else if(a[x][y]=='R') rs++;
         else ds++;
         x++,y--;
         c++;
      }
      if(bs>0 and rs>0) {
         ispos=0;
         break;
      }
      if(ds==c) cnt++;
      r++;
   }

   l=1,r=w-1;
   while(l<h){
      int x=l,y=r,c=0,rs=0,bs=0,ds=0;
      while(x<h and y>=0){
         if(a[x][y]=='B') bs++;
         else if(a[x][y]=='R') rs++;
         else ds++;
         x++,y--;
         c++;
      }
      if(bs>0 and rs>0){
         ispos=0;
         break;
      }
      if(ds==c) cnt++;
      l++;
   }
   if(!ispos){
      cout << 0 << endl;
      return;
   }
   cout << fpow(cnt) << endl;
}

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

   solve();
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
using P=pair<ll,ll>;
const ll inf = 1ll<<60;
int main() {
    ll h,w;
    cin >> h >> w;
    vector<vector<char>>mp(h,vector<char>(w));
    for(ll i=0;i<h;i++){
        for(ll j=0;j<w;j++) cin >> mp[i][j];
    }
    vector<ll>data(h+w,0);
    for(ll i=0;i<h+w;i++){
        bool blue=false,red=false,dot=false;
        for(ll j=0;j<=i;j++){
            if(j>=h||(i-j)>=w) continue;
            if(mp[j][i-j]=='R') {
                red=true;
            }
            if(mp[j][i-j]=='B'){
                blue=true;
            }
            if(mp[j][i-j]=='.'){
                dot=true;
            }
        }
        if(blue&&red) data[i]=0;
        else if(!dot) data[i]=1;
        else if(red||blue) data[i]=1;
        else if(dot)data[i]=2;
    }
    ll ans=1;
    for(ll i=0;i<h+w;i++){
        ans*=data[i];
        ans%=MOD;
    }
    cout << ans << endl;
}


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

int main()
{
    ll n, num = 0, hyp = 1;
    cin >> n;
    while (1)
    {
        string hikaku = to_string(hyp) + to_string(hyp);
        if (stoll(hikaku) <= n)
        {
            num++;
            hyp++;
        }
        else
        {
            break;
        }
    }
    cout << num << endl;
    return 0;
}

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

using namespace std;
using ll = long long;

ll a,b,c,i,j,k,n,m,l,x,y,z,result,chk,tmp,sum;

int main() {

  cin >> n;
  ll a[n];
  rep(i,n){
    cin >> a[i];
  }
  sort(a,a+n);

  for(i=n-1;i>=0;i--){
    sum += a[i]*((n-1)-2*(n-i-1));
  }

  cout << sum << endl;

  return 0;
}


#include <cstdio>
#include <cctype>
#define FOR(i,j,k) for(int i=j; i<=k; ++i)
inline int read (void) {
	int x = 0, f = 1, ch = getchar();
	while(!isdigit(ch)) { if(ch == '-') f = -f; ch = getchar(); }
	while(isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
int main (void) {
	int a = read(), b = read(), c = read();
	printf("%s\n", a*a+b*b<c*c?"Yes":"No");
	return 0;
}

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

#define INF 1234567890
#define ll long long

int A, B, C;
int N, M, K;
string s;

int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
	cin.exceptions(ios::badbit | ios::failbit);
	cin >> A >> B >> C;
	if (A*A + B*B < C*C) cout << "Yes";
	else cout << "No";
	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
typedef unsigned long long ll;
using namespace std;
double lg;
ll m;
ll calculate(string x, ll d) {
	ll sum = 0;
	ll temp = 1;
	for (int i = x.size() - 1; i >= 0; --i) {
		ll c = x[i] - '0';
		if (log2(c) + log2(temp) > lg)
			return 0;
		sum += c * temp;
		if (sum > m)
			return 0;
		if (log2(temp) + log2(d) > lg and i)
			return 0;
		temp *= d;
	}
	return sum;
}
int main() {
	string x;
	cin >> x >> m;
	ll cnt = 0;
	ll d = 0;
	for (char c : x)
		d = max(c - '0' + 1ULL, d);
	bool f = true;
	lg = log2(m);
	ll l = d + 1, r = 1e18;
	ll ans = 1;
	if (x.size() == 1) {
		if (x[0] - '0' > m)
			ans = 0;
	}
	else if (x.size() < 4) {
		while (l <= r) {
			ll mid = (l + r) >> 1;
			if (calculate(x, mid) == 0 or calculate(x, mid) <= calculate(x, mid - 1))
				r = mid - 1;
			else {
				ans = mid - d + 1;
				l = mid + 1;
			}
		}
	}
	else {
		ll sum2 = 0;
		ans = 0;
		while (true) {
			ll sum = calculate(x, d);
			if (sum <= sum2)
				break;
			++d;
			++ans;
		}
	}
	cout << ans << endl;
}

#include <bits/stdc++.h>
#define ll long long
#define X first
#define Y second
#define sz(x) (int)((x).size())
#define all(x) x.begin(), x.end()
#define pii pair<ll, int>
#define pi pair<int, pair<int, int>>

using namespace std;

typedef vector<int> vi;
typedef vector<unsigned int> vui;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vi vm;
typedef vector<vm> Mat;

const string yes = "YES", no = "NO";

int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10), cout << fixed;

    string str; cin >> str;
    reverse(str.begin(), str.end());
    ll m; cin >> m;
    if (str.size() == 1) {
        if (str[0] - '0' <= m)
            cout << 1;
        else cout << 0;
        cout << '\n';
        return 0;
    }

    ll mx = *max_element(str.begin(), str.end()) - '0';

    auto isValid = [&](ll base) {
        ll sum = str[0] - '0', b = 1;
        for (int i = 1; i < (int)str.size(); ++i) {
            if (__builtin_mul_overflow(b, base, &b)) {
                return false;
            }
            ll toAdd;
            if (__builtin_mul_overflow(str[i] - '0', b, &toAdd)) {
                return false;
            }
            if (__builtin_add_overflow(sum, toAdd, &sum)) {
                return false;
            }
        }
        return sum <= m;
    };

    ll l = mx, r = 1e18 + 1.0l;
    while (true) {
        ll mid = (l + r) / 2;
        if (mid == l) break;
        if (isValid(mid)) l = mid;
        else r = mid;
    }

    cout << l - mx << '\n';

    return 0;
}


#include <bits/stdc++.h>

int n;
struct node {
    int v, t, x;
}a[100010];
bool cmp(node a, node b) {
    return a.v < b.v;
}
int main() {
    scanf("%d", &n);
    for(int i=1;i<=n;i++) scanf("%d%d%d", &a[i].t, &a[i].v, &a[i].x);
    std::sort(a + 1, a + n + 1, cmp);
    for(int i=1;i<=n;i++) {
        if(a[i].x - a[i].t > 0) {
            printf("%d", a[i].v);
            return 0;
        }
    }
    puts("-1");
    return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define all(a) (a).begin(), (a).end()

using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll INF_L = 1LL << 60;
const int INF_I = 1 << 30;
const int MOD = (int)1e9 + 7;
const double PI = acos(-1);
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

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

    rep(i, n)
    {
        x[i] -= a[i];
        x[i] = max(x[i], 0);
    }

    int ans = 1001001001;
    rep(i, n)
    {
        if (x[i])
            ans = min(p[i], ans);
    }
    cout << (ans == 1001001001 ? -1 : ans) << "\n";
    return 0;
}


#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <vector>

using namespace std;

typedef long double LD;
typedef pair<int64_t, int64_t> II;
typedef pair<int64_t, II> III;

static const int64_t INF = 1LL << 60;

struct Dijkstra {
	vector<vector<II>> edges;
	vector<int64_t> c;
	vector<int64_t> d;

	Dijkstra(int64_t size) : edges(size) { }

	void add_edges(std::vector<int64_t>& A, std::vector<int64_t>& B) {
		for (int i = 0; i < A.size(); ++i) {
			add_edge(A[i], B[i], i);
		}
	}

	void add_edge(int64_t a, int64_t b, int64_t c = 1) {
		edges[a].push_back({ b, c });
		edges[b].push_back({ a, c });
	}

	vector<int64_t> get_min_costs(int64_t start) {
		vector<int64_t> min_costs(edges.size(), 1LL << 60);
		priority_queue<pair<int64_t, int64_t>, vector<pair<int64_t, int64_t>>, greater<>> q;
		min_costs[start] = 0;
		q.emplace(make_pair(0, start));
		while (!q.empty()) {
			pair<int64_t, int64_t> top = q.top();
			q.pop();
			int64_t cost = top.first, node = top.second;
			if (cost > min_costs[node]) continue;
			for (auto kv : edges[top.second]) {
				int64_t next_node = kv.first, i = kv.second;
				int64_t cc = c[i], dd = d[i];
				int64_t next_cost = top.first + cc + dd / (cost + 1);

				int64_t t = cost;
				int64_t sq = sqrt(dd);
				t = max(t, sq);

				auto calc = [&](int64_t t) {
					return cc + dd / (t + 1) + t;
				};

				int64_t left = t, right = t + 1e9;
				for (int tt = 0; tt < 100; ++tt) {
					int64_t p = calc((left * 2 + right) / 3);
					int64_t q = calc((left + right * 2) / 3);

					if (calc((left * 2 + right) / 3) <= calc((left + right * 2) / 3)) {
						right = (left + right * 2) / 3;
					} else {
						left = (left * 2 + right) / 3;
					}
				}
				for (int64_t tt = 0; tt < 6; ++tt) {
					next_cost = min(next_cost, calc(left + tt));
				}

				if (next_cost < min_costs[next_node]) {
					min_costs[next_node] = next_cost;
					q.emplace(make_pair(next_cost, next_node));
				}
			}
		}
		return min_costs;
	}
};

int64_t solve(int64_t N, int64_t M, std::vector<int64_t> A, std::vector<int64_t> B, std::vector<int64_t> C, std::vector<int64_t> D) {
	Dijkstra d(N);
	d.c = C;
	d.d = D;
	d.add_edges(A, B);
	vector <int64_t> costs = d.get_min_costs(0);
	int64_t ans = costs[N - 1];
	return ans < (1LL << 60) ? ans : -1;
}

int main() {
#if DEBUG || _DEBUG
	freopen("in.txt", "r", stdin);
	//	freopen("in_1.txt", "r", stdin);
#endif
	int64_t N, M;
	std::cin >> N >> M;
	std::vector<int64_t> A(M), B(M), C(M), D(M);
	for (int i = 0; i < M; i++) {
		std::cin >> A[i] >> B[i] >> C[i] >> D[i];
		--A[i], --B[i];
	}
	cout << solve(N, M, std::move(A), std::move(B), std::move(C), std::move(D)) << endl;
	return 0;
}

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

//wierd debugging stuff
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << "\n" ; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
//end of weird debugging stuff

using ll = long long;
using ld = long double;
using vi = vector<int>;
using ii = pair<int,int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;

#define F_OR(i, a, b, s) for (int i = (a); ((s) > 0 ? i < (b) : i > (b)); i += (s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define rep(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define each(x) for(auto& a: x)
#define ar array
#define pb push_back
#define vt vector

template<class T> bool umin(T& a,const T& b){return b<a?a=b,1:0;}
template<class T> bool umax(T& a,const T& b){return b>a?a=b,1:0;}

template<class A> void read(vt<A>& v);
template<class A, size_t S> void read(ar<A, S>& a);
template<class T> void read(T& x) {cin>>x;}
void read(double& d) {string t;read(t);	d=stod(t);}
void read(long double& d) {	string t;read(t);d=stold(t);}
template<class H, class... T> void read(H& h, T&... t) {read(h);read(t...);}
template<class A> void read(vt<A>& x) {	each(x)read(a);}

// no change
const ll MOD = 1e9+7;
/*
 p = each(x);
 */

void Solve(){
	int n,m;
	cin >> n >> m;
	vector<tuple<ll,ll,ll>> adj[n+1];
	for(int i = 0;i<m;i++){
		ll a,b,c,d;
		read(a,b,c,d);
		adj[a].push_back(make_tuple(b,c,d));
		adj[b].push_back(make_tuple(a,c,d));
	}
	priority_queue<ii,vt<ii>,greater<ii>> q;
	q.push(make_pair(0,1));
	ll inf = 2e18;
	ll x,y,t;
	vl dist(n+1,inf);
	dist[1] = 0;

	while(!q.empty()){
		auto p = q.top();q.pop();
		x = p.first ,y = p.second;
		if(x > dist[y])continue;
		for(auto [b,c,d] : adj[y]){
			ll t = dist[y];
			ll k = sqrt((double)d);
			ll x = inf;
			rep(3){
				ll s = k -2 +i;
				if(s<0 || s < t)continue;
				x = min(x,c+(s-t)+ d/(s+1));
			}
			x = min(x,c+d/(t+1));
			if(x+t < dist[b]){
				dist[b] = x+t;
				q.push(make_pair(x+t,b));
			}
		}
	}

	if(dist[n] == inf ){
		cout << -1 << "\n";
		return;
	}
	cout << dist[n] << "\n";

}

int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int T = 1;
	// cin >> T;		//cmt if one test case
    rep(T)Solve();
}


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

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,k;
    cin>>n>>k;
    vector<pair<int,int>>v(n);
    for(int i=0;i<n;i++){
        int a,b;
        cin>>a>>b;
        v[i].first=a;
        v[i].second=b;
    }
    sort(v.begin(),v.end());
    for(int i=0;i<n;i++){
        if(k>=v[i].first) k+=v[i].second;
        else break;
    }
    cout<<k;
    return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 1; i <= (n); ++i)
#define debug(a) cout << #a << " = " << a << endl;
using namespace std;
typedef long long ll;
int main()
{
    ll n, now; cin >> n >> now;

    map<ll, ll> mp;
    rep(i, n) {
        ll a, b; scanf("%lld %lld", &a, &b);
        mp[a] += b;
    }

    for (auto& [a, b] : mp) {
        if (a <= now) now += b;
        else break;
    }
    cout << now << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long double ld;
typedef pair<ll, ll> pll;
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define ROF(i, a, b) for(int i = a; i >= b; i--)
#define ms memset
#define pb push_back
#define F first
#define S second
ll MOD = 1000000007;
ll INF = 1000000000;

ll power(ll base, ll n){
	if (n == 0) return 1;
	if (n == 1) return base;
	ll halfn = power(base, n/2);
	if (n % 2 == 0) return (halfn * halfn) % MOD;
	return (((halfn * halfn) % MOD) * base) % MOD;
}

ll inverse(ll n){
	return power(n, MOD-2);
}

ll add(ll a, ll b){
	return (a+b) % MOD;
}

ll mul(ll a, ll b){
	return (a*b) % MOD;
}

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

int main(){
    ios::sync_with_stdio(false);
    ll n; cin >> n;
    vector<ll> a(n);
    FOR(i, 0, n){
    	cin >> a[i];
    	if (i % 2 == 1){
    		a[i] = 0 - a[i];
		}
	}
	vector<ll> A(n+1);
	map<ll, ll> mp;
	mp[0] = 1;
	ll ans = 0;
	FOR(i, 1, n+1){
		A[i] = A[i-1] + a[i-1];
		ans += mp[A[i]];
		mp[A[i]]++;
	}
	cout << ans << '\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
#define N 110000
//cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);

int main(){
  ll n; cin >> n;
  vector<string> s;
  vector<string> t;
  string x;
  REP(i,n){
    cin >> x;
    if (x[0]=='!') {
      x=x.erase(0,1);
      t.pb(x);
    }
    else {
      s.pb(x);
    }
  }
  sort(s.begin(), s.end());
  sort(t.begin(), t.end());
  string ans = "satisfiable";
  REP(i,s.size()) {
    ll ind = lower_bound(t.begin(),t.end(),s[i])-t.begin();
    if (ind<t.size()) if (s[i]==t[ind]) {
      ans = t[ind];
      break;
    }
  }
  cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long int
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define endl "\n";
#define rep(i, a, b) for (int i=a; i<b; i++)
#define pre(i, a, b) for (int i=a; i>=b; i--)

const int MAX_N = 1e5+5;
const int MOD = 1e9 + 7;
const int INF = 1e9;

#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>

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

	rep(i,0,k){
		if(!(n%200)){
			n /= 200;
		} else {
			string temp = to_string(n) + "200";
			n = stoll(temp);
		}
	}
	cout<<n;
}

signed main() {
    fast

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

    cout.precision(10);
    cout<<fixed;

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

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

/*
これを入れて実行
g++ code.cpp
./a.out
 */

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>
#include <bitset>
#include <functional>
#include <cassert>
#include <random>

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

using namespace std;
typedef long long ll;
typedef long double ld;

int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};

const long long INF = 1LL << 61;
const ll MOD = 1e9 + 7;

bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
    return f.second > s.second;
}

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

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

ll conbinationMemo[201][12];

void cmemoInit(){
    rep(i, 201){
        rep(j, 13){
            conbinationMemo[i][j] = -1;
        }
    }
}

ll nCr(ll n, ll r){
    if(conbinationMemo[n][r] != -1) return conbinationMemo[n][r];
    if(r == 0 || r == n){
        return 1;
    } else if(r == 1){
        return n;
    }
    return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1));
}

ll nPr(ll n, ll r){
    r = n - r;
    ll ret = 1;
    for (ll i = n; i >= r + 1; i--) ret *= i;
    return ret;
}
//-----------------------ここから-----------
ll n, m;
vector<ll> a, b;
ll dp[1010][1010] = {0};
ll rec(ll i, ll j){
    if(i == n && j == m) return 0;
    if(i >= n || j >= m) return m - j + n - i;

    if(dp[i][j] != -1) return dp[i][j];
    ll res = INF;
    if(a[i] == b[j]){
        res = min(res, rec(i + 1, j + 1));
    }
    res = min(res, rec(i + 1, j) + 1);
    res = min(res, rec(i, j + 1) + 1);
    res = min(res, rec(i + 1, j + 1) + 1);

    return dp[i][j] = res;
}
int main(void){
    cin >> n >> m;
    a.resize(n);
    b.resize(m);
    rep(i, n) cin >> a[i];
    rep(i, m) cin >> b[i];
    rep(i, 1010) rep(j, 1010) dp[i][j] = -1;

    cout << rec(0, 0) << endl;

}


/*  Jai Shree Ram 🚩🚩🚩 */
#include "bits/stdc++.h"
#define ll long long int
#define oo 1000000000000000000
#define forr(i,n) for(int i=0;i<n;i++)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define all(x) x.begin(),x.end()
#define unique(v) sort(all(v)); v.resize(distance(v.begin(),unique(all(v))))
#define eb emplace_back
#define FF first
#define SS second
#define mem(a,v) memset(a,v,sizeof(a))
#define pb push_back
#define popcount(x) __builtin_popcount(x)

using namespace std;

template<typename T>
ostream &operator<<(ostream &output,const vector<T> &v){
    if(v.empty()) return output;
    for(int i=0;i<v.size()-1;i++) output << v[i] <<" ";
    output << v.back();
    return output;
}
template<typename T>
istream &operator>>(istream &input,vector<T> &v){
    for(auto &i: v) cin >> i;
    return input;
}

void __sol(){
    int n,M; cin >> n >> M;
    map<int,vector<int>> m;
    forr(i,M){
        int x,y; cin >> x >> y;
        m[x].eb(y);
    }
    set<int> s = {n};
    int last = 0;
    for(auto &g: m){
        vector<int> add , del;
        for(auto &i: g.SS){
            if(s.count(i-1)) add.eb(i);
            else if(s.count(i+1)) add.eb(i);
            else if(s.count(i)) del.eb(i);
        }
        for(auto &i: del) s.erase(i);
        for(auto &i: add) s.insert(i);
    }
    int ans = 0;
    cout << s.size();
    return;
}

int main(){
    fastio;
    int tc=1;  // cin >> tc;
    while(tc--) __sol();
    return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define F first
#define S second
#define mp make_pair
#define vi vector<int>
#define pb push_back
using namespace std;
const int N=2e5+5;
ll dp[N*4][2];pii p[N];int n,useless;
map<int,int>m,M;int cnt;vi v[N*4];
int main()
{
	cin>>useless>>n;
	for(int i=0;i<n;i++)
	{
		cin>>p[i].F>>p[i].S;
		m[p[i].F]=1;
	}
	for(map<int,int>::iterator it=m.begin();it!=m.end();it++)
		it->S=++cnt;
	for(int i=0;i<n;i++)p[i].F=m[p[i].F];
	m.clear();cnt=0;m[0]=m[2*useless]=m[useless]=1;
	for(int i=0;i<n;i++)m[p[i].S]=m[max(p[i].S-1,0)]=m[min(p[i].S+1,2*useless)]=1;
	for(map<int,int>::iterator it=m.begin();it!=m.end();it++)
		it->S=++cnt;
	for(int i=0;i<n;i++)p[i].S=m[p[i].S];
	for(int i=0;i<n;i++)
		v[p[i].F].pb(p[i].S);
	dp[m[useless]][0]=dp[m[useless]][1]=1;
	for(int i=0;i<=n;i++)
	{
		for(int j=0;j<v[i].size();j++)
			dp[v[i][j]][1]=min((v[i][j]!=1?dp[v[i][j]-1][0]:0)+(v[i][j]!=cnt?dp[v[i][j]+1][0]:0),1ll);
		for(int j=0;j<v[i].size();j++)
			dp[v[i][j]][0]=dp[v[i][j]][1];
	}
	int ans=0;
	for(int i=1;i<=m[2*useless];i++)ans+=min(dp[i][0],1ll);
	cout<<ans<<endl;
	return 0;
}