#include<bits/stdc++.h>
using namespace std;
#define int long long //delete if causing problems
#define F first
#define dbg(x)  cout<<#x<<" "<<x<<endl;
#define S second
#define setbit(n) __builtin_popcount(n)
#define all(x) x.begin() , x.end()
#define clr(x) memset(x,0,sizeof(x))
#define fast  ios_base::sync_with_stdio(0); cin.tie(0);
#define endl "\n" //delete if interactive
#define MOD 1000000007
const int inf = 1e18;
int power(int a, int b);

signed   main()
{
	fast
	int tt = 1;
	//cin >> tt;
	while (tt--)
	{
		int n;
		cin >> n;
		int l = -inf, r = inf, add = 0;

		for (int i = 1; i <= n; i++) {
			int ele, type;
			cin >> ele >> type;

			if (type == 1) {
				add += ele;
				l += ele;
				r += ele;
			}
			else if (type == 2) { //max
				l = max(l, ele);
				r = max(r, ele);
			}
			else {
				l = min(l, ele);
				r = min(r, ele);
			}
		}

		int q;
		cin >> q;

		while (q--) {
			int x;
			cin >> x;

			int ans = x;
			ans += add;
			if (ans > r) ans = r;
			if (ans < l) ans = l;

			cout << ans << endl;
		}

	}
	return 0;
}

int power(int a, int b)
{
	int res = 1;
	while (b)
	{
		if (b % 2) b-- , res = res * a;
		else b = b / 2 , a *= a;
	}
	return res;
}

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

#include <bits/stdc++.h>
#define rep3(i, s, n, a) for (long long i = (s); i < (long long)(n); i += a)
#define rep2(i, s, n) rep3(i, s, n, 1)
#define rep(i, n) rep2(i, 0, n)

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

int main() {
    ll n;
    cin >> n;
    // vector<ll> x, t;
    vector<ll> boarder(2), b(2);
    boarder[0] = INT64_MIN;
    boarder[1] = INT64_MAX;
    // rep(i, 2){
    //     b[i] = boarder[i];
    // }
    boarder[0] += (ll)(3*100000)*(ll)INT32_MAX;
    boarder[1] -= (ll)(3*100000)*(ll)INT32_MAX;
    ll num = 0;
    rep(i, n){
        ll a, t;
        cin >> a >> t;
        if(t==1){
            num += a;
        }
        if(t==2){
            if(boarder[0]+num<a){
                boarder[0] = a-num;
                // b[0] = boarder[0] + num;
            }
            if(boarder[1]+num<a){
                boarder[1] = a-num;
                // boarder[1] = INT64_MAX - (ll)(3*100000)*(ll)INT32_MAX;
                // boarder[0] = boarder[1];
                // b[1] = INT64_MAX - (ll)(2*10000)*(ll)INT32_MAX;
            }
        }
        if(t==3){
            if(a<boarder[0]+num){
                boarder[0] = a-num;
                // boarder[0] = INT64_MIN + (ll)(3*100000)*(ll)INT32_MAX;
                // boarder[1] = boarder[0];
                // b[0] = INT64_MIN + (ll)(2*10000)*(ll)INT32_MAX;
            }
            if(a<boarder[1]+num){
                boarder[1] = a-num;
                // b[1] = INT64_MAX - (ll)(2*10000)*(ll)INT32_MAX;
            }
        }
    }
    int q;
    cin >> q;
    rep(i, q){
        ll x;
        cin >> x;
        if(x<boarder[0]){
            cout << boarder[0] + num << endl;
        }else{
            if(x<boarder[1]){
                cout << x + num << endl;
            }else{
                cout << boarder[1] + num << endl;
            }
        }
    }
    return 0;
}


//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include <algorithm>
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define PI              acos(-1.0)
#define No              cout<<"No\n"
#define Yes             cout<<"Yes\n"
#define no              cout<<"no\n"
#define yes             cout<<"yes\n"
#define NO              cout<<"NO\n"
#define YES             cout<<"YES\n"
// #define MOD             (int)1000000007
#define int             long long
#define ll              long long
#define pii             pair<int, int>
#define fi              first
#define se              second
#define sf(a)           scanf("%lld", &a)
#define pf(a)           printf("%lld ", a)
#define pfn(a)          printf("%lld\n", a)
#define case(a)         cout<<"Case "<<a<<": ";
#define FASTIO          ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define pb              push_back
#define mem(arr)        memset(arr, 0, sizeof(arr))
#define mem1(arr)       memset(arr, -1, sizeof(arr))
#define all(a)          a.begin(),a.end()
#define f(i,a,b)        for (int i = a; i<=b; i++)
#define fr(i,a,b)       for (int i = a; i>=b; i--)
#define rr              return 0
#define prec(n)         fixed<<setprecision(n)
#define maxpq           priority_queue<int>
#define minpq           priority_queue<int, vector<int>, greater<int> >
#define inf             (int)(1e18)
#define vi              vector<int>
#define vii             vector<pii>
#define sz(s)           s.size()
#define lcm(a,b)        (a*(b/gcd(a,b)))

int gcd(int a, int b)   {return __gcd(a,b);}

int MOD;
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p & 1LL) r = modMul(r, b); b = modMul(b, b); p >>= 1LL; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }

inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n or (1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }

const double eps = 1e-9;

int dx[5] = {+1, +0, -1, -0};
int dy[5] = {+0, +1, -0, -1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

inline bool Equal(double x, double y) { return fabs(x-y)<eps; }
inline bool Greater(double x, double y){ return x-eps>y; }
inline bool Lesser(double x, double y){ return x+eps<y; }

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cerr << '\n'; }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << " "; err(++it, args...); }

///Write what you need -->
/// nt, ft, kmp, tri, sufarr, ub_lb, lis
/// dsu, combi

///Template Ends Here////////////////////////////

signed main()
{
//    root = new trinode();
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    FASTIO

//    int n;
//    cin>>n;
//
//    int a[n+1];
//    f(i, 1, n){
//        cin>>a[i];
//    }

    int n, m;
    cin>>n>>m;

    MOD = m*m;
    int r = modPow(10, n);

    MOD = m;
    int r1 = modPow(10, n);

    cout<<(r-r1)/m;

//    tridel(root);
}

#include <bits/stdc++.h>
//#include <atcoder/modint.hpp>
using namespace std;
#define all(a)a.begin(),a.end()
using ll=long long;
const int INF = 1<<30;
const ll INFll =1LL<<62;
const int mod= int(1e9)+7;
const int mod2=998244353;
using P = pair<int,int>;
using Pl= pair<ll,ll>;
using ld=long double;
using V=vector<int>;
using Vl=vector<ll>;
using Vd=vector<ld>;
using VV=vector<vector<int>>;
using VVl=vector<vector<ll>>;
using VVd=vector<vector<ld>>;
//using mint=atcoder::modint1000000007;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;cin >>n;
    string t;cin >>t;
    ll k=1e10;
    ll ans=0;
    for (int i = 0; i < 3; ++i) {
        bool flg=true;
        for (int j = 0; j < n; ++j) {
            char s=(i+j)%3==2?'0':'1';
            if(s!=t[j])flg=false;
        }
        if(flg){
            ll pl=k-((n+i-1)/3+1);
            ans+=(pl+1);
        }
    }
    cout <<ans<<"\n";
    return 0;
}


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

#define forn(i,n) for(int i=0;i<(int)(n);i++)
#define si(c) ((int)(c).size())
#define forsn(i,s,n) for(int i = (int)(s); i<((int)n); i++)
#define dforsn(i,s,n) for(int i = (int)(n)-1; i>=((int)s); i--)
#define all(c) (c).begin(), (c).end()
#define D(a) cerr << #a << "=" << (a) << endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair

typedef long long int ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 2e5 + 10;

int n, p[N];
bool vis[N];
vi ans;

void swp(int i) {
    if (vis[i]) {
        cout << "-1\n";
        exit(0);
    }
    vis[i] = 1;
    swap(p[i], p[i+1]);
    ans.pb(i);
}

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

    cin >> n;
    forn(i,n) cin >> p[i];

    for (int i = 1; i < n; i++) {
        int j = i;
        while (j > 0 && p[j] < p[j-1]) {
            swp(j-1);
            --j;
        }
    }
    if (si(ans) != n-1) cout << "-1\n";
    else for (auto x : ans) cout << x+1 << '\n';

    return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
using namespace std;
using ll = long long;
const int INF = 1e9;
const ll LINF = 1e18;
template <class T>
void get_unique(vector<T>& x) {
    x.erase(unique(x.begin(), x.end()), x.end());
}
template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
    for (int i = 0; i < int(v.size()); i++) {
        is >> v[i];
    }
    return is;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0; i < int(v.size()); i++) {
        os << v[i];
        if (i < sz(v) - 1) os << ' ';
    }
    return os;
}
int main() {
    string a, b;
    cin >> a >> b;
    int sa = 0, sb = 0;
    for (char c : a) sa += c - '0';
    for (char c : b) sb += c - '0';
    cout << max(sa, sb) << '\n';
}


//#include "test.hpp"
#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define pre(a,b) cout<<fixed<<setprecision(a)<<b<<"\n";//a for precision b for answer
#define mp make_pair
#define pb push_back
#define forn(n) for(ll i=0;i<n;i++)
#define ff first
#define ss second
#define MAX 1e18
#define all(x) (x).begin(),(x).end()
#define lb lower_bound
#define ub upper_bound
const double pi =  3.141592653589793238;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;
const int N=3e5+5;
//cout<<"Case #"<<p<<": "<<;
//file input  freopen("input.txt","r",stdin);
//file output freopen("output.txt","w",stdout);

using namespace std;

//check edge cases, alternate methods, n==1,
//check MOD1,MOD2,
//stuck?? - try simplifying expression, try reversing the problem,

int32_t main()
{
  IOS;
  ll a,b,c;
  cin>>a>>b>>c;
  (a+c>b)?cout<<"Takahashi":cout<<"Aoki";
}

#include <iostream>
#include <algorithm>
using namespace std;

main()
{
    string tak = "Takahashi";
    string aok = "Aoki";
     int a, b, c;
     cin >> a >> b >> c;
     if(c == 0){
        while(true)
        {
            if(a <= 0){cout << aok;return 0;}
            a--;
            if(b <= 0){cout << tak; return 0;}
            b--;
        }
     }else {
         while(true)
        {
            if(b <= 0){cout << tak; return 0;}
            b--;
            if(a <= 0){cout << aok;return 0;}
            a--;
        }
     }
}


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

typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)
template<class T, class U> void umin(T& x, const U& y){ x = min(x, (T)y);}
template<class T, class U> void umax(T& x, const U& y){ x = max(x, (T)y);}
template<class T, class U> void init(vector<T> &v, U x, size_t n) { v=vector<T>(n, (T)x); }
template<class T, class U, typename... W> void init(vector<T> &v, U x, size_t n, W... m) { v=vector<T>(n);  for(auto& a : v) init(a, x, m...); }

int main(int argc, char** argv) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
    if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);

    int n;
    cin >> n;
    vector<int> a(n), b(n);
    vector<ll> c(n);
    FOR(i, n) cin >> a[i];
    FOR(i, n) cin >> b[i];

    ll am = a[0];
    ll bm = b[0];
    ll x = am*bm;
    FOR(i, n){
        umax(am, a[i]);
        if (b[i] > bm) {
            bm = b[i];
            x = am*bm;
        }
        umax(x, am * b[i]);
        c[i] = x;
    }
    FOR(i, n) cout << c[i] << '\n';

    if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
    return 0;
}

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

int main(){
    int a,b; cin >> a >> b;
    printf("%.5f\n",(a-b)/(double)a*100);
}


#include <bits/stdc++.h>
//#define rep(i,a,n) for (int i=a;i<n;i++)
#define overload4(_1, _2, _3, _4, name, ...) name
#define rep1(n) for(ll i = 0; i < (n); ++i)
#define rep2(i, n) for(ll i = 0; i < (n); ++i)
#define rep3(i, a, b) for(ll i = (a); i < (b); ++i)
#define rep4(i, a, b, c) for(ll i = (a); i < (b); i += (c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)

#define forn(i, n) for(int i = 0 ; (i) < (n) ; ++i)
#define rrep(i,a,n) for (int i=n-1;i>=a;i--)
#define ALL(x) x.begin(),x.end()

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pause "read -p 'Press Enter to continue...' var"

using namespace std;

//int gcd(int a,int b){return b?gcd(b,a%b):a;}
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; }
/*
テスト通りますように
　　●
　　/⌒ヽ
　 ｜ 　|/⌒ヽ(ヽ
　 (｀　∥ー⌒) |
　|￣||￣￣￣￣￣|
　|―||―――――|
　|　Ｕ　　　　　|
　|￣￣￣￣￣￣￣|
　|＿＿＿＿＿＿＿|
　　|―――――|
　　|―――――|
ｗｗＷｗｗＷｗＷＷｗ
*/

typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;

const ll INF = numeric_limits<ll>::max()/4;
const ll MAX = 200000;
const int MOD = 1e9 + 7;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int main(){
  ll n,C;
  cin >> n >> C;
  vector<pll> vec;
  vec.pb(mp(0,0));
  rep(i,n){
    ll a,b,c;
    cin >> a >> b >> c;
    vec.pb(mp(a,c));
    vec.pb(mp(b+1,c*-1));
  }
  sort(ALL(vec));
  ll now = 0;
  ll ans = 0;
  vec.pb(mp(vec[vec.size()-1].fi + 1,0));
  rep(i,vec.size()-1){
    now += vec[i].se;
    //cout << vec[i].fi << ' ' << vec[i+1].fi << ' ' << now << endl;
    if(vec[i].fi == vec[i+1].fi) continue;
    if(now >= C){
      ans += C * (vec[i+1].fi - vec[i].fi);
      //cout << "+=" << C * (vec[i+1].fi - vec[i].fi) << endl;
    }
    else{
      ans += now * (vec[i+1].fi - vec[i].fi);
      //cout << "2+=" << now * (vec[i+1].fi - vec[i].fi) << ' ' << ans << endl;
    }
  }
  cout << ans << endl;
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1e9+7;
void solve(){
    int n,C;cin>>n>>C;
    vector<int>a(n),b(n),c(n);
    for(int i=0;i<n;i++) cin>>a[i]>>b[i]>>c[i];
    map<int,int>mp;
    for(int i=0;i<n;i++){
        mp[a[i]]+=c[i];
        mp[b[i]+1]-=c[i];
    }
    int curr=0;
    int prev=0;
    int ans=0;
    for(auto x:mp){
        ans+=min(C,curr)*(x.first-prev);
        curr+=x.second;
        prev=x.first;
    }
    cout<<ans;
}
int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int T=1;
    // cin>>T;
    while(T--){
        solve();
    }
}


#include<bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x).size()
#define int long long int
#define loop(i,a,b) for(int i=a;i<b;i++)
#define scan(arr,n) for (int i = 0; i < n; ++i) cin >> arr[i]
#define vi vector<int>
#define si set<int>
#define pii pair <int, int>
#define sii set<pii>
#define vii vector<pii>
#define mii map <int, int>
#define pb push_back
#define ff first
#define ss second
#define all(aa) aa.begin(), aa.end()
#define rall(a) a.rbegin() , a.rend()
#define read(a,b) int a,b;  cin>>a>>b
#define readt(a,b,c) int a,b,c; cin>>a>>b>>c
#define readf(a,b,c,d) int a,b,c,d; cin>>a>>b>>c>>d;
#define print(v) for(auto x:v) cout<<x<<" ";cout<<endl
#define printPair(res) for(pair<int,int>& p:res) cout<<p.first<<" "<<p.second<<endl;
#define faster ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

int fac[300001];
void fact(int m){fac[0]=1;for(int i=1;i<=300000;i++) fac[i]=(fac[i-1]%m * i%m)%m;}
/* Iterative Function to calculate (x^y)%p in O(log y)*/
long long power( long long x,  int y, int p){   long long res = 1;
x = x % p; while (y > 0) { if (y & 1)  res = (res * x) % p;  y = y >> 1; x = (x * x) % p;} return res;}
// Returns n^(-1) mod p
long long modInverse( long long n, int p) {  return power(n, p - 2, p); }
long long nCr( long long n,int r, int p) { if (r == 0) return 1; if(n<r) return 0;
return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; }

int binarySearch(vi arr, int l, int r, int x) {
if (r >= l) { int mid = l + (r - l) / 2; if (arr[mid] == x) return mid;
if (arr[mid] > x)  return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x); } return -1; }
bool sortGrt(const pair<int,int> &a, const pair<int,int> &b)
{
    return (a.second > b.second);
}

void solve(int test)
{
  int n;cin>>n;
  vi a(n);
  scan(a,n);
  sort(all(a));
  int suffix[n]={0};
  suffix[n-1]=0;
  for(int i=n-2;i>=0;i--)
  {
    suffix[i]=suffix[i+1]+a[i+1];
  }
  // loop(i,0,n)
  // cout<<suffix[i]<<" ";
  // cout<<endl;
  int t1=0;
  for(int i=0;i<n;i++)
  {
    t1+=(a[i]*a[i]);
  }
  t1=t1* (n-1);

  int t2=0;
  loop(i,0,n)
  {
    t2+= a[i]*suffix[i];
  }
  t2*=2;

  t1=t1-t2;

  cout<<t1;
}

int32_t main()
{
      #ifndef ONLINE_JUDGE
          freopen("input.txt", "r", stdin);
          freopen("output.txt", "w", stdout);
      #endif
      faster;
      //fact(mod);
      int t=1;
      //cin>>t;
      for(int test=1;test<=t;test++)
      {
          solve(test);
      }
}

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

struct Fast {Fast() {std::cin.tie(0); ios::sync_with_stdio(false);}} fast;

using ull = unsigned long long;
using ll = long long;
using vi = vector<int>;
using vl = vector<long>;
using vll = vector<long long>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vvll = vector<vll>;
using vs = vector<string>;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define YESNO(bool) if(bool){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define yesno(bool) if(bool){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}
#define YesNo(bool) if(bool){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}

#define reps(i, a, n) for (ll i = (a); i < (ll)(n); ++i)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) reps(i, 1, n + 1)
#define repd(i,n) for(ll i=n-1;i>=0;i--)
#define rrepd(i,n) for(ll i=n;i>=1;i--)

#define debug(x) cerr << "\033[33m(line:" << __LINE__ << ") " << #x << ": " << x << "\033[m" <<endl;

inline int in_int() {int x; cin >> x; return x;}
inline ll in_ll() {ll x; cin >> x; return x;}
inline string in_str() {string x; cin >> x; return x;}
template <typename T> inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;}
template <typename T> inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;}

int main()
{
    int n = in_int();
    ll ret = 0;
    ll x = 0;
    ll y = 0;
    rep(i, n) {
        int a = in_int();
        ret += i * a * a - 2 * a * x + y;
        x += a;
        y += a * a;
    }
    //reps(i, 1, n) {
    //    rep(j, i) {
    //        ret += (a[i] - a[j]) * (a[i] - a[j]);
    //    }
    //}
    cout << ret;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <random>
#include <cstdio>
#include <cmath>
#include <climits>
#include <map>
#include <queue>
#include <functional>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (int) n; i++)
#define rep1(i, n) for(int i = 1; i <= (int) n; i++)
#define all(v) v.begin(),v.end()
const ll mod = 1e9+7;

// int main() {
//     int a,b,c,d;
//     cin >> a >> b >> c >> d;
//     cout << b-c << endl;
//     return 0;
// }
// /*
// int main() {
//     string s;
//     cin >> s;
//     rep(i,s.size()) {
//         if (s[i] == '.') {
//             cout << endl;
//             return 0;
//         }
//         cout << s[i];
//     }
//     cout << endl;
//     return 0;
// }

int main() {
    ll n;
    cin >> n;
    int d = 0;
    ll m = n;
    while (m > 0) {
        d++;
        m /= 10;
    }
    int ans = 0;
    for (int i = 1; i <= 7; i++)
    {
        for (int j = pow(10,i-1); j < pow(10,i); j++)
        {
            if (n >= j*pow(10,i) + j) {
                ans++;
            } else {
                cout << ans << endl;
                return 0;
            }
        }
    }

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int main(){
	cin >> n;
	for(int i=0; i<n ;i++){
		cout << i*2%n+1 << ' ' << (i*2+1)%n+1 << '\n';
	}
}


#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll=int;
using ld=long double;
using pll=pair<ll, ll>;
//using mint = modint1000000007;
#define rep(i,n) for (ll i=0; i<n; ++i)
#define all(c) begin(c),end(c)
#define PI acos(-1)
#define oo 2e18
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
//priority_queue<ll, vector<ll>, greater<ll>> Q;
/*
01234でタイルの方向管理？
*/
#define TIMEOVER_M 25000
#define TIMEOVER 1950000

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

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

// 行き止まりになったら、その時点でのstringを出力
void dfs(ll y, ll x, ll c){// {y, x, 歩数}
  bool used[4]={};
  rep(i, 4){
    clock_t now = clock();
    if(now-start_m>TIMEOVER_M) fin=true;
    if(now-start>TIMEOVER) all_fin=true;
    ll r=now%4;
    while(used[r]) (r+=1)%=4;
    used[r]=true;
    ll ny=y+dy[r];
    ll nx=x+dx[r];
//    cout << "time:" << now-start << endl;
    if(ny<0 || nx<0 || ny>49 || nx>49 || B[ny][nx] || fin || all_fin){
      if(chmax(high, score)){
        if(ans) free(ans);
        ans=strdup(W);
      }
    }
    else{
      //反映
      score+=P[ny][nx];
      W[c]=dc[r];
      steped(ny, nx, true);

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

}

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

#pragma GCC optimize("Ofast")
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip> // setprecision
#include <complex> // complex
#include <math.h>
#include <functional>
#include <cassert>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
constexpr ll INF = 1e18;
constexpr ll LLMAX = 9223372036854775807;
constexpr int inf = 1e9;
constexpr ll mod = 1'000'000'007;
// constexpr ll mod = 998244353;
const int dx[8] = {1, 0, -1, 0,1,1,-1,-1};
const int dy[8] = {0, 1, 0, -1,1,-1,1,-1};
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; }
#define eol endl
// ---------------------------------------------------------------------------

#include<time.h>

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  clock_t start,end;
  start = clock();
  int x = 0;
  int sy,sx;
  cin >> sy >> sx;
  int H = 50;
  int W = 50;
  vector<vector<int>> tile_id(H,vector<int>(W));
  for(int i=0; i<H; i++){
    for(int j=0; j<W; j++){
      cin >> tile_id[i][j];
    }
  }
  vector<vector<int>> score(H,vector<int>(W));
  for(int i=0; i<H; i++){
    for(int j=0; j<W; j++){
      cin >> score[i][j];
    }
  }
  vector<bool> used_id(10000,false);
  string str = "RDLU";
  int sum = 0;
  int ans_sum = 0;
  string ans;
  string now;
  auto solve = [&](){
    bool fin = false;
    function<void(int,int)> dfs = [&](int y,int x)->void{
      end = clock();
      if((double)(end-start)/CLOCKS_PER_SEC >= 1.95){
        fin = true;
        return;
      }
      vector<P> can;
      for(int k=0; k<4; k++){
        int ny = y + dy[k];
        int nx = x + dx[k];
        if(ny<0 || ny>=H || nx<0 || nx>=W) continue;
        if(used_id[tile_id[ny][nx]]) continue;
        can.emplace_back(score[ny][nx],k);
      }
      sort(can.rbegin(),can.rend());
      if(can.size() == 0){
        if(chmax(ans_sum,sum)){
          ans = now;
        }
        return;
      }
      for(auto hoge: can){
        int k = hoge.second;
        int ny = y + dy[k];
        int nx = x + dx[k];
        sum += score[ny][nx];
        used_id[tile_id[ny][nx]] = true;
        now += str[k];
        dfs(ny,nx);
        if(fin) return;
        sum -= score[ny][nx];
        used_id[tile_id[ny][nx]] = false;
        now.pop_back();
      }
    };
    sum += score[sy][sx];
    used_id[tile_id[sy][sx]] = true;
    dfs(sy,sx);
    return;
  };
  solve();
  cout << ans << eol;
  return 0;
}


# include<bits/stdc++.h>

using namespace std;
# define l long long
# define db long double
# define rep(i,a,b) for(l i=a;i<b;i++)
# define vi vector<l>
# define vvi vector<vi>
# define vsi vector<set<l> >
# define pb push_back
# define mp make_pair
# define ss second
# define ff first
# define pii pair<l,l>
# define trvi(v,it) for(vi::iterator it=v.begin();it!=v.end();++it)
# define read(a) freopen(a,"r",stdin)
# define write(a) freopen(a,"w",stdout)
# define io ios::sync_with_stdio(false)

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ' ' << p.second << ')'; }

const bool MULTIPLE_TEST_CASES = false;

const l MOD=1e9+7;
const l N=1e5+5;
const l INF=1e12;

void solve() {
	l n;
	l x;
	cin>>n>>x;
	vector<l> v(n), p(n);
	rep(i,0,n) {
		cin>>v[i]>>p[i];
	}
	x*=100;
	l cur = 0;
	rep(i,0,n) {
		cur+= (v[i]*p[i]);
		if(cur>x) {
			cout<<i+1<<"\n";
			return;
		}
	}
	cout<<"-1\n";
	return;
}

int main(){
	io;
	int t=1;
	if (MULTIPLE_TEST_CASES) cin>>t;
	rep(i,0,t) {
		solve();
	}

	return 0;
}

#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int n;
char p[201000], st[201000];
int main(){
    int i;
    scanf("%d",&n);
    scanf("%s",p);
    int top = 0;
    int res = 0;
    for(i=0;i<n;i++){
        if(p[i]!='f' && p[i]!='o' && p[i]!='x'){
            res += top;
            res++;
            top=0;
            continue;
        }
        st[++top] = p[i];
        while(st[top]=='x' && st[top-1]=='o' && st[top-2]=='f')top-=3;
    }
    res+=top;
    printf("%d\n",res);
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;

const lld N = 200043;
const lld MOD = 1000000007;

lld add(lld x, lld y)
{
    x =((x%MOD)+(y%MOD))%MOD;
    while(x >= MOD) x -= MOD;
    while(x < 0) x += MOD;
    return x;
}

lld mul(lld x, lld y)
{
    return ((x%MOD)*(y%MOD))% MOD;
}

lld binpow(lld x, lld y)
{
    lld z = 1;
    while(y)
    {
        if(y & 1) z = mul(z, x);
        x = mul(x, x);
        y >>= 1;
    }
    return z;
}

lld inv(lld x)
{
    return binpow(x, MOD - 2);
}

lld divide(lld x, lld y)
{
    return mul(x, inv(y));
}

// Combinations

/*
lld fact[N];

void precalc()
{
    fact[0] = 1;
    for(lld i = 1; i < N; i++)
        fact[i] = mul(fact[i - 1], i);
}

lld C(lld n, lld k)
{   if(k>n)
        return 0;
    return divide(fact[n], mul(fact[k], fact[n - k]));
}
*/

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    lld t,i,j;
    //cin>>t;
    t=1;
    while(t--)
    {
        lld x;
        cin>>x;
        if(x%100==0)
            cout<<100<<endl;
        else
            cout<<100-(x%100)<<endl;
    }
}

#define _USE_MATH_DEFINES
#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<climits>
#include<bitset>
#include<unordered_map>
#include<unordered_set>
#include<random>
#include<list>
#include<functional>

using namespace std;

#define MAX(A,B) ((A)>(B)?(A):(B))
#define MIN(A,B) ((A)<(B)?(A):(B))
#define LP(I,S,G) for (long long int I = S; I < G; I++)
#define IN(X) 	for (int i = 0; i < X.size(); i++)cin >> X[i]
#define OUT(X) 	for (int i = 0; i < X.size(); i++)out << X[i]
#define SORT(X) sort(X.begin(), X.end())
#define CSORT(X,Y) sort(X.begin(), X.end(),Y)
#define COPY(X,Y) copy(X.begin(), X.end(), Y.begin())
#define ALL(X,Y) for (auto X = Y.begin(); X != Y.end(); X++)

template<class I1, class I2>
istream& operator>>(istream& s, pair<I1, I2>& in) {
	s >> in.first >> in.second;
	return s;
}

long long int M = 998244353;

bool comp(pair<int, int>& a, pair<int, int>& b) {
	return (a.second < b.second);
}

vector<int> t(pow(10, 5), 0);

int main(void) {
	int n, m;
	cin >> n >> m;
	vector<int> t(pow(10, 3) + 1, 0);
	LP(i, 0, n) {
		int a;
		cin >> a;
		t[a] = 1;
	}
	LP(i, 0, m) {
		int b;
		cin >> b;
		t[b] |= 2;
	}
	ALL(i, t) {
		if (*i == 1 || *i == 2)cout << (i - t.begin()) << " ";
	}
	return 0;
}


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

int main() {
    int H, W;
    cin >> H >> W;
    char map[H][W];
    rep(i, H) rep(j, W) {
        char c;
        cin >> c;
        map[i][j] = c;
    }
    int count = 0;
    rep(i, H) rep(j, W) {
        if(map[i][j] == '.') {
            if(i+1 < H && map[i+1][j] == '.') count++;
            if(j+1 < W && map[i][j+1] == '.') count++;
        }
    }
    cout << count << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ll long long
#define pp pair<ll,ll>
#define ld long double
#define all(a) (a).begin(),(a).end()
#define mk make_pair
constexpr int inf=1000001000;
constexpr ll INF=2e18;
constexpr ll mod=1000000007;
// ll MOD=998244353;
constexpr ll MOD=998244353;

int main() {
    int h,w,n,m;
    cin >> h >> w >> n >> m;
    vector<vector<int>> a(w,vector<int>(h,0)),b(w,vector<int>(h,0));
    rep(i,n){
        int f,g;
        cin >> f >> g;
        f--;
        g--;
        a[g][f]=2;
    }
    rep(i,m){
        int f,g;
        cin >> f >> g;
        f--;
        g--;
        a[g][f]=1;
    }
    rep(i,h){
        int u=0;
        rep(j,w){
            if (a[j][i]==2) u=1;
            if (a[j][i]==1) u=0;
            if (u==1) b[j][i]=1;
        }
    }
    rep(i,h){
        int u=0;
        rep(j,w){
            if (a[w-j-1][i]==2) u=1;
            if (a[w-1-j][i]==1) u=0;
            if (u==1) b[w-j-1][i]=1;
        }
    }
    rep(i,w){
        int u=0;
        rep(j,h){
            if (a[i][j]==2) u=1;
            if (a[i][j]==1) u=0;
            if (u==1) b[i][j]=1;
        }
    }
    rep(j,w){
        int u=0;
        rep(i,h){
            if (a[j][h-i-1]==2) u=1;
            if (a[j][h-i-1]==1) u=0;
            if (u==1) b[j][h-i-1]=1;
        }
    }
    int ans=0;
    rep(i,h){
        rep(j,w){
            if (b[j][i]==1) ans++;
        }
    }
    cout << ans << endl;
}


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

int main() {
  int64_t N;
  cin >> N;
  set<int64_t> check;
  int64_t ans = 0;

  for (int i = 2; i <= log2(N); i++){
    int a = 2;
    while(true){
      if(pow(a, i) > N){
        break;
      }
      check.insert(pow(a,i));
      a++;
    }
  }

  ans = N - check.size();

  cout << ans <<endl;
}

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

#define ll long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int, int>

#define qi queue<int>
#define sti stack<int>
#define pqi priority_queue<int>
#define pqii priority_queue<pair<int, int>>

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define nl endl

#define INF (int)1e9
#define MOD 1000000007

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

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

   int case_t=1;
   // cin>>case_t; // Multiple testcases

   while(case_t--)
   {
      ll n;
      cin>>n;
      ll n1 = (int)sqrt(n);
      // cout<<n1;
      ll a[n1+5];
      memset(a, 0, sizeof(a));
      // p = 2;
      ll ans = n;
      ll p;
      for(ll p=2; p*p<=n; p++){
         if (a[p] == 1) continue;
         ll x = p*p;
         while(x<=n){
            if(x<=n1 && a[x]==1) ;
            else if (x<=n1){
               a[x]=1;
               ans--;
            }
            else ans--;
            x = x*p;
         }
      }

      // ans += n-p;
      cout<<ans;

      cout<<"\n";
   }

   cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
   return 0;
}


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

ll cnt(ll y,ll x,map<ll,ll> &data){
  if(data.count(y)){
    return data[y];
  }
  if(x>=y){
    data[y] = x-y;
    return (ll) x-y;
  }
  if(y%2==0){
    ll z = min(cnt(y/2,x,data)+1,abs(y-x));
    data[y] = z; return z;
  }
  else{
    ll z = min(min(cnt(y+1,x,data)+1,abs(y-x)),cnt(y-1,x,data)+1);
    data[y] = z; return z;
  }
}

int main(){
  long long X,Y;
  cin >> X >> Y;
  map<ll,ll> data;
  cout << cnt(Y,X,data) << endl;
}

#include <bits/stdc++.h>

#define int long long
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

signed main(){
	fastio
	int x,y;
	cin >> x >> y;
	int ans = 1e18;
	for(int i = 0;i <= 63;i++){
		if(x > LLONG_MAX/2) break;
		int sum = i;
		int diff = abs(x-y);
		sum += diff/(1LL<<i);
		diff %= (1LL<<i);
		for(int j = 0;j <= i;j++){
			if (diff&(1LL<<j)) {
				if (diff&(1LL<<j+1)) diff+=1LL<<j;
				else diff-=1LL<<j;
				sum++;
			}
		}
		ans = min(ans,sum);
		x *= 2;
	}
	cout << ans << "\n";
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e3+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
#define mst(a,b) memset(a,b,sizeof a)
#define lx x<<1
#define rx x<<1|1
#define reg register
#define PII pair<int,int>
#define fi first
#define se second
#define pb push_back
#define il inline
ll x,y,a,b,s;
int main(){
	cin>>x>>y>>a>>b;
	while(x<y/a&&x<(x+b)/a){
		x*=a;
		s++;
	}
	cout<<s+(y-1-x)/b<<'\n';
	return 0;
}

#include<bits/stdc++.h>
#include<chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
using namespace __gnu_pbds;
#define ll long long int
#define ull unsigned long long int
#define FOR(I,a,b) for(int I=a;I<b;I++)
#define FORit(it,a) for(auto it=a.begin();it!=a.end();it++)
#define ROF(I,a,b) for(int I=a;I>=b;I--)
#define vec vector
#define vi vec<int>
#define vll vec<ll>
#define pb push_back
#define pp pop_back
#define all(x) x.begin(),x.end()
#define testcases ll t;cin>>t;while(t--)
#define mem(a,k) memset(a,k,sizeof(a))
#define FF first
#define SS second
#define MP(x,y) make_pair(x,y)
#define rt return
#define br break
#define ct continue
#define elif else if
#define ii pair<int,int>
#define vecin(a,n,index) for(int I=index;I<n;I++)cin>>a[I]
#define vecout(a,n,index) for(int I=index;I<n;I++)cout<<a[I]<<"\n";cout<<endl;
//ll mod = 1000000007;
template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> using omset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

void solve() {
	ll x, y, a, b;
	cin >> x >> y >> a >> b;
	ll ans = 0;
	bool is = false;
	while (x * a <= x + b && x * a > 0) {
		x *= a;
		if (x >= y || x <= 0) {cout << ans << endl; is = true; br;}
		ans++;
	}
	if (!is) {
		ans += ((y - x - 1) / b);
		cout << ans << endl;
	}
}
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);
	// START FROM HERE :)
	solve();
}


#include <cstdio>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <utility>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <set>
#include <bitset>
#include <iterator>

#define len(v) v.size()
#define all(v) v.begin(), v.end()
#define fi first
#define se second
#define pb push_back
#define endl '\n'

using namespace std;

string toOctal(int n) {
	string ans = "";
	while(n != 0) {
		ans += to_string(n % 8);
		n /= 8;
	}
	return ans;
}

bool isLucky(int n) {
	string s = to_string(n);
	for(auto ch: s)
		if(ch == '7')
			return false;
	s = toOctal(n);
	for(auto ch: s)
		if(ch == '7')
			return false;
	return true;
}

int main() {

	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int N; cin >> N;
	int ans = 0;
	for(int i = 1;i <= N;++i)
		ans += isLucky(i);

	cout << ans << endl;

	return 0;
}

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

#define loop(i, x, y) for (i = x; i < y; i++)
#define loopr(i, x, y) for (i = x; i > y; i--)

//use *it instead of it
#define vloop(it, x) for (auto it = x.begin(); it != x.end(); it++)
#define vloopr(it, x) for (auto it = x.end() - 1; it != x.begin() - 1; it--)

#define yes printf("YES\n")
#define no printf("NO\n")
#define pb push_back
#define mp make_pair

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

typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ii, ll> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;

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

#define test(t) \
    ll t;       \
    cin >> t;   \
    while (t--)

//use double
#define precision(x)       \
    cout.setf(ios::fixed); \
    cout << setprecision(10) << x << endl;

// ll gcd(ll x_f, ll y_f)
// {
//     if (x_f == 0)
//         return y_f;
//     return gcd(y_f % x_f, x_f);
// }

// ll power(ll x_f, ll y_f, ll m_f)
// {
//     ll res_f = 1;

//     x_f = x_f% m_f;
//     while (y_f)
//     {
//         if (y_f & 1)
//             res_f = (res_f * x_f) % m_f;
//         y_f = y_f >> 1;
//         x_f = (x_f * x_f) % m_f;
//     }
//     return res_f;
// }

// ll less(ll x_f, ll y_f)
// {
//     return (x_f > y_f) ? y_f : x_f;
// }
// ll more(ll x_f, ll y_f)
// {
//     return (x_f < y_f) ? y_f : x_f;
// }

ll fac(ll x_f)
{
    ll fac_f = 1;
    for (ll i = 1; i <= x_f; i++)
    {
        fac_f *= i;
        fac_f = fac_f % MOD;
    }
    return fac_f;
}

bool composite[1000007] = {false};
void seive(ll x)
{
    composite[0] = true;
    composite[1] = true;
    for (ll i = 2; i <= x; i++)
    {
        if (!composite[i])
        {
            for (ll j = 2 * i; j <= x; j += i)
            {
                composite[j] = true;
            }
        }
    }
}

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

    ll n, ans = 0;
    cin >> n;

    for (ll i = 1; i <= n; i++)
    {
        ll num1 = i, num2 = i, flag = 0;
        //    debug(num);
        while (num1 > 0 && num2 > 0)
        {
            if (num1 % 10 == 7 || num2 % 8 == 7)
            {
                flag = 1;
                break;
            }
            num1 = num1 / 10;
            num2 = num2 / 8;
        }
        if (flag == 1)
            continue;
        //cout << i << endl;
        ans++;
    }
    cout << ans << endl;
    return 0;
}


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

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

signed main() {
    vi a(4); for(int& i : a) cin >> i;
    int bo = 0, sum = a[0] + a[1] + a[2] + a[3];
    function<void(int, int)> dfs = [&](int x, int nw) {
        if(x == 4) return bo |= (2 * nw == sum), void();
        dfs(x + 1, nw), dfs(x + 1, nw + a[x]);
    }; dfs(0, 0);
    puts(bo ? "Yes" : "No");
    fprintf(stderr, "time=%.4f\n", (db)clock()/CLOCKS_PER_SEC);
    return 0;
}

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

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>

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

using namespace std;

class CCalculator {
public:
    map<int, int> memo;

    int fib(int n) {
        if (n == 1 || n == 2) return 1;
        if (memo.count(n)) return memo[n];
        return memo[n] = fib(n - 1) + fib(n - 2);
    }

    void solve(std::istream& cin, std::ostream& cout) {
        int N;
        cin >> N;
        set<int> s;
        REV(i, 90, 0) {
            int val = fib(i + 1);
            if (N >= val) {
                N -= val;
                s.insert(i + 1);
            }
        }

        int num = *s.rbegin();
        if (num % 2 == 0) num++;

        vector<int> res;
        FOR(i, 1, num) {
            if (i % 2 == 1) {
                res.push_back(3);
            } else {
                res.push_back(4);
            }
            if (s.count(num - i + 1)) {
                if (i % 2 == 1) {
                    res.push_back(1);
                } else {
                    res.push_back(2);
                }
            }
        }
        cout << res.size() << endl;
        REP(i, res.size()) cout << res[i] << endl;
    }
};

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

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


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

#define int long long
#define all(x) (x).begin(),(x).end
#define rall(x) (x).rbegin(),(x).rend

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

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    //freopen("teamwork.in","r",stdin);
    //freopen("teamwork.out","w",stdout);
    int n;
    cin>>n;
    int a[n+1],pref[n+1]={};
    for(int i=1;i<=n;i++){
        cin>>a[i];
        pref[i]=a[i]+pref[i-1];
    }
    int sum1=0,sum2=0;
    for(int i=1;i<=n;i++) sum1+=(a[i]*a[i]);
    sum1*=n-1;
    for(int i=1;i<=n;i++){
        sum2+=a[i]*(pref[n]-pref[i]);
    }
    sum2*=(-2);
    cout<<sum1+sum2;
}

#pragma GCC target("avx2")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
// include
#include <bits/stdc++.h>

using namespace std;

// conversion
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
// change
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
// math
template <class T>
inline T sqr(T x) {
  return x * x;
}

template <class T>
inline T nCr(T n, T r) {
  T num = 1;
  for (int i = 1; i <= r; ++i) {
    num *= (n - i + 1) / i;
  }
  return num;
}
template <class T>
inline T nPr(T n, T r) {
  r = n - r;
  T sum = 1;
  int i;
  for (i = n; i >= r + 1; --i) sum *= i;
  return sum;
}

template <class T>
inline T facctorial(T k) {
  T sum = 1;
  for (int i = 1; i <= k; ++i) {
    sum *= i;
  }
  return sum;
}
// numeric
template <class T>
inline T gcd(T a, T b) {
  if (a < b) {
    a ^= b;
    b ^= a;
    a ^= b;
  }

  return b ? gcd(b, a % b) : a;
}

template <class T>
inline T lcm(T a, T b) {
  return a * b / gcd(a, b);
}
// using
using VI = vector<int>;
using VVI = vector<VI>;
using VS = vector<string>;
using PII = pair<int, int>;
using LL = int64_t;

// container util
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) static_cast<int>((a).size())
#define EACH(i, c) \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())

// repetition
#define FOR(i, a, b) for (int i = (a); i < static_cast<int>(b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
constexpr double EPS = 1e-10;
const double PI = acos(-1.0);
constexpr LL INF = 1e10;

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                         \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
       << " " << __FILE__ << endl;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int T;
  cin >> T;
  vector<PII> LR(T);
  for (auto &&[L, R] : LR) {
    cin >> L >> R;
  }
  for (auto &&[L, R] : LR) {
    const LL n = R - 2 * L + 1;
    if (n < 0) {
      cout << 0 << "\n";
      continue;
    }
    cout << n * (n + 1) / 2 << "\n";
  }
  return 0;
}


//デバッグ用オプション：-fsanitize=undefined,address

//コンパイラ最適化
#pragma GCC optimize("Ofast")

//インクルードなど
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using vint = vector<int>;
using vll = vector<ll>;
using vbool = vector<bool>;
template <class T>
using arr = vector<vector<T>>;

//マクロ
//forループ
//引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
//Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
//FORAは範囲for文(使いにくかったら消す)
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i < ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= ll(b); i--)
#define FORA(i, I) for (const auto &i : I)
//xにはvectorなどのコンテナ
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())
//定数
#define INF 1000000000000 //10^12:∞
#define MOD 1000000007    //10^9+7:合同式の法
#define MAXR 100000       //10^5:配列の最大のrange

// aよりもbが大きいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T>
bool chmax(T &a, const T &b)
{
  if (a < b)
  {
    a = b; // aをbで更新
    return true;
  }
  return false;
}
// aよりもbが小さいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T>
bool chmin(T &a, const T &b)
{
  if (a > b)
  {
    a = b; // aをbで更新
    return true;
  }
  return false;
}

template <typename... Args>
std::string to_str_by(const std::string &fmt, Args... args)
{
  size_t len = std::snprintf(nullptr, 0, fmt.c_str(), args...);
  std::vector<char> buf(len + 1);
  std::snprintf(&buf[0], len + 1, fmt.c_str(), args...);
  return std::string(&buf[0], &buf[0] + len);
}

template <class T>
int popcount(T &a)
{
  int c = 0;
  REP(i, 8 * (int)sizeof(a))
  {
    if ((a >> i) & 1)
      c++;
  }
  return c;
}

template <class T>
void pl(T x) { cout << x << " "; }

template <class T>
void pr(T x) { cout << x << endl; }

template <class T>
void prvec(vector<T> &a)
{
  REP(i, a.size() - 1)
  {
    cout << a[i] << " ";
  }
  pr(a[a.size() - 1]);
}

template <class T>
void prarr(arr<T> &a)
{
  REP(i, a.size())
  if (a[i].empty())
    pr("");
  else
    prvec(a[i]);
}

using P = pair<ll, ll>;
void prp(P &p) { cout << p.first << " " << p.second << endl; }

/**
 * 何も書かれていない黒板があります。 高橋くんは N 回の操作を行い、黒板に整数を書きます
 * i 回目の操作では、 Ai 以上 Bi 以下の整数すべてを 1 個ずつ、合計 Bi−Ai+1 個の整数を書きます。
 * N回の操作を終えたときの、黒板に書かれた整数の合計を求めてください。
 */
struct Args
{
  ll N;
  vll A, B;
  Args()
  {
    cin >> N;
    A.resize(N);
    B.resize(N);
    REP(i, N)
    {
      cin >> A.at(i) >> B.at(i);
    }
  }
};

class Solver
{
private:
  /* data */
  Args &args;
  ll ans;

public:
  Solver(Args &args) : args(args), ans(0)
  {
  }
  void solve()
  {
    ans = 0;
    REP(i, args.N)
    {
      ll A = args.A.at(i);
      ll B = args.B.at(i);
      ll sum = (B - A + 1) * (A + B) / 2;
      ans += sum;
    }
  }
  void output()
  {
    pr(ans);
  }
};

int main()
{
  //入力の高速化用のコード
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  Args args;
  Solver s(args);
  s.solve();
  s.output();
}

#include <iostream>
using namespace std;
using ll = int64_t;
int main(){
    ll n;
    cin >> n;
    ll ans = 0;
    for(ll i = 0; i < n; i++){
        ll a, b;
        cin >> a >> b;
        ans += b * (b + 1) / 2 - a * (a - 1) / 2;
    }
    cout << ans << endl;
}


// Artur Kraska, II UWr

#include <bits/stdc++.h>

#define forr(i, n)                  for(int i=0; i<n; i++)
#define FOREACH(iter, coll)         for(auto iter = coll.begin(); iter != coll.end(); ++iter)
#define FOREACHR(iter, coll)        for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter)
#define lbound(P,R,PRED)            ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;})
#define testy()                     int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests)
#define CLEAR(tab)                  memset(tab, 0, sizeof(tab))
#define CONTAIN(el, coll)           (coll.find(el) != coll.end())
#define FOR(i, a, b)                for(int i=a; i<=b; i++)
#define FORD(i, a, b)               for(int i=a; i>=b; i--)
#define MP                          make_pair
#define PB                          push_back
#define ff                          first
#define ss                          second
#define deb(X)                      X;

#define M 998244353
#define INF 1000000007LL

using namespace std;

int n, m, a;
long long tr[200057][22];
long long sito[200057];

int solve()
{
    scanf("%d %d", &n, &m);

    tr[0][0] = 1;
    FOR(i, 1, n+30)
    {
        tr[i][0] = 1;
        FOR(j, 1, 20)
        {
            tr[i][j] = (tr[i-1][j-1] + tr[i-1][j]) % M;
        }
    }

    FOR(i, 2, m)
    {
        if(sito[i] == 0)
        {
            sito[i] = i;
            for(long long j = i*(long long)i; j <= m; j+=i)
                if(sito[j] == 0)
                    sito[j] = i;
        }
    }

    long long res = 1;
    FOR(i, 2, m)
    {
        long long r = 1;
        int x = i;
        map <int, int> mapa;
        while(x != 1)
        {
            mapa[sito[x]]++;
            x  = x/sito[x];
        }
        for(auto p : mapa)
        {
            r = (r*tr[n+p.ss-1][p.ss])%M;
        }
        //cout << i << " daje " << r << endl;
        res = (res + r)%M;
    }
    printf("%lld\n", res);

    return 0;
}

int main()
{
    solve();

    return 0;
}

#include<bits/stdc++.h>
#define ll long long int
#define pll pair<ll,ll>
#define vpll vector< pll >
#define mpll map<ll,ll>
#define MOD 998244353
#define all(v) v.begin(),v.end()
#define s(v) v.size()
#define test ll t;cin>>t;while(t--)
#define vec vector<ll>
#define read0(v,n) for(int i=0;i<n;i++)cin>>v[i];
#define read1(v,n) for(int i=1;i<=n;i++)cin>>v[i];
#define trav(a,x) for (auto& a: x)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define cut(x) {cout<<x;return 0;}
#define print(x) {cout<<x<<nl;continue;}
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORB(i,a,b) for(int i=a;i>=b;i--)
#define err1(a) {cout<<#a<<' '<<a<<nl;}
#define err2(a,b) {cout<<#a<<' '<<a<<' '<<#b<<' '<<b<<nl;}

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define f first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define nl '\n'
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;
#define oset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
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 bpow(ll a, ll b)
{
	ll ans=1;
	while(b)
	{
		if(b&1)
			ans=(ans*a)%MOD;
		b/=2;
		a=(a*a)%MOD;
	}
	return ans;
}
mpll factorise(ll n)
{
    mpll fact;
    while (n % 2 == 0)
    {
        fact[2]++;
        n = n/2;
    }

    // n must be odd at this point. So we can skip
    // one element (Note i = i +2)
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        // While i divides n, print i and divide n
        while (n % i == 0)
        {
           fact[i]++;
            n = n/i;
        }
    }

    // This condition is to handle the case when n
    // is a prime number greater than 2
    if (n > 2)
        fact[n]++;
    return fact;
}
const int N=1e6;
ll fac[N];
void precompute()
{
    fac[0]=1;
    FOR(i,1,(ll)5e5)fac[i]=(i*fac[i-1])%MOD;
}
ll ncr(ll n,ll r)
{
    return (((fac[n]*bpow(fac[r],MOD-2))%MOD)*(bpow(fac[n-r],MOD-2)))%MOD;
}

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

	fast
	ll n,m;
	cin>>n>>m;
	precompute();
	ll sum=0;
	FOR(i,1,m)
	{
	    mpll temp=factorise(i);
	    ll x=1;
	    trav(it,temp)
	    {
	        x=(x*ncr(n+it.sc-1,n-1))%MOD;
	    }
	    sum=(sum+x)%MOD;
	}
	cut(sum)

}


#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<set>
//const int MOD = int(1e9) + 7;
using namespace std;
int main(){
    long long int s,p;
    cin >> s >> p;
    int flag = 0;
    for(int i =1; i< 10000000;i++){
        long long int t = pow(i,2);
        t = t - (s * i);
        if(t + p == 0){
            if(s - i > 0){
                cout << "Yes" << endl;
                flag++;
                break;
            }
        }
    }
    if(flag == 0){
        cout << "No" << endl;
    }

}

#include<bits/stdc++.h>
using namespace std;
long const mod = 1e9+7;
int main(){
    long s,p;
    cin >> s >> p;
    for(long i = 1;i<=sqrt(p);i++){
        if(p % i != 0)continue;
        if(i + p/i == s){
            cout << "Yes";
            return 0;
        }
    }
    cout << "No";
}


#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
using namespace std;

template <typename T>
inline void read(T&x){
    x=0; char temp=getchar(); bool f=false;
    while(!isdigit(temp)){if(temp=='-') f=true; temp=getchar();}
    while(isdigit(temp)){x=(x<<1)+(x<<3)+temp-'0'; temp=getchar();}
    if(f) x=-x;
}
template <typename T>
void print(T x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) print(x/10);
    putchar(x%10+'0');
}

typedef long long ll;
const int MAXN = 105;
const ll mod = 998244353;

inline ll Add(ll x,ll y){return x+y>=mod? x+y-mod:x+y;}

//basic
int n,w[MAXN],pre[MAXN],sum,aim;
ll fac[MAXN];

//DP
ll f[MAXN][MAXN][MAXN*MAXN];

inline ll DP(){
    f[0][0][0]=1;
    for(register int i=1;i<=n;i++)
        for(register int j=0;j<=i;j++)
            for(register int k=0;k<=pre[i];k++){
                int x=k,y=pre[i]-k,now=w[i];
                if(j>0&&x-now>=0) f[i][j][x]=Add(f[i][j][x],f[i-1][j-1][x-now]);
                if(y-now>=0) f[i][j][x]=Add(f[i][j][x],f[i-1][j][x]);
            }
    ll res=0;
    for(register int i=0;i<=n;i++) res=Add(res,fac[i]*fac[n-i]%mod*f[n][i][aim]%mod);
    return res;
}

int main(){
    read(n),fac[0]=1;
    for(register int i=1;i<=n;i++) read(w[i]),sum+=w[i],pre[i]=pre[i-1]+w[i],fac[i]=fac[i-1]*i%mod;
    if(sum&1) return puts("0"),0;
    aim=sum>>1;
    print(DP());
    return 0;
}

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;

ll a, b, k;
const int n = 35;
vector<vector<ll>> dp(n + 2, vector<ll>(n + 2));

string findkth(int i, int j, ll x) {
	if (i == 0) return string(j, 'b');
	if (j == 0) return string(i, 'a');
	if (x <= dp[i - 1][j]) return "a" + findkth(i - 1, j, x);
	return "b" + findkth(i, j - 1, x - dp[i - 1][j]);
}

int main() {

	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> a >> b >> k;

	dp[0][0] = 1;

	for (int i = 0; i <= n; i++) {
		for (int j = 0; j <= n; j++) {
			if (i > 0) dp[i][j] += dp[i - 1][j];
			if (j > 0) dp[i][j] += dp[i][j - 1];
		}
	}
	cout << findkth(a, b, k) << endl;

	return 0;
}


//#include<math.h>
#include<algorithm>
#include<stdlib.h>
#include<time.h>
#include<stdio.h>
#include<string.h>
#define un unsigned
#define srd srand(time(0))
#define ll long long
#define con continue
#define gtc getchar()
#define ptc putchar
#define dou double
#define eps 0.00000000001
#define opr operator
#define cl(x,a) memset(x,a,sizeof(x))
#define fo0(i,k) for(i=fr[k];i;i=nx[i])
#define fo1(i,l,r) for(i=l;i<=r;i++)
#define fo2(i,l,r) for(i=l;i>=r;i--)
#define fo(i,n) for(i=1;i<=n;i++)
#define ret return
#define x first
#define cint const int
#define y second
#define opi(x) freopen(x,"r",stdin)
#define opo(x) freopen(x,"w",stdout)
#define tpl template<class T>
#define priq priority_queue
#define mp make_pair
#define use using namespace
#define WT while(T--)
#define pb push_back
#define sz size()
use std;
typedef pair<int,int> pii;typedef pair<int,ll> pil;typedef pair<ll,int> pli;typedef pair<ll,ll> pll;
namespace io
{
	void _(int &k){char c;int e=1;k=0;while((c=gtc)>'9'||c<'0')if(c=='-')e=-1;k=c-'0';while((c=gtc)<='9'&&c>='0'){k*=10;k+=c-'0';}k*=e;}
	void _(ll &k){char c;int e=1;k=0;while((c=gtc)>'9'||c<'0')if(c=='-')e=-1;k=c-'0';while((c=gtc)<='9'&&c>='0'){k*=10;k+=c-'0';}k*=e;}
	void _(char &c){while((c=gtc)==' '||c=='\n');}void _(dou &c){scanf("%lf",&c);}void _(char *s){char c;while((c=gtc)!=EOF&&c!=' '&&c!=10)*s++=c;}
	template<class t1,class t2>void _(t1 &a,t2 &b){_(a);_(b);}template<class t1,class t2,class t3>void _(t1 &a,t2 &b,t3 &c){_(a);_(b);_(c);}
	template<class t1,class t2,class t3,class t4>void _(t1 &a,t2 &b,t3 &c,t4 &d){_(a);_(b);_(c);_(d);}
	template<class t1,class t2,class t3,class t4,class t5>void _(t1 &a,t2 &b,t3 &c,t4 &d,t5 &e){_(a);_(b);_(c);_(d);_(e);}
	void _p(dou k){printf("%.6lf",k);}void _p(char *c){for(;*c;ptc(*c++));}void _p(const char *c){for(;*c;ptc(*c++));}void _p(char c){ptc(c);}
	tpl void _p0(T k){if(k>=10)_p0(k/10);ptc(k%10+'0');}tpl void _p(T k){if(k<0){ptc('-');_p0(-k);}else _p0(k);}tpl void __p(T k){_p(k);ptc(' ');}
	tpl void _pn(T k){_p(k);ptc('\n');}template<class t1,class t2>void _p(t1 a,t2 b){__p(a);_pn(b);}
	template<class t1,class t2,class t3>void _p(t1 a,t2 b,t3 c){__p(a);__p(b);_pn(c);}
	template<class t1,class t2,class t3,class t4>void _p(t1 a,t2 b,t3 c,t4 d){__p(a);__p(b);__p(c);_pn(d);}
	tpl void op(T *a,int n){int i;n--;fo(i,n)__p(a[i]);_pn(a[n+1]);}int gi(){int x;_(x);ret x;}ll gll(){ll x;_(x);ret x;}
}
int gcd(int a,int b){ret b?gcd(b,a%b):a;}void fcl(){fclose(stdin);fclose(stdout);}
void fop(const char *s){char c[256],d[256];cl(c,0);cl(d,0);strcpy(c,s);strcpy(d,s);opi(strcat(c,".in"));opo(strcat(d,".out"));}
int eq(dou a,dou b){return a+eps>=b&&b+eps>=a;}tpl void _ma(T &a,T b){if(a<b)a=b;}tpl void _mi(T &a,T b){if(a>b)a=b;}
cint N=1234567,EE=100000000,GG=1000000000,ima=2147483647;
use io;
int n,m,a[N],f[N],T;
int main()
{
	int i,j,a1,a2;
	_(n,m);
	if(n+m>=15&&m>=8)
		_pn(1);
	else if(n+m>=10&&m>=3)
		_pn(2);
	else
		_pn(3+((n+m)<3));
}

#include<bits/stdc++.h>
#define fore(i,n) for(int i=0;i<(n);i++)
#define foree(i,a,n) for(int i=(a);i<=(n);i++)
#define mp make_pair
#define pb push_back
#define endl "\n"
#define pii pair<int,int>
#define pll pair<long long,long long>
#define ll long long
#define inp(n) int n;cin>>n;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define YES cout<<"YES\n";
#define NO cout<<"NO\n";
#define OK cout<<"OK\n";
#define si size()
#define emp empty()
#define fi first
#define se second
#define test int T; cin>>T; while(T--)
#define all(x) x.begin(),x.end()
ll mod=1e9+7;
using namespace std;
int checkprime(int N)
{
    int count = 0;
    for( int i = 1;i * i <=N;++i )
    {
        if( N % i == 0)
        {
            if( i * i == N )
                count++;
            else
                count += 2;
        }
    }
    if(count == 2)
        return 1;
    else
        return 0;

}
//---------------------------------------------
int main()
{
    fastio;
    int r,c,x,y;
    cin>>r>>c>>y>>x;
    char a[r][c];
    fore(i,r)
    {
        fore(j,c)
            cin>>a[i][j];
    }
    x--;
    y--;
    int ans=0;
    for(int i=y;i<r;i++)
    {
        if(a[i][x]=='.')
            ans++;
        else
            break;
    }
    for(int i=y-1;i>=0;i--)
    {
        if(a[i][x]=='.')
            ans++;
        else
            break;
    }
    for(int i=x+1;i<c;i++)
    {
        if(a[y][i]=='.')
            ans++;
        else
            break;
    }
    for(int i=x-1;i>=0;i--)
    {
        if(a[y][i]=='.')
            ans++;
        else
            break;
    }
    cout<<ans;
}


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

int main()
{
    ll n, a, b, i, sum = 0;
    cin>>n;
    vector<ll> arr(n);
    for(i=0;i<n;i+=1)
    {
        cin>>a>>b;
        arr[i] = 2*a+b;
        sum+=a;
    }
    sum = -sum;
    sort(arr.begin(), arr.end(), greater<ll>());
    i = 0;
    while(sum<=0)
    {
        sum+=arr[i];
        i+=1;
    }
    cout<<i<<endl;
}

#include<bits/stdc++.h>
using namespace std;
using ll=int64_t;
ll c,x;vector<ll>v;
int main(){
    ll n;cin>>n;vector<vector<ll>>s(n,vector<ll>(2));
    for(int i=0;i<n;i++){
        for(int j=0;j<2;j++)cin>>s[i][j];
        x+=s[i][0];
        v.push_back(2*s[i][0]+s[i][1]);
    }
    sort(v.begin(),v.end());reverse(v.begin(),v.end());
    for(int i=0;i<n;i++){if(x>=0){c++;x-=v[i];}else break;}
    cout<<c<<endl;
}


/*
2021-04-15 03:44:57.635786
Category: cumsum
Rating: x
*/
/*
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
*/
#include<bits/stdc++.h>
using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define int long long
#define ll long long
#define ld long double
#define gap ' '
#define endl '\n'

void solve(int testcase) {
	int n; cin>>n;
	vector<int> v(n+1),p(n+1,0);
	for(int i=1;i<=n;i++) cin>>v[i];
	sort(v.begin()+1,v.end(),greater<int>());
	for(int i=1;i<=n;i++) p[i]=p[i-1]+v[i];
	int sum=0;
	for(int i=1;i<=n-1;i++) {
		sum+=(n-i)*v[i]-(p[n]-p[i]);
	}
	cout<<sum<<endl;
}

int32_t main() {
	fio
	int T=1;
	//cin>>T;
	for(int t=1;t<=T;t++) solve(t);
	return 0;
}

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

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

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

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

const int MAX = 200005;
const int INF = 1001001001;
const int MOD = 1000000007;

int main(){
    int x[4];
    rep(i, 4) cin >> x[i];

    int S = 0;
    rep(i, 4) S += x[i];

    rep(i, 1<<4) {
        int t = 0;
        rep(j,4) {
            if (i>>j&1) t += x[j];
        }
        if (t == S-t) {
            puts("Yes");
            return 0;
        }
    }

    puts("No");
}


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

ll X, Y;
unordered_map<ll, ll> mp;

ll solve(ll y) {
    if(mp.find(y) != mp.end()) return mp[y];
    if (y == 1) return mp[y] = abs(X-y);
    else if (y % 2 == 1) return mp[y] = min(min(solve(y+1)+1, solve(y-1)+1), abs(X-y));
    else return mp[y] = min(abs(X-y), solve(y/2)+1);
}

int main() {
    cin >> X >> Y;
    solve(Y);
    cout << mp[Y] << endl;

    return 0;
}

#include<bits/stdc++.h>
#define faster  ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long
#define ull unsigned long long
#define pb push_back
const double PI = acos(-1.0);
using namespace std;

int main()
{
    faster

    ll n,m;
    cin>>n>>m;

    if(m==0)
    {
        cout<<1<<endl;
        return 0;
    }

    ll a[m];
    for(int i=0; i<m; i++)
        cin>>a[i];

    sort(a,a+m);

    vector<int> v;

    ll mn;

    if(a[0]!=1)
    {
        v.pb(a[0]-1);
    }

    for(int i=1; i<m; i++)
    {
        if(a[i]-(a[i-1]+1)!=0)
            v.pb(a[i]-(a[i-1]+1));
    }

    if(a[m-1]!=n)
    {
        v.pb(n-a[m-1]);
    }

    //cout<<v.size()<<endl;
    if(v.size()==0)
    {
        cout<<0<<endl;
        return 0;
    }

    sort(v.begin(),v.end());

    mn=v[0];

    ll ans=0;

    for(int i=0; i<v.size(); i++)
    {
        //cout<<v[i]<<" ";
        ans=ans+(v[i]/mn);

        if(v[i]%mn!=0)
            ans++;
    }

    cout<<ans<<endl;

    return 0;
}


#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <string.h>
using namespace std;

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

    for (int i = 0; i < k; i++)
    {

        if (n % 200 == 0)
            n = n / 200;
        else
        {
            string str = to_string(n);
            str = str + "200";
            n = stoll(str.c_str());
        }
    }

    cout << n;

    return 0;
}

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

int main(){
  double a, b;
  cin >> a >> b;

  cout << a * b / 100 << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const ll mod=1e9+7;
#define rep(i,n) for (int i = 0; i < (n); i++)
ll b,c,ans;
int main(){
    cin >> b >> c;
    ll k=c/2;
    if(c==1){
        if(b==0)ans=1;
        else ans=2;
    }
    else if(c%2==1) ans=(k*4+1)-max(0LL,min(b+k-1,k-b)-max(-b-k,b-k)+1);
    else ans=(k*4-1)-max(0LL,min(b+k-1,k-b-1)-max(-b-k+1,b-k)+1);
    cout << ans << endl;
    return 0;

}

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

int main() {

  int64_t B,C;
  cin >> B >> C;

  int64_t ans = 0;

  if(B == 0){
    cout << C << endl;
    return 0;
  }

  if(C < 3){
    cout << C+1 << endl;
    return 0;
  }

  if(abs(B) >= C){
    cout << (C-2)*2+3 << endl;
    return 0;
  }else{
    if(C <= abs(B)*2){
      cout << (C-2)*2+3 << endl;

      return 0;
    }

    if(B <= 0){
      cout << C + -B*2 << endl;
    }else{
      cout << C + B*2 - 1 << endl;
    }

  }

}


#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using vll = vector<LL>;

const vector<vll> CW = {{0, 1, 0}, {-1, 0, 0}, {0, 0, 1}};
const vector<vll> CCW = {{0, -1, 0}, {1, 0, 0}, {0, 0, 1}};

vector<vll> multiply(vector<vll>& A, vector<vll>& B){
    int m = A.size(), n = B.size(), p = B[0].size();
    vector<vll> answer(m, vll(p, 0));

    for (int i = 0; i < m; i++){
        for (int j = 0; j < p; ++j){
            for (int k = 0; k < n; ++k){
                answer[i][j] += A[i][k] * B[k][j];
            }
        }
    }
    return answer;
}

void print_matrix(vector<vll>& A){
    for (int i = 0; i < A.size(); ++i){
        for (int j = 0; j < A[i].size(); ++j){
            cout << A[i][j] << (j != A[i].size() - 1 ? ' ' : '\n');
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    int n; cin >> n;
    vector<vector<vll>> pieces(n);
    for (int i = 0; i < n; ++i){
        int x, y; cin >> x >> y;
        pieces[i] = {{x},{y},{1}};
    }

    int m; cin >> m;
    vector<vector<vll>> ops(m + 1);
    ops[0] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
    for (int i = 1; i <= m; ++i){
        int op; cin >> op;
        if (op == 1){
            ops[i] = CW;
        }
        else if (op == 2){
            ops[i] = CCW;
        }
        else if (op == 3){
            int p; cin >> p;
            ops[i] = {{-1 , 0, 2 * p}, {0, 1, 0}, {0, 0, 1}};
        }
        else{
            int p; cin >> p;
            ops[i] = {{1, 0, 0}, {0, -1, 2 * p}, {0, 0, 1}};
        }
    }
    for (int i = 1; i <= m; ++i){
        ops[i] = multiply(ops[i], ops[i - 1]);
    }

    int q; cin >> q;
    for (int i = 0; i < q; ++i){
        int a, b; cin >> a >> b;
        b--;
        vector<vll> res = multiply(ops[a], pieces[b]);
        cout << res[0][0] << ' ' << res[1][0] << '\n';
    }
    return 0;
}


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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
using ll = long long;

int main() {
    double r, x, y;
    cin >> r >> x >> y;

    double w = sqrt(x * x + y * y);
    int ans = ceil(w / r);
    if (ans == 1 && w != r) ans++;
    cout << ans << endl;
}

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

char f(char a, char b) {
  if (a == b) return a;
  if (a == 'R' && b == 'S') return a;
  if (a == 'S' && b == 'R') return b;
  if (a == 'P' && b == 'R') return a;
  if (a == 'R' && b == 'P') return b;
  if (a == 'S' && b == 'P') return a;
  return b;
}
string yo(string s, int k) {
  int n = s.size();
  if (k <= 7 && (1 << k) <= n) {
    s.resize(1 << k);
    n = 1 << k;
  }
  if (k == 0) return s;
  string p = "";
  for (int i = 0; i < n; i += 2) {
    p += f(s[i], s[i + 1]);
  }
  p += p;
  return yo(p, k - 1);
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k; cin >> n >> k;
  string s; cin >> s;
  s += s;
  cout << yo(s, k) << '\n';
  return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < (t); ++i)
#define dsrep(i,t,s) for(int i = (t)-1; i >= (s); --i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cerr<<#x<<" = "<<x<<endl;
#define bn(x) ((1<<(x))-1)
#define newline puts("")
using namespace std;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
template<typename T> using PQ = priority_queue<T,vc<T>,greater<T> >;
using ll = long long;
using uint = unsigned;
using ull = unsigned long long;
using P = pair<int,int>;
using T3 = tuple<int,int,int>;
using vi = vc<int>;
using vvi = vv<int>;
using vl = vc<ll>;
using vp = vc<P>;
using vt = vc<T3>;
int getInt(){int x;scanf("%d",&x);return x;}
template<typename T>istream& operator>>(istream&i,vc<T>&v){rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const vc<T>&v,const string& d=""){stringstream s;rep(i,sz(v))(i?s<<d:s)<<v[i];return s.str();}
template<typename T>ostream& operator<<(ostream&o,const vc<T>&v){if(sz(v))o<<join(v," ");return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.fi>>v.se;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.fi<<","<<v.se;}
vc<string> split(const string& s,char d=' '){vc<string> r(1);for(char c:s)if(c==d)r.pb("");else r.back()+=c;return r;}
string operator*(const string& s,int t){return join(vc<string>(t,s));}
template<typename T>bool mins(T& x,const T&y){if(x>y){x=y;return true;}else return false;}
template<typename T>bool maxs(T& x,const T&y){if(x<y){x=y;return true;}else return false;}
template<typename T>T dup(T x, T y){return (x+y-1)/y;}
template<typename T>ll suma(const vc<T>&a){ll res(0);for(auto&&x:a)res+=x;return res;}
template<typename T>void uni(vc<T>& a){sort(rng(a));a.erase(unique(rng(a)),a.end());}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return;}
#define yn {puts("Yes");}else{puts("No");}
const int MX = 200005;

struct Solver {
  int n;
  vvi to;
  int c;
  int cnt;
  int dfs(int v, int p=-1) {
    int y = 0;
    int w = -INF;
    for (int u : to[v]) {
      if (u == p) continue;
      int r = dfs(u,v);
      if (r < 0) maxs(w, -r);
      else maxs(y, r);
    }
    // cerr<<y<<" "<<w<<endl;
    if (y <= w-1) return -(w-1);
    if (y == c) {
      ++cnt;
      return -c;
    }
    return y+1;
  }
  int f() {
    cnt = 0;
    int r = dfs(0);
    // cerr<<r<<endl;
    if (r > 0) ++cnt;
    return cnt;
  }
  void solve() {
    int k;
    scanf("%d%d",&n,&k);
    to = vvi(n);
    rep(i,n-1) {
      int a, b;
      scanf("%d%d",&a,&b);
      --a; --b;
      to[a].pb(b);
      to[b].pb(a);
    }
    // c = 1; cerr<<f()<<endl; return;
    int l = 0, r = n;
    while (l+1<r) {
      c = (l+r)/2;
      if (f() <= k) r = c; else l = c;
    }
    cout<<r<<endl;
  }
};

int main() {
  int ts = 1;
  // scanf("%d",&ts);
  rrep(ti,ts) {
    Solver solver;
    solver.solve();
  }
  return 0;
}

#include<cstdio>
#include<iostream>

const int MAXN = 3e5 + 10, INF = 0x3f3f3f3f;
int f1[MAXN], f2[MAXN], is_key[MAXN], h[MAXN] = {0};
int N, M, tot, cnt;
struct E{
	int t, nxt;
}Ed[2 * MAXN];

void Add(int f, int t){Ed[++tot].t = t, Ed[tot].nxt = h[f]; h[f] = tot;}

void dfs(int u, int fa, int mid)
{
	f1[u] = -INF, f2[u] = INF;
	for (int i = h[u]; i; i = Ed[i].nxt)
	{
		int v = Ed[i].t; if (v == fa) continue;
		dfs(v, u, mid);
		f1[u] = std::max(f1[u], f1[v] + 1);
		f2[u] = std::min(f2[u], f2[v] + 1);
	}
	if (is_key[u] && f2[u] > mid) f1[u] = std::max(0, f1[u]);
	if (f1[u] + f2[u] <= mid) f1[u] = -INF;
	if (f1[u] == mid) f1[u] = -INF, f2[u] = 0, cnt++;
}

int check(int mid)
{
	cnt = 0;
	dfs(1, 0, mid);
	if (f1[1] >= 0) cnt++;
	return cnt <= M;
}

int rd()
{
	int ret = 0; char ch = getchar();
	while (!isdigit(ch)) ch = getchar();
	for (; isdigit(ch); ch = getchar()) ret = ret * 10 + (int)ch - 48;
	return ret;
}

int main()
{
	N = rd(), M = rd();
	for (int i = 1; i <= N; i++) is_key[i] =1;
	for (int i = 1; i < N; i++) {int f = rd(), t = rd(); Add(f, t); Add(t, f);}
	int l = 0, r = N, ans = N;
	while (l <= r)
	{
		int mid = (l + r) >> 1;
		if (check(mid)) ans = mid, r = mid - 1;
		else l = mid + 1;
	}
	dfs(1, 0, 1);
	printf("%d\n", ans);
	return 0;
}


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

signed main() {
    int N;
    cin >> N;
    //小さい N こと大きい N 個を組み合わせて作る
    vector<int> A(2*N);
    vector<P> B(2*N);
    rep(i,0,2*N-1){
        cin >> A[i];
        B[i].first=A[i];
        B[i].second=i;
    }
    sort(B.begin(),B.end());
    vector<int> small(2*N,0);
    rep(i,0,N-1){
        small[B[i].second]=1;
    }
    stack<int> smalls,larges;
    vector<char> ans(2*N);
    rep(i,0,2*N-1){
        if(small[i]==1){
            smalls.push(i);
        }
        else{
            larges.push(i);
        }
        if(smalls.size()&&larges.size()){
            int F=smalls.top();smalls.pop();
            int G=larges.top();larges.pop();
            ans[min(F,G)]='(';
            ans[max(F,G)]=')';
        }
    }
    rep(i,0,2*N-1){
        cout << ans[i];
    }
    cout << endl;
}

#include<algorithm>
#include<cstdio>
#define ll long long
#define MaxN 400500
using namespace std;
int n;
ll a[MaxN],x[MaxN];
int main()
{
  scanf("%d",&n);
  for (int i=1;i<=n+n;i++){
    scanf("%lld",&a[i]);
    x[i]=a[i]=a[i]*n+i;
  }
  sort(x+1,x+n+n+1);
  for (int i=1,cnt;i<=n+n;i++){
    if (a[i]<=x[n]){
      putchar(cnt<0 ? ')' : '(');
      cnt++;
    }else {
      putchar(cnt>0 ? ')' : '(');
      cnt--;
    }
  }return 0;
}


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  string x; cin >> x;
  int comma = find(ALL(x), '.') - x.begin();
  cout << x.substr(0, comma) << '\n';
  return 0;
}

#include <iostream>

using namespace std;

int main() {
    string s;
    getline(cin, s);
    int i = s.find('.');
    string x = s.substr(0, i);
    cout<<x;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using matrix = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++)
#define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--)
#define all(v) v.begin(), v.end()
template<class in_chmax> void chmax(in_chmax &x, in_chmax y) {x = max(x,y);}
template<class in_chmin> void chmin(in_chmin &x, in_chmin y) {x = min(x,y);}
void Yes() {cout << "Yes" << endl; exit(0);}
void No() {cout << "No" << endl; exit(0);}
template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);}
template<class in_vec_cout>
void vec_cout(vector<in_vec_cout> vec) {
    for (in_vec_cout res : vec) {cout << res << " ";}
    cout << endl;
}

const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll maxN = 110;

ll powmodp(ll x, ll n) {
    if (n==0) return 1;
    if (n%2) return x * powmodp(x,n-1) % mod;
    ll res = powmodp(x,n/2);
    return res * res % mod;
}

ll inv(ll x) {return powmodp(x,mod-2);}

matrix E(ll n) {
    matrix res(n,vector<ll>(n));
    rep(i,n) res[i][i] = 1;
    return res;
}

void addmodp(ll &x, ll y) {x = (x + y) % mod;}

matrix prodmatrix(matrix x, matrix y) {
    ll H = x.size(), W = y[0].size(), sz = y.size();
    matrix res(H,vector<ll>(W));
    rep(i,H) rep(j,W) rep(k,sz) {
        addmodp(res[i][j], x[i][k] * y[k][j] % mod);
    }
    return res;
}
vector<ll> prodmatrix(matrix A, vector<ll> x) {
    matrix X(x.size(),vector<ll>(1));//         |y1|   |A11    |   |x1|
    rep(i,x.size()) X[i][0] = x[i];  //         |y2| = |  A22  | * |x2|
    matrix Y = prodmatrix(A,X);      //         |y3|   |    A33|   |x3|
    vector<ll> y(Y.size());
    rep(i,Y.size()) y[i] = Y[i][0];
    return y;
}
vector<ll> prodmatrix(vector<ll> x, matrix A) {
    matrix X(1,vector<ll>(x.size()));//                               |A11    |
    rep(i,x.size()) X[0][i] = x[i];  // |y1, y2, y3| = |x1, x2, x3| * |  A22  |
    matrix Y = prodmatrix(X,A);      //                               |    A33|
    vector<ll> y(Y[0].size());
    rep(i,Y[0].size()) y[i] = Y[0][i];
    return y;
}

matrix powmatrix(matrix x, ll n) {
    if (n==0) return E(x.size());
    if (n%2) return prodmatrix(x,powmatrix(x,n-1));
    matrix res = powmatrix(x,n/2);
    return prodmatrix(res,res);
}

ll N, M, K;
vector<ll> A(maxN);
matrix mat(maxN,vector<ll>(maxN));
vector<ll> G[maxN];

int main() {
    cin >> N >> M >> K;
    rep(i,N) cin >> A[i];
    rep(i,M) {
        ll x, y; cin >> x >> y;
        x--; y--;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    rep(now,N) {
        ll n = G[now].size();
        ll p = n * inv(M) % mod, q = (1-p+mod) % mod;
        mat[now][now] = ((p * inv(2) % mod) + q) % mod;
        for (ll to : G[now]) {
            mat[now][to] = inv(M) * inv(2) % mod;
        }
    }
    vector<ll> res = prodmatrix(A,powmatrix(mat,K));
    rep(i,N) cout << res[i] << endl;
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    string n;
    int k;
    cin >> n >> k;
    int sz=n.size();
    n="$"+n;
    const ll mod=1000000007;
    auto add=[&](ll &a,ll b){a=(a+b)%mod;};
    auto digit=[&](char c)->int
    {
        if('0'<=c&&c<='9') return (c-'0');
        else return (10+c-'A');
    };
    vector<ll> dp(17,0);
    vector<bool> c(16,0);
    int cnt=0;
    for(int i=0;i<sz;i++)
    {
        if(i>0)
        {
            if(c[digit(n[i])]==0)
            {
                c[digit(n[i])]=1;
                cnt++;
            }
        }
        vector<ll> ndp(17,0);
        for(int j=0;j<=16;j++)
        {
            if(j>0) add(ndp[j],dp[j]*j);
            if(j<16) add(ndp[j+1],dp[j]*(16-j));
        }
        if(i>0) add(ndp[1],15);
        for(int j=(i==0);j<digit(n[i+1]);j++)
        {
            if(c[j]==1) add(ndp[cnt],1);
            else add(ndp[cnt+1],1);
        }
        dp=ndp;
    }
    add(dp[cnt+(c[digit(n[sz])]==0)],1);
    cout << dp[k] << "\n";
    return 0;
}


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

#ifdef LOCAL
#include "/debug.h"
#else
#define db(...)
#endif

#define all(v) v.begin(), v.end()
#define pb push_back
using ll = long long;
const int NAX = 2e5 + 5, MOD = 1000000007;

// int a[101][101];

#define int __int128_t
#define ll __int128_t

__int128_t myabs(__int128_t x)
{
    if (x < 0)
        return -x;
    return x;
}

int gcd(int a, int b, int &x, int &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    int x1, y1;
    int d = gcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - y1 * (a / b);
    return d;
}

bool find_any_solution(int a, int b, int c, int &x0, int &y0, int &g)
{
    g = gcd(myabs(a), myabs(b), x0, y0);
    if (c % g)
    {
        return false;
    }

    x0 *= c / g;
    y0 *= c / g;
    if (a < 0)
        x0 = -x0;
    if (b < 0)
        y0 = -y0;
    return true;
}

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

void solveCase()
{
    int32_t n1, s1, k1;
    cin >> n1 >> s1 >> k1;
    ll n = n1, s = s1, k = k1;
    // cin >> n >> s >> k;
    db(n, s, k);
    ll x0, y0, g;
    if (find_any_solution(n, -k, s, x0, y0, g))
    {
        if (!(n * x0 + (-k) * y0) == s)
        {
            exit(1);
        }
        db(n * x0 + (-k) * y0, s);
        db(x0, y0, g);
        ll dx = k / g;
        ll dy = n / g;
        // assert(dx > 0 && dy > 0);
        if (!(dx > 0 && dy > 0))
        {
            exit(1);
        }
        db(dx, dy);
        bool ok = false;
        if (x0 < 0)
        {
            auto t = (-x0 + dx - 1) / dx;
            db("a", x0, y0, t);
            x0 += t * dx;
            y0 += t * dy;
            db(x0, y0);
        }
        else if (x0 >= dx)
        {
            auto t = x0 / dx;
            db("a", x0, y0, t);
            x0 -= t * dx;
            y0 -= t * dy;
            db(x0, y0);
            ok = true;
        }
        if (y0 < 0)
        {
            auto t = (-y0 + dy - 1) / dy;
            db("a", x0, y0, t);
            x0 += t * dx;
            y0 += t * dy;
            db(x0, y0);
        }
        // else if (y0 >= dy)
        // {
        //     auto t = y0 / dy;
        //     db("a", x0, y0, t);
        //     x0 -= t * dx;
        //     y0 -= t * dy;
        //     db(x0, y0);
        // }
        if (y0 > 0)
            cout << y0 << '\n';
        else
            cout << "-1\n";
    }
    else
        cout << "-1\n";
}

int32_t main()
{
#ifndef LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    int32_t t = 1;
    cin >> t;
    for (int i = 1; i <= t; ++i)
        solveCase();
    return 0;
}

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

#define R register
#define LL long long

inline int read() {
	int x = 0, f = 1; char a = getchar();
	for(; a > '9' || a < '0'; a = getchar()) if(a == '-') f = -1;
	for(; a <= '9' && a >= '0'; a = getchar()) x = x * 10 + a - '0';
	return x * f;
}

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

void exgcd(int a, int b, LL &x, LL &y) {
	if(! b) {
		x = 1; y = 0; return ;
	}
	exgcd(b, a % b, x, y);
	int t = x; x = y; y = t - a / b * y;
}

void solve() {
	int n = read(), s = read(), k = read();
	int g = gcd(k, n);
	if((n - s) % g != 0) { cout << -1<<endl; return ; }
	LL x, y;
	//k /= g; n /= g;
	//k /= g; n /= g;
	exgcd(k, n, x, y);
	//cout << k << ' ' << n << ' ' << x << ' ' << y << endl;
	x *= (n - s) / g;
	x = (x * g % n + n) % n;
	x /= g;
	cout << x << endl;
}

signed main() {
	#ifdef IN
	//freopen(".in", "r", stdin);
	//freopen(".out", "w", stdout);
	#endif
	int Case = read();
	while(Case --) solve();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000
#define MOD 998244353
using ll=long long;
using Graph=vector<vector<int>>;

int n=1;
vector<int> tree;
int solve(int a,int b,int i,int left,int right){
  if(b<=left||right<=a){
    return 0;
  }
  if(a<=left&&right<=b){
    return tree[i];
  }
  int x=solve(a,b,2*i+1,left,(left+right)/2);
  int y=solve(a,b,2*i+2,(left+right)/2,right);
  int res=x^y;
  return res;
}

void update(int i,int x){
  i+=n-1;
  tree[i]=x;
  while(i>0){
    i=(i-1)/2;
    tree[i]=tree[2*i+1]^tree[2*i+2];
  }
}

int main(){
  int N,Q;
  cin>>N>>Q;
  vector<int> A(N);
  for(int i=0;i<N;i++){
    cin>>A[i];
  }
  while(n<N){
    n<<=1;
  }
  tree.resize(2*n-1,0);
  for(int i=0;i<N;i++){
    tree[i+n-1]=A[i];
  }
  for(int i=n-2;i>=0;i--){
    tree[i]=tree[2*i+1]^tree[2*i+2];
  }

  for(int i=0;i<Q;i++){
    int T,X,Y;
    cin>>T>>X>>Y;
    if(T==1){
      X--;
      int y=tree[X+n-1];
      y^=Y;
      update(X,y);
    }else{
      int x=solve(X-1,Y,0,0,n);
      cout<<x<<'\n';
    }
  }
}

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<bitset>
#include<deque>
#include<cstdlib>
#include<set>
#include<ctime>
#define ll long long
#define mp make_pair
using namespace std;
ll read()
{
	ll x=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0')
	{
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		x=x*10+c-'0';
		c=getchar();
	}
	return f*x;
}
int mts[12];
ll A,B,C;
ll pow_mod(ll a,ll b,ll mod)
{
	ll ans=1;
	while(b){
		if(b&1) ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}
int main()
{
	for(int i=2;i<=9;++i)
	{
		int num=1,tmp=i;
		num++;
		tmp=tmp*i%10;
		while(tmp!=i)
		{
			num++;
			tmp=tmp*i%10;
		 }
		mts[i]=num;
	//	cout<<i<<": "<<mts[i]<<'\n';
	}
	A=read(),B=read(),C=read();
	if(A%10==1) cout<<1<<'\n';
	else if(A%10==0) cout<<0<<'\n';
	else
	{
		ll tmp=pow_mod(B,C,mts[A%10]-1);
	//	cout<<tmp<<'\n';
		if(!tmp) tmp+=mts[A%10]-1;
		cout<<pow_mod(A,tmp,10)<<'\n';

	}
	return 0;
 }


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

int index(int y, int x)
{
    return y * 16 + x;
}
int search(bitset<256> ttm, int H, int W, int a, int b)
{
    if (a == 0 && b == 0)
        return 1;
    int ans = 0;
    int cx = 0, cy = 0;
    bool flag = true;
    for (int y = 0; y < H && flag; y++)
    {
        for (int x = 0; x < W && flag; x++)
        {
            if (!ttm[index(y, x)])
            {
                cx = x;
                cy = y;
                flag = false;
            }
        }
    }
    ttm.set(index(cy, cx));
    if (b != 0)
        ans += search(ttm, H, W, a, b - 1);
    if (a != 0)
    {
        if (cx + 1 < W && !ttm[index(cy, cx + 1)])
        {
            ttm.set(index(cy, cx + 1));
            ans += search(ttm, H, W, a - 1, b);
            ttm.reset(index(cy, cx + 1));
        }

        if (cy + 1 < H && !ttm[index(cy + 1, cx)])
        {
            ttm.set(index(cy + 1, cx));
            ans += search(ttm, H, W, a - 1, b);
            ttm.reset(index(cy + 1, cx));
        }
    }

    return ans;
}

void solve(long long H, long long W, long long A, long long B)
{
    bitset<256> ttm(0);
    cout << search(ttm, H, W, A, B) << endl;
}

// Generated by 2.2.0 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main()
{
    long long H;
    scanf("%lld", &H);
    long long W;
    scanf("%lld", &W);
    long long A;
    scanf("%lld", &A);
    long long B;
    scanf("%lld", &B);
    solve(H, W, A, B);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, n) for (int i = (int)(l); i < (int)(n); i++)
#define drep(i, n, l) for (int i = (int)(n); i > (int)(l); i--)
#define INF INT_MAX
#define def (200007)
#define MOD (1000000007)
typedef vector<int> veci;
typedef vector<vector<int>> Veci;
typedef vector<int64_t> vecit;
typedef vector<vector<int64_t>> Vecit;
typedef int64_t intt;
typedef vector<vector<double>> Vecd;
typedef vector<double> vecd;
typedef pair<int, int> P;
/*veci のときdist.resize(N) Veciのとき2回resize;*/
//powはdouble型
Veci dp;
int sum=0;
int H,W,A,B;

void dfs(int nx,int index){
  if(H*W==(2*index+B)){
    sum++;
    return;
  }
  if(nx>=H*W)return;
  if(dp[nx/W][nx%W]!=0){
    return dfs(nx+1,index);
  }
  if(nx<H*W)dp[nx/W][nx%W]=index+3;

  if((nx+1)%W>(nx%W)){
    if(dp[(nx+1)/W][(nx+1)%W]==0){
      dp[(nx+1)/W][(nx+1)%W]=2;
      dfs(nx+2,index+1);
      dp[(nx+1)/W][(nx+1)%W]=0;
    }
  }
  if((nx+W)/W<H){
    if(dp[(nx+W)/W][(nx+W)%W]==0){
      dp[(nx+W)/W][(nx+W)%W]=1;
      dfs(nx+1,index+1);
      dp[(nx+W)/W][(nx+W)%W]=0;
    }
  }
  dp[nx/W][nx%W]=0;
}

int next_combination(int sub){
  int x=sub&-sub,y=sub+x;
  return(((sub&~y)/x)>>1)|y;
}

//3*10^6 10!
//16C8 1.2*10^4
//0何もない 1:1*1の畳 2:1*2の畳
int main(){
  cin>>H>>W>>A>>B;
  dp.resize(H);
  if(B>0){
	int bit = (1<<B)-1;
    for(;bit<(1<<H*W);bit=next_combination(bit)){
      rep(i,0,H){
        dp[i].assign(W,0);
      }
      rep(i,0,H*W){
        if(bit & (1<<i)){
          rep(j,1,H+1){
            if(i<W*j){
              dp[j-1][i-W*(j-1)]=1;
              break;
            }
          }
        }
      }
      int nex=0;
      rep(i,0,H*W){
        if(dp[i/W][i%W] == 0 ){
          nex=i;
          break;
        }
      }
      dfs(nex,0);
    }
  }else{
    rep(i,0,H)dp[i].assign(W,0);
    dfs(0,0);
  }
  cout<<sum<<endl;

}


#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define swap(a,b) (a^=b^=a^=b)
#define ll long long int
#define ull unsigned long long int
#define uint unsigned int
#define format(a) memset(a,0,sizeof(a))
#define rep(x,y,z) for(int x=y;x<=z;x++)
#define dec(x,y,z) for(int x=y;x>=z;x--)
#define mst(x) memset(x,0,sizeof(x))
const int maxn=2e5+20;
inline int read()
{
    int ans=0;
    char last=' ',ch=getchar();
    while(ch<'0'|ch>'9')last=ch,ch=getchar();
    while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
    if(last=='-')ans=-ans;
    return ans;
}
int n;
int a[maxn];
vector<int>save[maxn];
int main()
{
    n=read();
    rep(i,1,n)a[i]=read();
    ll ans=0;
    rep(i,1,n)
    {
        int cur=a[i]%200;

        ans+=save[cur].size();
        save[cur].push_back(a[i]);
    }
    cout<<ans<<endl;
    return 0;
}

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

int main()
{
    long long n, p = 0, a[2500] = {0};
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> p;
        a[p % 200]++;
    }
    long long ans = 0;
    for (int i = 0; i < 200; i++)
    {
        ans += (a[i] * (a[i] - 1)) / 2;
    }
    cout << ans;
}