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

struct UnionFind {
	vector<int> data;
	UnionFind(int size) : data(size, -1) { }
	bool unionSet(int x, int y) {
		x = root(x); y = root(y);
		if (x != y) {
			if (data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool findSet(int x, int y) {
		return root(x) == root(y);
	}
	int root(int x) {
		return data[x] < 0 ? x : data[x] = root(data[x]);
	}
	int size(int x) {
		return -data[root(x)];
	}
};

struct Edge {
	int a, b, cost;

	Edge(int a, int b, int cost) : a(a), b(b), cost(cost) {}

	bool operator<(const Edge& o)const {
		return cost < o.cost;
	}
};

int main() {
	cin.tie(0); ios::sync_with_stdio(false);
	int n, Ans = 0; cin >> n;
	vector<Edge> es;
	for (int i = 1; i <= n;i++) {
		for (int j = 1; j <= n; j++) {
			int temp; cin >> temp;
			if (j < i)continue;
			if (temp != -1) {
				Edge e(i, j, temp);
				es.push_back(e);
			}
		}
	}
	sort(es.begin(), es.end());

	/*kruskal*/

	UnionFind uf(n+1);
	for (int i = 0; i < es.size();i++) {
		//cout << es[i].a << " " << es[i].b << " " << es[i].cost << endl;
		Edge& e = es[i];
		if (!uf.findSet(e.a, e.b)) {
			Ans += e.cost;
			uf.unionSet(e.a, e.b);
		}
	}

	cout << Ans << endl;

}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,x,n) for(int i=x; i<(n); i++)
#define ALL(n) begin(n),end(n)
#define MOD (1000000007)
#define INF (1e9)
#define INFL (1e18)

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
template<class T>using arr=vector<vector<T>>;
template<class T>int popcount(T &a){int c=0; rep(i, 8*(int)sizeof(a)){if((a>>i)&1) c++;} return c;}
template<class T>void pr(T x){cout << x << endl;}
template<class T>void prvec(vector<T>& a){rep(i, a.size()-1){cout << a[i] << " ";} pr(a[a.size()-1]);}
template<class T>void prarr(arr<T>& a){rep(i, a.size()) if(a[i].empty()) pr(""); else prvec(a[i]);}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

int main()
{
    int n; cin >> n;
    arr<int> A(n, vector<int>(n));
    vector<int> opp(n);
    rep(i, n){
        rep(j, n-1) {cin >> A[i][j]; A[i][j]--;}
        A[i][n-1] = -1;
        opp[i] = A[i][0];
    }

    vector<int> IND(n, 0);
    int ans = 0;
    int finished = 0;
    while(finished<n){
        ans++;
        // printf("day %d: ", ans);
        // rep(i, n) cout << opp[i] << " ";
        // pr("");

        vector<bool> seen(n, false);
        int game = 0;
        rep(i, n){

            if(IND[i]==n) continue;
            if(seen[i]) continue;
            seen[i] = true;
            if(IND[opp[i]]==n) continue;

            if(i == opp[opp[i]]){
                if(seen[opp[i]]) continue;
                int t = opp[i];
                // printf("%d: %d %d\n", ans+1, i, opp[i]);
                IND[i]++;
                IND[t]++;

                if(IND[i]==n-1) finished++;
                else opp[i] = A[i][IND[i]];

                if(IND[t]==n-1) finished++;
                else opp[t] = A[t][IND[t]];

                seen[t] = true;
                game++;
            }
        }
        if(game==0) {pr(-1); return 0;}
    }

    pr(ans);
    return 0;}


#include <iostream>
#include <cstdio>
using namespace std;
#define rep(i,n) for(int i = 0; i < n; i++)

int n, m;
int t[13][13];
int main(){
  while(cin >> n, n){
    rep(i,13)rep(j,13)t[i][j]=0;
    rep(i,n)rep(j,n){
      cin >> m;
      t[i][j] = m;
      t[n][j] += m;
      t[i][n] += m;
      t[n][n] += m;
    }
    rep(i,n+1){
      rep(j,n+1){
	printf("%5d",t[i][j]);
      }
      cout << endl;
    }
  }
  return 0;
}

#include <stdio.h>

int main(void){
    int n;
    int a,b;
    int aSum, bSum;

    while (scanf("%d", &n), n!=0){
        aSum = bSum=0;
        for (int i=0; i<n; i++){
            scanf("%d %d", &a, &b);
            if (a>b) aSum += (a+b);
            else if (a<b) bSum += (a+b);
            else aSum += a, bSum += b;
        }
        printf("%d %d\n", aSum, bSum);
    }

    return 0;
}


#include <iostream>
#include <functional>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <iomanip>
#include <complex>
#include <random>
#include <bitset>
#include <list>
// #include <prettyprint.hpp>
using namespace std;
#define repi(i,n) for(int i=0;i<n;++i)
#define rep(i,n) for(ll i=0;i<n;++i)
#define repinvi(i,n) for(int i=n-1;i>=0;--i)
#define sll(n) scanf("%lld", &n);
#define sii(n) scanf("%d", &n);
#define slf(n) scanf("%lf", &n);
#define pll pair<ll,ll>
#define pii pair<int,int>
#define psi pair<si,si>
typedef long long ll;
typedef double lf;
typedef short int si;

void Main(){
    ll N, l;
    sll(N);
    // vector<ll> ls(N, 0);
    map<ll, ll> counter;
    rep(i, N){
        sll(l);
        counter[l] ++;
    }

    ll ans = 0;
    for(auto p1:counter){
        for(auto p2:counter){
            if(p1.first >= p2.first){
                continue;
            }
            for(auto p3:counter){
                if(p1.first >= p3.first){
                    continue;
                }else if(p2.first >= p3.first){
                    continue;
                }

                if(p3.first >= p1.first + p2.first){
                    continue;
                }

                ans += (p1.second * p2.second * p3.second);

            }
        }
    }

    cout << ans <<endl;
}

int main(){
    Main();
}

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

int main()
{
    int n;
    cin >> n;
    vector<int> L(n);
    rep(i,n) cin >> L[i];

    int res = 0;
    for(int i = 0; i < n; ++i) {
        for(int j = i + 1; j < n; ++j) {
            for(int k = j + 1; k < n; ++k) {
                if(L[i] == L[j] || L[j] == L[k] || L[k] == L[i]) continue;
                vector<int> v = {L[i],L[j],L[k]};
                sort(v.begin(), v.end());
                res += (v[0] + v[1] > v[2]);
            }
        }
    }
    cout << res << "\n";
    return 0;
}


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

int main(){
  int a,b;
  cin >> a >>b;
  int mx = max(a,b);
  int mn = min(a,b);
  int mx2 = max(mx-1,mn);
  cout << mx + mx2 << endl;
}

// cout << fixed << setprecision(15) <<  << endl;

#include <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <time.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <numeric>
#include <limits>
#include <type_traits>

using namespace std;
#define SAY_YES cout << "YES" << endl;
#define SAY_Yes cout << "Yes" << endl;
#define SAY_NO cout << "NO" << endl;
#define SAY_No cout << "No" << endl;
#define IFYES(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "YES" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "NO" << endl;  \
    }
#define IFYes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "Yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "No" << endl;  \
    }
#define IFyes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "no" << endl;  \
    }
#define DEBUG_OUTPUT_ARRAY(XXX, ONE)                            \
    for (int i = 0; i < (ONE); i++)                             \
    {                                                           \
        cout << "DEBUG: i = " << i << " -> " << XXX[i] << endl; \
    }
#define DEBUG_OUTPUT_ARRAY2(XXX, ONE, TWO)                             \
    for (int i = 0; i < (ONE); i++)                                    \
    {                                                                  \
        cout << "<<< i = " << i << " >>>" << endl;                     \
        for (int j = 0; j < (TWO); j++)                                \
        {                                                              \
            cout << "DEBUG: j = " << j << " -> " << XXX[i][j] << endl; \
        }                                                              \
    }
#define DEBUG_OUTPUT_ARRAY2_BOX(XXX, ONE, TWO) \
    for (int i = 0; i < (ONE); i++)            \
    {                                          \
        cout << i << "  ";                     \
        for (int j = 0; j < (TWO); j++)        \
        {                                      \
            cout << XXX[i][j] << " ";          \
        }                                      \
        cout << endl;                          \
    }

typedef pair<long long int, long long int> pll;

const long long int mod = 1000000007;
const long long int INF = 1e18;
const long double PI=3.14159265358979323;

long long int mi=0;
long long int N,res=0,abssum=0;
long long int A,B,C,K;
string S;

pll clo[200020],revclo[200020];
int main()
{
    cout << fixed << setprecision(18);
    cin>>S;
    if(S.length()==3){
        reverse(S.begin(),S.end());
    }
    cout<<S<<endl;
}


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

string a(int aa, int b) {
  if(aa <= 8 && b <= 8) {
    return "Yay!";
  }
  return ":(";

}

int main() {
  int aa, b;
  cin >> aa >> b;
  cout << a(aa, b);
}

/*
R         R R R R        R        R R R      R      R    R    R      R    R R R R
R         R             R R       R    RR    R R    R    R    R R    R    R
R         R            R   R      R R R      R  R   R    R    R  R   R    R   R R
R         R R R       R R R R     R R        R   R  R    R    R   R  R    R     R
R         R          R       R    R   RR     R    R R    R    R    R R    R     R
R R R R   R R R R   R         R   R     RR   R      R    R    R      R    R R R R

*/

#include<bits/stdc++.h>
using namespace std;
#define  bf            ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define  ll            long long
#define  pb            push_back
#define  pr            10
#define  mx            100000 // 10^6
#define  md            1000000007
#define  result(a)     cout<<a<<endl;
#define  forr(a,x)     for(int i=a;i<=x;i++)
#define  input(n,a)    int a[n];for(int i=0;i<n;i++)cin>>a[i];
#define  print(a)      for(auto it:a)cout<<it<<' '; //0-based
#define  printt(n,a)   for(i=0;i<n;i++)cout<<a[i]<<' '; //1-based
#define  sz(a)         cout<<a.size()<<endl;
#define  prefx(n,a)    for(i=1;i<n;i++)a[i]=a[i-1]+a[i]; //0-based
#define  sortt(v)      sort(v.begin(),v.end());
#define  srt(n,a)      sort(a,a+n);
#define  rev(n,a)      reverse(a,a+n);
#define  revr(a)       reverse(a.begin(),a.end());
#define  yes           cout<<"Yes\n";
#define  no            cout<<"No\n";

int main()
{
  int a,b;
  cin>>a>>b;
  if(a<=8&&b<=8)
  {
    cout<<"Yay!\n";
  }
  else
  {
    cout<<":(\n";
  }
}


#include <iostream>
using namespace std;

int main() {
	int N;
	cin >> N;
	long long ans = 0, before;
	cin >> before;
	bool HasNoAns = (before > 0);
	for (int i = 1; i < N; i++) {
		int now;
		cin >> now;
		if (now == 0) {
			before = now;
			continue;
		}
		if (now == before + 1) ans++;
		else {
			if (now <= before) ans += now;
			else HasNoAns = true;
		}
		before = now;
	}
	cout << (HasNoAns ? -1 : ans) << endl;
}

#include <bits/stdc++.h>
using namespace std;
int a[200002], b[200002];
int main(){
    int n;
    scanf("%d", &n);
    bool ok = 1;
    for(int i=1;i<=n;i++){
        scanf("%d", &a[i]);
        if(a[i] > a[i-1] && a[i]-a[i-1] > 1){
            ok = 0;
        }
        if(i == 1 && a[i] != 0){
            ok = 0;
        }
    }
    if(!ok) printf("-1\n");
    else{
        long long ans = 0;
        for(int i=n;i>=1;i--){
            if(a[i] == 0) continue;
            if(a[i]+1 == a[i+1]) continue;
            ans += a[i];
        }
        printf("%lld\n", ans);
    }
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll,ll>;

#define INF (1LL << 60)
#define MOD 998244353
#define PI 3.14159265358979323846

#define REP(i,m,n) for(ll (i)=(m),(i_len)=(n);(i)<(i_len);++(i))
#define FORR(i,v) for(auto (i):v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x) ASC((x)); REV((x))
#define pb push_back
#define eb emplace_back

ll modpow(ll a, ll n, ll m)
{
    if(n == 0) return 1;
    ll t = modpow(a, n >> 1, m);
    t = t * t % m;
    if(n & 1) t = t * a % m;
    return t;
}

int main()
{
    ll N;
    cin >> N;
    vec D(N);
    REP(i,0,N) cin >> D[i];

    vec A(N, 0);
    if(D[0] != 0) PR(0);
    else {
        ll m = 0;
        REP(i,0,N) {
            ++A[D[i]];
            m = MAX(m,D[i]);
        }
        if(A[0] > 1) PR(0);
        else {
            ll cnt = 1;
            REP(i,1,m+1) {
                cnt = (cnt * modpow(A[i-1], A[i], MOD)) % MOD;
            }
            PR(cnt);
        }
    }

    return 0;
}

/*

*/

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

int main() {
  int N;
  cin >> N;
  vector<int> D(N), cnt(N);
  for (int i = 0, d; cin >> d; i++) {
    D.at(i) = d;
    cnt.at(d)++;
  }
  if (D.at(0) || cnt.at(0) != 1) return cout << 0 << "\n", 0;
  long ans = 1;
  int mx = *max_element(D.begin(), D.end());
  for (long i = 1; i <= mx; i++) {
    long tmp = cnt.at(i);
    while (tmp--) {
      ans *= cnt.at(i - 1);
      ans %= 998244353;
    }
  }
  cout << ans << "\n";
}


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

int main(){
#ifdef _DEBUG
	//freopen("_in" , "r", stdin );
	//freopen("_out", "w", stdout);
#endif
	ios::sync_with_stdio(0); cin.tie(0);
	string s[3]; cin >> s[0] >> s[1] >> s[2];
	int i = 0;
	for(;;) {
		if (!s[i].size()) break;
		int t;
		if (s[i][0] == 'a') t = 0;
		if (s[i][0] == 'b') t = 1;
		if (s[i][0] == 'c') t = 2;
		s[i].erase(0, 1);
		i = t;
	}
	if (i == 0) cout << 'A';
	if (i == 1) cout << 'B';
	if (i == 2) cout << 'C';
	cout << '\n';
}

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

int main(){
  int N;
  cin>>N;
  int A[N];
  for(int i=0;i<N;i++){
    cin>>A[i];
  }
  bool ans=true;
  sort(A,A+N);
  for(int i=0;i<N-1;i++){
    if(A[i]==A[i+1]){
      ans =false;
      break;
    }
  }
  if(ans){
    cout<<"YES"<<endl;
  }else{
    cout<<"NO"<<endl;
  }
}


#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define invrepr(i,a,b) for(int i=b-1;i>=a;i--)
#define invrep(i,n) invrepr(i,0,n)
#define repitr(itr,a) for(auto itr=a.begin();itr!=a.end();++itr)
#define P pair<int,int>
const int MOD=998244353;
const int MAX=1e5+10;
const ll INF=1e18;
const double PI=acos(-1);

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

    int n,x;
    cin >> n >> x;
    vector<int> a(n);
    rep(i,n) cin >> a[i];
    sort(a.begin(),a.end());
    int ans=n-1;
    rep(i,n) {
        x-=a[i];
        if (x==0) {
            ans=i+1;
            break;
        } else if (x<0) {
            ans=i;
            break;
        }
    }
    cout << ans << endl;

    return 0;
}

#include <bits/stdc++.h>
#define rep(i, start, end) for (long long i = start; i < end; ++i)
#define repreverse(i, start, end) for (long long i = start; i >= end; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define len(x) ((long long)(x).size())
#define lcm(a, b) ((a) / __gcd((a), (b)) * (b))
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vllvll = vector<vll>;
using vc = vector<char>;
using pll = pair<ll, ll>;
template<class T> void print1d(T x,ll n=-1){if(n==-1)n=x.size();rep(i,0,n){cout<<x[i]<<' ';}cout<<'\n';}
template<class T> void print2d(T x,ll r=-1,ll c=-1){if(r==-1)r=x.size();if(c==-1)c=x[0].size();rep(i,0,r)print1d(x[i],c);}
template<class T, class U> bool haskey(T mp, U key) { return mp.find(key) != mp.end(); }
template<class T, class U> bool isin(T el, U container) { return find(all(container), el) != container.end(); }
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
template<class T> bool even(T n) { return ! (n & 1); }
template<class T> bool odd(T n) { return n & 1; }
template<class T> ll rup(T a, T b) { return a % b ? a / b + 1 : a / b; }
template<class T> ld deg2rad(T deg) { return M_PI * deg / 180.0; }
template<class T> ld rad2deg(T rad) { return 180.0 * rad / M_PI; }
const long double pi = M_PI;
const long long big = 1LL << 50;
const long long inf = 1LL << 60;
const long long mod = 1e9 + 7;
ll intpow(ll a, ll n, ll _mod=numeric_limits<ll>::max()) { ll p=1; while (n) { if (n&1) p=p*a%_mod; a=a*a%_mod; n>>=1; } return p; }
ll modc(ll a, char op, ll b, ll _mod=mod)
{
        a %= _mod; b %= _mod; ll res = 1;
        switch (op) {
        case '+': res = (a + b) % _mod; break;
        case '-': res = (a - b) % _mod; break;
        case '*': res = a * b % _mod; break;
        case '/': res = modc(a, '*', modc(b, '^', _mod-2, _mod), _mod); break;
        case '^': res = intpow(a, b, _mod); break;
        case 'P': rep(i, a-b+1, a+1) res = modc(res, '*', i, _mod); break;
        case 'C': res = modc(modc(a, 'P', b, _mod), '/', modc(b, 'P', b, _mod)); break;
        }
        if (res < 0) { res += _mod; } return res;
}

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

        vll a(N);
        rep(i, 0, N) cin >> a[i];

        ll ans = 0;
        sort(all(a));
        rep(i, 0, N) {
                if (i != N-1) {
                        if (x >= a[i]) {
                                x -= a[i];
                                ++ans;
                        }
                } else {
                        if (a.back() == x) ++ans;
                }
        }

        cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vb=vector<bool>;
using vvb=vector<vb>;
using vd=vector<double>;
using vvd=vector<vd>;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll>;
using tlll=tuple<ll,ll,ll>;
using vs=vector<string>;

#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rep(i,n) range(i,0,n)
#define rrep(i,n) for(ll i=(n)-1;i>=0;i--)
#define range(i,a,n) for(ll i=(a);i<(n);i++)

#define LINF ((ll)1ll<<60)
#define INF ((int)1<<30)
#define EPS (1e-9)
#define MOD (1000000007ll)
#define fcout(a) cout<<setprecision(a)<<fixed
#define fs first
#define sc second
#define PI 3.1415926535897932384

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

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;}
template<class S>S acm(vector<S>&a){return accumulate(all(a),S());}
template<class S>S max(vector<S>&a){return *max_element(all(a));}
template<class S>S min(vector<S>&a){return *min_element(all(a));}
void YN(bool b){cout<<(b?"YES":"NO")<<"\n";}
void Yn(bool b){cout<<(b?"Yes":"No")<<"\n";}
void yn(bool b){cout<<(b?"yes":"no")<<"\n";}
int sgn(const double&r){return (r>EPS)-(r<-EPS);} // a>0  : sgn(a)>0
int sgn(const double&a,const double&b){return sgn(a-b);} // b<=c : sgn(b,c)<=0

ll max(int a,ll b){return max((ll)a,b);} ll max(ll a,int b){return max(a,(ll)b);}
template<class T>void puta(T&&t){cout<<t<<"\n";}
template<class H,class...T>void puta(H&&h,T&&...t){cout<<h<<' ';puta(t...);}
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool a=1; for(auto s:t){os<<(a?"":" ")<<s;a=0;} return os;}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	ll n;
	cin>>n;
	vector<pll> a,b;
	rep(i,n){
		string s;
		cin>>s;
		pll p(0,0);
		rep(j,s.size()){
			if(s[j]=='(')p.fs++;
			else p.fs--;
			chmin(p.sc,p.fs);
		}
		if(p.fs>=0) a.push_back(p);
		else b.push_back(p);
	}
	sort(all(a),[](const auto &a,const auto &b){
		if(a.sc!=b.sc)return a.sc>b.sc;
		else return a.fs>b.fs;
	});
	sort(all(b),[](const auto &a,const auto &b){
		return a.fs-a.sc>b.fs-b.sc;
	});
	ll cur=0;
	bool ans=true;
	for(auto x:a){
		if(cur+x.sc<0)ans=false;
		cur+=x.fs;
	}
	for(auto x:b){
		if(cur+x.sc<0)ans=false;
		cur+=x.fs;
	}
	Yn(ans&&cur==0);
}

#include<iostream>
#include<string>
using namespace std;
int main(void){
    string str;
    int q;
    cin >> str;
    cin >> q;
    for (int i=0;i<q;i++){
        string c;
        cin >> c;
        if (c=="print"){
            int a,b;
            cin >> a >> b;
            for (int i=a;i<=b;i++)
                cout << str[i];
            cout << endl;
        }
        if (c=="reverse"){
            int a,b;
            cin >> a >> b;
            char t;
            for (int i=0;i<=(b-a)/2;i++){
                t=str[a+i];
                str[a+i]=str[b-i];
                str[b-i]=t;
            }
        }
        if (c=="replace"){
            int a,b;
            string r;
            cin >> a >> b >> r;
            str.replace(a,b-a+1,r);
        }
    }
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
ll dp[5009];
  ll a[5000];
int main()
{
  ll n;ll ans=0;
  multiset<ll>s;cin>>n;
  for(int i=0;i<n;i++){
      ll x;cin>>x;
      if(s.size()==0){
          s.insert(x);
          ans++;
          }
          else
          {
              auto z=s.begin();
              if(*z>=x){
                  s.insert(x);ans++
;              }else{
              auto it=s.lower_bound(x);it--;
              s.erase(s.find(*it));
               s.insert(x);
}

}
  }cout<<ans;

}

#include <bits/stdc++.h>
#define rep(a,n) for (ll a = 0; a < (n); ++a)
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<P,ll> PP;
typedef vector<vector<ll> > Graph;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF = 1e18;

ll n;
vector<ll>a;

int main(){
    cin>>n;
    a.resize(n+1);
    rep(i,n)cin>>a[i+1];
    vector<ll>ans(n+1,0);
    ll M = 0;
    for(int i=n;i>=1;i--){
        int j=2;
        int cnt = 0;
        //倍数の和を求める
        while (i*j<=n){
            if(ans[i*j]==1)cnt++;
            j++;
        }
        if((a[i]%2)!=(cnt%2)){
            ans[i]++;
            M++;
        }
    }
    cout << M << endl;
    for(int i=1;i<=n;i++){
        if(ans[i]==1)cout<<i<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << x << endl
#define debugArray(x, n)                           \
  cerr << __LINE__ << ": " << #x << " = {";        \
  for (long long hoge = 0; (hoge) < (n); ++(hoge)) \
    cerr << ((hoge) ? "," : "") << x[hoge];        \
  cerr << "}" << endl
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#endif

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  int A[N];
  vector<pair<int, bool>> xy[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    xy[i].resize(A[i]);
    for (int j = 0; j < A[i]; j++) {
      cin >> xy[i][j].first >> xy[i][j].second;
      xy[i][j].first--;
    }
  }
  int ans = 0;
  for (int s = 1; s < (1 << N); s++) {
    bool isok = true;
    for (int i = 0; i < N; i++)
      if ((s >> i) & 1) {
        for (int j = 0; j < A[i]; j++) {
          isok &= !(((s >> xy[i][j].first) & 1) ^ xy[i][j].second);
        }
      }
    if (isok) ans = max(ans, __builtin_popcount(s));
  }
  cout << ans << '\n';
  return 0;
}

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

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

using namespace std;

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

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

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
int n;//計算する行列の数
const int MAX_N =110;
int dp[MAX_N][MAX_N];//dp[i][j]:=Ai...Ajの計算に必要な最小のコスト
pii A[MAX_N];//行列firstを行secondを列とする。
void init(){
	for(int i =0; i < n;i++){
		for(int j = 0;j < n;j++){
			dp[i][j]=INF;
		}
	}
	for(int i =0;i < n;i++){
		dp[i][i]=0;
	}
}

int solve(){
	for(int k =1;k <n;k++){
		for(int i =0;i <n-k;i++){
			for(int j =i;j <i+k;j++){
				int sz =A[i].first*A[j].second*A[i+k].second;
				dp[i][i+k]=
				min(dp[i][i+k],dp[i][j]+dp[j+1][i+k]+sz);
			}
		}
	}
	return dp[0][n-1];
}

int main(){
	cin>>n;
	init();
	for(int i =0;i < n;i++){
		cin>>A[i].first>>A[i].second;
	}
	cout <<solve()<<endl;
	return 0;
}


#include <bits/stdc++.h>

#define F first
#define S second
#define pii pair<int, int>
#define pb push_back

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 1e5 + 10;

ll x[N], p[N];
ll ans;

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n, S; cin >> n >> S;
	int pos = -1;
	for (int i=0; i<n; i++)
	{
		cin >> x[i] >> p[i];
		if(x[i] < S) pos = i;
	}

	int st = 0, en = n - 1, dir = -1;
	while(true)
	{
		if(st > pos){ ans += x[en] - S; break ;}
		else if(en <= pos){ ans += S - x[st]; break; }
		if(p[st] >= p[en])
		{
			if(dir != 0) ans += x[en] - x[st];
			dir = 0;
			p[st] += p[en];
			en --;
		}else
		{
			if(dir != 1) ans += x[en] - x[st];
			dir = 1;
			p[en] += p[st];
			st ++;
		}
	}
	cout << ans << endl;
}

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

const int maxn=100005;

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

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

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

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


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <functional>
#include <iomanip>
#include <bitset>
#include <numeric>
#include <queue>
#include <map>
#include <cstdint>
#include <set>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const long long mod = 1000000007;
typedef long long int ll;
typedef pair<ll, ll> P;
const vector<int> di = { -1, 0, 1, 0 };
const vector<int> dj = { 0, 1, 0, -1 };
const double PI = 3.14159265358979323846;

int main() {

	int x;
	cin >> x;
	int five_hund = 0;
	int five = 0;

	five_hund = x / 500;
	five = (x % 500) / 5;

	cout << five_hund * 1000 + five * 5 << endl;

	return 0;

}

/*
    -ensure correct output format
    -ensure printing required output
    -reread the problem statement
  */

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>pll;
typedef pair<ll,pair<ll,ll>>plll;
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
#define vll(v) v.begin(),v.end()
#define all(x) x.rbegin(),x.rend()
#define min3(a, b, c)   min(a, min(b, c))
#define max3(a, b, c)   max(a, max(b, c))
#define F first
#define S second
#define in  freopen("input.txt", "r", stdin)
#define out  freopen("output.txt", "w", stdout)
#define minheap int,vector<int>,greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x)  (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))
#define bug cout<<"BUG"<<endl;
const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const double PI  =3.141592653589793238463;
bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
       return (a.first > b.first);
}
ll lcm(ll a,ll b)
{
    if(a==0 || b==0)return 0;

    return a/__gcd(a,b)*b;
}

void input(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)cin>>ara[i];
}
void print(ll ara[],ll n)
{
    for(ll i=0; i<n; i++)
        cout<<ara[i]<<" ";
    cout<<endl;
}

int main()
{

   fastread();

   ll i,j,n,m,p,a,sum=0,k,t,b,c,d,cnt=0,q,l,r,ans=0;

      bool flag=false;

      string str;

      cin>>n;

      m=n/500;

      n%=500;

      m*=1000;

      a=n/5;
      m+=(a*5);

      cout<<m<<endl;

}


#include <iostream>
#include <algorithm>

using namespace std;

int partition(int A[], int p, int r) {
	int x = A[r];
	int i = p;

	for (int j = p; j < r; j++) {
		if (A[j] <= x) {
			swap(A[i++], A[j]);
		}
	}
	swap(A[i], A[r]);

	return i;
}

int main() {
	int n;
	cin >> n;
	int A[n], suffix;

	for (int i = 0; i < n; i++) {
		cin >> A[i];
	}
	suffix = partition(A, 0, n - 1);

	for (int i = 0; i < n; i++) {
		if (i == suffix) {
			cout << "[" << A[i] << "]";
		}
		else {
			cout << A[i];
		}

		if (i < n - 1) {
			cout << " ";
		}
	}
	cout << endl;

	return 0;
}

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef bitset<705> bs;

// dp[i] is number of colorings with sum equal to i
ll dp[300 * 300 + 5];
const int MOD = 998244353;

ll modpow(ll b, ll e, ll m) {
  ll r = 1;
  while(e) {
    if(e%2) {
      r*=b;
      r%=m;
    }
    b*=b;
    b%=m;
    e/=2;
  }
  return r;
}

ll compensate(vector<int>& v) {
  memset(dp, 0, sizeof(dp));
  dp[0] = 1;
  int tot = 0;
  for(int out: v) {
    for(int i = 90000; i >= out; i--) {
      dp[i] += dp[i-out];
      dp[i] %= MOD;
    }
    tot += out;
  }
  return 3 * dp[tot/2];
}

void solve() {
  int n;
  cin >> n;
  dp[0] = 1;
  ll total = 1;
  int sum = 0;
  vector<int> v;
  for(int i = 0; i < n; i++) {
    total *= 3;
    total %= MOD;
    int t;
    cin >> t;
    v.push_back(t);
    sum += t;
    for(int x = 300 * 300; x >= 0; x--) {
      dp[x] = 2 * dp[x];
      if(x >= t) dp[x] += dp[x-t];
      dp[x] %= MOD;
    }
  }
  ll ret = total;
  for(int i = (sum+1)/2; i <= sum; i++) {
    ret += 3 * (MOD - dp[i]);
    ret %= MOD;
  }
  if(sum%2 == 0) {
    ret += compensate(v);
    ret %= MOD;
  }
  cout << ret << "\n";
}

void casesolve() {
  int t;
  cin >> t;
  for(int i = 1; i <= t; i++) {
    cout << "Case #" << i << ": ";
    solve();
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  solve();
  // casesolve();
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    string s;
    int k;
    cin >> s >> k;
    int n = s.size();
    vector<vector<vector<int>>> dp(n, vector<vector<int>>(n, vector<int>(k + 1)));
    for (int i = n - 1; i >= 0; i--) {
        for (int j = i; j <= n - 1; j++) {
            for (int l = 0; l <= k; l++) {
                if (i == j) {
                    dp[i][j][l] = 1;
                } else if (i + 1 == j) {
                    dp[i][j][l] = 2;
                    if (l == 0 && s[i] != s[j]) {
                        dp[i][j][l] = 1;
                    }
                } else {
                    dp[i][j][l] = max(dp[i + 1][j][l], dp[i][j - 1][l]);
                    // dp[i][j][l] = max(dp[i][j][l], dp[i + 1][j - 1][l] + 1);
                    if (s[i] == s[j]) {
                        dp[i][j][l] = max(dp[i][j][l], dp[i + 1][j - 1][l] + 2);
                    } else if (l != 0) {
                        dp[i][j][l] = max(dp[i][j][l], dp[i + 1][j - 1][l - 1] + 2);
                    }
                }
            }
        }
    }
    cout << dp[0][n - 1][k] << '\n';
    return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<limits>
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define clr(a,v) memset((a),(v),sizeof(a))

const int MAX_N = 100, INF = std::numeric_limits<int>::max()>>2;

int n;
int r[MAX_N], c[MAX_N];
std::vector<std::vector<int> > dp( MAX_N, std::vector<int>( MAX_N, INF ) );

/*int rec( int b, int e )
{
    if( b == e )
        return dp[b][e] = 0;

    if( ~dp[b][e] )
        return dp[b][e];

    int ret = INF;

    rep( i, e-b )
        ret = std::min( ret, rec( b, b+i )+rec( b+i+1, e )+r[b]*c[b+i]*c[e] );

    return dp[b][e] = ret;
}*/

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

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

    repi( w, 1, n ) rep( i, n ) if( i+w < n ) repi( j, i, i+w )
        dp[i][i+w] = std::min( dp[i][i+w], dp[i][j]+dp[j+1][i+w]+r[i]*c[j]*c[i+w] );

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

    return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key

typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<ll> vi;
typedef long double ld;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;
int n,q,a,b;
ll x[222222];

struct node
{
	ll lazy;
	ll ans;
};

node st[2][800111];

void build(int ty, int id, int l, int r)
{
	st[ty][id].lazy=0;
	if(r-l<2)
	{
		st[ty][id].ans=ll(1e14);
		if(l==a)
		{
			st[ty][id].ans=abs(x[0] - b);
		}
		if(ty==0) st[ty][id].ans-=l;
		else st[ty][id].ans+=l;
		return ;
	}
	int mid=(l+r)>>1;
	build(ty,id*2,l,mid); build(ty,id*2+1,mid,r);
	st[ty][id].ans=min(st[ty][id*2].ans,st[ty][id*2+1].ans);
}

void push(int ty, int id, int l, int r)
{
	if(st[ty][id].lazy!=0)
	{
		st[ty][id].ans+=st[ty][id].lazy;
		if(r-l>=2)
		{
			st[ty][id*2].lazy+=st[ty][id].lazy;
			st[ty][id*2+1].lazy+=st[ty][id].lazy;
		}
		st[ty][id].lazy=0;
	}
}

void update(int ty, int id, int l, int r, int pos, ll v)
{
	push(ty,id,l,r);
	if(pos>=r||pos<l) return ;
	if(r-l<2)
	{
		st[ty][id].ans=min(v,st[ty][id].ans);
		return ;
	}
	int mid=(l+r)>>1;
	update(ty,id*2,l,mid,pos,v); update(ty,id*2+1,mid,r,pos,v);
	st[ty][id].ans=min(st[ty][id*2].ans,st[ty][id*2+1].ans);
}

void add(int ty, int id, int l, int r, int ql, int qr, ll v)
{
	push(ty,id,l,r);
	if(ql>=r||l>=qr) return ;
	if(ql<=l&&r<=qr)
	{
		st[ty][id].lazy+=v;
		push(ty,id,l,r);
		return ;
	}
	int mid=(l+r)>>1;
	add(ty,id*2,l,mid,ql,qr,v); add(ty,id*2+1,mid,r,ql,qr,v);
	st[ty][id].ans=min(st[ty][id*2].ans,st[ty][id*2+1].ans);
}

ll query(int ty, int id, int l, int r, int ql, int qr)
{
	push(ty,id,l,r);
	if(ql>=r||l>=qr) return ll(1e15);
	if(ql<=l&&r<=qr) return st[ty][id].ans;
	int mid=(l+r)>>1;
	return min(query(ty,id*2,l,mid,ql,qr), query(ty,id*2+1,mid,r,ql,qr));
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin>>n>>q>>a>>b; a--; b--;
	for(int i=0;i<q;i++) {cin>>x[i]; x[i]--;}
	ll ans=ll(1e18);
	for(int z=0;z<2;z++)
	{
		for(int ty=0;ty<2;ty++) build(ty,1,0,n);
		for(int i=1;i<q;i++)
		{
			ll real = min(query(0,1,0,n,0,x[i]) + x[i], query(1,1,0,n,x[i],n) - x[i]);
			for(int j=0;j<2;j++) add(j,1,0,n,0,n,abs(x[i-1]-x[i]));
			update(0,1,0,n,x[i-1],real-x[i-1]);
			update(1,1,0,n,x[i-1],real+x[i-1]);
		}
		for(int i=0;i<n;i++)
		{
			ans=min(ans,query(0,1,0,n,i,i+1)+i);
		}
		swap(a,b);
	}
	cout<<ans<<'\n';
}

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

#define EPS (1e-7)
const ll INF = 1e16;
#define PI (acos(-1))
//const ll mod = 1000000007;
struct LazySegmentTree {
private:
    int n;
    vector<ll> node, lazy;

public:
    LazySegmentTree() {
        int sz = 200050;
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n-1);
        lazy.resize(2*n-1, 0);

        for(int i=0; i<sz; i++) node[i+n-1] = INF;
        for(int i=n-2; i>=0; i--) node[i] = min(node[i*2+1], node[i*2+2]);
    }

    // k 番目のノードについて遅延評価を行う
    void eval(int k, int l, int r) {

    // 遅延配列が空でない場合、自ノード及び子ノードへの
    // 値の伝播が起こる
        if(lazy[k] != 0) {
            node[k] += lazy[k];

        // 最下段かどうかのチェックをしよう
        // 子ノードは親ノードの 1/2 の範囲であるため、
        // 伝播させるときは半分にする
            if(r - l > 1) {
                lazy[2*k+1] += lazy[k];
                lazy[2*k+2] += lazy[k];
            }

        // 伝播が終わったので、自ノードの遅延配列を空にする
            lazy[k] = 0;
        }
    }

    void add(int a, int b, ll x, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;

    // k 番目のノードに対して遅延評価を行う
        eval(k, l, r);

    // 範囲外なら何もしない
        if(b <= l || r <= a) return;

    // 完全に被覆しているならば、遅延配列に値を入れた後に評価
        if(a <= l && r <= b) {
            lazy[k] += x;
            eval(k, l, r);
        }

    // そうでないならば、子ノードの値を再帰的に計算して、
    // 計算済みの値をもらってくる
        else {
            add(a, b, x, 2*k+1, l, (l+r)/2);
            add(a, b, x, 2*k+2, (l+r)/2, r);
            node[k] = min(node[2*k+1], node[2*k+2]);
        }
    }

    ll getmin(int a, int b, int k=0, int l=0, int r=-1) {
        if(r < 0) r = n;

    // 関数が呼び出されたらまず評価！
        eval(k, l, r);

        if(b <= l || r <= a) return 1e17;
        if(a <= l && r <= b) return node[k];
        ll vl = getmin(a, b, 2*k+1, l, (l+r)/2);
        ll vr = getmin(a, b, 2*k+2, (l+r)/2, r);
        return min(vl, vr);
    }

};

int main() {
    //cout.precision(10);
    LazySegmentTree seg, forward, backward;
    ll N, Q, A, B;
    cin >> N >> Q >> A >> B;
    for(int i = 1; i <= N; i++) {
        forward.add(i, i + 1, i);
        backward.add(i, i + 1, (N - i));
    }
    seg.add(A, A+1, -INF);
    forward.add(A, A+1, -INF);
    backward.add(A, A+1, -INF);
    //cerr << backward.getmin(1, 3) << endl;
    ll before = B;
    while(Q--) {
        ll x;
        cin >> x;
        ll val = backward.getmin(1, x) - N + x;
        //cerr << val << endl;
        val = min(val, forward.getmin(x, N + 1) - x);
        //cerr << val << endl;
        seg.add(1, N + 1, abs(x - before));
        forward.add(1, N + 1, abs(x - before));
        backward.add(1, N + 1, abs(x - before));
        ll beforeval = seg.getmin(before, before + 1);
        //cerr << seg.getmin(before, before + 1) << endl;
        seg.add(before, before + 1, val - beforeval);
        //cerr << seg.getmin(before, before + 1) << endl;
        forward.add(before, before + 1, val - beforeval);
        //cerr << forward.getmin(before, before + 1) << endl;
        backward.add(before, before + 1, val - beforeval);
        //cerr << backward.getmin(before, before + 1) << endl;
        before = x;
        //cerr << endl << Q << endl;
        /*
        for(int i = 1; i <= N; i++) {
            cerr << i << " " << seg.getmin(i, i + 1) << endl;
        }
        */
    }
    cout << seg.getmin(1, N + 1) << endl;
    return 0;
}


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

string s;

int main() {
    cin.sync_with_stdio(0); cin.tie(0);
    cin >> s;
    int lc = 0, rc = 0;
    for (auto& i : s) {
        if (i == 'S') lc++;
        else {
            if (lc) lc--;
            else rc++;
        }
    }
    cout << lc + rc;
}

#include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i < b; i++)
#define Rep(i, a, b) for(int i = a; i <= b; i++)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define P pair<ll, ll>
using ll = long long;
#define ALL(v) (v).begin(),(v).end()
#define endl "\n"
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define co(x) cout << x << endl
#define coel cout << endl
#define pb push_back
#define sz(v) ((ll)(v).size())
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
#define pp pair<ll, pair<ll, ll>>

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

int main() {
    string s; cin >> s;
    bool f = true;
    while(f){
        bool f = true;
        rep(i, 0, sz(s)-5){
            if(s[i]=='S'&&s[i+1]=='S'&&s[i+2]=='S'&&
            s[i+3]=='T'&&s[i+4]=='T'&&s[i+5]=='T'){
                s.erase(i, 6);
                f = false;
                i -= 5;
            }
        }
        rep(i, 0, sz(s)-1){
            if(s[i] == 'S' && s[i+1] == 'T'){
                s.erase(i, 2);
                f = false;
                i--;
            }
        }
        if(f) break;
    }
    co(sz(s));

    return 0;
}


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

int main(){
    int n,a[5001];
    int ans;
    a[0]=0;
    while(cin>>n&&n){
        ans = INT_MIN;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            a[i] += a[i-1];
        }
        for(int i=1;i<=n;i++){
            for(int j=i;j<=n;j++){
                ans = max(ans,a[j]-a[i-1]);
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

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

int main()
{
    for (int n; cin >> n && n;) {
        vector<int> sequence(n);
        int sum = 0;
        int max_val = numeric_limits<int>::min();
        for (int x, i = 0; i < n && cin >> x; i++) {
            if (sum < 0)
                sum = x;
            else
                sum += x;
            max_val = max(max_val, sum);
        }
        cout << max_val << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const int mod=1000000007;
const int INF=1001001001;

int main() {
  int N;
  cin>>N;
  string s,t;
  cin>>s>>t;
  for(int i=N;i>=0;i--){
    string v=s;
    reverse(v.begin(),v.end());
    string r=v.substr(0,i);
    reverse(r.begin(),r.end());
    if(r==t.substr(0,i)){cout<<2*N-i<<endl;return 0;}
  }
  cout<<2*N<<endl;

  return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

typedef pair<int, int> P;

vector<int> G[100000];
int depth[100000];
bool ok[100000];
P depth_pair[100000];

void dfs_depth(int v, int d){
    depth[v] = d;
    for(int i = 0; i < G[v].size(); i++){
        if(depth[G[v][i]] == -1) dfs_depth(G[v][i], d+1);
    }
}

void dfs_ok(int v){
    ok[v] = true;
    for(int i = 0; i < G[v].size(); i++){
        if(depth[G[v][i]] > depth[v] && !ok[G[v][i]]) dfs_ok(G[v][i]);
    }
}

int main(){
    int N, K;
    cin >> N >> K;
    int ans = 0;
    for(int i = 0; i < N; i++){
        depth[i] = -1;
        int a;
        cin >> a;
        a--;
        if(i == 0){
            if(a != 0) ans++;
        }else{
            G[a].push_back(i);
            G[i].push_back(a);
        }
    }
    //cout << ans << endl;
    dfs_depth(0, 0);
    for(int i = 0; i < N; i++) depth_pair[i] = P(depth[i], i);
    sort(depth_pair, depth_pair+N, greater<P>());
    for(int i = 0; i < N; i++){
        if(depth_pair[i].first <= K) break;
        if(!ok[depth_pair[i].second]){
            ans++;
            int cur = depth_pair[i].second;
            //cout << depth_pair[i].second << endl;
            //K-1個上まで
            for(int j = 0; j < K-1; j++){
                for(int k = 0; k < G[cur].size(); k++){
                    if(depth[G[cur][k]] < depth[cur]){
                        cur = G[cur][k];
                        break;
                    }
                }
            }
            dfs_ok(cur);
        }
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int ans = 0;
  int minimum = N;
  for(int i = 0; i < N; i++) {
    int P;
    cin >> P;
    if(P <= minimum) {
      minimum = P;
      ans++;
    }
  }
  cout << ans << endl;
}

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

#define FOR(i,s,e) for(int i=(s);(i)<(int)(e);++(i))
#define REP(i,e) FOR(i,0,e)
#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define ppb(x) pop_back(x)
#define mp make_pair
#define mt make_tuple
#define t0(t) get<0>((t))
#define t1(t) get<1>((t))
#define t2(t) get<2>((t))

typedef long long ll;
const int N = 200000;
int n;
int p[N];

int main() {
  fscanf(stdin, "%d ", &n);
  int res = 0, m = N;
  REP(i,n) {
    fscanf(stdin, "%d ", p+i);
    m = min(m, p[i]);
    if (p[i] <= m) res++;
  }
  fprintf(stdout, "%d\n", res);
  return 0;
}


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

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define REP(i, a, b) for(ll i = a; i < b; i++)
#define out(x) std::cout << x << '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define sz(x) ((int)(x).size())
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; }
const int dx[4] = { 1, 0, -1,  0 };
const int dy[4] = { 0, 1,  0, -1 };
const int INF = INT_MAX / 2;
const ll  inf = LLONG_MAX / 2;
const int mod = 1000000007;
const int MAX_N = 101010;
const long double PI = acos(-1);

vector<int> eratos(int max_n) {
	vector<int> is_prime(max_n, 1);
	is_prime[0] = is_prime[1] = 0;
	for (int i = 2; i < max_n; i++) {
		if (!is_prime[i]) continue;
		for (int j = i * 2; j < max_n; j += i) {
			is_prime[j] = 0;
		}
	}
	return is_prime;
}

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

	int n;
	cin >> n;

	vector<int> prime = eratos(55555);
	vector<int> ans;
	for (int i = 0; i < sz(prime); i++)
	{
		if (prime[i]) {
			if (i % 10 == 1) {
				ans.push_back(i);
			}
		}
	}

	rep(i, n) {
		cout << ans[i] << " ";
	}
	cout << endl;

	return 0;
}

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

int main(void)
{
    const double PI=3.1415926535897932;
    int r; cin >> r;
    double l = (double) (2 * r) * PI;
    cout << fixed << setprecision(15) << l << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
int main(){
    ll h,w;cin >> h >>w;
    vvl g(h,vl(w));
    rep(i,h){
        rep(j,w){
            cin >> g[i][j];
            g[i][j]%=2;
        }
    }
    vector<pair<pl,pl>> ans;
    rep(i,h){
        if(i%2==0){
            rep(j,w-1){
                if(g[i][j]){
                    ans.pb({{i,j},{i,j+1}});
                    g[i][j+1]++;
                    g[i][j+1]%=2;
                }
            }
            if(g[i][w-1]){
                if(i+1<h){
                    ans.pb({{i,w-1},{i+1,w-1}});
                    g[i+1][w-1]++;
                    g[i+1][w-1]%=2;
                }
            }
        }
        else{
            per(j,w-1){
                if(g[i][j+1]){
                    ans.pb({{i,j+1},{i,j}});
                    g[i][j]++;
                    g[i][j]%=2;
                }
            }
            if(g[i][0]){
                if(i+1<h){
                    ans.pb({{i,0},{i+1,0}});
                    g[i+1][0]++;
                    g[i+1][0]%=2;
                }
            }
        }
    }
    cout << ans.size() <<endl;
    for(auto p:ans){
        cout << p.fi.fi+1 <<" " << p.fi.se+1 <<" "<<p.se.fi+1 << " " << p.se.se+1 <<endl;
    }
}

// #pragma GCC target("avx2")
#pragma GCC optimize("O3", "unroll-loops")

// #include <bits/extc++.h>
// using namespace __gnu_pbds;

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

#define int long long
#define double long double
// template <typename T>
// using pbds_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using pii = pair<int, int>;
template<typename T>
using prior = priority_queue<T, vector<T>, greater<T>>;
template<typename T>
using Prior = priority_queue<T>;

#define X first
#define Y second
#define ALL(x) (x).begin(), (x).end()
#define eb emplace_back
#define pb push_back

#define fastIO() ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define RANDOM() random_device __rd; \
                 mt19937 __gen = mt19937(__rd()); \
                 uniform_int_distribution<int> __dis(1, 1E8); \
                 auto rnd = bind(__dis, __gen)

const int INF = 1E18;
const int mod = 1E9 + 7;
const int maxn = 100 + 5;

int32_t main() {
    fastIO();

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

    int vec[n][m];
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) cin >> vec[i][j];
    }

    vector<pair<pii, pii>> op;
    for (int i = 0; i < n; ++i) {
        if (i < n-1) {
            for (int j = 0; j < m; ++j) {
                if (vec[i][j] & 1) {
                    op.pb({{i+1, j+1}, {i+2, j+1}});
                    ++vec[i+1][j];
                }
            }
        }
        else {
            for (int j = 0; j < m-1; ++j) {
                if (vec[i][j] & 1) {
                    op.pb({{i+1, j+1}, {i+1, j+2}});
                    ++vec[i][j+1];
                }
            }
        }
    }

    cout << op.size() << "\n";
    for (auto x : op) cout << x.X.X << " " << x.X.Y << " " << x.Y.X << " " << x.Y.Y << "\n";

    return 0;
}


#include <iostream>
#include <vector>
using namespace std;
int main(void){
    int n;
    cin >> n;
    vector<double> a(n);
    double sum = 0;
    for(int i = 0;i < n;i++){
          cin >> a[i];
          sum += a[i];
    }
    double ave = sum / n;
    double t = 101;
    int ans = -1;
    for(int i = a.size() - 1;i >= 0;i--){
          if(t >= abs(ave - a[i])){
                 t = abs(ave - a[i]);
                 ans = i;
          }
    }
    cout << ans << endl;

}

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>

using namespace std;
using LLONG = long long;
const LLONG MOD = 1000000007;

int main()
{
    int N; cin >> N;
    vector<double> as(N); for (auto &ai : as) cin >> ai;
    const double avg = accumulate(as.begin(), as.end(), 0.0) / static_cast<double>(N);
    map<double, int> diff2IndexMap;
    for (int i = N - 1; i >= 0; --i)
    {
        diff2IndexMap[abs(as[i] - avg)] = i;
    }
    cout << diff2IndexMap.begin()->second << endl;
}


#include<cstdio>
using namespace std;
#define LL long long
#define mod 998244353
const int NN=1000000+117;
LL fact[NN]={};
LL rev[NN]={};
LL comp(int m,int n){
	return fact[n]*rev[m]%mod*rev[n-m]%mod;
}
LL ksm(LL a,LL b){
	LL ret=1;
	while(b){
		if(b&1){
			(ret*=a)%=mod;
		}
		(a*=a)%=mod;
		b>>=1;
	}
	return ret;
}
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int top=n+m;
	fact[0]=1;
	for(int i=1;i<=top;++i)
		fact[i]=fact[i-1]*i%mod;
	rev[top]=ksm(fact[top],mod-2);
	for(int i=top;i>=1;--i)
		rev[i-1]=rev[i]*i%mod;
	if(n<m)m^=n^=m^=n;
	LL ans=0;
	for(int i=1;i<=m;++i){
		ans+=comp(i,2*i)*comp(m-i,n+m-2*i)%mod;
		ans%=mod;
	}
	ans=ans*rev[2]%mod*rev[m+n]%mod*fact[n]%mod*fact[m]%mod;
	ans+=n;
	ans%=mod;
	printf("%lld\n",ans);
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;

#ifdef LOCAL
	#define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr);
#else
	#define eprintf(...) 42
#endif

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template<typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
	return (ull)rng() % B;
}

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second

clock_t startTime;
double getCurrentTime() {
	return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}

const ll MOD = 998244353;
ll add(ll x, ll y) {
	x += y;
	if (x >= MOD) return x - MOD;
	return x;
}
ll sub(ll x, ll y) {
	x -= y;
	if (x < 0) return x + MOD;
	return x;
}
ll mult(ll x, ll y) {
	return (x * y) % MOD;
}
ll bin_pow(ll x, ll p) {
	if (p == 0) return 1;
	if (p & 1) return mult(x, bin_pow(x, p - 1));
	return bin_pow(mult(x, x), p / 2);
}
ll rev(ll x) {
	return bin_pow(x, MOD - 2);
}

const int N = (int)1e6 + 77;
ll f[N], rf[N];

ll getC(int n, int k) {
	if (k < 0 || k > n) return 0;
	return mult(f[n], mult(rf[k], rf[n - k]));
}

int main()
{
	startTime = clock();
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	f[0] = 1;
	for (int i = 1; i < N; i++)
		f[i] = mult(f[i - 1], i);
	rf[N - 1] = rev(f[N - 1]);
	for (int i = N - 1; i > 0; i--)
		rf[i - 1] = mult(rf[i], i);

	int n, m;
	scanf("%d%d", &n, &m);
	ll ans = 0;
	for (int k = 1; k <= min(n, m); k++)
		ans = add(ans, mult(getC(2 * k, k), getC(n + m - 2 * k, n - k)));
	ans = mult(ans, rev(mult(2, getC(n + m, m))));
	ans = add(ans, max(n, m));
	printf("%lld\n", ans);

	return 0;
}


#include <iostream>
using namespace std;

bool Include3(int n){
    if(n%10==3){
        return true;
    }
    n/=10;
    if(n){
      return Include3(n);
    }
    return false;
}
void CheckNum(int n){
    if(n%3==0){
        cout << " " << n;
        return;
    }
    if(n>9){
        if(Include3(n)){
            cout << " " << n;
        }
    }
}

int main(void){
    int n;
    cin >> n;
    for(int i=1;i<=n;i++){
       CheckNum(i);
    }
    cout << endl;
}

#include <iostream>
#include <math.h>

using namespace std;

int main() {

	int n;
	cin >> n;

	for (int i = 1; i <= n;i++) {

		if (i % 3 == 0) {
			cout << " " << i;
		}
		else {

			for (int j = 0; j < 4;j++) {

				int n;
				int m;

				n = pow(10, j + 1);
				m = n / 10;

				if (j == 0 && i % n == 3) {
					cout << " " << i;
					break;
				}else if (j != 0 && (i % n) / m == 3) {
					cout << " " << i;
					break;
				}
			}

		}
	}

	cout << endl;

	//cin >> /*変数他*/;
	//cout << /*文字列*/ << endl;
	return 0;
}


#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int n,h,w,res1,res2;
signed main(){
	cin>>n>>h>>w;
	res1=n-h;
	res1++;
	res2=n-w;
	res2++;
	res1=res1*res2;
	cout<<res1<<endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); i++)
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
using PP = pair<int,pair<int,int>>;
using T = tuple<string,int,int>;
const string EMP = " ";
const ll INF = 1LL<<60;
const ll MOD = 1000000007;
const double PI = acos(-1);

int main(){
  int n,h,w; cin >> n >> h >> w;
  int ans = 0;
  for(int i = 0; i < n; i++){
    for(int j = 0; j < n; j++){
      if(h+i-1 >= n) break;
      if(w+j-1 >= n) break;
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
//#include "MyMath.h"
//#include "MyDisjointset.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <functional>
#include <stdio.h>
using namespace std;
typedef pair<int, int> P;
const int INF = 1000000000;
struct edge {
	int from, to, cost;
};
int main() {
	int v, e, r; cin >> v >> e >> r;
	vector <edge> ed;
	for (int i = 0; i < e; i++) {
		int s, t, co; cin >> s >> t >> co;
		edge e1; e1.from = s; e1.to = t; e1.cost = co;
		ed.push_back(e1);
	}
	int *d = new int[v];
	fill(d, d + v, INF);
	d[r] = 0;
	while (true) {
		bool  update = false;
		for (int i = 0; i < ed.size(); i++) {
			edge ex = ed[i];
			if (d[ed[i].from] != INF && d[ed[i].to] > d[ed[i].from] + ex.cost) {
				d[ed[i].to] = d[ed[i].from] + ex.cost;
				//cout << "from " << d[ed[i].from] << " to " << d[ed[i].to] << " cost " << ex.cost << endl;
				update = true;
			}
		}
		if (!update)  break;
	}
	for (int i = 0; i < v; i++) {
		if (d[i] == INF) cout << "INF" << endl;
		else  cout << d[i] << endl;
	}
	delete[] d;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100005;
struct sj{
	int a,l,r;ll x;
	inline sj(){}
	inline sj(int a,int l,int r,ll x):a(a),l(l),r(r),x(x){}
	inline bool operator<(const sj &t)const{return x>t.x;}
}d[N];
inline void cmax(ll &a,ll b){if(a<b)a=b;}
inline ll Y(int i,ll k){
	ll a=d[i].a;
	if(k<=a)return k*d[i].l;
	else    return k*d[i].r+a*(d[i].l-d[i].r);
}
int main(){
	int n,i,a,l,r,x;
	ll L=0ll,R,M,S,g=0ll,t1,t2;
	scanf("%d%d",&n,&x);R=(S=(t1=((ll)x))*n)-1ll;
	for(i=1;i<=n;++i){
		scanf("%d%d%d",&a,&l,&r);t2=a;
		d[i]=sj(a,l,r,t1*r+t2*(l-r));
		g+=l*t2;
	}
	sort(d+1,d+n+1);
	while(L<=R){
		M=(L+R)>>1;
		l=M/x;
		r=M%x;
//		printf("M=%lld l=%d r=%d",M,l,r);
//		for(i=1;i<=n;++i)printf(" (%d,%d)",d[i].x,Y(i,r));
//		printf("\n");
		t1=-1000000000000ll;t2=0ll;
		for(i=1;i<=l;++i){
			t2+=d[i].x;
			cmax(t1,Y(i,r)-d[i].x);
		}
		t1+=d[i].x;
		for(   ;i<=n;++i)cmax(t1,Y(i,r));
		if(t1+t2>=g)R=(S=M)-1ll;else L=M+1ll;
	}
	printf("%lld",S);
	return 0;
}


#include <bits/stdc++.h>
#define rep2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) rep2(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
using namespace std;
using ll = long long;
template <typename T>
T sq(T x) {
  return x * x;
}

signed main() {
  int R, G;
  cin >> R >> G;
  cout << 2. * G - R << endl;
  return 0;
}

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

int main() {
	int r, g;
	cin >> r;
	cin >> g;
	cout << g - r + g << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(int i = (a); i >= (b); --i)
#define RI(i,n) FOR(i,1,(n))
#define REP(i,n) FOR(i,0,(n)-1)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define sz(w) (int) w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int inf = 1e9 + 5;
const int nax = 105;

char sl[nax][nax];
// int pref[nax][nax];
int dp[nax][nax][nax], old[nax][nax][nax];
int fixX[nax][nax], fixY[nax][nax];

/*int getSum(int x1, int y1, int x2, int y2) {
	if(x1 > x2) return 0;
	if(y1 > y2) return 0;
	if(x1 == x2) return fixX[x1][y2] - (y1 ? fixX[x1][y1-1] : 0);
	if(y1 == y2) return fixY[y1][x2] - (x1 ? fixY[y1][x1-1] : 0);
	assert(false);
}*/

int sumY(int y, int x1, int x2) {
	if(x1 > x2) return 0;
	maxi(x1, 0);
	mini(x2, nax - 1);
	return fixY[y][x2] - (x1 ? fixY[y][x1-1] : 0);
}
int sumX(int x, int y1, int y2) {
	if(y1 > y2) return 0;
	maxi(y1, 0);
	mini(y2, nax - 1);
	return fixX[x][y2] - (y1 ? fixX[x][y1-1] : 0);
}

int main() {
	int h, w;
	scanf("%d%d", &h, &w);
	int ex, ey;
	REP(y, h) {
		scanf("%s", sl[y]);
		REP(x, w) {
			if(x) fixY[y][x] += fixY[y][x-1];
			if(y) fixX[x][y] += fixX[x][y-1];
			//if(x) pref[x][y] += pref[x-1][y];
			//if(y) pref[x][y] += pref[x][y-1];
			//if(x && y) pref[x][y] -= pref[x-1][y-1];
			if(sl[y][x] == 'E') {
				ex = x;
				ey = y;
			}
			else if(sl[y][x] == 'o') {
				//++pref[x][y];
				++fixX[x][y];
				++fixY[y][x];
			}
		}
	}
	int ans = 0;

	REP(a,nax)REP(b,nax)REP(c,nax) dp[a][b][c] = old[a][b][c] = -inf;

	dp[ex][ey][ey] = 0;

	FORD(x1, ex, 0) {
		if(x1 != ex) REP(a,nax)REP(b,nax)REP(c,nax) {
			old[a][b][c] = dp[a][b][c];
			dp[a][b][c] = -inf;
		}
		FOR(x2, ex, w - 1) FORD(y1, ey, 0) FOR(y2, ey, h - 1) {
			int & me = dp[x2][y1][y2];
			//if(y2 < h - ey + y1) {
			//	puts("a");
			if(y1 >= (y2 - ey))
				maxi(me, dp[x2][y1+1][y2] + sumY(y1, max(x1, x2 - ex), min(x2, w - 1 - (ex - x1))));
			if(y2 < h - (ey - y1))
				maxi(me, dp[x2][y1][y2-1] + sumY(y2, max(x1, x2 - ex), min(x2, w - 1 - (ex - x1))));

			//if(x2 < w - ex + x1) {
			//	puts("b");
			if(x2 < w - (ex - x1))
				maxi(me, (x2 ? dp[x2-1][y1][y2] : 0) + sumX(x2, max(y1, y2 - ey), min(y2, h - 1 - (ey - y1))));
			if(x1 >= (x2 - ex))
				maxi(me, old[x2][y1][y2] + sumX(x1, max(y1, y2 - ey), min(y2, h - 1 - (ey - y1))));

			//printf("%d,%d,%d,%d -> %d\n", x1, x2, y1, y2, me);
			maxi(ans, me);
		}
	}

	printf("%d\n", ans);

	/*FOR(x1, 0, ex) {
		int x2 = ex - 1;
		while(x2 + 1 < w && x2 + 1 + (ex - x1) < w) ++x2;
		if(x2 == ex - 1) continue;
		FOR(y1, 0, ey) {
			int y2 = ey - 1;
			while(y2 + 1 < h && y2 + 1 + (ey - y1) < h) ++y2;
			if(y2 == ey - 1) continue;
			int maybe = pref[x2][y2];
			if(x1) maybe -= pref[x1-1][y2];
			if(y1) maybe -= pref[x2][y1-1];
			if(x1 && y1) maybe += pref[x1-1][y1-1];
			//printf("%d %d %d %d\n", x1, x2, y1, y2);
			maxi(ans, maybe);
		}
	}
	printf("%d\n", ans); */
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define int long long
namespace yspm{
	inline int read()
	{
		int res=0,f=1; char k;
		while(!isdigit(k=getchar())){if(k=='-') f=-1;}
		while(isdigit(k)){res=res*10+k-'0'; k=getchar();}
		return res*f;
	}
	const int N=2e3+10;
	int n,m,T,sum1[N][N],g[N][N];
	int lin[N][N],row[N][N],sum2[N][N];
	char s[N];
	signed main()
	{
		n=read(); m=read(); T=read();
		for(int i=1;i<=n;++i)
		{
			scanf("%s",s);
			for(int j=1;j<=m;++j) g[i][j]=s[j-1]-'0';
		}
		for(int i=1;i<=n;++i)
		{
			for(int j=1;j<=m;++j)
			{
				sum1[i][j]=sum1[i-1][j]+sum1[i][j-1]-sum1[i-1][j-1];
				sum2[i][j]=sum2[i-1][j]+sum2[i][j-1]-sum2[i-1][j-1];
				if(g[i][j]) ++sum2[i][j];
				row[i][j]=row[i][j-1]; lin[i][j]=lin[i-1][j];
				if(g[i][j])
				{
					if(g[i-1][j]) ++sum1[i][j],++row[i][j];
					if(g[i][j-1]) ++sum1[i][j],++lin[i][j];
				}
			}
		}
		int x1,y1,x2,y2;
		while(T--)
		{
			x1=read(); y1=read(); x2=read(); y2=read();
			int tmp=sum1[x2][y2]-sum1[x2][y1-1]-sum1[x1-1][y2]+sum1[x1-1][y1-1];
			tmp-=lin[x2][y1]-lin[x1-1][y1];
			tmp-=row[x1][y2]-row[x1][y1-1];
			int ans=sum2[x2][y2]-sum2[x2][y1-1]-sum2[x1-1][y2]+sum2[x1-1][y1-1]-tmp;
			printf("%lld\n",ans);
		}
		return 0;
	}
}
signed main()
{
	yspm::main();
	return 0;
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
#define ll long long
#define ld long double
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repo(i,n) for(int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define uniq(v) v.erase(unique(v.begin(),v.end()),v.end())
#define lb(v,x) (lower_bound(v.begin(),v.end(),x)-v.begin())
#define ub(v,x) (upper_bound(v.begin(),v.end(),x)-v.begin())
using Pair = pair<ll,pair<int,int>>;
#define pq priority_queue<Pair, vector<Pair>, greater<Pair>>
const ll mod=1000000007;
//const ll mod=998244353;
const ld pi=acos(-1.0);
const ll INF = 1LL<<61;
template<class T>bool chmax(T &a, const T &b) {
  if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) {
  if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
//intの最大値2147483647 ≒ 2×10^9
//long longの最大値9223372036854775807 ≒ 9×10^18
//'大文字'+=32;    で小文字に
//  cout << fixed << setprecision (20);   小数点以下2０桁まで
//実行時間制約2秒では２×10^8回くらいまで計算できる

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

  ll n,m,v,p;
  cin>>n>>m>>v>>p;

  vector<ll> q(n);
  rep(i,n){
    cin>>q[i];
  }

  sort(all(q));
  reverse(all(q));

//————————————————二分探索——————————————————————————

  int ng = n;
  int ok = p-1;
  while (abs(ok - ng) > 1) {
    int mid = (ng + ok) / 2;

    ll x=q[mid]+m;

    if(x<q[p-1]){
      ng=mid;
      continue;
    }

    int cnt=n-mid+p-1;
    ll hyo=m*cnt;

    for(int i=p-1;i<mid;i++){
      hyo+=x-q[i];
    }

    (m*v<=hyo? ok : ng) = mid;
  }

  cout << ok+1 << endl;

//——————————————————————————————————————————

}

#include<bits/stdc++.h>
using namespace std;
template<typename T>inline T read(){
	T f=0,x=0;char c=getchar();
	while(!isdigit(c)) f=c=='-',c=getchar();
	while(isdigit(c)) x=x*10+c-48,c=getchar();
	return f?-x:x;
}
#define int long long
namespace run{
	const int N=2e5+9;
	int a[N],m,v,p,n,ans,sum[N];
	int main(){
		n=read<int>(),m=read<int>(),v=read<int>(),ans=p=read<int>();
		for(int i=1;i<=n;i++) a[i]=read<int>();
		sort(a+1,a+n+1);
		for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i];
	//	for(int i=1;i<=n;i++) cout<<a[i]<<" ";cout<<endl;
		for(int i=1;i<=n-p;i++){
			if(m+a[i]<a[n-p+1]) continue;
			else{
				int tmp=(p-1+i)*m+((n-p+1-i)*(a[i]+m)-(sum[n-p+1]-sum[i]));
			//	cout<<tmp<<endl;
				if(tmp>=m*v) ans++;
			}
		}
		printf("%lld\n",ans);
		return 0;
	}
}
#undef int
int main(){
#ifdef my
	freopen(".in","r",stdin);
	freopen(".out","w",stdout);
#endif
	return run::main();
}


#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define MIN(a,b) ((a)>(b)? (b): (a))
#define MAX(a,b) ((a)<(b)? (b): (a))

const long long INF = 1LL << 60;
typedef unsigned long long ll;
const long long MOD = 1000000000 + 7;

int main(){
    ll n;
    cin >> n;
    vector<ll> sq;
    ll cnt = 1;
    while (cnt * cnt <= n)
    {
        sq.push_back(cnt*cnt);
        cnt++;
    }
    sort(sq.begin(), sq.end());
    cout << sq[sq.size() - 1] << endl;
    return 0;
}

#include<bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define PI acos(-1)
#define pcnt __builtin_popcountll
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define sz(x) (int)(x).size()
#define v(T) vector<T>
#define vv(T) v(v(T))
#define fi first
#define se second

using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;

template<typename T>inline istream& operator>>(istream&i,v(T)&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) {if (a > b) { a = b; return true; } return false; }

ll INF = 1001001001;
ll LINF = 1001001001001001001ll;

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

    string s;
    cin>>s;
    ll n = sz(s);
    reverse(rng(s));
    s.push_back('0');
    vvl dp(n + 2, vl(2, LINF));
    dp[0][0] = 0, dp[0][1] = 0;
    rep(i, n + 1) rep(j, 2) rep(k, 2) {     // j:繰り上がりをする k:繰り上がりされた
        ll c = s[i] - '0';
        if (i > 0 && k) c++;
        if (c == 10 && j == 0) continue;
        if (c == 0 && j == 1) continue;
        if (j) chmin(dp[i + 1][j], dp[i][k] + (10 - c) % 10);
        else chmin(dp[i + 1][j], dp[i][k] + c);
    }
    cout<<min(dp[n + 1][0], dp[n + 1][1])<<endl;
}


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

int main() {
  int n;
  cin>>n;
  vector<int> a(n);
  for(int i=0;i<n;i++){
    cin>>a.at(i);
  }
  sort(a.begin(),a.end());
  int flag=0;
  for(int i=0;i<n-1;i++){
    if(a.at(i)==a.at(i+1)){
      flag=1;
    }
  }
  if(flag==0){
    cout<<"YES";
  }else{
    cout<<"NO";
  }
}

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

int main() {
  int n;
  cin >>n;
  map<int,int>mp;
  for(int i=0;i<n;i++){
    int a;
    cin>>a;
    mp[a]++;
    mp[a]%=2;
  }
  long long kotae=0;
  auto begin=mp.begin(),end=mp.end();
  for(auto iter=begin;iter!=end;iter++){
    int x=iter->second;
    if(x==1)kotae++;
  }
  cout<<kotae<<endl;
  return 0;
}


#include <stdio.h>

int main(void){
  int n,i,j,k;
  scanf("%d",&n);
  int retsu[n];
  for (i = 1; i <= n; i++){
    k = n - i;
    scanf("%d",&retsu[k]);
  }
  for (j = 0; j < n; j++){
    if( j ) printf(" ");
    printf("%d",retsu[j]);
    }
    printf("\n");
 return 0;
}

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int main(void){
  int n;
  cin>>n;
  vector<ll> a(n),b(n),c(n);
  for(int i=0;i<n;i++){
    cin>>a[i];
  }
  for(int i=0;i<n;i++){
    cin>>b[i];
  }
  for(int i=0;i<n;i++){
    cin>>c[i];
  }
  sort(a.begin(),a.end());
  sort(c.begin(),c.end());
  ll ans=0;
  for(int i=0;i<n;i++){
    auto it=lower_bound(a.begin(),a.end(),b[i]);
    ll Adis=distance(a.begin(),it);
    it=upper_bound(c.begin(),c.end(),b[i]);
    ll Cdis=distance(it,c.end());
    ans+=(Adis*Cdis);
  }
  cout<<ans<<endl;
  return 0;
}


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

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

	int n; cin >> n;
	vector<int> a(n), b(n);
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	for (int i = 0; i < n; ++i)
		cin >> b[i];
	reverse(b.begin(), b.end());
	int start = -1, fin = -1;
	for (int i = 0; i < n; ++i) {
		if (a[i] == b[i]) {
			if (start == -1) start = i;
			fin = i;
		}
	}
	bool bad = 0;
	if (start != -1) {
		for (int i = 0, j = start; j <= fin; ++i) {
			if (i == start) i = fin + 1;
			if (i == n) {
				bad = 1;
				break;
			}
			if (a[i] != b[j] && b[j] != b[i]) {
				swap(b[i], b[j++]);
			}
		}
	}
	if (bad) {
		cout << "No" << '\n';
	}
	else {
		cout << "Yes" << '\n';
		for (int i = 0; i < n; ++i) {
			if (i) cout << " ";
			cout << b[i];
		}
		cout << '\n';
	}

	return 0;
}

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

int main(){
    int N;
    cin >> N;
    vector<int> A(N);
    vector<int> B(N);
    rep(i,N){
        cin >> A[i];
    }
    rep(i,N){
        cin >> B[i];
    }
    map<int,int> mpa;
    map<int,int> mpb;
    rep(i,N){
        mpa[A[i]]++;
        mpb[B[i]]++;
    }
    vector<int> a(N + 1, 0);
    vector<int> b(N + 1, 0);
    rep(i,N){
        a[A[i]] = i + 1;
    }
    rep(i,N){
        b[B[i]] = i + 1;
    }
    rep(i,N){
        a[i + 1] = max(a[i + 1], a[i]);
        b[i + 1] = max(b[i + 1], b[i]);
    }
    cout << endl;
    int max_sum = 0;
    int max_pointer = 1;
    for (int i = 1; i <= N; i++){
        if (max_sum < mpa[i] + mpb[i]){
            max_sum = mpa[i] + mpb[i];
            max_pointer = i;
        }
    }
    bool possible = true;
    vector<int> C(N);
    if (max_sum > N){
        possible = false;
    }
    else{
        int zurashi = -1 * N;
        rep(i,N){
            zurashi = max(zurashi, a[i + 1] - b[i]);
        }
        rep(i,N){
            C[(i + zurashi) % N] = B[i];
        }
    }
    if (!possible) cout << "No" << endl;
    else{
        cout << "Yes" << endl;
        rep(i,N){
            cout << C[i] << " ";
        }
        cout << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef long long ll;

int gi() {
	int x=0,o=1;char ch=getchar();
	while(!isdigit(ch)&&ch!='-') ch=getchar();
	if(ch=='-') o=-1,ch=getchar();
	while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
	return x*o;
}

struct dat {
	int l,r;
	bool operator<(const dat &A) const {
		return r<A.r;
	}
} a[N];

int n,pre1[N],suf1[N],pre2[N],suf2[N],ans;

int len(int l,int r) {
	if(l>r) return 0;
	return r-l+1;
}

int main() {
	cin>>n;
	for(int i=1;i<=n;i++) a[i].l=gi(),a[i].r=gi();
	sort(a+1,a+n+1);
	pre2[0]=1e9;
	for(int i=1;i<=n;i++) {
		pre1[i]=max(pre1[i-1],a[i].l);
		pre2[i]=min(pre2[i-1],a[i].r);
	}
	suf2[n+1]=1e9;
	for(int i=n;i;i--) {
		suf1[i]=max(suf1[i+1],a[i].l);
		suf2[i]=min(suf2[i+1],a[i].r);
	}
	for(int i=1;i<=n;i++) {
		int L=max(pre1[i-1],suf1[i+1]);
		int R=min(pre2[i-1],suf2[i+1]);
		ans=max(ans,a[i].r-a[i].l+1+len(L,R));
		if(i^n) ans=max(ans,len(pre1[i],pre2[i])+len(suf1[i+1],suf2[i+1]));
	}
	cout<<ans;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
const ll p=1000000007;
int main(){
  ll n;
  cin >> n;
  ll bb=0;
  ll k[n];
  rep(i,n){
    ll a,b;
    cin >> a >> b;
    bb+=b;
    k[i]=a+b;
  }
  sort(k,k+n);
  ll ans=0;
  rep(i,n-n/2){
    ans+=k[n-1-2*i];
  }
  cout << ans-bb << endl;
}


// g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<unordered_map>
#include<utility>
#include<cmath>
#include<random>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<random>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#define loop(i,a,b) for(long long i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define FOR(i,a) for(auto i:a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
#define show1d(v) {rep(_,v.size())cout<<" "<<v[_];cout<<endl;}
#define show2d(v) {rep(__,v.size())show1d(v[__]);}
using namespace std;
//kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
#define mt make_tuple
typedef tuple<int,int,int> tp;
typedef vector<tp> vt;
typedef vector<vt>vvt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
const double PI=acos(-1);
const double EPS=1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10;
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
#define yes cout<<"Yes\n"
#define no cout<<"No\n"

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

	int n,m,q;
	cin>>n>>m>>q;
	int out=0,mi=inf;
	vi a(n),b(m);
	rep(i,n)cin>>a[i],cmin(mi,a[i]);
	out+=mi;
	mi=inf;
	rep(i,m)cin>>b[i],cmin(mi,b[i]);
	out+=mi;

	rep(i,q){
		int x,y,z;
		cin>>x>>y>>z;
		cmin(out,a[x-1]+b[y-1]-z);
	}
	cout<<out<<endl;

}

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

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

  vector<int> refs(a);
  vector<int> renges(b);
  for(int i = 0; i < a; i++) {
     cin >> refs.at(i);
  }

  for(int i = 0; i < b; i++) {
     cin >> renges.at(i);
  }

  int ans = refs.at(0) + renges.at(0);
  for(int i = 0; i < m; i++) {
   int aIndex,bIndex,discount;
    cin >> aIndex >> bIndex >> discount;
    int tempAns = refs.at(aIndex - 1) + renges.at(bIndex - 1) - discount;

    if(tempAns < ans) {
      ans = tempAns;
    }
  }

  std::sort(refs.begin(), refs.end());
  std::sort(renges.begin(), renges.end());

  int noDiscount = refs.at(0) + renges.at(0);

  if(noDiscount < ans) {
    cout << noDiscount << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}


#include<stdio.h>
#include<string.h>
void haichi(void);
void Block(int,int);
int c,d,x,y,xg,yg,M,Flag;
int MAP[101][101],MF[101][101];
int main(void)
{
	int w,h,xs,ys,n;
	while(1){
		Flag=0;
		memset(MAP,0,sizeof(MAP));
		memset(MF,0,sizeof(MF));
		scanf("%d %d",&w,&h);
		if(w == 0 && h == 0) break;
		scanf("%d %d %d %d",&xs,&ys,&xg,&yg);
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%d %d %d %d",&c,&d,&x,&y);
			haichi();
		}
		/*for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				printf("%d",MAP[i][j]);
			}
			printf("\n");
		}*/
		M = MAP[ys][xs];//
		if(MAP[ys][xs] == MAP[yg][xg])
			Block(xs,ys);
		if(Flag==1)
			printf("OK\n");
		else
			printf("NG\n");
	}
	return 0;
}

void haichi(void)
{
	if(d==0){
		MAP[y][x]=c;MAP[y][x+1]=c;MAP[y][x+2]=c;MAP[y][x+3]=c;
		MAP[y+1][x]=c;MAP[y+1][x+1]=c;MAP[y+1][x+2]=c;MAP[y+1][x+3]=c;
	}
	else if(d==1){
		MAP[y][x]=c;MAP[y][x+1]=c;
		MAP[y+1][x]=c;MAP[y+1][x+1]=c;
		MAP[y+2][x]=c;MAP[y+2][x+1]=c;
		MAP[y+3][x]=c;MAP[y+3][x+1]=c;
	}
}

void Block(int X,int Y)
{
	MF[Y][X] = 1;
	//printf("%d",M);
	if(X == xg && Y == yg)
		Flag = 1;
	if(MAP[Y-1][X] == M && MF[Y-1][X] == 0) Block(X,Y-1); //???
	if(MAP[Y+1][X] == M && MF[Y+1][X] == 0) Block(X,Y+1); //???
	if(MAP[Y][X-1] == M && MF[Y][X-1] == 0) Block(X-1,Y); //???
	if(MAP[Y][X+1] == M && MF[Y][X+1] == 0) Block(X+1,Y); //???
}

#include<iostream>
using namespace std;
#define MAX 100
#define BLANK 0
#define LONG 4
#define SHORT 2
#define HORIZONTAL 0
#define VERTICAL 1
#define VISITED 9

int field[MAX+2][MAX+2];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
void rec(int x,int y,int c)
{
  int i;
  if(field[x][y]!=c)return;
  field[x][y]=VISITED;
  for(i=0;i<4;i++)rec(x+dx[i],y+dy[i],c);
  return;
}

int main()
{
  int w,h,xs,ys,xg,yg,n,c,d,x,y;
  int i,j;
  while(cin>>w>>h>>xs>>ys>>xg>>yg>>n,w|h){
    for(j=1-1;j<=h+1;j++)for(i=1-1;i<=w+1;i++)field[i][j]=BLANK;
    while(n-->0){
      cin>>c>>d>>x>>y;
      if(d==HORIZONTAL)
	for(i=0;i<LONG;i++)for(j=0;j<SHORT;j++)field[x+i][y+j]=c;
      else
	for(i=0;i<SHORT;i++)for(j=0;j<LONG;j++)field[x+i][y+j]=c;
    }
    if(field[xs][ys]!=BLANK)rec(xs,ys,field[xs][ys]);
    puts(field[xg][yg]==VISITED?"OK":"NG");
  }
}


#include <iostream>
#include <vector>
using namespace std;
const int V_MAX = 100000;

struct LowLink
{
	const vector<vector<int>> &g;
	int V;
	vector<vector<int>> dfs_tree;
	vector<int> dfs_roots;
	int pre_order[V_MAX];
	int p_ord = 0;
	int lowlink[V_MAX];
	bool articulation[V_MAX] = {};
	vector<pair<int, int>> bridge;

	LowLink(const vector<vector<int>> &g) : g(g), V(g.size()), dfs_tree(g.size()) {}

	void dfs(int v, int parent) {
		lowlink[v] = pre_order[v] = p_ord;
		p_ord++;
		for (int nv : g[v]) {
			if (nv == parent) continue;
			if (pre_order[nv] == -1) {
				dfs_tree[v].push_back(nv);
				dfs(nv, v);
				lowlink[v] = min(lowlink[v], lowlink[nv]);
			}
			lowlink[v] = min(lowlink[v], pre_order[nv]);
		}
	}

	void calc() {
		p_ord = 0;
		fill(pre_order, pre_order+V_MAX, -1);
		for (int i = 0; i < V; i++) {
			if (pre_order[i] != -1) continue;
			dfs_roots.push_back(i);
			dfs(i, -1);
		}
		for (int i = 0; i < V; i++) {
			for (int j : dfs_tree[i]) {
				if (pre_order[i] < lowlink[j]) bridge.push_back({i, j});
			}
		}
		for (int i = 0, x = 0; i < V; i++) {
			bool ok = false;
			if (x < dfs_roots.size() && dfs_roots[x] == i) {
				if (dfs_tree[i].size() > 1) ok = true;
				x++;
			} else {
				for (int j : dfs_tree[i]) {
					if (pre_order[i] <= lowlink[j]) ok = true;
				}
			}
			articulation[i] = ok;
		}
	}
};

int main() {
	int V, E;
	cin >> V >> E;
	vector<vector<int>> g(V);
	for (int i = 0; i < E; i++) {
		int a, b;
		cin >> a >> b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	LowLink L(g);
	L.calc();
	for (int i = 0; i < V; i++) {
		if (L.articulation[i]) cout << i << endl;
	}
	return 0;
}

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

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end

vector<int> g[101010];
int col[101010],cnt;
bool dfs(int v,int c){
   col[v]=c; cnt++; bool res=1;
   for(int t:g[v]){
      if(col[t]!=-1){
         if(col[t]+c!=1)res=0;
      }
      else if(!dfs(t,c^1))res=0;
   } return res;
}

int main(){
   int n,m; cin>>n>>m;
   rep(i,0,n)col[i]=-1;
   rep(i,0,m){
      int x,y; cin>>x>>y; x--; y--;
      g[x].push_back(y); g[y].push_back(x);
   }
   ll a=0,b=0,c=0;
   rep(i,0,n)if(col[i]==-1){
      cnt=0; bool f=dfs(i,0);
      if(cnt==1)a++;
      else if(f)b++; else c++;
   }
   cout<<(a*a+a*(n-a)*2)+(b+c)*(b+c)+b*b<<endl;
   return 0;
}


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

int main() {
    int n, cnt = 0; cin >> n;
    ll x; cin >> x;
    vector<ll> a(n);
    rep(i,n) cin >> a[i];
    sort(a.begin(), a.end());
    rep(i,n) {
        if (x >= a[i]) {
            if (i == n-1 && x == a[i]) cnt++;
            else if (i != n-1) {
                x -= a[i];
                cnt++;
            }
        }
        else break;
    }
    cout << cnt << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for (ll i=0; i<n; ++i)
#define all(c) begin(c),end(c)
#define PI acos(-1)
#define oo LLONG_MAX
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
/*

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

  ll N;
  cin >> N;
  vector<ll> A(N, 0);
  rep(i, N) cin >> A[i];
  ll high = A[0], ans = 0;
  rep(i, N){
    ans += max(high - A[i], 0LL);
    chmax(high, A[i]);
  }
  cout << ans << endl;
}


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

int main() {
  int n;cin>>n;
  int ans=0;
  for(int i=1;i<=n;i++){
    if(i*i>n)break;
    ans=max(ans,i*i);
  }
  cout<<ans<<endl;
}

#include <iostream>
using namespace std;

int main(void)
{
    int x; cin >> x;
    int ans = 0;
    ans += 1000 * (x / 500);
    x = x % 500;
    ans += 5 * (x / 5);
    cout << ans << endl;
}


/*
ID: anonymo14
TASK: wormhole
LANG: C++
*/
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pair<int,int>> vpii;

#define F first
#define S second
#define PU push
#define PUF push_front
#define PUB push_back
#define PO pop
#define POF pop_front
#define POB pop_back

#define REP(i,a,b) for(int i=a; i<=b; i++)

#define MOD 1000000007

void solve(int test_case) {
	ll a,b;
	cin>>a>>b;
	ll dc = __gcd(a,b);
	int cnt = 1;
	for(ll i=2;i*i<=dc;i++) {
		if(dc%i==0) {
			cnt++;
			while(dc%i==0)dc/=i;
		}
	}
	if(dc!=1)cnt++;
	cout<<cnt;
}

int main() {

	////// FILE BASED IO////
	//freopen("wormhole.in", "r", stdin);
	//freopen("wormhole.out", "w", stdout);
	///////////////

	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	//cin>>t;
	REP(i,1,t) {
		solve(i);
	}
	return 0;
}

#include <bits/stdc++.h>
#define PI 3.14159265359
#define NIL (-1)
#define LL long long
using namespace std;
const int64_t MOD = 1e9 + 7;
const int INF = INT_MAX;

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

	int a, b;
	a = 0;
	b = N;

	for (int i = 0; i < M; i++) {
		cin >> L >> R;
		a = max(a, L);
		b = min(b, R);
	}

	int ret = b - a;
	if (ret < 0) ret = 0;
	else ret++;

	cout << ret << endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef double db;
typedef vector<ll> VLL;
typedef vector<VLL> VVLL;
typedef pair<ll,ll> PLL;

#define REP(x,l,u) for(ll x = l; x < u; x++)
#define RREP(x,l,u) for(ll x = l; x >= u; x--)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define mst(x,v) memset(x, v, sizeof(x))
#define sz(x) (ll)x.size()

string to_string(string s) {return s;}
string to_string(char c) {string s = string(1, c);return s;}
template <typename A, typename B> string to_string(pair<A,B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A> string to_string(vector<A> v) { string s = "("; int first = 1; for (A a : v) { if (!first) { s += ", "; } first = 0; s += to_string(a); } s += ")"; return s; }
template <typename A> string to_string(set<A> v) { string s = "("; int first = 1; for (A a : v) { if (!first) { s += ", "; } first = 0; s += to_string(a); } s += ")"; return s; }

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

void in() {}
template <typename A> void in(A & x) { cin >> x; }
template <typename A, typename B> void in(pair<A,B> & x) { in(x.first); in(x.second); }
template <typename A> void in(vector<A> & x) { REP(i,0,(ll)x.size()) in(x[i]); }
template <typename Head, typename... Tail> void in(Head & H, Tail & ... T) {in(H); in(T...); }

#ifndef ONLINE_JUDGE
#define debug(...) do { cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__); } while (false)
#else
#define debug(...) do { } while (false)
#endif

const ll inf = (ll)1e18 + 5;
const ll mod = 1e9+7;

ll nax = 1005;
VLL p(nax*nax, 1);
VLL P;

void init() {
	p[0] = 0;
	p[1] = 0;

	for (ll x = 2; x < nax*nax; x++) {
		if (p[x] == 0) {
			continue;
		}

		P.push_back(x);

		for (ll f = x; x * f < nax * nax; f++) {
			p[x*f] = 0;
		}
	}
}

map<ll,ll> pf(ll x) {
    map<ll,ll> f;
    REP(i,0,sz(P)) {
        if (P[i] * P[i] > x) break;
        while (x % P[i] == 0) {
            f[P[i]]++;
            x /= P[i];
        }
    }
    if (x > 1) f[x]++;
    return f;
}

VLL F(100500, -1);
VLL INVF(100500, -1);

ll pow(ll a, ll b) {
	if (b == 0) return 1;
	ll h = pow(a, b/2);
	ll ans = b % 2 ? h * h % mod * a : h * h;
	ans %= mod;
	return ans;
}

ll f(ll x) {
	if (F[x] != -1) return F[x];
	if (x == 0) return 1;
	F[x] = f(x-1) * x % mod;
	return F[x];
}

ll invf(ll x) {
	if (INVF[x] != -1) return INVF[x];
	INVF[x] = pow(f(x), mod-2);
	return INVF[x];
}

ll ncr(ll n, ll r) {
	// n! / r! / (n-r)!
	ll ans = f(n);
	ans *= invf(r);
	ans %= mod;
	ans *= invf(n-r);
	ans %= mod;
	return ans;
}

void solve() {
	ll n, m;
	in(n, m);

	map<ll,ll> factors = pf(m);

	REP(i,0,n) f(i); // ease on the recursion

	ll ans = 1;
	for (PLL a : factors) {
		ans *= ncr(a.second + n - 1, a.second) % mod;
		ans %= mod;
	}
	cout << ans << endl;

}

signed main() {
	init();
	ll t = 1;
	REP(i,0,t) solve();
	return 0;
}

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
#define SIZE 200005
#define INF 1000000005LL
//#define INF 1e18
#define MOD 1000000007

using namespace std;
typedef long long int ll;
typedef pair <int,int> P;

const int MAX = 510000;

long long fac[MAX], finv[MAX], inv[MAX]; //n!, 1/n!, 1/n

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

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

map<ll,ll> prime_factor(ll n){
  map<ll,ll> res;
  for(ll i=2;i*i<=n;i++){
    while(n%i==0){
      ++res[i];
      n /= i;
    }
  }
  if(n!=1) res[n] = 1;
  return res;
}

int main(){
  ll n,m;
  cin >> n >> m;
  COMinit();
  ll ans=1;
  map<ll,ll> M = prime_factor(m);
  for(auto p : M){
    ll k = p.second;
    ans *= COM(n+k-1,k);
    ans %= MOD;
  }
  cout << ans << endl;

  return 0;
}


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

int main(){
	int n; cin>>n;
	n*=2;
	vector<int> v(n);
	rep(i,n) cin>>v[i];
	sort(rall(v));
	int cnt=0;
	for(int i=0; i<n; i+=2){
		cnt+=v[i+1];
	}
	cout<<cnt<<endl;
}

#include<iostream>
#include<vector>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<map>
#define INF 0xFFFFFF
#define ull unsigned long long
#define ll long long
#define ms(x,a) memset(x,a,sizeof(x))
#define endl "\n"
#define M(a,b)  (a+b)/2
#define L(x) 2*x
#define R(x) 2*x+1
using namespace std;
typedef pair<int,ll> ii;
typedef pair<int,ii> iii;
int Move[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};
/*
    nothing at all
*/
int scr(string s){
    int score=INF;
    for(int i=0;i<s.size()-1;i++){
        score=min(score,abs(s[i]-s[i+1]));
    }
    //cout<<s<<" "<<score<<endl;
    return score;
}
int main(){
    int n;
    cin>>n;
    int arr[2*n+10];
    for(int i=0;i<2*n;i++){
        cin>>arr[i];
    }
    sort(arr,arr+(2*n));
    int rzlt=0;
    for(int i=0;i<n;i++){rzlt+=arr[2*i];}
    cout<<rzlt;
    return 0;
}


#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,K,Q,A,B;
string S;
//const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
const ll INF = 1LL << 60;
typedef pair<ll,ll> P;

int main(){
    cin>>N>>M>>Q;
    vector<string> grid(N);
    rep(i,N) cin>>grid[i];
    mat edge(N, vec(M, 0)), num(N + 1, vec(M + 1, 0)), addr(N, vec(M,0)), addc(N, vec(M,0));
    rep(i,N){
        rep(j,M){
            bool blue = grid[i][j] == '1';
            if(i > 0) {
                edge[i][j] += edge[i-1][j] + (blue && grid[i-1][j] == '1');
                addc[i][j] = addc[i-1][j] + (blue && grid[i-1][j] == '1');
            }
            if(j > 0) {
                edge[i][j] += edge[i][j-1] + (blue && grid[i][j-1] == '1');
                addr[i][j] = addr[i][j-1] + (blue && grid[i][j-1] == '1');
            }
            if(i > 0 && j > 0) edge[i][j] -= edge[i-1][j-1];
            num[i+1][j+1] = num[i][j+1] + num[i+1][j] - num[i][j] + blue;
        }
    }
    vec x(2), y(2);
    rep(i,Q){
        rep(j,2) cin>>x[j]>>y[j];
        --x[0], --y[0];
        ll res = 0;
        rep(j,2) rep(k,2) res += (j^k ? -1 : 1) * num[x[j]][y[k]];
        --x[1], --y[1];
        rep(j,2) rep(k,2) res += (j^k ? 1 : -1) * edge[x[j]][y[k]];
        res -= addr[x[0]][y[1]] - addr[x[0]][y[0]];
        res -= addc[x[1]][y[0]] - addc[x[0]][y[0]];
        cout<<res<<endl;
    }
    /*rep(i,N){
        rep(j,M) cout<<edge[i][j]<<' ';
        cout<<endl;
    }*/
}


#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;
char c[55][55];
bool visited[55][55];
int d[55][55];
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
queue<P> Q;

int main()
{
	int H,W;
	cin >> H >> W;
	int cntW = 0;
	rep(i,H)
	{
		rep(j,W)
		{
			cin >> c[i][j];
			if (c[i][j] == '.') cntW++;
		}
	}
	rep(i,55)
	{
		rep(j,55)
		{
			d[i][j] = 1e8;
		}
	}
	d[0][0] = 0;
	Q.push(P(0,0));
	while(!Q.empty())
	{
		P q = Q.front();
		int f = q.first, s = q.second;
		Q.pop();
		if (visited[f][s]) continue;
		visited[f][s] = true;
		rep(i,4)
		{
			int xx = f+dx[i], yy = s+dy[i];
			if (xx >= 0 && xx < H && yy >= 0 && yy < W && c[xx][yy] == '.')
			{
				d[xx][yy] = min(d[xx][yy],d[f][s]+1);
				Q.push(P(xx,yy));
			}
		}
	}
	/*
	rep(i,H)
	{
		rep(j,W)
		{
			cout << d[i][j] << " ";
		}
		cout << endl;
	}
	*/
	if (d[H-1][W-1] == 1e8) cout << -1 << endl;
	else cout << cntW - d[H-1][W-1] - 1 << endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<lint> vlint;
typedef vector<vlint> vvlint;
typedef vector<bool> vbool;
typedef vector<int> vint;

#define inf 1e+9
#define endl "\n"
#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 rep_rev(i, n) for (int i = n-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()

#define pb push_back
#define eb emplace_back
#define Size(c) (int)(c).size()

#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))

template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;

template <class T, class S> inline bool chmax(T &a, S b) {
    if(a < b) {a = b;return 1;} return 0;
}
template <class T, class S> inline bool chmin(T &a, S b) {
    if(a > b) {a = b;return 1;} return 0;
}
template <class T>
void line(vector<T> &x){for (auto&& xi : x) cin >> xi;}

inline void IN(void){ return;}

template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){ cin >> first; IN(rest...); return;}

#define INT(...) int __VA_ARGS__; IN (__VA_ARGS__)
#define LINT(...) lint __VA_ARGS__; IN (__VA_ARGS__)
#define STR(...) string __VA_ARGS__; IN(__VA_ARGS__)

template <class T> void UNIQUE(vector<T> &x) {
    sort(all(x)); x.erase(unique(all(x)), x.end());
}
template <class T> void print_vec(T first, T end){
    for (auto i = first; i < end - 1; i++) cout << *i << " ";
    cout << *(end-1) << endl;
}

template<class... T>
void debug_print(T... args){
    vector<lint> tmp = initializer_list<lint>{args...};
    print_vec(all(tmp));
}
template<class T>
void print(T a){ cout << a << endl;}

vector<string> Yes = {"No", "Yes"};
vector<string> YES = {"NO", "YES"};

int ord(char x){ return (int)(x - 'a');}
char chr(lint x){ return (char)(x + (lint)('a'));}
lint mod = 1e9+7;
// lint mod =

lint sum(vlint a){lint ret = 0; for(auto&& v:a) ret += v; return ret;}
lint vmini(vlint a, lint &index){
    lint ret = LLONG_MAX;
    rep(i, Size(a)){ if (chmin(ret, a[i])) index = i;}
    return ret;
}
lint vmaxi(vlint a, lint &index){
    lint ret = -LLONG_MAX;
    rep(i, Size(a)){ if (chmax(ret, a[i])) index = i;}
    return ret;
}

lint vmin(vlint a){
    lint ret = LLONG_MAX; for (auto && v : a) chmin(ret, v); return ret;
}
lint vmax(vlint a){
    lint ret = -LLONG_MAX; for (auto && v : a) chmax(ret, v); return ret;
}

vlint base_3(int k){
    vlint ret; while (k > 0){ ret.pb(k % 3); k /= 3;} reverse(all(ret)); return ret;
}
map<lint, lint> trial_division(lint n){
    map<lint, lint> factor;
    lint tmp = (lint)(sqrt(n)) + 1;
    for (lint i = 2; i < tmp; ++i){
        while (n % i == 0){
            n /= i;
            factor[i] ++;
        }
    }
    if(n != 1) factor[n] ++;
    return factor;
}

lint dist(lint x1, lint y1, lint x2, lint y2){
    return (x1-x2) * (x1-x2) + (y1-y2) * (y1 - y2);
}

int main(){
    INT(h, w);
    char o;
    vector<vint> grid(h+2, vint(w+2, 0));
    int cnt = 0;
    rep1(i, h+1){
        rep1(j, w+1){
            cin >> o;
            if (o == '.'){
                grid[i][j] = 1;
                cnt ++;
            }
        }
    }
    queue<pair<int, int>> q;
    q.push({1, 1});
    vector<vint> dist(h+2, vint(w+2, -1));
    dist[1][1] = 0;
    int y, x, ny, nx;
    vint dx = {-1, 0, 0, 1};
    vint dy = {0, -1, 1, 0};
    while (!q.empty()){
        y = q.front().first;
        x = q.front().second; q.pop();
        rep(i, 4){
            ny = y + dy[i];
            nx = x + dx[i];
            if (dist[ny][nx] != -1 || grid[ny][nx] == 0){
                continue;
            }
            q.push({ny, nx});
            dist[ny][nx] = dist[y][x] + 1;
        }
    }
    if (dist[h][w] == -1){
        print(-1);
    }
    else{
        print(cnt - dist[h][w]-1);
    }

}


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

using namespace std;
using namespace atcoder;

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}

ll nextLong() { ll x; scanf("%lld", &x); return x;}

int main2() {
  int N = nextLong();
  int Q = nextLong();
  fenwick_tree<ll> fw(N+10);
  REP(i, N) {
    int x = nextLong();
    fw.add(i, x);
  }
  REP(qqq, Q) {
    int t = nextLong();
    if (t == 0) {
      int p = nextLong();
      int x = nextLong();
      fw.add(p, x);
    }
    else
    {
      int l = nextLong();
      int r = nextLong();
      ll ans = fw.sum(l, r);
      cout << ans << '\n';
    }
  }

  return 0;
}

int main() {

#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}

#include <iostream>
#include <stdio.h>
using namespace std;

double sum(int b[],int);
void bubble(int b[],int);

int main(){
    int a;
    cin >> a;
    int b[a];
    for (int i=0;i<a;i++)
        cin >> b[i];
    bubble(b,a);
    cout << b[0] << " " << b[a-1] << " " ;
    printf("%.0f\n",sum(b,a));
}

void bubble(int b[],int a){
    int x;
    for (int i=0;i< a-1 ;i++){
        for (int j=0;j<a-1 ;j++)
            if(b[j]>b[j+1]){
                x=b[j];
                b[j]=b[j+1];
                b[j+1]=x;
        }
    }
}

double sum(int b[], int a){
    double sum=0;
    for (int i=0;i<a;i++)
        sum += b[i];
    return sum;
}