#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define LL long long
#define int long long
#define ull unsigned long long
#define fi first
#define se second
#define pll pair<LL, LL>
#define pii pair<int, int>
#define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define SZ(x) ((int)(x.size()))
#define LC (id<<1)
#define RC (id<<1|1)

constexpr int N = 1e6+9;
constexpr int M = 1e9+7;
#ifdef int
constexpr int INF = 0x3f3f3f3f3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcfcfcfcfcf;
#else
constexpr int INF = 0x3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcf;
#endif

signed main() {
    fastio;
    int n;
    cin >> n;
    vector<int> fa(n, -1);
    vector<vector<int>> ch(n);
    for (int i = 1; i < n; i++) {
        cin >> fa[i];
        fa[i]--;
        ch[fa[i]].emplace_back(i);
    }
    vector<int> sz(n, 1);
    // sz*2 + sz-1
    vector<int> dp(n, 0);
    function<void(int)> dfs = [&](int u) {
        dp[u] = 1;
        array<vector<pii>, 2> sub;
        for (int v: ch[u]) {
            dfs(v);
            sz[u] += sz[v];
            sub[sz[v]&1].emplace_back(dp[v], sz[v]-dp[v]);
        }
        sort(sub[0].begin(), sub[0].end(), [](const pii& p1, const pii& p2) {
            return p1.fi-p1.se > p2.fi-p2.se;
        });
        sort(sub[1].begin(), sub[1].end(), [](const pii& p1, const pii& p2) {
            return p1.fi-p1.se < p2.fi-p2.se;
        });
        while (!sub[0].empty() && sub[0].back().fi <= sub[0].back().se) {
            dp[u] += sub[0].back().fi;
            sub[0].pop_back();
        }
        while (!sub[0].empty()) {
            if (SZ(sub[1]) & 1)
                dp[u] += sub[0].back().se;
            else
                dp[u] += sub[0].back().fi;
            sub[0].pop_back();
        }
        for (int i = 0; i < SZ(sub[1]); i++) {
            if (i & 1) dp[u] += sub[1][i].se;
            else dp[u] += sub[1][i].fi;
        }
    };
    dfs(0);
    cout << dp[0] << "\n";
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
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;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;

template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

// graph template
// ref : https://ei1333.github.io/library/graph/graph-template.cpp
template<typename T=int>
struct Edge{
    int from,to;
    T w;
    int idx;
    Edge()=default;
    Edge(int from,int to,T w=1,int idx=-1):from(from),to(to),w(w),idx(idx){}
    operator int() const{return to;}
};

template<typename T=int>
struct Graph{
    vector<vector<Edge<T>>> g;
    int V,E;
    Graph()=default;
    Graph(int n):g(n),V(n),E(0){}

    size_t size(){
        return g.size();
    }
    void resize(int k){
        g.resize(k);
    }
    inline const vector<Edge<T>> &operator[](int k)const{
        return (g.at(k));
    }
    inline vector<Edge<T>> &operator[](int k){
        return (g.at(k));
    }
    void add_directed_edge(int from,int to,T cost=1){
        g[from].emplace_back(from,to,cost,E++);
    }
    void add_edge(int from,int to,T cost=1){
        g[from].emplace_back(from,to,cost,E);
        g[to].emplace_back(to,from,cost,E++);
    }
    void read(int m,int pad=-1,bool weighted=false,bool directed=false){
        for(int i=0;i<m;i++){
            int u,v;cin>>u>>v;
            u+=pad,v+=pad;
            T w=T(1);
            if(weighted) cin>>w;
            if(directed) add_directed_edge(u,v,w);
            else         add_edge(u,v,w);
        }
    }
};

// 結局小さいのからとらせればいい説
signed main(){
    int n;cin>>n;
    Graph<> g(n);
    for(int i=1;i<n;i++){
        int j;cin>>j;j--;
        g.add_directed_edge(j,i);
    }

    vector<int> dp(n,0);// dp[i] : iの1を取る手順の人の利得
    vector<int> sz(n,0);
    function<void(int)> dfs=[&](int cur){
        int s=1;
        for(auto &e:g[cur]){
            dfs(e);
            s+=sz[e];
        }

        sz[cur]=s;

        int res=1;// A-B
        bool A=false;
        {
            vector<int> ch,no;
            for(auto &e:g[cur]){
                if(sz[e]%2) ch.push_back(dp[e]);
                else        no.push_back(dp[e]);
            }

            sort(ALL(ch));
            sort(ALL(no));

            int chi=0,noi=0;
            while(chi<(int)ch.size() or noi<(int)no.size()){
                // 相手に押し付けたい
                if(noi<(int)no.size() and no[noi]<0){
                    if(!A) res+=no[noi];
                    else   res-=no[noi];
                    noi++;
                }
                else if(chi<(int)ch.size()){
                    if(!A) res+=ch[chi];
                    else   res-=ch[chi];
                    chi++;
                    A^=1;
                }else{
                    if(!A) res+=no[noi];
                    else   res-=no[noi];
                    noi++;
                }
            }
        }
        dp[cur]=res;
    };

    dfs(0);
    cout<<(dp[0]+n)/2<<endl;
    return 0;
}


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

int main(){
  double N; cin >> N;
  double ans = 0;
  for(int i=1; i<N; i++){
    ans += N/i;
  }
  printf("%.10f",ans);
}

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
double dp[maxn];
int main()
{
	int n;scanf("%d",&n);
	dp[0]=0;
	for(int i=1;i<=n;i++){
		dp[i]=(dp[i-1]*(n-i+1)+n)/(n-i+1);
	}
	printf("%.10lf\n",dp[n]-1);
}


//AUTHOR: RAVAN_2070
//PUNE INSTITUTE OF COMPUTER TECHNOLOGY
/*
      I ♥ CLARICE STARLING...
        EXPLAINATION BELOW->
*/
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<pair<ll,ll>> vii;
typedef pair<ll,ll> pii;
typedef map<ll,ll> mii;
#define MOD7 1000000007
#define MOD9 1000000009
#define pi 3.1415926535
#define Test_cases ll TC;cin>>TC;while(TC--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(), x.rend()
#define sp(x) fixed<<setprecision(x)
#define sz(x) (ll)(x.size())
#define fo(i,a,b) for(i=a;i<b;i++)
#define foe(i,a,b) for(i=a;i<=b;i++)
const ll INF = (ll)2e18 + 77;
ll num(ll a,int f)
{
    vector<ll>d;
    while(a)
    {
        d.pb(a%10);
        a/=10;
    }
    if(f==0)sort(all(d));
    else sort(all(d),greater<ll>());
    ll ans=0ll;
    for(auto dg:d)
    {
        ans*=10;
        ans+=dg;
    }
    return ans;
}
void solve()
{
    ll N,K,i;cin>>N>>K;
    ll a=N;
    foe(i,1,K)
    {
       a=(num(a,1)-num(a,0));
    }
    cout<<a<<"\n";
}
int main()
{
   fastio
   //Test_cases
   solve();
  return 0;
}

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

// function to count the no of removal of digits
// to make a very large number divisible by 3
int divisible(string num)
{
    int n = num.length();

    // add up all the digits of num
    int sum = accumulate(begin(num), end(num), 0) - '0' * 1;

    // if num is already is divisible by 3
    // then no digits are to be removed
    if (sum % 3 == 0)
        return 0;

    // if there is single digit, then it is
    // not possible to remove one digit.
    if (n == 1)
        return -1;

    // traverse through the number and find out
    // if any number on removal makes the sum
    // divisible by 3
    for (int i = 0; i < n; i++)
        if (sum % 3 == (num[i] - '0') % 3)
            return 1;

    // if there are two numbers then it is
    // not possible to remove two digits.
    if (n == 2)
        return -1;

    // Otherwise we can always make a number
    // multiple of 2 by removing 2 digits.
    return 2;
}

// driver program to test the above function
int main()
{
    string num ;cin>>num;
    cout << divisible(num);
    return 0;
}


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

int main(){
  ll n,m,x,y,inf=1e18;
  cin >> n >> m >> x >> y;
  x--; y--;
  vector<vector<vector<ll>>> a(n,vector<vector<ll>>(0,vector<ll>(3)));
  rep(i,m){
    ll c,d,t,k;
    cin >> c >> d >> t >> k;
    c--; d--;
    a[c].emplace_back();
    a[d].emplace_back();
    a[c][(int)a[c].size()-1]={d,t,k};
    a[d][(int)a[d].size()-1]={c,t,k};
  }

  vector<ll> b(n,inf);
  queue<int> q;
  q.push(x); b[x]=0;
  while(q.size()){
    int now=q.front(); q.pop();
    rep(i,(int)a[now].size()){
      ll p=(b[now]-1)/a[now][i][2]+1;//発車時刻
      p*=a[now][i][2];
      if(b[now]==0) p=0;
      if(b[a[now][i][0]]>p+a[now][i][1]){
        b[a[now][i][0]]=p+a[now][i][1];
        q.push(a[now][i][0]);
      }
    }
  }
  if(b[y]==inf) cout << -1 << endl;
  else cout << b[y] << endl;
}

#pragma GCC optimize("Ofast")
#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>

#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define pb push_back
#define fi first
#define se second
#define ins insert
#define sz(x) (int)x.size()
#define dbg(x) cout << x << "\n";

const int N = 1e5 + 5;
const long long int mod = 1e9 + 7;
const long long int Mod = 998244353;
const long double Pi = acos(-1);
const long long int Inf = 4e18;

using namespace std;

struct nxt{
        long long int to, ti, in;
};

vector <struct nxt> g[N];
vector <long long int> ans(N);

long long int Dijkstra (long long int x, long long int y){
        ans.assign(N, Inf);
        ans[x] = 0;
        set < pair <long long int, long long int> > s;
        s.ins({0, x});
        while(!s.empty()){
                long long int cur = s.begin()->fi, idx = s.begin()->se;
                s.erase(s.begin());
                for(auto u: g[idx]){
                        long long int upd = cur;
                        if(cur % u.in != 0){
                                upd = ((cur + u.in - 1)/u.in) * u.in;
                        }
                        if(ans[u.to] > upd + u.ti){
                                ans[u.to] = upd + u.ti;
                                s.ins({ans[u.to], u.to});
                        }
                }
        }
        if(ans[y] == Inf) return -1;
        else return ans[y];
}

void TestCase (){
        long long int n, m, x, y;
        cin >> n >> m >> x >> y;
        for(int i = 1; i<=m; i++){
                long long int u, v, t, k;
                cin >> u >> v >> t >> k;
                g[u].pb({v, t, k});
                g[v].pb({u, t, k});
        }
        cout << Dijkstra(x, y);
}

int main(){
        IOS;
        int T = 1;
        // cin >> T;
        while(T--){
                TestCase();
        }
        return 0;
}


#include <bits/stdc++.h>

using namespace std;

// using mint = long double;
// using mint = modint998244353;
// using mint = modint1000000007;

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> T;
typedef pair<ll, vector<ll>> Pd;

const ll INF = 1e18;
const ll fact_table = 320000;

priority_queue<ll> pql;
priority_queue<P> pqp;
// big priority queue
priority_queue<ll, vector<ll>, greater<ll>> pqls;
priority_queue<P, vector<P>, greater<P>> pqps;

// small priority queue
// top pop

ll dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
ll dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
//↓,→,↑,←

#define p(x) cout << x << "\n";
#define el cout << "\n";
#define pe(x) cout << x << " ";
#define ps(x) cout << fixed << setprecision(25) << x << endl;
#define pu(x) cout << (x);
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pc(x) cout << x << ",";
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, a, b) for (ll i = a; i <= (b); i++)
#define rep3(i, a, b) for (ll i = a; i >= (b); i--)
#define all(c) begin(c), end(c)
#define sorti(v) sort(all(v))
#define sortd(v)                                                               \
    sort(all(v));                                                              \
    reverse(all(v));
#define SUM(v) accumulate(all(v), 0LL)
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))

typedef vector<ll> vec;
typedef vector<vector<ll>> mat;

// vec v(n) -> 長さnのベクトルを宣言
// mat dp(h, vec(w)) -> h * w の行列を宣言

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

ll mypow(ll a, ll b, ll m = mod) {
    ll x = 1;
    while (b) {
        while (!(b & 1)) {
            (a *= a) %= m;
            b >>= 1;
        }
        (x *= a) %= m;
        b--;
    }
    return x;
}
vec rv(ll read) {
    vec res(read);
    for (int i = 0; i < read; i++) {
        cin >> res[i];
    }
    return res;
}
/*
ll fact[fact_table + 5], rfact[fact_table + 5];

void c3_init() {
    fact[0] = rfact[0] = 1;
    for (ll i = 1; i <= fact_table; i++) {
        fact[i] = (fact[i - 1] * i) % mod;
    }
    rfact[fact_table] = mypow(fact[fact_table], mod - 2, mod);
    for (ll i = fact_table; i >= 1; i--) {
        rfact[i - 1] = rfact[i] * i;
        rfact[i - 1] %= mod;
    }
    return;
}

ll c3(ll n, ll r) {
    return (((fact[n] * rfact[r]) % mod) * rfact[n - r]) % mod;
}
*/

bool icpc = false;
bool multicase = false;

ll n, m;
ll xd[200005], yd[200005];

bool solve() {
    cin >> n;
    vector<P> xmax, xmin, ymax, ymin;

    rep(i, n) {
        ll x, y;
        cin >> x >> y;
        xd[i] = x, yd[i] = y;
        xmax.pb(P(x, i));
        xmin.pb(P(x, i));

        sorti(xmin);
        sortd(xmax);

        ymax.pb(P(y, i));
        ymin.pb(P(y, i));

        sorti(ymin);
        sortd(ymax);

        if (i >= 2) {
            xmin.pop_back();
            xmax.pop_back();
            ymin.pop_back();
            ymax.pop_back();
        }
    }
    set<P> st;
    rep(i, 2) {
        rep(j, 2) {
            st.insert(P(xmax[i].second, xmin[j].second));
            st.insert(P(ymax[i].second, ymin[j].second));
        }
    }
    vec v;
    for (auto pa : st) {
        auto [a, b] = pa;
        ll dis = max(abs(xd[a] - xd[b]), abs(yd[a] - yd[b]));
        v.pb(dis);
    }
    sortd(v);
    ll ans = v[1];
    p(ans);

    return true;
}

/*

*/

int main() {
    // ios::sync_with_stdio(false);
    // cin.tie(nullptr);
    if (icpc) {
        while (solve())
            ;
        return 0;
    }
    ll q, testcase = 1;
    if (multicase) {
        cin >> q;
    } else {
        q = 1;
    }
    while (q--) {
        // cout << "Case #" << testcase << ": ";
        solve();
        testcase++;
    }

    // solve();
    return 0;
}

/*
One more time, one more time
“Please just lemme roll for another day”
That’s what she said, What she said
Don’t you know the meanings are hidden under cover?

“One more time? Stop it’s fine.”
“Are’nt you getting tired of doing this?”
“I’ll stop your breath from rolling out… for now.”
*/

/*
... - "How many times will you try?"
.. - "As long as I can bear it"
*/

#include <bits/stdc++.h>
#define fastio()                  \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ull unsigned long long
#define ld long double
#define pow power
#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define siz(a) int((a).size())
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define Forn(i,a,b) for(int (i)=(a);(i) >= (b); --(i))
#define endl "\n"
#define pi 3.14159265
const ll mod = 1000 * 1000 * 1000 + 7;
const ll mod1 = 998244353;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7;
//typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
//typedef tree<pair<ll, ll>,null_type,less<pair<ll, ll>>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
using namespace std;

ll power(ll x, ll y)
{
   ll res = 1;

    while (y > 0)
    {

        if (y & 1)
            res = (long long)(res*x);

        y = y>>1;

        x = (long long)(x*x);

        //cout<<x<<'\n';
    }
    return res;
}

// Stolen Templates
template <typename T>
T MIN(T first) { return first; }

template <typename T, typename... Args>
T MIN(T first, Args... args) { return min(first, MIN(args...)); }

template <typename T>
T MAX(T first) { return first; }

template <typename T, typename... Args>

T MAX(T first, Args... args) { return max(first, MAX(args...)); }
// Stolen Templates end here

int main()
{

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

fastio();

    ll n;
    cin>>n;

    vector <ll> x(n); vector <ll> y(n);
    vector <pii> tx(n); vector <pii> ty(n);

    For(i,0,n) {
        cin>>x[i]>>y[i];
        tx[i].ff = x[i];
        tx[i].ss = i;
        ty[i].ff = y[i];
        ty[i].ss = i;
    }

    ll av1,av2;

    sort(all(tx)); sort(all(ty));

    if (ty.back().ff - ty[0].ff >= tx.back().ff - tx[0].ff) {
        av1 = ty.back().ss;
        av2 = ty[0].ss;
    } else {
        av1 = tx.back().ss;
        av2 = tx[0].ss;
    }

    ll ans = 0;

    For(i,0,n) {
        tx[i].ff = x[i];
        tx[i].ss = i;
        ty[i].ff = y[i];
        ty[i].ss = i;
    }

    vector <pii> cx[3]; vector <pii> cy[3];

    For(i,0,n) {
        if (tx[i].ss != av1) {
            cx[0].pb(tx[i]);
            cy[0].pb(ty[i]);
        }
        if (tx[i].ss != av2) {
            cx[1].pb(tx[i]);
            cy[1].pb(ty[i]);
        }
        if (tx[i].ss != av1 and tx[i].ss != av2) {
            cx[2].pb(tx[i]);
            cy[2].pb(ty[i]);
        }
    }

    For(i,0,3) {
        sort(all(cx[i]));
        sort(all(cy[i]));
        ans = max(ans,cy[i].back().ff - cy[i][0].ff);
        ans = max(ans,cx[i].back().ff - cx[i][0].ff);
    }

    cout<<ans;

return 0;
}

// check all product based operations for integer overflow
// careful of renamed variables especially in loops
//python interactive_runner.py python local_testing_tool.py 0 -- c.exe


#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;

const int MAX = 550;
const int MAXM = 1e8+50;
struct Edge{
    int v, w, next;
}es[MAXM];
int head[MAX*MAX], k, n, m;
int A[MAX][MAX], B[MAX][MAX];
int id[MAX][MAX], idx;
int d[MAX*MAX];
bool vis[MAX*MAX];

void add(int u, int v, int w) {
    es[++k].next = head[u]; es[k].v = v; es[k].w = w; head[u] = k;
}

int dij() {
    fill(d, d+n*m+1, 1e7);
    priority_queue<P, vector<P>, greater<P> > q;
    q.push(P(0, 1));
    d[1] = 0;
    while (q.size()) {
        int u = q.top().second; q.pop();
        if (vis[u]) continue;
        vis[u] = true;
        //if (u == n*m) return d[u];
        for (int i = head[u]; i; i = es[i].next) {
            int v = es[i].v;
            if (!vis[v] && d[v]>d[u]+es[i].w) {
                d[v] = d[u] + es[i].w;
                q.push(P(d[v], v));
            }
        }
    }
    return d[n*m];
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) id[i][j] = ++idx;
    for (int i = 1; i <= n; i++) for (int j = 1; j < m; j++) scanf("%d", &A[i][j]);
    for (int i = 1; i < n; i++) for (int j = 1; j <= m; j++) scanf("%d", &B[i][j]);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (j > 1) {
                add(id[i][j], id[i][j-1], A[i][j-1]);
                //add(id[i][j-1], id[i][j], A[i][j-1]);
            }
            if (j < m) {
                add(id[i][j], id[i][j+1], A[i][j]);
                //add(id[i][j+1], id[i][j], A[i][j]);
            }
            if (i < n) {
                add(id[i][j], id[i+1][j], B[i][j]);
                //add(id[i+1][j], id[i][j], B[i][j]);
            }
            if (i > 1) {
                for (int k = i-1; k >= 1; k--) {
                    add(id[i][j], id[k][j], i-k+1);
                }
            }
        }
    }
    printf("%d\n", dij());
    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;

#define ff                  first
#define ss                  second
#define pb                  push_back
#define eb                  emplace_back
#define mp                  make_pair
#define lb                  lower_bound
#define ub                  upper_bound
#define setbits(x)          __builtin_popcountll(x)
#define zrobits(x)          __builtin_ctzll(x)
#define sz(v)               (int)v.size()
#define ps(y)               cout << fixed << setprecision(y)
#define ms(arr, v)          memset(arr, v, sizeof(arr))
#define all(v)              v.begin(), v.end()
#define rall(v)             v.rbegin(), v.rend()
#define trav(x, v)          for(auto &x: v)
#define w(t)                int t; cin >> t; while(t--)
#define rep0(i, n)          for(int i = 0; i < n; i++)
#define rrep0(i, n)         for(int i = n - 1; i >= 0; i--)
#define rep1(i, n)          for(int i = 1; i <= n; i++)
#define rrep1(i, n)         for(int i = n; i > 0; i--)
#define inp(arr, n)         rep0(i, n) cin >> arr[i];
#define rep(i, a, b)        for(int i = a; i <= b; i++)
#define rrep(i, a, b)       for(int i = a; i >= b; i--)

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef map<ll, ll> mii;
typedef map<char, ll> mci;
typedef priority_queue<ll> pq_mx;
typedef priority_queue<ll, vi, greater<>> pq_mn;
typedef tree<ll, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> pbds;
/*
 * find_by_order(i) -> returns an iterator to the element at ith position (0 based)
 * order_of_key(i)  -> returns the position of element i (0 based)
 */

const int N = 2e6 + 5;
const int mod = 1e9 + 7;
//const int mod = 998244353;
const ll inf = 1e18;

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

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

const int M = 1e7 + 5;
ll spf[M], mobius[M];
vi primes;

void sieve() {
    rep(x, 2, 1e7) {
        if (spf[x] == 0) spf[x] = x, primes.pb(x);
        for (int y = 0; y < sz(primes) and primes[y] <= spf[x] and x * primes[y] <= 1e7; y++)
            spf[x * primes[y]] = primes[y];
    }
}

void Mobius() {
    /*
     * mobius[x] =  1 => x is a square-free +ve int with even no. of prime factors
     * mobius[x] = -1 => x is a square-free +ve int with odd no. of prime factors
     * mobius[x] =  0 => x has a squared prime factor
     */

    rep1(x, 1e7) {
        if (x == 1) mobius[x] = 1;
        else {
            if (spf[x] == spf[x / spf[x]]) mobius[x] = 0;
            else mobius[x] = -mobius[x / spf[x]];
        }
    }
}

int main() {
    fio();
    sieve();
    Mobius();

    int l, r;
    cin >> l >> r;
  	if(l == 1) ++l;

    ll res = 0;
    rep(x, 2, r) {
        ll cur = r / x - (l - 1) / x;
        res -= cur * cur * mobius[x];
    }
//    cout << res << endl;
    rep(x, l, r) {
        --res;
        for (int y = 2 * x; y <= r; y += x) res -= 2;
    }
    cout << res;

    return 0;
}

#pragma GCC optimize(2)
#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;
#define int long long
#define maxn 1000005
vector < int > d[maxn];
int cnt;
bool vis[maxn];
int prime[maxn], mu[maxn], pre[maxn];

void init( int n ) {
	mu[1] = 1;
	for( int i = 2;i <= n;i ++ ) {
		if( ! vis[i] ) prime[++ cnt] = i, mu[i] = -1;
		for( int j = 1;j <= cnt && i * prime[j] <= n;j ++ ) {
			vis[i * prime[j]] = 1;
			if( i % prime[j] == 0 ) {
				mu[i * prime[j]] = 0;
				break;
			}
			mu[i * prime[j]] = -mu[i];
		}
	}
	for( int i = 1;i <= n;i ++ ) pre[i] = pre[i - 1] + mu[i];
}

int calc( int n, int m ) {
	if( n > m ) swap( n, m );
	int last, ans = 0;
	for( int i = 1;i <= n;i = last + 1 ) {
		last = min( n / ( n / i ), m / ( m / i ) );
		ans += ( pre[last] - pre[i - 1] ) * ( n / i ) * ( m / i );
	}
	return ans;
}

signed main() {
	int L, R;
	scanf( "%lld %lld", &L, &R );
	init( R );
	int ans = 0;
	for( int k = 2;k <= R;k ++ ) {
		int l = ( L - 1 ) / k, r = R / k;
		ans += calc( r, r ) - calc( l, r ) - calc( r, l ) + calc( l, l );
	}
	for( int i = 2;i <= R;i ++ )
		for( int j = i << 1;j <= R;j += i )
			d[j].push_back( i );
	for( int i = max( 2ll, L );i <= R;i ++ ) {
		for( int j = d[i].size() - 1;~ j;j -- )
			if( d[i][j] >= L ) ans --;
			else break;
		ans -= ( R / i );
	}
	printf( "%lld\n", ans );
	return 0;
}


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

  int main(){
    int h , w;
    cin >> h >> w;
    int n = h * w;
    vector<int> v(n);
    rep(i,n){
    cin >> v[i];
    }
    int Min = *min_element(ALL(v));
    int sum = 0;
    rep(i,n){
    sum += v[i] - Min;
    }
    cout << sum;
  }

//Codeforcesで128bit整数を使いたいとき
//→__int128_tを使う&GNU C++17 (64)で提出する

//インクルードなど
#include <iostream> // cout, endl, cin
// string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower

#include <string>
using namespace std;
typedef long long ll;

//イテレーション
#define REP(i,n) for(ll i=0;i<ll(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=ll(b);i--)
#define FORA(i,I) for(const auto& i:I)
//x:コンテナ
#define ALL(x) x.begin(),x.end()
#define SIZE(x) ll(x.size())
//定数
#define INF32 2147483647 //2.147483647×10^{9}:32bit整数のinf
#define INF64 9223372036854775807 //9.223372036854775807×10^{18}:64bit整数のinf
#define MOD 998244353 //問題による
//略記
#define F first
#define S second
//出力(空白区切りで昇順に)
#define coutALL(x) for(auto i=x.begin();i!=--x.end();i++)cout<<*i<<" ";cout<<*--x.end();
#define coutall(x) {for(int i=0;i<sizeof(x)/sizeof(x[0]);i++){cout<<x[i]<<"|";};};cout;
//aをbで割る時の繰上げ,繰り下げ
ll myceil(ll a,ll b){return (a+(b-1))/b;}
ll myfloor(ll a,ll b){return a/b;}

signed main(){
    ll a[10000],h,w,m=INF32,ans=0;
    cin>>h>>w;
    REP(i,h*w){
            cin>>a[i];
            if(a[i]<m){m=a[i];};
            ans+=a[i];
    };
    cout<<ans-h*w*m;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define bs binary_search
#define fi first
#define se second

#define int long long

using pii = pair<int,int>;
using ll = long long;
const int maxn = 2e5;
const int inf = 1e18;
const int mod = 1e9+7;

void open_io() { freopen("in","r",stdin); freopen("out","w",stdout); }
void fast_io() { ios::sync_with_stdio(0); cout.tie(0);  cin.tie(0);  }

int n,m;
int cost[maxn+5];
int ans[maxn+5];
int maxval[maxn+5];
vector<int> adj[maxn+5];
bool vis[maxn+5];

void dfs(int u)
{
	vis[u] = 1;
	ans[u] = maxval[u] = -inf;
	for(int v : adj[u])
	{
		if(not vis[v]) dfs(v);
		maxval[u] = max({maxval[u],cost[v],maxval[v]});
	}
	ans[u] = maxval[u] - cost[u];
}

int solve()
{
	int ret = -inf;
	for(int i=1;i<=n;i++)
		if(not vis[i])
			dfs(i);
	for(int i=1;i<=n;i++)
		ret = max(ret,ans[i]);
	return ret;
}

int32_t main ()
{
	//open_io();
	fast_io();
	cin >> n >> m;
	for(int i=1;i<=n;i++) cin >> cost[i];
	while(m--)
	{
		int a,b;
		cin >> a >> b;
		adj[a].pb(b);
	}
	cout << solve() << endl;
}

#include<bits/stdc++.h>
#define For(i,a,b) for(register int i=(a);i<=(b);++i)
#define pb push_back
#define pr pair<int,int>
#define fi first
#define se second
#define LL long long
#define mk(a,b) make_pair(a,b)
using namespace std;
int read(){
	char x=getchar(); int u=0,fg=0;
	while(!isdigit(x)){ if(x=='-') fg=1; x=getchar(); }
	while(isdigit(x)){ u=(u<<3)+(u<<1)+(x^48); x=getchar(); }
	return fg?-u:u;
}
const int maxx=1e5+10;
int n,m,sum=0,cnt=0,mx,ans=1e9;
int w[10],tmp[10],s[10],dis[10],v[maxx],l[maxx];
int p[10]={0,1,2,3,4,5,6,7,8};
pr a[maxx];
bool cmp(pr x,pr y){
	if(x.fi!=y.fi) return x.fi<y.fi;
	return x.se>y.se;
}
int iso(int x){
	x=lower_bound(v+1,v+cnt+1,x)-v-1;
	return l[x];
}
int solve(){
	s[0]=0;
	For(i,1,n){ tmp[i]=w[p[i]]; s[i]=s[i-1]+tmp[i];  dis[i]=0;}
	For(i,2,n) For(j,1,i-1){
		dis[i]=max(dis[i],dis[j]+iso(s[i]-s[j-1]));
		if(dis[i]>=ans) return dis[i];
	}
	return dis[n];
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif
	int u1,u2;
	n=read(); m=read();
	For(i,1,n){ w[i]=read(); mx=max(w[i],mx);}
	For(i,1,m){
		u1=read(); u2=read();
		a[i]=mk(u2,u1);
		if(u2<mx){ printf("-1\n"); return 0; }
	}
	sort(a+1,a+m+1,cmp);
	v[++cnt]=a[1].fi; l[cnt]=a[1].se;
	For(i,2,m)
		if(a[i].se>l[cnt]){
			v[++cnt]=a[i].fi;
			l[cnt]=a[i].se;
		}
	do{
		ans=min(ans,solve());
	}while(next_permutation(p+1,p+n+1));
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e2+5;
int x[N],y[N];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>x[i]>>y[i];
    }
    int flag=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            for(int k=j+1;k<=n;k++)
            {
               if(x[i]*y[j]-x[j]*y[i]+x[j]*y[k]-x[k]*y[j]+x[k]*y[i]-y[k]*x[i]==0)
                {
                    flag=1;
                    //cout<<x[i]<<" "<<y[i]<<" "<<x[j]<<" "<<y[j]<<" "<<x[k]<<" "<<y[k]<<endl;
                    break;
                }
            }
            if(flag==1)
                break;
        }
        if(flag==1)
            break;
    }
    if(flag==1)
        cout<<"Yes"<<endl;
    else
        cout<<"No"<<endl;

}

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

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

    vector<ll> x(N);
    vector<ll> y(N);
    rep(i, N) {
        cin >> x.at(i) >> y.at(i);
    }

    for (int i = 0; i < N - 2; ++i) {
        for (int j = i + 1; j < N - 1 ; ++j) {
            for (int k = j + 1; k < N ; ++k) {
               if ((x.at(k) - x.at(i)) * (y.at(j) - y.at(i)) == (x.at(j) - x.at(i)) * (y.at(k) - y.at(i))) {
                   cout << "Yes" << endl;
                   return 0;
                }
            }
        }
    }

    cout << "No" << endl;

}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
//const int MOD = 1000000007;
const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

struct io_setup{
    io_setup(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

template<int mod>
struct Mod_Int{
    int x;

    Mod_Int() : x(0) {}

    Mod_Int(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

    Mod_Int &operator += (const Mod_Int &p){
        if((x += p.x) >= mod) x -= mod;
        return *this;
    }

    Mod_Int &operator -= (const Mod_Int &p){
        if((x += mod - p.x) >= mod) x -= mod;
        return *this;
    }

    Mod_Int &operator *= (const Mod_Int &p){
        x = (int) (1LL * x * p.x % mod);
        return *this;
    }

    Mod_Int &operator /= (const Mod_Int &p){
        *this *= p.inverse();
        return *this;
    }

    Mod_Int &operator ++ () {return *this += Mod_Int(1);}

    Mod_Int operator ++ (int){
        Mod_Int tmp = *this;
        ++*this;
        return tmp;
    }

    Mod_Int &operator -- () {return *this -= Mod_Int(1);}

    Mod_Int operator -- (int){
        Mod_Int tmp = *this;
        --*this;
        return tmp;
    }

    Mod_Int operator - () const {return Mod_Int(-x);}

    Mod_Int operator + (const Mod_Int &p) const {return Mod_Int(*this) += p;}

    Mod_Int operator - (const Mod_Int &p) const {return Mod_Int(*this) -= p;}

    Mod_Int operator * (const Mod_Int &p) const {return Mod_Int(*this) *= p;}

    Mod_Int operator / (const Mod_Int &p) const {return Mod_Int(*this) /= p;}

    bool operator == (const Mod_Int &p) const {return x == p.x;}

    bool operator != (const Mod_Int &p) const {return x != p.x;}

    Mod_Int inverse() const{
        assert(*this != Mod_Int(0));
        return pow(mod-2);
    }

    Mod_Int pow(long long k) const{
        Mod_Int now = *this, ret = 1;
        for(; k > 0; k >>= 1, now *= now){
            if(k&1) ret *= now;
        }
        return ret;
    }

    friend ostream &operator << (ostream &os, const Mod_Int &p){
        return os << p.x;
    }

    friend istream &operator >> (istream &is, Mod_Int &p){
        long long a;
        is >> a;
        p = Mod_Int<mod>(a);
        return is;
    }
};

using mint = Mod_Int<MOD>;

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

    vector<int> a(N);
    rep(i, N){
        cin >> a[i]; a[i]--;
    }

    vector<int> used(N, false);
    int K = 0;

    rep(i, N){
        //if(used[i]) continue;

        int ptr = i;
        set<int> s;
        while(true){
            if(used[ptr]) break;
            used[ptr] = true;
            s.emplace(ptr);
            ptr = a[ptr];

            if(s.count(ptr)) {K++; break;}
        }
    }

    cout << mint(2).pow(K)-1 << '\n';
}

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define LL long long
#define int long long
#define ull unsigned long long
#define fi first
#define se second
#define pll pair<LL, LL>
#define pii pair<int, int>
#define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define SZ(x) ((int)(x.size()))
#define LC (id<<1)
#define RC (id<<1|1)

constexpr int N = 1e3+9;
constexpr int M = 1e9+7;
#ifdef int
constexpr int INF = 0x3f3f3f3f3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcfcfcfcfcf;
#else
constexpr int INF = 0x3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcf;
#endif

int n, k;
int t[N][N];
int ans;
int vis[N];
int cur;

void dfs(int i, int u) {
    if (i == n) {
        cur += t[u][1];
        ans += cur == k;
        cur -= t[u][1];
        return;
    }
    for (int j = 1; j <= 8; j++) {
        if (!vis[j]) {
            vis[j] = 1;
            cur += t[u][j];
            dfs(i+1, j);
            cur -= t[u][j];
            vis[j] = 0;
        }
    }
}

signed main() {
    fastio;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> t[i][j];
        }
    }
    vis[1] = 1;
    dfs(1, 1);
    cout << ans << "\n";
    return 0;
}


#include<bits/stdc++.h>
#define fs first
#define se second
#define pb push_back
#define cppio ios::sync_with_stdio(false);cin.tie(0)
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> VI;

const int maxn=2e5+5;
const ll inf=0x3f3f3f3f;
const ll mod=1e9+7;

int a[maxn],b[maxn];

int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",a+i);
	}
	for(int i=1;i<=n;i++){
		scanf("%d",b+i);
	}
	int l=0,r=1010;
	for(int i=1;i<=n;i++){
		l=max(a[i],l);
		r=min(b[i],r);
	}
	printf("%d\n",max(0,r-l+1));
	return 0;
}

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define l long
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define vi vector<int>
#define vll vector<ll>
#define pb push_back
#define ld long double
#define mp make_pair
#define pii pair<int,int>
#define mod 1000000007
#define rep(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(),v.end()
int main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
    ll n;
    cin>>n;
    ll a[n+1];
    ll sum=0;
    vector<ll> v(401,0);
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        a[i]=a[i]+200;
        v[a[i]]++;
    }
    for(int i=0;i<401;i++)
    {
        for(int j=i;j<401;j++)
        {
            sum+=(j-i)*(j-i)*v[i]*v[j];
        }

    }
    cout<<sum<<endl;
}


#include <iostream>
#include <algorithm>

using namespace std;

int a, b, n, ans, ans2;

struct A {
  int a, b, num;
}Ans[10001];

bool C (A a, A b) {
  return a.a < b.a;
}

bool D (A a, A b) {
  return a.b < b.b;
}

int main () {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> Ans[i].a >> Ans[i].b;
    Ans[i].num = i;
  }
  sort (Ans, Ans + n, C);
  ans = Ans[0].a;
  ans2 = Ans[1].a;
  int s = Ans[0].num;
  sort (Ans, Ans + n, D);
  if (s == Ans[0].num) {
    cout << min (ans + Ans[0].b, min (max (ans, Ans[1].b), max (ans2, Ans[0].b))) << endl;
  }
  else {
    cout << max(ans, Ans[0].b);
  }
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
const int N=1e6+5;
long long arr[N];

int solve()
{
    long long n, mn=LLONG_MAX;
    cin>>n;
    set<long long>st;
    for(int i=0;i<n;i++) {
        cin >> arr[i];
        st.insert(arr[i]);
        mn = min(mn, arr[i]);
    }
    long long ans=mn*n;
    for(auto j=st.begin();j!=st.end();j++)
    {
        long long curan=0;
        for(int i=0;i<n;i++)
        {
            if(arr[i]>=*j)
                curan+=*j;
            else
                curan = 0;
            ans=max(ans,curan);
        }

        curan=0;

    }
   cout<<ans;

    return 0;
}

int main()
{

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

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

 //   int t;
  //  cin>>t;
  //  while(t--)
        solve();

    return 0;
}


// code by lynmisakura. wish to be accepted!

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

#define rep(i,N) for(int i = 0;i < N;i++)
#define rng(i,a,b) for(int i = a;i < b;i++)

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

#define lmd(func) auto func = [&]

#define mp make_pair
#define pb push_back
#define eb emplace_back

#define debug(arr) cout << #arr << " = " << arr << '\n'
#define debug2(a,b) cout<<"["<<#a<<","<<#b<<"] = "<<"["<<a<<","<<b<<"]\n"
#define debug3(a,b,c) cout<<"["<<#a<<","<<#b<<","<<#c<<"] = "<<"["<<a<<","<<b<<","<<c<<"]\n"
template<class T> ostream &operator << (ostream& out, const vector<T>& arr) {
  cout << "{"; for (int i = 0; i < arr.size(); i++)cout << (!i ? "" : ", ") << arr[i]; cout << "}";
  return out;
}
template<class T> ostream &operator << (ostream& out, const vector<vector<T> >& arr) {
  cout << "{\n"; for (auto& vec : arr)cout << "  " << vec << ",\n"; cout << "}";
  return out;
}
template<class S,class T> ostream &operator << (ostream& out, const pair<S,T>& p){
  cout << "{" << p.first << "," << p.second << "}" << '\n';
  return out;
}
template<class T> istream &operator >> (istream& in, vector<T>& arr) {
  for (auto& i : arr)cin >> i; return in;
}
template<class S,class T> istream &operator >> (istream& in,pair<S,T>& p){
  cin >> p.first >> p.second; return in;
}

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using vpi = vector<pi>;
using vpl = vector<pl>;

/////////////////////////////////////////////////////////////////////////////

template<class T> void chmin(T& a,T b){ a = min(a,b);}
template<class T> void chmax(T& a,T b){ a = max(a,b);}

int n;
ll mx[200020],mn[200020];

ll dp[200020][2];

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

  cin >> n;
  rep(i,n+1){
    mx[i] = -INT_MAX;
    mn[i] = INT_MAX;
  }

  vector<int> cs;
  vector<pair<ll,ll>> a(n);
  rep(i,n){
    ll x,c;cin >> x >> c;
    c--;
    a[i] = mp(x,c);
    cs.pb(c);
  }

  sort(all(cs));
  cs.erase(unique(cs.begin(),cs.end()),cs.end());

  rep(i,n){
    auto[x,c] = a[i];
    c = lower_bound(all(cs),c) - cs.begin();
    chmax(mx[c],x);
    chmin(mn[c],x);
  }

  rep(i,200020)rep(j,2) dp[i][j] = (1LL << 55);

  int i = 0;

  dp[i][0] = abs(mx[i]) + mx[i] - mn[i];
  dp[i][1] = abs(mn[i]) + mx[i] - mn[i];

//  debug2(mn[i],mx[i]);

  i++;
  n = cs.size();
  for(;i < n;i++){

    ll l = mn[i-1],r = mx[i-1];
    ll nl = mn[i],nr = mx[i];

    chmin(dp[i][0] , dp[i-1][0] + abs(l - nr) + nr - nl);
    chmin(dp[i][0] , dp[i-1][1] + abs(r - nr) + nr - nl);
    chmin(dp[i][1] , dp[i-1][0] + abs(l - nl) + nr - nl);
    chmin(dp[i][1] , dp[i-1][1] + abs(r - nl) + nr - nl);
  }

  cout << min(dp[n-1][0] + abs(mn[n-1]),dp[n-1][1] + abs(mx[n-1])) << '\n';

}

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long n,x,a[110],f[110][110][110],ans=1145141919810233454;
int main()
{
	scanf("%lld %lld",&n,&x);
	for(int i=1;i<=n;++i)	scanf("%lld",&a[i]);
	for(int s=1;s<=n;++s)
	{
		memset(f,-0x3f,sizeof(f));
		f[0][0][0]=0;
		for(int i=1;i<=n;++i)
		{
			for(int j=1;j<=min(s,i);++j)
			{
				for(int k=0;k<n;++k)	f[i][j][k]=max(f[i-1][j][k],f[i-1][j-1][((k-a[i])%s+s)%s]+a[i]);
			}
		}
		if(f[n][s][x%s]>=0)	ans=min(ans,(x-f[n][s][x%s])/s);
	}
	printf("%lld\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
  string S;
  int64_t M,z=0;
  cin>>S>>M;
  int64_t N=S.size(),K=*max_element(S.begin(),S.end())-'0';
  if(N<2){
    cout<<(M<K?0:1);
    return 0;
  }
  int64_t ok=0,ng=M+10;
  while(ng-ok>1){
    __int128 m=(ok+ng)/2,sum=0,temp=1,l=0;
    for(int j=N-1;j>=0;temp*=m,j--){
      sum+=(S[j]-'0')*temp;
      if(temp>M){
        l=1;
        break;
      }
    }
    if(l||sum>M)ng=m;
    else ok=m;
  }
  cout<<max(z,ok-K);
}

#pragma GCC optimize ("O2")
#pragma GCC target ("avx")
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
//#include<atcoder/all>
//using namespace atcoder;

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

char cn[1200010];
char* ci = cn, * di = cn, ct;
char tc[200002], *d1 = tc + 100000, *d2 = tc + 200000;
const char d[2] = "0";

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

	rep(i, 100000) tc[i] = '0';
	rep(i, 100000) d1[i] = '1';
	d2[0] = '0';
	d2[1] = '\n';
	fread(cn, 1, 1200010, stdin);
	int T = 0;
	while ((ct = *ci++) >= '0') T = T * 10 + ct - '0';

	rep(t, T) {
		int N = 0;
		while ((ct = *ci++) >= '0') N = N * 10 + ct - '0';
		memcpy(di, d1 - N, N);
		di += N;
		memcpy(di, d2 - N, N + 2);
		di += N + 2;
		ci += 6 * N + 3;
	}
	fwrite(cn, 1, di - cn, stdout);

	Would you please return 0;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
#define INF ((1<<30)-1)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MOD2 = 998244353;

ll dp[101][1000010];

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

    dp[0][0] = 1;

    for(int i = 1; i <= n; i++){
        ll tmp[i+1] = {};
        rep(j, 1000010){
            int x = j % i;
            tmp[x] += dp[i-1][j];
            if (j - i*(k+1) >= 0) {
                tmp[x] += m - dp[i-1][j-i*(k+1)];
                tmp[x] %= m;
            }
            dp[i][j] = tmp[x];
            dp[i][j] %= m;
        }
    }

    for(int i = 1; i <= n; i++){
        ll res = 0;
        rep(j, 1000010) {
            res += (dp[i-1][j] * dp[n-i][j])%m;
            res %= m;
        }
        res *= k + 1;
        res += m - 1;
        res %= m;
        cout << res << endl;
    }

    return 0;
}

#include <bits/stdc++.h>
#define LL long long
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define all_of(v) (v).begin(), (v).end()
#define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define fi first
#define se second
const int MAXN = 101;
//const LL INF = (LL) 1e9 + 8763;
//const LL MOD = (LL) 1e9 + 7;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, mod, k, ans[MAXN], dp[MAXN][MAXN*MAXN*MAXN];

inline int add(int x, int y) {
	int sum = x + y;
	if (sum >= mod) sum -= mod;
	return sum;
}

void solve() {
	cin >> n >> k >> mod;

	int bound = n * (n + 1) / 2 * k;
	dp[0][0] = 1;
	for (int i = 1; i <= n; i++) {
		for (int r = 0; r < i; r++) {
			int sum = 0;
			for (int j = r; j <= bound; j += i) {
				sum = add(sum, dp[i - 1][j]);
				if (j - (k + 1) * i >= 0) {
					sum = add(sum, -dp[i - 1][j - (k + 1) * i] + mod);
				}
				dp[i][j] = sum;
			}
		}
	}

	for (int i = 1; i <= n; i++) {
		int a = i - 1, b = n - i;
		for (int j = 0; j <= bound; j++) {
//			cout << "At " << i << ' ' << j << " -> " << dp[a][j] << ' ' << dp[b][j] << '\n';
			if (dp[a][j] == 0 || dp[b][j] == 0) continue;
			ans[i] = (ans[i] + (LL) dp[a][j] * dp[b][j] % mod * (k + 1) % mod) % mod;
		}
		ans[i] = (ans[i] - 1 + mod) % mod;
	}

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

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

	int tc = 1;
//	cin >> tc;
	for (int i = 1; i <= tc; i++) {
		solve();
	}

	return 0;
}


#include <bits/stdc++.h>
//#include <atcoder/all>

typedef long long ll;
using namespace std;
//using namespace atcoder;

#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) for(ll i = 0; i < (ll)n; i++)
#define _reps(i, s, n) for(ll i = (ll)(s); i < (ll)n; i++)
#define REP(...) _overload(__VA_ARGS__, _reps, _rep,)(__VA_ARGS__)
#define all(v) (v).begin(),(v).end()
#define UNIQUE(v) (v).erase( unique( all(v) ), (v).end() );
#define MOD 1000000007

ll powll(ll x, ll n){
	ll r = 1;
	while( n > 0 ){
		if( n & 1 )	r = x * r;
		x = x * x;
		n >>= 1;
	}
	return r;
}

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

	REP(i, 1, 38) REP(j, 1, 26){
		if( powll(3, i) + powll(5, j) == n ){
			cout << i << " " << j << endl;
			return 0;
		}
	}

	cout << -1 << endl;

	return 0;
}

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<ctime>
#include<map>
#define ll long long
#define MAXN 200005
#define uns unsigned
#define INF 0x3f3f3f3f
#define lowbit(x) ((x)&(-(x)))
using namespace std;
inline ll read(){
	ll x=0;bool f=1;char s=getchar();
	while((s<'0'||s>'9')&&s>0){if(s=='-')f^=1;s=getchar();}
	while(s>='0'&&s<='9')x=(x<<1)+(x<<3)+s-'0',s=getchar();
	return f?x:-x;
}
int n;
ll sum,mx,a[MAXN],ans;
int main()
{
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=n;i++){
		mx=max(mx,a[i]);
		sum+=a[i],ans+=sum;
		printf("%lld\n",ans+mx*i);
	}
	return 0;
}


#include <bits/stdc++.h>
#include <bits/stdint-intn.h>

#include <algorithm>
#include <cstdint>

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    string s;
    cin >> s;
    if (s == "AND") {
      A[i] = 0;
    } else {
      A[i] = 1;
    }
  }

  int64_t count0 = 1;
  int64_t count1 = 1;
  for (int i = 0; i < N; i++) {
    int64_t next0 = 0;
    int64_t next1 = 0;
    if (A[i]) {
      // OR
      // 0 | 0 = 0
      // 0 | 1 = 1
      // 1 | 0 = 1
      // 1 | 1 = 1
      next0 = count0;
      next1 = count0 + count1 * 2;
    } else {
      // AND
      // 0 & 0 = 0
      // 0 & 1 = 0
      // 1 & 0 = 0
      // 1 & 1 = 1
      next0 = count0 * 2 + count1;
      next1 = count1;
    }
    count0 = next0;
    count1 = next1;
  }

  cout << count1 << endl;

  return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define vec vector<ll>
#define vecp vector<pair<ll,ll> >
#define mapp map<ll,ll>
#define mapit map<ll,ll>::iterator
#define mapr map<pair<ll,ll>,ll>
#define maprit map<pair<ll,ll>,ll>::iterator
#define fs first
#define sc second
#define bg begin()
#define en end()
#define pb push_back
#define fin(x,y) for(x=0;x<y;x++)
#define fin1(x,y) for(x=1;x<y;x++)
#define mod1 1000000007
#define INF 1000000000000000000
#define twodp vector<vec >dp(n+1,vec(k+1,0))
#define all(x) x.bg,x.en
#define rel(x) x.rbegin(),x.rend()
using namespace std;
ll gcd(ll a,ll b)
{
  if(a==0 || b==0)
    return a+b;
  else
    return __gcd(a,b);
}
int main()
{
     ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
  ll a,b;cin>>a>>b;
  ll i,j,k,l;ll ans=0;
  fin1(i,a+1)
  {
    fin1(j,b+1)
    {
      l=j+100*i;
      ans+=l;
    }
  }
  cout<<ans;
  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>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef pair<int,int>P;

const int MOD=1000000007;
//~ const int MOD=998244353;
const int INF=0x3f3f3f3f;
const ll INFL=0x3f3f3f3f3f3f3f3f;

int c[300000];

unordered_map<int,int>mp[300000];
vector<int>v[300000];

int par[300000];

void merge(int x,int y){//x<-y
	if(v[x].size()<v[y].size())swap(x,y);
	for(auto&p:mp[y]){
		mp[x][p.first]+=p.second;
	}
	for(int i:v[y]){
		v[x].push_back(i);
		par[i]=x;
	}
}

int main(){
	int n,q;cin>>n>>q;
	rep(i,n)scanf("%d",&c[i]),c[i]--;
	rep(i,n){
		par[i]=i;
		v[i].push_back(i);
		mp[i][c[i]]++;
	}
	rep(i,q){
		int ty;scanf("%d",&ty);
		int a,b;scanf("%d%d",&a,&b);a--;b--;
		if(ty==1){
			a=par[a];b=par[b];
			if(a!=b)merge(a,b);
		}
		else{
			a=par[a];
			if(!mp[a].count(b))puts("0");
			else printf("%d\n",mp[a][b]);
		}
	}
}

#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include<bits/stdc++.h>
using namespace std;
#define rep(i,s,t) for(ll i = (ll)(s); i < (ll)(t); i++)
#define rrep(i,s,t) for(ll i = (ll)(s-1);(ll)(t) <= i; i--)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> Pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
constexpr ll INF = numeric_limits<ll>::max()/4;
constexpr ll n_max = 2e5+10;
#define int ll
const long double pi = 3.14159265358979323846;

template <typename A, typename B>
string to_string(pair<A, B> p);
string to_string(const string &s) {return '"' + s + '"';}
string to_string(const char *c) {return to_string((string) c);}
string to_string(bool b) {return (b ? "true" : "false");}
template <size_t N>
string to_string(bitset<N> v){
    string res = "";
    for(size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]);
    return res;
}
template <typename A>
string to_string(A v) {
    bool first = true;
    string res = "{";
    for(const auto &x : v) {
        if(!first) res += ", ";
        first = false;
        res += to_string(x);
    }
    res += "}";
    return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p){return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}

void debug_out() {cerr << endl;}
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << to_string(H);
    debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

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

signed main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    ll n,m; cin >> n >> m;
    vector<ll> w(n);
    for(int i = 0; i < n; i++) cin >> w[i];
    vector<Pll> lv(m);
    for(auto &[l, v] : lv){
        cin >> l >> v;
    }

    rep(i,0,n){
        bool ok = true;
        for (auto [l, v] : lv) {
            ok &= v >= w[i];
        }
        if(!ok){
            cout << -1 << endl;
            return 0;
        }
    }

    map<ll, ll> mp;
    rep(i,0,1LL << n){
        ll tmp = 0;
        rep(j,0,n){
            if((i >> j) & 1){
                tmp += w[j];
            }
        }
        if (mp.count(tmp)) continue;
        ll len = 0;
        rep(j,0,m){
            auto [l, v] = lv[j];
            if (tmp > v) chmax(len, l);
        }
        mp[tmp] = len;
    }

    vector<ll> v(n);
    iota(all(v), 0);
    ll ans = INF;
    do {
        vector<ll> pos(n);
        rep(i,0,n){
            ll tmp = w[v[i]];
            ll tpos = pos[i];
            rep(j, i + 1, n) {
                tmp += w[v[j]];
                ll len = mp[tmp] + tpos;
                if(chmax(pos[j], len)){
                    rep(k, j, n) chmax(pos[k], pos[k - 1]);
                }
            }
        }
        // debug(v, pos);
        chmin(ans, pos.back() - pos[0]);
    } while (next_permutation(all(v)));

    cout << ans << endl;
}


//#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <algorithm>
#include <climits>
#include <functional>
#include <cstring>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <complex>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define itn int
#define nit int
#define ll long long
#define ms multiset
#define F(i,a,b) for(register int i=a,i##end=b;i<=i##end;++i)
#define UF(i,a,b) for(register int i=a,i##end=b;i>=i##end;--i)
#define re register
#define ri re int
#define il inline
#define node pair<int,int>
#define x first
#define y second
#define cp complex<double>
//#pra gma G CC opti mize(3)
using namespace std;
using std::bitset;
//using namespace __gnu_pbds;
const double Pi=acos(-1);
template<class T>
inline T read(T &x) {
	x=0;
	bool fu=0;
	char ch=0;
	while(ch>'9'||ch<'0') {
		ch=getchar();
		if(ch=='-')fu=1;
	}
	while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
	if(fu)x=-x;
}
inline int read(){
	int x=0;
	bool fu=0;
	char ch=0;
	while(ch>'9'||ch<'0') {
		ch=getchar();
		if(ch=='-')fu=1;
	}
	while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
	return fu?-x:x;
}
int n,m,l=1,r=1,dis[2002][2002];
char c[2002][2002];
bool fin[100002],vis[2002][2002];
node s,t,q[4000002];
void bfs(){
	q[r]=s;vis[s.x][s.y]=1;dis[s.x][s.y]=0;
	ri i,j;
	while(l<=r){
		i=q[l].x,j=q[l].y;
		l++;
		if(!vis[i+1][j]&&c[i+1][j]!='#'&&i<n){vis[i+1][j]=1;
			q[++r]=make_pair(i+1,j);
			dis[i+1][j]=dis[i][j]+1;
		}if(!vis[i][j+1]&&c[i][j+1]!='#'&&j<m){vis[i][j+1]=1;
			q[++r]=make_pair(i,j+1);
			dis[i][j+1]=dis[i][j]+1;
		}if(!vis[i-1][j]&&c[i-1][j]!='#'&&i>1){vis[i-1][j]=1;
			q[++r]=make_pair(i-1,j);
			dis[i-1][j]=dis[i][j]+1;
		}if(!vis[i][j-1]&&c[i][j-1]!='#'&&j>1){vis[i][j-1]=1;
			q[++r]=make_pair(i,j-1);
			dis[i][j-1]=dis[i][j]+1;
		}if(c[i][j]>='a'&&c[i][j]<='z'){
			if(!fin[c[i][j]]){
				fin[c[i][j]]=1;
				F(ii,1,n)F(jj,1,m){
					if(c[ii][jj]==c[i][j]&&!vis[ii][jj]){
						vis[ii][jj]=1;
			q[++r]=make_pair(ii,jj);
			dis[ii][jj]=dis[i][j]+1;
					}
				}
			}
		}
	}
}
int main(){memset(dis,0x3f,sizeof(dis));
    cin>>n>>m;
    F(i,1,n)scanf("%s",c[i]+1);
    F(i,1,n){
    	F(j,1,m){
    		if(c[i][j]=='S'){
    			s=make_pair(i,j);
			}else if(c[i][j]=='G'){
				t=make_pair(i,j);
			}
		}
	}bfs();
	cout<<(dis[t.x][t.y]<1e9?dis[t.x][t.y]:-1);
    return 0;
}

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

string refridgerator(string s, vector<char> &p){
    for(char &c : s) c = p[c-'0'];
    return s;
}

signed main(){
    string s1,s2,s3; cin >> s1 >> s2 >> s3;
    set<char> cs;
    for(char c : s1) cs.insert(c);
    for(char c : s2) cs.insert(c);
    for(char c : s3) cs.insert(c);
    if(cs.size() > 10){
        cout << "UNSOLVABLE"; return 0;
    }
    vector<char> css(cs.begin(), cs.end());
    for(char &c : s1) c = '0'+(find(css.begin(), css.end(), c) - css.begin());
    for(char &c : s2) c = '0'+(find(css.begin(), css.end(), c) - css.begin());
    for(char &c : s3) c = '0'+(find(css.begin(), css.end(), c) - css.begin());
    vector<char> p(10); for(int i = 0; i < 10; i ++) p[i] = '0'+i;
    do{
        string t1 = refridgerator(s1,p);
        string t2 = refridgerator(s2,p);
        string t3 = refridgerator(s3,p);
        if(t1[0] == '0' or t2[0] == '0' or t3[0] == '0') continue;
        if(stoll(t1) + stoll(t2) == stoll(t3)){
            cout << t1 << "\n" << t2 << "\n" << t3; return 0;
        }
    }while(next_permutation(p.begin(),p.end()));
    cout << "UNSOLVABLE\n";
}


#include <bits/stdc++.h>
//#include <atcoder/all>

using namespace std;
//using namespace atcoder;

using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using ld = long double;
using vld = vector<ld>;
using vb = vector<bool>;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define dbg(x) true
#endif

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

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

template <class T> ostream& operator<<(ostream& s, const vector<T>& a) {
    for(auto i : a) s << i << ' ';
    return s;
}

constexpr int INF = 1 << 30;
constexpr ll INFL = 1LL << 62;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);

struct mint {
    static const long long mod = 998244353;
    long long x;
    mint(long long x = 0) : x((x % mod + mod) % mod) {}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint& a) {if ((x += a.x) >= mod) x -= mod; return *this;}
    mint& operator-=(const mint& a) {if ((x += mod - a.x) >= mod) x -= mod; return *this;}
    mint& operator*=(const mint& a) {(x *= a.x) %= mod; return *this;}

    friend const mint operator+(const mint& a, const mint& b) { mint ret = a; return ret += b; }
    friend const mint operator-(const mint& a, const mint& b) { mint ret = a; return ret -= b; }
    friend const mint operator*(const mint& a, const mint& b) { mint ret = a; return ret *= b; }
    friend ostream& operator<<(ostream& s, const mint& a) { return s << a.x; }

    mint pow(long long t) const {
        if (!t) return 1;
        mint a = pow(t >> 1);
        a *= a;
        if (t & 1) a *= *this;
        return a;
    }

    // for prime mod
    mint inv() const { return pow(mod - 2);}
    mint& operator/=(const mint a) { return (*this) *= a.inv();}
    friend const mint operator/(const mint& a, const mint& b) { mint ret = a; return ret /= b; }
};

void solve() {
    ll n;
    cin >> n;
    vll a(n);
    rep(i, n) cin >> a[i];
    sort(a.begin(), a.end());

    mint base = 1;
    vector<mint> b(n);
    b[0] = 1;
    for(int i = 1; i < n; ++i) {
        b[i] = base;
        base *= 2;
    }
    rep(i, n) b[i] *= a[i];
    vector<mint> bs(n+1);    // [l,r)のb[i]の和はbs[r]-bs[l]
    rep(i, n) bs[i+1] = bs[i] + b[i];

    mint ans = 0;
    base = 1;
    rep(i, n) {
        ans += ((bs[n] - bs[i+1]) * base + a[i]) * a[i];
        base /= 2;
    }
    cout << ans << endl;
    return;
}

int main() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
}

/**
 *  created: 28/03/2021, 15:04:37
**/

#include <bits/stdc++.h>

using namespace std;

const int max_n = 200222, mod = 998244353;

int n, ans, a[max_n];

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

int mul(int x, int y) {
    return 1LL * x * y % mod;
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
    }
    sort(a, a + n);
    int sum = 0;
    for (int i = 0; i < n; ++i) {
        inc(ans, mul(sum + a[i], a[i]));
        sum = mul(sum, 2);
        inc(sum, a[i]);
    }
    printf("%d\n", ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(i=0;i<n;i++)
#define Rep(i,x,n) for(i=x;i<=n;i++)
#define all(v) v.begin(),v.end()
#define foreach(c,itr) for(__typeof(c.begin()) itr=c.begin();itr!=c.end();itr++)
#define p_b push_back
#define pii pair<int,int>
#define fr first
#define sc second
#define m_p make_pair
#define zero(a) memset(a,0,sizeof(a))
#define setp setprecision
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f;
const ll mod=1000000007;
const ld eps=1e-14;
int i,j;

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

	string s;
	cin>>s;
	cout<<s.substr(1,s.size()-1)<<s[0]<<endl;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
using LL=long long;
using P=pair<int,int>;
using T=tuple<LL,int,int,int>;
constexpr LL mod=1000000007;
constexpr LL inf=1e18;
constexpr double eps=1e-13;
constexpr double pi=3.141592653589;
int in(){
    int x;
    scanf("%d",&x);
    return x;
}
struct segtree{
    int N=262144;
    vector<int>data;
    segtree(){
        data.assign(N*2,0/*inf*/);
    }
    segtree(int n){
        N=n;
        data.assign(N*2,0/*inf*/);
    }
    void upd(int l,int r,int n,int nl,int nr,LL x){
        if(l<=nl && nr<=r){
            data[n]+=x;
            return;
        }
        if(r<=nl || nr<=l)return;
        int mid=(nl+nr)/2;
        upd(l,r,n*2,nl,mid,x),upd(l,r,n*2+1,mid,nr,x);
    }
    void update(int l,int r,LL x){upd(l,r,1,0,N,x);}
    LL get(int x){
        LL res=0;
        for(int i=N+x;i;i/=2)res+=data[i];
        return res;
    }
};
int main(){
    int n=in();
    segtree s;
    LL ans=0;
    map<int,vector<int>>m;
    for(int i=0;i<n;i++)m[in()+i].emplace_back(i);
    for(auto &p:m)reverse(p.second.begin(),p.second.end());
    for(int i=0;i<n;i++){
        int x=in()+i;
        if(m[x].size()==0){
            cout<<-1<<endl;
            return 0;
        }
        int pos=m[x].back();
        m[x].pop_back();
        ans+=pos+s.get(pos)-i;
        s.update(0,pos,1);
    }
    cout<<ans<<endl;
}


#include <iostream>
#include <string>

using namespace std;

using Int = long long int;

Int combi(int A, int B)
{ // (A + B)! / (A! B!);
  if (A > B) return combi(B, A);
  if (A == 0) return 1;
  return combi(A - 1, B) * (A + B) / A;
}

void f(const string &S, int A, int B, Int &K)
{
  if (K <= 0) return;
  Int C = combi(A, B);
  if (K > C) {
    K -= C;
    return;
  }
  if (A == 0 && B == 0) {
    --K;
    cout << S << endl;
    return;
  }
  if (A > 0) f(S + 'a', A - 1, B, K);
  if (B > 0) f(S + 'b', A, B - 1, K);
}

int main()
{
  int A, B;
  long long int K;
  cin >> A >> B >> K;
  string S;

  f(S, A, B, K);
}

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

void comb(vector<vector <long long int> > &v){
  for(int i = 0;i <v.size(); i++){
    v[i][0]=1;
    v[i][i]=1;
  }
  for(int k = 1;k <v.size();k++){
    for(int j = 1;j<k;j++){
      v[k][j]=(v[k-1][j-1]+v[k-1][j]);
    }
  }
}

int main() {
  int a, b;
  ll k;
  cin >> a >> b >> k;
  vector<vector<long long int> > v(60+1,vector<long long int>(60+1,0));
  comb(v);

  string ans = "";

  int sum = a+b;
  rep(i,sum){
    if(v[a+b-1][a-1] >= k){
      if(a > 0){
        ans += 'a';
        a--;
      }else{
        ans += 'b';
        b--;
      }

    }else{
      k -= v[a+b-1][a-1];
      ans += 'b';
      b--;
    }
  }
  cout << ans << endl;

  return 0;
}


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

int main(){
  int n;
  cin >> n;
  int c;
  vector<vector<int>> a(n, vector<int>(2));
  for(int i = 0; i < 2*n; i++){
    cin >> a.at(i % n).at(i / n);
  }
  for(int i = 0; i < n; i++){
    c += a.at(i).at(0) * a.at(i).at(1);
  }
  if ( c == 0){
    cout << "Yes" << endl;
  }
  else{
    puts("No");
  }
}

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

void solve() {
    ll n;
    cin >> n;

    vector<ll> a(n), b(n);
    for (int i = 0; i < n; i++)
        cin >> a[i];

    for (int i = 0; i < n; i++)
        cin >> b[i];

    ll ans = 0;
    for (int i = 0; i < n; i++)
        ans += a[i]*b[i];

    if (ans == 0) cout << "Yes" << endl;
    else cout << "No" << endl;
}

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


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

int main() {
  int N,M,Q;
  cin >> N >> M >> Q;
  vector<pair<int,int>> a(N);
  rep(i,N) cin >> a.at(i).second >> a.at(i).first;
  vector<int> b(M),k(M);
  rep(i,M){
    cin >> b.at(i);
    k.at(i) = b.at(i);
  }
  vector<pair<int,int>> c(Q);
  rep(i,Q) cin >> c.at(i).first >> c.at(i).second;

  sort(a.rbegin(),a.rend());
  rep(i,Q){
    for(int j=c.at(i).first; j<=c.at(i).second; j++) k.at(j-1) = 0;
    int ans=0;

    sort(k.begin(),k.end());
    for(int j=0; j<N; j++){
      rep(l,M){
        if(a.at(j).second <= k.at(l)){
          ans += a.at(j).first;
          k.at(l) = 0;
          break;
        }
        else continue;
      }
    }
    cout << ans << endl;
    rep(j,M) k.at(j) = b.at(j);

  }
}

#include <bits/stdc++.h>

typedef long long ll;
typedef long double ld;
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define PRECISION cout << fixed << setprecision(20);

using namespace std;

void solve() {
    int n, m, q;
    cin >> n >> m >> q;

    vector<array<int, 2>> arr(n);
    for (auto &a: arr) cin >> a[0] >> a[1];

    sort(arr.begin(), arr.end(), [&](array<int, 2> a, array<int, 2> b) {
        return pair<int, int>{a[1], -a[0]} > pair<int, int>{b[1], -b[0]};
    });

    vector<int> x(m);
    for (int i = 0; i < m; i++) {
        cin >> x[i];
    }

    while (q--) {
        int l, r;
        cin >> l >> r;
        l--; r--;
        vector<int> ok(m, 1);
        int ans = 0;
        for (auto &a: arr) {
            int ind = -1;
            for (int i = 0; i < m; i++) {
                if (i >= l && i <= r) continue;
                if (x[i] < a[0]) continue;
                if (!ok[i]) continue;
                if (ind == -1 || x[i] < x[ind]) ind = i;
            }
            if (ind != -1) {
                ok[ind] = 0;
                ans += a[1];
            }
        }
        cout << ans << '\n';
    }

}

int main() {
    FASTIO;
    PRECISION;

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

    for (int i = 1; i <= t; i++) {
        solve();
    }
}


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

int main(){
    ll n, ans = 0;
    cin >> n;
    vector<ll>a(n);
    rep(i,n) cin >> a[i];
    ans = (n-1)*(1+n-1)/2;
    sort(a.begin(),a.end());
    bool check = false;
    ll onaji_num = 0;
    ll onaji_sum;
    rep(i,n){
        if(a[i] == a[i+1] && !check){
            check = true;
            onaji_num = 1;
            onaji_sum = 1;
        }else if(a[i] == a[i+1] && check){
            onaji_num++;
            onaji_sum = (onaji_num)*(1+onaji_num)/2;
        }else if(a[i] != a[i+1] && check){
            ans -= onaji_sum;
            onaji_num = 0;
            check = false;
        }else{
            if(check){
                ans -= 1;
                onaji_num = 0;
                check = false;
            }
            continue;
        }
    }
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long n;
    cin>>n;
    long long a[n];
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    long long sum=0;
    map<int,int>m;
    for(int i=0;i<n;i++)
    {
        sum+=(n-i-1-m[a[i]]);
        m[a[i]]++;
    }
    cout<<sum;

    return 0;
}


#include<bits/stdc++.h>
#include<bits/stdc++.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define lli long long int
using namespace std;
int main()
{
     string s;
     cin>>s;
     int flag=0,count=0;
     for(int i=0; i<=s.length(); i+=2)
     {
            if(s[i]>=97 && s[i]<=122)
            {
                 count++;
            }
     }
     for(int i=1; i<s.length(); i+=2)
     {
          if(s[i]>=65 && s[i]<=90)
          {
               flag++;
          }
     }
    // cout<<count<<" "<<flag<<" "<<s.length()<<endl;
     if(count+flag==s.length())
     {
          cout<<"Yes";
     }
     else
     {
        cout<<"No";
     }

    return 0;
}

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

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

    if((N * 108) / 100 < 206) cout << "Yay!" << endl;
    else if((N * 108) / 100 == 206) cout << "so-so" << endl;
    else cout << ":(" << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define CEIL(a, b) ((a - 1) / b + 1)

void solve() {
    string S;
    cin >> S;
    reverse(ALL(S));
    for (auto &x : S) {
        if (x == '6') {
            x = '9';
        } else if (x == '9') {
            x = '6';
        }
    }
    cout << S << endl;
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    solve();
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define rep1(i,n) for(int i=1;i<=(int)n;i++)
#define sp(n) cout << fixed << setprecision(n)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
typedef long long ll;
using namespace std;
int main(void){
    ll n;cin>>n;
    string s;cin>>s;
    vector<ll> a(n+10),c(n+10,100);
    rep(i,n+1) cin>>a[i];
    ll d=1e9;
    rep(i,n)chmin(d,abs(a[i+1]-a[i]));
    cout<<d<<endl;
    rep(i,d){
        vector<ll> b;
        rep(j,n+1)cout<<(a[j]+i)/d<<" ";
        cout<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a); i < (b); i++)
#define drep(i,b,a) for(int i = (b)-1; i >= (a); i--)
#define bit(n) (1LL << (n))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define SORT(v) sort(v.begin(),v.end());
#define RSORT(v) sort(v.rbegin(),v.rend());
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<vd> vvd;
typedef vector<vb> vvb;
typedef vector<vc> vvc;
const int inf = 1 << 30; const ll infl = 1LL << 60; const double PI = acos(-1.0);
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> inline bool  odd(T x) { return x & 1; }
template<class T> inline bool even(T x) { return !(x & 1); }
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
    std::fill( (T*)array, (T*)(array+N), val );
}
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << endl; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ for(auto it : P) { s << "<" << it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s; }
//----------------------------------------------------------------------------------------------

int main()
{
    ll n;
    cin >> n;
    __int128_t l = 0, r = 4000000000;
    while(r-l>1) {
        __int128_t mid = (l+r)/2;
        if(mid*(mid+1)/2 <= (__int128_t)(n+1)) l = mid;
        else r = mid;
    }
    cout << (ll)(n - l + 1) << endl;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;
const long LINF = 1L << 62;
const int IINF = 1 << 30;
#define rep(i, s, n) for (long i = s; i < (long)(n); i++)
template <class T>
using Graph = std::vector<std::vector<T>>;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
bool is_in(int x, int y, int xlim, int ylim)
{
    return (x >= 0 && x < xlim && y >= 0 && y < ylim);
}
int next_combination(int sub)
{
    int x = sub & -sub, y = sub + x;
    return (((sub & ~y) / x) >> 1) | y;
}
using ll = long;
constexpr ll MOD = 1000000007;

int main()
{
    std::deque<char> deq;
    string s;
    cin >> s;
    bool pos = true;
    for (ll i = 0; i < s.size(); i++) {
        char c = s[i];
        if (c == 'R') {
            pos = !pos;
        } else {
            if (pos) {
                deq.push_back(c);
            } else {
                deq.push_front(c);
            }
        }
    }
    if (!pos) {
        reverse(deq.begin(), deq.end());
    }
    std::string ans;
    for (char& c : deq) {
        if (ans.size() > 0 && ans.back() == c) {
            ans.pop_back();
        } else {
            ans.push_back(c);
        }
    }
    cout << ans << endl;
}


#line 2 "/home/defineprogram/Desktop/Library/template/template.cpp"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(ll i=0;i<n;i++)
#define REP(i,n) for(ll i=1;i<n;i++)
#define rev(i,n) for(ll i=n-1;i>=0;i--)
#define all(v) v.begin(),v.end()
#define P pair<ll,ll>
#define len(s) (ll)s.size()

template<class T,class U> inline bool chmin(T &a, U b){
	if(a>b){a=b;return true;}
	return false;
}
template<class T,class U> inline bool chmax(T &a, U b){
	if(a<b){a=b;return true;}
	return false;
}
constexpr ll inf = 3e18;
#line 2 "main.cpp"

int main(){
	cin.tie(0);ios::sync_with_stdio(false);
	ll N;cin>>N;
	if(N==1){
		cout<<"1\n";return 0;
	}
	ll ok=2,ng=min((ll)2e9,N);
	while(ng-ok>1){
		ll mid=(ok+ng)/2;
		ll s=(2+mid)*(mid-1)/2;
		if(s<=N)ok=mid;
		else ng=mid;
	}
	cout<<N-ok+1<<endl;
}

#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include<cstdio>
#include<functional>
#include <bitset>
#include <iomanip>
#include <cctype>
#include <list>
#include <cassert>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for(ll i = n; i >= 0; i--)
#define ll long long
#define repi(i,a,b) for(ll i=a;i<b;++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

using namespace std;
template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
const ll INF = 1000000000000000000;
const ll MOD = 1e9 + 7;
using vvv = vector<vector<vector<ll>>>;
using vv = vector<vector<ll>>;
using vec = vector<ll>;
using P = pair<ll, ll>;

int main(void){
    ll n; cin >> n;
    ll ans = n * 1.08;
    if(ans < 206) cout << "Yay!" << endl;
    else if(ans == 206) cout << "so-so" << endl;
    else cout << ":(" << endl;
}


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

#define int long long
#define pb push_back
#define S second
#define F first
#define f(i,n) for(int i=0;i<n;i++)
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>

const int MOD = 1e9+7;

int mod_pow(int a,int b,int M = MOD)
{
    int res = 1;

    while(b)
    {
        if(b&1) res=(res*a)%M;
        a=(a*a)%M;
        b>>=1;
    }

    return res;
}

int go(int x)
{
    int res = (x);
    res=(res*(x+1));
    res/=2;
    res%=MOD;
    return res;
}

int go2(int x)
{
    int res = (x*(x+1));
    res/=2;
    res%=MOD;
    res=(res*res)%MOD;
    return res;
}

void solve()
{
   int n,a,b;
    cin >> n >> a >> b;

   if(a + b > n)
   {
       cout << 0 << '\n';
       return;
   }

   int res = 0;

   int temp = (n-a+1);
   temp = (temp*(n-b+1))%MOD;
   temp = (temp*go(n-a-b+1))%MOD;
   res = temp;

   res = (res - go2(n-a-b+1) + MOD)%MOD;

   res = (res*4)%MOD;
   cout << res << '\n';
}

signed main()
{
    fast;

    int t = 1;

    cin >> t;

    while(t--)

    solve();
}

#include<bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
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;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;

template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

void solve(){
    int n;cin>>n;
    vector<ll> a(n);
    cin>>a;

    map<ll,int> mp;
    for(auto &x:a) mp[x]++;
    bool f=true;
    for(auto [_,x]:mp){
        if(x%2) f=false;
    }

    if(n%2){
        if(f) cout<<"First"<<endl;
        else  cout<<"Second"<<endl;
    }else{
        if(f) cout<<"Second"<<endl;
        else  cout<<"First"<<endl;
    }

}

signed main(){
    int q;cin>>q;
    while(q--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9+7;
//const long long MOD = 998244353;
const long long INF_LL = 1e18+11; const int INF_int = 1e9+11;
#define ll long long
#define mp std::make_pair
#define rep(i,n) for(int i = 0;i<n;i++)
#define lrep(i,n) for(long long i = 0;i<n;i++)
#define All(a) a.begin(),a.end()
#define Debug_Output_ALL(a) for(auto x:a) cout << x << " ";cout << endl;
#define endl "\n"
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
void fast(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}
int main(){
    fast();
    map<char,char> tr;
    tr['a']='A';tr['b']='B';tr['c']='C';
    char s,t;
    cin >> s >> t;
    if(s=='Y') cout << tr[t] << endl;
    else cout << t << endl;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int main(){
    string s, t;
    cin >> s >> t;
    if(s[0] == 'Y'){
        printf("%c\n", t[0]+('A'-'a'));
    } else {
        cout << t << endl;
    }
}


// arc120_c
#pragma GCC optimize ("O3")

#include <bits/stdc++.h>
#ifdef LOCAL
#include "../../debug_util/cxx-prettyprint/prettyprint.hpp"
#include "../../debug_util/rng.hpp"
#include "../../debug_util/timer.hpp"
#endif
using namespace std;

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

#define REP(i, n) for (int i = 0 ; i < (int)(n) ; ++i)
#define REPN(i, m, n) for (int i = m ; i < (int)(n) ; ++i)
#define REP_REV(i, n) for (int i = (int)(n) - 1 ; i >= 0 ; --i)
#define REPN_REV(i, m, n) for (int i = (int)(n) - 1 ; i >= m ; --i)
#define ALL(x) x.begin(), x.end()

#define INF (ll)(1e15)
#define MOD (1000000007)

#define print2D(h, w, arr) REP(i, h) { REP(j, w) cout << arr[i][j] << " "; cout << endl; }
#define print_line(vec, n) {for(int idx=0;idx<(n-1);idx++) cout << (vec)[idx] << " "; cout << (vec)[(n)-1] << endl;}
template<class T> void print(const T& x){cout << x << "\n";}
template<class T, class... A> void print(const T& first, const A&... rest) { cout << first << " "; print(rest...); }
struct PreMain {PreMain(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} premain;

// 区間add, 区間sum
template<typename T>
struct LazySegmentTree{
    int n;
    vector<T> val;
    vector<T> lazy;
    vector<int> width;

    explicit LazySegmentTree(int n_){
        n = 1;
        while (n < n_) n <<= 1;

        val = vector<T>(n<<1, 0);
        lazy = vector<T>(n<<1, 0);
        width = vector<int>(n<<1, 1);
        for (int i = n-2; i >= 0; i--) {
            width[i] = width[i*2+1] + width[i*2+2];
        }
    }
    void lazy_propagate(int k) {
        val[k] += lazy[k] * width[k];
        if (k < n-1) {
            lazy[k*2+1] += lazy[k];
            lazy[k*2+2] += lazy[k];
        }
        lazy[k] = 0;
    }
    void add(int a, int b, int k, int l, int r, int x) {
        lazy_propagate(k);

        if (b <= l || r <= a) {
            return;
        }
        if (a <= l && r <= b) {
            lazy[k] += x;
            lazy_propagate(k);
            return;
        }
        int mid = (l+r)/2;
        add(a, b, k*2+1, l, mid, x);
        add(a, b, k*2+2, mid, r, x);
        val[k] = val[k*2+1] + val[k*2+2];
    }

    void add(int a, int b, int x) {
        add(a, b, 0, 0, n, x);
    }

    T get_sum(int a, int b, int k, int l, int r) {
        lazy_propagate(k);

        if (b <= l || r <= a) {
            return 0;
        }
        if (a <= l && r <= b) {
            return val[k];
        }
        int mid = (l+r)/2;
        T sl = get_sum(a, b, k*2+1, l, mid);
        T sr = get_sum(a, b, k*2+2, mid, r);
        return sl + sr;
    }

    T get_sum(int a, int b) {
        return get_sum(a, b, 0, 0, n);
    }
};

int main() {
#ifdef LOCAL
    ifstream in("../arg.txt"); cin.rdbuf(in.rdbuf());
#endif

    int N;
    cin >> N;
    vector<int> A(N), B(N);
    REP(i, N) cin >> A[i];
    REP(i, N) cin >> B[i];

    map<int, vector<int>> mp;
    REP_REV(i, N){
        mp[A[i] + i].emplace_back(i);
    }

    LazySegmentTree<ll> st(N);
    REP(i, N){
        st.add(i, i+1, i);
    }

    ll ans = 0;
    REP(i, N){
        int x = B[i] + i;
        if (mp[x].empty()){
            print(-1);
            return 0;
        }

        int idx = mp[x].back();
        ans += st.get_sum(idx, idx+1);

        mp[x].pop_back();
        st.add(idx+1, N, -1);

//        print(i, idx);
    }

    print(ans);

    return 0;
}

#include <cstdio>
#include <iostream>
#include <cassert>
#include <string>
#include <algorithm>
#include <cstring>
#include <utility>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <random>
#include <chrono>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)

using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAXN = 234567;
int bit[MAXN];

void add(int x, int N) {
    while (x <= N) {
        bit[x]++;
        x += (x & -x);
    }
}

int query(int x) {
    int ans = 0;
    while (x > 0) {
        ans += bit[x];
        x -= (x & -x);
    }
    return ans;
}

int main() {
    int N;
    scanf("%d", &N);
    vector<int> A(N + 1), B(N + 1);
    for (int i = 1; i <= N; i++)
        scanf("%d", &A[i]);
    for (int i = 1; i <= N; i++)
        scanf("%d", &B[i]);

    map<int, deque<int>> mp;
    for (int i = 1; i <= N; i++) {
        mp[A[i] + i].push_back(i);
    }
    for (int i = 1; i <= N; i++) {
        if (!mp.count(B[i] + i) || mp[B[i] + i].empty()) return !printf("-1\n");
        A[i] = mp[B[i] + i].front();
        mp[B[i] + i].pop_front();
    }
    // finding # inversion pair
    LL ans = 0;
    for (int i = 1; i <= N; i++) {
        int cur = A[i];
        ans += query(N) - query(cur);
        add(cur, N);
    }
    printf("%lld\n", ans);
    return 0;
}
/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
    * DON'T GET STUCK ON ONE APPROACH
by Benq;
*/


#include <bits/stdc++.h>
using namespace std;
int n,m;

int a[805][805];

int pre[805][805];

bool check(int x) {
	for(int i=1;i<=n;i++) {
		for(int j=1;j<=n;j++) {
			pre[i][j]=pre[i-1][j]+pre[i][j-1]-pre[i-1][j-1]+(a[i][j]<=x?-1:1);
		}
	}
	for(int i=m;i<=n;i++) {
		for(int j=m;j<=n;j++) {
			if(pre[i][j]-pre[i-m][j]-pre[i][j-m]+pre[i-m][j-m]<=0) {
				return 1;
			}
		}
	}
	return 0;
}

int main() {
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++) {
		for(int j=1;j<=n;j++) {
			scanf("%d",&a[i][j]);
		}
	}
	int l=0,r=1e9;
	while(l<r) {
		int mid=(l+r)>>1;
		if(check(mid)) {
			r=mid;
		}else {
			l=mid+1;
		}
	}
	printf("%d",r);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
  int N,M;
  cin>>N>>M;
  int L=max(N,M);
  vector<int> A(N);
  vector<int> B(M);
  vector<int> C(N+M);

  for(int i=0;i<N;i++){cin>>C.at(i);}
  for(int i=0;i<M;i++){cin>>C.at(i+N);}

  sort(C.begin(),C.end());
  if(C.at(0)!=0&&C.at(0)!=C.at(1)) cout<<C.at(0)<<" ";
  for(int i=1; i<N+M-1;i++){
  	if(C.at(i)!=0&&C.at(i)!=C.at(i+1)&&C.at(i)!=C.at(i-1)) cout<<C.at(i)<<" ";
  }
  if(C.at(N+M-1)!=0&&C.at(N+M-1)!=C.at(N+M-2)) cout<<C.at(N+M-1)<<" ";

  return 0;

}


#include<iostream>
#include<bits/stdc++.h>
#include<vector>
#define vi vector<int>
#define lli long long int
#define fo(i,n) for(int i=1;i<=n;i++)
#define foo(i,n) for(int i=0;i<n;i++)
#define ff first
#define ss second
#define pb push_back
#define vii vector<lli>
#define ok ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
#define vp vector<pair<lli,lli>>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
lli v[4],t=0;
foo(j,4) cin>>v[j];
	foo(i,4) t+=v[i];
	if(t%2) {
		cout<<"No";
		return 0;
	}
	lli s = 0;
	lli mask = 15;
	fo(i,mask){
		s=0;
		foo(j,4){
			if(i&(1<<j)){
				s+=v[j];
			}
		}
		if(s==t/2){
			cout<<"Yes";
			return 0;
		}
	}
	cout<<"No";
	return 0;
}

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
# define rep(i,a,b) for(int i=(a); i<=(b); ++i)
inline int readint(){
	int a = 0; char c = getchar(), f = 1;
	for(; c<'0'||c>'9'; c=getchar())
		if(c == '-') f = -f;
	for(; '0'<=c&&c<='9'; c=getchar())
		a = (a<<3)+(a<<1)+(c^48);
	return a*f;
}

const int MaxN = 200005;

int dp[MaxN][7]; // 1: T wins
char opt[MaxN], s[MaxN];

int dfs(int t,int x){
	if(~dp[t][x]) return dp[t][x];
	int &res = dp[t][x];
	if(opt[t] == 'A'){
		if(dfs(t+1,(10*x+s[t]-'0')%7)
			*dfs(t+1,(10*x)%7) == 0)
				return res = 0;
		return res = 1;
	}
	if(opt[t] == 'T'){
		if(dfs(t+1,(10*x+s[t]-'0')%7)
		or dfs(t+1,(10*x)%7) == 1)
			return res = 1;
		return res = 0;
	}
	return -1; // impossible
}

int main(){
	int n = readint();
	scanf("%s %s",s+1,opt+1);
	memset(dp,-1,sizeof(dp));
	rep(i,1,6) dp[n+1][i] = 0;
	dp[n+1][0] = 1;
	puts(dfs(1,0) ? "Takahashi" : "Aoki");
	return 0;
}


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

        #define ll long long
        #define pb push_back
        #define printV(v) for(int i=0;i<(int)v.size();i++){cout << v[i]<< " ";} cout << endl
        #define printRev(v) for(int i=(int)v.size()-1;i >= 0;i--){cout << v[i] << " ";} cout << endl
        #define sortV(v) sort(v.begin(),v.end())
        #define revV(v) reverse(v.begin(),v.end())

        ll a,b,c,d,e,f,g,n,m,k,t,x,h,w,p,q,y;
        string str = "";
        //ll mod = 1e9 + 7;
        ll mod = 998244353;
        const int N = 100009;

       void solve(){
            cin >> n >> m;
            if(m == 0){
                cout << "1\n";
                return;
            }else if(n == m){
                cout << "0\n";
                return;
            }
            vector<ll> v(m);
            for(int i=0;i  < m; i++){
                cin >> v[i];
            }
            sortV(v);
            v.push_back(n+1);
            vector<ll> distances;
            ll last = 0;
            ll mxLen = 0;
            ll mnLen = n;
            for(ll k : v){
                ll dis = k - last - 1;
                if(dis > 0){
                    mnLen = min(mnLen, dis);
                    mxLen = max(mxLen, dis);
                    distances.push_back(dis);
                }
                last = k;
            }
            ll answer = 0;
            for(ll k : distances){
                //cout << k  << " ";
                answer += (k/mnLen);
                if(k%mnLen) answer++;
            }
            cout << answer << endl;

       }

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

            int cases = 1;
           // cin >> cases;

            while(cases--) solve();
            return 0;

        }

#include <bits/stdc++.h>
using namespace std;
long long INF = 1000000000000000000;
int main(){
    string X;
    cin >> X;
    long long M;
    cin >> M;
    int N = X.size();
    int ma = 0;
    for(int i = 0; i < N; i++){
        ma = max(ma, (int)(X[i] - '0'));
    }
    if(N == 1){
        if(M < X[0] - '0') cout << 0 << endl;
        else cout << 1 << endl;
    }
    else{
        long long ub = INF + 1, lb = ma;
        while(ub - lb > 1){
            long long mid = (ub + lb) / 2;
            long long t = 1;
            long long sum = 0;
            int flag = 0;
            for(int i = N - 1; i >= 0; i--){
                sum += t * (X[i] - '0');
                if(sum > M){
                    flag = 1;
                    break;
                }
                if(i != 0 && (M - sum) / mid <  t){
                    flag = 1;
                    break;
                }
                t *= mid;
            }
            if(flag == 1) ub = mid;
            else lb = mid;
        }
        cout << lb - ma << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ff        first
#define ss        second
#define ll        long long
#define int       long long
#define inf       ((ll)1e18)
#define mod       1000000007
#define double    long double
#define ull       unsigned long long
#define vi        vector<ll>
#define ppi       pair<int,int>
#define pii       pair<int,pair<int,int>>
#define pb        push_back
#define pi        2*acos(0.0)
#define rev       greater<int>()
#define pr(a,x,y) for(int i=x;i<y;i++){cout<<a[i]<<" ";}
#define ps(s)     for(auto i:s){cout<<i<<"";}
#define sp(x,y)   fixed<<setprecision(y)<<x
#define w(x)      ll x; cin>>x; while(x--)
#define all(v)    v.begin(), v.end()
#define rall(v)   v.rbegin(), v.rend()
#define test      cout<<"This is test"<<endl;
#define str       string
#define endl      '\n'
#define e         cout<<'\n';
void kehsihba(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	/*#ifndef ONLINE_JUDGE
		freopen("input.txt", "r", stdin);
		freopen("output.txt", "w", stdout);
	#endif*/
}
ll get(ll n){
	return (n*(n+1))/2;
}
void solve(){
	ll n;cin>>n;
	ll ans=0;
	while(n--){
		ll x,y;cin>>x>>y;
		ans+=(get(y)-get(x-1));
	}
	cout<<ans;
}
int32_t main(){
	kehsihba();
	ll t=1;
	//cin>>t;
	for(int k=1;k<=t;k++){
		//cout<<"Case #"<<k<<": ";
		solve();
		//e;
	}
}

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

int main(void) {
	ll n, ans = 0;
	ll A, B;

	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> A >> B;
		ans += B * (B + 1) / 2 - (A - 1) * A / 2;
	}

	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n,counter=0,mx=0,ans=0;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
  cin>>arr[i];
}
sort(arr,arr+n);
for(int i=2;i<=arr[n-1];i++){
  counter=0;
  for(int a=0;a<n;a++){
    if(arr[a]%i==0){
      counter++;
    }
  }
  if(counter>mx){
    mx=counter;
    ans=i;
  }
}
cout<<ans;
}

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
#define int ll
#define FOR(i,s,e) for(ll i = s; i <= (ll)e; ++i)
#define DEC(i,s,e) for(ll i = s; i >= (ll)e; --i)
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#define db(x) cerr << #x << "=" << x << "\n"
#define db2(x, y) cerr << #x << "=" << x << " , " << #y << "=" << y << "\n"
#define db3(a,b,c) cerr<<#a<<"="<<a<<","<<#b<<"="<<b<<","<<#c<<"="<<c<<"\n"
#define dbv(v) cerr << #v << ":"; for (auto ite : v) cerr << ite << ' '; cerr <<"\n"
#define dbvp(v) cerr << #v << ":"; for (auto ite : v) cerr << "{"  << ite.f << ',' << ite.s << "} "; cerr << "\n"
#define dba(a,ss,ee) cerr << #a << ":"; FOR(ite,ss,ee) cerr << a[ite] << ' '; cerr << "\n"
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define reach cerr << "LINE: " << __LINE__ << "\n";
typedef pair <ll, ll> pi;
typedef tuple<ll,ll,ll> ti3;
string cts(char x) {string t(1,x); return t;}
ll rand(ll a, ll b) { return a + rng() % (b-a+1); }
const int MOD = 1e9 + 7;
const int inf = (int)1e9 + 500;
const long long oo = (ll)1e18 + 500;
template <typename T> void chmax(T& a, T b) { a = max(a, b); }
template <typename T> void chmin(T& a, T b) { a = min(a, b); }
const int MAXN = -1;
#ifndef LOCAL
#define cerr if(0)cout
#endif
map<int,int>mp;
int32_t main()
{
	IAMSPEED
	int n; cin >> n;
	vector<int> v;
	FOR(i,1,n) {
		int x; cin >> x;
		v.pb(x);
	}
	FOR(i,2,1000) {
		for(auto x:v) {
			if(x%i == 0)mp[i]++;
		}

	}
	pi ans = pi(-oo,-oo);
	for(auto i:mp) {
		chmax(ans,pi(i.s,i.f));
	}
	cout<<ans.s;

}


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

// 插入此處
constexpr int MAXN = 1e5 + 2;
array<LL, MAXN> A;

int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    int N; cin >> N;
    for (int i = 0; i < N; ++i)  cin >> A[i];
    sort(A.begin(), A.begin() + N);
    LL x2 =  1. * A[N/2];
    LL ans = 0;
    for (int i = 0; i < N; ++i)  ans += (x2 + 2 * A[i] - 2 * min(A[i], x2));
    cout.precision(9);
    cout << fixed << 1. * ans / 2 / N << endl;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define REP(i, a, b) for (ll i = a; i <= b; i++)
#define REP_REV(i, a, b) for (ll i = a; i >= b; i--)
#define debug(x) cerr << #x << " is " << x << endl;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define endl "\n"

#define pb push_back
#define rsz resize
#define f first
#define s second
#define mp make_pair

using ll = int64_t;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vc = vector<char>;
using ull = unsigned int64_t;
using pi = pair<int, int>;
using vl = vector<int64_t>;
using pl = pair<int64_t, int64_t>;
using vpl = vector<pair<int64_t, int64_t>>;
using msl = map<string, int64_t>;
using vvl = vector<vector<int64_t>>;

#define DBG1(x) cerr << (#x) << ": " << (x) << endl;
#define DBG2(x, y) cerr << (#x) << " " << (#y) << ": " << (x) << " " << y << endl;
#define DBG3(x, y, z) cerr << (#x) << " " << (#y) << " " << (#z) << ": " << (x) << " " << y << " " << z << endl;
#define DBG4(a, b, c, d) cerr << (#a) << " " << (#b) << " " << (#c) << " " << (#d) << ": " << a << " " << b << " " << c << " " << d << endl;
template <class T>
void DBGvec(vector<T> a) {
    for (T i : a) cerr << i << " ";
    cerr << endl;
}
template <class T>
void DBGvec_pair(vector<T> a) {
    for (T i : a) cerr << "{" << i.f << " " << i.s << "} ";
    cerr << endl;
}
template <class K, class P>
void DBGmap(map<K, P> a) {
    for (auto i : a) cerr << "{" << i.f << " " << i.s << "} ";
    cerr << endl;
}

const ll MOD = 1e9 + 7;
const int INF = int(1e18);

bool isPrime(ll n) {
    if (n == 1) return false;
    if (n == 2) return true;
    for (ll i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

unsigned long long factorial(int n) {
    const unsigned int M = MOD;
    unsigned long long f = 1;
    for (int i = 1; i <= n; i++)
        f = (f * i) % M;
    return f;
}

ll expon(ll a, ll b, ll m = MOD) {
    ll res = 1;
    a %= m;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}

double f(double a, double x) {
    return a + x - min(a, 2 * x);
}

void solve() {
    ll n;
    cin >> n;
    vector<double> a(n);
    REP(i, 0, n - 1) {
        cin >> a[i];
    }

    ll t = 1000;
    double l = 0, r = 1e9;

    while (t--) {
        double m1 = l + (r - l) / 3;
        double m2 = r - (r - l) / 3;
        double s1 = 0, s2 = 0;

        REP(i, 0, n - 1) {
            s1 += f(a[i], m1);
            s2 += f(a[i], m2);
        }
        if (s1 > s2) {
            l = m1;
        } else {
            r = m2;
        }
    }

    double ans = 0;
    REP(i, 0, n - 1) {
        ans += f(a[i], l);
    }
    ans /= n;
    cout << setprecision(20) << ans;
}

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

#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
    int tt = clock();
#endif

    // freopen("diamond.in", "r", stdin);
    // freopen("diamond.out", "w", stdout);

    // ll t;
    // cin >> t;
    // while (t--)
    solve();

#ifdef _DEBUG
    cerr << "\nTIME = " << clock() - tt << endl;
    tt = clock();
#endif
    return 0;
}