//ver 8.1
#include <bits/stdc++.h>
using namespace std;
void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);}
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vd = vector<ld>;
using vs = vector<string>;
using vb = vector<bool>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<ld>>;
using vvs = vector<vector<string>>;
using vvb = vector<vector<bool>>;
using pll = pair<ll,ll>;
using mll = map<ll,ll>;
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
#define each(x,v) for(auto& x : (v))
#define reps(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define mp make_pair
const ll INF = 1LL << 60;
#define CLR(mat,f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) ((a)<=(x)&&(x)<=(b))
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ) //被り削除
#define debug cout << "line : " << __LINE__ << " debug" << endl;
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ind(...) long double __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define inc(...) char __VA_ARGS__; in(__VA_ARGS__)
void in(){}
template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
template <typename T> void in1(T &s) {rep(i,s.size()){in(s[i]);}}
template <typename T> void in2(T &s,T &t) {rep(i,s.size()){in(s[i],t[i]);}}
template <typename T> void in3(T &s,T &t,T &u) {rep(i,s.size()){in(s[i],t[i],u[i]);}}
template <typename T> void in4(T &s,T &t,T &u,T &v) {rep(i,s.size()){in(s[i],t[i],u[i],v[i]);}}
template <typename T> void in5(T &s,T &t,T &u,T &v,T &w) {rep(i,s.size()){in(s[i],t[i],u[i],v[i],w[i]);}}
void out(){cout << endl;}
template <typename T,class... U> void out(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; out(u...);}
void die(){cout << endl;exit(0);}
template <typename T,class... U> void die(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; die(u...);}
template <typename T> void outv(T s) {rep(i,s.size()){if(i!=0)cout<<" ";cout<<s[i];}cout<<endl;}
template <typename T> void out1(T s) {rep(i,s.size()){out(s[i]);}}
template <typename T> void out2(T s,T t) {rep(i,s.size()){out(s[i],t[i]);}}
template <typename T> void out3(T s,T t,T u) {rep(i,s.size()){out(s[i],t[i],u[i]);}}
template <typename T> void out4(T s,T t,T u,T v) {rep(i,s.size()){out(s[i],t[i],u[i],v[i]);}}
template <typename T> void out5(T s,T t,T u,T v,T w) {rep(i,s.size()){out(s[i],t[i],u[i],v[i],w[i]);}}
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
template <typename T> T allSum(vector<T> a){T ans=T();each(it,a)ans+=it;return ans;}
template<typename T> bool inside(T a,T b){auto it=a.begin()-1;each(x,b){it=find(it+1,a.end(),x);if(it==a.end())return false;}return true;}
ll ceilDiv(ll a,ll b) {return (a+b-1)/b;}
ld dist(pair<ld,ld> a, pair<ld,ld> b){return sqrt(abs(a.fi-b.fi)*abs(a.fi-b.fi)+abs(a.se-b.se)*abs(a.se-b.se));} // ２点間の距離
ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }
ll lcm(ll a,ll b){ return a / gcd(a,b) * b;}
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
#define YES(n) ((n) ? "YES" : "NO"  )
#define Yes(n) ((n) ? "Yes" : "No"  )
#define yes(n) ((n) ? "yes" : "no"  )

int main(){
  init();
  inl(n);
  V<ll> a(n);
  in1(a);
  V<ll> sum(n+1),sum2(n+1);
  rep(i,n)sum[i+1]+=sum[i]+a[i];
  rep(i,n)sum2[i+1]+=sum2[i]+sum[i+1];
  ll mx=-INF;
  rep(i,n){
    chmax(mx,a[i]);
    ll ans=mx*(i+1)+sum2[i+1];
    out(ans);
  }
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F(i,a,b) for(int i=(int)(a);i<=(int)(b);i++)
#define R(i,b,a) for(int i=(int)(b);i>=(int)(a);i--)
#define endl "\n"
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pii pair<int,int>
#define pb push_back
#define all(v) v.begin(),v.end()
#define I first
#define S second

const int N=2e5+7;
long double sa[N],sb[N],tmp;

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

  	set <int> con;
  	F(i,1,k){
  		int x;
  		cin>>x;
  		con.insert(x);
  	}

  	if(m<=k){
  		int f=0;
  		F(i,1,n){
  			int cnt=0;
  			F(j,i,i+m-1) cnt+=(con.find(j)!=con.end());
  			if(cnt==m) f=1;
  		}
  		if(f) return cout<<-1<<endl,0;
  	}

  	R(i,n-1,0){
  		if(con.find(i)==con.end()){
	  		sa[i]=(sa[i+1]-sa[i+m+1])/m;
	  		sb[i]=(sb[i+1]-sb[i+m+1])/m+1;
	  	}
	  	else sa[i]=1;

  		sa[i]+=sa[i+1];
  		sb[i]+=sb[i+1];
  	}

  	tmp=(sb[0]-sb[1])/(1-(sa[0]-sa[1]));
  	cout<<setprecision(14)<<tmp<<endl;
}


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

int check(int d,int h,int d0,int h0){
    int a,b;
    if(d%2==1){
        a=d/2+1;
    }
    else{
        a=d/2;
    }
    if(h%2==1){
        b=h/2+1;
    }
    else{
        b=h/2;
    }
    if(d0>a || h0<b){
        return 1;
    }
    return 0;
}
int main()
{
    int n,d,h;
    cin>>n>>d>>h;
    double mini=0.0;
    while(n--){
        int d0,h0;
        cin>>d0>>h0;

        double a1=(((h-h0) * (1.0*d))/ (d-d0));
        a1=h-a1;
        mini=max(mini,a1);

    }
    cout<<mini<<endl;

    return 0;
}

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

  ld res = 0;
  rep(i,n) {
    ld tmp = H + D * (h[i]-H)/(D-d[i]);
    tmp = max((ld)0, tmp);
    chmax(res, tmp);
  }
  printf("%.10Lf\n", res);
  return 0;
}


//khodaya khodet komak kon
# include <bits/stdc++.h>

using namespace std;

typedef long long                                        ll;
typedef long double                                      ld;
typedef pair <int, int>                                  pii;
typedef pair <pii, int>                                  ppi;
typedef pair <int, pii>                                  pip;
typedef pair <pii, pii>                                  ppp;
typedef pair <ll, ll>                                    pll;

# define A                                               first
# define B                                               second
# define endl                                            '\n'
# define sep                                             ' '
# define all(x)                                          x.begin(), x.end()
# define kill(x)                                         return cout << x << endl, 0
# define SZ(x)                                           int(x.size())
# define lc                                              id << 1
# define rc                                              id << 1 | 1
# define InTheNameOfGod                                  ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);

ll power(ll a, ll b, ll md) {return (!b ? 1 : (b & 1 ? a * power(a * a % md, b / 2, md) % md : power(a * a % md, b / 2, md) % md));}

const int xn = 1e2 + 10;
const int xm = - 20 + 10;
const int sq = 320;
const int inf = 1e9 + 10;
const ll INF = 1e18 + 10;
const ld eps = 1e-15;
const int mod = 998244353;
const int base = 257;

int n, a[xn], dp[xn][xn * xn], sum, fact[xn], ans;

int main(){
	InTheNameOfGod;

	fact[0] = 1;
	for (int i = 1; i < xn; ++ i)
		fact[i] = 1ll * fact[i - 1] * i % mod;
	cin >> n;
	for (int i = 1; i <= n; ++ i)
		cin >> a[i], sum += a[i];
	if (sum % 2)
		kill(0);
	dp[0][0] = 1;
	for (int i = 1; i <= n; ++ i)
		for (int j = i; j >= 1; -- j)
			for (int k = sum / 2; k >= a[i]; -- k)
				dp[j][k] = (dp[j][k] + dp[j - 1][k - a[i]]) % mod;
	for (int i = 1; i <= n; ++ i)
		ans = (ans + 1ll * dp[i][sum / 2] * fact[i] % mod * fact[n - i] % mod) % mod;
	cout << ans << endl;

	return 0;
}

#include<bits/stdc++.h>
template <typename _Tp>void read(_Tp &x){
	char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
	if(f)x=-x;
}
template <typename _Tp,typename... Args>void read(_Tp &t,Args &...args){read(t);read(args...);}
const int N=105,mod=998244353;
template<typename _Tp1,typename _Tp2>inline void add(_Tp1 &a,_Tp2 b){(a+=b)>=mod&&(a-=mod);}
template<typename _Tp1,typename _Tp2>inline void sub(_Tp1 &a,_Tp2 b){(a-=b)<0&&(a+=mod);}
int f[N][20005],g[N][20005];
long long fac[N];
int main(){
	fac[0]=fac[1]=1;for(int i=2;i<N;++i)fac[i]=fac[i-1]*i%mod;
	int n;read(n);
	f[0][10000]=1;int s=0;
	for(int i=1,x;i<=n;++i){
		read(x),s=std::min(10000,x+s);
		memset(g,0,sizeof(g));
		for(int j=0;j<i;++j){
			for(int k=10000-s;k<=10000+s;++k){
				if(k-x>=0)add(g[j+1][k-x],f[j][k]);
				if(k+x<=20000)add(g[j][k+x],f[j][k]);
			}
		}
		memcpy(f,g,sizeof(f));
	}
	int ans=0;
	for(int i=0;i<=n;++i)add(ans,1LL*f[i][10000]*fac[i]%mod*fac[n-i]%mod);
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#define X first
#define Y second

using namespace std;
typedef long long llint;

const int maxn = 2e5+10;
const int base = 31337;
const int mod = 1e9+7;
const llint inf = (1LL << 60);
const int logo = 18;
const int off = 1 << logo;
const int treesiz = off << 1;

int n, m;
llint niz[maxn];
llint dp[maxn];
vector< int > graph[maxn];

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) {
		scanf("%lld", niz+i);
	}
	for (int i = 0; i < m; i++) {
		int a, b;
		scanf("%d%d", &a, &b);

		graph[b].push_back(a);
	}

	llint sol = -2*inf;
	for (int i = 1; i <= n; i++) {
		dp[i] = inf;
		for (int j = 0; j < graph[i].size(); j++) {
			int tren = graph[i][j];
			dp[i] = min(dp[i], dp[tren]);
		}
		sol = max(sol, niz[i] - dp[i]);
		dp[i] = min(dp[i], niz[i]);
	}
	printf("%lld\n", sol);
	return 0;
}

#include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
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 dx[] = { 0, 1, -1, 0, 1, -1, 1, -1 };  // i<4:4way i<8:8way
int dy[] = { 1, 0, 0, -1, 1, -1, -1, 1 };

const ll mod = 1e9 + 7;
const ll INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);

int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    int n,m;
    cin >> n >> m;
    vector<pair<ll,int>> a(n);
    vector<ll> b(n); // b[i] := 街iでのコスト
    vector<vector<int>> g(n);
    rep(i,n){
        cin >> a[i].first;
        a[i].second = i;
        b[i] = a[i].first;
    }
    rep(i,m){
        int x,y;
        cin >> x >> y;
        x--;y--;
        g[x].push_back(y);
    }
    sort(ALL(a));
    vector<bool> used(n);
    ll ans = -1e18;
    rep(i,n){
        ll cost = a[i].first;
        queue<int> q;
        q.push(a[i].second);
        while(!q.empty()){
            auto now = q.front();q.pop();
            for(auto next:g[now]){
                if(used[next])continue;
                else{
                    q.push(next);
                    used[next] = true;
                    chmax(ans,b[next] - cost);
                }
            }
        }
    }
    cout << ans << endl;
}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <bitset>
#include <vector>
using namespace std;

const int N = 1e5;

int main() {
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    int t;
    cin >> t;
    string atcoder = "atcoder";
    for (int tests = 0; tests < t; ++tests) {
        string s;
        cin >> s;
        if (s > atcoder) {
            puts("0");
            continue;
        }

        bool ok = false;
        int ans = 1e9;
        for (int i = 0; i < s.length(); ++i) {
            if (s[i] > 'a') {
                ok = true;
                ans = min(ans, i);
            }
            if (s[i] > 't') {
                ans = min(ans, i - 1);
            }
        }
        if (!ok) {
            puts("-1");
            continue;
        }
        cout << ans << endl;
    }
    return 0;
}

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

int main() {
    int t;
    cin >> t;
    while (t--) {
        string s;
        cin >> s;
        string at = "atcoder";
        int ans = 1e9;
        int acc = 0;
        for (int i = 0; i < at.size(); ++i) {
            int posx = -1, posy = -1;
            for (int j = i; j < s.size(); ++j) {
                if (s[j] == at[i] && posx == -1) posx = j;
                if (s[j] > at[i] && posy == -1) posy = j;
            }
            if (posy != -1) ans = min(ans, acc + posy - i);
            if (posx == -1) break;
            while (posx != i) {
                acc += 1;
                swap(s[posx], s[posx-1]);
                posx -= 1;
            }
        }
        if (at < s) ans = min(ans, acc);
        if (ans < 1e7) cout << ans << endl;
        else cout << -1 << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {

  /*very unique*/

  int A,B;

  cin>>A>>B;

  cout<<max(A/100+(A%100)/10+A%10,B/100+(B%100)/10+B%10)<<endl;

}

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

int a, b;
//string s;
//vector<int> a;
//vector<int> b;

int main(){
    cin >> a >> b;
    if(a == b) cout << a << endl;
    else if((a == 0 && b == 1) || (a == 1 && b == 0)) cout << 2 << endl;
    else if((a == 1 && b == 2) || (a == 2 && b == 1)) cout << 0 << endl;
    else if((a == 2 && b == 0) || (a == 0 && b == 2)) cout << 1 << endl;
    return 0;
}


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

int main() {
	int n; cin >> n;
  	int lo = 1000000000, hi = 1;
  	vector<vector<int>> a(n, vector<int>(5));
  	for (int i = 0; i < n; i++) {
    	for (int j = 0; j < 5; j++) {
        	cin >> a[i][j];
          	lo = min(lo, a[i][j]);
          	hi = max(hi, a[i][j]);
        }
    }

  	int ans = lo;
  	while (lo <= hi) {
    	int mid = lo + (hi - lo) / 2;

      	vector<int> check(32, 0);
      	for (int i = 0; i < n; i++) {
        	int index = 0;
          	for (int j = 0; j < 5; j++) {
            	if (a[i][j] >= mid) index += (1 << j);
            }
          	check[index]++;
        }

      	bool good = false;
      	for (int i = 0; i < 32 && !good; i++) {
          	if (!check[i]) continue;
          	check[i]--;
        	for (int j = i; j < 32 && !good; j++) {
              	if (!check[j]) continue;
              	check[j]--;
            	for (int k = j; k < 32 && !good; k++) {
                  	if (!check[k]) continue;
                  	check[k]--;

                  	bool can = true;
                  	for (int p = 0; p < 5 && can; p++) {
                    	int mask = 1 << p;
                      	if (!(i & mask || j & mask || k & mask))
                          can = false;
                    }
                  	if (can)
                      good = true;
                  	check[k]++;
                }
              	check[j]++;
            }
          	check[i]++;
        }

      	if (good) {
          	ans = max(ans, mid);
        	lo = mid + 1;
        } else {
        	hi = mid - 1;
        }
    }
 	cout << ans;
}

/*
 * @Author: AsilenceBTF
 * @LastEditTime: 2020-11-02 19:30:56
 */
#include<bits/stdc++.h>
#define sz(x) int(x.size())
#define ALL(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
const int N = 105;
const double eps = 1e-8;
int x[N], y[N];
int sgn(double x){
    if(fabs(x) < eps) return 0;
    if(x < 0) return -1;
    return 1;
}
struct Point{
    double x, y;
    Point(double _x = 0, double _y = 0){
        x = _x, y = _y;
    }
    Point operator - (const Point &b)const{
        return Point(x - b.x, y - b.y);
    }
    double operator ^(const Point &b)const{
        return x * b.y - y * b.x;
    }
};
struct Line{
    Point s, e;
    Line(){}
    Line(Point _s, Point _e){
        s = _s;
        e = _e;
    }
    int relation(Point p){
        int c = sgn((p - s) ^ (e - s));
        if(c < 0) return 1;
        else if(c > 0) return 2;
        else return 3;
    }
};
int main(){
    int n, f = 0;
    cin >> n;
    for(int i = 1; i <= n; ++i) cin >> x[i] >> y[i];
    for(int i = 1; i <= n; ++i){
        for(int j = i + 1; j <= n; ++j){
            for(int k = j + 1; k <= n; ++k){
                Line t = Line(Point(x[i], y[i]), Point(x[j], y[j]));
                if(t.relation(Point(x[k], y[k])) == 3){
                    cout << "Yes" << endl;
                    return 0;
                }
            }
        }
    }
    cout << "No" << endl;
    // system("pause");
}


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

// BFS
int main(void){
    int n, m;
    cin >> n >> m;
    vector<vector<int>> to(n);
    rep(i,m) {
        int a, b;
        cin >> a >> b;
        --a; --b;
        to[a].push_back(b);
    }

    int ans = 0;
    rep(sv,n) {
        queue<int> q;
        vector<int> used(n);
        used[sv] = 1; //既に訪れた点は計算しない
        ++ans;
        q.push(sv);
        while (q.size()) {
            int v = q.front(); q.pop();
            //cout << "----" << endl;
            for (int u : to[v]) { //範囲列挙 to[0]なら1で行先
                //cout << u << endl;
                if (used[u]) continue;
                ++ans;
                used[u] = 1;
                q.push(u);
            }
        }
    }
    cout << ans << endl;
}

#include<bits/stdc++.h>

using namespace std;

vector<int>graph[3000];
bool visited[3000];

long long int dfs(int index){
    visited[index] = true;
    int cnt = 1;
    for (auto i:graph[index])
        if (!visited[i])
            cnt+=dfs(i);
    return cnt;
}

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

    for (int i=0;i<m;i++){
        int a,b; cin>>a>>b;
        graph[a-1].push_back(b-1);
    }

    long long int ans = 0;
    for (int i=0;i<n;i++){
        for (int j=0;j<n;j++) visited[j] = false;
        ans+=dfs(i);
    }

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


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

namespace my {
/*汎用*/
template <typename T>
void input_vec(vector<T> &a, ll n) {
  assert(a.empty());
  for (ll i = 0; i < n; i++) {
    T tmp;
    cin >> tmp;
    a.push_back(tmp);
  }
}
template <typename T>
void input_set(set<T> &a, ll n) {
  assert(a.empty());
  for (ll i = 0; i < n; i++) {
    T tmp;
    cin >> tmp;
    a.insert(tmp);
  }
}

char itoc(int i) { return i + '0'; }
int ctoi(char c) { return c - '0'; }

/*整数*/
ll gcd(ll a, ll b) {  // O( log(max(a,b)) )
  a = abs(a);
  b = abs(b);
  if (a < b) swap(a, b);
  while (b != 0) {
    a %= b;
    swap(a, b);
  }
  return a;
}
ll lcm(ll a, ll b) { return a * (b / my::gcd(a, b)); }  // O( log(max(a,b)) )
ll modinv(ll n, ll mod) {                               // O( log(max(n,mod)) )
  assert(mod > 0 && my::gcd(n, mod) == 1);
  n %= mod;
  if (n < 0) n += mod;
  ll a = mod, b = n, x = 1, y = 0;
  while (b != 0) {
    ll q = a / b, r = a % b;
    a = b;
    b = r;
    y -= q * x;
    swap(x, y);
  }
  y %= mod;
  if (y < 0) y += mod;
  return y;
}

bool is_prime(ll n) {  // O( sqrt(n) )
  bool flag = true;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      flag = false;
      break;
    }
  }
  return flag;
}
vector<pair<ll, ll>> prime_factorize(ll n) {  // O( sqrt(n) )
  vector<pair<ll, ll>> ret;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i != 0) continue;
    ll exp = 0;
    while (n % i == 0) {
      n /= i;
      exp++;
    }
    ret.push_back(make_pair(i, exp));
  }
  if (n != 1) ret.push_back(make_pair(n, 1));
  return ret;
}

/*union find*/
class unionfind {
 private:
  /*根のparの値は「-(その木の要素数)」とする*/
  ll n_;
  vector<ll> par;

  ll root(ll a) {  // O( log(n) )
    if (par.at(a) < 0)
      return a;
    else {
      par.at(a) = root(par.at(a));  //経路圧縮
      return par.at(a);
    }
  }

 public:
  unionfind(ll n) : n_(n), par(n, -1) {}  // O(n)

  void unite(ll a, ll b) {  // O( log(n) )
    assert(0 <= a && a < n_);
    assert(0 <= b && b < n_);
    if (root(a) == root(b))
      return;
    else {
      ll ra = root(a), rb = root(b);
      if (-par.at(ra) < -par.at(rb)) swap(ra, rb);  //サイズによる併合
      par.at(ra) += par.at(rb);
      par.at(rb) = ra;
    }
  }

  bool is_same(ll a, ll b) {  // O( log(n) )
    assert(0 <= a && a < n_);
    assert(0 <= b && b < n_);
    return root(a) == root(b);
  }

  ll size(ll a) {  // O( log(n) )
    assert(0 <= a && a < n_);
    return -par.at(root(a));
  }
};
}  // namespace my
////////////////////////////////////////////////

int main() {
  ll n, m;
  cin >> n >> m;
  if ((m != 0 && m == n - 1) || m == n || m < 0) {
    cout << -1 << endl;
    return 0;
  }

  vector<pair<ll, ll>> lr;
  ll tmpl = 1, tmpr = 2LL * n;
  for (ll i = 0; i < n - m - 1; i++) {
    lr.push_back(make_pair(tmpl, tmpr));
    tmpl++;
    tmpr--;
  }
  for (ll i = tmpl; i + 1 <= tmpr; i += 2) {
    lr.push_back(make_pair(i, i + 1));
  }

  for (auto &lri : lr) cout << lri.first << " " << lri.second << endl;
}

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

using namespace std;

int main()
{
    #ifdef LOCAL_EXEC
    #else
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #endif // LOCAL_EXEC
    long long int n,i,j,k,x=0,y,z,sum=0;
    cin>>n>>k;
    long long int cnt[n+1]={0};
    for(i=1;i<=n;i++){
        cin>>x;
        cnt[x]+=1;
    }
    y=n;
    for(i=1;i<=k;i++){
        for(j=0;j<=n;j++){
            if(cnt[j]==0||y==0)
                break;
            cnt[j]-=1;
            y-=1;
        }
        sum+=j;
        if(y==0)
            break;
    }
    cout<<sum;
    return 0;
}


#include <bits/stdc++.h>

#define int long long
#define double long double
#define ff first
#define ss second
#define endl '\n'
#define ii pair<int, int>
#define mp make_pair
#define mt make_tuple
#define DESYNC                      \
  ios_base::sync_with_stdio(false); \
  cin.tie(0);                       \
  cout.tie(0)
#define pb push_back
#define vi vector<int>
#define vii vector<ii>
#define all(x) x.begin(), x.end()
#define EPS 1e-9
#define INF 1e18
#define ROOT 1
#define curtime chrono::steady_clock::now().time_since_epoch().count
#define rep(i, beg, n, s) for (int i = beg; i < n; i += s)
using ll = long long;
using namespace std;
const double PI = acos(-1);
const int M = 1000000007;
// const int M = 998244353;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int mod(int n, int m = M) {
  int ret = n % m;
  if (ret < 0) ret += m;
  return ret;
}

int exp(int n, int k, int m = M) {
  if (k == 0) return 1;
  if (k == 1) return n;
  int ax = exp(n, k / 2, m);
  ax = mod(ax * ax, m);
  if (k % 2) ax = mod(ax * n, m);
  return ax;
}

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

int solve(const vector<int> &v, const vector<ii> &b, const vector<int> &calc) {
  int n = v.size();
  int g[n][n];
  for (int i = 0; i < n; i++) {
    int sum = v[i];
    for (int j = i + 1; j < n; j++) {
      sum += v[j];
      int id = upper_bound(all(b), ii(sum - 1, INF)) - b.begin();
      id--;
      if (id == -1)
        g[i][j] = 0;
      else
        g[i][j] = calc[id];
    }
  }

  vector<int> d(n, 0);
  d[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      d[j] = max(d[j], d[i] + g[i][j]);
    }
  }
  return d[n - 1];
}

//#define MULTIPLE_TEST_CASE
void solution() {
  int n, m;
  cin >> n >> m;
  vector<ii> b(m);
  vector<int> calc;
  vi v(n);
  int mx = -INF, mn = INF;
  for (int &x : v) cin >> x, mx = max(mx, x);
  for (ii &x : b) cin >> x.ss >> x.ff, mn = min(mn, x.ff);
  int ans = INF;

  if (mx > mn) {
    cout << -1 << endl;
    return;
  }

  vector<int> p(n, 0);
  for (int i = 0; i < n; i++) p[i] = i;
  sort(all(v));
  sort(all(b));

  for (ii x : b) {
    if (calc.size() == 0)
      calc.pb(x.ss);
    else
      calc.pb(max(calc.back(), x.ss));
  }

  do {
    vector<int> w;
    for (int x : p) w.pb(v[x]);
    ans = min(ans, solve(w, b, calc));
  } while (next_permutation(all(p)));
  cout << ans << endl;
}

int32_t main() {
  DESYNC;
  int t = 1;
#ifdef MULTIPLE_TEST_CASE
  cin >> t;
#endif
  while (t--) solution();
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int n,m;
  	int vdnsddglkdk;
    cin>>n>>m;
    vector<pair<int,int>>a(m);
    for(int i=0;i<m;i++)
    {
        cin>>a[i].first>>a[i].second;
    }
    int k;
    cin>>k;
    vector<pair<int,int>>b(k);
    for(int j=0;j<k;j++)
    {
        cin>>b[j].first>>b[j].second;
    }
    ll l=0;
    ll tot=1<<k;
    for(ll mask=0;mask<tot;mask++)
    {
        vector<int>v(n+1);
        ll ans=0;
        for(int i=0;i<k;i++)
        {
            if(mask & (1<<i))
                v[b[i].second]=1;
            else
                v[b[i].first]=1;
        }
        for(int i=0;i<m;i++)
        {
            if(v[a[i].first] && v[a[i].second])
                ans++;
        }
        l=max(l,ans);
    }
    cout<<l<<endl;
    return 0;
}


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

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
	return (ull)rng() % B;
}

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n; cin >> n;
	vector<vector<int>> c(n,vector<int>(n));
	ll sum=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin >> c[i][j];
			sum+=(ll)c[i][j];
		}
	}
	if(sum%n!=0){
		printf("No\n");
		return 0;
	}
	sum/=n;
	vector<int> a(n,2e9),b(n,2e9);
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			a[i]=min(a[i],c[i][j]);
			b[j]=min(b[j],c[i][j]);
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(a[i]+b[j]<c[i][j]){
				printf("No\n");
				return 0;
			}
		}
	}
	for(int i=0;i<n;i++){
		ll psum=0;
		for(int j=0;j<n;j++){
			psum+=(ll)c[j][(i+j)%n];
		}
		if(psum!=sum){
			printf("No\n");
			return 0;
		}
	}
	int mi=1e9;
	int k=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(mi>c[i][j]){
				mi=c[i][j];
				k=j;
			}
		}
	}
	b[k]=0;
	for(int i=0;i<n;i++){
		a[i]=c[i][k];
	}
	for(int j=0;j<n;j++){
		b[j]=c[0][j]-a[0];
	}
	cout << "Yes" << endl;
	for(int i=0;i<n;i++){
		cout << a[i] << " ";
	}
	cout << endl;
	for(int i=0;i<n;i++){
		cout << b[i] << " ";
	}
	cout << endl;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template<class T> bool chmin(T& a,T b){if(a>b) {a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b){if(a<b) {a = b; return true;} return false;}
#define all(x) (x).begin(),(x).end()
#define debug(x)  cerr << #x << " = " << (x) << endl;

class UnionFind{
private:
    vec<int> p,s;
    int cnt;
public:
    UnionFind(){}
    UnionFind(int N):cnt(N),s(N,1),p(N){
        iota(p.begin(),p.end(),0);
    }
    int find(int x){
        if(p[x]==x) return x;
        else return p[x] = find(p[x]);
    }
    void unite(int x,int y){
        x = find(x); y = find(y);
        if(x==y) return;
        if(s[x]>s[y]) swap(x,y);
        p[x] = y;
        s[y] += s[x];
        cnt--;
    }
    bool is_same_set(int x,int y) {return find(x)==find(y);}
    int size(int x) {return s[find(x)];}
    int compnents_number(){return cnt;}
};

struct edge{
    int s,t,val;
    bool operator<(const edge& e)const{
        return val<e.val;
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vec<int> X(N+2),Y(N+2);
    for(int i=1;i<=N;i++) cin >> X[i] >> Y[i];
    vec<edge> E;
    for(int i=1;i<=N;i++){
        int d = 100-Y[i];
        E.push_back({0,i,d*d});
    }
    for(int i=1;i<=N;i++){
        int d = 100+Y[i];
        E.push_back({N+1,i,d*d});
    }
    for(int i=1;i<N;i++) for(int j=i+1;j<=N;j++){
        int dx = X[j]-X[i],dy = Y[j]-Y[i];
        E.push_back({i,j,dx*dx+dy*dy});
    }
    double ans = 0;
    sort(all(E));
    UnionFind uf(N+2);
    for(auto& x:E){
        ans = sqrt(x.val)/2;
        uf.unite(x.s,x.t);
        if(uf.is_same_set(0,N+1)) break;
    }
    cout << fixed << setprecision(10) << ans << "\n";
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<cstring>
#include<map>
#include<cmath>
#include<iomanip>
#include<set>
#include<stack>
#include<fstream>
#define inf 2147483647
constexpr int Mod = 998244353;
using namespace std;

long long A, B, C;

int main() {

    cin >> A >> B >> C;

    if (C % 2) {
        if (A >= 0 && B >= 0) {
            if (abs(A) < abs(B)) {
                cout << "<" << endl;
            }
            else if (abs(A) > abs(B)) {
                cout << ">" << endl;
            }
            else {
                cout << "=" << endl;
            }
        }
        else {
            if (A < 0) {
                cout << "<" << endl;
            }
            else if(B<0){
                cout << ">" << endl;
            }
        }
    }
    else {
        if (abs(A) < abs(B)) {
            cout << "<" << endl;
        }
        else if (abs(A) > abs(B)) {
            cout << ">" << endl;
        }
        else {
            cout << "=" << endl;
        }
    }

    return 0;
}

#define DEBUG 0
#include <bits/stdc++.h>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define eb emplace_back
#define rep(i,n) for(int i=0; i<(n); i++)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define REP(i, begin, end) for(int i = int(begin); i < int(end); i++)
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template<class T>using numr=std::numeric_limits<T>;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const int INF = 1e9;
const ll LLINF = 1e16;
const int mod = 1000000007;
const int mod2 = 998244353;
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) { std::cerr << " " << head; debug_impl(tail...); }
#if DEBUG
#define debug(...)\
    do {\
        std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
        debug_impl(__VA_ARGS__);\
        std::cerr << std::noboolalpha;\
    } while (false)
#else
#define debug(...) {}
#endif

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr>
std::istream& operator>> (std::istream& is, Container& v)
    { for (auto & x : v) { is >> x; } return is; }

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr >
std::ostream& operator<< (std::ostream& os, Container const& v) {
os << "{";
    for (auto it = v.begin(); it != v.end(); it++)
        {os << (it != v.begin() ? "," : "") << *it;}
    return os << "}";
}

// make_v -> auto a = make_v<int>(n,m)
template< typename T = int64_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...));
}
//ジェネッリックラムダ
template< typename F >
struct FixPoint : F {
    FixPoint(F &&f) : F(forward< F >(f)) {}

    template< typename... Args >
    decltype(auto) operator()(Args &&... args) const {
        return F::operator()(*this, forward< Args >(args)...);
    }
};
template< typename F >
inline decltype(auto) MFP(F &&f) {
    return FixPoint< F >{forward< F >(f)};
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    ll a,b,c;
    cin >> a >> b >> c;
    if(c%2==0){
        if(abs(a) > abs(b)) cout << '>' << endl;
        else if(abs(a) == abs(b)) cout << '=' << endl;
        else cout << '<' << endl;
    }else{
        if(a > b) cout << '>' << endl;
        else if(a == b) cout << '=' << endl;
        else cout << '<' << endl;
    }

}


#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T>
using V = vector<T>;
template <class T>
inline void chmin(T &a, const T &b) { a = min(a, b); }
template <class T>
inline void chmax(T &a, const T &b) { a = max(a, b); }
template <class T>
inline bool kbit(const T &x, const int &k) { return ((x >> k) & 1LL); }
inline int popcount(const int &n) { return __builtin_popcount(n); }
inline ll popcountll(const ll &n) { return __builtin_popcountll(n); }
inline ll mask(const ll &k) { return (1LL << k) - 1LL; }
template <class T>
void zip(V<T> &v) {
    sort(all(v));
    v.erase(unique(all(v)), v.end());
}
template <class T>
int lwb(V<T> &v, const T &x) { return lower_bound(all(v), x) - v.begin(); }
template <class T>
int upb(V<T> &v, const T &x) { return upper_bound(all(v), x) - v.begin(); }
void dump() {
    cerr << '\n';
}
template <class Head, class... Tail>
void dump(Head &&head, Tail &&...tail) {
    cerr << head << (sizeof...(Tail) == 0 ? " " : ", ");
    dump(std::move(tail)...);
}
template <class T>
void print(const vector<T> &v) {
    for (int i = 0; i < v.size(); i++) cout << v[i] << (i + 1 == v.size() ? '\n' : ' ');
}
template <class T>
void read(vector<T> &v) {
    for (int i = 0; i < v.size(); i++) cin >> v[i];
}
constexpr char sp = ' ', newl = '\n';
constexpr int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
constexpr ll INF = (1LL << 60) - 1LL;
constexpr ll MOD = 998244353LL;
//@docs Docs/BinaryIndexedTree.md
template <class T>
struct BinaryIndexedTree {
    int n;
    vector<T> dat;
    BinaryIndexedTree(const int &n) : n(n + 1), dat(n + 1) {}
    void add(int k, const T &x) {
        for (++k; k < n; k += k & -k) dat[k] += x;
    }
    T get(int k) {
        T res = 0;
        for (++k; k > 0; k -= k & -k) res += dat[k];
        return res;
    }
    inline T get(const int &l, const int &r) { //0-indexed [l,r)
        return get(r - 1) - get(l - 1);
    }
};
//////////////////////////////////////////////INSERT ABOVE HERE
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    V<ll> a(n), b(n);
    read(a);
    read(b);
    map<ll, int> id;
    V<V<int>> pos(n + 1);
    V<int> is(n + 1);
    int idx = 0;
    for (int i = 0; i < n; i++) {
        a[i] += i;
        b[i] += i;
    }
    V<ll> c = a, d = b;
    sort(all(c));
    sort(all(d));
    if (c != d) {
        cout << -1 << newl;
        return 0;
    }
    //転倒数
    map<P, int> mp;
    map<int, int> cnt;
    for (int i = 0; i < n; i++) {
        if (!cnt.count(b[i])) {
            cnt[b[i]] = 0;
        }
        cnt[b[i]]++;
        mp[P(b[i], cnt[b[i]])] = i;
    }
    cnt.clear();
    V<ll> t(n);
    BinaryIndexedTree<ll> bit(n + 10);
    ll res = 0;
    for (int i = 0; i < n; i++) {
        if (!cnt.count(a[i])) {
            cnt[a[i]] = 0;
        }
        cnt[a[i]]++;
        t[i] = mp[P(a[i], cnt[a[i]])];
        res += bit.get(n + 1) - bit.get(t[i]);
        bit.add(t[i], 1);
    }
    cout << res << newl;
}

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

typedef long long int ll;

bool comp(string a, string b)
{
    string l = a+b;
    string m = b+a;
    if(l>m)
    {
        return true;
    }
    else
        return false;
}
int main()
{
  int n;
  cin>>n;
  int arr[n];
  for(int i=0;i<n;i++)
    cin>>arr[i];
  int cnt[n+1] = {0};
  bool valid = true;
  int numbersVisited = 0;
  for(int i=0;i<n;i++)
  {
      int val = arr[i];
      if(cnt[val] != 0)
      {
          valid = false;
          break;
      }
      else
      {
          cnt[val] = 1;
          numbersVisited++;
      }
  }
  if(valid && numbersVisited == n)
  {
      cout<<"Yes\n";
  }
  else
    cout<<"No\n";
}


#include<bits/stdc++.h>
using namespace std;
int n,k;
char s[500];
inline char check(char a,char b){
	if(a=='R'&&b=='S')
		return a;
	if(a=='P'&&b=='R')
		return a;
	if(a=='S'&&b=='P')
		return a;
	return b;
}
int main(){
	scanf("%d%d",&n,&k);
	scanf("%s",s+1);
	for(int i=1;i<=k;++i){
		for(int j=1;j<=n;++j)
			s[j+n]=s[j];
		for(int j=1;j<=n;++j)
			s[j]=check(s[j*2-1],s[j*2]);
	}
	printf("%c\n",s[1]);
}

#include<bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i=1;i<=n;i++)
typedef int t;
int d[100010];
int main(){
	int a,b,c;
	cin>>a>>b>>c;
	if(c%2==0){
		a=abs(a);
		b=abs(b);
	}
	cout<<(a<b? '<':a>b? '>':'=');
	return 0;
}


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

//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define pb push_back
#define fi first
#define se second
#define ll long long
#define tp top()
#define fr front()
#define vi vector<int>
#define sz size()
#define rep(i,a,b) for(int i = a; i < b; ++i)
#define mem(a,  b) memset(a,  (b),  sizeof(a))
#define clr(a) memset(a,  0,  sizeof(a))
#define sqr(x) ( (x) * (x) )
#define all(v) v.begin(),  v.end()

typedef pair<int, int> pii;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;

void base(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}

void solve(){
	int n;
	cin>>n;
	priority_queue<pair<ll, pair<ll,ll>>> pq;
	ll suma=0;
	rep(i,0,n){
		ll a,b;
		cin>>a>>b;
		suma+=a;
		pq.push({2*a+b,{a,b}});
	}
	ll sumb=0;
	int cnt=0;
	while(pq.sz){
		sumb += pq.tp.se.fi + pq.tp.se.se;
		suma -= pq.tp.se.fi;
		cnt++;
		pq.pop();
//		cerr<<cnt<<" "<<suma<<" "<<sumb<<"|n";
		if (sumb > suma) break;
	}
	cout<<cnt<<"\n";
}

int main()
{
	base();
//  freopen("input.txt", "r", stdin);
//  freopen("output.txt", "w", stdout);
	int t=1;
//	cin>>t;
	while(t--){
		solve();
	}
	return 0;
}

#include <cstdio>
#include <cstring>

typedef long long ll;
const int N=300005;

int n,a[N];
ll res;
struct BIT
{
    int tr[N],r;
    void add(int x){for(++x;x<=n;x+=x&-x)tr[x]++;}
    int ask(int x){r=0;for(++x;x;x&=x-1)r+=tr[x];return r;}
}fw;

int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",a+i);
        res+=i-fw.ask(a[i]);
        fw.add(a[i]);
    }
    for(int i=0;i<n;i++)
    {
        printf("%lld\n",res);
        res+=n-a[i]*2-1;
    }
    return 0;
}


// Author : Sarthak Kapoor
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int ll
#define rep(i,n) for(int i=0;i<n;++i)
#define repa(i,a,n) for(int i=a;i<n;++i)
#define repr(i,n) for(int i=n-1;i>=0;--i)
#define ll long long
#define pi pair<int,int>
#define pii pair<pi,int>
#define vi vector<int>
#define pb push_back
#define fi first
#define sec second
#define all(v) v.begin(),v.end()
#define s(v) v.size()
ll mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());//use rng()%n for numbers in range [0,n-1]
ll gcd(ll a,ll b){
	if(b==0)return a;
	return gcd(b,a%b);
}
ll fastexp(ll x,ll a, ll mod){
    ll res = 1;
    while(a>0){
        if(a&1){
            res = (res*x)%mod;
        }
        a=a>>1;
        x=(x*x)%mod;
    }
    return res;
}
ll inverse(ll n,ll mod){
    return fastexp(n,mod-2,mod);
}
template <typename T>
void add(T &a, T b, T mod){
	a += b;
	if(a >= mod)a -= mod;
}
template <typename T>
void sub(T &a, T b, T mod){
	a -= b;
	if(a < 0)a += mod;
}
template <typename T>
void mul(T &a, T b, T mod){
	a *= b;
	if(a >= mod)a %= mod;
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int a,b,c;
	cin>>a>>b>>c;
	int ans = 1;
	// mod = 4;
	int z = fastexp(b,c,4ll);
	// cout<<z<<"\n";
	// mod = 10;
	if(a%2 == 0){
		int d = 0;
		while(a%2 == 0){
			a /= 2;
			d++;
		}
		mul(d,z,4ll);
		if(d%4 == 0){
			ans = 6;
		}
		else if(d%4 == 1){
			ans = 2;
		}
		else if(d % 4 == 2){
			ans = 4;
		}
		else if(d%4 == 3){
			ans = 8;
		}
	}
	if(a % 5 == 0){
		while(a % 5 == 0){
			a /= 5;
		}
		mul(ans,5ll,10ll);
	}
	for(int i = 3;i*i <= a;i += 2){
		if(a%i == 0){
			int d = 0;
			while(a%i == 0){
				d++;
				a /= i;
			}
			// cout<<i<<" "<<d<<"\n";
			mul(d,z,4ll);
			// cout<<d<<"\n";
			int temp = fastexp(i,d,10ll);
			// cout<<temp<<
			mul(ans,temp,10ll);
			// cout<<ans<<"\n";
		}
	}
	if(a > 1){
		// cout<<a<<"\n";
		int d = 1;
		// while(a%i == 0){
		// 	d++;
		// 	a /= i;
		// }
		mul(d,z,4ll);
		mul(ans,fastexp(a,d,10ll),10ll);
	}
	cout<<ans;
	// int z = fastexp(b,c);
	// mod = 10;
	// cout<<fastexp(a,z);
	return 0;
}

#include <cstdio>
#define DIV 10000LL
using namespace std;

typedef long long LL;

LL x, y, R;

inline LL read()
{
	// Returns: input * 10000.
	LL res = 0LL;
	int num = 0;
	bool flag = false, negative = false;
	for(char c=getchar(); c != ' ' && c != '\n'; c=getchar())
	{
		if(c == '-') negative = true;
		else if(c == '.') flag = true;
		else
		{
			res *= 10LL;
			res += c - '0';
			if(flag) num ++;
		}
	}
	for(int i=num; i<4; i++)
		res *= 10LL;
	return negative? -res: res;
}

inline LL in_circle(const LL& dx, const LL& dy)
{
	return dx * dx + dy * dy <= R * R;
}

inline LL findtop(LL i)
{
	i *= DIV;
	LL l = y, r = y + R;
	while(l < r)
	{
		LL mid = l + r + 1LL >> 1LL; // l<0?
		if(in_circle(i - x, mid - y))
			l = mid;
		else r = mid - 1LL;
	}
	return l;
}

inline LL ceildiv(const LL& a)
{
	// Returns: ceil(a / DIV).
	if(a < 0LL) return a / DIV;
	if(a % DIV == 0LL) return a / DIV;
	return a / DIV + 1LL;
}

inline LL floordiv(const LL& a)
{
	// Returns: floor(a / DIV).
	if(a >= 0LL) return a / DIV;
	if(a % DIV == 0LL) return a / DIV;
	return a / DIV - 1LL;
}

int main()
{
	x = read(), y = read(), R = read();
	LL ans = 0LL, left = ceildiv(x - R), right = floordiv(x + R);
	for(LL i=left; i<=right; i++)
	{
		LL top = findtop(i);
		LL bottom = (y << 1LL) - top;
		ans += floordiv(top) - ceildiv(bottom) + 1LL;
	}
	printf("%lld\n", ans);
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define IO(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)
using namespace __gnu_pbds;
using namespace std;
using ld = long double;
using ll = long long;

int n, m, q, x[200001], y[200001];
pair<int, int> op[200001];
vector<pair<int, int>> que[200001];
pair<ll, ll> ans[200000];

int main(){
    //IO("input.txt", "output.txt");
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> x[i] >> y[i];
    cin >> m;
    for(int i = 1; i <= m; i++){
        int typ, p;
        cin >> typ;
        if(typ < 3) op[i] = make_pair(typ, 0);
        else{
            cin >> p;
            op[i] = make_pair(typ, p);
        }
    }
    cin >> q;
    for(int i = 0; i < q; i++){
        int a, b;
        cin >> a >> b;
        que[a].emplace_back(b, i);
    }
    for(auto i : que[0])
        ans[i.second] = make_pair(x[i.first], y[i.first]);
    bool rev = false;
    ll px = 0, sx = 1, py = 0, sy = 1;
    for(int i = 1; i <= m; i++){
        if(op[i].first < 3){
            if(rev) op[i].first = (op[i].first == 1) ? 2 : 1;
            if(op[i].first == 1) sx *= -1, px *= -1;
            else sy *= -1, py *= -1;
            rev ^= true;
        }else{
            if(rev) op[i].first = (op[i].first == 3) ? 4 : 3;
            if(op[i].first == 3)
                px = 2 * op[i].second - px, sx *= -1;
            else
                py = 2 * op[i].second - py, sy *= -1;
        }
        for(auto j : que[i]){
            pair<ll, ll> res = make_pair(px + sx * x[j.first], py + sy * y[j.first]);
            if(rev) swap(res.first, res.second);
            ans[j.second] = res;
        }
    }
    for(int i = 0; i < q; i++)
        cout << ans[i].first << ' ' << ans[i].second << '\n';
    return 0;
}

/*
90 deg clockwise is: (x, y) -> (y, -x)
90 deg counterclockwise is: (x, y) -> (-y, x)
reflection across x = p is: (x, y) -> (2 * p - x, y)
reflection across y = p is: (x, y) -> (x, 2 * p - y)
*/

#include<bits/stdc++.h>
//#include<atcoder/all>
using namespace std;
//using namespace atcoder;
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<int, int>;
#define rep(i,m,n) for(int (i)=(m);(i)<(n);++(i))
#define rrep(i,m,n) for(int (i)=(n)-1;(i)>=(m);--(i))
#define all(x) (x).begin(),(x).end()
#define out(y,x,h,w) (y)<0||(x)<0||(y)>=(h)||(x)>=(w)
constexpr ll INF = 1LL << 30;
constexpr ll mod = 10007; //(ll)1e9 + 7;
constexpr double PI = 3.1415926535897932;

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;
}
inline void init() {
    cin.tie(nullptr); cout.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
}

vector<vector<ll>>func(vector<vector<ll>>A, vector<vector<ll>>B) {
    vector<vector<ll>>res(vector<vector<ll>>(A.size(), vector<ll>(B[0].size())));
    rep(i, 0, res.size()) {
        rep(j, 0, res[i].size()) {
            ll a = 0;
            rep(k, 0, 3) {
                a += A[i][k] * B[k][j];
            }
            res[i][j] = a;
        }
    }
    return res;
}
int main() {
    init();

    int N; cin >> N;
    vector<P>XY(N);
    rep(i, 0, N) {
        int a, b; cin >> a >> b;
        XY[i] = { a,b };
    }
    int M; cin >> M;
    vector<vector<vector<ll>>>O(M, vector<vector<ll>>(3, vector<ll>(3)));
    rep(i, 0, M) {
        int a;cin >> a;
        vector<vector<ll>>A(vector<vector<ll>>(3, vector<ll>(3)));
        if (a == 1) {
            A = { {0,1,0},
                {-1,0,0},
                {0,0,1} };
        }
        if (a == 2) {
            A = { {0,-1,0},
                {1,0,0},
                {0,0,1} };
        }
        if (a == 3) {
            int p; cin >> p;
            A = { {-1,0,2 * p},
                {0,1,0},
                {0,0,1} };
        }
        if (a == 4) {
            int p; cin >> p;
             A = { {1,0,0},
                {0,-1,2*p},
                {0,0,1} };
        }
        if (i == 0)O[i] = A;
        else O[i] = func(A, O[i-1]);
    }
    int Q; cin >> Q;
    rep(i, 0, Q) {
        int a, b; cin >> a >> b;
        --b;
        if (a == 0) {
            cout << XY[b].first << " " << XY[b].second << "\n";
        }
        else {
            --a;
            vector<vector<ll>>B =
            { {XY[b].first},
                {XY[b].second},
                {1} };
            auto ans = func(O[a], B);
            cout << ans[0][0] << " " << ans[1][0] << "\n";
        }
    }

    return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define LL long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define f first
#define s second
#define int long long
#define sz(x) (ll)(x.size())
using namespace std;

const int mod = 1e9+7;

int expo_pow(int x,int y){
 if(y == 0) return 1;
  y=y%(mod-1);
  x%=mod;
  if(y==0) y=mod-1;
  int res=1;
  while(y){
    if(y&1) res=(res*x)%mod;
    x=(x*x)%mod;
    y>>=1;
  }
  return res;
}

ll add()
{
    return 0;
}

template <typename T, typename... Types>
T add(T var1, Types... var2){
    return (((((ll)(var1)) % mod + (ll)(add(var2...))) % mod) + mod) % mod;
}

ll mul(){
    return 1;
}

template <typename T, typename... Types>
T mul(T var1, Types... var2){
    return (((ll)(var1)) % mod * (ll)(mul(var2...))) % mod;
}

class triplet{
    public:
    LL a;
    LL b;
    LL gcd;
};

triplet extended_euclid(LL a,LL b)
{
    if(b==0)
    {
        triplet x;
        x.a=1;
        x.b=0;
        x.gcd=a;
        return x;
    }
    triplet x1=extended_euclid(b,a%b);
    triplet x;
    x.a=x1.b;
    x.b=x1.a-(a/b)*(x1.b);
    x.gcd=x1.gcd;
    return x;
}
LL mod_inverse(LL a, LL m)
{
    LL ans=extended_euclid(a,m).a;
    return (ans%m +m )%m;
}

void solve(){
  int n,s,k;
  cin >> n >> s >> k;
  int gcd = __gcd(n,k);
  int steps_to_move = n - s;
  if (steps_to_move%gcd != 0) {
    cout << "-1\n";
    return;
  }

  n /= gcd;
  k /= gcd;
  steps_to_move /= gcd;

  int ans = mod_inverse(k,n);
  ans*= steps_to_move;
  ans %= n;
  ans += n;
  ans %= n;

  cout << ans << "\n";

}

signed main(){
  fast;
  int test = 1;
  cin>>test;
  int i=1;
  while(test--){
    solve();
  }
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007;
//const ll mod = 998244353;
const int inf = 1e9 + 10;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

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

  ll t;
  cin >> t;
  while (t--) {
    ll n, a, b;
    cin >> n >> a >> b;

    if (n - a - b < 0) {
      cout << 0 << '\n';
      continue;
    }
    ll ans = ((n - a + 1) * (n - b + 1) - (n - a - b + 1) * (n - a - b + 2) + mod * mod) % mod;
    ans = ans * ans % mod;
    ans = ((n - a + 1) * (n - a + 1) % mod * (n - b + 1) % mod * (n - b + 1) % mod - ans + mod) % mod;
    cout << ans << '\n';
  }

  return 0;
}


/*
	JAI JAGANNATH!
*/
//@Author : zanj0

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

#define int 			long long int

#define ff              first
#define ss              second
#define pb              push_back
#define MOD             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define w(x)            int x; cin>>x; while(x--)
#define endl       		"\n"
#define timetaken cerr<<"Time : "<<1000*(long double)clock()/(long double)CLOCKS_PER_SEC<<"ms\n"

void zanj0()
{
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
const int MAX = 3e6 + 5;
int fac[MAX], finv[MAX];
int 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;
	}
}
int COM(int n, int k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
int add(int a, int b) {
	return ((a % MOD) + (b % MOD)) % MOD;
}
int mul(int a, int b) {
	return ((a % MOD) * (b % MOD)) % MOD;
}
int fastPow(int base, int p) {
	int ret = 1;
	while (p) {
		if (p & 1) ret = mul(ret, base);
		base = mul(base, base);
		p >>= 1;
	}
	return ret;
}
int sub(int a, int b) {
	return (((a % MOD) - (b % MOD)) + MOD) % MOD;
}
void solve() {
	int n, m, k;
	cin >> n >> m >> k;
	COMinit();
	if (n > m + k) {
		cout << 0 << endl;
	} else if (k >= n) {
		cout << COM(n + m, n) << endl;
	} else {
		cout << sub(COM(n + m, n), COM(n + m, m + k + 1)) << endl;
	}

}
int32_t main()
{
	zanj0();
	solve();
	timetaken;
	return 0;
}

#include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007LL
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
#define vi vector<ll>
#define vvi vector<vi>
#define SHIFT(n) (1LL<<((ll)n))
#define ALL(a) (a).begin(),(a).end()
#define Max(a) (*max_element(ALL(a)))
#define Min(a) (*min_element(ALL(a)))
#define Sum(a) (accumulate(ALL(a),0LL))
using namespace std;

struct Random{
    random_device rd;
    mt19937 eng;
    uniform_int_distribution<ll> distr;
    uniform_real_distribution<float> distrf;
    Random():eng(rd()),distr(0,INF),distrf(0,1){}
    ll nextInt(ll f, ll t){
        return (distr(eng)%(t-f+1))+f;
    }

    double nextDouble(){
        return (distrf(eng));
    }

    vi next_n_int(ll f, ll t, ll n){
        set<ll> se;
        vi ret;
        while(se.size() < n){
            se.insert(nextInt(f, t));
        }
        for(auto a:se){
            ret.pb(a);
        }
        return ret;
    }
};

vector<vvi> dist(33,vvi(33, vi(4, INF)));
vector<vvi> cd(33,vvi(33, vi(4, INF)));

Random rnd;

ll vx[4] = {1, 0, -1, 0}, vy[4] = {0, 1, 0, -1};
char nc[4] = {'D', 'R', 'U', 'L'};
ll h = 30, w = 30;

bool check(ll x, ll y){
    return x >= 0 && x < h && y >= 0 && y < w;
}

pair<string, ll> make_path(ll si, ll sj, ll ti, ll tj){
    priority_queue<pair<pair<ll,string>,pair<ll,ll>>,vector<pair<pair<ll,string>,pair<ll,ll>>>,greater<>>que;
    que.push(mp(mp(0,""),mp(si,sj)));
    vvi d(33,vi(33,llINF));
    d[si][sj] = 0;
    while(!que.empty()){
        ll ni = que.top().S.F, nj = que.top().S.S;
        ll cost = que.top().F.F;
        string s = que.top().F.S;
        ///cout << ni << " " << nj << endl;
        que.pop();
        if(d[ni][nj] > cost){
            continue;
        }
        if(ni == ti && nj == tj){
            return mp(s, cost);
        }
        for(ll i = 0;i < 4; i++){
            ll nx = ni + vx[i], ny = nj + vy[i];
            if(check(nx, ny)){
                if(d[nx][ny] > cost + dist[ni][nj][i]){
                    d[nx][ny] = cost + dist[ni][nj][i];
                    que.push(mp(mp(d[nx][ny], s+nc[i]),mp(nx, ny)));
                }
            }
        }
    }
}

void solve(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll q = 1000;
    while(q--){
        ll si, sj, ti, tj;
        cin >> si >> sj >> ti >> tj;
        pair<string, ll> path = make_path(si, sj, ti, tj);
        cout << path.F << endl;
        flush(cout);
        ll cost;
        cin >> cost;
        ll pcost = path.S;
        ll D = rnd.nextInt(100, 2000);
        ll x = si, y = sj;
        ll sum = 0, cnt = 0, nokori = cost - pcost;
        ll psize = path.F.size();
        ll gap = nokori/psize;
        for(auto c:path.F){
            //DRUL
            if(c == 'U'){
                dist[x][y][2] = dist[x-1][y][0] = max(1000+D, dist[x][y][2] + gap);
                x--;
            }else if(c == 'D'){
                dist[x][y][0] = dist[x+1][y][2] = max(1000+D, dist[x][y][0] + gap);
                x++;
            }else if(c == 'R'){
                dist[x][y][1] = dist[x][y+1][3] = max(1000+D, dist[x][y][1] + gap);
                y++;
            }else{
                dist[x][y][3] = dist[x][y-1][1] = max(1000+D, dist[x][y][3] + gap);
                y--;
            }
        }
    }
    return;
}

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


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define REP(i,m,n) for (int i = (m); i < (n); ++i)
#define rrep(i,n) for (int i = (n)-1; i >= 0; --i)
#define RREP(i,m,n) for (int i = (n)-1; i >= (m); ++i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
using namespace std;
using ll = long long int;
using vec = vector<ll>;
using P = pair<int,int>;
const int INF = 1e9+7;

int main() {
  int n, qq; cin >> n >> qq;
  vec a(n), b(n);
  rep(i,n) cin >> a[i];
  sort(all(a));
  b[0] = a[0]-1;
  REP(i,1,n) {
    b[i] = a[i] - a[i-1] - 1 + b[i-1];
  }
  rep(q,qq) {
    ll k; cin >> k;

    if (k <= b[0]) {
      cout << k << endl;
      continue;
    }

    int j = upper_bound(all(b), k-1) - b.begin() - 1;
    cout << k - b[j] + a[j] << endl;
  }
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(x,y) memset(x,y,sizeof(x))
#define pb push_back
#define mem(x,y) meset(x,y,sizeof(x))

ll len = 1e10;
string pat = "110";

int main()
{
    ll i,j,k,s,d,r,m,n,l;
    cin>>n;
    string line;
    cin>>line;

    if(n==1)
    {
        if(line=="0")   cout<<len<<endl;
        else            cout<<2*len<<endl;
        return 0;
    }

    if(line[0]=='1' && line[1]=='1') j = 0;
    else if(line[0]=='1' && line[1]=='0') j = 1;
    else j = 2;

    for(i=0, d=0; i<n; i++, j++)
    {
        j = j%3;
        if(line[i] != pat[j]) d++;
    }
    if(d)
    {
        cout<<"0\n";
        return 0;
    }

    for(i=0, s=0; i+2<n; i++)
    {
        if(line[i]==pat[0] && line[i+1]==pat[1] && line[i+2]==pat[2]) s++;

        //cout<<(line[i]==pat[0]) <<" "<< (line[i+1]==pat[1]) <<" "<< (line[i+2]==pat[2])<<endl;
    }
    if(line[0]!='1' || line[1]!='1')    s++;
    if(line[n-1] != '0')                s++;

    cout<<len-s+1<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(lli i = a; i < b; i++)
#define lli long long int
#define ld long double
#define all(v) v.begin(), v.end()
#define hell 1000000000000000
#define pb push_back
#define pf push_front
#define vi vector<lli>
#define vip vector<pair<lli, lli>>
#define F first
#define S second
#define pi 2*acos(0.0)
#define sz(s) s.size()
#define atmod (1000000000+7)
#define mod 998244353
// auto _C=clock();
/*
lli n = 10000000;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
vector<bool> p(n + 1, true);
void sieve()
{
    for (long long int i = 2; i * i <= n; i++)
    {
        if (p[i])
        {
            for (long long int j = i + i; j <= n; j = j + i)
            {
                p[j] = false;
            }
        }
    }
}
*/
/*
WINNERS NEVER QUIT AND QUITTERS NEVER WIN!!

Falling down is an accident, Staying down is a choice.

*/
/*
lli binpow(lli a, lli n,lli m)
{
    lli r = 1;
    a %=m ;
    while (n > 0)
    {
        if (n & 1)
        {
            r = (r * a)%m ;
        }
        a = (a * a)%m ;
        n = n / 2;
    }
    return r;
}
*/
/*bool cmp(pair<lli,lli> a,pair<lli,lli> b)
{
    if(a.F<b.F) return 1;
    else if(a.F==b.F) return(a.S>b.S);
    else return 0;
}*/
void solve()
{
    lli h,w;
    cin>>h>>w;
    vector<vector<lli>> g(h+1,vector<lli>(w+1,0));
    vector<vector<lli>> dp(h+1,vector<lli>(w+1,0));
    // max differnce between the score of the player 1 and player 2 if currently player 1 is moving
    rep(i,1,h+1)
    {
        rep(j,1,w+1)
        {
            char c;
            cin>>c;
            if(c == '+')
            {
                g[i][j] = 1;
            }
            else
            {
                g[i][j] = -1;
            }
        }
    }
    for(lli i = h;i >= 1; i--)
    {
        for(lli j = w;j >= 1; j--)
        {
            if(i==h&&j==w)
            {
                dp[i][j]=0;
            }
            else if(i==h)
            {
                dp[i][j] = g[i][j+1] - dp[i][j+1];
            }
            else if(j==w)
            {
                dp[i][j] = g[i+1][j] - dp[i+1][j];
            }
            else
            {
                dp[i][j] = max( g[i][j+1]-dp[i][j+1] , g[i+1][j]-dp[i+1][j] );
            }
        }
    }
    if(dp[1][1]>0)
    {
        cout<<"Takahashi";
    }
    else if(dp[1][1]<0)
    {
        cout<<"Aoki";
    }
    else
    {
        cout<<"Draw";
    }
}
int main()
{
    // ios_base::sync_with_stdio(false);
    // cin.tie(0);
    // cout.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    // sieve();
    lli t = 1;
    // cin >> t;
    rep(i,1,t+1)
    {
        solve();
    }
    // cerr<<"\n\n\nTime elapsed: ";
    // cout<<"\n"<<(double)(clock()-_C)*1000.0/CLOCKS_PER_SEC<<"ms\n";
}

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

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

const int L=72;

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

  ll A,B;
  cin>>A>>B;

  vector<ll> v;
  for(ll i=A;i<=B;i++) v.push_back(i);
  int V=v.size();

  vector<int> is_prime(L+1,1);
  is_prime[0]=0;
  is_prime[1]=0;
  for(int i=2;i<=L;i++){
      if(is_prime[i]==0) continue;
      for(int j=2;i*j<=L;j++) is_prime[i*j]=0;
  }
  vector<int> primes;
  for(int i=2;i<=L;i++) if(is_prime[i]) primes.push_back(i);
  int P=primes.size();

  vector<vector<ll>> dp(V+1,vector<ll> (1<<P,0));
  dp[0][0]=1;
  for(int i=0;i<V;i++){
      for(int j=0;j<(1<<P);j++){
          ll value=dp[i][j];
          if(value==0) continue;
          dp[i+1][j]+=value;
          {
              int state=0;
              rep(k,P) if(v[i]%primes[k]==0) state+=(1<<k);
              if((j&state)==0) dp[i+1][j^state]+=value;
          }
      }
  }

  ll ans=0;
  rep(j,(1<<P)) ans+=dp[V][j];
  cout<<ans<<endl;

  return 0;
}


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

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(N), B(M);
  for (int i = 0; i < N; i++) cin >> A.at(i);
  for (int i = 0; i < M; i++) cin >> B.at(i);

  vector<vector<int>> DP(N + 1, vector<int>(M + 1));
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= M; j++) {
      if (i == 0 || j == 0) DP.at(i).at(j) = i + j;
      else if (A.at(i - 1) == B.at(j - 1)) {
        DP.at(i).at(j) = min(min(DP.at(i - 1).at(j) + 1,
                                 DP.at(i).at(j - 1) + 1),
                             DP.at(i - 1).at(j - 1));
      }
      else {
        DP.at(i).at(j) = min(min(DP.at(i - 1).at(j) + 1,
                                 DP.at(i).at(j - 1) + 1),
                             DP.at(i - 1).at(j - 1) + 1);
      }
    }
  }

  cout << DP.at(N).at(M) << endl;
}

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

#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define lli long long int
#define ld long double
#define pb push_back
#define pf push_front
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define pll pair<lli,lli>
#define ve vector<lli>
#define vep vector<pair<lli,lli> >
#define fr(i,a,n) for(lli i=a;i<(n);i++)
#define frj(j,a,n) for(lli j=a;j<(n);j++)
#define frx(i,a,n) for(lli i=a;i>(n);i--)
#define frxj(j,a,n) for(lli j=a;j>(n);j--)
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define db(x) cout << "[ " << x << " ]" << endl
#define db2(x,y) cout<<x<<" "<<y<<endl
#define dbarr(arr,a,n) fr(i,a,n) cout<<arr[i]<<" "; cout<<endl
#define yes cout<<"Yes"<<endl
#define no cout<<"No"<<endl

lli dp[1005][1005],a[1005],b[1005];

lli edit(lli i,lli j){
    if(i==0)return j;
    if(j==0)return i;
    if(dp[i][j]!=-1)return dp[i][j];
    if(a[i]==b[j]){
        dp[i][j]=edit(i-1,j-1);
    }
    else{
        dp[i][j]=1+min(edit(i-1,j-1),min(edit(i-1,j),edit(i,j-1)));
    }
    return dp[i][j];
}

void solve(){
    memset(dp,-1,sizeof(dp));
    lli n,m;
    cin>>n>>m;
    fr(i,1,n+1)cin>>a[i];
    fr(i,1,m+1)cin>>b[i];
    cout<<edit(n,m);
}

int main(){
    fast;
    lli t=1;
    // cin>>t;
    while(t--){
        solve();
    }
}


//#define _GLIBCXX_DEBUG
//#include "atcoder/all"
//using namespace atcoder;
#include <bits/stdc++.h>
#define int long long
#define ll long long
using ull = unsigned long long;
using namespace std;
#define Dump(x)                             \
    if (dbg) {                              \
        cerr << #x << " = " << (x) << endl; \
    }
#define overload4(_1, _2, _3, _4, name, ...) name
#define FOR1(n) for (ll i = 0; i < (n); ++i)
#define FOR2(i, n) for (ll i = 0; i < (n); ++i)
#define FOR3(i, a, b) for (ll i = (a); i < (b); ++i)
#define FOR4(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define bit(n, k) (((n) >> (k)) & 1) /*nのk bit目*/
namespace mydef {
const int INF = 1ll << 60;
const int MOD = 1e9 + 7;
template <class T>
bool chmin(T& a, const T& b) {
    if (a > b) {
        a = b;
        return 1;
    } else
        return 0;
}
template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    } else
        return 0;
}
void Yes(bool flag = true) {
    if (flag)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}
void No(bool flag = true) {
    Yes(!flag);
}
void YES(bool flag = true) {
    if (flag)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
}
void NO(bool flag = true) {
    YES(!flag);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T& val) {
    std::fill((T*)array, (T*)(array + N), val);
}
bool dbg = true;
}  // namespace mydef
using namespace mydef;
#define pb push_back
//#define mp make_pair
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define SZ(x) ((int)(x).size())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int, int>>
#define vvp vector<vector<pair<int, int>>>
#define pi pair<int, int>
//#define P pair<int, int>
//#define V vector<int>
//#define S set<int>
#define asn ans

int H, W;

void solve() {
}

signed main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    cin >> H >> W;
    int SUM = 0, MIN = INF;
    for (int i = 0; i < H * W; i++) {
        int x;
        cin >> x;
        chmin(MIN, x);
        SUM += x;
    }
    cout << SUM - MIN * H * W << endl;

    solve();
    return 0;
}

// Problem: F - Range Xor Query
// Contest: AtCoder - AtCoder Beginner Contest 185
// URL: https://atcoder.jp/contests/abc185/tasks/abc185_f
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
//
// Author: masked_huh
// Created: 13/12/2020 18:24

/*Success isn't permanent, failure isn't fatal,
it's the courage to continue that counts.*/

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

#define pfin(a) printf("%d\n",a);
#define pfln(a) printf("%lld\n",a);
#define pfis(a) printf("%d ",a);
#define pfls(a) printf("%lld ",a);
#define sfi(a) scanf("%d",&a);
#define sfl(a) scanf("%lld",&a);
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define f(i,a,b) for(int i=a;i<b;i++)
#define pb(a) push_back(a);
#define mp(a,b) make_pair(a,b)
typedef long long ll;
#define F first
#define S second
#define vi vector<int>
#define vc vector<char>

const ll mod=1e9+7;

const int N=300001;
int arr[N];
int tree[4*N];

void build(int node,int start,int end)
{
    if(start==end)
    {
        tree[node]=arr[start];
        return;
    }

    int mid=(start+end)/2;

    build(2*node,start,mid);
    build(2*node+1,mid+1,end);

    tree[node]=tree[2*node]^tree[2*node+1];

    return;
}

void update(int node,int start,int end,int idx,int val)
{
    if(start==end)
    {
        arr[idx]^=val;
        tree[node]^=val;
        return;
    }

    int mid=(start+end)/2;

    if(start<=idx && idx<=mid)
        update(2*node,start,mid,idx,val);
    else
        update(2*node+1,mid+1,end,idx,val);

    tree[node]=tree[2*node]^tree[2*node+1];

    return;
}

int query(int node,int start,int end,int l, int r)
{
    if(start>r||end<l)
        return 0;
    if(start>=l&&end<=r)
        return tree[node];

    int mid=(start+end)/2;

    int val1=query(2*node,start,mid,l,r);
    int val2=query(2*node+1,mid+1,end,l,r);

    return (val1^val2);
}

void solve()
{
    int n,q;
    sfi(n)
    sfi(q)

    f(i,1,n+1)
    	sfi(arr[i])

    build(1,1,n);

    while(q--)
    {
    	int t,x,y;
    	sfi(t)
    	sfi(x)
    	sfi(y)
    	if(t==1)
    	{
    		update(1,1,n,x,y);
    	}
    	else
    	{
    		int ans=query(1,1,n,x,y);

    		pfin(ans)
    	}
    }

    return;
}

int main()
{
	solve();

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

using LL = long long;
const int MOD = (int)1e9 + 7;
const int NS = (int)104;
int N, k;
char s[NS];
char dlr[NS][NS];
char dp[NS][NS][NS];
int tpow[NS];

char get(char A, char B){
    if(A == B){
        return A;
    }
    if(A == 'R' && B == 'S') return A;
    if(A == 'P' && B == 'R') return A;
    if(A == 'S' && B == 'P') return A;
    return B;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N >> k;
    cin >> s;
    tpow[0] = 1;
    for(int i = 1; i < NS; ++i){
        tpow[i] = tpow[i - 1] * 2 % N;
    }
    for(int i = 0; (1 << i) <= N; ++i){
        for(int l = 0; l + (1 << i) - 1 < N; ++l){
            int r = l + (1 << i) - 1;
            int mid = (l + r) >> 1;
            if(!i){
                dlr[l][r] = s[l];
            }
            else{
                dlr[l][r] = get(dlr[l][mid], dlr[mid + 1][r]);
            }
        }
    }
    for(int i = 1; i <= k; ++i){
        for(int l = 0; l < N; ++l){
            for(int r = 0; r < N; ++r){
                int mid = (l + tpow[i - 1] - 1 + N) % N;
                char lw, rw;
                if(i <= 20 && l + (1 << (i - 1)) - 1 <= mid){
                    lw = dlr[l][mid];
                }
                else{
                    lw = dp[i - 1][l][mid];
                }
                if(i <= 20 && (mid + 1) % N + (1 << (i - 1)) - 1 <= r){
                    rw = dlr[(mid + 1) % N][r];
                }
                else{
                    rw = dp[i - 1][(mid + 1) % N][r];
                }
//                cout << i << ' ' << l << ' ' << r << ' ' << lw << ' ' << rw << endl;
                dp[i][l][r] = get(lw, rw);
            }
        }
    }
    if(k <= 20 && (1 << k) <= N){
        cout << dlr[0][(1 << k) - 1];
    }
    else{
        cout << dp[k][0][(tpow[k] - 1 + N) % N];
    }
    return 0;
}

/*
      @author : ashnove
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define lld long double
#define F first
#define S second
#define Th third
#define endl "\n"
#define nl cout<<endl;
#define pb push_back
#define f(i,a,b) for(ll i=a;i<b;i++)
#define what_is(x) cerr << #x << " is " << x << endl;
#define mat vector<vector<ll>>
mat cn(ll n, ll m) {return vector< vector<ll> >(n, vector<ll>(m));}
bool comp1(char &s1, char &s2) {return s1 > s2;}
bool comp2(const pair<ll, pair<ll, ll>> &a,  const pair<ll, pair<ll, ll>> &b) {
      if (a.first > b.first ) return 1;
      if ( a.first == b.first && a.S.S > b.S.S ) return 1;
      return 0;
}
class Pair { public: ll first, second;};
bool comp3(const Pair &a, const Pair &b) {
      if (a.F > b.F) return 1;
      return 0;
}
class Trips { public: ll first, second, third;};
bool comp4(const Trips &a, const Trips &b) {
      if (a.Th > b.Th) return 1;
      return 0;
}

ll const mod = 1e9 + 7;
ll const inf = 1e18;
ll const maxn = 2e5 + 1;

ll f[maxn];
void fact(){
      f[0]  =1;
      for(ll i = 1; i < maxn; i++)
            f[i] = (f[i-1] * i)%mod;
}

bool isValid(ll i, ll j, ll n,ll m){
      return (1<=i&&i<=n && 1<=j&&j<=m);
}

void solve()
{
      ll n; cin >> n;
      ll m; cin >> m;
      string a[n];
      f(i,0,n) cin >> a[i];
      map<ll,ll> cnt;
      vector<ll> vv;
      ll o= 0, eve = 0;
      f(i,0,n){
            ll sum = 0;
            f(j,0,m) sum += a[i][j]-'0';
            vv.pb(sum);
            // cout << sum << endl;
            cnt[sum]++;
            if(sum&1) o++; else eve++;
      }
      ll ans = 0;
      for(ll i = 0; i < n-1; i++){
            // cout <<  cnt[vv[i]] << endl;
            // cnt[vv[i]]--;
            if(vv[i]&1)o--; else eve--;
            if(vv[i]&1)
                  ans += eve;
            else ans += o;

      }
      cout << ans << endl;
}

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

      // fact();
      ll tt = 1;
      // cin >> tt;
      f(testcases, 0, tt) {
            // cout << "Case #" <<testcases+1<<": ";
            solve();
      }
      // printf("TIME: %.3lf\n", double(clock()) / CLOCKS_PER_SEC);
      return 0;
}


#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,s,n) for (int i = (s); i < (n); ++i)
#define rrep(i,n,g) for (int i = (n)-1; i >= (g); --i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define len(x) (int)(x).size()
#define dup(x,y) (((x)+(y)-1)/(y))
#define pb push_back
#define Field(T) vector<vector<T>>
using namespace std;
using ll = long long;
using P = pair<int,int>;

int main() {
  int h, w, n, m;
  cin >> h >> w >> n >> m;
  vector<vector<int>> field(h, vector<int>(w, 0));
  rep(i,0,n) {
    int a, b;
    cin >> a >> b;
    field[a-1][b-1] = 1;
  }
  rep(i,0,m) {
    int c, d;
    cin >> c >> d;
    field[c-1][d-1] = 2;
  }
  rep(i,0,h) {
    bool ok = false;
    rep(j,0,w) {
      if (field[i][j] == 1) {
        ok = true;
      } else if (field[i][j] == 2) {
        ok = false;
      } else {
        if (ok) field[i][j] = 3;
      }
    }
    ok = false;
    rrep(j,w,0) {
      if (field[i][j] == 1) {
        ok = true;
      } else if (field[i][j] == 2) {
        ok = false;
      } else {
        if (ok) field[i][j] = 3;
      }
    }
  }
  rep(j,0,w) {
    bool ok = false;
    rep(i,0,h) {
      if (field[i][j] == 1) {
        ok = true;
      } else if (field[i][j] == 2) {
        ok = false;
      } else {
        if (ok) field[i][j] = 3;
      }
    }
    ok = false;
    rrep(i,h,0) {
      if (field[i][j] == 1) {
        ok = true;
      } else if (field[i][j] == 2) {
        ok = false;
      } else {
        if (ok) field[i][j] = 3;
      }
    }
  }
  int ans = 0;
  rep(i,0,h) rep(j,0,w) {
    if (field[i][j] == 1 || field[i][j] == 3) ans++;
  }
  cout << ans << endl;
  return 0;
}

#include <bits/stdc++.h>
#define DAU  ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define PLEC exit(0);
using namespace std;
const int dx[] {1, -1, 0, 0};
const int dy[] {0, 0, 1, -1};
char mat[2002][2002];
int dp[2002][2002], n, m, x, y, xv, yv;
pair<int, int> stop, curr;
vector<pair<int, int>> telep[30];
queue<pair<int, int>> q;
inline bool Inside(int a, int b) {
    return (1 <= a && a <= n && 1 <= b && b <= m);
}
int32_t main() {
    DAU
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        cin >> (mat[i] + 1);
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j) {
            dp[i][j] = 1e9;
            if (mat[i][j] == 'S') {
                q.emplace(i, j);
                dp[i][j] = 0;
            }
            else if (mat[i][j] == 'G')
                stop = {i, j};
            else if ('a' <= mat[i][j] && mat[i][j] <= 'z')
                telep[mat[i][j] - 'a'].emplace_back(i, j);
        }
    while (!q.empty()) {
        tie(x, y) = q.front();
        q.pop();
        for (int dir = 0; dir < 4; ++dir) {
            xv = x + dx[dir];
            yv = y + dy[dir];
            if (Inside(xv, yv) && mat[xv][yv] != '#' && dp[xv][yv] > dp[x][y] + 1) {
                dp[xv][yv] = dp[x][y] + 1;
                q.emplace(xv, yv);
            }
        }
        if(mat[x][y] >= 'a' && mat[x][y] <= 'z') {
            int val = mat[x][y] - 'a';
            while (!telep[val].empty()) {
                int xv = telep[val].back().first,
                    yv = telep[val].back().second;
                telep[val].pop_back();
                if(dp[xv][yv] > dp[x][y] + 1) {
                    dp[xv][yv] = dp[x][y] + 1;
                    q.emplace(xv, yv);
                }
            }
        }
    }
    tie(x, y) = stop;
    if (dp[x][y] == 1e9)
        dp[x][y] = -1;
    cout << dp[x][y];
    PLEC
}


#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

//const int MAX=INT_MAX;

const ll MAX=100000000000;

const int mod = 1000000007;
//const int mod = 998244353;

int main(){

    int N;
    cin>>N;
    ll T;
    cin>>T;
    vector<ll> A(N);
    for(int i=0;i<N;i++){
        cin>>A[i];
    }

    int M=N/2;

    vector<ll> A1;
    vector<ll> A2;

    for(int i=0;i<(1<<M);i++){
        int bit[22];
        ll sum=0;
        for(int j=0;j<M;j++){
            int Div=(1<<j);
            bit[j]=(i/Div)%2;
        }

        for(int j=0;j<M;j++){
            sum+=A[j]*bit[j];
        }
        //cout<<sum<<endl;

        A1.push_back(sum);
    }

    for(int i=0;i<(1<<(N-M));i++){
        int bit[22];
        ll sum=0;
        for(int j=0;j<N-M;j++){
            int Div=(1<<j);
            bit[j]=(i/Div)%2;
        }

        for(int j=0;j<N-M;j++){
            sum+=A[j+M]*bit[j];
        }
        //cout<<sum<<endl;
        A2.push_back(sum);
    }

    sort(A1.begin(),A1.end());
    sort(A2.begin(),A2.end());
/*
    for(ll i=0;i<A1.size();i++){
        cout<<A1[i]<<" ";
    }
    cout<<endl;
     for(ll i=0;i<A2.size();i++){
        cout<<A2[i]<<" ";
    }
    cout<<endl;
*/
    ll ans=0;

    for(ll i=0;i<A2.size();i++){
          if(T-A2[i]<0){
            break;
        }

        auto itr=lower_bound(A1.begin(),A1.end(),T-A2[i]);

       // cout<<*itr<<":"<<T-A2[i]<<":"<<distance(A1.begin(),itr)<<endl;

        if(itr==A1.end()){
            itr=prev(itr);
            //cout<<"prev"<<*itr<<endl;
            ans=max(*itr+A2[i],ans);

        }else if((*itr)+A2[i]==T){
            ans=T;

            break;
        }else{
            if(itr!=A1.begin()){
                 itr=prev(itr);
                 ans=max(*itr+A2[i],ans);

            }else{
                ans=max(A2[i],ans);
            }

        }

    }

    cout<<ans<<endl;

}

#include <algorithm>
#include <iostream>
int n, t, ans, a[45];
long long suf[45];
void dfs(int k, long long sum) {
	if(k > n) return;
	if(t - sum > ans)
		ans = t - sum;
	else if(t - sum + suf[k + 1] <= ans)
		return;
	if(!sum) {
		printf("%lld", t);
		exit(0);
	}
	if(sum - a[k + 1] >= 0) dfs(k + 1, sum - a[k + 1]);
	dfs(k + 1, sum);
}

const bool cmp(int x, int y) {
	return x > y;
}

int main() {
	scanf("%d%d", &n, &t);
	for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
	std::sort(a + 1, a + n + 1, cmp);
	for(int i = n; i >= 1; i--) suf[i] = suf[i + 1] + a[i];
	dfs(0, t);
	printf("%lld", ans);
	return 0;
}


#include <bits/stdc++.h>

const long long base = 2000000000ll;
const long long ten[6] = {1ll, 10ll, 100ll, 1000ll, 10000ll, 100000ll};

using namespace std;

long long str2ll(string str) {
  long long ret = 0;

  bool fp = false;
  int f = 0;
  for (int i = 0; i < (int)str.size(); i++) {
    if (str[i] == '-') continue;
    else if (str[i] == '.') {
      fp = true;
      ret *= ten[4];
    } else if (fp) {
      ret += ten[3 - f] * (str[i] - '0');
      ++f;
    } else {
      ret *= 10;
      ret += str[i] - '0';
    }
  }

  if (!fp) ret *= ten[4];

  return ret;
}

long long sqroot(long long x) {
  long long ok = 0, ng = 2000000000ll;
  while (ng - ok > 1) {
    long long mid = (ok + ng) >> 1;

    (mid * mid <= x ? ok : ng) = mid;
  }
  return ok;
}

int main() {
  long long X, Y, R;

  string X_str, Y_str, R_str;
  cin >> X_str >> Y_str >> R_str;

  X = str2ll(X_str) + base;
  Y = str2ll(Y_str) + base;
  R = str2ll(R_str);

  long long ans = 0;
  for (long long x = (X - R) / ten[4] * ten[4] - ten[4]; x <= (X + R) / ten[4] * ten[4] + ten[4]; x += ten[4]) {
    if (R * R - (x - X) * (x - X) < 0) continue;

    long long s = sqroot(R * R - (x - X) * (x - X));
    ans += (Y + s) / ten[4] - (Y - s - 1) / ten[4];
  }

  cout << ans << endl;

  return 0;
}

#include <bits/stdc++.h>

#define fr first
#define sc second
#define mk make_pair
#define pb push_back
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const int inf = 1e9 + 7;
const int N = 1e6 + 5;

void solve() {
    ll b, c, ans = 1;
    cin >> b >> c;
    if (b != 0 && c > 0) ans ++;
    if (b < 0) {
        ans += c / 2;
        ans += (c - 1) / 2;
        ans += min(abs(b), (c - 1) / 2);
        ans += min(abs(b), (c - 2) / 2);
        if (abs(b) <= (c - 2) / 2) ans --;
    } else if (b == 0) {
        ans += c / 2;
        ans += (c - 1) / 2;
    } else {
        ans += min(b, c / 2);
        if ((c - 1) / 2 >= b) ans --;
        ans += min(b, (c - 1) / 2);
        ans += (c - 1) / 2;
        ans += (c - 2) / 2;
    }
    cout << ans << endl;
}

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


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

int main(){
  long long N = 0;
  long long cnt = 0;
  long long cmb = 0;

  cin >> N;
  vector<long long> A(N);
  vector<long long> B(N);
  for(int i=0; i<N; i++){
    cin >> A.at(i);
    B.at(i) = A.at(i) % 200;
  }

  for(int j=0; j<200; j++){
    cmb = 0;
    for(int i=0; i<N; i++){
      if(j == B.at(i)) cmb++;
    }
    if(cmb>0)cnt = cnt + cmb*(cmb-1)/2;
  }

  cout << cnt << endl;
}

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

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>

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

using namespace std;

typedef pair<int, int> P;

class comb_n{
private:
void comb_num(int n, int k, int ans = 0, int i = 0){
    if(i == k){
        _comb = ans;
        return;
    }
    if(i == 0){
        comb_num(n, k, ans + n, i+1);
    }
    else{
        comb_num(n, k, ans * (n - i) / (i + 1), i+1);
    }
    return;
}
public:
    int _comb;//この値を使う
    comb_n(int n, int k){
        comb_num(n, k);
    }
};

class CRingosFavoriteNumbers2 {
public:
	void solve(std::istream& cin, std::ostream& cout) {
        int n;
        cin >> n;
        vector<int> a(n);
        REP(i,n) cin >> a[i];

        map<int, int> m;
        REP(i,n){
            m[a[i]%200]++;
        }
        int ans = 0;
        for(auto itr = m.begin(); itr != m.end(); itr++){
            auto cn = comb_n(itr->second, 2);
            ans += cn._comb;
        }
        cout << ans << endl;
	}
};

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


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define asort(a,n) sort(a,a+n)
#define vsort(v) sort(v.begin(),v.end())
#define all(v) v.begin(),v.end()
#define run ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0);
#define mod 1000000007
#define LL_MAX LLONG_MAX
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl

//********************************* mekch ************************************//

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

}

        /*
        for(i=0;i<n;i++)
       {
           cout<<c[i] ;
           cout<<"--->"<<d[i]<<endl;
       }

       for(i=0;i<n;i++)
         cout<<a[i]<<" ";

         cout<<endl;
         */

int main()
 {
  run;
  ll n;
  cin>>n;
  if(n>=0)
   cout<<n;
   else
   cout<<0;

 }

#include<iostream>
#include<utility>
#include<vector>
#include<climits>
#include<set>
using namespace std;

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

  char ch1,ch2,ch3;
  cin>>ch1>>ch2>>ch3;
  if(ch1==ch2 && ch1==ch3) cout<<"Won\n";
  else cout<<"Lost\n";

  return 0;
}


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

void solve(long long N){
    cout << ((N - 1) / 100) + 1 << endl;
}

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

#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pb push_back
#define fastio ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0);
const int MOD = 1000000007;
#define PI = acos(-1)
using namespace std ;
int main(){
   fastio ;
   int n ;
   cin >> n ;
   int ans = n/100;
   if(n % 100) ans++;
   cout << ans ;
}


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

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

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

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

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

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

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

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

	read(t);
	while (t--)
	{
	    readc(n, s, k);
	    int x, y, g;
	    bool status = find_any_solution(n, -k, s, x, y, g);
	    if (!status) {cout << -1 << endl; continue;}

	    n /= g;
	    int z = y/n, ans = inf;
	    if (y - z*n > 0) ans = min(ans, y - z*n);
	    if (y - (z - 1)*n > 0) ans = min(ans, y - (z - 1)*n);
	    print(ans);
	}
}

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

using TLT = tuple<long long, long long, long long>;
//{a, b} -> {g, x, y} : ax+by=g=gcd(a,b)
TLT extgcd(const long long a, const long long b) {
    if (b == 0) return make_tuple(a, 1, 0);
    else {
        TLT ret = extgcd(b, a%b);
        return make_tuple(get<0>(ret), get<2>(ret), get<1>(ret)-a/b*get<2>(ret));
    }
}
//solve Ax = b (mod m)
long long solver (long long a, long long b, long long m){
    long long x, y, g;
    tie(x, y, g) = extgcd(a, b);
    if(b % m) return -1;
    else {

    }
}

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

    int t;
    cin >> t;
    while(t--){
        int n, s, k;
        cin >> n >> s >> k;
        long long x, y, g;
        tie(g, x, y) = extgcd(k, n);
        if(s % g) cout << -1 << endl;
        else {
            s /= g;
            n /= g;
            x *= (-s);
            cout << (x%n+n)%n << endl;
        }
        //kx = -s (mod n)
        //(k/g)x = -(s/g)
    }
    return 0;
}


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

vector<int> a(10,0),b(10.0),c(10,0);

int f(int i){
    int S=0;
    for(int j=1;j<=9;j++){
        if(j!=i) S+=j*(int)pow(10,b.at(j));
        else S+=j*(int)pow(10,b.at(j)+1);
    }
    return S;
}

int g(int i){
    int S=0;
    for(int j=1;j<=9;j++){
        if(j!=i) S+=j*(int)pow(10,c.at(j));
        else S+=j*(int)pow(10,c.at(j)+1);
    }
    return S;
}

int main(){
    int64_t k,cnt1,cnt2=0;
    string s,t;
    cin >> k >> s >> t;
    cnt1=(9*k-8)*(9*k-9);
    int S,T;
    for(int i=0;i<4;i++){
        int x=s.at(i)-'0',y=t.at(i)-'0';
        a.at(x)++; b.at(x)++;
        a.at(y)++; c.at(y)++;
    }
    for(int i=1;i<=9;i++){
        if(a.at(i)+1>k) continue;
        for(int j=1;j<=9;j++){
            if(a.at(j)+1>k || (i==j && a.at(j)+2>k)) continue;
            S=f(i),T=g(j);
            if(S>T){
                if(i!=j) cnt2+=(k-a.at(i))*(k-a.at(j));
                else cnt2+=(k-a.at(i))*(k-a.at(j)-1);
            }
        }
    }
    long double d=(long double)cnt2/(long double)cnt1;
    cout << fixed << setprecision(12);
    cout << d << endl;
    return 0;
}

#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
using ll = int64_t;

ll score(string S){
    vector<ll> cnt(10);
    iota(cnt.begin(), cnt.end(), 0);
    for(char c : S) cnt[c - '0'] *= 10;
    return accumulate(cnt.begin(), cnt.end(), 0);
}
int main(){
    ll K;
    string S, T;
    cin >> K >> S >> T;
    vector<ll> cnt(10, K);
    for(char c : S + T) cnt[c - '0']--;
    ll win = 0;
    for(ll x = 1; x <= 9; x++) for(ll y = 1; y <= 9; y++){
        S.back() = '0' + x;
        T.back() = '0' + y;
        if(score(S) <= score(T)) continue;
        win += cnt[x] * (cnt[y] - (x == y));
    }
    const ll rem = 9 * K - 8;
    cout << double(win) / rem / (rem - 1) << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define x first
#define y second
#define bg begin()
#define ed end()
#define pb push_back
#define mp make_pair
#define sz(a) int((a).size())
#define R(i,n) for(int i(0);i<(n);++i)
#define L(i,n) for(int i((n)-1);i>=0;--i)
const int iinf=0x3f3f3f3f;
const ll linf=0x3f3f3f3f3f3f3f3f;

//Data
const int N=2e5;
int n,a[N],b[N],p[N],q[N],o[N];
vector<pair<int,int>> ns;

//Function

//Main
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n;
    R(i,n) cin>>a[i],o[i]=i;
    R(i,n) cin>>b[i];
    R(i,n) cin>>p[i],q[--p[i]]=i;
    R(i,n)if(i!=p[i]&&a[i]<=b[p[i]])
        return !(cout<<-1<<'\n');
    sort(o,o+n,[&](int i,int j)
        {return a[i]<a[j];});
    R(t,n){
        int i=o[t];
        if(i!=p[i]){
            ns.pb({i,q[i]});
            int tp=p[i],tq=q[i];
            p[tq]=tp,p[i]=i;
            q[tp]=tq,q[i]=i;
        }
    }
    cout<<sz(ns)<<'\n';
    for(auto u:ns) cout<<u.x+1<<' '<<u.y+1<<'\n';
    return 0;
}
/*
每个连通块，从人轻到人重挪
*/

#include<bits/stdc++.h>
#define pb           push_back
#define pf           push_front
#define in           insert
#define ff           first
#define ss           second
#define int          long long
#define rep(i,x,m)   for(int i=x;i<m;i++)
#define MOD          1000000007
#define endl         "\n"
#define all(v)       v.begin(),v.end()
#define rall(v)      v.rbegin(),v.rend()
#define vi           vector<int>
#define pi           pair<int,int>
#define sz(x)       (int)x.size()
#define pq           priority_queue<int>
#define fix(y)       fixed<<setprecision(y)
#define print(v)     for(int i=0;i<v.size();i++) cout<<v[i]<<" ";cout<<endl
#define FIO         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

int power(int n,int m)
{int p=1;if(m==0)return 1;p=(power(n,m/2)%MOD);p=(p%MOD*p%MOD)%MOD;
return(m&1?((p%MOD*n%MOD)%MOD):(p%MOD));}

void solve()
{
     int n;
     cin>>n;
     int a[n];
     int sum=0,ans=0;
     rep(i,0,n){
      cin>>a[i];sum+=a[i];}
      sort(a,a+n);
      rep(i,0,n)
      {
        //cout<<a[i]*(n-i-1)<<" "<<(sum-a[i])<<endl;
             ans+=abs(a[i]*(n-1-i)-(sum-a[i]));
             sum-=a[i];
      }
      cout<<ans;

}

signed main()
{
        FIO

        int t=1;
        //cin>>t;
        while(t--)
        solve();
        cerr<<(float)clock()/CLOCKS_PER_SEC*1000<<" ms"<<endl;

        return 0;
}


#include<bits/stdc++.h>
#define print(x) cout << (#x) << ": " << (x) << endl
#define p1d(x) cout << (#x) << ": ["; for(auto& zz: x) cout << zz << " "; cout << "]\n"
#define p2d(x) cout << (#x) << ": \n["; for(auto& vec: x) {for(auto& v: vec) cout << v << " "; cout << ",\n";}
#define p2s(x) cout << (#x) << ": ["; for(auto& vec: x) {cout << vec << ",\n";}
#define pb push_back
#define endl "\n"
// __builtin_popcount

typedef long long ll;
using namespace std;
int static fast = [](){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0); return 0;
}();
// freopen("input.txt", "r", stdin);
int main() {
    int n, val;
    cin >> n;
    vector<int> rec(n+1, 0);
    for(int i = 0; i < n; i++) {
        cin >> val;
        rec[val] = 1;
    }
    for(int i = 1; i <= n; i++) {
        if (rec[i] == 0) {
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define fastio   ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl "\n"

int main()
{
    fastio;

    int n,a;
    cin>> n;

    vector <int> v;
    list <int> lst;

    while(n!=0)
    {
        cin>> a;
        v.push_back(a);
        lst.push_back(a);
        n--;
    }

    lst.sort();
    lst.unique();

    if(lst.size()==v.size())
        cout<< "Yes";
    else
        cout<< "No";

    return 0;
}


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

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

void solve(std::string S){
	reverse(S.begin(), S.end());

	rep(i, S.size()){
		char w = S[i];
		if(w == '0' or w == '1' or w == '8'){
			cout << w;
			continue;
		}

		if(w == '9'){
			cout << '6';
			continue;
		}

		if(w == '6'){
			cout << '9';
			continue;
		}
	}

	cout << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
    std::string S;
    std::cin >> S;
    solve(S);
    return 0;
}

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

int main(){
  string S;
  cin >> S;

  S = regex_replace(S, regex("6"), "5");
  S = regex_replace(S, regex("9"), "6");
  S = regex_replace(S, regex("5"), "9");
  string S_reversed (S.rbegin(), S.rend());

  cout << S_reversed << endl;
}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define repa(i,a,n) for (long long i = (a); i < (long long)(n); i++)
#define Rrep(i,n) for (long long i = (long long)(n)-1; i >= 0; i--)
#define Rrepa(i,a,n) for (long long i = (long long)(n)-1; i >= a; i--)
using ll = long long;
class Prints {
    class __Prints {
    public:
        __Prints(const char* sep, const char* term) : sep(sep), term(term) {}
        template <class... Args>
        void operator()(const Args&... args) const { print(args...); }
        template <typename T>
        void pvec(const T& vec, size_t sz) const {
            for (size_t i = 0; i < sz; i++)
                std::cout << vec[i] << (i == sz - 1 ? term : sep);
        }
        template <typename T>
        void pmat(const T& mat, size_t h, size_t w) {
            for (size_t i = 0; i < h; i++)
                for (size_t j = 0; j < w; j++)
                    std::cout << mat[i][j] << (j == w - 1 ? term : sep);
        }

    private:
        const char *sep, *term;
        void print() const { std::cout << term; }
        void print_rest() const { std::cout << term; }
        template <class T, class... Tail>
        void print(const T& head, const Tail&... tail) const { std::cout << head, print_rest(tail...); }
        template <class T, class... Tail>
        void print_rest(const T& head, const Tail&... tail) const { std::cout << sep << head, print_rest(tail...); }
    };

public:
    Prints() {}
    __Prints operator()(const char* sep = " ", const char* term = "\n") const { return __Prints(sep, term); }
};
Prints print;
//一次元配列はprint().pvec(vec,要素数);
//二次元配列はprint().pmat(vec,要素数1,要素数2);

int main() {
    int si,sj,ti,tj;
    rep(k,1000) {
        cin >> si >> sj >> ti >> tj;
        string ans;
        if (si < ti) rep(i,ti-si) ans += 'D';
        else rep(i,si-ti) ans += 'U';

        if (sj < tj) rep(i,tj-sj) ans += 'R';
        else rep(i,sj-tj) ans += 'L';
        cout << ans << endl;

        int score;
        cin >> score;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (long long i = 0; i < (n); i++)
using ll = long long;
using P = pair<ll, ll>;
template<class T>
bool chmax(T &a, T b) {if (a < b) {a = b;return true;}else return false;}
template<class T>
bool chmin(T &a, T b) {if (a > b) {a = b;return true;}else return false;}
// struct Edge { ll to; ll cost; Edge(ll t, ll c) { to = t; cost = c;} };
// using Graph = vector<vector<Edge>>;
// using Graph = vector<vector<ll>>;
// priority_queue<P, vector<P>, greater<P>> q;

const int INF = (1 << 30) - 1;
const ll LINF = (1LL << 60) - 1;
const double PI = acos(-1);

int main() {
    string s;
    cin >> s;
    reverse(s.begin(), s.end());
    string t = "";
    for (ll i = 0; i < s.size(); i++) {
        if (s[i] == '6') t = t + '9';
        else if (s[i] == '9') t = t + '6';
        else t = t + s[i];
    }
    cout << t << endl;
    return 0;
}

//小数点の出力
//cout << fixed << std::setprecision(15) << ans << endl;

#include <bits/stdc++.h>//For vectors,pairs
using namespace std;

#define ll long long
#define rep(i,j,n)  for(int i=j;i<n;i++)
#define _rep(i,n) for(int i=n;i>=0;i--)

void solve();

void cp(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("error.txt", "w", stderr);
    freopen("output.txt", "w", stdout);
    #endif
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
   // cp();
    int t=1;//cin>>t;
    while(t--){
        solve();
        cout<<endl;
    }

   // cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
    return 0;
}
void solve(){
    string s;
    cin>>s;
    ll n = s.size();
    rep(i,0,n){
        if(s[i]=='6'){
            s[i] = '9';
            continue;
        }
        if(s[i]=='9'){
            s[i] = '6';
        }
    }
    _rep(i,n-1){
        cout<<s[i];
    }
    return;
}