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

ll fib[105];
int p[105];

int main(){
    ll n; cin >> n;
    ll N = n;
    fib[0] = fib[1] = 1;
    rep(i,100){
        fib[i+2] = fib[i] + fib[i+1];
        if(fib[i+2] > 1e18 + 100) break;
    }
    int mx = 0;
    for(int i=87; i>=0; i--){
        if(n >= fib[i]){
            p[i] = 1;
            chmax(mx,i);
            n -= fib[i];
        }
    }
    vl ans;
    ll x = 0, y = 0;
    for(int i=mx; i>=0; i--){
        if(i & 1 ^ 1){
            if(p[i]) x++, ans.push_back(1);
            y = x + y;
            ans.push_back(4);
        }else{
            if(p[i]) y++, ans.push_back(2);
            x = x + y;
            ans.push_back(3);
        }
    }
    assert(x == N);
    cout << ans.size() << "\n";
    for(auto v : ans) cout << v << "\n";
    //debug(x,y,ans.size());
}

#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	string s;
	cin >> s;
	int num[15] = {0};

	for(int i = 0; i < s.length(); i++)
	{
		num[(s[i]-'0')]++;
	}
	int ans = 0;
	if(s.length() == 1)
	{
		if((s[0]-'0') == 8) ans = 1;
	}
	if(s.length() == 2)
	{
		int a  = s[0] - '0';
		int b = s[1] - '0';
		if((a*10+b)% 8 == 0) ans = 1;
		if((b*10+a) % 8 == 0) ans = 1;

	}
	for(int i = 8; i <= 999; i += 8)
	{
		int need[15] = {0};
		int a = i % 10,b = (i / 10) % 10, c =  i / 100;
		need[a]++;
		need[b]++;
		need[c]++;
		if(num[a] >= need[a] && num[b] >= need[b] && num[c] >= need[c]) ans = 1;
	}
	if(!ans)
	{
		cout << "No" << endl;
		return 0;
	}
	else cout << "Yes" << endl;

	return 0;
}


#include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
#define rep(i, n) for(int i=0;i<n;i++)
#define all(i, v) for(auto& i : v)
typedef long long ll;
using namespace std;
const ll mod=1000000007, INF=(1LL<<60);
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;

int n, m;
vector<vector<pair<int, int> > > v;
vector<pair<int, int> > h;
vector<int> ans;
vector<int> c;

void dfs(int now){
    all(to, v[now]) if(!ans[to.second]){
        ans[to.second] = (h[to.second].first == now ? 1 : -1);
        dfs(to.first);
    }
}

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

    cin >> n >> m;
    v.resize(n);
    c.resize(n);
    ans.resize(m, 0);

    rep(i, m){
        int a, b;
        cin >> a >> b;
        a--; b--;
        h.push_back({a, b});
        v[a].push_back({b, i});
        v[b].push_back({a, i});
    }

    rep(i, n) cin >> c[i];
    rep(i, n) all(to, v[i]) if(c[to.first] > c[i]) ans[to.second] = (h[to.second].first == i ? -1 : 1);
    rep(i, n){
        vector<int> s;
        all(to, v[i]) if(!ans[to.second] && c[i] == c[to.first]) s.pb(to.second);

        int maki = s.size();
        rep(to, maki){
            if(to == maki - 1) {
                if(h[s[to]].first == i){
                    ans[s[to]] = 1;
                    dfs(h[s[to]].second);
                }else{
                    ans[s[to]] = -1;
                    dfs(h[s[to]].first);
                }
            }else ans[s[to]] = (h[s[to]].first == i ? -1 : 1);
        }
    }
    rep(i, m) cout << (ans[i] == 1 ? "->" : "<-") << endl;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

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

  vector<vector<int>> graph(N, vector<int>(N, 0));
  vector<array<int, 2>> edges(M);

  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    --a; --b;

    ++graph[a][b];
    ++graph[b][a];
    edges[i] = {a, b};
  }

  vector<int> c(N);

  for (int i = 0; i < N; i++) cin >> c[i];

  vector<bool> used(N, false);

  for (int i = 0; i < N; i++) {
    stack<array<int, 2>> s;
    vector<bool> candidate(N, false);

    if (!used[i]) {
      s.push({i, -1});
      candidate[i] = true;
    }

    while (s.size()) {
      auto [node, parent] = s.top();

      bool exists = false;

      for (int next = 0; next < N; next++) {
        if (!graph[node][next] || used[next] || candidate[next]) continue;

        if (c[node] == c[next]) {
          exists = true;
          candidate[node] = true;
          s.push({next, node});
          break;
        }
      }

      if (!exists) {
        if (parent >= 0) {
          graph[node][parent] = 0;
        }

        for (int next = 0; next < N; next++) {
          if (!graph[node][next] || used[next]) continue;

          if (c[node] == c[next]) graph[next][node] = 0;
        }

        used[node] = true;
        s.pop();
      }
    }

    for (int j = 0; j < N; j++) {
      if (!graph[i][j]) continue;

      if (c[i] > c[j]) {
        graph[j][i] = 0;
      } else if (c[i] < c[j]) {
        graph[i][j] = 0;
      }
    }
  }

  for (int i = 0; i < M; i++) {
    if (graph[edges[i][0]][edges[i][1]]) cout << "->" << endl;
    else if (graph[edges[i][1]][edges[i][0]]) cout << "<-" << endl;
    else exit(1);
  }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll; //int:2*10**9
typedef long double ld;
typedef pair<ll,ll> P;
#define REP(i,n) for(ll i = 0; i<(ll)(n); i++)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define pb push_back
#define MOD 1000000007 //998244353
#define PI 3.141592653
#define INF 100000000000000 //14
//cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);

ll powe(ll a, ll b){//aのb乗のmod
    ll ret = 1;
    while(0 < b){
        if((b%2) == 0){
            a = (a*a);
            b >>= 1;
        }
        else{
            ret = (ret*a);
            b--;
        }
    }
    return ret;
}

int main(){
    string s, t, u; cin >> s >> t >> u;
    vector<ll> cl(26,0);
    vector<ll> hl(26,0);
    REP(i,s.length()) {
        if (i==0) hl[s[i]-'a']=1;
        cl[s[i]-'a']=1;
    }
    REP(i,t.length()) {
        if (i==0) hl[t[i]-'a']=1;
        cl[t[i]-'a']=1;
    }
    REP(i,u.length()) {
        if (i==0) hl[u[i]-'a']=1;
        cl[u[i]-'a']=1;
    }
    ll cnter = 0;
    REP(i,26) if (cl[i]==1) cnter++;
    if (cnter>10) {
        cout << "UNSOLVABLE" << endl;
        return 0;
    }
    ll ind = 0;
    REP(i,26) {
        if (cl[i]) {
            cl[i]=ind;
            ind++;
        }
        else {
            cl[i]=-1;
        }
    }
    vector<ll> keisu(26,0);
    ll ss = s.length(); ll tt = t.length(); ll uu = u.length();
    REP(i,s.length()) keisu[s[i]-'a']+=powe(10,ss-1-i);
    REP(i,t.length()) keisu[t[i]-'a']+=powe(10,tt-1-i);
    REP(i,u.length()) keisu[u[i]-'a']-=powe(10,uu-1-i);
    vector<ll> digits = {0,1,2,3,4,5,6,7,8,9};
    do {
        ll val = 0;
        bool jud = true;
        REP(i,26){
            if (hl[i]==1 && digits[cl[i]]==0) jud = false;
        }
        if (jud==false) continue;
        REP(i,26) {
            if (cl[i]==-1) continue;
            val+=digits[cl[i]]*keisu[i];
        }
        if (val==0) {
            // REP(i,26) cout << keisu[i] << " ";
            // cout << endl;
            // REP(i,26) cout << cl[i] << " ";
            // cout << endl;
            // REP(i,26) cout << digits[cl[i]] << " ";
            // cout << endl;
            // REP(i,26) cout << hl[i] << " ";
            // cout << endl;
            vector<char> charl(26,'0');
            REP(i,26) {
                if (cl[i]!=-1) charl[i]+=digits[cl[i]];
            }
            REP(i,ss) s[i]=charl[s[i]-'a'];
            REP(i,tt) t[i]=charl[t[i]-'a'];
            REP(i,uu) u[i]=charl[u[i]-'a'];
            cout << s << endl;
            cout << t << endl;
            cout << u << endl;
            return 0;
        }
    } while (next_permutation(digits.begin(),digits.end()));
    cout << "UNSOLVABLE" << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define REP(a,b) for(long long a = 0;a < b;++a)
using ld = long double;
unsigned long xor128() {
  static unsigned long x=time(NULL), y=362436069, z=521288629, w=88675123;
  unsigned long t=(x^(x<<11));
  x=y; y=z; z=w;
  return ( w=(w^(w>>19))^(t^(t>>8)) );
}

typedef struct Sponsor{
    int x;
    int y;
    int area;
} Sponsor;
typedef struct AD{
    int x;
    int y;
    int dx;
    int dy;
    Sponsor sponsor;
} AD;

inline bool check_intersect(AD a,AD b){
    bool ans = (min(a.x,b.x) <= max(a.x,b.x) and max(a.x,b.x) < min(a.x+a.dx,b.x + b.dx));
    ans &= (min(a.y,b.y) <= max(a.y,b.y) and max(a.y,b.y) < min(a.y+a.dy,b.y + b.dy));
    return ans;
}
bool validate(vector<AD> inputs){
    for(int i = 0;i < inputs.size();++i){
        if(inputs[i].x + inputs[i].dx > 10000 or inputs[i].y + inputs[i].dy > 10000) return false;
        for(int q = i+1;q < inputs.size();++q){
            if(check_intersect(inputs[i],inputs[q]) == true) return false;
        }
    }
    return true;
}
ld scoring(vector<AD> inputs,ld border){
    ld ans = 0;
    for(int i = 0;i < inputs.size();++i){
        if(inputs[i].x <= inputs[i].sponsor.x and inputs[i].sponsor.x < inputs[i].x + inputs[i].dx
            and inputs[i].y <= inputs[i].sponsor.y and inputs[i].sponsor.y < inputs[i].y + inputs[i].dy){
                ld real_area = inputs[i].dx * inputs[i].dy;
                ld expected_area = inputs[i].sponsor.area;
                ans += 1.0L - abs(border - pow(1 - min(real_area,expected_area)/max(real_area,expected_area),2.0));
            }else return -1;
    }
    ans /= (ld)inputs.size();
    return ans;
}

int main(){
    int n;
    cin >> n;
    vector<AD> inputs;
    REP(i,n){
        int a,b,c;
        cin >> a >> b >> c;
        inputs.push_back(AD{a,b,1,1,Sponsor{a,b,c}});
    }
    ld now_score = scoring(inputs,0);
    for(int i = 0;i < 3000000;++i){
        int target = xor128() % inputs.size();
        AD tmp = inputs[target];
        int dir = xor128() % 8;
        if(i % 20000 != 0){
            if(dir == 0){
                inputs[target].dx += 1;
            }else if(dir == 1){
                inputs[target].dy += 1;
            }else if(dir == 2){
                inputs[target].x -= 1;
                inputs[target].dx += 1;
            }else if(dir == 3){
                inputs[target].y -= 1;
                inputs[target].dy += 1;
            }else if(dir == 4){
                inputs[target].x += 1;
            }else if(dir == 5){
                inputs[target].y += 1;
            }else if(dir == 6){
                inputs[target].x -= 1;
            }else if(dir == 7){
                inputs[target].y -= 1;
            }
        }
        bool ok = true;
        for(int q = 0;q < inputs.size();++q){
            if(q != target and check_intersect(inputs[q],inputs[target]) == true){
                ok = false;
            }
        }
        if(inputs[target].dx <= 0 or inputs[target].dy <= 0 or inputs[target].x < 0 or inputs[target].y < 0) ok = false;
        if(inputs[target].x + inputs[target].dx > 10000 or inputs[target].y + inputs[target].dy > 10000) ok = false;
        ld score = 0;
        int hoge = (ld)(3000000.0 - i) / 300000.0;
        ld prob = hoge / 10.0;
        prob *= prob;
        score = scoring(inputs,prob) + (1.00L - prob);
        if(ok and score >= now_score){
            now_score = score;
        }else{
            inputs[target] = tmp;
        }
    }
    for(int i = 0;i < n;++i){
        cout << inputs[i].x << " " << inputs[i].y << " " << inputs[i].x + inputs[i].dx << " " << inputs[i].y + inputs[i].dy << endl;
    }
}


// Siddhant Dugar

#include<bits/stdc++.h>
#include<string>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <climits>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define REP(i, a, b) for(int i = (a); i <=(b); ++i)
#define REPD(i, a, b) for(int i = (a); i >=(b); --i)
#define TR(it, a) for(typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define RESET(a, v) memset(a, (v), sizeof(a))
#define SZ(a) (int(a.size()))
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define II pair<int, int>
#define X first
#define Y second
#define VI vector<int>
#define VII vector<II>
#define endl '\n'
#define ll long long int
#define mod 1000000007
#define mk(arr,n,type) type*arr=new type[n];
using namespace std;

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

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

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

    int x, y;
    cin >> x;
    cin >> y;
    if (abs(x - y) < 3)cout << "Yes";
    else cout << "No";

    return 0;

}

#include<iostream>
using namespace std;

int main(){
    int X, Y;
    cin >> X >> Y;

    if (X + 3 > Y && Y + 3 > X) {
        cout << "Yes" << endl;
    }
    else{
        cout << "No" << endl;
    }
    return 0;
}


/**
*    author:  shu8Cream
*    created: 02.05.2021 10:37:54
**/

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

int n;
double D,H;

bool check(double mid, vi &d, vi & h){
    rep(i,n){
        if((h[i]-mid)*D>(H-mid)*d[i]) return false;
    }
    return true;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin >> n >> D >> H;
    vi d(n),h(n);
    rep(i,n) cin >> d[i] >> h[i];
    double ok = 0.0, ng = 10000.0;
    int loop = 100;
    while(loop--){
        double mid = (ok + ng) / 2.0;
        if(check(mid,d,h)) ng = mid;
        else ok = mid;
    }
    printf("%.10lf\n", ok);
}

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

typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef vector<int> vi;
mt19937 mrand(time(0));
#define debug(...) fprintf(stderr, __VA_ARGS__)
ll get(ll r) { return ((ll)mrand() * mrand() % r + r) % r; }
ll get(ll l, ll r) { return get(r - l + 1) + l; }

int n, D, H; db ans;
signed main() {
    scanf("%d %d %d", &n, &D, &H);
    for(int i = 1; i <= n; i++) {
        int d, h; scanf("%d %d", &d, &h);
        // y = (H - h) / (D - d) * (x - D) + H
        // x = 0  ==>  y = (H - h) / (D - d) * (0 - D) + H
        ans = max(ans, 1. * (H - h) / (D - d) * (0 - D) + H);
    }
    printf("%.10f\n", ans);
    debug("time=%.4f\n", (db)clock()/CLOCKS_PER_SEC);
    return 0;
}


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

#define mp make_pair
#define pb push_back
#define ll long long int
#define sd(x) scanf("%lld",&x)
#define sdi(x) scanf("%d",&x)
#define sdc(c) scanf("%c",&c)
#define inf 1000000000000000000ll
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define bits(x) __builtin_popcountll(x)
#define ld long double
#define test() ll test; cin>>test; while(test--)
#define fi first
#define se second
#define all(x) x.begin(),x.end()

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

clock_t time_p = clock();
void time_taken()
{
    time_p = clock() - time_p;
    cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}

inline ll GCD(ll x, ll y) {
    if(x<y) swap(x,y);
    if(x==0) return y;
    if(y==0) return x;
    return GCD(x%y,y);
}

ll phi(ll n) {
    ll result = n;
    for (ll i = 2; i * i <= n; i++) {
        if(n % i == 0) {
            while(n % i == 0)
                n /= i;
            result -= result / i;
        }
    }
    if(n > 1)
        result -= result / n;
    return result;
}

ll power(ll x, ll n, ll mod) {
    ll res = 1;
    x %= mod;
    while(n) {
        if(n&1) {
            res = ((res*x)%mod+mod)%mod;
        }
        x = ((x*x)%mod+mod)%mod;
        n>>=1;
    }

    return res;
}

const int MOD = 998244353;

inline ll add(ll x, ll y, ll MOD) {
    x %= MOD;
    y %= MOD;
    ll ans = (x+y)%MOD;
    return ans;
}

inline ll mul(ll x,ll y, ll MOD) {
    x %= MOD;
    y %= MOD;
    ll ans = ((x*y)%MOD+MOD)%MOD;
    return ans;
}

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

    if(N==1) {
        ll ans = power(K,M,MOD);
        cout<<ans<<endl;
        return 0;
    }

    if(M==1) {
        ll ans = power(K,N,MOD);
        cout<<ans<<endl;
        return 0;
    }

    ll ans = 0;
    for(ll i=1;i<=K;i++) {
        ll fst = add(power(i,N,MOD),-power(i-1,N,MOD)+MOD,MOD);
        ll snd = power(K-i+1,M,MOD);
        ans = add(ans,mul(fst,snd,MOD),MOD);
    }

    cout<<ans<<endl;

    time_taken();
}

//  main.cpp
//  ervb
//
//  Created by Kanak Gautam on 21/04/20.
//  Copyright © 2020 Kanak Gautam. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <utility>
#define pb push_back
#define mk make_pair
#define endl "\n"
#define mod 998244353
#define mod1 1610612741
#define mul 31
#define PI 3.14159265358979323846264
using namespace std;
typedef long long int lli;
typedef double ld;
typedef pair<lli,lli> ii;
priority_queue <lli, vector<lli>, greater<lli> > ti;
//priority_queue<pair<lli,pair<lli,lli>>>e;
vector <lli> p[200005],a(2000005),f(200005,1e18),d(200005,0),b(1000005);
//vector<set<lli>>s(200005);
//set<pair<lli,lli>>s;
//vector<vector<lli>>dp(5005,vector<lli>(5005,-1));
//lli b[1000005],l[1000005];
//vector<vector<lli>> d(300005,vector<lli>(18,0));
//vector<pair<lli,ii>>p[300005];
map<pair<lli,lli>,lli>mp;
//vector<pair<pair<lli, lli>,lli> > st;
//map<lli,lli> np;
//queue<lli> qy;
lli gcd(lli a, lli b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);

}
lli bpow(lli a, lli b) {
    lli res = 1;
    while (b > 0) {
        if (b & 1)
            res = (res * a)%mod;
        a = (a * a)%mod;
        b >>= 1;
    }
    return res%mod;
}
void fact(lli i)
{
    f[0]=1;
    for(lli k=1;k<=i;k++)
    {
        (f[k]=f[k-1]*k)%=mod;
    }
}
lli isprime(lli n)
{
    if(n==1)
        return 0;
    for(lli i=2;i<=sqrt(n);i++)
        if(n%i==0)
            return 0;
    return 1;
}
lli find(lli x)
{
    if(f[x]==x)
        return x;
    else
        return f[x]=find(f[x]);
}
bool cmp(lli x,lli y)
{
    return x<y;
}
/*lli comb(lli i,lli j)
{
    if(j>i)return 0;
    lli k=f[i];
    lli g=(f[j]*(f[i-j]))%mod;
    lli h=bpow(g,mod-2);
    return (k*h)%mod;
}*/
/*void sieve()
{
    for(lli i=2;i<=sqrt(10000000);i++)
    {
        if(b[i]==0)
        {
            k.pb(i);
        for(lli j=2;i*j<=sqrt(10000000);j++)
        {
            b[i*j]=1;
        }
        }
    }
}*/
int main ()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    lli n,m,k;
    cin>>n>>m>>k;
    lli ans=0;
    if(n==1 or m==1)
    {
        ans=bpow(k,max(n,m));
        cout<<ans<<endl;
        exit(0);
    }
    for(lli i=1;i<=k;i++)
    {
        lli h=(bpow(i,n)-bpow(i-1,n)+mod)%mod;
        lli g=bpow(k-i+1,m);
        ans+=(h*g)%mod;
        ans%=mod;
        //cout<<" "<<h<<" "<<g<<endl;
    }
    cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
#define A(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define UNI(x) sort(A(x)); (x).erase(unique(A(x)),(x).end())
#define int int64_t
void _read();

const int INF = 3e17 + 9847;
main () { _read();

      int n,m;
      cin >> n >> m;
      int src,des;
      cin >> src >> des;
      vector<vector<tuple<int,int,int>>> adj(n + 1);
      while(m--) {
            int a,b,T,K;
            cin >> a >> b >> T >> K;
            adj[a].emplace_back(b,T,K);
            adj[b].emplace_back(a,T,K);
      }
      // The parameter really is just the extra time.
      //? Say you are now at time t, to get to the next edge to must add and extra time oh Yeah that fixes stuff.
      //! Simple DIJKSTRA. --_(^_^)_--
      vector<int> dis(n + 1,INF);
      // Because distance is time!!
      dis[src] = 0;
      set<pair<int,int>> sp;
      sp.emplace(0,src);
      while(!sp.empty()) {
            auto [d,u] = * sp.begin();
            sp.erase(sp.begin());
            for(auto &[v,t,k] : adj[u]) {
                  int d_now = ((d + k - 1) / k) * k + t;
                  if(dis[v] > d_now) {
                        if(dis[v] != INF) {
                              sp.erase(sp.find({dis[v],v}));
                        }
                        dis[v] = d_now;
                        sp.emplace(d_now,v);
                  }
            }
      }
      cout << (dis[des] == INF ? -1 : dis[des]) << '\n';
      return 0;
};

void _read() {
      ios_base :: sync_with_stdio(false);
      cin.tie(NULL);
      #ifdef LOCAL
      freopen("input.txt","r",stdin);
      #endif
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (ll i=(ll)N-1; i>=0; i--)

const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const ll INF = pow(2,30)-1;

vector<ll> fac;
void c_fac(ll x=pow(10,7)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }
ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { ll d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }
ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }
ll modpow(ll x, ll p) { ll result = 1, now = 1, pm = x; while (now<=p) { if (p&now) { result = result * pm % MOD; } now*=2; pm = pm*pm % MOD; } return result; }
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }

struct query {
  ll T, X, Y;
};

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

struct SegTree {
  ll size;
  vector<ll> pos;
  SegTree(ll N) { size = 1; while(size<N) size<<=1; pos.resize(2*size,0); }
  ll operator[](const ll &x) const { return pos[x+size]; }

  void set(ll x, const ll v) { pos[x+size] = v; }
  void update(ll x, const ll v) { set(x,v); x+=size; while (x>>=1) operate(x); }
  ll query(ll a, ll b) {
    ll L = 0, R = 0;
    for (a+=size, b+=size; a<b; a>>=1, b>>=1) {
      if (a&1) { L = q(L,pos[a]); a++; }
      if (b&1) { b--; R = q(pos[b],R); }
    }
    return q(L,R);
  }
  void operate(ll i) { pos[i] = q(pos[i*2], pos[i*2+1]); }
  ll q(ll x, ll y) { return x^y; }
};

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

int main() {
  ll N, Q; cin >> N >> Q;
  ll A[N]; rep(i,N) cin >> A[i];
  query q[Q]; rep(i,Q) cin >> q[i].T >> q[i].X >> q[i].Y;

  SegTree st(N); rep(i,N) st.update(i,A[i]);
  rep(i,Q) {
    if (q[i].T==1) {
      q[i].X--;
      st.update(q[i].X,st[q[i].X]^q[i].Y);
    }
    else {
      q[i].X--;
      ll result = st.query(q[i].X,q[i].Y);
      cout << result << endl;
    }
  }
  return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
int h[33][33];
int v[33][33];
int main() {
	for (int i = 0; i < 30; i++) {
		for (int j = 0; j < 30; j++) {
			h[i][j] = 5000;
		}
	}
	for (int i = 0; i < 1000; i++) {
		int sx, sy, gx, gy, score;
		cin >> sx >> sy >> gx >> gy;
		pair<int,int> prev[33][33];
		int dist[33][33];
		for (int i = 0; i < 30; i++) {
			for (int j = 0; j < 30; j++) {
				dist[i][j] = 1000000007;
			}
		}
		bool bo[33][33] = { true };
		priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> que;
		que.push(make_tuple(0, sx, sy));
		bo[sx][sy] = true;
		dist[sx][sy] = 0;
		while (!que.empty()) {
			tuple<int, int, int> t = que.top();
			int x = get<0>(t);
			int y = get<1>(t);
			int z = get<2>(t);
			que.pop();
			if (y == gx && z == gy)break;
			if (dist[y][z] < x)continue;
			if (y != 29) {
				if (dist[y + 1][z] > dist[y][z] + h[y][z]) {
					dist[y + 1][z] = dist[y][z] + h[y][z];
					prev[y + 1][z] = make_pair(y, z);
					que.push(make_tuple(dist[y][z] + h[y][z], y + 1, z));
				}
			}
			if (y != 0) {
				if (dist[y - 1][z] > dist[y][z] + h[y - 1][z]) {
					dist[y - 1][z] = dist[y][z] + h[y - 1][z];
					prev[y - 1][z] = make_pair(y, z);
					que.push(make_tuple(dist[y][z] + h[y - 1][z], y - 1, z));
				}
			}
			if (z != 29) {
				if (dist[y][z + 1] > dist[y][z] + v[y][z]) {
					dist[y][z + 1] = dist[y][z] + v[y][z];
					prev[y][z + 1] = make_pair(y, z);
					que.push(make_tuple(dist[y][z] + v[y][z], y, z + 1));
				}
			}
			if (z != 0) {
				if (dist[y][z - 1] > dist[y][z] + v[y][z - 1]) {
					dist[y][z - 1] = dist[y][z] + v[y][z - 1];
					prev[y][z - 1] = make_pair(y, z);
					que.push(make_tuple(dist[y][z] + v[y][z - 1], y, z - 1));
				}
			}
		}
		int nx = gx, ny = gy, co = 0;
		vector<tuple<int, int, int>> edge;
		string ans = "";
		while (nx != sx || ny != sy) {
			if (nx - prev[nx][ny].first == 1) {
				ans += 'D';
				edge.emplace_back(make_tuple(0, prev[nx][ny].first, prev[nx][ny].second));
			}
			if (nx - prev[nx][ny].first == -1) {
				ans += 'U';
				edge.emplace_back(make_tuple(0, nx, ny));
			}
			if (ny - prev[nx][ny].second == 1) {
				ans += 'R';
				edge.emplace_back(make_tuple(1, prev[nx][ny].first, prev[nx][ny].second));
			}
			if (ny - prev[nx][ny].second == -1) {
				ans += 'L';
				edge.emplace_back(make_tuple(1, nx, ny));
			}
			int nx1 = nx;
			nx = prev[nx][ny].first;
			ny = prev[nx1][ny].second;
			co++;
		}
		reverse(ans.begin(), ans.end());
		cout << ans << endl;
		cin >> score;
		if (score < 5000 * co) {
			for (int j = 0; j < co; j++) {
				tuple<int, int, int> t = edge[j];
				int x = get<0>(t);
				int y = get<1>(t);
				int z = get<2>(t);
				if (x) {
					h[y][z] = 4999;
				}
				else {
					v[y][z] = 4999;
				}
			}
		}
		else if (score > 5000 * co) {
			for (int j = 0; j < co; j++) {
				tuple<int, int, int> t = edge[j];
				int x = get<0>(t);
				int y = get<1>(t);
				int z = get<2>(t);
				if (x) {
					h[y][z] = 5001;
				}
				else {
					v[y][z] = 5001;
				}
			}
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);}
template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1};
typedef pair<ll,ll> P;

ll d[2222][2222];

void Main() {
  ll n,m;
  cin >> n >> m;
  vector<ll> v[n];
  rep(i,m) {
    ll x,y;
    cin >> x >> y;
    x--,y--;
    v[x].pb(y);
  }
  rep(k,n) {
    fill(d[k],d[k]+n,MAX);
    d[k][k]=0;
    queue<ll> que;
    que.push(k);
    while(!que.empty()) {
      ll x=que.front();que.pop();
      rep(i,v[x].size()) {
        ll y=v[x][i];
        if(d[k][y]!=MAX) continue;
        d[k][y]=d[k][x]+1;
        que.push(y);
      }
    }
  }
  ll ans=0;
  rep(i,n) {
    rep(j,n) {
      if(d[i][j]<MAX) ans++;
    }
  }
  pr(ans);
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}

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

#define faster      ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pi          acos(-1.0)
#define nl          '\n'

/**-------Library-------*/
#define mem(a,b)       memset(a,b,sizeof(a))
#define all(a)         a.begin(),a.end()
#define Sort(x)        sort(x.begin(),x.end())
#define SortR(x)       sort(x.begin(),x.end(),greater<int>())
#define Reverse(x)     reverse(x.begin(),x.end())

#define Sum(A,n)       accumulate(A,A+n,0)
#define SortA(ar,s)    sort(ar,ar+s)
#define SortD(ar,s)    sort(ar,ar+s,greater<int>())
#define maxa(A,n)      *max_element(A,A+n)
#define mina(A,n)      *min_element(A,A+n)
#define gcd(a,b)       __gcd(a,b)
#define lcm(a,b)       (a*(b/gcd(a,b)))
#define sq(x)          (x)*(x)
#define mid(l,r)       (l+(r-l)/2)

#define trzero(x)       __builtin_ctz(x)
#define parity(x)       __builtin_parity(x)
#define countone(x)     __builtin_popcount(x)
#define left(p)         (p<<1)
#define right(p)        (p>>1)

#define min3(a,b,c)       min(a,min(b,c))
#define min4(a,b,c,d)     min(a,min(b,min(c,d)))
#define max3(a,b,c)       max(a,max(b,c))
#define max4(a,b,c,d)     max(a,max(b,max(c,d)))
#define pb(x)             push_back(x)
#define mod               1000000007
#define precision(x)      cout<<fixed<<setprecision(x)
#define nel               cout<<nl
#define yes               cout<<"YES"<<nl
#define no                cout<<"NO"<<nl

/**----------Graph Moves----------*/
const int fx[] = {+1,-1,+0,+0};                         // 4 X directions
const int fy[] = {+0,+0,+1,-1};                         // 4 Y directions
const int fx8[] = {+0,+0,+1,-1,-1,+1,-1,+1};            // King X Moves
const int fy8[] = {-1,+1,+0,+0,+1,+1,-1,-1};            // King Y Moves
const int kx[] = {-2, -2, -1, -1,  1,  1,  2,  2};      // Knight X Moves
const int ky[] = {-1,  1, -2,  2, -2,  2, -1,  1};      // Knight Y Moves

/**----data type----*/
typedef long long int ll;
typedef unsigned long long int  llu;

int main()
{
    int a,b;
    cin>>a>>b;
    if(a==b)
        cout<<a<<nl;
    else
    {
        cout<<3-(a+b)<<nl;
    }
    return 0;
}


#pragma region HEAD
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define EMIT(s) std::cout << (s) << std::endl
#define ANSWER(b) EMIT((b) ? "Yes" : "No")
#define SCREAM(b) EMIT((b) ? "YES" : "NO")
#define WHISPR(b) EMIT((b) ? "yes" : "no")
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
using namespace std;
using ll = long long;

int solve();

int main()
{
#ifdef DEBUG_MODE
  ifstream in("input.dat");
  cin.rdbuf(in.rdbuf());
#else
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
#endif
  solve();
  return 0;
}
#pragma endregion

int solve()
{
  int n;
  cin>>n;
  vector<ll> an(n);
  REP(i,n){
    cin>>an[i];
  }
  set<ll> s;
  REP(i,n){
    s.insert(an[i]);
  }
  while(s.size()>1){
    auto maxi = *s.rbegin();
    auto mini = *s.begin();
    s.erase(maxi);
    s.insert(maxi-mini);
  }
  cout<<*s.begin()<<endl;
  return 0;
}

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

int32_t main()
{
   int n;
   cin>>n;
   int a[n];
   multiset<int>s;
   for(int i=0;i<n;i++){cin>>a[i];s.insert(a[i]);}
   int ans=a[0];
   for(int i=1;i<n;i++)ans=__gcd(ans,a[i]);
   cout<<ans<<endl;

}


#include <iostream>

using namespace std;

int main(){
  long int N; cin >> N;
  long int sum = 0;
  int day = 0;
  do{
    sum += day++;
  }while(sum < N);

  cout << day-1 << endl;

  return 0;
}

#include <cstdio>
#include <cctype>
#include <algorithm>
using namespace std;
#define FOR(i,j,k) for(int i=j; i<=k; ++i)
inline int read (void) {
	int x = 0, f = 1, ch = getchar();
	while(!isdigit(ch)) { if(ch == '-') f = -f; ch = getchar(); }
	while(isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
int n, ans, a[25];
void dfs (int x, int Xor, int Or) {
	if(x > n) {
		ans = min(ans, Xor ^ Or);
		return ;
	}
	dfs (x+1, Xor, Or | a[x]);
	dfs (x+1, Xor ^ Or, a[x]);
}
int main (void) {
	n = read(); ans = (1 << 31) - 1;
	FOR(i,1,n) a[i] = read();
	dfs (1, 0, 0);
	printf("%d\n", ans);
	return 0;
}


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

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

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

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

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

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

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

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

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

ll conbinationMemo[201][12];

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

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

ll nPr(ll n, ll r){
    r = n - r;
    ll ret = 1;
    for (ll i = n; i >= r + 1; i--) ret *= i;
    return ret;
}
//-----------------------ここから-----------
int main(void){
    ll n, m;
    cin >> n >> m;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];
    vector<ll> mp(n, 0);

    rep(i, m){
        mp[a[i]]++;
    }
    ll ans = n;
    rep(i, n){
        if(mp[i] == 0) ans = min(ans, (ll)i);
    }

    for(ll i = m; i < n; i++){
        mp[a[i]]++;
        mp[a[i - m]]--;
        if(mp[a[i - m]] == 0) ans = min(ans, a[i - m]);
    }
    cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define clr(a) memset(a, 0, sizeof(a))
#define full(a) memset(a, 0x3f, sizeof(a))
#define fornext(x, i) for(int i = head[x], y = ver[i]; i; i = nxt[i], y = ver[i])
#define mset(a, b) memset(a, b, sizeof(a))
#define Rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define dRep(i, a, b) for(int i = (a); i >= (b); --i)
#define IOset(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout);
#define endl '\n'
#define int ll
#define un unsigned
#define ll long long
#define db double
#define rept cerr<<1.0*clock()/CLOCKS_PER_SEC<<endl;
#define dbg cout<<"c "
template<typename _T> inline void prt(_T _d) { cerr<<_d<<' '; }
template <typename _T,typename ...Args> inline void prt(_T _d,Args ..._a) { prt(_d), prt(_a...), putchar('\n'); }
template <typename _T,typename ...Args> inline void cbg(_T _d,Args ..._a) { cerr<<"--at line "<<__LINE__<<"--: "; prt(_d,_a...); }
template<typename _T> inline void rd(_T &_d) { int _f; char _c;_f=_d=0;while(_c=getchar(),!isdigit(_c)) if (_c=='-') _f=1;
while(isdigit(_c)) _d=_d*10+_c-'0',_c=getchar();
_d=(_f?-_d:_d);}
template <typename _T,typename ...Args> inline void rd(_T &_d,Args &..._a) { rd(_d);rd(_a...); }

int n,k;

int c(int x) {
	return min(x-1,n)-max(1LL,x-n)+1;
}

signed main() {
	rd(n,k);
	k=abs(k);
	int ans=0;
	Rep(i,k+2,n*2) ans+=c(i)*c(i-k);
	cout<<ans<<endl;
	return 0;
}


#include <cstdio>
using namespace std;

int main()
{
	long long a, mul = 10, res = 1, cur = 1, mod;
	int b;

	scanf("%lld%d", &a, &b);
	mod = 1ll * b * b;

	while (cur <= a)
	{
		if (cur & a)
			res = (res * mul) % mod;

		mul = (mul * mul) % mod;
		cur <<= 1;
	}

	printf("%lld\n", res / b);

	return 0;
}

#include <bits/stdc++.h>

using namespace std;

const int M = 10005;
typedef __int128 ll;
typedef __int128 lll;

ll n;
int m;

ll inv[M];
map<ll, int> book;

inline void init() {
	inv[1] = 1;
	for (int i = 2; i <= m; i++) {
		inv[i] = (m - m/i) * inv[m%i] % m;
	}
}

inline ll qpow(ll a, ll b, ll p) {
	ll res = 1;
	while (b) {
		if (b & 1) res = res * a % p;
		a = a * a % p;
		b >>= 1;
	}
	return res;
}

int main()
{
	long long tn, tm;
	scanf("%lld%lld", &tn, &tm);
	n = tn, m = tm;

	init();

	ll x = 1;
	ll a = 0, b = 0;
	ll c = 0, d = 0, len = -1;

	while (c < n) {
		x *= 10;
		c++;
		a = (a * 10 % m + x / m % m) % m;
		x %= m;
		if (book[x]) {
			len = c - book[x];
			break;
		}
		book[x] = c;
	}

	if (c >= n) {
		printf("%lld", (long long)a);
		return 0;
	}

	x = 1;
	for (int i = 1; i <= c-len; i++) {
		x *= 10;
		x %= m;
	}

	while (d < len) {
		x *= 10;
		d++;
		b = (b * 10 % m + x / m % m) % m;
		x %= m;
	}

//	printf("c = %lld, len = %lld, x = %lld\n", c, len, x);
//	printf("a = %lld, b = %lld\n", a, b);

	ll k = (n - c) / len;
	ll t = qpow(10, len, m);

//	printf("k = %lld, t = %lld\n", k, t);

	ll ans = 0;
//	if (t == 1) ans = k;
//	else ans = (lll)((1ll - qpow(t, k, m)) % m + m) % m * (lll)inv[((1ll-t)%m+m)%m] % m;
//	ans = (lll)ans * (lll)b % m;
	ans = (lll)k * (lll)b % m;

//	printf("ans = %lld\n", ans);

	ans = (ans + a * qpow(t, k+1, m) % m) % m;

	ll r = (n-c) % len;

	d = 0;
	while (d < r) {
		x *= 10;
		d++;
		ans = (ans * 10 % m + x / m % m) % m;
		x %= m;
	}

	printf("%lld", (long long)ans);

	return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

#define rep(i,n) for (long long i = 0; i < (n); ++i)
#define DIV 1000000007 //10^9+7
#define INF LONG_MAX/3
#define bit(n) (1LL<<(n))

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; }
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

string S[105];
vector<ll> ot[105];

set<ll> from[105];

void dfs(ll cur, ll start) {
	if(from[start].count(cur)) return;
	from[start].insert(cur);
	for(ll next: ot[cur]) dfs(next, start);
}

int main(){
	ll N;
	cin >> N;
	rep(i, N) cin >> S[i];

	rep(i, N) rep(j, N) {
		if(S[i][j] == '1') {
			ot[j].push_back(i);
		}
	}
	rep(i, N) dfs(i, i);

	double ans = 0;
	rep(i, N) {
		double num = from[i].size();
		ans += 1.0/num;
	}
	printf("%.10f\n", ans);

}

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define fir first
#define sec second
#define mod 998244353
#define ll long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read()
{
	char ch=getchar(); int nega=1; while(!isdigit(ch)) {if(ch=='-') nega=-1; ch=getchar();}
	int ans=0; while(isdigit(ch)) {ans=ans*10+ch-48;ch=getchar();}
	if(nega==-1) return -ans;
	return ans;
}
inline char rdchar()
{
	char ch=getchar();
	while(!(ch>='a'&&ch<='z')&&!(ch>='A'&&ch<='Z')&&!(ch>='0'&&ch<='9')) ch=getchar();
	return ch;
}
typedef pair<int,int> pii;
void print(vector<int> x){for(int i=0;i<(int)x.size();i++) printf("%d%c",x[i]," \n"[i==(int)x.size()-1]);}
#define N 105
int a[N][N];
signed main()
{
	int n=read();
	for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) a[i][j]=rdchar()-'0';
	for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(a[i][k]&&a[k][j]) a[i][j]=1;
	for(int i=1;i<=n;i++) a[i][i]=1;
	double ans=0;
	for(int i=1;i<=n;i++)
	{
		int s=0; for(int j=1;j<=n;j++) s+=a[j][i];
		ans+=(double)n/s;
	}
	printf("%.9lf\n",ans/n);
	return 0;
}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define INF 1000000000
#define INFLL 1000000000000000000
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define repp(i, n, s) for (int i = (int)(n); i >= (int)(s); i--)
#define mp make_pair
#define tp make_tuple
typedef pair<int,int> P;
typedef pair<ll,ll> PL;
typedef tuple<int,int,int> T;
typedef tuple<ll,ll,ll> TL;
ll mod = 1000000007;
ll mod2 = 998244353;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll k;cin>>k;
    ll ans = 0;
    rep(i,1,k+1){
        ll num = 0;
        for (ll j=1;j*j<=i;j++){
            if (i%j==0){
                num++;
                if (i!=j*j) num++;
            }
        }

        ans += (k/i)*num;
    }

    cout<<ans<<endl;
}

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

using namespace std;

using ll = long long;
using pp = pair<int,int>;
#define rep(i,n) for(int i = 0;i<n;i++)
const int m = 1000000001;

int main(){

    ll x, y, a, b;cin >> x >> y >> a >> b;
    ll e=0;
    if (x < b){
        while((a-1)*x < b && x*a < y){
        x *= a;
        e++;
    }
    }

    e += (y-x-1)/b;
    cout << e << endl;

}


#include <bits/stdc++.h>
#include <time.h>
#define rep(i, n)	for(int i=0;i<(int)(n);i++)
#define ALL(a)	(a).begin(),(a).end()
using namespace std;
using ll=long long;
typedef pair<int,int> P;
#define TIME_LIMIT  1.5

int sy,sx;
int t[50][50],p[50][50];
int dx[4]={0, 0, -1, 1}, dy[4]={-1, 1, 0, 0};
map<char,int> mp;

inline void InitRand()
{
    srand((unsigned int)time(NULL));
}

struct STATE{
    bool visited[50][50];
    vector<char> route;
    int y,x;
};

void check_sametile(STATE& state){
    rep(i,4){
        int ny=state.y+dy[i], nx=state.x+dx[i];
        if(0<=ny&&ny<50&&0<=nx&&nx<50&&t[ny][nx]==t[state.y][state.x]){
            state.visited[ny][nx]=true;
            break;
        }
    }
}

void init(STATE& state){
    state.y=sy; state.x=sx;
    rep(i,50) fill(state.visited[i],state.visited[i]+50,false);
    state.visited[sy][sx]=true;
    check_sametile(state);
}

void modify(STATE& state){
    int R=rand()%4;
    if(R==0 && state.y-1>=0 && !state.visited[state.y-1][state.x]){
        state.y--;
        state.route.push_back('U');
    }
    else if(R==1 && state.y+1<=49 && !state.visited[state.y+1][state.x]){
        state.y++;
        state.route.push_back('D');
    }
    else if(R==2 && state.x-1>=0 && !state.visited[state.y][state.x-1]){
        state.x--;
        state.route.push_back('L');
    }
    else if(R==3 && state.x+1<=49 && !state.visited[state.y][state.x+1]){
        state.x++;
        state.route.push_back('R');
    }
    if(!state.visited[state.y][state.x]){
        state.visited[state.y][state.x]=true;
        check_sametile(state);
    }
}

int calc_score(STATE& state){
    int res=p[sy][sx];
    int y=sy, x=sx;
    for(auto c:state.route){
        y+=dy[mp[c]]; x+=dx[mp[c]];
        res+=p[y][x];
    }
    return res;
}

STATE pre_state[5];
STATE reset_state;

pair<int,vector<char>> mountain(){
    STATE state;
    init(state);
    //double start_time = clock();
    int cnt=0,re=0,mov=0;
    while(cnt<=2500){
        // double now_time = clock();
        // if(now_time - start_time > TIME_LIMIT) break;

        STATE new_state = state;
        modify(new_state);
        if(state.x==new_state.x&&state.y==new_state.y) re++;
        else{
            for(int i=min(mov,5)-1;i>0;i--){
                pre_state[i]=pre_state[i-1];
            }
            pre_state[0]=state;
            re=0;
            state=new_state;
            mov++;
        }
        if(re>=20||reset_state.x==state.x&&reset_state.y==state.y){
            reset_state=state;
            if(mov<=5) break;
            else{
                state=pre_state[4];
            }
            re=0;
        }
        cnt++;
    }
    return {calc_score(state),state.route};
}

int main(){
    cin>>sy>>sx;
    rep(i,50) rep(j,50) cin>>t[i][j];
    rep(i,50) rep(j,50) cin>>p[i][j];
    mp['U']=0;
    mp['D']=1;
    mp['L']=2;
    mp['R']=3;
    vector<pair<int,vector<char>>> res;
    InitRand();
    rep(i,250){
        reset_state.y=-1; reset_state.x=-1;
        res.push_back(mountain());
    }
    sort(ALL(res),greater<pair<int,vector<char>>>());
    for(auto x:res[0].second) cout<<x;
    cout<<endl;
    //rep(i,10) cout<<res[i].first<<endl;
}

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define p(x,y) pair<x,y>
#define oset(x) tree<x, null_type, less<x>, rb_tree_tag, tree_order_statistics_node_update>
#define all(x) (x).begin(),(x).end()
#define ll long long
#define scan(a) for(auto &it:a)cin>>it;
#define print(a) {for(auto it:a)cout<<it<<" ";cout<<endl;}
#define out(x) cout<<((x)?"Yes":"No")<<endl;
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define int ll

void solve()
{
    int n,q;
    cin>>n>>q;
    vector<int> a(n);
    scan(a);
    sort(all(a));
    n=unique(all(a))-a.begin();
    a.resize(n);
    int b[n],c[n];
    // print(a);
    rep(i,0,n)
    b[i]=a[i]-(i>0)*(a[i-1])-1;
    partial_sum(b,b+n,c);
    // print(c);
    while(q--)
    {
        int x;
        cin>>x;
        int in=lower_bound(c,c+n,x)-c;
        if(in==n||a[in]!=x)
            in--;
        if(in>=0LL)
            x-=c[in];
        int no=0LL;
        if(in>=0LL)
            no=a[in];
        no+=x;
        cout<<no<<endl;
    }
}
int32_t main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    int t=1;
    //init();
    //cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int re()
{
    char f=getchar();
    int x=0,k=1;
    while(f>'9'||f<'0')
    {
        if(f=='-') k=-1;
        f=getchar();
    }
    while(f>='0'&&f<='9')
    {
        x=x*10+f-'0';
        f=getchar();
    }
    return x*k;
}

signed main()
{
    int n=re(),ans=0;
    n*=2;
    for(int i=1;i<=sqrt(n);i++)
        if(n%i==0)
        {
            if(i&1)
            ans++;
            if((n/i)&1 && i*i<n)
                ans++;
        }

    cout<<ans*2;
}

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <cassert>
#define rep(i,n) for(int i = 0; i < n; ++i )
using namespace std;
using ll = long long;

vector<int> g[100005];
int d[100005];
int dfs(int u){
  for(int v:g[u]) d[u] += dfs(v);
  return ++d[u];
}
int f(int u){
  int res=1,i=1;
  vector<int> t[2];
  for(int v:g[u]) t[d[v]&1].push_back(f(v));
  sort(t[1].begin(),t[1].end());
  for(int s:t[0]) res += min(s,0);
  for(int s:t[1]) res += i*s, i*=-1;
  for(int s:t[0]) res += i*max(s,0);
  return res;
}
int main() {
  int n;
  cin >> n;
  rep(i,n-1){
    int p;
    cin >> p;
    --p;
    g[p].push_back(i+1);
  }
  dfs(0);
  cout << (n+f(0))/2 << endl;
}


/**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⢀⡀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡖⠁⠀⠀⠀⠀⠀⠀⠈⢲⣄
⠀⠀⠀⠀⠀⠀⠀⠀⣼⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣧
⠀⠀⠀⠀⠀⠀⠀⣸⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣇
⠀⠀⠀⠀⠀⠀⠀⣿⣿⡇⠀⢀⣀⣤⣤⣤⣤⣀⡀⠀⢸⣿⣿
⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣔⢿⡿⠟⠛⠛⠻⢿⡿⣢⣿⣿⡟
⠀⠀⠀⠀⣀⣤⣶⣾⣿⣿⣿⣷⣤⣀⡀⢀⣀⣤⣾⣿⣿⣿⣷⣶⣤⡀
⠀⠀⢠⣾⣿⡿⠿⠿⠿⣿⣿⣿⣿⡿⠏⠻⢿⣿⣿⣿⣿⠿⠿⠿⢿⣿⣷⡀
⠀⢠⡿⠋⠁⠀⠀⢸⣿⡇⠉⠻⣿⠇⠀⠀⠸⣿⡿⠋⢰⣿⡇⠀⠀⠈⠙⢿⡄
⠀⡿⠁⠀⠀⠀⠀⠘⣿⣷⡀⠀⠰⣿⣶⣶⣿⡎⠀⢀⣾⣿⠇⠀⠀⠀⠀⠈⢿
⠀⡇⠀⠀⠀⠀⠀⠀⠹⣿⣷⣄⠀⣿⣿⣿⣿⠀⣠⣾⣿⠏⠀⠀⠀⠀⠀⠀⢸
⠀⠁⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⢇⣿⣿⣿⣿⡸⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠈
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣧⡀
⠀⠀⠀⠐⢤⣀⣀⢀⣀⣠⣴⣿⣿⠿⠋⠙⠿⣿⣿⣦⣄⣀⠀⠀⣀⡠⠂
⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠉⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠋⠁
**/
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define nl cout<<"\n";
#define ll long long int
#define ld double
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define F(i,a,b) for(i=a;i<b;i++)
//#include <ext/pb_ds/assoc_container.hpp> // policy based data structure header files
//#include <ext/pb_ds/tree_policy.hpp> // policy based data structure header files
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
//using namespace __gnu_pbds; // for pbds
//#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> // have functions like order_of_key, find_by_order
const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781641;
const ll M=1e18;
const ll MAXN=100200;
ll i,j,mask;
void solve()
{
    ll n, m;
    cin>>n>>m;
    vector<vector<pair<ll, pair<ll,ll> > > > v(n+1);
    while(m--) {
        ll a, b, c, d;
        cin>>a>>b>>c>>d;
        v[a].pb({b, {c, d} });
        v[b].pb({a, {c, d} });
    }
    priority_queue<pair<ll,ll>, vector<pair<ll,ll> >, greater<pair<ll,ll> > > pq;
    pq.push({0, 1});
    vector<ll> dist(n+1, M), vis(n+1);
    dist[1]=0;
    while(!pq.empty()) {
        ll t=pq.top().fi;
        ll node=pq.top().se;
        pq.pop();
        if(vis[node])
            continue;
        vis[node]=1;
        for(auto x:v[node]) {
            ll to=x.fi, c=x.se.fi, d=x.se.se;
            ll k;
            ll g=sqrt(d);
            ll change=M;
            F(k,max((ll)0, g-2), g+3) {
                ll val=max(k, t);
                change=min(change, val+c+d/(val+1));
            }
            if(change<dist[to]) {
                dist[to]=change;
                pq.push({change, to});
            }
        }
    }
    if(dist[n]==M) {
        cout<<-1;
    } else {
        cout<<dist[n];
    }
}
int main()
{
    IOS
    /*#ifndef ONLINE_JUDGE
       freopen("input.txt","r",stdin);
       freopen("output.txt","w",stdout);
    #endif // ONLINE_JUDGE*/
    ll t,test=1;
    // cin>>test;
    F(t,1,test+1) {
        //cout<<"Case #"<<t<<": ";
        solve();
        nl
    }
    return 0;
}

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

#define rep(i, n) for(int i = 0; i < n; i++)
#define per(i, n) for(int i = n - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define mod 1000000007
using namespace std;
template<class T, class U>
T &chmax(T &a, const U &b){ if(a < b) a = b; return a; }
template<class T, class U>
T &chmin(T &a, const U &b){ if(a > b) a = b; return a; }
ll search(int d){
  double left = 0, right = 2e14;
  rep(_, 100){
    double l = (left*2 + right) / 3;
    double r = (left + right*2) / 3;
    double f1 = d/(l + 1) + l;
    double f2 = d/(r + 1) + r;
    if(f1 > f2) left = l;
    else right = r;
  }
  return ceil(left);
}
int main(){
  int n,m;
  cin >> n >> m;
  vector<vector<tuple<int,ll,ll>>> root(n);
  rep(i, m){
    int a,b,c,d;
    cin >> a >> b >> c >> d;
    a--; b--;
    root[a].push_back({b, c, d});
    root[b].push_back({a, c, d});
  }
  priority_queue<pll, vector<pll>, greater<pll>> que;
  que.push({0, 0});
  vl checked(n, -1);
  checked[0] = 0;
  while(!que.empty()){
    ll pos, score;
    tie(score, pos) = que.top();
    que.pop();
    if(checked[pos] != -1 && checked[pos] < score) continue;
    for(auto x : root[pos]){
      ll s = search(get<2>(x));
      chmax(s, score);
      ll v = s + get<1>(x) + get<2>(x) / (s + 1);
      //cout << s << " " << v << "\n";
      if(checked[get<0>(x)] != -1 && checked[get<0>(x)] <= v) continue;
      checked[get<0>(x)] = v;
      que.push({v, get<0>(x)});
    }
  }
  //rep(i, n) cout << checked[i] << " ";
  cout << checked[n - 1] << "\n";
}


#include<bits/stdc++.h>
using namespace std;
/*#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
*/typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;

#define int ll
#define LOCAL 0
#define dbg(x) cout << #x << " is " << x << "\n"
#define gll(x) scanf("%d",&x)
#define gll2(x,y) scanf("%d%d",&x,&y)
#define gll3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define gllarr(arr,n) f(i,n) gll(arr[i]);
#define sz(x) ((int)x.size())
#define s(x) sort(x.begin(),x.end())
#define all(v) v.begin(),v.end()
#define rs(v) { s(v) ; r(v) ; }
#define r(v) {reverse(all(v));}
#define pb push_back
#define f(i,n) for(int i=0;i<n;i++)
#define fr(i,n) for(int i=n-1;i>=0;i--)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repr(i,a,b) for(int i=a;i>=b;i--)

const ll mod = (ll)1e9 + 7;
const ll inf = (ll)1e16;
const ld eps = 1e-12;
const ll N = (int)1e5 + 5;
const ll LOGN = 19;
const ld PI = 3.14159265358979323846;
inline ll mul(ll a, ll b, ll m = mod) { return (ll)(a * b) % m;}
inline ll add(ll a, ll b, ll m = mod) { a += b; if(a >= m) a -= m; if(a < 0) a += m; return a;}
inline ll power(ll a, ll b, ll m = mod) { if(b == 0) return 1; if(b == 1) return (a % m); ll x = power(a, b / 2, m); x = mul(x, x, m); if(b % 2) x = mul(x, a, m); return x;}

void solve() {
	int n;
	cin>>n;
	int a[n];
	f(i, n) cin>>a[i];
	int pre[n];
	int pre_max[n];
	pre[0] = a[0];
	pre_max[0] = max(0ll, pre[0]);
	rep(i, 1, n - 1) {
		pre[i] = pre[i - 1] + a[i];
		pre_max[i] = max(pre_max[i - 1], pre[i]);
	}
	int ans = 0;
	int curr = 0;
	f(i, n) {
		ans = max(ans, curr + pre_max[i]);
		curr += pre[i];
		//cout<<curr<<endl;
		ans = max(ans, curr);
	}
	cout<<ans;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (LOCAL) {
        freopen("C:\\Users\\Dishant\\Desktop\\Collection-DEV c++\\input.txt", "r", stdin);
        freopen("C:\\Users\\Dishant\\Desktop\\Collection-DEV c++\\output.txt", "w", stdout);
    }
    int t = 1;
    //cin>>t;
    for(int test = 1; test <= t; test++) {
        //cout<<"Case #"<<test<<": ";
        solve();
    }
    return 0;
}

/* Priyansh Agarwal*/

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<algorithm>
#include<string.h>
#include<unordered_map>
#include<vector>
#include<unordered_set>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<map>
#include<chrono>

using namespace std;
using namespace __gnu_pbds;
using namespace chrono;

#define debug(x) cout << #x << " " << x <<endl;
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define MOD 1000000007
#define MOD1 998244353
#define INF 1e18
#define nline "\n"
#define pb push_back
#define ppb pop_back;
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcount
#define sz(x) (int)(x.size())

typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key

/*---------------------------------------------------------------------------------------------------------------------------*/
ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}
void extendgcd(ll a, ll b, ll*v) {if (b == 0) {v[0] = 1; v[1] = 0; v[2] = a; return ;} extendgcd(b, a % b, v); ll x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return;} //pass an arry of size1 3
ll mminv(ll a, ll b) {ll arr[3]; extendgcd(a, b, arr); return arr[0];} //for non prime b
ll mminvprime(ll a, ll b) {return expo(a, b - 2, b);}
bool revsort(ll a, ll b) {return a > b;}
void swap(int &x, int &y) {int temp = x; x = y; y = temp;}
ll combination(ll n, ll r, ll m, ll *fact, ll *ifact) {ll val1 = fact[n]; ll val2 = ifact[n - r]; ll val3 = ifact[r]; return (((val1 * val2) % m) * val3) % m;}
void google(int t) {cout << "Case #" << t << ": ";}
vector<int> sieve(int n) {int*arr = new int[n + 1](); vector<int> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
ll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;}  //only for prime m
ll phin(ll n) {ll number = n; if (n % 2 == 0) {number /= 2; while (n % 2 == 0)n /= 2;} for (ll i = 3; i <= sqrt(n); i += 2) {if (n % i == 0) {while (n % i == 0)n /= i; number = (number / i * (i - 1));}} if (n > 1)number = (number / n * (n - 1)) ; return number;} //O(sqrt(N))
/*--------------------------------------------------------------------------------------------------------------------------*/

void solve() {

	ll n;
	cin >> n;
	ll arr[n];
	for (int i = 0; i < n; i++)
		cin >> arr[i];
	ll max1 = 0;
	ll sum1 = 0;
	ll ans = 0;
	ll value = 0;
	for (int i = 0; i < n; i++) {
		ans = max(ans, value + max1);
		value += sum1;
		sum1 += arr[i];
		value += arr[i];
		max1 = max(sum1, max1);
		ans = max(ans, value);
	}
	cout << ans << nline;
}

int main() {
	fastio();
#ifndef ONLINE_JUDGE
	freopen("Input.txt", "r", stdin);
	freopen("Output.txt", "w", stdout);
	freopen("Error.txt", "w", stderr);
#endif
	auto start1 = high_resolution_clock::now();
	solve();
	auto stop1 = high_resolution_clock::now();
	auto duration = duration_cast<microseconds>(stop1 - start1);
#ifndef ONLINE_JUDGE
	cerr << "Time: " << duration.count() / 1000 << endl;
#endif
	return 0;
}


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

int main(){
    ll n;
    cin >> n;
    ll ans = 0;
    for(ll i=1; i*i <=2 * n; i++){
        if((2*n) % i == 0){
            ll a = i;
            ll b = (2*n) / i;
            if((a+b-1) % 2 == 0 && (a-b+1) % 2 == 0){
                ans ++;
            }
            a = (2*n) / i;
            b = i;
            if((a+b-1) % 2 == 0 && (a-b+1) % 2 == 0){
                ans ++;
            }

        }
    }

    cout << ans << endl;
}

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

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

  long long count = 0;
  int64_t A = sqrt(N);

  for (int64_t n = 1; n < 2000000 ; n++) {
    if ((2*N - n*(n-1)) > 0 && (2*N - n*(n-1)) % (2*n) == 0){
        count += 2;
    }
  }

cout << count <<endl;

}


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

int main(){
    int n; cin >> n;
    vector<int> A(n);
    for (int i=0; i<n; i++) cin >> A[i];
    sort(A.begin(), A.end());
    int amax = A.back();
    int gcd = 0;
    int ans = 0;
    for (int i=2; i<=amax; i++){
        int cnt = 0;
        for (auto a:A){
            if (a % i == 0) cnt++;
        }
        if (gcd < cnt){
            gcd = cnt; ans = i;
        }
    }
    cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define U 998244353
#define N 1000005
#define int long long
#define sz(c) (int)c.size()
#define fr first
#define ll long long
#define sc second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define rep(i,a,n) for(int i=a ; i<n ; i++)
#define r0 return 0;
#define endl '\n'
#define INF (int)1e15

/* Debug Begins */
# define trace(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); \
  stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
string to_string(char c) { return "'" + string(1, c) + "'";}
string to_string(string s) { return '"' + s + '"';}
string to_string(bool f) { if(f) return "True"; else return "False";}
string to_string(const char* s) { return to_string((string) s);}
template<typename A> string to_string(A);
template<typename A, typename B> string to_string(pair<A, B> p){
  return "(" + to_string(p.first) + ": " + to_string(p.second) + ")";}
template<typename A> string to_string(A v) {bool f = false; string r = "{";
  for (auto x: v) {if (f) r += ", "; r += to_string(x); f = true;} return r += "}";}
template<typename A> string to_string(vector<vector<A>> v) {string r;
  for (auto x: v) r += "\n" + to_string(x); return r;}
int Nerr;
template<typename A> string to_string(A *p) {return to_string(vector<A>(p, p + Nerr));}
void err(istream_iterator<string>) { cerr << endl; }
template<typename T,typename... Args> void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << to_string(a) << "; "; err(++it, args...); }
template<typename T> void kek(T ans) {cout << ans << endl; exit(0);}
#define Lu(...) [&] (auto &&u) { return __VA_ARGS__; }
#define Luv(...) [&] (auto &&u, auto &&v) { return __VA_ARGS__; }
/***************************************************************/

set<int> primeFactors(int n)
{
    set<int> ans;
    while (n % 2 == 0)
    {
        ans.insert(2);
        n = n/2;
    }
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        while (n % i == 0)
        {
            ans.insert(i);
            n = n/i;
        }
    }
    if (n > 2)
        ans.insert(n);
    return ans;
}

signed main()
{
    ios_base::sync_with_stdio(0);
    int TESTS=1;
    // cin>>TESTS;
    while(TESTS--)
    {
        int n;
        cin >> n;
        vector<int> a(n);
        map<int,int> m;
        rep(i,0,n){
            int t;
            cin >> t;
            set<int> f = primeFactors(t);
            for(auto j : f) m[j]++;
        }
        int ans = 0, mx = 0;
        for(auto j : m){
            if(j.sc > mx) {
                mx = j.sc;
                ans = j.fr;
            }
        }
        cout << ans;
    }
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long LL;

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

LL LCM(LL a,LL b)
{
	return a/gcd(a,b)*b;
}

int main()
{
    ios::sync_with_stdio(false);

	int n;

	cin>>n;

	LL lcm=1;
	for(int i=2;i<=n;i++) lcm=LCM(lcm,i);

	cout<<lcm+1<<endl;

    return 0;
}

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

int N;

bool isPrime(int n) {
	for(int i = n-1; i > 1; i--) {
		if(n % i == 0) return false;
	}
	return true;
}

int main() {
	cin >> N;

	long long x = 1;

	for(int i = 2; i <= N; i++) {
		if(isPrime(i)) {
			int p = i;
			while(p <= N) {
				x *= i;
				p *= i;
			}
		}
	}
	cout << (x + 1) << endl;
}


// Artur Kraska, II UWr

#include <bits/stdc++.h>

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

#define M 998244353
#define INF 1000000007LL

using namespace std;

int n, m, a;
long long tr[5005][5005];
long long res;
map <pair<int, long long>, long long> mapa;

long long gen(int nr, int zostalo)
{
    if(CONTAIN(MP(nr, zostalo), mapa))
        return mapa[MP(nr, zostalo)];
    long long res = 0;
//    cout << "na " << nr << " zostalo " << zostalo << ", r: " << r << endl;
    if(zostalo == 0)
    {
        return 1;
    }
    if(zostalo < (2<<nr))
        return 0;
    for(int i=0; (i<<nr)<= zostalo && i <= n; i+=2)
    {
        res = (res + tr[n][i]*gen(nr+1, zostalo - (i<<nr))%M) % M;
    }
    mapa[MP(nr, zostalo)] = res;
    return res;
}

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

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

    printf("%lld\n", gen(0, m));

    return 0;
}

int main()
{
    solve();

    return 0;
}

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <cassert>
#include <vector>
#include <random>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>

//#include <atcoder/all>

#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=998244353;
const int dx[8]={-1,0,1,0,1,1,-1,-1},dy[8]={0,1,0,-1,1,-1,1,-1};

//using namespace atcoder;

ll Pow(ll n,ll p){
	ll r=1;
	for(;p>0;p>>=1){
		if(p&1) r=(r*n)%mod;
		n=(n*n)%mod;
	}
	return r;
}

const int M=1000005;
ll F[M];

void Init(){
	F[0]=1;
	for(int i=1;i<M;i++) F[i]=F[i-1]*i%mod;
}

ll Div(ll n,ll m){
	return n*Pow(m,mod-2)%mod;
}

ll nCk(ll n,ll k){
	return Div(F[n],F[n-k]*F[k]%mod);
}

ll dp[21][5005],N;

ll Rec(int i,int n){
	if(n==0) return 1;
	if(i<0) return 0;
	if(dp[i][n]>=0) return dp[i][n];
	ll res=0,x=1<<i;
	for(int j=0;j*x<=n&&j<=N;j+=2){
		(res+=nCk(N,j)*Rec(i-1,n-j*x))%=mod;
	}
	return dp[i][n]=res;
}

int m;

int main(){
	for(int i=0;i<21;i++) for(int j=0;j<5005;j++) dp[i][j]=-1;
	Init();
	cin>>N>>m;
	cout<<Rec(20,m)<<endl;
}


//*Bismillahir Rahmanir Raheem
//!BlackBeard
#include <bits/stdc++.h>
#include <algorithm>
#define NFS                      \
    ios::sync_with_stdio(false); \
    cin.tie(0)

#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define yes cout << "yes" << endl
#define No cout << "No" << endl
#define NO cout << "NO" << endl
#define no cout << "no" << endl

#define ll long long
#define ull unsigned long long
#define pb push_back

#define FILL(x, y) memset(x, y, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fore(arr) for (auto &x : (arr))
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define gcd(x, y) __gcd(x, y)
#define lcm(x, y) x *(y / gcd(x, y))
#define toint(c) c - 48
#define tochar(c) c + 48

#define MAX 2000010
#define MOD 100000007
#define bug cout << "*_*\n"
using namespace std;

template <class T>
void print(vector<T> v)
{
    fore(v) cout << x << ' ';
    cout << endl;
}
void solve()
{
    int h, w, sum = 0, a;
    cin >> h >> w;
    int mn = INT_MAX;
    for (int i = 0; i < h; i++)
    {
        for (int j = 0; j < w; j++)
        {
            cin >> a;
            sum += a;
            mn = min(a, mn);
        }
    }
    int ans = mn * h * w;
    cout << sum - ans << endl;
}

int main()
{
    NFS;
#ifndef ONLINE_JUDGE
    freopen("D:/code/C++/OJ/in.txt", "r", stdin);
    freopen("D:/code/C++/OJ/out.o", "w", stdout);
#endif
    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
}

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

int memo[1<<16];
const string yes="Yes",no="No";

int main(){
  int n,m,k;
  cin>>n>>m;
  vector<int>v(n*m);
  cin>>v.at(0);
  k=v.at(0);
  for(int i=1;i<n*m;i++){
    cin>>v.at(i);
    k=min(k,v.at(i));
  }
  int s=0;
  for(int i=0;i<n*m;i++){
    s+=v.at(i)-k;
  }
  cout<<s<<endl;
}


#include<bits/stdc++.h>

/*

Pranav Here!!

*/

using namespace std;

#define pb push_back
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)

#define sz(x) (int)(x).size()

#define ff first
#define ss second

#define mp make_pair

#define ll long long
#define ld long double
#define lli long long int

#define mod 1000000007

ll powmod(ll a,  ll b){
    if(b==0){
        return 1 ;
    }
    ll x = powmod(a,b/2);
    x = (x*x)%mod ;
    if(b%2){
        x = (a*x)%mod ;
    }
    return x;
}

set<ll> prm;

void Sieve(ll n)
{
  bool prime[n+1];
  memset(prime,true, sizeof(prime));
  for(ll p=2;p*p<=n;p++)
  {
    if(prime[p]==true)
    {
      for(ll i=p*p;i<=n;i+=p)
      {
        prime[i]=false;
      }
    }
  }
  if(!prm.empty()) {
    prm.clear();
  }
  for(ll p=2;p<=n;p++)
  {
    if(prime[p])
    {
      prm.insert(p);
    }
  }
}

ll fact(ll n) {
    ll res = 1;
    for(ll i=2;i<=n;i++) {
        res*=i;
    }
    return res;
}

ll _lcm(ll x,ll y) {
    return x*y/__gcd(x,y);
}

ll _gcd(ll a,ll b) {
    if(!a) {
        return b%mod;
    }
    return _gcd((b%a)%mod,a%mod);
}

ll nCr(ll n,ll r) {
    return (ld)fact(n)/(ld)(fact(r)*fact(n-r));
}

int main() {
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //Sieve(1000000);
    ll n,m;
    char ch;
    cin>>n>>m;
    char s[n][m];
    for(ll i=0;i<n;i++) {
        for(ll j=0;j<m;j++) {
            cin>>s[i][j];
        }
    }
    ll cnt=0;
    for(ll i=1;i<n;i++) {
        for(ll j=1;j<m;j++) {
            string r="";
            r+=s[i-1][j-1];
            r+=s[i-1][j];
            r+=s[i][j-1];
            r+=s[i][j];
            ll z=count(all(r),'#');
            if(z==1 || z==3) {
                cnt++;
            }
        }
    }
    cout<<cnt;
    return  0;
}


#include "bits/stdc++.h"
using namespace std;
#define int long long
using ll = long long;

signed main() {
	ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int N; cin >> N;
	vector<int>A(N);
	vector<int>sum(N + 1), ma(N+1);
	int before = 0;
	int tmp = 0;
	for (int i = 0; i < N; i++) {
		cin >> A[i];
		sum[i + 1] = sum[i] + A[i];
		if (A[i] > 0) {
			ma[i + 1] = max(ma[i], tmp + A[i]);
		}
		else {
			ma[i + 1] = max(ma[i], tmp);
		}
		before += A[i];
		tmp = before;
	}
	vector<int>sum2(N + 1);
	int now = 0;
	for (int i = 1; i <= N; i++) {
		sum2[i] = now + ma[i];
		now += sum[i];
	}
	int ans = 0;
	for (int i = 0; i <= N; i++) {
		ans = max(ans, sum2[i]);
	}
	cout << ans << endl;
	return 0;
}

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

int n, a, ans;

int32_t main () {
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &a);
		ans = __gcd(ans, a);
	}
	printf("%d\n", ans);
}


#include "bits/stdc++.h"

using namespace std;

int main(){
    int n;cin>>n;
    vector<pair<int, int>> v;
    for(int i=0;i<n;i++){
        int a,b;cin>>a>>b;
        v.push_back(make_pair(a,b));
    }
    int count=0;
    float slopey;
    float slopex;
    for(int j=0;j<v.size();j++){
        for(int i=j+1;i<v.size();i++){
            slopey = (v[j].second-v[i].second);
            slopex = (v[j].first-v[i].first);
            float slope = slopey/slopex;
            if(slope<=1&&slope>=-1) count++;
        }
    }
    cout<<count;
}

#include <bits/stdc++.h>

using namespace std;

#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 FOR(i, a, n) for (int i=(a); i<(n); ++i)
#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()

#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl;

template<class T>
ostream &operator<<(ostream &os, const vector <T> &v) {
    os << "[";
    REP(i, SZ(v)) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "]";
}

template<class T, class U>
ostream &operator<<(ostream &os, const pair <T, U> &p) {
    return os << "(" << p.first << " " << p.second << ")";
}

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

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld eps = 1e-9;

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

    int n; cin >> n;
    vi x(n), y(n);
    REP(i, n) cin >> x[i] >> y[i];

    int ans = 0;
    REP(i, n) {
        for (int j = i+1; j < n; ++j) {
            if (abs(y[i] - y[j]) <= abs(x[i] - x[j])) {
                ++ans;
            }
        }
    }
    cout << ans << endl;

    return 0;
}


// Template
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iomanip>
#include <tuple>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <array>
#include <cassert>
#include <cmath>
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll infll = 3003003003003003003LL;
template <typename T>
inline bool chmin(T &x, const T &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T>
inline bool chmax(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
    for (T &element : vec) is >> element;
    return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
    for (int i = 0, vec_len = (int)vec.size(); i < vec_len; ++i) {
        os << vec[i] << (i + 1 == vec_len ? "" : " ");
    }
    return os;
}
struct IOSET {
    IOSET() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(10);
    }
} ioset;

// Main
int main() {
    int n;
    cin >> n;
    vector<ll> a(n);
    cin >> a;

    ll mx = 0, sum = 0, sum2 = 0;
    rep(i, n) {
        chmax(mx, a[i]);
        sum += a[i];
        cout << sum + mx * (i + 1) + sum2 << '\n';
        sum2 += sum;
    }
}

#define DEBUG 0

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

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------

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

    if (S[0] != S.back()) cout << 1 << endl;
    else {
        int i;
        for (i = 0; i < S.size()-1; i++) {
            if ((S[i] != S[0]) && (S[i+1] != S.back())) {
                cout << 2 << endl;
                return 0;
            }
        }
        cout << -1 << endl;
    }

    return 0;
}


/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB emplace_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
 #define DEBUG(...) {printf("[DEBUG] ");W(__VA_ARGS__);}
#else
 #define DEBUG(...)
#endif
/*}}}*/
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
const int SIZE = 1<<20;
int N;
int x[SIZE], y[SIZE];
int cmpx(int v1, int v2){
    return x[v1]<x[v2];
}
int cmpy(int v1, int v2){
    return y[v1]<y[v2];
}
void solve() {
    VI d(N);
    REP(i,N)d[i]=i;
    sort(ALL(d),cmpx);
    VPII pp;
    REP(i,2){
        REPP(j,N-2,N){
            int x=d[i];
            int y=d[j];
            if(x>y)swap(x,y);
            if(x!=y)pp.PB(MP(x,y));
        }
    }
    sort(ALL(d),cmpy);
    REP(i,2){
        REPP(j,N-2,N){
            int x=d[i];
            int y=d[j];
            if(x>y)swap(x,y);
            if(x!=y)pp.PB(MP(x,y));
        }
    }
    SORT_UNIQUE(pp);
    VI an;
    REP(i,SZ(pp)){
        an.PB(max(abs(x[pp[i].F]-x[pp[i].S]),abs(y[pp[i].F]-y[pp[i].S])));
    }
    sort(ALL(an));
    W(an[SZ(an)-2]);
}
void input() {
    R(N);
    REP(i,N){
        R(x[i],y[i]);
    }
}
int main(){
    input();
    solve();
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef pair<long long, int> P;
int main(){
    int N;
    cin >> N;
    vector<P> X(N), Y(N);
    for(int i =0 ; i < N; i++) {
        long long x, y;
        cin >> x >> y;
        X[i] = {x, i};
        Y[i] = {y, i};
    }
    sort(X.begin(), X.end());
    sort(Y.begin(), Y.end());
    set<pair<int, int>> s;
    long long t = X[N - 1].first - X[0].first;
    vector<long long> temp;
    temp.push_back(t);
    s.insert({X[N - 1].second, X[0].second});
    s.insert({X[0].second, X[N - 1].second});
    long long te = Y[N - 1].first - Y[0].first;
    if(!s.count({Y[N - 1].second, Y[0].second})){
        s.insert({Y[N - 1].second, Y[0].second});
        s.insert({Y[0].second, Y[N - 1].second});
        temp.push_back(te);
    }
    t = X[N - 2].first - X[0].first;
    if(!s.count({X[N - 2].second, X[0].second})){
        s.insert({X[N - 2].second, X[0].second});
        s.insert({X[0].second, X[N - 2].second});
        temp.push_back(t);
    }
    t = X[N - 1].first - X[1].first;
    if(!s.count({X[N - 1].second, X[1].second})){
        s.insert({X[N - 1].second, X[1].second});
        s.insert({X[1].second, X[N - 2].second});
        temp.push_back(t);
    }
    t = Y[N - 2].first - Y[0].first;
    if(!s.count({Y[N - 2].second, Y[0].second})){
        s.insert({Y[N - 2].second, Y[0].second});
        s.insert({Y[0].second, Y[N - 2].second});
        temp.push_back(t);
    }
    t = Y[N - 1].first - Y[1].first;
    if(!s.count({Y[N - 1].second, Y[1].second})){
        s.insert({Y[N - 1].second, Y[1].second});
        s.insert({Y[1].second, Y[N - 1].second});
        temp.push_back(t);
    }
    sort(temp.begin(), temp.end());
    int sz = temp.size();
    cout << temp[sz - 2] << endl;
}


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

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

    if(C == 0) {
        if(A > B) cout << "Takahashi" << endl;
        else cout << "Aoki" << endl;
    }else {
        if(A < B) cout << "Aoki" << endl;
        else cout << "Takahashi" << endl;
    }

    return 0;
}

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
///Welcome to Nasif's Code
#define bug printf("bug\n");
#define bug2(var) cout<<#var<<" "<<var<<endl;
#define co(q) cout<<q<<endl;
#define all(q) (q).begin(),(q).end()
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define MODADD(ADD_X,ADD_Y)  (ADD_X+ADD_Y)%MOD
#define MODSUB(SUB_X,SUB_Y)  ((SUB_X-SUB_Y)+MOD)%MOD
#define MODMUL(MUL_X,MUL_Y)  (MUL_X*MUL_Y)%MOD
#define LCM(LCM_X,LCM_Y) (LCM_X/__gcd(LCM_X,LCM_Y))*LCM_Y
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9+7;
const int MAX = 1e6+50;
int dx[]= {1,0,-1,0,1,-1,1,-1};
int dy[]= {0,1,0,-1,1,-1,-1,1};
int main()
{
    FastRead
    //freopen("output.txt", "w", stdout);
    int a,b,c;
    cin>>a>>b>>c;

    while(1)
    {
        if(c)
        {
            if(b==0){
                cout<<"Takahashi"<<endl;
                return 0;
            }
            b--;
        }
        else
        {
             if(a==0){
                cout<<"Aoki"<<endl;
                return 0;
            }
            a--;

        }
        c^=1;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin>>n;
 if(floor(1.08*n)<206) cout<<"Yay!";
 if(floor(1.08*n)==206) cout<<"so-so";
 if(floor(1.08*n)>206) cout<<":(";
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto&(x) : (a))
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  REP(i, n) { cin >> a[i]; }
  sort(ALL(a));

  vector<ll> s(n + 1, 0);
  REP(i, n) { s[i + 1] = s[i] + a[i]; }
  ll ans = 0;
  FOR(i, 1, n + 1) { ans += s[n] - s[i] - (n - i) * a[i - 1]; }
  cout << ans << endl;
}

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i,n) for(int i = 0; i < (n); i++)

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i,n) cin >> a[i];
    sort(a.begin(), a.end());
    ll ans = 0;
    rep(i,n) {
        ans += (ll)a[i] * i;
        ans -= (ll)a[i] * (n-1-i);
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>

template<long long mod>
class modint{
private:
  long long a;
public:
  constexpr modint(const long long x = 0) noexcept : a((x%mod+mod)%mod) {}
  constexpr long long& value() noexcept { return a; }
  constexpr const long long& value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint& operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if(a >= mod) a -= mod;
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if(a < rhs.a) a += mod;
    a -= rhs.a;
    return *this;
  }
  constexpr modint& operator*=(const modint rhs) noexcept {
    a = a*rhs.a%mod;
    return *this;
  }
  constexpr modint& operator/=(modint rhs) noexcept {
    long long k = mod-2;
    while(k > 0){
      if(k&1){
        *this *= rhs;
      }
      rhs *= rhs;
      k /= 2;
    }
    return *this;
  }
  friend std::ostream& operator<<(std::ostream &os, const modint &X){
    return os << X.a;
  }
  friend std::istream& operator>>(std::istream &is, modint &X){
    is >> X.a;
    X.a %= mod;
    if(X.a < 0) X.a += mod;
    return is;
  }
};

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

void solve(){
  using mint = modint<1000000007>;
  long long N, A, B;
  cin >> N >> A >> B;
  if(N < A+B){
    cout << 0 << '\n';
    return;
  }
  if(A < B) swap(A,B);
  mint n = N, a = A, b = B;

  mint ans = (n-a+1)*(n-a+1)*(n-b+1)*(n-b+1);

  ans -= (b-1)*(n*2-b-a*2+2)/2*(b-1)*(n*2-b-a*2+2)/2*4;
  ans -= (b-1)*(n*2-b-a*2+2)/2*(n-a+1)*(a-b+1)*4;

  ans -= (n-a+1)*(n-a+1)*(a-b+1)*(a-b+1);

  cout << ans << '\n';
}

int main(){
  int T;
  cin >> T;
  while(T--) solve();
}

#define _DEBUG
#include "bits/stdc++.h"
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#ifdef _DEBUG
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#else
#define debug(...)
#endif
#define rep(index,num) for(int index=0;index<(int)num;index++)
#define rep1(index,num) for(int index=1;index<=(int)num;index++)
#define brep(index,num) for(int index=(int)num-1;index>=0;index--)
#define brep1(index,num) for(int index=(int)num;index>0;index--)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-7
#define mp(a1,a2) make_pair(a1,a2)
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl=(ll)1e+18+1;
int INF=1e+9+1;
/*modint構造体*/
template<unsigned int mod_>
struct ModInt{
	using uint = unsigned int;
	using ll = long long;
	using ull = unsigned long long;

	constexpr static uint mod = mod_;

	uint v;
	ModInt():v(0){}
	ModInt(ll _v):v(normS(_v%mod+mod)){}
	explicit operator bool() const {return v!=0;}
	static uint normS(const uint &x){return (x<mod)?x:x-mod;}		// [0 , 2*mod-1] -> [0 , mod-1]
	static ModInt make(const uint &x){ModInt m; m.v=x; return m;}
	ModInt operator+(const ModInt& b) const { return make(normS(v+b.v));}
	ModInt operator-(const ModInt& b) const { return make(normS(v+mod-b.v));}
	ModInt operator-() const { return make(normS(mod-v)); }
	ModInt operator*(const ModInt& b) const { return make((ull)v*b.v%mod);}
	ModInt operator/(const ModInt& b) const { return *this*b.inv();}
	ModInt& operator+=(const ModInt& b){ return *this=*this+b;}
	ModInt& operator-=(const ModInt& b){ return *this=*this-b;}
	ModInt& operator*=(const ModInt& b){ return *this=*this*b;}
	ModInt& operator/=(const ModInt& b){ return *this=*this/b;}
	ModInt& operator++(int){ return *this=*this+1;}
	ModInt& operator--(int){ return *this=*this-1;}
	ll extgcd(ll a,ll b,ll &x,ll &y) const{
		ll p[]={a,1,0},q[]={b,0,1};
		while(*q){
			ll t=*p/ *q;
			rep(i,3) swap(p[i]-=t*q[i],q[i]);
		}
		if(p[0]<0) rep(i,3) p[i]=-p[i];
		x=p[1],y=p[2];
		return p[0];
	}
	ModInt inv() const {
		ll x,y;
		extgcd(v,mod,x,y);
		return make(normS(x+mod));
	}
	ModInt pow(ll p) const {
		if(p<0) return inv().pow(-p);
		ModInt a = 1;
		ModInt x = *this;
		while(p){
			if(p&1) a *= x;
			x *= x;
			p >>= 1;
		}
		return a;
	}
	bool operator==(const ModInt& b) const { return v==b.v;}
	bool operator!=(const ModInt& b) const { return v!=b.v;}
	friend istream& operator>>(istream &o,ModInt& x){
		ll tmp;
		o>>tmp;
		x=ModInt(tmp);
		return o;
	}
	friend ostream& operator<<(ostream &o,const ModInt& x){ return o<<x.v;}
};
using mint = ModInt<1000000007>;
int main(){
	int T;
	scan(T);
	rep(t,T){
		ll N,A,B;
		scan(N>>A>>B);
		if(N<A+B){
			prin(0);
			continue;
		}
		mint all=(mint)(N-A+1)*(N-A+1)*(N-B+1)*(N-B+1);
		mint oneDimNotCross=(mint)(N-A-B+2)*(N-A-B+1);
		mint oneDimCross=(mint)(N-A+1)*(N-B+1)-oneDimNotCross;
		mint ans=all-oneDimCross*oneDimCross;
		prin(ans);
	}
	return 0;
}


// give up
#include<iostream>
#include<vector>

constexpr auto MOD = 998244353LL;

template <class T> void in(T& a) {std::cin >> a;}
template <class T, class... U> void in(T& a, U&... b) {in(a); in(b...);}
template <char D = '\n', class T> void out(T a) {std::cout << a << D;}
template <class T, class... U> void out(T a, U... b) {out<' '>(a); out(b...);}

template <long long MOD> auto make_comb_mod(long long size) {
  size++;
  std::vector<long long> fact(size, 1), inv(size, 1), fact_inv(size, 1);
  for (auto i = 2LL; i < size; i++) {
    fact[i] = fact[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    fact_inv[i] = fact_inv[i - 1] * inv[i] % MOD;
  }
  return [fact, fact_inv](long long n, long long k) {
    return n >= k && k >= 0
      ? fact[n] * (fact_inv[k] * fact_inv[n - k] % MOD) % MOD : 0LL;
  };
}

int main() {
  // input
  long long N, M;
  in(N, M);

  // solve
  std::vector<long long> dp(1, 1);
  const auto comb_mod = make_comb_mod<MOD>(5000);
  for (auto i = 1; i <= M; i++) {
    dp.emplace_back(0);
    if (~i & 1) {
      for (auto j = 0; j * 2 <= i; j++) {
        dp.back() += (comb_mod(N, j * 2) * dp[i / 2 - j]) % MOD;
        dp.back() %= MOD;
      }
    }
  }
  auto result = dp.back();

  // output
  out(result);
}


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

int main(){
  int A, B, C;
  cin >> A >> B >> C;
  if(A==B){
    if(C==0) ++B;
    else ++A;
  }
  if(A>B){
    cout << "Takahashi" << endl;
  }else{
    cout << "Aoki" << endl;
  }
  return 0;
}

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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int test=1;
    //cin>>test;
    while(test--)
    {
      vector<int> v(3);
      cin>>v[0]>>v[1]>>v[2];
      sort(v.begin(),v.end());
      //cout<<v[0]<<v[1]<<v[2];
      if(v[1]-v[0]==v[2]-v[1])
        cout<<"Yes\n";
      else
        cout<<"No\n";
    }
    return 0;
}