#include <bits/stdc++.h>//Carefully Crafted by hetp111
using namespace std;
#define int long long
#define double long double
#define all(v) (v).begin(),(v).end()
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vii vector<pii>
#define MOD 1000000007
#define MOD2 998244353
#define MOD3 1000000009
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
#define FASTER ios_base::sync_with_stdio(0);cin.tie(0)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
//ifstream cinn("in.in");ofstream coutt("out.out");
int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;}
int ceil(const int &a,const int &b){return (a+b-1)/b;}
////Read:
//Check corner cases, n==1;
//
////Some function:
//__builtin_popcountll(),

int n,c;
vi v;

signed main(){
    FASTER;
    cin>>n>>c;
    v=vi(n);
    for(int &i:v)cin>>i;
    int ans=0;
    for(int i=0;i<=n;i++){
        for(int j=0;j<=n;j++){
            if(i+j>c || i+j>n)continue;
            vi hand;
            int cst=c,sum=0;
            for(int k=0;k<i;k++)hand.push_back(v[k]),cst--,sum+=hand.back();
            for(int k=0;k<j;k++)hand.push_back(v[n-k-1]),cst--,sum+=hand.back();
            sort(all(hand));

            for(int i:hand){
                if(i>=0)break;
                if(cst){
                    cst--;
                    sum-=i;
                }
            }
            ans=max(ans,sum);
        }
    }
    cout<<ans;
}

#include <bits/stdc++.h>

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

using namespace std;

int gcdi(int a, int b) {
	if(a<0) a=b;
	if(b<0) b=a;
    if (b == 0) return a;
    return gcdi(b, a%b);
}

int lcmi(int a, int b) {
    return a/gcdi(a, b)*b;
}

int gcdv(vector<int> gv){
	int tmp=gv[0];
	for(int i=1;i<(int)gv.size();i++) tmp=gcdi(tmp, gv[i]);
	return tmp;
}

int lcmv(vector<int> gv){
	int tmp=gv[0];
	for(int i=1;i<(int)gv.size();i++) tmp=lcmi(tmp, gv[i]);
	return tmp;
}

int main(){

	int n, k, gc;
  	vector<int> v;
	map<LLi, int> mp;

  	cin>> n >> k;
  	v.resize(n);
  	rep(i, n){
      	cin>> v[i];
      	if(v[i]==k){
          	cout<< "POSSIBLE" <<endl;
          	return 0;
        }
    }

  	gc=gcdv(v);

  	rep(i, n){
      	if(v[i]-k>0 && (v[i]-k)%gc==0){
          	cout<< "POSSIBLE" <<endl;
          	return 0;
        }
    }

  	cout<< "IMPOSSIBLE" <<endl;

	return 0;
}


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

// #define int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n) for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n) for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) long long(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll,ll,ll>>;
using vb = vector<bool>;

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

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

ll dp[105][10];

signed main()
{
  cin.tie( 0 ); ios::sync_with_stdio( false );
  ll h,w,k; cin>>h>>w>>k;
  k--;
  dp[0][0]=1;
  rep(i,h){
    rep(j,w){
      for(ll bit=0; bit<(1<<(w-1)); bit++){
        bool ok=true;
        rep(ki,w-2){
          if(bit>>ki&1 && bit>>(ki+1)&1){
            ok=false;
            break;
          }
        }
        if(!ok) continue;
        ll nj=j;
        if(bit>>j&1) nj=j+1;
        else if(j>0 && bit>>(j-1)&1) nj=j-1;
        dp[i+1][nj]+=dp[i][j];
        dp[i+1][nj]%=MOD;
      }
    }
  }
  cout<<dp[h][k]<<endl;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <random>
#include <chrono>
#include <queue>
#include <math.h>
#include <string>
#include <sstream>
#include <stack>
#include <deque>
#define pb push_back
#define make make_pair
#define kaku(a) cout << a << endl
#define isort(a) sort(a.begin(),a.end())
#define ll long long

using namespace std;

#define rep(i,n) for(ll i=0;i<n;i++)
#define PI 3.14159265359
long long gcd(long long a,long long b){ return b==0?a:gcd(b,a%b);}
long long lcm(long long a,long long b){ return a/gcd(a,b)*b;}

const ll inf =998244353;
typedef pair<ll,ll> Pair;

typedef vector<ll> vl;

const ll MAX = 510000;
const ll MOD = 998244353;

ll fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(ll n, ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

//繰り返し２乗
ll cal(ll x,ll n) {  // (K/x)^N
    int p = n;
    x %= inf;
    ll res = 1;
    while (p > 0) {
        if (p & 1) res = (x * res) % inf; //pが奇数ならresはx倍される
        x = x * x % inf; //ここはxを二乗している
        p >>= 1; //pを二で割って、あまり切り捨てか。
    }
    return res;
}

void solve(){
    ll n,m,k;
    cin >> n >>m >>k;
    COMinit();
    ll ans=0;
    rep(j,k+1){
        ll score=m;
        score*=COM(n-1,j);
        score%=MOD;
        score*=cal(m-1,n-1-j);
        score%=MOD;
        ans+=score;
        ans%=MOD;
    }
    kaku(ans);
}

int main(){
    solve();
}


#include <iostream>
#include<vector>
#include<string>
using namespace std;

int Roma(char a){
switch(a){
	case 'I':return 1;
	case 'V':return 5;
	case 'X':return 10;
	case 'L':return 50;
	case 'C':return 100;
	case 'D':return 500;
	case 'M':return 1000;
}
return 0;
}
int main() {
while(1){
	string a;
	cin>>a;
	if(cin.fail())break;
	int s=0;
	for(int b=0;b<a.length();b++){
		if(a.length()-1==b){
			s+=Roma(a[b]);
		}
		else{
			if(Roma(a[b])<Roma(a[b+1])){
				s+=Roma(a[b+1])-Roma(a[b]);
				b++;
			}
			else s+=Roma(a[b]);
		}
	}
	cout<<s<<endl;
}
}

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

int main() {
  string s;
  cin >> s;
  bool ok = true;
  if (s[0] != 'A') ok = false;
  int cnt = 0;
  for (int i = 1; i < s.size(); i++) {
    if (i >= 2 && i <= s.size() - 2) {
      if (s[i] == 'C') {
        cnt++;
        continue;
      }
    }
    if (!islower(s[i])) {
      ok = false;
    }
  }
  if (cnt != 1) ok = false;
  if (ok) cout << "AC" << endl;
  else cout << "WA" << endl;
  return 0;
}


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

int main(){
  int N;
  cin >> N;
  int check = 1000;
  for(int i; i < N; i++){
    int A;
    int count = 0;
    cin >> A;
    while(A % 2 == 0){
      A /= 2;
      count += 1;
    }
    if(count < check){
      check = count;
    }
  }
  cout << check << endl;
}

// ALDS 1_10_B
#include<cstdio>
#include<cstring>
#include<vector>
#define rep(i,a) for( int i = 0; i != (a); ++i )
#define repi(i,a,b) for( int i = (a); i != (b); ++i )

const int INF = 1<<28;

int n;
std::vector<int> r(100);
std::vector<std::vector<int> > dp( 100, std::vector<int>( 100, INF ) );

int main()
{
	scanf( "%d", &n );
	rep( i, n )
		scanf( "%d%d", &r[i], &r[i+1] );

	rep( i, n )
		dp[i][i] = 0;

	repi( k, 1, n ) rep( i, n-k ) repi( j, i, i+k )
		dp[i][i+k] = std::min( dp[i][i+k], dp[i][j]+dp[j+1][i+k]+r[i]*r[j+1]*r[i+k+1] );

	printf( "%d\n", dp[0][n-1] );

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define all(t) t.begin(), t.end()
#define inrange(i, a, b) (((i)>= min((a), (b))) && ((i) <= max((a), (b))))
typedef vector<ll> vi;
typedef pair<ll, ll> pii;
#define fi first
#define se second
#define upb upper_bound
#define lwb lower_bound
#define show(x) cout << #x << " is " << x << "\n";
const ll inf = 2e18;
const ll mod = 1e9 + 7;
const ld pi = 3.141592653589793238462643383279502884;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void print(ll a[], ll n){for(ll i=0;i<n;i++){cout<<a[i]<<" ";}cout<<"\n";}
ll power(ll x, ll y){
    if(y<=0) return 1;
    ll ans = 1;
    x %= mod;
    while(y){
        if(y&1)
            ans = (x * ans) % mod;
        x = (x * x) % mod;
        y >>= 1;
    }
    return ans;
}
ll modInverse(ll n) {return power(n, mod-2);}
inline ll mul(ll a, ll b){ return (a * b) % mod; }
inline ll sub(ll a, ll b){ ll c = (a - b); if(c < 0) c += mod; return c; }
inline ll add(ll a, ll b){ ll c = (a + b); if(c >= mod) c -= mod; return c; }
inline ll divi(ll a, ll b){ return mul(a, modInverse(b)); }

//------------------------------------------------------------------------------------------

const ll N = 5e5 + 1;
//vector<ll> adj[N];
//bool visited[N];

const int B = sqrt(N); // sqrt(n)

struct Q {
    int L, R, cmp, ind;
    bool operator<(const Q &temp) const {
        return cmp < temp.cmp;
    }
};

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

	ll n, q;
	cin>>n>>q;
	ll a[n];
	map<ll, vi> mp;
	for(int i=0;i<n;i++){
		cin>>a[i];
		mp[a[i]].pb(i);
	}

	int k=0;
	for(auto it : mp){
		for(auto x : it.second){
			a[x] = k;
		}
		k++;
	}
	vi cnt(k);

	vector<Q> que(q);
	for(int i=0;i<q;i++){
		cin>>que[i].L>>que[i].R;
		que[i].L--;
		que[i].R--;
		que[i].cmp = (que[i].L/B)*n + que[i].R;
		que[i].ind = i;
	}

	sort(all(que));

	int l=0, r=-1, dist=0;
	vi ans(q);

	auto add = [&](int i) { if (++cnt[i]==1) dist++; };
    auto rem = [&](int i) { if (--cnt[i]==0) dist--; };

	for(auto it : que){
		int l1 = it.L;
		int r1 = it.R;
		while(l<l1) rem(a[l++]);
		while(l>l1) add(a[--l]);
		while(r<r1) add(a[++r]);
		while(r>r1) rem(a[r--]);
		ans[it.ind] = dist;
	}

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

    return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define rep(i,N) for(int i=0;i<(int)N;i++)
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
static inline void PUT(char c)
{
  static char buf[1<<15],*ptr=buf;
  if(ptr==buf+strlen(buf)||c==0){fwrite(buf,1,ptr-buf,stdout),ptr=buf;}*ptr++=c;
}
static inline int IN(void)
{
  int x=0,f=0,c=getchar();while(c<48||c>57){f^=c==45,c=getchar();}
  while(c>47&&c<58){x=x*10+c-48,c=getchar();}return f?-x:x;
}
static inline void OUT(int a)
{
  if(a<0)PUT('-'),a=-a;
  int d[40],i=0;do{d[i++]=a%10;}while(a/=10);
  while(i--){PUT(d[i]+48);}PUT('\n');
}
int N=0;
static inline void Add(int *fwt,int i,int v){while(i<N){fwt[i]+=v;i|=i+1;}}
static inline int Sum(int *fwt,int i){int v=0;while(i>=0){v+=fwt[i];i=(i&(i+1))-1;}return v;}
int main()
{
  N=IN();int Q=IN(),after[500001],dat[500000],fenwick[500000],id[500001],left[500001],pos[500000],result[500000];
  rep(i,N){dat[i]=IN()-1;}
  rep(i,Q){left[i+1]=IN()-1;int r=IN()-1;after[i+1]=id[r];id[r]=i+1;}
  for(int r=0;r<N;r++)
  {
    if(pos[dat[r]]){Add(fenwick,pos[dat[r]]-1,-1);}
    Add(fenwick,r,1);
    for(int i=id[r];i;i=after[i]){result[i-1]=Sum(fenwick,r)-Sum(fenwick,left[i]-1);}
    pos[dat[r]]=r+1;
  }
  rep(i,Q){OUT(result[i]);}
}


/*====Corycle====*/
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<complex>
#include<cstdio>
#include<vector>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<map>
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const int Mod=1e9+7;
const int N=1e6+5;
int read(){
	int s=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){s=s*10+c-'0';c=getchar();}
	return s*f;
}
int n,m,a,b,Ans,fac[N],inv[N];
int mod(int x){return x>=Mod?x-Mod:x;}
void Prepare(){
	fac[0]=fac[1]=inv[0]=inv[1]=1;
	for(int i=2;i<N;i++)fac[i]=1ll*fac[i-1]*i%Mod;
	for(int i=2;i<N;i++)inv[i]=1ll*inv[Mod%i]*(Mod-Mod/i)%Mod;
	for(int i=2;i<N;i++)inv[i]=1ll*inv[i-1]*inv[i]%Mod;
}
int C(int n,int m){return n<m?0:1ll*fac[n]*inv[m]%Mod*inv[n-m]%Mod;}
int F(int n,int m){return C(n+m-2,n-1);}
int main(){
//	freopen("_.in","r",stdin);
//	freopen("_.out","w",stdout);
	n=read();m=read();a=read();b=read();Prepare();
	for(int i=1;i<=n-a;i++)Ans=mod(Ans+1ll*F(i,b)*F(n-i+1,m-b)%Mod);
	printf("%d\n",Ans);
	return 0;
}

#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef vector<P> vpl;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(ll i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {0,1,0,-1,1,-1,1,-1};
ll 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 T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << endl;}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << endl;
}
const int mod = 1e9 + 7;

vector<ll> fac(MAX), finv(MAX), inv(MAX);

void comInit(){
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for(ll i=2; i<MAX; i++){
		fac[i] = fac[i-1]*i % mod;
		inv[i] = mod - inv[mod%i] * (mod/i) % mod;
		finv[i] = finv[i-1] * inv[i] % mod;
	}
}

ll com(ll n, ll k){
	if(n < k) return 0;
	if(n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n-k] % mod) % mod;
}

int main(){
	ll n,x; cin >> n >> x;
	comInit();
	vl a(n); rep(i,n) cin >> a[i];
	sort(rall(a));
	vvl dp(n+1,vl(x+1,0));
	dp[0][x] = 1;
	rep(i,n){
		rep(j,x+1){
			dp[i+1][j%a[i]] += inv[n-i] * dp[i][j];
			dp[i+1][j] += ((n-1-i)*inv[n-i])%mod * dp[i][j];
			dp[i+1][j%a[i]] %= mod;
			dp[i+1][j] %= mod;
		}
	}
	ll ans = 0;
	rep(i,x+1){
		ans += dp[n][i] * fac[n] % mod * i;
		ans %= mod;
	}
	cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define txt                        \
    freopen("ip.txt", "r", stdin); \
    freopen("op.txt", "w", stdout)
#define a3 INT_MAX
#define i3 INT_MIN
#define a6 INT64_MAX
#define i6 INT64_MIN
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define fo(i, n) for (int i = 0; i < (int)n; i++)
#define rep(i, k, n) for (int i = k; k < (int)n ? i < (int)n : i > (int)n; k < (int)n ? i += 1 : i -= 1)
#define quickie                       \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);
#define div2(a) (!(a & 1))
#define deb(x) cout << (#x) << " " << (x) << "\n";
#define newl cout << "\n"
#define all(s) s.begin(), s.end()
#define pi 3.14159265358979323846
#define fp(x) fixed << setprecision(x) <<
template <class T>
T mod(T a)
{
    if (a < 0)
    {
        a += 1000000007;
    }
    return a % 1000000007;
}
template <class T>
T addmod(T a, T b)
{
    return (mod(a) + mod(b)) % 1000000007;
}
template <class T>
T prodmod(T a, T b)
{
    return (mod(a) * mod(b)) % 1000000007;
}
template <class T>
T bin_exp(T a, T b)
{
    T res = 1;
    T temp = 1;
    if (b <= 1)
        return a;
    if (b % 2 == 0)
        res = bin_exp(a * a, b / 2);
    else
    {
        res *= a;
        temp = bin_exp(a, --b);
    }
    return res * temp;
}
template <class T>
T bin_expmod(T a, T b)
{
    T res = 1;
    T temp = 1;
    if (b <= 1)
        return a;
    if (b % 2 == 0)
        res = bin_expmod(prodmod(a, a), b / 2);
    else
    {
        res = prodmod(a, res);
        temp = bin_exp(a, --b);
    }
    return prodmod(res, temp);
}

template <class T>
int digit(T a)
{
    vector<long long> dig = {
        0,
        9,
        99,
        999,
        9999,
        99999,
        999999,
        9999999,
        99999999,
        999999999,
        9999999999,
        99999999999,
        999999999999,
        9999999999999,
        99999999999999,
        999999999999999,
        9999999999999999,
        99999999999999999,
        999999999999999999,
        INT64_MAX};
    auto it = lower_bound(dig.begin(), dig.end(), a);
    int idx = (it - dig.begin());
    return idx;
}
template <class T>
int sumAllInts(T n)
{
    int a = 1;
    int su = 0;
    while (n)
    {
        su += n % 10;
        n /= 10;
    }
    return su;
}
template <class T>
void split(char c, T s)
{
    int n = s.size();
    int i = 0;
    while ((cout << s[i], i += 1) && (i < n) && (cout << c << ' '))
        ;
}
void print(vi &vec)
{
    for (int i = 0; i < vec.size(); i++)
    {
        cout << vec[i] << " ";
    }
}

void solve()
{
    int n,t;
    cin>>n>>t;
    int temp;cin>>temp;
    ll ans = t;
    fo(i,n-1){
        int te; cin>>te;
        int diff = te - temp;
        ans+=min(diff,t);
        temp = te;
    }
    cout<<ans;
}
int main()
{
    quickie;
    //txt;
    int t = 1;
    while (t--)
    {
        solve();
        newl;
    }
    return 0;
}

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

int main(){
  ll N,T;cin >> N >> T;
  ll sum =0;
  vector<ll> C(N);
  for(int i=0;i<N;i++){
    cin >> C[i];
  }
  for(int i=0;i<N-1;i++){
    sum += min(T,C[i+1]-C[i]);
  }
  sum += T;
  cout << sum << endl;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using iii = pair<ii, int>;
#define vt vector
#define pq priority_queue
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define __lcm(a, b) a * b / __gcd(a, b)
// #define multicase
const int MOD = 1e9 + 7;
const int INF = 2e9;
const int N = 2e5 + 5;

int x;

void solve() {
    int T = 1;
  #ifdef multicase
    cin >> T;
  #endif
    while(T--) {
        cin >> x;
        cout << (x < 1200 ? "ABC\n" : x < 2800 ? "ARC\n" : "AGC\n");
    }
}

int main() {

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

    solve();

}

#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <bitset>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <string>
#include <chrono>
#include <random>
#include <cmath>
#include <cassert>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <sstream>

using namespace std;

int main(int argc, char** argv) {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int r;
    cin >> r;
    if (r < 1200) {
        cout << "ABC\n";
    } else if (r < 2800) {
        cout << "ARC\n";
    } else {
        cout << "AGC\n";
    }

    return 0;
}


#include <iostream>
using namespace std;

int XX(int a, int d){
	return a * a * d;
}

int main(){
	int d;
	while (true){
		int sum = 0;
		cin >> d;
		if (cin.eof()) { break; }
		for (int i = 0;i<600/d;i++){
			sum += XX(i*d, d);
		}
		cout << sum << endl;
	}
	return 0;
}

#include<stdio.h>
int l;
int ans=0;

int main()
{
    while(scanf("%d",&l)!=-1)
    {
        ans=0;
        for(int i=0;i*l<600;i++)
        {
            ans=ans+i*l*i*l*l;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <iostream>
using namespace std;

int main(){
	int a=0,b=0;
	int na,nb;
	int nc;
	while(1){
	cin>>nc;
	if(nc==0) break;
	a=b=0;
	for(int i=0;i<nc;i++){
		cin>>na>>nb;
		if(na>nb) a+=(na+nb);
		else if(na<nb) b+=(na+nb);
		else {a+=na;b+=nb;}
	}
	cout<<a<<" "<<b<<endl;
	}
	return 0;
}

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

string station = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

int n;
int main() {
    while(cin>>n) {
        if(n==0) return 0;
        vector<int> vec(n);
        rep(i,n) {
            int a; cin>>a;
            vec[i]=a;
        }
        string ss;
        cin>>ss;
        rep(i,ss.size()) {
            int a = i % vec.size();
            int t = station.find(ss[i])-vec[a];
            if(t<0) t += station.size();
            t %= station.size();
            cout<<station[t];
        }
        cout<<endl;

    }

    return 0;
}


#include<bits/stdc++.h>
#define forr(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = 1; i <= (n); i++)
#define ALL(a) (a.begin()),(a.end())
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll, ll> LP;
const ll LINF = 1LL<<60;
const int INF = 1001001001;

/* --------------------------------------------------- */

int main() {
    int a, b, k;
    cin >> a >> b >> k;
    rep(i, k) {
        if(i % 2 == 0) {
            a /= 2;
            b += a;
        } else {
            b /= 2;
            a += b;
        }
    }
    cout << a << " " << b << endl;
    return 0;
}

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;

int main() {
  int N, H, W; cin >> N >> H >> W;
  cout << (N-H+1) * (N-W+1) << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
typedef long long int ll;
int main()
{
	int n;
	ll a, b, x, d, op = 0 ;
	cin >> n >> x;
	cin >> a >> b;
	if( a + b > x )
	{
		d = a + b - x;
		if( d <= b ){
	    	b -= d;
        }
	    else
        {
	    	b = 0;
        }
          op += d;
	}
	a = b;
	rep(i,2,n)
	{
	    cin >> b;
	    if( a + b > x )
		{
			d = a + b - x;
			if( d <= b ){
		    	b -= d;
            }
		    else
            {
              b = 0;
            }
			op += d;
		}
		a = b;
	}
	cout << op<< endl;
	return 0;
}

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

int main() {
	int N, x;
	cin >> N >> x;
	vector<int> a(N);
	for (int i = 0; i < N; ++i) cin >> a[i];

	vector<int> cpa = a;

	long long ans = 0;
	for (int i = 0; i < N; ++i) {
		if (i == 0) {
			if (cpa[i] > x) ans += cpa[i] - x, cpa[i] = x;
		}
		else {
			if (cpa[i] + cpa[i-1] > x) ans += cpa[i] + cpa[i-1] - x, cpa[i] = x - cpa[i-1];
		}
	}

	cpa = a;
	long long ans2 = 0;
	for (int i = N - 1; i >= 0; i--) {
		if (i == N - 1) {
			if (cpa[i] > x) ans2 += cpa[i] - x, cpa[i] = x;
		}
		else {
			if (cpa[i] + cpa[i+1] > x) ans2 += cpa[i] + cpa[i+1] - x, cpa[i] = x - cpa[i+1];
		}
	}

	cout << min(ans, ans2) << endl;
	return 0;
}


// https://atcoder.jp/contests/abc152/tasks/abc152_c

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

int main()
{
    long a, b;
    cin >> a >> b;
    long ans = 0;
    for (int x = a; x <= b; x++)
    {
        string s, t;
        s = to_string(x);
        t = s;
        reverse(t.begin(), t.end());
        if (s == t)
        {
            ans++;
        }
    }
    cout << ans << endl;
    return 0;
}

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

int main() {
  int A, B;
  cin >> A >> B;
  int res = 0;
  for (int i = A; i <= B; i++) {
    if (i/10000 == i%10) {
      if (i/1000%10 == i%100/10) {
        res += 1;
      }
      i += 9;
    }
  }
  cout << res << endl;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <random>
#include <chrono>
#include <bitset>

using namespace std;

typedef long long ll;
typedef long double ld;
#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);

const ll SIZE = 1e5 + 100, INF = 1e9 * 1e4 + 10, MOD = 1e9 + 7;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll n, a, b, c, d, dp[SIZE][30], lf, q;
vector<ll> vec;
map<ll, ll> ch;

ll check(int val, int pos) {
    if (val == 0) return pos;

    ll lft = val, u = pos;
    for (ll i = 29; i >= 0; i--) {
        if (lft - (1ll << i) >= 0) {
            u = dp[u][i];
            lft -= (1ll << i);
        }
    }

    return u;
}
void sol() {
    cin >> n;
    vec.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> vec[i];
    }

    cin >> lf;

    cin >> q;

    for (int i = 29; i >= 0; i--) {
        dp[n - 1][i] = n - 1;
    }
    for (int i = vec.size() - 2; i >= 0; i--) {
        int l = i, r = vec.size();

        while (r - l > 1) {
            int mid = (l + r) / 2;
            if (vec[i] + lf < vec[mid]) {
                r = mid;
            }
            else {
                l = mid;
            }
        }

        dp[i][0] = l;
        for (int j = 1; j < 30; j++) {
            dp[i][j] = dp[dp[i][j - 1]][j - 1];
        }
    }

    while (q--) {
        ll lq, rq;
        cin >> lq >> rq;
        if (lq > rq) swap(lq, rq);

        if (lq == rq) {
            cout << "0\n";
            continue;
        }

        lq--;
        rq--;
        ll dist = 0, cur = lq, curR = rq;

        int l = 0, r = vec.size() + 1;
        while (r - l > 1) {
            int mid = (l + r) / 2;
            if (check(mid, cur) >= curR) {
                r = mid;
            }
            else {
                l = mid;
            }
        }
        cout << r << "\n";
    }
}

int main()
{
    fastInp;

    ll t = 1;

    while (t--) {
        sol();
    }

    return 0;
}

#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")

using namespace std;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
typedef long long ll;
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REP(i,num,n) for(int i=num, i##_len=(n); i<i##_len; ++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; }
#define print(x) (cout << (x) << endl)
#define pb push_back
#define mp make_pair
#define sz(x) int(x.size())
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
#define fs first
#define sc second
// 多倍長
#include <boost/rational.hpp>
#include <boost/multiprecision/cpp_int.hpp>
using boost::rational;
using boost::multiprecision::cpp_int;

const int INF = 1e9;
const ll LLINF = 1e16;
const ll MOD = 1e9+7;
template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

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

// 指定した文字cが文字列に何文字入ってるか
ll stringcount(string s, char c) {
    return count(s.cbegin(), s.cend(), c);
}

// 割り算の切り上げ処理
template<typename T>
T rp(T a, T b) {
  return (a + b - 1) / b;
}

// 桁和
template<typename T>
T digsum(T n) {
    T res = 0;
    while(n > 0) {
        res += n%10;
        n /= 10;
    }
    return res;
}

// 回文判定
bool kai (string s) {
    string t{s};
    reverse(begin(t), end(t));
    return s == t;
}

using pint = pair<int, int>;
using pll = pair<ll, ll>;

int dp[3100][3100];

int main(void)
{
    cout.tie(0);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    int n,t;
    cin >> n >> t;
    vector<pint> p(n);
    rep(i, n){
        cin >> p[i].fs >> p[i].sc;
    }
    sort(all(p));
    int ans = 0;
    rep(i, n){
        rep(j, t){
            chmax(dp[i + 1][j], dp[i][j]);
            if(j < p[i].fs) continue;
            chmax(dp[i + 1][j], dp[i][j - p[i].fs] + p[i].sc);
        }
        int now = dp[i][t - 1] + p[i].sc;
        chmax(ans, now);
    }
    print(ans);

}


#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<sstream>
#include<cmath>
#include<numeric>
#include<map>
#include<stack>
#include<queue>
#include<list>
using namespace std;
int inf = 1000000000;

int main(void) {

  string alp = "IVXLCDM";
  int num[7] = {1, 5, 10, 50, 100, 500, 1000};

  string s;
  while(cin >> s){
    reverse(s.begin(), s.end());
    int n = 0;
    int b = 0;
    for(int i=0; i<s.size(); i++){
      for(int j=0; j<7; j++){
        if( s[i] == alp[j] ){
          // cerr << n << " " << b << endl;
          if( num[j] < b ) n -= num[j];
          else n += num[j];

          b = num[j];
        }
      }

    }
    cout << n << endl;

  }

  return 0;
}

// EOF

#include <stdio.h>

int main(void){
    char fig[101];
    int num;

    while (scanf("%s", fig) != EOF){
        num = 0;
        for (char *pt=fig; *pt!='\0'; pt++){
            switch (*pt){
            case 'I':
                switch(*(pt+1)){
                case 'V':
                    num += 4;
                    pt++;
                    break;
                case 'X':
                    num += 9;
                    pt++;
                    break;
                default:
                    num += 1;
                    break;
                }
                break;
            case 'V':
                num += 5;
                break;
            case 'X':
                switch(*(pt+1)){
                case 'L':
                    num += 40;
                    pt++;
                    break;
                case 'C':
                    num += 90;
                    pt++;
                    break;
                default:
                    num += 10;
                    break;
                }
                break;
            case 'L':
                num += 50;
                break;
            case 'C':
                switch(*(pt+1)){
                case 'D':
                    num += 400;
                    pt++;
                    break;
                case 'M':
                    num += 900;
                    pt++;
                    break;
                default:
                    num += 100;
                    break;
                }
                break;
            case 'D':
                num += 500;
                break;
            case 'M':
                num += 1000;
                break;
            }
        }
        printf("%d\n", num);
    }

    return 0;
}


const int LG = 21;
const int FN = 400005;
const long long MOD = 998244353;
const long long INF = 1e9;
const long long INFLL = 1e18;

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

#define forn(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define fi first
#define se second
#define re return
#define pb push_back
#define uniq(x) sort(all(x)); (x).resize(unique(all(x)) - (x).begin())

#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " " << #x << " " << x << endl
#define ln cerr << __LINE__ << endl
#else
#define dbg(x) void(0)
#define ln void(0)
#endif // LOCAL

int cx[4] = {-1, 0, 1, 0};
int cy[4] = {0, -1, 0, 1};

ll inq(ll x, ll y)
{
    if (!y) re 1 % MOD;
    ll l = inq(x, y / 2);
    if (y % 2) re l * l % MOD * x % MOD;
    re l * l % MOD;
}

ll rev(ll x)
{
    return inq(x, MOD - 2);
}

bool __precomputed_combinatorics = 0;
vector<ll> __fact, __ufact, __rev;

void __precompute_combinatorics()
{
    __precomputed_combinatorics = 1;
    __fact.resize(FN);
    __ufact.resize(FN);
    __rev.resize(FN);
    __rev[1] = 1;
    for (int i = 2; i < FN; i++) __rev[i] = MOD - __rev[MOD % i] * (MOD / i) % MOD;
    __fact[0] = 1, __ufact[0] = 1;
    for (int i = 1; i < FN; i++) __fact[i] = __fact[i - 1] * i % MOD, __ufact[i] = __ufact[i - 1] * __rev[i] % MOD;
}

ll fact(int x)
{
    if (!__precomputed_combinatorics) __precompute_combinatorics();
    return __fact[x];
}

ll cnk(int n, int k)
{
    if (k < 0 || k > n) return 0;
    if (!__precomputed_combinatorics) __precompute_combinatorics();
    return __fact[n] * __ufact[n - k] % MOD * __ufact[k] % MOD;
}

int Root(int x, vector<int> &root)
{
    if (x == root[x]) return x;
    return root[x] = Root(root[x], root);
}

void Merge(int v, int u, vector<int> &root, vector<int> &sz)
{
    v = Root(v, root), u = Root(u, root);
    if (v == u) return;
    if (sz[v] < sz[u])
    {
        sz[u] += sz[v];
        root[v] = u;
    }
    else
    {
        sz[v] += sz[u];
        root[u] = v;
    }
}

int ok(int x, int n)
{
    return 0 <= x && x < n;
}

const int N = 1100000;

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll n, m, k;
    cin >> n;
    vi a(2 * n);
    forn(i, 2 * n) cin >> a[i];
    sort(all(a));
    ll ans = 0;
    forn(i, n) ans += min(a[2 * i], a[2 * i + 1]);
    cout << ans;
}

/* Note:
Check constants at the beginning of the code.
    N is set to 4e5 but be careful in problems with large constant factor.
    Setting N in every problem is more effective.
Check corner cases.
    N = 1
No def int long long for now.
Add something here.
*/

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

int N;
int L[200];

int main() {
    int total = 0;
    cin >> N;
    for (int i = 0; i < 2 * N; i++) {
        cin >> L[i];
    }
    sort(L, L + 2 * N);
    for (int i = 0; i < 2 * N; i += 2) {
        total += min(L[i], L[i + 1]);
    }
    cout << total;
}


#include <algorithm>
#include <cmath>
#include <vector>
#include <functional>
#include <cstdlib>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <ctype.h>
#include <climits>
#include <queue>
#include <iomanip>
using namespace std;
typedef long long ll;
#define REP(i, n) for(ll i = 0; i < (ll)(n); ++i)
template<class T> inline bool chmax(T& a, T b) { if(a < b){ a=b; return 1; } return 0;}
template<class T> inline bool chmin(T& a, T b) { if(a > b){ a=b; return 1; } return 0;}

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

	string s="";
	while(n!=0){
		if(n%-2==1){
			s = '1'+s;
			n /= -2;
		}else if(n%-2==-1){
			s = '1'+s;
			n /= -2;
			n += 1;
		}else{
			s = '0'+s;
			n /= -2;
		}
	}

	if(s.size()==0) s = "0";
	cout << s << endl;

	return 0;
}

#include <iostream>
#include <map>
typedef long long ll;
using namespace std;

// 素数 -> 個数
map<ll, ll> mp;

// 素数
map<ll, bool> prime;

bool isPrime(ll n) {
    if (prime.find(n) != prime.end()) return prime[n];
    if (n == 2) {
        prime[2] = true;
        return true;
    }
    else {
        bool flg = true;
        for (ll i = 2; i * i <= n; i++) {
            if (n % i == 0) {
                flg = false;
                break;
            }
        }
        prime[n] = flg;
        return flg;
    }
}
void prime_split(ll n) {
    ll gen = 2;
    while (true) {
        if (isPrime(gen)) {
            if (mp.find(gen)==mp.end()) mp[gen] = 0;
            while (n % gen == 0) {
                n /= gen;
                mp[gen] += 1;
            }
        }
        if (n == 1) break;
        gen++;
    }
}
int main(void) {
    int N;cin>>N;
    ll result = 1;
    for (int i = 2; i <= N; i++) {
        prime_split(i);
    }
    for (map<ll,ll>::iterator it = mp.begin(); it != mp.end(); ++it) {
        result *= it->second + 1;
        result %= 1000000007;
    }
    cout << result << endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
int main(){
    int a,b;
    cin >> a >> b;
    a--;b--;
    cout << (a+b-1)/a << endl;
    return 0;
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <fstream>
#include <numeric>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;

#define EPS (1e-7)
#define INF 1e18
#define max(p,q)((p)>(q)?(p):(q))
#define min(p,q)((p)<(q)?(p):(q))
#define PI (acos(-1))

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

int main() {
    ll N, M;
    cin >> N >> M;

    if ((N % 2) == 1){
        REP(i, M){
            cout << (i + 1) << " " << (N - i - 1) << endl;
        }
    }else{

        vector<P> v;
        int i = 1;
        while(v.size()<M+1){
            v.push_back(P(i, N + 1 - i));
            v.push_back(P(N / 2 - i, N / 2 + i));
            i++;
        }
        REP(i, M) cout << v[i].first << " " << v[i].second << endl;

    }

    return 0;
}


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

typedef long long ll;

main()
{
    int a, b, sum, c = 1;
    cin >> a >> b;
    sum = a;
    if(b == 1){
        cout << 0;
    }
    else if(b <= a){
        cout << 1;
    }
    else{
        while(1){
            sum = sum + (a - 1);
            c++;
            if(sum >= b){
                break;
            }
        }
        cout << c;
    }

}

///promy_pompom
//hello world

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

int main() {
  int A, B;
  string S;
  cin >> A >> B >> S;
  string ans="Yes";
  for (int i=0; i<A; i++) {
    if (S[i]=='-') {
      ans="No";
    }
  }

  if (S[A]!='-') {
    ans="No";
  }

  for (int i=A+1; i<A+B+1; i++) {
    if (S[i]=='-') {
      ans="No";
    }
  }

  cout << ans << endl;

}


#include<iostream>
#include<vector>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#define rep(i,n) for (int i= 0;i<(n);++i)
using namespace std;
typedef long long ll;
int main(){
    int n,m;
    cin>>n>>m;
    vector<string>vec(n);
    int s;
    string c;
    rep(i,m){
       cin>>s>>c;
       if(n!=1&&s==1&&c=="0"){
            cout<<-1<<endl;
             return 0;
        }
        if(vec.at(s-1)==""){
           vec.at(s-1)=c;
        }else if(vec.at(s-1)!=c){
           cout<<-1<<endl;
           return 0;
         }

   }
   string ans="";

   if(n!=1&&vec.at(0)==""){
       vec.at(0)="1";
   }
   rep(i,n){
       if(vec.at(i)==""){
           ans+="0";
       }else{
           ans+=vec.at(i);
       }
   }
   cout<<ans<<endl;

}

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <bitset>

typedef unsigned long long ULLONG;
typedef long long LLONG;
static const LLONG MOD_NUM = 1000000007; //998244353;

template<class _T> static void get(_T& a) {
	std::cin >> a;
}
template<class _T> static void get(_T& a, _T& b) {
	std::cin >> a >> b;
}
template<class _T> static void get(_T& a, _T& b, _T& c) {
	std::cin >> a >> b >> c;
}
template <class _T> static _T tp_pow(int base, int exp)
{
	_T ans = 1;

	for (int i = 0; i < exp; i++) {
		ans *= (_T)base;
	}
	return ans;
}

static void D();

int main()
{
	D();
	fflush(stdout);
	return 0;
}

static void D()
{
	int N, M;
	get(N, M);

	std::vector<int> num(N, -1);

	for (int i = 0; i < M; i++) {
		int s, c;
		get(s, c);

		if ((s > N) || ((num[s - 1] != -1) && (num[s - 1] != c))) {
			printf("-1\n");
			return;
		}
		num[s - 1] = c;
	}

	if (num[0] == 0 && N > 1) {
		printf("-1\n");
		return;
	}
	else if (num[0] == -1 && N > 1) num[0] = 1;

	for (int i = 0; i < N; i++) {
		if (num[i] < 0) num[i] = 0;
		printf("%d", num[i]);
	}
	putchar('\n');
}


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

int main(){
  string s;
  cin >> s;
  bool flag = (s.size()%2 == 0);
  while(s.size()>0 && flag){
    if(s.substr(0,2) == "hi"){
      s = s.substr(2,s.size()-2);
    }
    else flag = 0;
  }
  cout << (flag?"Yes":"No") << endl;
}

#include <bits/stdc++.h>

#define debug(...) fprintf(stderr, __VA_ARGS__)

#ifndef AT_HOME
#define getchar() IO::myGetchar()
#define putchar(x) IO::myPutchar(x)
#endif

namespace IO {
	static const int IN_BUF = 1 << 23, OUT_BUF = 1 << 23;

	inline char myGetchar() {
		static char buf[IN_BUF], *ps = buf, *pt = buf;
		if (ps == pt) {
			ps = buf, pt = buf + fread(buf, 1, IN_BUF, stdin);
		}
		return ps == pt ? EOF : *ps++;
	}

	template<typename T>
	inline bool read(T &x) {
		bool op = 0;
		char ch = getchar();
		x = 0;
		for (; !isdigit(ch) && ch != EOF; ch = getchar()) {
			op ^= (ch == '-');
		}
		if (ch == EOF) {
			return false;
		}
		for (; isdigit(ch); ch = getchar()) {
			x = x * 10 + (ch ^ '0');
		}
		if (op) {
			x = -x;
		}
		return true;
	}

	inline int readStr(char *s) {
		int n = 0;
		char ch = getchar();
		for (; isspace(ch) && ch != EOF; ch = getchar())
			;
		for (; !isspace(ch) && ch != EOF; ch = getchar()) {
			s[n++] = ch;
		}
		s[n] = '\0';
		return n;
	}

	inline void myPutchar(char x) {
		static char pbuf[OUT_BUF], *pp = pbuf;
		struct _flusher {
			~_flusher() {
				fwrite(pbuf, 1, pp - pbuf, stdout);
			}
		};
		static _flusher outputFlusher;
		if (pp == pbuf + OUT_BUF) {
			fwrite(pbuf, 1, OUT_BUF, stdout);
			pp = pbuf;
		}
		*pp++ = x;
	}

	template<typename T>
	inline void print_(T x) {
		if (x == 0) {
			putchar('0');
			return;
		}
		static int num[40];
		if (x < 0) {
			putchar('-');
			x = -x;
		}
		for (*num = 0; x; x /= 10) {
			num[++*num] = x % 10;
		}
		while (*num){
			putchar(num[*num] ^ '0');
			--*num;
		}
	}

	template<typename T>
	inline void print(T x, char ch = '\n') {
		print_(x);
		putchar(ch);
	}

	inline void printStr_(const char *s, int n = -1) {
		if (n == -1) {
			n = strlen(s);
		}
		for (int i = 0; i < n; ++i) {
			putchar(s[i]);
		}
	}

	inline void printStr(const char *s, int n = -1, char ch = '\n') {
		printStr_(s, n);
		putchar(ch);
	}
}
using namespace IO;

const char ch[] = "hi";

int n;
char s[15];

void solve() {
	n = readStr(s);
	if (n & 1) {
		printStr("No");
		return;
	}
	for (int i = 0; i < n; ++i) {
		if (s[i] != ch[i % 2]) {
			printStr("No");
			return;
		}
	}
	printStr("Yes");
}

int main() {
	int T = 1;
	// read(T);
	while (T--) {
		solve();
	}
}


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

int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    map<int, bool> m;
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        if(m[a]) m.erase(a);
        else m[a] = true;
    }
    cout << m.size() << "\n";
}

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

int main(void){
    long long N;cin>>N;
    std::vector<long long>A(N);
    for(long long i=0;i<N;i++){
        cin>>A[i];
    }
    sort(A.begin(),A.end());
    long long ans=0;
    long long tmp=0;
    for(long long i=1;i<N;i++){
        if(A[i]==A[i-1]){
            tmp++;
            if(i==N-1){
                if(tmp%2==0){
                    ans++;
                }
            }
        }
        else{
            if(tmp%2==0){
                ans++;
            }
            tmp=0;
        }
    }
    if(A[N-1]!=A[N-2]){
        ans++;
    }
    cout<<ans<<endl;
    return 0;
}


#pragma region header
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#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 rev(i, n) for(int i = (int)(n - 1); i >= 0; i--)
#define rev1(i, n) for(int i = (int)(n); i > 0; i--)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define resort(v) sort((v).rbegin(), (v).rend())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
using ll = long long;
using P = pair<int, int>;
/* ----------------よく使う数字や配列----------------- */
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
constexpr ll mod = 1e9+7;
constexpr int inf = INT32_MAX/2;
constexpr ll INF = LLONG_MAX/2;
constexpr long double eps = DBL_EPSILON;
constexpr long double pi = 3.141592653589793238462643383279;
/* ----------------------end----------------------- */

/* --------------------テンプレート------------------ */
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
/* ----------------------end----------------------- */

/* --------------------ライブラリ-------------------- */
ll fact(int i) {       //階乗
    if (i == 0) return 1;
    return (fact(i - 1)) * i % mod;
}
ll gcd(ll a, ll b) {        //最大公約数
    if(b == 0) return a;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) {      //最小公倍数
    return a / gcd(a, b) * b ;
}
int keta(ll n) {        //桁数を求める
    if(n == 0) return 1;
    int count = 0;
    while(n != 0) {
        n /= 10;
        count++;
    }
    return count;
}
ll ketasum(ll n) {    //各桁の和
    ll sum = 0;
    while(n != 0) {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}
bool isprime(int n) { //素数判定
    if(n==1)return false;
    if(n==2)return true;
    for(int i = 2;i<=sqrt(n);i++) {
        if(n%i==0) return false;
    }
    return true;
}
/* ----------------------end----------------------- */
#pragma endregion
vi prime_fact (int k) {
    vi v;
    for(int i = 2;i<=sqrt(k);i++) {
        if(k%i==0) {
            while(k%i==0) k/=i;
            v.pb(i);
        }
    }
    if(k!=1) v.pb(k);
    return v;
}
signed main() {
    int g;
    {
        int a,b;cin >> a >> b;
        g=gcd(a,b);
    }
    cout << prime_fact(g).size()+1 << endl;
    return 0;
}

#include <iostream>
#include <time.h>
#include <algorithm>
#include <stdio.h>
#include<string.h>

typedef long long LL;

using namespace std;

const int times = 20;
LL fac[1001];
int cnt;

LL mul(LL a, LL b, LL mod)
{
	LL ans = 0;
	while (b)
	{
		if (b & 1)
		{
			ans = (ans + a) % mod;
		}
		a = (a << 1) % mod;
		b >>= 1;
	}
	return ans;
}

LL pow(LL a, LL b, LL mod)
{
	LL ans = 1;
	while (b)
	{
		if (b & 1)
		{
			ans = mul(ans, a, mod);
		}
		b >>= 1;
		a = mul(a, a, mod);
	}
	return ans;
}

bool witness(LL a, LL n)
{
	LL temp = n - 1;
	int j = 0;
	while (temp % 2 == 0)
	{ // 其实就是得到 m
		j++;
		temp /= 2;
	}
	LL x = pow(a, temp, n);
	if (x == 1 || x == n - 1)
	{ // 判断a^m
		return true;
	}
	while (j--)
	{
		x = mul(x, x, n); // 进一步判断 a^(2m)  a^(4m) ...
		if (x == n - 1)
			return true;
	}
	return false;
}

bool miller_rabin(LL n)
{
	if (n == 2)
	{ //  如果是2肯定是素数
		return true;
	}
	if (n < 2 || n % 2 == 0)
	{ //如果小于2或者是大于2的偶数肯定不是素数
		return false;
	}
	for (int i = 0; i < times; i++)
	{ //随机化检验
		LL a = rand() % (n - 1) + 1;
		if (!witness(a, n))
			return false;
	}
	return true;
}

LL gcd(LL a, LL b)
{ // 这里的gcd和一般的gcd不一样
	if (a == 0)
	{ // pollard_rho的需要
		return 1;
	}
	if (a < 0)
	{ // 可能有负数
		return gcd(-a, b);
	}
	while (b)
	{
		LL t = a % b;
		a = b;
		b = t;
	}
	return a;
}

LL pollard_rho(LL n, LL c)
{                    // 找因子
	LL i = 1, k = 2; // 用来判断是否成环
	LL xx = rand() % n, y = xx;
	while (1)
	{
		i++;
		xx = (mul(xx, xx, n) + c) % n;
		LL d = gcd(y - xx, n);
		if (1 < d && d < n)
		{ // 找到一个因数
			return d;
		}
		if (y == xx)
		{ // 出现循环，那么查找失败
			return n;
		}
		if (i == k)
		{ // 相当一个优化？
			y = xx;
			k <<= 1;
		}
	}
}

void find(LL n)
{ // 通过因数来找质因子
	if (miller_rabin(n))
	{
		fac[cnt++] = n; // 记录质因子
		return;
	}
	LL p = n;
	while (p >= n)
		p = pollard_rho(p, rand() % (n - 1) + 1); //如果转了一圈还是p那么继续
	find(p);
	find(n / p);
}

int main()
{
	srand(251);
	LL a, b;
	cin>>a>>b;
    if(a==1||b==1){
        cout<<1;
        return 0;
    }
	if (miller_rabin(a))
	{
		if (b % a == 0)
			cout << 2;
		else
			cout << 1;
		return 0;
	}
	if (miller_rabin(b))
	{
		if (a % b)
			cout << 2;
		else
			cout << 1;
		return 0;
	}
	cnt = 0;
	find(a);
	LL fa[1001];
	memcpy(fa, fac, sizeof(fac));
	sort(fa, fa + cnt);
	int ss = cnt;
	cnt = 0;
	find(b);
	sort(fac, fac + cnt);
	int ans = 0;
	for (int i = 0; i < ss; i++) {
		if (i && fa[i] == fa[i - 1])continue;
		for (int j = 0; j < cnt; j++) {
			if (j && fac[j] == fac[j - 1])continue;
			if (fac[j] == fa[i])ans++;
		}
	}
	cout << ans+1;
	return 0;
}


#include <bits/stdc++.h>
#define lol long long
using namespace std;
int main(){
    int a,b,c;
    string s,t,q;
    cin>>a>>b;
    cout<<a+(b-a)*2<<"\n";
    return(0);
}

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

#define loop(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < n; ++i)
#define dump(a) cerr << #a << " = " << (a) << "(L:" << __LINE__ << ")" << endl

void solve(long long A, long long B) {
    cout << A * B << endl;
}

int main() {
    long long A;
    scanf("%lld", &A);
    long long B;
    scanf("%lld", &B);
    solve(A, B);
    return 0;
}


#include <bits/stdc++.h>
#include <limits>
#include <math.h>
#include <cmath>
#include <cctype>
#include <cstdio>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pll = pair<ll, ll> ;
using vs = vector<string>;
using vb = vector<bool>;
using vll = vector<long long>;
using vvll = vector<vector<long long >>;
#define PI  3.1415926535
#define rep(i, k, n) for(ll i = k; i < n; i++)

vector<vector<char>> maze; //迷路を表す文字列
vvll d; //距離
ll INF = 10000000000;

ll r, c;
ll dy[4] = {0, 1, 0, -1};
ll dx[4] = {1, 0, -1, 0};

ll bfs(){
  queue<pll> que;
  //全ての点をINFで初期化
  rep(i, 0, r){
    rep(j, 0, c){
      d[i][j] = INF;
    }
  }

  //スタート地点をキューに入れ、距離を0にする
  que.push(pll(0, 0));
  d[0][0] = 0;

  //キューが空になるまでループ
  while(que.size()){
    //キューの先頭を取り出す
    pll p = que.front();
    que.pop();

    //取り出してきたものがゴールなら探索終了
    if(p.first == r-1 && p.second == c-1){break;}

    //移動四方向ループ
    rep(i, 0, 4){
      //移動後の点(nx,ny)
      ll ny = p.first + dy[i];
      ll nx = p.second + dx[i];
      //移動可能か、すでに訪れているか判定
      if(ny >= 0 && ny < r && nx >= 0 && nx < c && d[ny][nx] == INF && maze[ny][nx] != '#'){
        //移動できるならキューに入れ、その点をpの距離＋１にする
        que.push(pll(ny, nx));
        d[ny][nx] = d[p.first][p.second] + 1;
      }
    }
  }

  return d[r-1][c-1];
}

int main(){
  cin >> r >> c;

  maze = vector<vector<char>> (r,vector<char>(c));
  d = vvll (r,vll(c,INF));
  ll num_akichi = 0;

  rep(i, 0, r){
    rep(j, 0, c){
      cin >> maze[i][j];
      if(maze[i][j] == '.'){num_akichi ++;}
    }
  }

  ll dist = bfs();

  ll score;

  if(dist == INF){score = -1;}
  else{score = num_akichi - dist - 1;}

  cout << score << endl;

}

#include<algorithm>
#include<bitset>
#include<cmath>
#include<complex>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<iterator>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;
typedef long long ll;

#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 ALL(x) (x).begin(),(x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define D()
#define INF 1000000000000
#define MOD 10000007
#define MAXR 100000
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define INITA(a,i,j,v) for(ll k=i;k<=j;k++){a[k]=v;}

int main() {
    int n; cin >> n;
    string s, t; cin >> s >> t;
    int res = 300;
    FOR(i, 0, n) {
        FOR(j, 0, n) {
            string conc = s.substr(0, i) + t.substr(j, n-j);
            if (conc.size() >= n && conc.substr(0, n) == s && conc.substr(conc.size()-n, n) == t) {
                res = min(res, (int)conc.size());
            }
        }
    }
    cout << res << endl;
    return 0;
}


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

const int maxn=100005;

inline int read(){
	register int x=0,w=1,ch=getchar();
	for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')w=-1;
	for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-48;
	return w*x;
}

int n,s,x[maxn];
long long p[maxn];

long long calc(int l,int r,int lst){
	if(s<x[l])return x[r]-s;
	if(x[r]<s)return s-x[l];
	if(p[l]>=p[r]){
		p[l]+=p[r];
		return calc(l,r-1,l)+(lst==r)*(x[r]-x[l]);
	}
	else{
		p[r]+=p[l];
		return calc(l+1,r,r)+(lst==l)*(x[r]-x[l]);
	}
}

int main(){
	n=read(),s=read();
	for(int i=1;i<=n;i++)
		x[i]=read(),p[i]=read();
	cout<<calc(1,n,p[1]<p[n]?1:n)<<'\n';
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e15;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
// __uint128_t
const ll MOD = 1e9+7;

int main(){
    ll H, W; cin >> H >> W;
    vector<string> S(H); for(int i = 0; i < H; i++) cin >> S[i];
    ll bs = 0;
    for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++) if(S[i][j] == '#') bs++;
    }

    vector<vector<ll>> V(H, vector<ll>(W, INF));
    queue<ll> qh, qw;
    qh.push(0); qw.push(0);
    V[0][0] = 0;
    int dh[] = {0, 1, 0, -1};
    int dw[] = {1, 0, -1, 0};

    while(!qh.empty()){
        ll h = qh.front(), w = qw.front();
        qh.pop(), qw.pop();
        for(int i = 0; i < 4; i++){
            ll nh = h+dh[i], nw = w+dw[i];
            if(0<=nh && nh < H && 0<=nw && nw<W && S[nh][nw] == '.' && V[h][w]+1<V[nh][nw]){
                qh.push(nh);
                qw.push(nw);
                V[nh][nw] = V[h][w]+1;
            }
        }
    }
    if(V[H-1][W-1] == INF) cout << -1 << endl;
    else cout << H*W-bs-V[H-1][W-1]-1 << endl;

}


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

char str[303]; int dp[303][303][303] , N , K;

int solve(int x , int y , int z){
	if(x > y) return 0;
	if(x == y) return 1;
	if(dp[x][y][z]) return dp[x][y][z];
	if(str[x] == str[y]) return dp[x][y][z] = solve(x + 1 , y - 1 , z) + 2;
	return dp[x][y][z] = max(solve(x + 1 , y , z) , max(solve(x , y - 1 , z) , z == 0 ? 0 : 2 + solve(x + 1 , y - 1 , z - 1)));
}

int main(){
	scanf("%s %d" , str + 1 , &K); N = strlen(str + 1);
	cout << solve(1 , N , K) << endl; return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define send {ios_base::sync_with_stdio(false);}
#define help {cin.tie(NULL); cout.tie(NULL);}
#define f first
#define s second
#define getunique(v) {sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());}
typedef long long ll;
typedef long double lld;
typedef unsigned long long ull;

template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v);
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
	cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";
}
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) {
	cin >> p.first;
	return cin >> p.second;
}

mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
// mt19937 rng(61378913);
/* usage - just do rng() */

void usaco(string filename) {
  // #pragma message("be careful, freopen may be wrong")
	freopen((filename + ".in").c_str(), "r", stdin);
	freopen((filename + ".out").c_str(), "w", stdout);
}

const lld pi = 3.14159265358979323846;
const ll mod = 1000000007;
// const ll mod = 998244353;
// ll mod;

ll n, m, k, q, l, r, x, y, z;
const ll template_array_size = 1e6 + 1389;
ll a[template_array_size];
ll b[template_array_size];
ll c[template_array_size];
string s, t;
ll ans = 0;

void solve(int tc = 0) {
	cin >> n;
	for (ll i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (ll i = 0; i < n; i++) {
		cin >> b[i];
	}

	reverse(b, b + n);

	ll lpt = 0, rpt = n - 1;

	for (ll i = 0; i < n; i++) {
		if (a[i] == b[i]) {
			if (b[lpt] != b[i] && a[lpt] != b[i]) {
				swap(b[i], b[lpt]);
				++lpt;
			} else if (b[rpt] != b[i] && a[rpt] != b[i]) {
				swap(b[i], b[rpt]);
				--rpt;
			} else {
				cout << "No\n";
				return;
			}
		}
	}

	for (ll i = 0; i < n; i++) {
		if (a[i] == b[i]) {
			cout << "No\n";
			return;
		}
	}

	cout << "Yes\n";
	for (ll i = 0; i < n; i++) {
		cout << b[i] << " ";
	}
	cout << '\n';
}

int main() {
	#ifdef galen_colin_local
		auto begin = std::chrono::high_resolution_clock::now();
	#endif

	send help

	#ifndef galen_colin_local
		// usaco("moop");
	#endif

	// usaco("cowland");

	// freopen("tc.cpp", "r", stdin);

	int tc = 1;
	// cin >> tc;
	for (int t = 0; t < tc; t++) solve(t);

	#ifdef galen_colin_local
		auto end = std::chrono::high_resolution_clock::now();
		cout << setprecision(4) << fixed;
		// cout << "Execution time: " << std::chrono::duration_cast<std::chrono::duration<double>>(end - begin).count() << " seconds" << endl;
	#endif
}

#include<map>
#include<set>
#include<bitset>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<chrono>
#include<stack>
#include<fstream>
#include<list>
#include<math.h>
#define REP(i,x,y) for(ll i=x;i<=y;i++)
#define SIZE(a) ll(a.size())
#define vll vector<ll>
#define MEMSET(a, n, m) for(ll i=0;i<=n;i++) a[i] = m
#define BIT(n) (ll(1)<<n)
#define UNIQUE(v) v.erase(unique(v.begin(),v.end()),v.end())
#define UNIQUE_ARRAY(a,x) unique(a + 1, a + x + 1) - a - 1
#define SORT(a,n) sort(a+1,a+n+1)
#define SORT_O(a,n,order) sort(a+1,a+n+1,order)
#define PER(i,y,x) for(ll i=y;i>=x;i--)
typedef long long ll;
using namespace std;
double const PI = 3.1415926535;

struct edge{
	ll from;  long long to; long long cost;
	bool operator<(const edge& rhs) const {
		return cost > rhs.cost;
	}
};

ll const MAX_UF = 200005;
ll parent[MAX_UF];
ll rk[MAX_UF];

void init(ll n) {
	for (ll i = 1; i <= n; i++) {
		parent[i] = i;
		rk[i] = 1;
	}
}

ll find(ll x) {
	if (parent[x] == x) {
		return x;
	}
	parent[x] = find(parent[x]);
	return parent[x];
}

bool same(ll x, ll y) {
	return find(x) == find(y);
}

void unite(ll x, ll y) {
	if (!same(x, y)) {
		x = parent[x];
		y = parent[y];
		if (rk[x] < rk[y]) {
			parent[x] = y;
		}
		else {
			parent[y] = x;
			if (rk[x] == rk[y]) {
				rk[x]++;
			}
		}
	}
}

ll const MAX_seg = 800005; //4倍に取っておく
ll segtree[MAX_seg];
ll mintree[MAX_seg];

//演算
ll op(ll x, ll y) {
	return min(x, y);
}
//init valueの変更を忘れない
ll const INIT_VALUE = 1e17;
ll n_ = 1;

void init_seg(ll n) {
	while (n_ <= n) {
		n_ *= 2;
	}
	REP(i, 1, n_ * 2) {
		segtree[i] = INIT_VALUE;
	}
	REP(i, n_ , n_ * 2) {
		mintree[i] = i - n_;
	}
	REP(i, 1, n_ - 1) {
		mintree[i] = mintree[i * 2];
	}
}

//k番目をxでupdate
void update(ll k, ll x) {
	k += n_;
	segtree[k] = x;
	while (k >= 1) {
		k /= 2;
		segtree[k] = op(segtree[k * 2], segtree[k * 2 + 1]);
		if (segtree[k * 2] > segtree[k * 2 + 1]) {
			mintree[k] = mintree[k * 2 + 1];
		}
		else {
			mintree[k] = mintree[k * 2];
		}
	}
}

ll val_o(ll a, ll b, ll k, ll l, ll r,ll& g) {
	if (a <= l && r <= b) {
		g = mintree[k];
		return segtree[k];
	}
	else if (r <= a || b <= l) {
		//ここの値注意
		return INIT_VALUE;
		g = mintree[k];
	}
	else {
		ll g1 = 0; ll g2 = 0;
		ll x1 = val_o(a, b, k * 2, l, (l + r) / 2, g1);
		ll x2 = val_o(a, b, k * 2 + 1, (l + r) / 2, r, g2);
		if (x1 > x2) {
			g = g2;
		}else{
			g = g1;
		}
		return op(x1, x2);
	}

}

//[a,b)のvalueを求める
ll val(ll a, ll b, ll& g) {
	return val_o(a, b, 1, 0, n_, g);
}

ll const MAX = 2e5;
ll n, d;
ll a[MAX];
ll ord[MAX];

bool orders(ll x, ll y) {
	return a[x] < a[y];
}

ll bignumber = 5e16;

priority_queue<edge> pq;

void makegraph(ll type) {
	init_seg(n);
	REP(i, 1, n) {
		ll cur = ord[i];
		if (i > 1) {
			ll min_i = 0;
			ll min_a;
			if (type == 1) {
				min_a = val(cur + 1, n + 1, min_i);
			}
			else {
				min_a = val(1, cur, min_i);
			}
		//	cout << type << " " << cur << " " << min_a << endl;
			if (min_a <= bignumber) {
				ll c;
				if (type == 1) {
					c = min_a - d * cur + a[cur];
				}
				else {
					c = min_a + d * cur + a[cur];
				}
				pq.push({ min_i,cur,c });
			}
		}
		ll t = a[cur] + type * cur * d;
		update(cur, t);
	//	cout << "?" << cur << " " << t << endl;
	}
}

int main() {
	cin >> n >> d;
	REP(i, 1, n) {
		cin >> a[i];
	}
	REP(i, 1, n) {
		ord[i] = i;
	}
	SORT_O(ord, n, orders);
	makegraph(1);
	makegraph(-1);
	ll ans = 0;
	init(n);
	while (!pq.empty()) {
		edge cur = pq.top();
		pq.pop();
		//cout << "!" << cur.cost << endl;
		if (!same(cur.from, cur.to)) {
			ans += cur.cost;
			unite(cur.from, cur.to);
		}
	}
	cout << ans << endl;
}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
#include <iomanip>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

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

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }
#define INF 1LL<<60

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

    set<ll> numbers;
    map<ll, ll> map_numbers;
    REP(i, N) {
        ll tmp;
        cin >> tmp;
        numbers.insert(tmp);
        map_numbers[tmp]++;
    }

    ll ans = 0;
    FORR(t, 37, 1) {
        ll x = pow(2, t-1);
        ll y = pow(2, t);

        if(numbers.size() == 0) break;
        ll max = *numbers.rbegin();
        if(max < x) continue;

        set<ll> to_remove;
        for(auto a = numbers.rbegin(); a != numbers.rend(); ++a) {
            if(map_numbers[*a]==0) continue;
            if(*a < x) break;
            if(map_numbers[y - *a]>0 && *a != y - *a) {
                ll num_pair = min(map_numbers[*a], map_numbers[y-*a]);
                ans+=num_pair;
                map_numbers[y-*a]-=num_pair;
                map_numbers[*a]-=num_pair;
                if(map_numbers[*a] == 0)
                    to_remove.insert(*a);
                if(map_numbers[y-*a] == 0)
                    to_remove.insert(y-*a);
            } else if(map_numbers[y - *a]>=2 && *a == y - *a) {
                ll num_pair = map_numbers[*a] / 2;
                ans+=num_pair;
                map_numbers[*a]-=num_pair;
                if(map_numbers[*a] == 0)
                    to_remove.insert(*a);
            } else {
                map_numbers[*a] = 0;
                to_remove.insert(*a);
            }
        }

        for(ll r: to_remove) {
            numbers.erase(numbers.find(r));
        }
    }

    cout << ans << endl;

    return 0;
}

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

using ll=long long;
#define rng(i,l,r) for(int i=int(l);i<int(r);i++)
#define rep(i,r) rng(i,0,r)
#define rrng(i,l,r) for(int i=int(r)-1;i>=int(l);i--)
#define rrep(i,r) rrng(i,0,r)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#define inf INT_MAX/2-100
#define infl LLONG_MAX/3
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<' '<<#x<<' '<<x<<endl
#else
#define dmp(x) void(0)
#endif

template<class t,class u>bool chmax(t&a,u b){if(a<b)a=b;return a<b;}
template<class t,class u>bool chmin(t&a,u b){if(b<a)a=b;return b<a;}

template<class t>using vc=vector<t>;
template<class t>using vvc=vector<vector<t>>;

using pi=pair<int,int>;
using pl=pair<ll,ll>;
using vi=vc<int>;
using vl=vc<ll>;

ll readl(void){
	ll x;
	cin>>x;
	return x;
}
int readi(void){
	int x;
	cin>>x;
	return x;
}

string readstr(){
	string s;
	cin>>s;
	return s;
}
vi readvi(int n,int off=0){
	vi v(n);
	rep(i,n)v[i]=readi(),v[i]+=off;
	return v;
}

vl readvl(int n,int off=0){
	vl v(n);
	rep(i,n)v[i]=readl(),v[i]+=off;
	return v;
}

template<class t>
void print(t x,int suc=1){
	cout<<x;
	if(suc==1)cout<<"\n";
	if(suc==2)cout<<" ";
}

template<class t>
void print(const vc<t>&v,int suc=1){
	rep(i,si(v))print(v[i],i==int(si(v))-1?1:suc);
}

template<class t>
bool inc(t a,t b,t c){
	return !(c<b||b<a);
}

template<class t>
void compress(vc<t>&v){
	sort(all(v));
	v.erase(unique(all(v)),v.ed);
}

template<class t>
int lwb(const vc<t>&v,const t&a){
	return lower_bound(all(v),a)-v.bg;
}

template<class t>
struct Compress{
	vc<t>v;
	Compress()=default;
	Compress(const vc<t>&x){
		add(x);
	}
	Compress(const initializer_list<vc<t> >&x){
		for(auto &p:x)add(p);
	}
	void add(const t&x){
		v.eb(x);
	}
	void add(const vc<t>&x){
		copy(all(x),back_inserter(v));
	}
	void build(){
		compress(v);
	}
	int get(const t&x)const{
		return lwb(v,x);
	}
	vc<t>get(const vc<t>&x)const{
		vc<t>res(x);
		for(auto &p:res)p=get(p);
		return res;
	}
	const t &operator[](int x)const{
		return v[x];
	}
	int size(){
		return v.size();
	}
};
void Yes(bool ex=true){
	cout<<"Yes\n";
	if(ex)exit(0);
}
void YES(bool ex=true){
	cout<<"YES\n";
	if(ex)exit(0);
}
void No(bool ex=true){
	cout<<"No\n";
	if(ex)exit(0);
}
void NO(bool ex=true){
	cout<<"NO\n";
	if(ex)exit(0);
}
void orYes(bool x,bool ex=true){
	if(x)Yes(ex);
	else No(ex);
}
void orYES(bool x,bool ex=true){
	if(x)YES(ex);
	else NO(ex);
}
void Possible(bool ex=true){
	cout<<"Possible\n";
	if(ex)exit(0);
}
void POSSIBLE(bool ex=true){
	cout<<"POSSIBLE\n";
	if(ex)exit(0);
}
void Impossible(bool ex=true){
	cout<<"Impossible\n";
	if(ex)exit(0);
}
void IMPOSSIBLE(bool ex=true){
	cout<<"IMPOSSIBLE\n";
	if(ex)exit(0);
}
void orPossible(bool x,bool ex=true){
	if(x)Possible(ex);
	else Impossible(ex);
}
void orPOSSIBLE(bool x,bool ex=true){
	if(x)POSSIBLE(ex);
	else IMPOSSIBLE(ex);
}
ll n,m,vis[100005],ans,e,o,s,r;
vl g[100005];
bool dfs(int v,int k){
	bool res=true;
	vis[v]=k;
	s++;
	for(auto &i:g[v]){
		if(vis[i]==0){
			res&=dfs(i,k*-1);
		}else if(vis[i]==k){
			res=false;
		}
	}
	return res;
}
int main(void){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cin>>n>>m;
	rep(i,m){
		ll a,b;
		cin>>a>>b;
		g[--a].pb(--b);
		g[b].pb(a);
	}
	fill(vis,vis+n,0);
	r=n;
	rep(i,n){
		if(vis[i]==0){
			s=0;
			bool bd=dfs(i,1);
			if(s==1)ans+=r*2LL-1LL,r--;
			else if(bd)e++;
			else o++;
		}
	}
	ans+=o*o;
	ans+=e*o*2LL;
	ans+=e*e*2LL;
	print(ans);
}


#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
typedef vector<ll> vel;
typedef vector<str> ves;
#define REP(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, b) REP(i, 0, b)
const ll mod = 1000000007;
const double pi = 3.14159265358979;

ll expo(ll a, ll b){
    ll ex = 1;
    rep(i, b){
        ex *= a;
    }
    return ex;
}

ll fact(ll a){
    ll fc = 1;
    REP(i, 1, a+1){
        fc *= i;
    }
    return fc;
}

int main() {
    ll a, b;
    cin >> a >> b;
    cout << max(a+b, max(a-b, a*b)) << endl;
}

#include <iostream>
#include <utility>
#include <map>
#include <vector>
#include <algorithm>
#include <queue>
#include <math.h>
#include <stack>
#include <set>
#include <deque>
#include <cmath>
#include <cassert>

using namespace std;
typedef long long ll;
ll mod = 1e9+7;
#define rep(i,n) for(int i = 0; i < (n); ++i)

int main() {
    int a, b;
    cin >> a >> b;
    int d = 0;
    rep(i, b) {
        int c;
        cin >> c;
        d = max(d, c);
    }
    cout << max(0, d-(a-d)-1) << endl;

    return 0;
}


#include <bits/stdc++.h>

#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)

using namespace std;

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

int main() {
  int n, x;
  cin >> n >> x;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);
  sort(a.begin(), a.end());
  int res = 0;
  rep(i, n) {
    int aa = a.at(i);
    if (aa > x) {
      break;
    }
    if (i == n - 1) {
      if (aa != x) {
        break;
      }
    }
    x -= aa;
    res++;
  }
  cout << res << endl;
  return 0;
}

#include <bits/stdc++.h>
#include <set>
using namespace std;
using pii = pair<int,int>;
using pll = pair<long long, long long>;
const long long INF = 1<<29;

const int MOD = 1000000007;
long long ruizyou(long long m,long long n){
  if(m == 0)return 0;
  if(m == 1)return 1;
  long long ans = 1;
  long long tmp = m;
  for(int i=0;i<=30;i++){
    if(n & (1<<i)){
      ans *= tmp;
      ans %= MOD;
    }
    tmp *= tmp;
    tmp %= MOD;
  }
  return ans;
}
long long kaizyou(long long x){
  if(x == 0)return 1;
  return x * kaizyou(x-1) % MOD;
}
long long comb(long long x,long long y){
  long long bunsi = kaizyou(x);
  long long bunbo = kaizyou(x-y) * kaizyou(y) % MOD;
  return bunsi * ruizyou(bunbo,MOD-2) % MOD;
}
struct unionfind{
  vector<int> par;
  vector<vector<int>> children;
  vector<int> hukasa;
  vector<int> kazu;
  unionfind(int n){
    par = vector<int>(n);
    hukasa = vector<int>(n,0);
    kazu = vector<int>(n,1);
    for(int i=0;i<n;i++){
      par.at(i) = i;
    }
  }
  int root(int x){
    if(par.at(x) == x)return x;
    return root(par.at(x));
  }
  void unite(int x,int y){
    int rx = root(x);
    int ry = root(y);
    if(rx == ry)return;
    if(hukasa.at(rx) >= hukasa.at(ry)){
      par.at(ry) = rx;
      hukasa.at(rx) = max(hukasa.at(ry) + 1,hukasa.at(rx));
      kazu.at(rx) += kazu.at(ry);
    }
    else{
      par.at(rx) = ry;
      kazu.at(ry) += kazu.at(rx);
    }
  }
  void newkazu(){
    int n = kazu.size();
    for(int i=0;i<n;i++){
      kazu.at(i) = kazu.at(root(i));
    }
  }
  bool same(int x,int y){
    return root(x) == root(y);
  }
};
int ctoi(char a){
  return a - '0';
}
long long gcd(long long a,long long b){
  long long c = max(a,b);
  long long d = min(a,b);
  if(d % c == 0)return c;
  return gcd(c,d%c);
}
long long lcm(long long a,long long b){
  return a * b / gcd(a,b);
}
void hukasa(vector<vector<int>> s,vector<vector<int>> table,int &ans,pii frm,int n,int m){
  int x,y;
  tie(x,y) = frm;
  set<pii> se;
  if(x-1 >= 0)se.insert(pii(x-1,y));
  if(x+1 < n)se.insert(pii(x+1,y));
  if(y-1 >= 0)se.insert(pii(x,y-1));
  if(y+1 < m)se.insert(pii(x,y+1));
  for(auto t:se){
    int tx,ty;
    tie(tx,ty) = t;
    if(s.at(tx).at(ty) && table.at(tx).at(ty) == -1){
      table.at(tx).at(ty) = table.at(x).at(y) + 1;
      ans = max(ans,table.at(tx).at(ty));
      hukasa(s,table,ans,t,n,m);
      table.at(tx).at(ty) = -1;
    }
  }
}
void daiku(int frm,vector<vector<pii>> g,vector<int> &cost,int n){
  priority_queue<pii,vector<pii>,greater<pii>> q;
  q.push(pii(cost.at(frm),frm));
  vector<bool> kakutei(n,false);
  while(!q.empty()){
    pii now = q.top();
    q.pop();
    int n1,n2;
    tie(n1,n2) = now;
    if(kakutei.at(n2))continue;
    kakutei.at(n2) = true;
    for(auto t:g.at(n2)){
      int tx,ty;
      tie(tx,ty) = t;
      if(cost.at(tx) > n1 + ty){
        cost.at(tx) = n1 + ty;
        q.push(pii(cost.at(tx),tx));
      }
    }
  }
}

int main(){
  int n;
  string s;
  cin >> n >> s;
  vector<int> r(n+1,0),g(n+1,0),b(n+1,0);
  for(int i=0;i<n;i++){
    if(s.at(i) == 'R')r.at(i)++;
    else if(s.at(i) == 'G')g.at(i)++;
    else b.at(i)++;
  }
  for(int i=n-2;i>=0;i--){
    r.at(i) += r.at(i+1);
    g.at(i) += g.at(i+1);
    b.at(i) += b.at(i+1);
  }
  long long ans = 0;
  for(int i=0;i<n-2;i++){
    for(int j=i+1;j<n-1;j++){
      if(s.at(i) == s.at(j))continue;
      unordered_set<char> se;
      se.insert(s.at(i));
      se.insert(s.at(j));
      char c;
      if(!se.count('R')){
        ans += r.at(j+1);
        if(j+(j-i) < n && s.at(j+(j-i)) == 'R')ans--;
      }
      else if(!se.count('G')){
        ans += g.at(j+1);
        if(j+(j-i) < n && s.at(j+(j-i)) == 'G')ans--;
      }
      else{
        ans += b.at(j+1);
        if(j+(j-i) < n && s.at(j+(j-i)) == 'B')ans--;
      }
    }
  }
  cout << ans << endl;
}


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

int rr[8], cc[8];
int r[8], c[8];

int main(){
  int k;
  cin >> k;
  int count=0;
  for(int i=0;i<k;i++) cin >> r[i] >> c[i];

  for(int co=0;co<pow(8,8-k);co++)
  {
    for(int i=0;i<8;i++)
    {
      rr[i]=-1;
      cc[i]=-1;
    }
    //無理やり配置を用意して

    for(int i=0;i<8;i++)
    {
      rr[i]=i;
      if(i<k) cc[r[i]]=c[i];
    }
    int hoge=0;
    for(int i=0;i<8;i++)
    {
      int a=pow(8,hoge);
      if(cc[i]==-1)
      {
        cc[i]=co%(a*8)/a;
        hoge++;
      }
    }
    //襲撃をtrueで埋めて
    bool masu[8][8]{false};
    for(int i=0;i<8;i++)
    {
      for(int j=1;j<8;j++)
      {
        if(cc[i]-j>=0) masu[rr[i]][cc[i]-j]=true;
        if(cc[i]+j<=7) masu[rr[i]][cc[i]+j]=true;
        if(rr[i]-j>=0) masu[rr[i]-j][cc[i]]=true;
        if(rr[i]+j<=7) masu[rr[i]+j][cc[i]]=true;

        if(rr[i]-j>=0&&cc[i]-j>=0) masu[rr[i]-j][cc[i]-j]=true;
        if(rr[i]+j<=7&&cc[i]-j>=0) masu[rr[i]+j][cc[i]-j]=true;
        if(rr[i]-j>=0&&cc[i]+j<=7) masu[rr[i]-j][cc[i]+j]=true;
        if(rr[i]+j<=7&&cc[i]+j<=7) masu[rr[i]+j][cc[i]+j]=true;
      }
    }

    count=0;
    for(int j=0;j<8;j++)
    {
      if(masu[rr[j]][cc[j]]==false) count++;
    }
    //cout << count << endl;
    if(count==8)
    {
      count=0;

      for(int j=0;j<k;j++)
      {
        if(c[j]==cc[r[j]]) count++;
      }

      if(count==k)
      {
        break;
        //return 0;
      }
    }
  }
  for(int i=0;i<8;i++)
        {
          for(int j=0;j<8;j++)
          {
            if(cc[i]==j) cout << 'Q';
            else cout <<'.';
          }
          cout << endl;
        }
  //cout << "hoge"<< endl;
  return 0;
}

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

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

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

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

bool ok(int x, int y) {
    if (x < 0 || x >= 8 || y < 0 || y >= 8) return false;
    return true;
}

bool check(int nowmap[8][8]) {
    int count = 0;
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            if (nowmap[i][j] == 1) {
                count++;
            }
        }
    }
    if (count >= 9) {
        return false;
    }
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            if (nowmap[i][j] == 1) {
                int dx[] = {1,1,-1,-1};
                int dy[] = {1,-1,-1,1};
                for (int k = 0; k < 4; k++) {
                    int px = j;
                    int py = i;
                    while(ok(px + dx[k], py + dy[k])) {
                        px += dx[k];
                        py += dy[k];
                        if (nowmap[py][px] == 1) {
                            return false;
                        }
                    }
                }
            }
        }
    }
    return true;
}

void print(int nowmap[8][8]) {
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            if (nowmap[i][j] == 1) {
                cout << "Q";
            } else {
                cout << ".";
            }
        }
        cout << endl;
    }
}

int main(int argc, char const *argv[]) {
    int N;
    cin >> N;
    int map[8][8] = {{0}};
    for (int i = 0; i < N; i++) {
        int a, b;
        cin >> a >> b;
        map[a][b] = 1;
    }

    std::vector<int> perm;
    for (int i = 0; i < 8; i++) {
        perm.push_back(i);
    }

    do {
        int nowmap[8][8] = {{0}};
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                nowmap[i][j] = map[i][j];
            }
        }
        for (int i = 0; i < 8; i++) {
            int x = i;
            int y = perm[i];

            nowmap[y][x] = 1;

        }
        if (check(nowmap)) {
            print(nowmap);
            return 0;
        }
    } while(next_permutation(perm.begin(), perm.end()));

    return 0;
}


// include
// ------------------------------------------------
#include <bits/stdc++.h>
#include <algorithm>
#include <math.h>

using namespace std;

// func
// ------------------------------------------------
int CalcSumOfDigit(int n);      // 各桁の和を計算する。
int getDigit(int n);            // 数字の桁数を取得する。
string upper(string str);       // 英字を大文字に変換する。
string lower(string str);       // 英字を小文字に変換する。

// class
// ------------------------------------------------
class Combi
{
  public:
    Combi();
    long long Combination(long long n, long long k);
  private:
    vector<vector<long long>> memo;
    long long n_num;
    long long k_num;
    void Resize(long long n, long long k);
};

// define
// ------------------------------------------------
#define all(a)        (a).begin(),(a).end()
#define rall(a)       (a).rbegin(),(a).rend()
#define sz(a)         int((a).size())
#define rep(i,n)      for(int(i)=0;(i)<(n);(i)++)
#define repe(i,n)     for(int(i)=0;(i)<=(n);(i)++)
#define vsort(v)      sort((v).begin(),(v).end())
#define rvsort(v)     sort(rall((v)))
#define vi            vector<int>
#define GCD(a,b)      __gcd((a),(b))
#define LCM(a,b)      (a)/GCD((a),(b))*(b)
#define kiriage(a,b)  ((a)+(b)-1)/(b)

const int INF = 1e9;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<long> vll;
// code
// ------------------------------------------------
int main() {

  ll x,y;
  cin >> x >> y;

  ll ans = 0;

  if(x * y > 0)
  {
    ans = abs(y-x) + ( x < y ? 0 : 2);
  }
  else if(x == 0)
  {
    ans = abs(y) + ( y < 0 ? 1 : 0);
  }
  else if(y == 0)
  {
    ans = abs(x) + ( x < 0 ? 0 : 1);
  }
  else
  {
    ans = abs( y + x ) + 1;
  }

  cout << ans << endl;
  return 0;
}
// funcの実体
// ------------------------------------------------
int getDigit(int n)
{
  int i = 1;
  while(1)
  {
    n = n / 10;
    if(n == 1)
      break;
    i++;
  }
  return i;
}
int CalcSumOfDigit(int n)
{
  int s = 0;
  while(n)
  {
    s += n % 10;
    n = n / 10;
  }
  return s;
}

string upper(string str)
{
  for(auto itr = str.begin();itr != str.end() ; itr++)
  {
    if(97 <= *itr && *itr <= 122)
    {
      *itr = *itr - 32;
    }
  }
  return str;
}

string lower(string str)
{
  for(auto itr = str.begin();itr != str.end() ; itr++)
  {
    if(65 <= *itr && *itr <= 90)
    {
      *itr = *itr + 32;
    }
  }
  return str;
}

Combi::Combi(){
  n_num = -1;
  k_num = -1;
};
ll Combi::Combination(ll n, ll k)
{
  Resize(n,k);

  ll ret;
  if(memo[n][k] != 0)
  {
    ret = memo[n][k];
  }
  else if(n == k || k == 0)
  {
    memo[n][k] = 1;
    ret = 1;
  }
  else
  {
    ret = Combination(n - 1, k - 1) + Combination(n - 1, k);
    memo[n][k] = ret;
  }
  return ret;
}

void Combi::Resize(ll n, ll k)
{
  if(n_num <= n || k_num <= k)
  {
    n_num = (n + 1) * 2;
    k_num = (k + 1) * 2;
    memo.resize(n_num);
    for(auto itr = memo.begin(); itr != memo.end(); ++itr)
    {
      itr->resize(k_num);
    }
  }
}

#include <bits/stdc++.h>
using namespace std;
int main(){
  int x, y;
  cin >> x >> y;
  if (x == 0){
    if (y >= 0){
      cout << y << endl;
    } else {
      cout << 1 - y << endl;
    }
  } else if (y == 0){
    if (x <= 0){
      cout << -x << endl;
    } else {
      cout << x + 1 << endl;
    }
  } else if (x <= y){
    if (x >= 0 && y >= 0){
      cout << y - x << endl;
    } else if (x <= 0 && y <= 0){
      cout << y - x << endl;
    } else if (abs(x) <= abs(y)){
      cout << y + x + 1 << endl;
    } else {
      cout << -x - y + 1 << endl;
    }
  } else {
    if (x >= 0 && y >= 0){
      cout << x - y + 2 << endl;
    } else if (y <= x && x <= 0){
      cout << x - y + 2 << endl;
    } else if (abs(x) <= abs(y)){
      cout << -x - y + 1 << endl;
    } else {
      cout << x + y + 1 << endl;
    }
  }
}


#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <functional>
#include <numeric>
#include <iomanip>
#include <climits>
#include <cstring>
#include <cmath>

using namespace std;

#define int long long
#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 rrep(i, n) for (int i=(int)(n)-1;i>=0;--i)
#define rrep1(i, n) for (int i=(int)(n);i>=1;--i)
#define range(i, l, r) for (int i=l;i<(int)(r);++i)
#define rrange(i, l, r) for (int i=(int)(r)-1;i>=l;--i)
#define unless(a) if(!(a))
#define all(a) begin(a),end(a)
#define fst first
#define scd second
#define PB emplace_back
#define PPB pop_back

using vi=vector<int>;
using pii=pair<int, int>;
using vpii=vector<pii>;
using ll=long long;

constexpr int TEN(int n){return n==0?1:10*TEN(n-1);}
template<typename T>bool chmin(T&a,T b){return a>b?(a=b,1):0;}
template<typename T>bool chmax(int&a,int b){return a<b?(a=b,1):0;}
int read(){int a;scanf("%lld",&a);return a;}

const double pi = acos(-1);
constexpr int inf = 3*TEN(18) + 10;

signed main()
{
	int N,A,B; cin >> N >> A >> B;
	vi X(N);
	rep(i, N) cin >> X[i];

	int ans = 0, cur = X[0];
	rep(i, N - 1) {
		ans += min((X[i + 1] - cur) * A, B);
		cur = X[i + 1];
	}

	cout << ans << endl;
}

#pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int cm = 1 << 17;
char cn[cm], * ci = cn + cm, ct;
inline char getcha() {
	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }
	return *ci++;
}
inline int getint() {
	int A = 0;
	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';
	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';
	return A;
}
const int dm = 1400000;
char dn[dm], * di = dn;
//const int MAX = 1000;
//class unko {
//public:
//	char C[MAX * 6 + 10];
//	int K[MAX + 10];
//	constexpr unko() : C(), K() {
//		auto di = C + 1;
//		C[0] = '0';
//		K[0] = 0;
//		K[1] = 1;
//		rep1(i, MAX) {
//			int X = i;
//			int keta = 0;
//			char c[10] = {};
//			while (X) {
//				*(c + keta) = '0' + X % 10;
//				X /= 10;
//				keta++;
//			}
//			for (int j = keta - 1; j >= 0; j--)* di++ = (*(c + j));
//			K[i + 1] = K[i] + keta;
//		}
//	}
//};
//constexpr unko f;
//inline void putint(ll X) {
//	int ue = X / 1000;
//	if (ue) {
//		for (int i = f.K[ue]; i < f.K[ue + 1]; i++)* di++ = f.C[i];
//		int sita = X - ue * 1000;
//		rep(i, 3 - f.K[sita + 1] + f.K[sita])* di++ = '0';
//		for (int i = f.K[sita]; i < f.K[sita + 1]; i++)* di++ = f.C[i];
//	}
//	else for (int i = f.K[X]; i < f.K[X + 1]; i++)* di++ = f.C[i];
//	*di++ = '\n';
//}
inline void putint(int X) {
	if (X == 0) {
		*di++ = '0';
		*di++ = '\n';
		return;
	}
	int keta = 0;
	char C[10];
	while (X) {
		*(C + keta) = '0' + X % 10;
		X /= 10;
		keta++;
	}
	for (int i = keta - 1; i >= 0; i--)* di++ = (*(C + i));
	*di++ = '\n';
}

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

	int N = getint(), K = getint();

	int A[200001];
	rep(i, N) A[i] = getint();
	ll owa = ll(N) * N;

	rep(p, K) {
		int B[200001] = {};
		rep(i, N) {
			B[max(0, i - A[i])]++;
			B[min(N, i + A[i] + 1)]--;
		}
		ll kei = B[0];
		A[0] = B[0];
		rep1(i, N - 1) {
			kei +=(A[i] = A[i - 1] + B[i]);
		}
		if (kei == owa) break;
	}

	rep(i, N) putint(A[i]);
	fwrite(dn, 1, di - dn, stdout);

	Would you please return 0;
}


#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,b,a) for(int i=b;i>=a;i--)
#define fori(a) for(auto i : a )
#define all(a) begin(a), end(a)
#define set(a,b) memset(a,b,sizeof(a))
#define sz(a) a.size()
#define pi 3.14159
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PF push_front //deque
#define mp make_pair
#define pq priority_queue
#define mod 1000000007
#define f first
#define s second
#define pii pair< int, int >
#define vi vector<int>
#define vpii vector<pii>
#define debug(v) for(auto i:v) cout<<i<<" ";
#define tc int t; cin >> t; while(t--)

using namespace std;
string repeat(string s, int n) {
    string s1 = "";
    for (int i=0; i<n;i++)
        s1+=s;
    return s1;
}
string getString(char x) {
    string s(1, x);
    return s;
}

void optimizeIO(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
int gcd(int a, int b){
    if (a == 0)  return b;
    return gcd(b % a, a);
}
void solve(){
  int n;
  cin>>n;
  int a[n+2];
  rep(i,1,n+1) cin>>a[i];
  a[0]=0;
  a[n+1]=0;
  // int mn=*min_element(a,a+n),mx=*max_element(a,a+n);
  int v1[n+1],pref[n+1];
  rep(i,0,n+1) v1[i]=abs(a[i+1]-a[i]);//a[i+1]*a[i]>=0?abs(a[i+1]-a[i]):abs(a[i+1]-a[i])*2;
  pref[0]=v1[0];
  rep(i,1,n+1) pref[i]=pref[i-1]+v1[i];
  rep(i,1,n+1){
    ll ans=abs(a[i+1]-a[i-1])-abs(a[i]-a[i-1])-abs(a[i+1]-a[i])+pref[n];
    cout<<ans<<endl;
  }
}
int main(){
    optimizeIO();
    // tc
    { solve();
    }
}

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define REP(i,n) for (int64_t i=0; i<(n); ++i)
#define P pair<int,int>
using ll=int64_t;
using namespace std;
#define ketasuu(n) fixed<<setprecision(n)
#define btoe(p) p.begin(),p.end()
#define etob(p) p.rbegin(),p.rend()

int main(){
    int n; cin>>n;
    vector<int> p(n);
    rep(i,n) cin>>p[i];
    int sum=0;
    rep(i,n-1){
        sum+=abs(p[i]-p[i+1]);
    }
    sum+=abs(p[0]); sum+=abs(p[n-1]);
    if(p[0]*(p[1]-p[0])>=0){
        cout<<sum<<endl;
    }else{
        cout<<sum-2*min(abs(p[0]), abs(p[1]-p[0]))<<endl;
    }
    for(int i=1; i<n-1; ++i){
        if((p[i-1]-p[i])*(p[i]-p[i+1])>=0){
            cout<<sum<<endl;
        }else{
            cout<<sum-2*min(abs(p[i-1]-p[i]), abs(p[i]-p[i+1]))<<endl;
        }
    }
    if((p[n-2]-p[n-1])*p[n-1]>=0){
        cout<<sum<<endl;
    }else{
        cout<<sum-2*min(abs(p[n-2]-p[n-1]), abs(p[n-1]))<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7;

int n;
vector<int> adj[N];

vector<int> bfs(int src){
    vector<int> vis(n + 2, 0);
    vector<int> dis(n + 1, 0);
    vis[src] = 1;
    dis[src] = 0;
    queue<int> q;
    q.push(src);
    while(q.size()){
        int node = q.front();
        q.pop();
        for(auto i: adj[node]){
            if(!vis[i]){
                vis[i] = 1;
                dis[i] = dis[node] + 1;
                q.push(i);
            }
        }
    }
    return dis;
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int pos_a, pos_b;
    cin >> n >> pos_a >> pos_b;
    if(pos_a == pos_b) return cout << 0, 0;
    for(int i = 1; i < n; i++){
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    auto dis_a = bfs(pos_a);
    auto dis_b = bfs(pos_b);
    int ans = 0;
    for(int i = 1; i <= n; i++){
      if(dis_a[i] < dis_b[i]) ans = max(ans, dis_b[i] - 1);
    }
    cout << ans;

    return 0;
}

#include<stdio.h>

int main(){
	int N;
	int n1;
	int M;
	int m1;
	int k;
	scanf("%d %d %d %d %d",&N ,&n1,&M,&m1,&k );
	int waktu = ((M*60+m1)-(N*60+n1))-k;
	printf ("%d",waktu);

	return 0;
}


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

string query(int n) {
	cout << n << '\n' << flush;
	string s; cin >> s;
	if (s == "Vacant") exit(0);
	return s;
}

int main() {
	int n; cin >> n;
	int l = 0;
	int r = n - 1;
	string L = query(l);
	string R = query(r);
	while (true) {
		int m = (l + r) / 2;
		R = query(m);
		if ((L == R) ^ (m % 2)) l = m;
		else r = m;
	}
	return 0;
}

// This amazing code is by Eric Sunli Chen.
#include<bits/stdc++.h>
using namespace std;
template<typename T> bool get_int(T &x)
{
	char t=getchar();
	bool neg=false;
	x=0;
	for(; (t>'9'||t<'0')&&t!='-'&&t!=EOF; t=getchar());
	if(t=='-')neg=true,t=getchar();if(t==EOF)return false;
	for(; t<='9'&&t>='0'; t=getchar())x=x*10+t-'0';
	if(neg)x=-x;return true;
}
template<typename T> void print_int(T x)
{
	if(x<0)putchar('-'),x=-x;
	short a[20]= {},sz=0;
	while(x>0)a[sz++]=x%10,x/=10;
	if(sz==0)putchar('0');
	for(int i=sz-1; i>=0; i--)putchar('0'+a[i]);
}
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define get1(a) get_int(a)
#define get2(a,b) (get1(a)&&get1(b))
#define get3(a,b,c) (get1(a)&&get2(b,c))
#define printendl(a) print_int(a),puts("")
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f;
const LL Linf=1ll<<61;
const double pi=acos(-1.0);

bool P(int x){for(int i=2;i*i<=x;i++)if(x%i==0)return false;return true;}

int p[1111],p1[1111],p2[1111],tot,n;

LL a[555][555];

int main()
{
	for(int i=2;i<=10000&&tot<1002;i++)if(P(i))p[++tot]=i;
	for(int i=1;i<=501;i++)p1[i*2-1]=p[i];
	for(int i=1;i<=501;i++)p2[i*2-1]=p[i+501];
	for(int i=1;i<=501;i++)for(int j=1;j<=501;j++)
	{
		int k1=i+j-1,k2=i-j+501;
		a[i][j]=1;
		if(k1%2==1)a[i][j]*=p1[k1];else a[i][j]*=p1[k1-1]*p1[k1+1];
		if(k2%2==1)a[i][j]*=p2[k2];else a[i][j]*=p2[k2-1]*p2[k2+1];
		if(k1%2==0)a[i][j]++;
	}

	get1(n);
	for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)printf("%lld%c",a[i][j],j==n?'\n':' ');
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define endl "\n"
#define F first
#define S second
#define mod 1000000007
#define pb push_back
#define FOR(i,a,n) for(int i=a;i<n;i++)
#define REV(i,a,n) for(int i=a;i>=n;i--)
#define all(a) a.begin(),a.end()
const int NUM = 2e5 + 5;

int32_t main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int num_tests=1;
    //cin >> num_tests;
    while(num_tests-->0){
        int n; cin >> n;
        int ans = 0;
        while(n >= 500)
        {
            int x = n/500;
            ans += x*1000;
            n -= (x)*500;
        }
        while(n >= 5)
        {
            int y = n/5;
            ans += y*5;
            n -= y*5;
        }
        cout << ans;
    }
}

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

#define fast_io ios_base::sync_with_stdio(false) ; cin.tie(0); cout.tie(0);

int main() {
    fast_io;
    long long x;
    cin>>x;

    long long thousand=(x/500)*1000;

    long long rest=(x%500)/5*5;;

    long long ans=thousand+rest;
    cout<<ans<<endl;
}


#include<bits/stdc++.h>

using namespace std;
const int MAXN=10005;
int P=1e9+7,d,len;
char k[MAXN];
long long a[MAXN],dp[10005][105];

long long dfs(int pos,int tot,bool lim)
{
//	cout<<pos<<' '<<tot<<' '<<lim<<endl;
	if(pos==len+1)
	{
		if(tot==0)	return 1;
		return 0;
	}
	if(!lim&&dp[pos][tot]!=-1) return dp[pos][tot];

	long long ret=0 ,  up=lim?a[pos]:9  ;

	for(int i=0;i<= up  ;++i)
	{
		ret=(ret+dfs(pos+1,(tot+i)%d, lim&&i==a[pos] ))%P;

		//(ret+=dfs(pos+1,(tot+i)%d, lim&&i==a[pos] ) )%=P;
	}

	if(!lim) dp[pos][tot]=ret;
	return ret;
}

int main()
{
	memset(dp,-1,sizeof(dp));
	scanf("%s",k+1);
	scanf("%d",&d);
	len=strlen(k+1);
	for(int i=1;i<=len;++i)
	{
		a[i]=k[i]-'0';
	}

	long long ans=(dfs(1,0,1)-1+P)%P;
	printf("%lld\n",ans);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ld long double
#define ll long long
#define pb push_back
#define F first
#define S second
const int A = 3e2 + 2;
const int mod = 1e9 + 7;
int n;
bool vis[A][A][A];
ld mem[A][A][A];
ld solve (int c1, int c2, int c3) {
    if (c1 + c2 + c3 == 0) return 0;
    if (vis[c1][c2][c3])
        return mem[c1][c2][c3];
    vis[c1][c2][c3] = 1;
    int c0 = n - (c1 + c2 + c3);
    ld ret = 1;
    if (c1)
        ret += solve(c1 - 1, c2, c3) * (c1 / (n * 1.0));
    if (c2)
        ret += solve(c1 + 1, c2 - 1, c3) * (c2 / (n * 1.0));
    if (c3)
        ret += solve(c1, c2 + 1, c3 - 1) * (c3 / (n * 1.0));
    ret /= (1 - (c0 / (n * 1.0)));
    return mem[c1][c2][c3] = ret;
}
int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n;
    int freq[4] = {};
    for (int i = 0, x; i < n; i++) {
        cin >> x;
        freq[x]++;
    }
    cout << setprecision(9) << fixed << solve(freq[1], freq[2], freq[3]);
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
using namespace std;
#define MAX 100

int main(){
  int n,dammy;
  cin>>n;
  int p[MAX+1];
  cin>>p[0];
  for(int i=0;i<n-1;i++){
    cin>>p[i+1];
    cin>>dammy;
  }
  cin>>p[n];
  int C[MAX+1][MAX+1];
  for(int i=1;i<=n;i++) C[i][i]=0;
  for(int h=2;h<=n;h++){
    for(int s=1;s<=n-h+1;s++){
      int e=s+h-1;
      C[s][e]=C[s][s]+C[s+1][e]+p[s-1]*p[s]*p[e];
      for(int j=s+1;j<e;j++){
        C[s][e]=min(C[s][e],C[s][j]+C[j+1][e]+p[s-1]*p[j]*p[e]);
      }
    }
  }
  cout<<C[1][n]<<endl;
}

#include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int dp[111][111],n,L[111],R[111];

int dfs(int l,int r){
	if(dp[l][r]!=-1)return dp[l][r];
	int res=1e9;
	for(int i=l;i<r;i++){
		res=min(res,dfs(l,i)+dfs(i+1,r)+L[l]*R[i]*R[r]);
	}
	return dp[l][r]=res;
}

signed main(){
	memset(dp,-1,sizeof(dp));
	r(i,111)dp[i][i]=0;
	cin>>n;
	r(i,n)cin>>L[i]>>R[i];
	cout<<dfs(0,n-1)<<endl;
}


#include<bits/stdc++.h>
#define reg register

const int maxn = 1000005;

int N;

char S[maxn];
char T[maxn];

int main(){
        scanf("%d", &N);
        scanf("%s%s", S+1, T+1);
        int flag = 1;
        for(reg int i = 1; i <= N; i ++)
                if(S[i] != T[i]){ flag = 0; break ; }
        if(flag){ printf("0\n"); return 0; }
        int Min_l = N, Ans = 1;
        std::queue <int> Q;
        for(reg int i = N; i >= 1; i --){
                Min_l = std::min(i, Min_l);
                if(T[i] == T[i-1]) continue ;
                while(Min_l && S[Min_l] != T[i]) Min_l --;
                if(!Min_l){ printf("-1\n"); return 0; }
                while(!Q.empty()){
                        if(Q.front()-Q.size()+1 > i) Q.pop();
                        else break ;
                }
                Q.push(Min_l);
                if(Min_l != i) Ans = std::max(Ans, (int)Q.size() + 1);
        }
        printf("%d\n", Ans);
        return 0;
}

/******************
*  Author: BD747  *
******************/
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<string>
#include<vector>
#include<bits/stdc++.h>
using namespace std;

#define MP make_pair

int r[100005];

int FIND(int x){
	return (x==r[x]) ? x : (r[x]=FIND(r[x]));
}

int n;
long long ans;
vector<pair<int,pair<int,int> > > v;
pair<pair<int,int>,int> g[100005];
int cnt;

int main(){

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

	for(int i=0;i<=100000;i++){
		r[i]=i;
	}

	cin>>n;
	for(int i=0;i<n;i++){
		cin>>g[i].first.first>>g[i].first.second;
		g[i].second=i;
	}
	sort(g,g+n);
	for(int i=0;i<n-1;i++){
		int x1=g[i].first.first,y1=g[i].first.second,id1=g[i].second;
		int x2=g[i+1].first.first,y2=g[i+1].first.second,id2=g[i+1].second;
		v.push_back(MP( min(abs(x1-x2),abs(y1-y2)) , MP(id1,id2)));
		g[i].first.first=y1;g[i].first.second=x1;
	}
	int x=g[n-1].first.first,y=g[n-1].first.second;
	g[n-1].first.first=y;
	g[n-1].first.second=x;
	sort(g,g+n);
	for(int i=0;i<n-1;i++){
		int x1=g[i].first.first,y1=g[i].first.second,id1=g[i].second;
		int x2=g[i+1].first.first,y2=g[i+1].first.second,id2=g[i+1].second;
		v.push_back(MP( min(abs(x1-x2),abs(y1-y2)) , MP(id1,id2)));
	}
	sort(v.begin(),v.end());
	//cout<<pq.size()<<endl;
	for(int i=0;i<v.size();i++){
		int id1=v[i].second.first,id2=v[i].second.second,len=v[i].first;
		if( !(FIND(id1)==FIND(id2)) ){
			ans+=((long long)len);
			r[FIND(id1)]=FIND(id2);
			cnt++;
		}
		if(cnt>=n-1){
			break;
		}
	}
	cout<<ans<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin>>n;
  vector<int> b;
  while(n!=0){
    if(abs(n)%2==0)
      b.push_back(0);
    else{
      b.push_back(1);
      n--;
    }
    n/=-2;
  }
  if(b.empty())
    cout<<0;
  for(int i=b.size()-1;i>=0;i--)
    cout<<b.at(i);
  cout<<endl;
}