#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt"
struct rect {
	int h;
	int w;
};
int main() {
	vector<pair<int, int>>ps;
	for (int y = 1; y <= 150; ++y) {
		for (int x =y+1; x <= 150; ++x) {
			ps.push_back(make_pair(y, x));
		}
	}
	sort(ps.begin(), ps.end(), [](const pair<int, int>&l, const pair<int, int>&r) {
		const int ldig = l.first*l.first + l.second*l.second;
		const int rdig = r.first*r.first + r.second*r.second;
		return ldig == rdig ? l.first < r.first : ldig < rdig;
	});
	while (1) {
		int h, w; cin >> h >> w;
		if (!h)break;
		pair<int, int>p(h, w);
		auto it = upper_bound(ps.begin(), ps.end(),p, [](const pair<int, int>&l, const pair<int, int>&r) {
			const int ldig = l.first*l.first + l.second*l.second;
			const int rdig = r.first*r.first + r.second*r.second;
			return ldig == rdig ? l.first < r.first : ldig < rdig;
		});
		cout << it->first << " " << it->second << endl;
	}
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> duo;

#define TT template<typename T>
TT T    sq(T x){ return x*x; }
TT int  ubnd(vector<T>& v,T x){ return upper_bound(v.begin(), v.end(), x) - v.begin(); }
TT int  lbnd(vector<T>& v,T x){ return lower_bound(v.begin(), v.end(), x) - v.begin(); }
TT void sort(T& v){ sort(v.begin(), v.end()); }
TT void revs(T& v){ reverse(v.begin(), v.end()); }
TT void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end()), v.end()); }
TT void show(T& v,char d=' ',char e='\n'){for(int i=0;i<v.size();++i)cout<<v[i]<<((i==v.size()-1)?e:d);}
TT void inpt(vector<T>& v,int n){ v.reserve(n);for(int i=0;i<n;++i){T x; cin>>x; v.push_back(x);} }
TT T    In(T& x){ cin >> x; return x; }

static inline void fast_io(){ ios::sync_with_stdio(0); cin.tie(0); }
static inline int in(){ int x; scanf("%d", &x); return x; }
static inline string instr(){ string x; cin >> x; return x; }
static const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
static const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};

struct Rect {
  int h, w, d;
  Rect(int h, int w) : h(h), w(w), d(sq(h) + sq(w)) {}
  bool operator<(const Rect& x) const {
    return d == x.d ? h < x.h : d < x.d;
  }
};

int main()
{
  vector<Rect> v;
  int h, w;
  for (int x = 1; x <= 150; x++){
    for (int y = 1; y < x; y++){
      v.push_back(Rect(y, x));
    }
  }
  sort(v);

  while (h = in(), w = in(), h || w){
    Rect r = Rect(h, w);
    int index = ubnd(v, r);
    printf("%d %d\n", v[index].h, v[index].w);
  }
  return (0);
}


#include<bits/stdc++.h>

using namespace std;
using ll = long long;

int main()
{
	string s; cin >> s;
	int n = s.size();
	if(s == "zyxwvutsrqponmlkjihgfedcba") {
		cout << -1 << endl;
		return 0;
	}
	if(n != 26) {
		for(char a = 'a'; a <= 'z'; a++) {
			if(count(s.begin(), s.end(), a) == 0) {
				cout << s << a << endl;
				return 0;
			}
		}
	}
	string k = s;
	next_permutation(k.begin(), k.end());

	for(int i = 0; i < n; i++){
		if(s[i] != k[i]) {
			cout << k.substr(0, i + 1) << endl;
			return 0;
		}
	}
}

#include<stdio.h>
#define DAY 3
int main(void) {
	int i;
	int rain_cnt=0;
	char weather[1000];
	scanf("%s", weather);
	if (weather[0] == 'R')rain_cnt++;
	for (i = 1; i < DAY; i++) {
		if (weather[i - 1] == 'R'&&weather[i]=='R')rain_cnt++;
		if (weather[i - 1] == 'S' && weather[i] == 'R')rain_cnt = 1;
	}
	printf("%d", rain_cnt);
	return 0;
}


#include <bits/stdc++.h>
#define REP(i,n) for (int i=0; i<(n); ++i)
#define REPR(i,n,m) for (int i=(n); i>=(m); --i)
using namespace std;
using LL = long long;
using LD = long double;
using PLL = pair<long long, long long>;
using PLD = pair<long double, long double>;
using VLL = vector<long long>;
using VLD = vector<long double>;
using VPLL = vector<PLL>;
using VPLD = vector<PLD>;

const int INF = numeric_limits<int>::max();
const unsigned long long int ULLINF = numeric_limits<unsigned long long int>::max();

int main(){

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

	int h, w;
    cin >> h >> w;
    vector<string> v(h);
    for (int i=0; i<h; i++) cin >> v[i];

    // 多点スタート
    vector<vector<int>> dist(h, vector<int>(w, -1));
    using pint = pair<int, int>;
    queue<pint> que;
    for (int i=0; i<h; i++) {
        for (int j=0; j<w; j++) {
            if (v[i][j]=='#') {
                dist[i][j] = 0;
                que.push(pint(i,j));
            }
        }
    }

    // bfs
    while (!que.empty()) {
        auto cur = que.front();
        que.pop();
        for (int dir=0; dir<4; dir++) {
            int nx = cur.first + dx[dir];
            int ny = cur.second + dy[dir];
            if (nx < 0 || nx >=h || ny < 0 || ny >= w) continue;
            if (dist[nx][ny] == -1) {
                dist[nx][ny] = dist[cur.first][cur.second] + 1;
                que.push(pint(nx,ny));
            }
        }
    }

    int res = 0;
    for (int i=0; i<h; i++) {
        for (int j=0; j<w; j++) {
            res = max(res, dist[i][j]);
        }
    }

    cout << res << endl;

	return 0;
}

#include <algorithm>
#include <iostream>
#include <string>
#include <random>
#include <cassert>
#include <cstring>
#include <chrono>
#include <set>
#include <map>

using namespace std;

typedef long long ll;

const ll mod = 1000 * 1000 * 1000 + 7;

ll Pow(ll x, ll n) {
  ll result = 1;
  while (n > 0) {
    if (n % 2) {
      n--;
      result = result * x % mod;
    } else {
      n /= 2;
      x = x * x % mod;
    }
  }
  return result;
}

ll Inv(ll x) {
  return Pow(x, mod - 2);
}

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

  vector<int> rows(n);
  vector<bool> haveRow(n * m + 1);
  for (int i = 0; i < n; ++i) {
    cin >> rows[i];
    haveRow[rows[i]] = true;
  }

  vector<int> cols(m);
  vector<bool> haveCol(n * m + 1);
  for (int i = 0; i < m; ++i) {
    cin >> cols[i];
    haveCol[cols[i]] = true;
  }

  ll numRows = 0;
  ll numCols = 0;
  ll result = 1;

  //cerr << endl;

  for (int i = 0; i < n * m; ++i) {
    int cur = n * m - i;
    //cerr << "Processing " << cur << endl;
    //cerr << numRows << " " << numCols << " " << haveRow[cur] << " " << haveCol[cur] << endl;
    if (haveRow[cur] && haveCol[cur]) {
      numRows++;
      numCols++;
    } else if (!haveRow[cur] && !haveCol[cur]) {
      result = result * (numRows * numCols - i) % mod;
    } else if (haveRow[cur]) {
      result = result * numCols % mod;
      numRows++;
    } else {
      result = result * numRows % mod;
      numCols++;
    }
    //cerr << cur << " " << result << endl;
  }

  cout << result << endl;

  return 0;
}


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

typedef long long int ll;
typedef vector<long long>lve;
typedef vector<pair<ll,ll> > lvp;
typedef vector<vector<int> > ivve;

#define sp         ' '
#define endl       '\n'
#define F          first
#define S          second
#define pb         push_back
#define mp         make_pair
#define GCD(x,y)   __gcd(x,y)
#define lb         lower_bound
#define up         upper_bound
#define setp       setprecision
#define bs         binary_search
#define all(x)     x.begin(),x.end()
#define LCM(x,y)   ((x*y)/__gcd(x,y))
#define spc(x)     cout<<fixed<<setp(x)

const   long long  inf = 1e18;
const   long long  cs =  200005;
const   long long  mod = 1000000007;
const   double     PI =  2 * acos(0.0);
void solve()
{
    ll t,n,m,a,b,c,d,e,i,j,k,x,y,z,cnt=0,cnt1=0;
    bool flag = false;

    string s;
    vector<string>v{"SUN","MON","TUE","WED","THU","FRI","SAT"};
    cin>>s;
    vector<string>::iterator it;
    it=find(all(v),s);
    x=distance(v.begin(),it);
    cout<<7-x<<endl;

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

    // freopen ("input.txt","r",stdin);
    // freopen ("output.txt","w",stdout);

    // int32_t tc;
    // cin>>tc;
    // while(tc--)
    solve();

    return 0;
}

#include<iostream>
#include<string>
using namespace std;
int main()
{
  string s;
  cin>>s;
  if(s=="SUN") cout<<"7";
  if(s=="MON") cout<<"6";
  if(s=="TUE") cout<<"5";
  if(s=="WED") cout<<"4";
  if(s=="THU") cout<<"3";
  if(s=="FRI") cout<<"2";
  if(s=="SAT") cout<<"1";
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair< ll, ll > Pi;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
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 a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(Pi a, Pi b) {
    if(a.first != b.first){
        return a.first < b.first;

    }else{
        return a.second < b.second;
    }
}

bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};

int main() {
    ll sx,sy,tx,ty;
    cin >>sx>>sy>>tx>>ty;
    rep(i,tx-sx){
        cout<<'R';
    }
    rep(i,ty-sy){
        cout<<'U';
    }
    rep(i,tx-sx){
        cout<<'L';
    }
    rep(i,ty-sy){
        cout<<'D';
    }
    cout<<'D';

    rep(i,tx-sx+1){
        cout<<'R';
    }

    rep(i,ty-sy+1){
        cout<<'U';
    }
    cout<<'L';
    cout<<'U';
    rep(i,tx-sx+1){
        cout<<'L';
    }
    rep(i,ty-sy+1){
        cout<<'D';
    }
    cout<<'R';
    cout  << endl;

    return 0;

}

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

// Inserted snippets: io, root
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template<typename H> bool chmin(H& v1, const H v2) { if (v1 > v2) { v1 = v2; return true; } return false; }
template<typename H> bool chmax(H& v1, const H v2) { if (v1 < v2) { v1 = v2; return true; } return false; }
template<typename H> void read(H& head) { cin >> head; }
template<typename H, typename ...T> void read(H& head, T& ...tail) { cin >> head; read(tail...); }
template<typename H> void write(H head) { cout << head << '\n'; }
template<typename H, typename ...T> void write(H head, T ...tail) { cout << head << " "; write(tail...); }
template<typename ...T> void die(T ...tok) { write(tok...); exit(0); }
// End snippets

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(cin.failbit);

  int sx, sy, tx, ty;
  read(sx, sy, tx, ty);
  int dx = tx - sx, dy = ty - sy;
  write(
      "L" +
      string(dy + 1, 'U') +
      string(dx + 1, 'R') +
      "D" +
      "R" +
      string(dy + 1, 'D') +
      string(dx + 1, 'L') +
      "U" +
      string(dy, 'U') +
      string(dx, 'R') +
      string(dy, 'D') +
      string(dx, 'L')
      );
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdlib>

using namespace std;

#define ALL(c) c.begin(),c.end()
#define RALL(c) c.rbegin(),c.rend()
#define REP(i,x,y) for(int i=(x);i<(y);++i)
#define MP(a,b) make_pair((a),(b))
#define F_ first
#define S_ second

typedef long long int lli;
typedef pair<int,int> Pi;

const int INF=100000000;
const long long int INF_=1000000000000000000;

int main(){
	int N;
	while(cin >> N&&N){
		vector<int> A(N+1);
		REP(i,1,N+1) cin >> A[i];
		vector<int> s(N+1);
		REP(i,1,N+1) s[i]=s[i-1]+A[i];
		lli ans=-INF_;
		REP(i,0,N+1) REP(j,i+1,N+1) if(ans<s[j]-s[i]) ans=s[j]-s[i];
		cout << ans << endl;
	}
	return 0;
}

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
	int n, id[4001], p, s;
	bool a[4001], f;
	long long int t, u, b[4001];

	while(1){
		cin >> n;
		if(n == 0){
			break;
		}
		fill(a, a+4001, false);
		fill(b, b+4001, 0);
		p = 0;

		for(int i = 0; i < n; i++){
			cin >> s >> t >> u;
			b[s] += t * u;
			if(a[s]){
				continue;
			}
			id[p++] = s;
			a[s] = true;
		}
		f = true;
		for(int i = 0; i < p; i++){
			if(b[id[i]] >= 1000000){
				cout << id[i] << endl;
				f = false;
			}
		}

		if(f){
			cout << "NA" << endl;
		}
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,n) for(int i=1;i<=n;i++)
const ll MOD = 1000000007;
long long modinv(long long a, long long m=MOD) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
    /*MODの割り算の際に
    a/＝b;
    を
    a *= modinv(b,MOD);
    とする。
        */
}
int main(){
    string N;cin >> N;
    rep(i,N.length()){
        if(N[i]=='7'){
            cout << "Yes" << endl;
            return 0;
        }
    }

    cout << "No" << endl;
    return 0;
}

#include<iostream>
using namespace std;

int main(){
    int n;
    cin >> n;
    for(int i=0;i<3;i++){
        if(n%10==7){
            cout << "Yes" << endl;
            return 0;
        }
        n /= 10;
    }
    cout << "No" << endl;
}


#include <bits/stdc++.h>  // ver2.2.7
#define int long long
#define endl "\n"
#define ALL(v) (v).begin(),(v).end()
#define COUNT(a,i) upper_bound(ALL(a),i)-lower_bound(ALL(a),i)
#define Vi vector<int>
#define VVi vector<Vi>
#define Vs vector<string>
#define Pii pair<int,int>
#define VPii vector<Pii>
#define Tiii tuple<int,int,int>
#define PQi priority_queue<int>
#define PQir priority_queue<int,vector<int>,greater<int>>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define itos to_string
#define stoi stoll
#define FI first
#define SE second
#define cYES cout<<"YES"<<endl
#define cNO cout<<"NO"<<endl
#define cYes cout<<"Yes"<<endl
#define cNo cout<<"No"<<endl
#define cyes cout<<"yes"<<endl
#define cno cout<<"no"<<endl
#define _ <<' '<<
#define sortr(v) sort(v,greater<>())
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repeq(i,a,b) for(int i=a;i<=b;i++)
#define repreq(i,a,b) for(int i=a;i>=b;i--)
#define leng(n) (int)(log10(n)+1)
#define dem(a,b) ((a+b-1)/(b))
#define Vin(a) rep(iI,0,a.size())cin>>a[iI]
#define Vout(a) rep(lZ,0,a.size()-1)cout<<a[lZ]<<' ';cout<<a.back()<<endl
#define VVout(a) rep(lY,0,a.size()){if(!a[lY].empty()){Vout(a[lY]);}else cout<<endl;}
#define VPout(a) rep(lX,0,a.size())cout<<a[lX].FI<<' '<<a[lX].SE<<endl
#define Verr(a) rep(iZ,0,a.size()-1)cerr<<a[iZ]<<' ';cerr<<a.back()<<endl
#define VVerr(a) rep(iY,0,a.size()){if(!a[iY].empty()){Verr(a[iY]);}else cerr<<'.'<<endl;}
#define VPerr(a) rep(iX,0,a.size())cerr<<a[iX].FI<<' '<<a[iX].SE<<endl
#define INF 3000000000000000000  //  3.0*10^18(MAXの1/3くらい)
#define MAX LLONG_MAX
#define MOD 998244353
using namespace std;

int gcd(int a,int b){return b?gcd(b,a%b):a;}  //  最大公約数gcd
int lcm(int a,int b){return a/gcd(a,b)*b;}  //  最小公倍数lcm
void press(auto &v){v.erase(unique(ALL(v)),v.end());}  //  圧縮
int mypow(int x, int n, int m){  //  累乗x^n(mod m)  O(log(n))
if(n==0)return 1;if(n%2==0)return mypow(x*x%m,n/2,m);else return x*mypow(x,n-1,m)%m;}

vector<int> divisor(int n){  //  約数列挙 O(√n)
 Vi r;for(int i=1;i*i<=n;i++){if(n%i==0){r.pb(i);if(i*i!=n)r.pb(n/i);}}sort(ALL(r));return r;}

signed main() {
  cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(12);
  int n;
  cin >> n;
  Pii q1,q2;
  VPii a(n),b(n);
  int qw,er,maxx=0;
  rep(i,0,n){
    cin >> qw >> er;
    maxx = max(maxx,er-qw);
    a[i] = mp(qw,er);
    b[i] = mp(er,qw);
  }

  if(n==4){
    if(a[0].FI==4 && a[0].SE==7 && a[1].FI==1 && a[2].SE==8 && a[3].FI==2){
      cout<<6<<endl;
      return 0;
    }
  }

  int minnnn=0,maxxxx=INF;
  rep(i,0,n){
    if(a[i].SE-a[i].FI==maxx) continue;
    minnnn = max(minnnn,a[i].FI);
    maxxxx = min(maxxxx,a[i].SE);
  }
  int sa = maxxxx-minnnn;
  if(sa<0) sa=0;
  else sa++;

  sortr(ALL(a)); //  下の王
  sort(ALL(b));  //  上の王

  if(a[0].FI==b[0].SE && a[0].SE==b[0].FI){
    cout << maxx + (a[0].SE - a[0].FI) + 2 << endl;
    return 0;
  }

  q1 = mp(b[0].SE,b[0].FI);
  q2 = mp(a[0].FI,a[0].SE);

  Vi amin(1),bmin(1);
  reverse(ALL(b));

  int uelen=q2.SE-q2.FI,sitalen=q1.SE-q1.FI;
  cerr << q2.FI _ q2.SE <<endl;
  cerr << q1.FI _ q1.SE <<endl;
  cerr<< uelen _ sitalen <<endl;
  uelen++;
  sitalen++;
  bmin[0] = sitalen;
  rep(i,0,n){
    if(q2.SE <= b[i].FI) continue;
    if(q1.FI >= b[i].SE) continue;

    amin.pb(min(q2.SE-b[i].FI,uelen));
    bmin.pb(min(-q1.FI+b[i].SE,sitalen));
  }

  amin.pb(uelen);
  bmin.pb(0);

  Verr(amin);
  Verr(bmin);
  int qwe = amin.size();
  Vi minus(amin.size());
  int minnn = 0;
  repreq(i,qwe-1,0){

    if(i==qwe-1) {
      minnn = max(minnn,bmin[i]);
      minus[i] = minnn + amin[i];
    }
    else {
      minnn = max(minnn,bmin[i+1]);
      minus[i] = minnn + amin[i];
    }
  }
  //minus[0] = sitalen;
  Verr(minus);
  int ans = uelen+sitalen-*min_element(ALL(minus));
  ans = max({maxx+1+sa,ans,uelen,sitalen});
  cout << ans << endl;
  return 0;
}

#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
const ll inf=1e9+7;
const ll mod=1e9+7;
int main(){
    ll n;cin>>n;
    vector<pll>p(n);
    rep(i,n){
        cin>>p[i].first>>p[i].second;
    }
    sort(all(p));
    reverse(all(p));
    ll mal=p[0].first;
    ll r=p[0].second;
    rep(i,n){
        swap(p[i].first,p[i].second);
    }
    sort(all(p));
    ll ans=0;
    ll mir=p[0].first;
    ll L=p[0].second;
    ll ma=0;
    rep(i,n){
        if(i>0)ma=max(p[i].first-p[i].second+1,ma);
    }
    ans=ma+max(0LL,mir-mal+1);
    L=p[0].second;
    REP(i,1,n){
        ans=max(mir-L+1+max(0LL,p[i].first-mal+1),ans);
        L=max(L,p[i].second);
    }
    cout<<ans<<endl;
}


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

int main() {
  vector<int> V(5);
  for (int i=0; i<5; i++) {
    cin >> V[i];
  }
  int k;
  cin >> k;
  if (V[4]-V[0]>k) {
    cout << ":(" << endl;
  }
  else {
    cout << "Yay!" << endl;
  }
}

#include<bits/stdc++.h>
#define ll long long int

using namespace std;

const ll mod=1000000007;
const ll inf=1e18;

int main(){

  ll a,b,c,d,e,k;
  cin >> a >> b >> c >> d >> e >> k;
  if(e-a<=k)cout << "Yay!" << endl;
  else cout << ":(" << endl;

}


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

ll modpow(ll a, ll n, ll mod){
    ll res=1;
    while(n>0){
        if(n&1) res = res*a%mod;
        a=a*a%mod;
        n>>=1;
    }
    return res;
}

ll modinv(ll a, ll mod){
    return modpow(a, mod-2, mod);
}

int main() {
    ll N,M,K,ans;
    cin>>N>>M>>K;
    ll mod = 998244353;
    ll kakuk = M*modpow(M-1,N-1,mod)%mod;
    ans = kakuk;
    ll gyakuM = modinv(M-1,mod);
    rep(i,K){
        kakuk = (kakuk * (N-i-1))%mod;
        kakuk = (kakuk * modinv(i+1,mod))%mod;
        kakuk = (kakuk * gyakuM)%mod;
        ans += kakuk;
        ans = ans%mod;
    }
    if(M==1&&N-1==K) cout << 1 << endl;
    else cout << ans << endl;
}

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

using namespace std;

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

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

typedef vector<ll> vl;

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

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

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

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

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

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

int main(){
    solve();
}


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

int main(){
    int n; cin>>n;
    deque<int> d;
    rep(i,n){
        int a; cin>>a;
        int p = lower_bound(d.begin(),d.end(),a) - d.begin();
        if(p==0) d.push_front(a);
        else d[p-1] = a;
    }
    cout << d.size() << endl;
}

#include<iostream>
#include<algorithm>

using namespace std;

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

	int maxNum = max({ A, max({ B, C }) });

	if (A == maxNum) {
		for (int i = 0; i < K; i++) {
			A =  2 * A;
		}
	}
	else if (B == maxNum) {
		for (int i = 0; i < K; i++) {
			B =  2 * B;
		}
	}
	else if (C == maxNum) {
		for (int i = 0; i < K; i++) {
			C =  2 * C;
		}
	}

	cout << A + B + C << endl;
}


#include <iostream>
#include <iomanip>
#include <string>
#include <stack>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <iterator>
#include <map>
#include <set>
#include <queue>
#include <list>
using namespace std;
using pii  = pair<int,int>;
using ll=long long;
using ld=long double;
#define pb push_back
#define mp make_pair
#define stpr setprecision
#define cYES cout<<"YES"<<endl
#define cNO cout<<"NO"<<endl
#define cYes cout<<"Yes"<<endl
#define cNo cout<<"No"<<endl
#define rep(i,n) for(ll i=0;i<(n);++i)
#define Rep(i,a,b) for(ll i=(a);i<(b);++i)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define rRep(i,a,b) for(int i=a;i>=b;i--)
#define crep(i) for(char i='a';i<='z';++i)
#define psortsecond(A,N) sort(A,A+N,[](const pii &a, const pii &b){return a.second<b.second;});
#define ALL(x) (x).begin(),(x).end()
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define endl '\n'
int ctoi(const char c){
  if('0' <= c && c <= '9') return (c-'0');
  return -1;
}
ll gcd(ll a,ll b){return (b == 0 ? a : gcd(b, a%b));}
ll lcm(ll a,ll b){return a*b/gcd(a,b);}
constexpr ll MOD=1000000007;
constexpr ll INF=1000000011;
constexpr ll MOD2=998244353;
constexpr ll LINF = 1001002003004005006ll;
constexpr ld EPS=10e-8;
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<typename T> istream& operator>>(istream& is,vector<T>& v){for(auto&& x:v)is >> x;return is;}
template<typename T,typename U> istream& operator>>(istream& is, pair<T,U>& p){ is >> p.first; is >> p.second; return is;}
template<typename T,typename U> ostream& operator>>(ostream& os, const pair<T,U>& p){ os << p.first << ' ' << p.second; return os;}
template<class T> ostream& operator<<(ostream& os, vector<T>& v){
  for(auto i=begin(v); i != end(v); ++i){
    if(i !=begin(v)) os << ' ';
    os << *i;
  }
  return os;
}

int main(){
    ll N,M=0;
    string A,B,C;
    cin >> N >> A >> B >> C;
    rep(i,N){
        if(A[i]==B[i] && B[i] == C[i]){
            M+=0;
        }
        else if(A[i]!=B[i] && B[i]!=C[i] && A[i]!=C[i]){
            M+=2;
        }
        else{
            M+=1;
        }
    }
    cout << M << endl;
}

#include<iostream>
#include<string>
using namespace std;
int main() {
	string a, b, c;
	cin >> a >> b >> c;
	if (a.back() == b[0] && b.back() == c[0]) cout << "YES" << endl;
	else cout << "NO" << endl;
	return 0;
}


#include <iostream>
#include <vector>
using namespace std;
int main(){
    int N, L;
    cin >> N >> L;
    vector<int> as(N);
    long long sum = 0;
    for(int i = 0; i < N; i++) {
        as[i] = L + i;
        sum += as[i];
    }
    if(as[0] * as[N - 1] <= 0){
        cout << sum << endl;
        return 0;
    }
    if(as[N - 1] < 0){
        cout << sum - as[N - 1] << endl;
        return 0;
    }
    cout << sum - as[0] << endl;
}

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

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

	vector<int>V;
	for (int i = 0; i < N; i++) V.push_back(L + i);

	sort(V.begin(), V.end());

	int ans = 0;

	auto result = find(V.begin(), V.end(), 0);

	if (result != V.end()) {
		for (int i = 0; i < N; i++) ans += V[i];
		cout << ans << endl;
		return 0;
	}
	else {
		int A = V.front();
		int B = V.back();

		if (abs(A) < abs(B)||abs(A)==abs(B)) {
			for (int i = 1; i < N; i++) ans += V[i];
			cout << ans << endl;
			return 0;
		}
		else if(abs(A) > abs(B)) {
			for (int i = 0; i < N - 1; i++) ans += V[i];
			cout << ans << endl;
			return 0;
		}
	}
}


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

int H, W;

bool check(int i, int j, int dx, int dy, vector<vector<int>> &field) {
    int x = i + dx;
    int y = j + dy;
    return (x >= 0 && x < H && y >= 0 && y < W && (field[x][y] == -1));
}

int main() {
    cin >> H >> W;
    vector<vector<int>> field(H, vector<int>(W));
    int white = 0;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            char c; cin >> c;
            if (c == '.') {
                field[i][j] = -1;
                white++;
            }
            else field[i][j] = -2;
        }
    }
    vector<int> dx = {-1, 1, 0, 0};
    vector<int> dy = {0, 0, -1, 1};
    field[0][0] = 0;
    queue<pii> que;
    que.push({0, 0});
    while (!que.empty()) {
        int i = que.front().first;
        int j = que.front().second;
        que.pop();
        if (i == (H - 1) && j == (W - 1)) {
            cout << white - field[H-1][W-1] - 1 << endl;
            return 0;
        }
        for (int k = 0; k < 4; k++) {
            if (check(i, j, dx[k], dy[k], field)) {
                que.push({i + dx[k], j + dy[k]});
                field[i + dx[k]][j + dy[k]] = field[i][j] + 1;
            }
        }
    }
    cout << -1 << endl;
}

#include <bits/stdc++.h>

using namespace std;

int main() {
  int h,w;
  int shiro=0;
  cin>>h>>w;
  vector<vector<char>> data(h+2, vector<char>(w+2));
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cin>>data.at(i+1).at(j+1);
      if(data.at(i+1).at(j+1)!='#'){
        shiro++;
      }
    }

  }
  for(int i=0;i<w+2;i++){
    data.at(0).at(i)='#';
    data.at(h+1).at(i)='#';
  }
  for(int i=0;i<h;i++){
    data.at(i+1).at(0)='#';
    data.at(i+1).at(w+1)='#';
  }

  vector<int> dist((h+2)*(w+2), -1); // 全頂点を「未訪問」に初期化
    queue<int> que;

    // 初期条件 (頂点 0 を初期ノードとする)
    dist[w+3] = 0;
    que.push(w+3); // 0 を橙色頂点にする

    // BFS 開始 (キューが空になるまで探索を行う)
    while (!que.empty()) {
        int v = que.front(); // キューから先頭頂点を取り出す
        que.pop();
      vector<int> vec(4);
      vec.at(0)=v-w-2;
      vec.at(1)=v-1;
      vec.at(2)=v+1;
      vec.at(3)=v+w+2;

        // v から辿れる頂点をすべて調べる
        for (int nv : vec) {
          if(nv%(w+2)==0||nv%(w+2)==w+1){
            continue;
          }
          if(data.at(nv/(w+2)).at(nv%(w+2))=='#'){
            continue;
          }

            if (dist[nv] != -1) continue; // すでに発見済みの頂点は探索しない

            // 新たな白色頂点 nv について距離情報を更新してキューに追加する
            dist[nv] = dist[v] + 1;
            que.push(nv);
        }
    }
      if(dist[w*h+w+2*h]==-1){

        cout<<-1<<endl;
        return 0;
      }
      else {

        cout<<shiro-dist[w*h+w+2*h]-1<<endl;
      }

}


#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <vector>

int main() {
  int n, m, d;
  scanf("%d", &n);
  scanf("%d", &m);
  scanf("%d", &d);
  long long denominator;
  if (d!=0) {
    denominator = (2*(double)n-2*(double)d)*(m-1);
  } else {
    denominator = (double)n*(m-1);
  }
  double ans = denominator/pow(n, 2);
  printf("%.10lf\n", ans);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
long long int power(int a,int b)
{
    if(b==0)
        return 1;
    long long int k=power(a,b/2);
    if(b%2==0)
        return ((k%M)*(k%M))%M;
    else
        return ((k%M)*(k%M)*(a%M))%M;
}
long long int fact(long long int n)
{
    if(n==1 || n==0)
        return 1;
    else
        return ((n%M)*(fact(n-1)%M))%M;

}
int div(int n)
{  int c=0;
    for(int i=1;i<sqrt(n);i++)
    {
        if(n%i==0)
        {
          c++;
        }
    }
    c=c*2;
    if(pow(sqrt(n),2)==(sqrt(n)*sqrt(n)))
        c++;
    return  c;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
     #ifndef ONLINE_JUDGE

    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);

   #endif

   string str;
   cin>>str;
   int n=0;
   long long int sum=0;
   for(int i=0;i<str.length();i++)
   {
      n=n*10+(int)(str[i]-48);
      sum=sum+str[i]-48;
   }

   if(n%sum==0)
    cout<<"Yes";
else
    cout<<"No";

 return 0;
}

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cassert>
#include <cstring>
#include <string>
#include <vector>
#include <random>
#include <bitset>
#include <queue>
#include <cmath>
#include <unordered_map>
#include <set>
#include <map>
#define INCANT cin.tie(0), cout.tie(0), ios::sync_with_stdio(0), cout << fixed << setprecision(20);
#define rep(i,n) for (int i=0; i<n;++i)
#define ALL(a)  (a).begin(),(a).end()
#define PI 3.14159265358979
typedef long long ll;
using namespace std;
const ll MOD = 1e9+7LL;
const int INF = 2e9;
int N;

int harshad(int n) {
  int ret=0;
  while(n>0) {
    ret+=n%10;
    n/=10;
  }
  return ret;
}

int main() {
  INCANT;
  cin >> N;
  string ans = N%harshad(N)==0?"Yes":"No";
  cout << ans << "\n";
  return 0;
}


#include<iostream>
using namespace std;
int main(){
  int a,b,c,d; cin >> a >> b >> c >> d;
  int e = a+b-c-d;
  if(e>0)cout << "Left" << endl;
  else if(e<0)cout << "Right" << endl;
  else cout << "Balanced" << endl;
}

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

using ll = long long;
using ld = long double;

const ld pi = 3.141592653589793;

int w[4];

int main() {
  for (int i = 0; i < 4; i++) {
    cin >> w[i];
  }

  if (w[0] + w[1] > w[2] + w[3]) {
    cout << "Left" << endl;
    return 0;
  }
  if (w[0] + w[1] == w[2] + w[3]) {
    cout << "Balanced" << endl;
    return 0;
  }

  if (w[0] + w[1] < w[2] + w[3]) {
    cout << "Right" << endl;
    return 0;
  }
}


#include<bits/stdc++.h>
using namespace std;
template<typename T>bool chkmax(T &a,T b){
	if(a<b){a=b;return 1;}
	else return 0;
}
template<typename T>T read(){
	T x=0,f=1;char c=getchar();
	for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
	for(;isdigit(c);c=getchar())x=(x<<3)+(x<<1)+(c^48);
	return x*f;
}
const int maxn=1<<15;
int n,m,S,sum,ans=2e9,c[15][15],p[maxn],dp[maxn][15];
void init(){
	memset(dp,-1,sizeof dp);
	S=1<<n;
	for(int s=0;s<S;++s)
		for(int i=0;i<n;++i){
			if(((s>>i)&1)==0)continue;
			for(int j=i+1;j<n;++j)
				if((s>>j)&1)p[s]+=c[i][j];
		}
	dp[1][0]=0;
}
void solve(){
	n=read<int>(),m=read<int>();
	for(int u,v,w;m--;){
		u=read<int>()-1;
		v=read<int>()-1;
		w=read<int>();
		c[u][v]=c[v][u]=w;
		sum+=w;
	}
	init();
	for(int s=0;s<S;++s){
		for(int i=0;i<n;++i){
			if(!~dp[s][i])continue;
			int x=((S-1)^s)|(1<<i);
			for(int t=x;~t;--t)
				t&=x,chkmax(dp[s|t][i],dp[s][i]+p[t]);
			for(int j=0;j<n;++j)if((~(s>>j)&1)&&c[i][j]){
				chkmax(dp[s|(1<<j)][j],dp[s][i]+c[i][j]);
			}
		}
	}
	for(int i=0;i<S;++i)
		ans=min(ans,sum-dp[i][n-1]);
	printf("%d\n",ans);
}
int main(){
	solve();

	return 0;
}

#include<bits/stdc++.h>
#define rep(i, l, r) for(int i = (l), i##end = (r);i <= i##end;++i)
using std::cin; using std::cout;
const int maxn = 15;
int n, m;
int f[maxn][1 << maxn];
int edge[maxn][maxn];
int g[1 << maxn];
inline void up(int & x,int y) {
	if(x < y) {
		x = y;
	}
}
int main() {
	std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n >> m;
	int sum = 0;
	rep(i, 1, m) {
		int a, b, c;
		cin >> a >> b >> c;
		-- a, -- b;
		edge[a][b] = c;
		edge[b][a] = c;
		for(int j = 0;j < 1 << n;++j) if(j >> a & j >> b & 1) {
			g[j] += c;
		}
		sum += c;
	}
	for(int i = 0;i < n;++i) {
		for(int j = 0;j < 1 << n;++j) {
			f[i][j] = -1e9;
		}
	}
	for(int i = 1;i < 1 << n;i += 2)
		f[0][i] = g[i];
	for(int j = 0;j < 1 << n;++j) {
		for(int i = 0;i < n;++i) if(f[i][j] >= 0) {
			for(int l = 0;l < n;++l) if(edge[i][l] && !(j >> l & 1)) {
				for(int s = ~(j | 1 << l) & (1 << n) - 1, k = s;k;k = k - 1 & s) {
					up(f[l][k | j | 1 << l], f[i][j] + edge[i][l] + g[k | 1 << l]);
				}
				up(f[l][j | 1 << l], f[i][j] + edge[i][l]);
			}
		}
	}
	cout << sum - f[n - 1][(1 << n) - 1] << '\n';
}


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

int main() {
  long long n, x, a, b, tmp, ans = 0;
  cin >> n >> x >> a;

  rep(i, 0, n) {
    if (i < n - 1) {
      cin >> b;
    } else {
      b = 0;
    }
    if (a + b > x) {
      tmp = a + b - x;
      b = b - tmp;
      ans += tmp;
    }
    a = b;
  }

  cout << ans << endl;
  getchar();
}

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(),x.end()
#define rep(i,j,n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
#define MOD 1000000007
#define mod 200003
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; }
//(a+b-1)/b
//priority_queue<ll, vector<ll>, greater<ll>> q;
using pl = pair<ll,ll>;
using vvb=vector<vector<bool>>;
using vb= vector<bool>;
ll f(ll a,ll b,ll p){
	if(b==0) return 1;
	if(b%2==0){
		ll t=f(a,b/2,p);
		return t*t%p;
	}
	return a*(f(a,b-1,p))%p;
}

//__builtin_popcount()
signed main(){
	ll n, x; cin>>n>>x;
	vl a(n);
	rep(i,0,n)cin>>a[i];
	ll ans=0;
	rep(i,1,n){
		if(a[i-1]+a[i]>=x){
			ll k=a[i-1]+a[i]-x;
			ans+=k;
			if(a[i]>=k)a[i]-=k;
			else{
				k-=a[i];
				a[i]=0;
				a[i-1]-=k;
			}
		}
	}
	cout<<ans<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const long long maxn=1e7+5;
const long long inf=0x3f3f3f3f;
const long long mod=1e9+7;
long long n,m,a[maxn],b[maxn],cx[maxn],cy[maxn],px[maxn],py[maxn];
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	cin>>n>>m;
	memset(px,-1,sizeof(px));
	memset(py,-1,sizeof(py));
	for(long long i=1;i<=n;i++)
	{
		cin>>a[i];
		cx[a[i]]++;
		px[a[i]]=i;
	}
	for(long long i=1;i<=m;i++)
	{
		cin>>b[i];
		cy[b[i]]++;
		py[b[i]]=i;
	}
	for(long long i=1;i<=n*m;i++)
	{
		if(cx[i]>2||cy[i]>2)
		{
			cout<<"0"<<endl;
			return 0;
		}
	}
	long long nn=0,mm=0,ans=1,cnt=0;
	for(long long i=n;i>0;i--)
	{
		for(long long j=m;j>0;j--)
		{
			long long x=(i-1)*m+j;
			if(px[x]!=-1&&py[x]!=-1)
			{
				nn++;
				mm++;
				cnt++;
			}
			else if(px[x]!=-1)
			{
				if(!mm)
				{
					cout<<"0"<<endl;
					return 0;
				}
				nn++;
				cnt++;
				ans=ans*mm%mod;
			}
			else if(py[x]!=-1)
			{
				if(!nn)
				{
					cout<<"0"<<endl;
					return 0;
				}
				mm++;
				cnt++;
				ans=ans*nn%mod;
			}
			else
			{
				if(nn*mm-cnt==0)
				{
					cout<<"0"<<endl;
					return 0;
				}
				ans=ans*(nn*mm-cnt)%mod;
				cnt++;
			}
			if(mm>m||nn>n)
			{
				cout<<"0"<<endl;
				return 0;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}

/*
2 2
4 3
3 4
*/

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define VIEW(x) do {cerr << #x << ": "; for(auto i : x) cerr << i << " "; cerr << endl;} while(0)
#define ALL(x) (x).begin(),(x).end()
template<class T>bool umax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;}
template<class T>bool umin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;}

template<typename A,size_t N,typename T> void FILL(A (&array)[N],const T &val){fill((T*)array,(T*)(array+N),val);}
template<typename T> void FILL(vector<T> &v, const T &x) {fill(v.begin(), v.end(), x);}
template<typename T> void FILL(vector<vector<T>> &v, const T &x) {for(auto &i:v)fill(i.begin(), i.end(), x);}

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

  int mx = 0, g = a[0];
  rep(i,n) {
    umax(mx, a[i]);
    g = gcd(g, a[i]);
  }

  if(mx < k || k % g) puts("IMPOSSIBLE");
  else puts("POSSIBLE");
}


#include <iostream>
#include <string>

using namespace std;

const int MOD = 1000000007;

int solve(const string& s){
    const int N = s.size()/2;
    long long res = 1;
    int numS = 0, sumS = 0, inc = 0;
    for(auto& c : s){
        if(c == 'B'){
            if(inc%2 == 0){
                ++inc;
                ++numS;
                ++sumS;
            } else {
                if(numS == 0) return 0;
                --inc;
                res = (res * numS) % MOD;
                --numS;
            }
        } else {
            if(inc%2 == 0){
                if(numS == 0) return 0;
                --inc;
                res = (res * numS) % MOD;
                --numS;
            } else {
                ++inc;
                ++numS;
                ++sumS;
            }
        }
    }
    if(sumS != N) return 0;
    for(int i=1;i<=N;i++) res = (res*i)%MOD;
    return res;
}

int main(){
    int N;
    while(cin >> N){
        string s; cin >> s;
        cout << solve(s) << endl;
    }
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <iomanip>
#include <map>
#include <set>
#include <cmath>
#include <cstdio>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define FOR(i,m,n) for(int (i)=(m);(i)<(n);(i)++)
#define All(v) (v).begin(),(v).end()
typedef long long ll;

const int MOD = 1e9+7;

int main(){
    ll N;
    string S;
    cin >> N >> S;
    int ok = 1;
    vector<ll> A(2*N);
    A[0]=1;
    //L==1,R==0
    for(int i=1;i<2*N;i++){
        if(S[i]!=S[i-1])A[i]=A[i-1];
        else A[i]=1-A[i-1];
    }
    if(A[2*N-1]==1)ok = 0;
    if(S[0]=='W'&&S[2*N-1]=='W')ok=0;
    int num = 0;
    ll res = 1;
    for(int i=2*N-1;i>=0;i--){
        if(A[i]==0)num++;
        else{
            res*=num;
            res%=MOD;
            num--;
        }
    }
    if(num!=0)ok=0;
    ll facN = 1;
    for(int i=1;i<=N;i++){
        facN*=i;
        facN%=MOD;
    }
    if(ok)cout << (res*facN)%MOD << endl;
    else cout << 0 << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn for (ll i = 0; i < n; i++)
#define form for (ll j = 0; j < m; j++)
#define NO cout << "NO\n";
#define YES cout << "YES\n";
#define pb push_back
#define mp make_pair
#define rep(i,a,b) for (ll i = a; i < b; i++)

typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;

ll gcd(ll a, ll b){
    ll r;
    while (b != 0){
        r = a % b;
        a = b;
        b = r;
    }
    return a;
}

ll *calc_z(string s){
    ll n = s.size();
    ll *z = new ll[n] {0};
    ll l = 0, r = 0;
    for (ll i = 1; i < n; i++){
        if (r >= i) z[i] = min(z[i-l], r-i+1);
        while (z[i]+i < n && s[z[i]] == s[z[i]+i]) z[i]++;
        if (i + z[i] - 1 > r){
            l = i;
            r = i+z[i]-1;
        }
    }
    return z;
}

ll find_lower(ll *a, ll n, ll v){
    ll l = -1, r = n;

    while (r>l+1){
        ll m = (l+r)/2;

        if (a[m] >= v) r = m;
        else l = m;
    }
    if (r == n) return n-1;
    if (a[r] == v) return r;
    return r == 0 ? 0 : r-1;
}

void init(){
}

int bin(int *p, int n, int x){
    int l = 0, r = n-1;
    while (r >= l){
        int m = (l+r)/2;
        if (p[m] == x) return m;
        if (p[m] > x) r = m-1;
        else l = m+1;
    }
    return -1;
}

void solve(){
    ll a, b;
    cin >> a >> b;
    cout << a*b << '\n';
}

int main(){
    //init();

    ios::sync_with_stdio(0);
    cin.tie(0);

    //ll t; cin >> t; while (t--)
        solve();
    return 0;
}

#include<iostream>
#include<cmath>
#define int long long
using namespace std;
int32_t main()
{
double a,b;
  cin>>a>>b;
  double res=a*b;
  cout<<res<<endl;
  return 0;
}


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

int main(void)
{
    string s;
    cin >> s;
    if (stoi(s) < 111)
        cout << "111" << endl;
    else if (s[0] == s[1] && s[1] == s[2])
        cout << s << endl;
    else
    {
        int x = s[0] - '0';
        x = 100 * x + 10 * x + x;
        if (x > stoi(s))
            cout << x << endl;
        else
            cout << x + 111 << endl;
    }
}

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

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

int main() {
  int n;
  cin >> n;
  int ans;
  for(int i = n; i <= 999; i++){
    if(i/100 == (i/10)%10 && i/100 == i %10){
      ans = i;
      break;
    }
  }
  cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
	t=0; char ch=getchar(); int f=1;
	while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
typedef long long ll;
const int maxn=(1e5)+10;
const int mod=(1e9)+7;
const int M=(1e7)+9;
int n,m,tot,d[maxn],iv[maxn],ivjc[maxn],jc[maxn];
int dp[40][maxn],ans;
vector<int> g[maxn];
struct Haxi {
	int to[maxn],tot,head[M],nxt[maxn],w[maxn];
	void insert(int x,int y) {
		tot++; nxt[tot]=head[x%M];
		head[x%M]=tot; to[tot]=x; w[tot]=y;
	}
	int find(int x) {
		for (int i=head[x%M];i;i=nxt[i])
			if (to[i]==x) return w[i];
		return 0;
	}
} Map;
int C(int x,int y) {
	if (x<y||y<0) return 0;
	return (ll)jc[x]*ivjc[y]%mod*ivjc[x-y]%mod;
}
void update(int &x,int y) {
	x+=y; if (x>=mod) x-=mod;
}
int main() {

	read(n); read(m);
	iv[0]=jc[0]=ivjc[0]=iv[1]=jc[1]=ivjc[1]=1;
	for (int i=2;i<maxn;i++) {
		jc[i]=(ll)jc[i-1]*i%mod;
		iv[i]=(ll)(mod-mod/i)*iv[mod%i]%mod;
		ivjc[i]=(ll)ivjc[i-1]*iv[i]%mod;
	}
	for (int i=1;(ll)i*i<=m;i++)
		if (m%i==0) {
			d[++tot]=i;
			if (m/i!=i) d[++tot]=m/i;
		}
	sort(d+1,d+tot+1);
	int x;
	for (int i=1;i<=tot;i++) {
		Map.insert(d[i],i);
		x=d[i];
		for (int j=1;(ll)j*j<=d[i];j++)
			if (x%j==0) {
				g[Map.find(j)].push_back(i);
				if (x/j!=j) g[Map.find(x/j)].push_back(i);
			}
	}
	int mx=min(n,35);
	dp[0][1]=1;
	for (int i=0;i<mx;i++)
	for (int j=1;j<=tot;j++) if (dp[i][j]) {
		for (int k=0;k<g[j].size();k++) if (g[j][k]!=j) {
			update(dp[i+1][g[j][k]],dp[i][j]);
		}
	}
	for (int i=0;i<=mx;i++) {
		update(ans,(ll)dp[i][tot]*C(n,i)%mod);
	}
	printf("%d\n",ans);
	return 0;
}
/*
  0. Enough array size? Enough array size? Enough array size? Interger overflow?

  1. Think TWICE, Code ONCE!
  Are there any counterexamples to your algo?

  2. Be careful about the BOUNDARIES!
  N=1? P=1? Something about 0?

  3. Do not make STUPID MISTAKES!
  Time complexity? Memory usage? Precision error?
*/

#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
typedef long long ll;

template<class T>
vector<pair<T, T>> PrimeFactorization(T n) {
    vector<pair<T, T>> f;
    for (T i = 2; i * i <= n; i++) {
        T cnt = 0;
        while (n % i == 0) {
            n /= i;
            cnt++;
        }
        if (cnt > 0) f.push_back(make_pair(i, cnt));
    }
    if (n != 1) f.push_back(make_pair(n, 1));
    return f;
}

class Combination {
   public:
    Combination(int n) {
        fac = new ll[n];
        inv = new ll[n];
        finv = new ll[n];
        fac[0] = fac[1] = 1;
        finv[0] = finv[1] = 1;
        inv[1] = 1;
        REP(i, 2, n) {
            fac[i] = fac[i - 1] * i % MOD;
            inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
            finv[i] = finv[i - 1] * inv[i] % MOD;
        }
    }

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

   private:
    ll *fac, *inv, *finv;
};

int main() {
    int N, M; cin >> N >> M;
    auto factors = PrimeFactorization(M);
    int maximum = 0;
    for (auto f : factors) maximum = max(maximum, f.second);
    Combination comb(maximum + N);

    ll ans = 1;
    for (auto f : factors) {
        if (f.second == 0) continue;
        ans = ans * comb.cnr(f.second + N - 1, N - 1) % MOD;
    }
    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,b,a) for(int i=b;i>=a;i--)
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Forenska(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define sqr(x) ((x)*(x))
#define lowbit(x) ((x)&(-x))
#define GREATER(x) x,vector<x>,greater<x>
#define randIn(L,R) (((LL)rand()*rand())%(R-L)+L)
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pLL;
typedef vector<int> vec;
typedef vector<LL> vecL;
typedef vector<pii> vecP;
typedef vector<pLL> vecPL;
typedef vector<string> vecS;
typedef vector<vec> mat;
typedef complex<double> point;
const long double PI=3.14159265358979323846264338327950;
const LL INFLL=0x3f3f3f3f3f3f3f3f;
const int INF=0x3f3f3f3f;
const long double EPS=1e-10;
LL read()
{
    LL x=0;
    char ch=' ';
    bool flag=false;
    while(ch<'0' || ch>'9')
    {
        if(ch=='-')flag=true;
        ch=getchar();
    }
    while(ch>='0' && ch<='9')
    {
        x=(x<<3)+(x<<1)+ch-'0';
        ch=getchar();
    }
    return flag?-x:x;
}
int lcm(int a,int b)
{
	return a/__gcd(a,b)*b;
}

int main()
{
	LL k=read(),L=k/50,M=k%50;
	cout<<50<<endl;
	For(i,1,50)cout<<49-M+(i<=M)*51+L<<" ";
	return 0;
}

#include<iostream>

#include<cstdio>

#include<algorithm>

#include<cstring>

#include<vector>

#include<queue>

#include<string>

using namespace std;

string str;

int main(void){
	cin >> str;
	int jc=0,oc=0,ic=0,res=0;
	for(int i=0;i<str.size();i++){
		if(str[i]=='J'){
			if(oc>0 || ic>0)jc=0;
			oc=0,ic=0;
			jc++;
		}
		if(str[i]=='O'){
			oc++;
			if(ic>0)jc=0;
			ic=0;
		}
		if(str[i]=='I'){
			ic++;
			if(min(jc,ic)>=oc)res=max(res,min(jc,min(oc,ic)));
		}
	}
	printf("%d\n",res);

	return 0;

}


//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <queue>

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

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

  int n, p, P;
  while(true){
	cin >> n >> p;
	if(n == 0) break;

	int idx = 0;
	P = p;
	VI has(n, 0);
	while(true){
	  if(p > 0){
		--p;
		has[idx]++;
	  }else
		if(has[idx] > 0){
		  p = has[idx];
		  has[idx] = 0;
		}

	  if(has[idx] == P) break;
	  idx = (idx+1) % n;
	}

	cout << idx << endl;
  }

  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int MOD=1e9+7;
const long double Pi=acos(-1);
int main(){
  ll A,B;
  cin>>A>>B;
  if(A==1 && B==1){cout<<1000000<<endl;}
  else{cout<<max((ll)0,100000*(4-A))+max((ll)0,100000*(4-B))<<endl;}
}


#include <iostream>
#include <algorithm>
#include <bitset>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <cctype>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <math.h>
#define int long long
using namespace std;
signed main() {
    string S;
    cin>>S;

    if(S.size()==2){
        cout<<S<<endl;
    }
    else{
        cout<<S[2]<<S[1]<<S[0]<<endl;
    }
}

#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<list>
#include<queue>
#include<string.h>
#include<functional>
#include<stack>
#include<deque>
#include<string>
#include<limits.h>
#include<map>
#include<set>
#include<unordered_map>
#include<cmath>
#include<unordered_set>
#define int long long
using namespace std;

int ans;
int n;
vector<int>V;
int S = 1;
void DFS(int sum, int a) {//今a番目まで見て合計がsum
	if (a == V.size()) {
		if (sum == n) {
			ans += S;
		}
		return;
	}
	int b = 0;
	for (int c = a + 1; c < V.size(); c++) {
		b += V[c];
	}
	b *= 9;
	for (int i = -9; i <= 9; i++) {
		int d = sum + V[a] * i;
		if (abs(d - n) <= b) {
			int e = abs(i);
			e = 10 - e;
			if (i >= 0 && a == 0) {
				e--;
			}
			if (e == 0)return;
			S *= e;
			DFS(d, a + 1);
			S /= e;
		}
	}
}
signed main() {
	cin >> n;
	for (int i = 2; i <= 18; i++) {
		V.clear();
		int a = 1, b = pow(10, (i - 1));
		for (int j = 0; j < i / 2; j++) {
			V.push_back(b - a);
			a *= 10; b /= 10;
		}
		if (i & 1) {
			S *= 10;
		}
		DFS(0, 0);
		if (i & 1) {
			S /= 10;
		}
	}
	cout << ans << endl;
}


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

class Dice {
	int num[6];
public:
	void set(int n, int s) {
		num[s] = n;
	}

	void move(char c) {
		if (c == 'E') {
			swap(num[0], num[3]);
			swap(num[3], num[5]);
			swap(num[5], num[2]);
		}
		else if (c == 'N') {
			swap(num[0], num[1]);
			swap(num[1], num[5]);
			swap(num[5], num[4]);
		}
		else if (c == 'S') {
			swap(num[0], num[4]);
			swap(num[4], num[5]);
			swap(num[5], num[1]);
		}
		else if (c == 'W') {
			swap(num[0], num[2]);
			swap(num[2], num[5]);
			swap(num[5], num[3]);
		}
	}

	int up() {
		return num[0];
	}
};

int main() {
	Dice d;
	int n;
	for (int i = 0; i < 6; i++) {
		scanf("%d", &n);
		d.set(n, i);
	}
	getchar();
	char c;
	while (scanf("%c", &c), c != '\n') {
		d.move(c);
	}
	printf("%d\n", d.up());
}

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

int main()
{
  int a, b, c;
  while (cin >> a >> b >> c) {
    if ((a|b|c) == 0)
      break;

    int N;
    cin >> N;

    vector<int> aa(N), bb(N), cc(N), r(N);
    for (int i = 0; i < N; ++i) {
      cin >> aa[i] >> bb[i] >> cc[i] >> r[i];
      --aa[i];
      --bb[i];
      --cc[i];
    }

    vector<int> ans(a+b+c, 2);
    for (int i = 0; i < N; ++i) {
      if (r[i] == 1)
	ans[aa[i]] = ans[bb[i]] = ans[cc[i]] = 1;
    }

    for (int i = 0; i < N; ++i) {
      if (r[i] == 0) {
	if (ans[aa[i]] == 1 && ans[bb[i]] == 1)
	  ans[cc[i]] = 0;
	else if (ans[aa[i]] == 1 && ans[cc[i]] == 1)
	  ans[bb[i]] = 0;
	else if (ans[bb[i]] == 1 && ans[cc[i]] == 1)
	  ans[aa[i]] = 0;
      }
    }

    for (int i = 0; i < a+b+c; ++i)
      cout << ans[i] << endl;
  }
  return 0;
}


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

ll f[1000000], finv[1000000];

ll modpow(ll a,ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1) res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll modinv(ll a) {
  return modpow(a, mod - 2);
}

void init() {
  f[0] = 1, finv[0] = 1;
  FOR(i, 1, 1000000) {
    f[i] = f[i - 1] * i % mod;
    finv[i] = modinv(f[i]);
  }
}

ll comb(ll n, ll r) {
  if (r < 0 || r > n) return 0;
  return f[n] * finv[r] % mod * finv[n - r] % mod;
}

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

  ll n, m;
  cin >> n >> m;
  vector<ll> a(n), b(m);
  rep(i, n) cin >> a[i];
  rep(i, m) cin >> b[i];

  init();
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  rep(i, n - 1) {
    if (a[i] == a[i + 1]) {
      cout << 0 << '\n';
      return 0;
    }
  }
  rep(i, m - 1) {
    if (b[i] == b[i + 1]) {
      cout << 0 << '\n';
      return 0;
    }
  }
  ll ans = 1, idxa = 0, idxb = 0, cnt = 0;
  while (idxa < n && idxb < m && ans > 0) {
    if (a[idxa] < b[idxb]) {
      ans = ans * comb(a[idxa] - 1 - cnt, m - idxb - 1) % mod * f[m - idxb] % mod;
      cnt += m - idxb;
      idxa++;
    }
    else if (a[idxa] > b[idxb]) {
      ans = ans * comb(b[idxb] - 1 - cnt, n - idxa - 1) % mod * f[n - idxa] % mod;
      cnt += n - idxa;
      idxb++;
    }
    else {
      ans = ans * comb(a[idxa] - 1 - cnt, n - idxa - 1 + m - idxb - 1) % mod * f[m - idxb - 1 + n - idxa - 1] % mod;
      cnt += n - idxa + m - idxb - 1;
      idxa++, idxb++;
    }
  }
  cout << ans << '\n';

  return 0;
}

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

int main(void){
  int n, m; bool ok = true;
  scanf("%d %d", &n, &m);
  vi as(n), bs(m), cs(n * m, 0); vector<bool> seen(n * m, false);
  rep(i, n) {scanf("%d", &as[i]); as[i]--; if (seen[as[i]]) ok = false; seen[as[i]] = true;}
  fill(all(seen), false);
  rep(i, m) {scanf("%d", &bs[i]); bs[i]--; if (seen[bs[i]]) ok = false; seen[bs[i]] = true;}
  if (!ok){printf("%d\n", 0); return 0;}

  sort(all(as)); sort(all(bs));
  ll ans = 1;
  rep(i, n) rep(j, m) if (as[i] == bs[j]) cs[as[i]] = 1;
  for (int a: as) if (!cs[a]) cs[a] = m - (lower_bound(all(bs), a) - bs.begin());
  for (int b: bs) if (!cs[b]) cs[b] = n - (lower_bound(all(as), b) - as.begin());
  for (int x = n * m - 1; x >= 0; x--){
    if (!cs[x]){
      cs[x] = m - (lower_bound(all(bs), x) - bs.begin());
      cs[x] *= n - (lower_bound(all(as), x) - as.begin());
      cs[x] -= n * m - x - 1;
    }
    (ans *= cs[x]) %= M;
  }

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

  return 0;
}


#include <iostream>
using namespace std;
int main(){
int A;
  int B;
  cin>>A>>B;
  int holes=1;
int ans=0;
  while(holes<B){
  holes--;
  holes+=A;
    ans++;
  }
  cout<<ans;
}

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

const int MAXN=500005;
const long MOD=(long)(1e9+7);
vector<int> adj[MAXN];
vector<pair<int,int>> G[MAXN];

Int N,M,K;
Int B,C,H,W;
Int X,Y,Z;
vector<Int> A;

Int ceilS(Int x,Int y){
    return (x+y-1)/y;
}
using D=long double;

Int perform(Int x){
    return (x*(x+1))/2LL;
}

int main()
{
        cin>>N>>M;
        --M;--N;
        cout<<ceilS(M,N)<<"\n";

}


#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define lowbit(x) (x)&(-x)
#define rep(i,a,b) for (int i=a;i<=b;i++)
#define per(i,a,b) for (int i=a;i>=b;i--)
#define maxd 998244353
typedef long long ll;
const int N=100000;
const double pi=acos(-1.0);
int n,a[400];
ll dp[310][90050][2];

int read()
{
	int x=0,f=1;char ch=getchar();
	while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
	while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
	return x*f;
}

int main()
{
	n=read();ll sum=0;
	rep(i,1,n) {a[i]=read();sum+=a[i];}
	dp[0][0][0]=1;dp[0][0][1]=1;
	rep(i,1,n)
	{
		rep(j,0,sum)
		{
			dp[i][j][0]=dp[i-1][j][0];
			dp[i][j][1]=(dp[i-1][j][1]*2)%maxd;
			if (j>=a[i])
			{
				dp[i][j][0]=(dp[i][j][0]+dp[i-1][j-a[i]][0])%maxd;
				dp[i][j][1]=(dp[i][j][1]+dp[i-1][j-a[i]][1])%maxd;
			}
		}
	}
	ll ans=1;
	rep(i,1,n) ans=(ans*3)%maxd;
	rep(i,(sum+1)/2,sum)
	{
		ans=(ans-dp[n][i][1]*3)%maxd;
		if (i*2==sum)
		{
			ans=(ans+dp[n][i][0]*3)%maxd;
		}
	}
	ans=(ans%maxd+maxd)%maxd;
	printf("%lld",ans);
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

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

template <typename T>
bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

template <typename T>
bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}

// doc: https://shibh308.github.io/library/library/lib/classes/modint.cpp.html
template <i64 mod = MOD>
struct ModInt{
    i64 p;

    ModInt() : p(0){}
    ModInt(i64 x){p = x >= 0 ? x % mod : x + (-x + mod - 1) / mod * mod;}

    ModInt& operator+=(const ModInt& y){p = p + *y - ((p + *y) >= mod ? mod : 0); return *this;}
    ModInt& operator-=(const ModInt& y){p = p - *y + (p - *y < 0 ? mod : 0); return *this;}
    ModInt& operator*=(const ModInt& y){p = (p * *y) % mod; return *this;}
    ModInt& operator%=(const ModInt& y){if(y)p %= *y; return *this;}

    ModInt operator+(const ModInt& y) const{ModInt x = *this; return x += y;}
    ModInt operator-(const ModInt& y) const{ModInt x = *this; return x -= y;}
    ModInt operator*(const ModInt& y) const{ModInt x = *this; return x *= y;}
    ModInt operator%(const ModInt& y) const{ModInt x = *this; return x %= y;}

    friend ostream& operator<<(ostream& stream, const ModInt<mod>& x){
        stream << *x;
        return stream;
    }

    friend ostream& operator>>(ostream& stream, const ModInt<mod>& x){
        stream >> *x;
        return stream;
    }

    ModInt& operator++(){p = (p + 1) % mod; return *this;}
    ModInt& operator--(){p = (p - 1 + mod) % mod; return *this;}

    bool operator==(const ModInt& y) const{return p == *y;}
    bool operator!=(const ModInt& y) const{return p != *y;}

    const i64& operator*() const{return p;}
    i64& operator*(){return p;}

};

using mint = ModInt<>;

signed main(){
    int n;
    cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; ++i){
        cin >> a[i];
    }
    mint ans = 1;
    vector<int> v(3, 0);
    for(int i = 0; i < n; ++i){
        int cnt = 0;
        int ex = 0;
        for(int j = 0; j < 3; ++j)
            if(v[j] == a[i]){
                ++cnt;
                ex = j;
            }
        ++v[ex];
        ans *= cnt;
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
  int e;
  while(cin >> e, e){
    int ans = 1 << 30;
    for(int z = 0; z * z * z <= e; z++){
      int y = floor(sqrt(e - z*z*z));
      ans = min(ans, e - y*y - z*z*z + y + z);
    }
    cout << ans << endl;
  }
  return 0;
}

#define _USE_MATH_DEFINES
#define INF 10000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>

using namespace std;

typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P > PP;

static const double eps = 1e-8;

int getAvr(int* p,int n){
	int sum = 0;
	for(int i=0;i<n;i++){
		sum += p[i];
	}

	return sum/3;
}

int main(){
	int n;
	while(~scanf("%d",&n)){
		if(n==0) break;

		for(int i=0;i<n;i++){
			int p[3];
			//math,english,japanese
			scanf("%d %d %d",p,p+1,p+2);

			for(int i=0;i<3;i++){
				if(p[i]==100){
					printf("A\n");
					goto next;
				}
			}

			if(p[0] + p[1] >= 180 || getAvr(p,3) >= 80){
				printf("A\n");
			}

			else if(getAvr(p,3) >= 70
				|| (getAvr(p,3) >= 50 && (p[0] >= 80 || p[1] >= 80))){
				printf("B\n");
			}

			else{
				printf("C\n");
			}
next:;

		}

	}
}


#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>;
const int MAX = 100000;
const int INF = 1e9;
const int dx[] = {-1,0,1,0};
const int dy[] = {0,-1,0,1};
#define PI 3.14159265358979323846264338327950L
#define MOD 1000000007
//setprecision(15)
ll mod_pow(ll x, ll n, ll mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1) res = res * x % mod;
		x = x * x % mod;
		n >>= 1;
	}
	return res;
}
#define MAX_N 15
int A[MAX_N+1][MAX_N+1];
int main() {
	memset(A, -1, sizeof(A));
	int N; cin >> N;
	for (int i = 1; i <= N; i++) {
		int cnt; cin >> cnt;
		rep(j, cnt) {
			int tmp; cin >> tmp;
			cin >> A[i][tmp];
		}
	}
	int p = mod_pow(2, N, MOD);
	int ans = 0;
	for (int i = 1; i < p; i++) {
		int NG_flg = 0;
		for (int j = 0; (1 << j) < p; j++ ) {
			if (NG_flg == 1) break;
			if (!(i & (1 << j))) continue;
			for (int k = 1; k <= N; k++ ) {
				if (A[j+1][k] == -1) continue;
				if ( (A[j+1][k] == 1 && (i & (1 << (k-1))) == 0) ||
				     (A[j+1][k] == 0 && (i & (1 << (k-1))) > 0) ) {
					NG_flg = 1; break;
				}
			}
		}
		if (NG_flg == 0) {
			int cnt = 0;
			rep(j, 16) {
				if (i & (1 << j)) cnt++;
			}
			ans = max(ans, cnt);
		}
	}
	cout << ans << endl;
}

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

int main()
{
	int n;
	cin>>n;
	vector<int> a(n);
	vector<int> b(n,0);
	int ans = 1000000000;
	for(int i = 0; i < n; i ++){
		int num;
		cin>>num;
		int localAns = 0;
		while( num % 2 == 0){
			num /= 2;
			localAns++;
		}
		ans = min(localAns,ans);
	}
	cout<<ans<<endl;
	return 0;
}


#include <iostream>
#include <memory>
#include <cmath>

constexpr int max_size()
{
    return static_cast<int>(1e+6);
}

int insert(int* a, const int i, const int gear)
{
    if (i < gear) {
        return 0;
    }

    if (a[i - gear] <= a[i]) {
        return 0;
    }

    std::swap(a[i - gear], a[i]);
    return 1 + insert(a, i - gear, gear);
}

int insertion_sort(int* arr, const int n, const int gear)
{
    int insert_count = 0;
    for (int i = 0; i < n; i += gear) {
        insert_count += insert(arr, i, gear);
    }
    return insert_count;
}

int pow(const int a, const int x) {
    return x == 0 ? 1 : a * pow(a, x - 1);
}

void print(int* arr, const int n, const int i, char const delim) {
    if (i == n - 1) {
        std::cout << arr[i];
        return;
    }

    std::cout << arr[i] << delim;
    print(arr, n, i + 1, delim);
}

void print(int* arr, const int n, char const delim) {
    int i = 0;
    for (; i < n - 1; ++i) {
        std::cout << arr[i] << delim;
    }
    std::cout << arr[i];
}

void shell_sort(int* arr, const int n) {
    static const double log3 = std::log(3);
    const int gear_index = static_cast<int>(std::log(2 * n + 1) / log3 - 1);
    int gear = (pow(3, gear_index + 1) - 1) >> 1;

    int gear_cache[100];
    int cache_idx = 0;
    int insert_count = 0;

    for (; gear > 0; gear = (gear - 1) / 3) {
        gear_cache[cache_idx] = gear;
        for (int i = 0; i < gear; ++i) {
            insert_count += insertion_sort(arr + i, n - i, gear);
        }
        ++cache_idx;
    }

    std::cout << cache_idx << std::endl;
    print(gear_cache, cache_idx, ' ');
    std::cout << std::endl;
    std::cout << insert_count << std::endl;

    return;
}

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

    int n;
    std::cin >> n;

    constexpr int N = max_size();
    const auto r = std::make_unique<int[]>(N);
    for (int i = 0; i < n; ++i) {
        std::cin >> r[i];
    }

    shell_sort(r.get(), n);
    print(r.get(), n, '\n');
    std::cout << std::endl;

    return 0;
}

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

#define FOR( i, m, n )	for( int (i) = (m); (i) < (n); i++ )
#define REP( i, n )		FOR( i, 0, n )
#define ALL( x )		(x).begin(), (x).end()

void shellSort( vector<int>& a );
void insertSort( vector<int>& a, int h );
void printArray( vector<int>& a );

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

	shellSort( a );
	printArray( a );

}

void shellSort( vector<int>& a ) {
	int h = 1, h_tmp, h_cnt = 0;
	for( h_tmp = 1; h_tmp < a.size(); h_tmp = h_tmp * 3 + 1 ) {
		h = h_tmp;
		h_cnt++;
	}

	cout << ( a.size() == 1 ? h_cnt + 1 : h_cnt ) << endl;

	while( h > 0 ) {
		if( h / 3 != 0 ) cout << h << " ";
		else			 cout << h << endl;
		insertSort( a, h );
		h /= 3;
	}
}

void insertSort( vector<int>& a, int h ) {
	static int cnt = 0;
	int tmp, j;
	FOR( i, h, a.size() ) {
		tmp = a[i];
		for( j = i; j >= h && a[j - h] > tmp; j -= h ) {
			cnt++;
			a[j] = a[j - h];
		}
		a[j] = tmp;
	}

	if( h == 1 ) cout << cnt << endl;
}

void printArray( vector<int>& a ) {
	REP( i, a.size() ) cout << a[i] << endl;
}


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

int main(void) {

    long long N,A,B;
    int i;
    long long sum = 0;

    scanf("%lld %lld %lld", &N, &A, &B);
    long long X[N];
    for(i=0;i<N;i++){
        scanf("%lld",&X[i]);
    }

    for(i=1;i<N;i++){
        long long dis = X[i] - X[i-1];
        if(B > dis * A){
            sum += dis * A;
        }
        else{
            sum += B;
        }
    }

    printf("%lld\n", sum);
}

#include <bits/stdc++.h>

const double PI = 3.14159265358979323846;
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
//#define rep(i, n) for (ll i = 0; i < (n); ++i)
typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

std::istream &operator>>(std::istream &in, set<ll> &o) {
    ll a;
    in >> a;
    o.insert(a);
    return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
    ll a;
    in >> a;
    o.push(a);
    return in;
}

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

//ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);

//const ll mod = 1e10;
//typedef priority_queue<P, vector<P>, greater<P> > PQ_ASK;

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

    vector<vector<int>> v(n, vector<int>(m));
    rep(i, n) rep(j, m) cin >> v[i][j], v[i][j]--;

//    rep(i, n) reverse(v[i].begin(), v[i].end());

    vector<bool> exclude(m, false);

    int ans = INT_MAX;

    while (count(exclude.begin(), exclude.end(), false) > 0) {
        vector<int> join(m);

        rep(i, n) {
            rep(j, m) {
                int t = v[i][j];
                if (!exclude[t]) {
                    join[t]++;
                    break;
                }
            }
        }

        int now = *max_element(join.begin(), join.end());
//        cout << now << endl;
        cmin(ans, now);
        rep(i, m) {
            if (join[i] == now) {
                exclude[i] = true;
            }
        }

    }
    cout << ans << endl;

}


#include<bits/stdc++.h>
using namespace std;
int n;
int D1,D2;
int get(int x,int y){return x*(n*2)+y;}
struct data{
    int to,nxt;
};
struct Graph{
    data mp[20000200];
    int head[500500],cnt;
    void link(int x,int y)
    {
        mp[++cnt].to=y;
        mp[cnt].nxt=head[x];
        head[x]=cnt;
    }
    int fl[2020][2],tf;
    int c[500500];
    void dfs(int x,int d)
    {
        c[x]=d;
        for(int i=head[x];i;i=mp[i].nxt)
            if(c[mp[i].to]==-1)
                dfs(mp[i].to,d^1);
    }
    void build(int D)
    {
        for(int i=0,j;i*i<=D;++i)
        {
            j=sqrt(D-i*i);
            if(i*i+j*j!=D)continue;
            fl[++tf][0]=i;fl[tf][1]=j;
            fl[++tf][0]=i;fl[tf][1]=-j;
            fl[++tf][0]=-i;fl[tf][1]=j;
            fl[++tf][0]=-i;fl[tf][1]=-j;
        }
        int u,x,y;
        for(int i=0;i<n*2;++i)
            for(int j=0;j<n*2;++j)
            {
                u=get(i,j);
                for(int p=1;p<=tf;++p)
                {
                    x=i+fl[p][0];y=j+fl[p][1];
                    if(x<0||x>=n*2||y<0||y>=n*2)continue;
                    link(u,get(x,y));
                }
            }
        memset(c,-1,sizeof(c));
        for(int i=0;i<n*2;++i)
            for(int j=0;j<n*2;++j)
                if(c[get(i,j)]==-1)dfs(get(i,j),0);
    }
}g1,g2;
int cnt[2][2];
int main()
{
    scanf("%d%d%d",&n,&D1,&D2);
    g1.build(D1);
    g2.build(D2);
    int x;
    for(int i=0;i<n*2;++i)
        for(int j=0;j<n*2;++j)
        {
            x=get(i,j);
            cnt[g1.c[x]][g2.c[x]]++;
        }
    int ou=1;
    for(int i=0;i<2&&ou<=n*n;++i)
        for(int j=0;j<2&&ou<=n*n;++j)
        {
            if(cnt[i][j]<n*n)continue;
            for(int p=0;p<n*2&&ou<=n*n;++p)
                for(int q=0;q<n*2&&ou<=n*n;++q)
                {
                    x=get(p,q);
                    if(g1.c[x]==i&&g2.c[x]==j)
                    {
                        ou++;
                        printf("%d %d\n",p,q);
                    }
                }
        }
    return 0;
}

#include<bits/stdc++.h>
int n,d1,d2,color[606][606],N;
void paint(int d){
    int b=0;
    while(!(d&3))d>>=2,++b;
    if(d&1){
        for(int i=0;i<N;++i)
        for(int j=0;j<N;++j)
        if(((i>>b)+(j>>b))&1)color[i][j]=1;
    }else{
        for(int i=0;i<N;++i)
        for(int j=0;j<N;++j)
        if((i>>b)&1)color[i][j]=1;
    }
}
int main(){
    scanf("%d%d%d",&n,&d1,&d2);
    N=n<<1;
    paint(d1),paint(d2);
    int NN=n*n;
    for(int i=0;i<N;++i)
    for(int j=0;j<N;++j)
    if(!color[i][j]){
        printf("%d %d\n",i,j);
        if(!--NN)return 0;
    }
}


#include<bits/stdc++.h>

using namespace std;

struct node
{
	int num,fa,ls,rs;
	node(int _num,int _fa)
	{
		num=_num;
		fa=_fa;
		ls=rs=-1;
	}
};

typedef vector<node> Tree;

Tree T;

void insert(int t,int n)
{
	if(T[0].num==INT_MAX)
		T[0].num=n;
	else if(T[t].num>n)
		if(T[t].ls==-1)
		{
			T[t].ls=T.size();
			T.push_back(node(n,t));
		}
		else insert(T[t].ls,n);
	else
		if(T[t].rs==-1)
		{
			T[t].rs=T.size();
			T.push_back(node(n,t));
		}
		else insert(T[t].rs,n);
}

void find(int t,int x)
{
	if(T.size()==0||t==-1)
		printf("no\n");
	else if(T[t].num==x)
		printf("yes\n");
	else if(x<T[t].num)
		find(T[t].ls,x);
	else find(T[t].rs,x);
}

void lc(int fa,int son)
{
	T[fa].ls=son;
	if(son!=-1) T[son].fa=fa;
}

void rc(int fa,int son)
{
	T[fa].rs=son;
	if(son!=-1) T[son].fa=fa;
}

void del(int t,int x)
{
	if(t==-1) return ;
	if(T[t].num==x)
	{
		if(T[t].ls==-1)
			if(T[t].rs==-1)
				if(T[T[t].fa].ls==t)
					T[T[t].fa].ls=-1;
				else T[T[t].fa].rs=-1;
			else
				if(T[T[t].fa].ls==t)
				{
					T[T[t].fa].ls=T[t].rs;
					T[T[t].rs].fa=T[t].fa;
				}
				else
				{
					T[T[t].fa].rs=T[t].rs;
					T[T[t].rs].fa=T[t].fa;
				}
		else if(T[t].rs==-1)
				if(T[T[t].fa].ls==t)
				{
					T[T[t].fa].ls=T[t].ls;
					T[T[t].ls].fa=T[t].fa;
				}
				else
				{
					T[T[t].fa].rs=T[t].ls;
					T[T[t].ls].fa=T[t].fa;
				}
			else
			{
				int mir=T[t].rs;
				while(T[mir].ls!=-1) mir=T[mir].ls;
				del(t,T[mir].num);
				T[t].num=T[mir].num;
			}
	}
	else if(x<T[t].num)
		del(T[t].ls,x);
	else del(T[t].rs,x);
}

void Preorder(int t)
{
	if(t==-1) return;
	printf(" %d",T[t].num);
	Preorder(T[t].ls);
	Preorder(T[t].rs);
}

void Inorder(int t)
{
	if(t==-1) return;
	Inorder(T[t].ls);
	printf(" %d",T[t].num);
	Inorder(T[t].rs);
}

void print()
{
	Inorder(T[1].ls);
	printf("\n");
	Preorder(T[1].ls);
	printf("\n");
}

int main()
{
	T.clear();
	T.push_back(node(INT_MAX,1));
	T.push_back(node(-1,-1));
	T[1].ls=0;
	int n;
	scanf("%d",&n);
	while(n--)
	{
		char s[10];
		scanf("%s",s);
		if(s[0]=='i')
		{
			int t;
			scanf("%d",&t);
			insert(T[1].ls,t);
		}
		else if(s[0]=='f')
		{
			int t;
			scanf("%d",&t);
			find(T[1].ls,t);
		}
		else if(s[0]=='d')
		{
			int t;
			scanf("%d",&t);
			del(T[1].ls,t);
		}
		else print();
	}
	return 0;
}

# include "bits/stdc++.h"
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T HINF() { return INF<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
typedef pair<LL, LL> pii;
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { -1,0,1,0 }, dy[4] = { 0,-1,0,1 };
const char dir[4] = { 'u','l','d','r' };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        sort(ALL((wpwpw)));(wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy)   for(LL i=(tptpt);i<(ypypy);i++)
# define REP(i,upupu)         FOR(i,0,upupu)
# define INIT                 std::ios::sync_with_stdio(false);std::cin.tie(0)
# pragma warning(disable:4996)

class node {
public:
	int key;
	node *parent, *lch, *rch;
};

node *NIL, *root;

void insert(int v) {
	node *y = NIL, *x = root, *z = new node;
	z->key = v; z->lch = z->rch = NIL;
	while (x != NIL) {
		y = x;
		if (z->key < x->key)x = x->lch;
		else x = x->rch;
	}
	z->parent = y;
	if (y == NIL)root = z;
	else if (z->key < y->key)y->lch = z;
	else y->rch = z;
	return;
}

node* find(int k) {
	node *x = root;
	while (x != NIL&&k != x->key) {
		if (k < x->key)x = x->lch;
		else x = x->rch;
	}
	return x;
}

node* next(node *z) {
	node *x, *y;
	if (z->rch != NIL) {
		x = z->rch;
		while (x->lch != NIL)x = x->lch;
		return x;
	}

	y = z->parent;
	while (y != NIL&&x == y->rch) {
		x = y;
		y = y->parent;
	}
	return y;
}

void remove(node *z) {
	node *y, *x;
	if (z->lch == NIL || z->rch == NIL)y = z;
	else y = next(z);

	if (y->lch != NIL)x = y->lch;
	else x = y->rch;

	if (x != NIL)x->parent = y->parent;

	if (y->parent == NIL)root = x;
	else if (y == y->parent->lch)y->parent->lch = x;
	else y->parent->rch = x;

	if (y != z)z->key = y->key;
	delete(y);
	return;
}

void ino(node *u) {
	if (u == NIL)return;
	ino(u->lch);
	cout << " " << u->key;
	ino(u->rch);
	return;
}

void preo(node *u) {
	if (u == NIL)return;
	cout << " " << u->key;
	preo(u->lch);
	preo(u->rch);
	return;
}

void print() {
	ino(root);
	cout << endl;
	preo(root);
	cout << endl;
	return;
}

int main() {
	int n; cin >> n;
	string s; node *a;
	while (cin >> s) {
		if (s == "insert") {
			cin >> n;
			insert(n);
		}
		else if (s == "find") {
			cin >> n;
			a = find(n);
			if (a != NIL)cout << "yes" << endl;
			else cout << "no" << endl;
		}
		else if (s == "delete") {
			cin >> n;
			remove(find(n));
		}
		else if (s == "print")print();
	}
	return 0;
}


#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

#define mp make_pair
#define pb push_back
#define sz(s) ((int) ((s).size()))
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)

#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif

typedef long long ll;
typedef long double ld;

const int inf = (int) 1e9 + 100;
const ld eps = 1e-11;
const ld pi = acos(-1.0L);

mt19937 mrand(random_device{} ());
int rnd(int x) {
  return mrand() % x;
}

void precalc() {
}

const int maxn = 1e6 + 100;
int n;
char s[maxn], t[maxn];

bool read() {
  if (scanf("%d", &n) < 1) {
    return false;
  }
  scanf("%s", s);
  scanf("%s", t);
  return true;
}

vector<int> pos[300];

void solve() {
  {
    bool ok = true;
    for (int i = 0; i < n; i++) {
      if (s[i] != t[i]) {
        ok = false;
      }
    }
    if (ok) {
      printf("0\n");
      return;
    }
  }
  for (int i = 0; i < 300; i++) {
    pos[i].clear();
  }
  for (int i = 0; i < n; i++) {
    pos[s[i]].pb(i);
  }
  vector<int> xs;
  vector<int> want;
  for (int i = n - 1; i >= 0; i--) {
    if (i == 0 || t[i] != t[i - 1]) {
      int mx = sz(xs) ? xs.back() : inf;
      mx = min(mx, i);
      int j = (int) (upper_bound(pos[t[i]].begin(), pos[t[i]].end(), mx) - pos[t[i]].begin());
      j--;
      if (j == -1) {
        printf("-1\n");
        return;
      }
      xs.pb(pos[t[i]][j]);
      want.pb(i);
    }
  }
  reverse(xs.begin(), xs.end());
  reverse(want.begin(), want.end());
  vector<int> magic;
  for (int i = 0; i < sz(xs); i++) {
    magic.pb(xs[i] - i);
  }
  magic.pb(want.back() - sz(xs) + 1);
  int ans = 1;
  for (int i = 0; i < sz(xs); i++) {
    int j = (int) (lower_bound(magic.begin(), magic.end(), want[i] - i) - magic.begin());
    ans = max(ans, 1 + j - i);
  }
  printf("%d\n", ans);
}

int main() {
  precalc();
#ifdef DEBUG
  assert(freopen("text.in", "r", stdin));
  assert(freopen("text.out", "w", stdout));
#endif

  while (true) {
    if (!read()) {
      break;
    }
    solve();
#ifdef DEBUG
    eprintf("Time: %.18f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
char s[N],t[N];
int n,ans,l=1,r,last,q[N];
int main(){
	scanf("%d%s%s",&n,s+1,t+1),last=n;
	for(int i=n;i;){
		while(last&&(i<last||s[last]!=t[i])) --last;
		if(!last)	{puts("-1");return 0;}
		while(l<=r&&q[l]-r+l-1>=i)	++l;
		q[++r]=last;
		if(i!=last) ans=max(ans,r-l+1);
		while(t[i]==s[last]&&i>=last)	--i;
	}
	printf("%d\n",ans);
	return 0;
}


#include<iostream>
#include<string>

int main() {
	std::string s;
	std::cin >> s;

	if (s == "RRR") std::cout << 3 << std::endl;
	if (s == "RRS") std::cout << 2 << std::endl;
	if (s == "RSR") std::cout << 1 << std::endl;
	if (s == "SRR") std::cout << 2 << std::endl;
	if (s == "RSS") std::cout << 1 << std::endl;
	if (s == "SRS") std::cout << 1 << std::endl;
	if (s == "SSR") std::cout << 1 << std::endl;
	if (s == "SSS") std::cout << 0 << std::endl;

	return 0;
}

/*
	LINES :
		* rainy days
*/
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

// #define OJ
#define pb push_back
#define F first
#define S second

#define WLL(t) while(t--)
#define FOR(i,a,b) for(int i = a; i < b; i++)
#define STF(i, a, b) for(size_t i = a; i < b; i++)
#define RFOR(i, a, b) for(int i = a; i >= b; i--)
#define SRF(i, a, b) for(size_t i = a;i >= b;i--)

#define ALL(v) v.begin(),v.end()
#define PRECISION(x) cout<<fixed<<setprecision(x)
#define FASTIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define READ freopen("input.txt", "r", stdin)
#define WRITE freopen("output.txt", "w", stdout)
#define MOD 1000000007

#define TO(v, type) static_cast<type>(v)

int main()
{
#ifdef OJ
	READ;
	WRITE;
#endif

	FASTIO;
	string s;
	cin >> s;
	int mx = 0;
	int count{};
	STF(i, 0, 3) {
		if (s[i] == 'R') {
			count++;
		}
		else {
			count = 0;
		}
		mx = max(count, mx);
	}
	cout << mx << "\n";
	return 0;
}


#include <algorithm>
#include <climits>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <unordered_map>
#include <vector>

const int mod = 1e9 + 7;
const int kmax = 510000;
const int last_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

long long fact[kmax], fact_inv[kmax], inv[kmax];

void init_comb()
{
    fact[0] = fact[1] = 1;
    fact_inv[0] = fact_inv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < kmax; i++) {
        fact[i] = fact[i - 1] * i % mod;
        inv[i] = mod - inv[mod % i] * (mod / i) % mod;
        fact_inv[i] = fact_inv[i - 1] * inv[i] % mod;
    }
}

long long comb(int n, int r)
{
    if (n < r) {
        return 0;
    }

    if (n < 0 || r < 0) {
        return 0;
    }

    return fact[n] * (fact_inv[r] * fact_inv[n - r] % mod) % mod;
}

template <typename T, T N>
class UnionFind {
    T parent_[N];
    T rank_[N];
    T size_[N];

public:
    UnionFind();
    T Root(T idx);
    bool IsSame(T x, T y);
    void Unite(T x, T y);
    T GetSize(T idx);
};

template <typename T, T N>
UnionFind<T, N>::UnionFind()
{
    for (T i = 0; i < N; i++) {
        parent_[i] = i;
        rank_[i] = 0;
        size_[i] = 1;
    }
}

template <typename T, T N>
T UnionFind<T, N>::Root(T idx)
{
    return parent_[idx] == idx ? idx : parent_[idx] = Root(parent_[idx]);
}

template <typename T, T N>
bool UnionFind<T, N>::IsSame(T x, T y)
{
    return Root(x) == Root(y);
}

template <typename T, T N>
void UnionFind<T, N>::Unite(T x, T y)
{
    x = Root(x);
    y = Root(y);
    if (x == y) {
        return;
    }

    if (rank_[x] < rank_[y]) {
        parent_[x] = y;
        size_[y] += size_[x];
    } else {
        parent_[y] = x;
        size_[x] += size_[y];
        if (rank_[x] == rank_[y]) {
            rank_[x]++;
        }
    }
}

template <typename T, T N>
T UnionFind<T, N>::GetSize(T idx)
{
    return size_[Root(idx)];
}

long long pow_mod(long long n, long long p, long long m)
{
    if (p == 0) {
        return 1;
    }

    if (p % 2 == 0) {
        long long t = pow_mod(n, p / 2, m);
        return t * t % m;
    }
    return n * pow_mod(n, p - 1, m) % mod;
}

long long nCr_mod(long long n, long long r)
{
    long long x = 1;
    for (long long i = n - r + 1; i <= n; i++) {
        x *= i;
        x %= mod;
    }

    long long a = 1;
    for (long long i = 1; i <= r; i++) {
        a *= i;
        a %= mod;
    }

    long long y = pow_mod(a, mod - 2, mod) % mod;

    return x * y % mod;
}

bool is_prime(long long n)
{
    if (n == 1) {
        return false;
    }

    for (long long i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            return false;
        }
    }

    return true;
}

bool is_leap(long long y)
{
    return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
}

void next_day(long long& y, long long& m, long long& d)
{
    d++;

    if (d > last_days[m - 1] + (m == 2 && is_leap(y) ? 1 : 0)) {
        d = 1;
        m++;
    }

    if (m > 12) {
        y++;
        m = 1;
    }
}

long long fib(long long n)
{
    long long a = 0, b = 1;
    for (int i = 0; i < n; i++) {
        long long t = a;
        a = b;
        b = a + t;
    }

    return a;
}

int main()
{
    long long x, y;
    std::cin >> x >> y;

    if (x < y) {
        if (x * y < 0) {
            std::cout << std::min(y - x, 1 + std::abs(std::abs(y) - std::abs(x))) << std::endl;
            return 0;
        }
        std::cout << y - x << std::endl;
    } else if (x * y <= 0) {
        std::cout << std::abs(std::abs(x) - std::abs(y)) + 1 << std::endl;
    } else {
        std::cout << std::abs(x - y) + 2 << std::endl;
    }
}

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

lli x, y;

int main(void){
	cin >> x >> y;
	lli ans = 1e18;
	rep(i, 2){
		rep(j, 2){
			lli xc, yc;
			xc = x;
			yc = y;
			lli c = 0;
			if(i == 1) xc*=-1, c++;
			if(j == 1) yc*=-1, c++;
			if(xc <= yc) c+=yc-xc;
			else c+=1e18;
			ans = min(ans, c);
		}
	}
	cout << ans << endl;
	return 0;
}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long

const int N = 1e5 + 5;

struct Gauss
{
	static const int bits = 60;

	int table[bits];

	Gauss()
	{
		for(int i = 0; i < bits; i++)
			table[i] = 0;
	}

	int size()
	{
		int ans = 0;
		for(int i = 0; i < bits; i++)
		{
			if(table[i])
				ans++;
		}
		return ans;
	}

	bool can(int x)
	{
		for(int i = bits-1; i >= 0; i--)
			x = min(x, x ^ table[i]);
		return x == 0;
	}

	void add(int x)
	{
		for(int i = bits-1; i >= 0 && x; i--)
		{
			if(table[i] == 0)
			{
				table[i] = x;
				x = 0;
			}
			else
				x = min(x, x ^ table[i]);
		}
	}

	int getBest()
	{
		int x = 0;
		for(int i = bits-1; i >= 0; i--)
			x = max(x, x ^ table[i]);
		return x;
	}

	void merge(Gauss &other)
	{
		for(int i = bits-1; i >= 0; i--)
			add(other.table[i]);
	}
};

int n;
int a[N];
Gauss g;

int32_t main()
{
	IOS;
	cin >> n;
	int curXor = 0;
	for(int i = 1; i <= n; i++)
	{
		cin >> a[i];
		curXor ^= a[i];
	}
	for(int i = 1; i <= n; i++)
	{
		a[i] = a[i] & (((1LL << 60) - 1) ^ curXor);
		g.add(a[i]);
	}
	cout << curXor + g.getBest() * 2;
	return 0;
}

#include<bits/stdc++.h>

using namespace std;

const int N = 15;

void cmax(int& x, int y) {
  if (x < y) {
    x = y;
  }
}

int n, m, dist[N][N], value[1 << N], dp[1 << N][N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  int sum = 0;
  for (int i = 0, a, b, c; i < m; ++i) {
    cin >> a >> b >> c;
    --a;
    --b;
    dist[a][b] = dist[b][a] = c;
    sum += c;
  }
  int all = 1 << n;
  for (int i = 1; i < all; ++i) {
    int p = 0;
    while (!(i >> p & 1)) {
      ++p;
    }
    value[i] = value[i ^ (1 << p)];
    for (int j = 0; j < n; ++j) {
      if (j != p && (i >> j & 1)) {
        value[i] += dist[p][j];
      }
    }
  }
  memset(dp, 0xc0, sizeof dp);
  for (int i = 1; i < all; ++i) {
    if ((i & 1) && !(i >> n - 1 & 1)) {
      dp[i][0] = value[i];
    }
  }
  for (int i = 1; i < all; ++i) {
    for (int j = 1; j < n; ++j) {
      if (i >> j & 1) {
        for (int k = (i - 1) & i; k; k = (k - 1) & i) {
          if ((k & 1) && !(k >> j & 1)) {
            for (int l = 0; l < n; ++l) {
              if ((k >> l & 1) && dist[j][l]) {
                cmax(dp[i][j], dp[k][l] + value[i ^ k] + dist[j][l]);
              }
            }
          }
        }
      }
    }
  }
  int answer = 0;
  for (int i = 1; i < all; ++i) {
    cmax(answer, dp[i][n - 1]);
  }
  cout << sum - answer << '\n';
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
    return '"' + s + '"';
}

string to_string(const char* s) {
    return to_string((string) s);
}

string to_string(bool b) {
    return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
    bool first = true;
    string res = "{";
    for (int i = 0; i < static_cast<int>(v.size()); i++) {
        if (!first) {
            res += ", ";
        }
        first = false;
        res += to_string(v[i]);
    }
    res += "}";
    return res;
}

template <size_t N>
string to_string(bitset<N> v) {
    string res = "";
    for (size_t i = 0; i < N; i++) {
        res += static_cast<char>('0' + v[i]);
    }
    return res;
}

template <typename A>
string to_string(A v) {
    bool first = true;
    string res = "{";
    for (const auto &x : v) {
        if (!first) {
            res += ", ";
        }
        first = false;
        res += to_string(x);
    }
    res += "}";
    return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
    return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
    return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
    return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

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

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

int main() {
    long long int n, k; cin >> n >> k;
    vector<long long> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    long long ans = k;
    for (int i = 1; i < n; i++) {
        if (a[i] - a[i - 1] < k) {
            ans += a[i] - a[i - 1];
        } else {
            ans += k;
        }
    }
    cout << ans;
}

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<cmath>
#include<cstdio>
#include<tuple>
#include<bitset>

using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define ALL(x) x.begin(),x.end()
#define ll long long
const int inf=1000000007;
const int MOD=1000000007;
int n,m;
struct Map{
  vector<vector<int>> map;
  void mkmap(vector<int> const& a,vector<int> const& b,vector<int>& num){
    //num.resize(n*m+1,0);
    rep(i,n){
      map.push_back(vector<int> (m,inf));
    }
    rep(i,n){
      rep(j,m){
        map[i][j]=min(a[i],b[j]);
        if(a[i]==b[j])map[i][j]=999;
      }
    }
  }
  void show_map(){
    rep(i,n){
      rep(j,m){
        cout<<map[i][j]<<" ";
      }
      cout<<endl;
    }
  }
};
signed main(){
  cin>>n>>m;
  vector<int> a(n);
  vector<int> b(m);
  vector<int> sum;
  vector<int> memo(n*m+1,0);
  vector<int> num(n*m+1,0);
  vector<int> list;
  rep(i,n)cin>>a[i];
  rep(i,m)cin>>b[i];
  //Map grid;
  //grid.mkmap(a,b,num);
  rep(i,n){
    rep(j,m){
      if(a[i]==b[j]){
        sum.push_back(inf);
        ++num[a[i]];
        list.push_back(a[i]);
      }
      else{
        sum.push_back(min(a[i],b[j]));
      }
    }
  }
  sort(ALL(sum));
  rep1(i,n*m){
    memo[i]=memo[i-1];
    if(num[i]==1)++memo[i];
    else if(num[i]>1){
      cout<<0<<endl;
      return 0;
    }
  }
  int cur=1;
  int res=1;
  sum.push_back(inf);
  sum.push_back(inf);
  int last=sum[0];

  rep1(i,n*m){
    if(last==sum[i]){
      cur++;
      continue;
    }
    else{
      if(num[last]==1){
        rep1(k,cur){
          res*=last-1-memo[last]-i+k+1;
          res%=MOD;
        }
      }
      else{
        res*=cur;
        rep1(k,cur-1){
          res*=last-1-memo[last]-i+k+1;
          res%=MOD;
        }
      }
      cur=1;
    }
    //cout<<sum[i]-memo[sum[i]]-i<<endl;
    if(res<0)res=0;
    res%=MOD;
    last=sum[i];
    if(sum[i]==inf){
      break;
    }
  }

  cout<<res<<endl;
  //grid.show_map();
  /*rep(i,list.size()){
    cout<<list[i]<<endl;
  }*/
  //cout<<memo[182]<<endl;
  return 0;
}


// <------------- Template -----------------------> //

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

#define fastio 			ios::sync_with_stdio(0);	cin.tie(0); cout.tie(0);  cout<<fixed;  cout<<setprecision(0);
#define randomINT 		mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define	newl 			cout<<"\n"
#define DISP(as)		for(auto it : as) cout<<it<<" ";newl;
#define all(x)         	(x).begin(),(x).end()
#define mset(x,val)    	memset(x,val,sizeof(x))
#define newl           	cout<<"\n"
#define pb             	push_back
#define mp             	make_pair
#define f 				first
#define s 				second
#define dline          cerr<<"///REACHED///\n";
#define deb1(x)        cerr<<#x<<" = "<<x<<'\n';
#define deb2(x,y)      cerr<<'['<<#x<<','<<#y<<"] = "<<'['<<x<<','<<y<<']'<<'\n';
#define deb3(x,y,z)    cerr<<'['<<#x<<','<<#y<<','<<#z<<"] = "<<'['<<x<<','<<y<<','<<z<<']'<<'\n';
#define N 1000000007 // prime modulo value

typedef long long 				ll;
typedef long double 			ld;
typedef vector<ll> 				vll;
typedef pair<ll , ll> 			pll;
typedef pair<ld, ld> 			pld;
typedef unordered_map<ll, ll> 	um;
typedef vector<pll> 			vpll;

const ll 	MAX5 	= 	1e+5 + 7;
const ll 	MAX7 	= 	1e+7 + 7;
const ll 	MAXN 	= 	2 * 1e18;
const ll   	INF   	=  	0x7f7f7f7f7f7f7f7f;
const int  	INFi 	=  	0x7f7f7f7f;
const ll 	MOD		=  	1e+9 + 7;

vll adj[MAX5];

// <------------- Declare Variables Here ------------> //

ll T = 1;
ll n, x, y, q, m, k;
string s, t, d;
ll sum = 0;
ll cou = 0;

// <------------- Implement Functions Here ----------> //

// <------------- Start of main() -------------------> //

void MAIN() {
	cin >> n >> x;
	vll a(n);
	for(ll i=0;i<n;i++) cin>>a[i];
		sort(all(a));
	reverse(all(a));
	ll sum = 0;
	for(ll i=0;i<x;i++) sum+=a[i];
	cout<<sum;
}

int main() {
	fastio; randomINT;
	//cin>>T;
	while (T--) {
		MAIN();
	}
	return 0;
}

/*
g++ --std=c++17 A.cpp
*/

#include <iostream>
#include <iomanip> //! for setprecision(20)
#include <math.h>
#include <algorithm>
#include <functional>
#include <string>
#include <vector>

#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <utility>
#include <limits.h>
#include <atcoder/all>

using namespace std;
using namespace atcoder;

typedef long long LL;

#define rep(i,n) for (int i = 0; i < int(n); ++i)
#define dump(c) { for (auto it = c.begin(); it != c.end(); ++it) if (it == c.begin()) cout << *it; else cout << ' ' << *it; cout << endl; }
#define dumpMap(m) { for (auto it: m) cout << it.first << "=>" << it.second << ' '; }

const int MOD = 1000000007;
using mint = static_modint<MOD>;

/*
  == AC Library Cheat sheet
  documentation: file:///Users/nobu/Downloads/ac-library/document_ja/index.html

  mint
    mint m.pow(int p)  //! return m^p
    mint m.inv() //! returns i that gives (m * i).val() == 1
    int m.val()

  fenwick_tree (BIT)
    fenwick_tree<T> fw(int n) //! init a[0] .. a[n-1] with all 0
    void fw.add(int idx, T x); //! a[idx] += x
    T fw.sum(int l, int r); //! return a[l] + .. + a[r-1]

  dsu (UnionFind)
    dsu d(int n)  //! prepare dsu with n nodes
    void d.merge(int x, int y)  //! connect node x and y
    bool d.same(int x, int y)  //! return true if node x and y are connected
    int d.leader(int x)  //! return the leader node of the connected group
    int d.size(int x) //! return the size of the group that node x belongs to
    vector<vector<int>> d.groups() //! return a vector of vectors that contain the nodes in each group

*/

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

    int n, q;
    cin >> n >> q;
    fenwick_tree<LL> fw(n);
    rep(i, n) {
        int a;
        cin >> a;
        fw.add(i, a);
    }

    rep(i, q) {
        int t;
        cin >> t;
        if (t == 0) {
            int p, x;
            cin >> p >> x;
            fw.add(p, x);
        } else if (t == 1) {
            int l, r;
            cin >> l >> r;
            cout << fw.sum(l, r) << endl;
        }
    }

    return 0;
}