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

#define all(v) v.begin(), v.end()

const ll mod=1000000007;

ll countBits(ll in){
  int res=0;
  for(;in>0;in>>=1){
    if((in&0x01)!=0){
      res++;
    }
  }
  return res;
}

template<typename T>
void show2dVector(vector<vector<T>>& v){
  for(int i=0;i<v.size();i++){
    int m=v[i].size();
    cout<<i<<" : ";
    for(int j=0;i<m;i++){
      cout<<v[i][j]<<" ";
    }
    cout<<endl;
  }
}

void bfs(const vector<vector<int>>&g, int v, vector<bool>& seen){
  seen[v]=true;
  cout<<v<<" ";
  for(auto next: g[v]){
    if(seen[next]){
      continue;
    }
    bfs(g,next,seen);
  }
}

bool dfs(vector<vector<int>> &g,int start,int goal,vector<bool>&seen){
  bool res=false;
  seen[start]=true;
  if(start==goal){
    return true;
  }
  for(auto next: g[start]){
    if(seen[next]){
      continue;
    }
    res=dfs(g,next,goal,seen);
    if(res){
      break;
    }
  }
  return res;
}

ll gcd(ll a,ll b) {
  return b? gcd(b,a%b): a;
}
ll lcm(ll a,ll b) {
  return a*b/gcd(a,b);
}
bool isLowerCase(char c){
  return (c>='a'&&c<='z');
}

char toLowerCase(char c){
  if(isLowerCase(c)){
    return c;
  }else{
    return c+'a'-'A';
  }
}
char toUpperCase(char c){
  if(isLowerCase(c)){
    return c-('a'-'A');
  }else{
    return c;
  }
}
ll powm(ll a,ll n, ll m){
  ll ret=1;
  while(n>0){
    if(n%2==1){
      ret=(ret*a)%m;
    }
    n>>=1;
    a=(a*a)%m;
  }
  return ret;
}

const string yesno(bool ans){
  return (ans?"Yes":"No");
}
void func(vector<int>& l, int depth, int idx){
  ;
}
int main() {
  ll a[2],k;cin>>a[0]>>a[1]>>k;
  for(int i=0;i<k;i++){
    ll half=a[i%2]/2;
    if(a[i%2]%2!=0){
      a[i%2]--;
    }
    a[i%2]-=half;
    a[(i+1)%2]+=half;
  }
  cout<<a[0]<<" "<<a[1]<<endl;
  return 0;
}

#include<bits/stdc++.h>
#define rep(i,N) for(ll (i)=0;(i)<(N);(i)++)
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int mod = 1000000007;

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

  rep(i, k) {
    if(i % 2 == 0) {
      a /= 2;
      b += a;
    } else {
      b /= 2;
      a += b;
    }
  }
  cout << a << " " << b << endl;
}


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

int main() {
  int N,M;
  cin >> N;
  vector<string> blue(N);
  int MAX = 0;

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

  cin >> M;
  vector<string> red(M);

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

  int count;
  string S;

  for(int i = 0; i < N; i++){
    count = 0;
    S = blue.at(i);
    for(int j = 0; j < N; j++){
      if(S == blue.at(j)){
        count++;
      }
    }
   for(int k = 0; k < M; k++){
     if(S == red.at(k)){
       count--;
     }
   }
   if(MAX < count){
     MAX = count;
   }

  }

  if(MAX < 1){
    cout << 0 << endl;
  }else{
  cout << MAX << endl;
  }

}

#define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>
#include <cstring>
#include <cmath>
#include <stack>
#include <iomanip>
#include <tuple>
#include <functional>
#include <cfloat>
#include <map>
#include <set>
#include <array>
#include <stdio.h>
#include <string.h>
#include <random>
using ll = long long;
using ull = unsigned long long;
using namespace std;
#define int long long
#define CONTAINS_VEC(v,n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a)+(size))
#define REMOVE(v,a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define ARY_REVERSE(v,a) (reverse((v), (v)+(a)))
#define REP(i, n) for (int (i)=0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
#define CONTAINS_SET(m, a) (m).find((a)) != m.end()
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }

int N;
int M;
map<string, int> S;
map<string, int> T;

signed main()
{
	cin >> N;
	REP(i, N)
	{
		string s;
		cin >> s;
		if (CONTAINS_MAP(S, s))
		{
			S[s]++;
		}
		else
		{
			S[s] = 1;
		}
	}
	cin >> M;
	REP(i, M)
	{
		string s;
		cin >> s;
		if (CONTAINS_MAP(T, s))
		{
			T[s]++;
		}
		else
		{
			T[s] = 1;
		}
	}

	int ma = 0;
	for (auto pair : S)
	{
		int n = pair.second;
		if (CONTAINS_MAP(T, pair.first))
		{
			n -= T[pair.first];
		}
		ma = max(ma, n);
	}
	cout << ma << endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
struct Graph{
  Graph(int n):E(n),d(n){}
  vector<vector<tuple<int,int,int>>>E;
  vector<int>d;
  void add_edge(int a,int b,int c,int d){
    E[a].push_back(make_tuple(b,c,d));
  }
  void dijkstra(int s,int f){
    priority_queue<P,vector<P>,greater<P>>q;
    fill(d.begin(),d.end(),1e9);
    q.push({d[s]=0,s});
    while(q.size()){
      auto p=q.top();q.pop();
      if(p.first>d[p.second])continue;
      for(auto& a:E[p.second]){
        int x=get<0>(a),y=get<1>(a)+p.first;
        if(f)y=get<2>(a)+p.first;
        if(y<d[x])d[x]=y,q.push({y,x});
      }
    }
  }
};
main(){
  int n,m,a,b,c,d,k;
  while(cin>>n>>m,n){
    Graph E(m);
    for(int i=0;i++<n;E.add_edge(--a,--b,c,d),E.add_edge(b,a,c,d))cin>>a>>b>>c>>d;
    cin>>k;
    for(int i=0;i++<k;cout<<E.d[--b]<<endl){
      cin>>a>>b>>c;
      E.dijkstra(--a,c);
    }
  }
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;

int day_of_week(int y,int m,int d){
	static int t[]={0,3,2,5,0,3,5,1,4,6,2,4};
	y-=m < 3;
	return ( y + y/4 - y/100 + y/400 + t[m-1] + d) % 7 ;
}

int main(){
	int m , d , w=4 , fw;
	while (cin >> m >> d, m){
		switch(day_of_week(2004,m,d)){
			case 0:
			cout << "Sunday" << endl;
			break;
			case 1:
			cout << "Monday" << endl;
			break;
			case 2:
			cout << "Tuesday" << endl;
			break;
			case 3:
			cout << "Wednesday" << endl;
			break;
			case 4:
			cout << "Thursday" << endl;
			break;
			case 5:
			cout << "Friday" << endl;
			break;
			case 6:
			cout << "Saturday" << endl;
		}
	}
	return 0;
}

#include <stdio.h>

int main(void)
{
	int day[13][32];
	int i, j;
	int n = 4;
	int month;
	int da;

	for (i = 1; i <= 12; i++){
		 if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12){
			for (j = 1; j <= 31; j++){
				day[i][j] = n;
				n++;
				if (n == 8){
					n = 1;
				}
			}
		}
		else if (i == 4 || i == 6 || i == 9 || i == 11){
			for (j = 1; j <= 30; j++){
				day[i][j] = n;
				n++;
				if (n == 8){
					n = 1;
				}
			}
		}
		else if (i == 2){
			for (j = 1; j <= 29; j++){
				day[i][j] = n;
				n++;
				if (n == 8){
					n = 1;
				}
			}
		}
	}

	while (1){

		scanf("%d %d", &month, &da);
		if (month == 0){
			return (0);
		}

		//printf("%d\n", day[month][da]);

		switch (day[month][da]){
			case 1:
			printf("Monday\n");
			break;

			case 2:
			printf("Tuesday\n");
			break;

			case 3:
			printf("Wednesday\n");
			break;

			case 4:
			printf("Thursday\n");
			break;

			case 5:
			printf("Friday\n");
			break;

			case 6:
			printf("Saturday\n");
			break;

			case 7:
			printf("Sunday\n");
			break;
		}
	}
}


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

int main() {
  ll N, Y ; cin >> N >> Y;
  rep(i,N+1){
    rep(j,(N-i+1)){
      if( Y == i*10000 + j*5000 + (N-i-j)*1000 ){
        cout << i << " " << j << " " << N-i-j << endl;
        return 0;
      }
    }
  }
  cout << "-1 -1 -1" << endl;
}

#include<cstdio>
#include<algorithm>
using namespace std;
int main(void)
{
	int n,y;
	int i,j,flg=0;
	scanf("%d %d",&n,&y);
	for(i=0;i<=n;i++){
		for(j=0;j<=n;j++){
			if(n-(i+j)<0) {
				break;
			}
			if(i+j+n-(i+j)==n&&y==i*10000+j*5000+(n-(i+j))*1000){
				flg=1;
				break;
			}
		}
		if(flg==1) break;
	}
	if(flg==1){
		printf("%d %d %d\n",i,j,n-(i+j));
	}
	else if(flg==0||flg==2){
		printf("-1 -1 -1\n");
	}
	return 0;
}


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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const llint INF = 1<<21;
// static const llint MOD = 1e9 + 7;

bool compPair(const pint& arg1, const pint& arg2) { return arg1.first > arg2.first; }
template<class T> void chmax(T& a, T b) { if (a < b) { a = b; } }
template<class T> void chmin(T& a, T b) { if (a > b) { a = b; } }

int main(void) {
    int n, t;
    cin >> n >> t;

    int ans = 0, tnow, tpush, tbfr;
    cin >> tbfr;
    for(int in=1;in<n;in++) {
        cin >> tpush;
        if(tpush-tbfr<=t) ans += tpush-tbfr;
        else ans += t;
        tbfr = tpush;
    }
    ans += t;
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>

#define ln "\n"
#define SORT(X) sort(X.begin(), X.end())
#define SORTi(X) sort(X.begin(), X.end(), greater<ll>())
#define REP(x, a, b) for (auto x = a; x <= b; x++)
#define REPi(x, b, a) for (auto x = b; x >= a; x--)
#define REPit(x, A) for (auto x = A.begin(); x != A.end(); x++)
#define REPr(x, A) for (auto x : A)
#define ll long long
#define ull unsigned long long
#define PB push_back
#define EB emplace_back
#define UMP unordered_map
#define UST unordered_set

using namespace std;

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

    int N , T, sum = 0;
    cin >> N >> T;
    vector <int> A(N);
    REP(i, 0, N-1)
    {
        cin>>A[i];
    }
    int tmp = 0;
    REP(i, 0, N-2)
    {
        tmp = (A[i+1] - A[i]);
        if(tmp < T)
        {
            sum = sum + tmp;
        }
        else if (tmp >= T)
        {
            sum = sum + T;
        }
    }
    sum = sum + T;
    cout<<sum<<ln;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = 1; i < (int)(n+1); i++)
const long long INF = 1LL << 60;
int main(){
  int A,B,C;
  cin>>A>>B>>C;
  if(A<=C&&C<=B){
      cout<<"Yes"<<endl;
  }
  else if(B<=C&&C<=A){
      cout<<"Yes"<<endl;
  }
  else{
      cout<<"No"<<endl;
  }
}

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a, b;
    scanf("%d%d", &a, &b);
    if(a <= 8 && b <= 8) printf("Yay!\n");
    else printf(":(\n");
    return 0;
}


/*input
3
2
1 2
10
2
1 1
10
6
2 3 4 5 6 7
111000
*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#define ALL(a) a.begin(),a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i,n) for(int i=0;i<((int)n);i++)
#define eb emplace_back
#define pb push_back
#define MP(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (int)(lower_bound(c.begin(),c.end(),x)-c.begin())
#define EL cout<<'\n'
#define BS(a,x) binary_search(ALL(a),x)
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
	out<<'('<<P.F<<','<<P.S<<')';
	return out;
}
template<typename T>
ostream& operator<<(ostream& out,vector<T> V){
	REP(i,SZ(V)) out<<V[i]<<((i!=SZ(V)-1)?" ":"");
	return out;
}
//}}}
const ll maxn=300005;
const ll maxlg=20;
const ll INF64=1e18;
const int INF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;
//const ll p=880301;
//const ll P=31;

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

int n;
string s;
ll a[maxn];

ll v[60];
bitset<60> vis;

void solve(){
	vis.reset();

	cin>>n;
	REP(i,n) cin>>a[i];
	cin>>s;

	bool flag = 0;
	for(int i=n-1;i>=0;i--){
		if(s[i]=='1'){
			for(int j=60;j>=0;j--) if(a[i]&(1LL<<j)){
				if(!vis[j]) flag = 1;
				else{
					a[i]^=v[j];
				}
			}
			if(flag) break;
		}
		else{
			for(int j=60;j>=0;j--) if(a[i]&(1LL<<j)){
				if(!vis[j]){
					vis[j] = 1;
					v[j]=a[i];
					break;
				}
				else{
					a[i]^=v[j];
				}
			}
		}
	}
	cout<<flag<<'\n';
}

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

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define endl '\n'
const int N = 1e6 + 5;
ll a[N];
int main(){
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        if (fopen("BONUS.INP", "r")){
                freopen("BONUS.INP", "r", stdin);
                freopen("BONUS.OUT", "w", stdout);
        }
        ll t;
        cin >> t;
        while (t--){
                ll n;
                cin >> n;
                for (int i = 0; i < n; i++){
                        cin >> a[i];
                }
                string s;
                cin >> s;
                vector<ll> now;
                bool flag = true;
                for (int i = n - 1; i >= 0; i--){
                        for (auto j : now) a[i] = min(a[i], a[i] ^ j);
                        now.push_back(a[i]);
                        if (s[i] == '1' && a[i]){
                                flag = false;
                                break;
                        }
                }
                if (flag) cout << 0 << endl;
                else cout << 1 << endl;
        }
}


#define NguyenDangQuan the_author

#include <bits/stdc++.h>
#define all(x) x.begin(),x.end()
#define mset(x, i) memset(x,i,sizeof(x))
#define elif else if
#define heap priority_queue
#define fi first
#define se second
#define pb push_back
#define ld long double
#define ll long long
#define ull unsigned long long
#define task "codejam"
using namespace std;

int typetest;
template<class T>
void read(T &x){
    register int c;
    T neg = 1;
    x = (T)0;
    while ((c = getchar()) <= 47 || c >= 58)
		if(c == '-') neg = -1;
    for (; (c > 47 && c < 58); c = getchar()){
        x = (x << 3) + (x << 1) + (T)(c - 48);
    }
	x *= neg;
}
inline void fastIOfileinput(){
	ios:: sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
//	freopen(task".INP", "r", stdin);
//	freopen(task".OUT", "w", stdout);
//	freopen(task".in", "r", stdin);
//	freopen(task".out", "w", stdout);
	typetest = 0;
}

const int N = 2e5 + 2;
int m, n, k;
vector<int> ker[N], kel[N];
int ltr[N], ltl[N];

void Enter(){
	read(n); read(m); read(k);
	while(m--){
		int a, b;
		read(a); read(b);
		ker[a].pb(b);
		ker[b].pb(a);
	}
	while(k--){
		int a, b;
		read(a); read(b);
		kel[a].pb(b);
		kel[b].pb(a);
	}
}

void BFS(int i, int lt[], vector<int> ke[]){
	++m;
	lt[i] = m;
	queue<int> open;
	open.push(i);
	while(open.size()){
		int c = open.front();
		open.pop();
		for(auto i : ke[c]){
			if((!lt[i]))
				open.push(i), lt[i] = m;
		}
	}
}

void solve(){
	m = 0;
	for(int i = 1; i <= n; ++i)
		if(ltr[i] == 0)
			BFS(i, ltr, ker);
	m = 0;
	for(int i = 1; i <= n; ++i)
		if(ltl[i] == 0)
			BFS(i, ltl, kel);
	map<int, map<int, int> > s;
	for(int i = 1; i <= n; ++i){
		++s[ltr[i]][ltl[i]];
//		cerr << ltr[i] << " " << ltl[i] << "\n";
	}
	for(int i = 1; i <= n; ++i){
		int& v = s[ltr[i]][ltl[i]];
		cout << v << " ";
	}
}

signed main(){
	fastIOfileinput();
	if(typetest){
		int t;
		cin >> t;
		int v = t;
		while(t--){
			Enter();
			cout << "Case #" << v - t << ": ";
			solve();
		}
	}
	else{
		Enter();
		solve();
	}
}

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

const int MOD = 1e9 + 7;
const int iINF = 1000000000;
const long long int llINF = 1000000000000000000;

using namespace std;
using ll = long long int;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;

typedef pair<ll, ll> pll;
bool paircomp(const pll &a, const pll &b) {
  if (a.first == b.first)
    return a.second < b.second;
  return a.first < b.first;
}

#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define AUTO(i, m) for (auto &i : m)
#define ALL(a) (a).begin(), (a).end()
#define MAX(vec) *std::max_element(vec.begin(), vec.end())
#define MIN(vec) *std::min_element(vec.begin(), vec.end())
#define ARGMAX(vec)                                                            \
  std::distance(vec.begin(), std::max_element(vec.begin(), vec.end()))
#define ARGMIN(vec)                                                            \
  std::distance(vec.begin(), std::min_element(vec.begin(), vec.end()))
#define REV(T) greater<T>()
#define PQ(T) priority_queue<T, vector<T>, greater<T>>
#define VVL(a, b, c) vector<vector<ll>>(a, vector<ll>(b, c))
#define VVVL(a, b, c, d)                                                       \
  vector<vector<vector<ll>>>(a, vector<vector<ll>>(b, vector<ll>(c, d)))
#define SP(a) setprecision(a)
#define SQRT(a) sqrt((long double)(a))
#define DPOW(a, b) pow((long double)(a), (long double)(b))

ll POW(ll n, ll m) {
  if (m == 0) {
    return 1;
  } else if (m % 2 == 0) {
    ll tmp = POW(n, m / 2);
    return (tmp * tmp);
  } else {
    return (n * POW(n, m - 1));
  }
}

struct UnionFind {
  vector<ll> data;

  UnionFind(ll sz) { data.assign(sz, -1); }

  bool unite(ll x, ll y) {
    x = root(x), y = root(y);
    if (x == y)
      return (false);
    if (data[x] > data[y])
      swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  ll root(ll k) {
    if (data[k] < 0)
      return (k);
    return (data[k] = root(data[k]));
  }

  ll size(ll k) { return (-data[root(k)]); }

  ll same(ll a, ll b) { return root(a) == root(b); }
};

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

struct PO {
  ll ar;
  ll br;
  ll i;
};
bool pocomp(const PO &a, const PO &b) {
  if (a.ar == b.ar)
    return a.br < b.br;
  return a.ar < b.ar;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll N, K, L;
  cin >> N >> K >> L;
  auto uf1 = UnionFind(N);
  auto uf2 = UnionFind(N);
  vector<PO> P(N);
  REP(i, K) {
    ll p, q;
    cin >> p >> q;
    uf1.unite(p - 1, q - 1);
  }

  REP(i, L) {
    ll r, s;
    cin >> r >> s;
    uf2.unite(r - 1, s - 1);
  }

  REP(i, N) { P[i] = {uf1.root(i), uf2.root(i), i}; }
  sort(ALL(P), pocomp);
  vl ans(N);
  for (ll i = 0; i < N; i++) {
    ll j = i;
    while (j + 1 < N && P[j].ar == P[j + 1].ar && P[j].br == P[j + 1].br) {
      j++;
    }
    for (ll k = i; k <= j; k++) {
      ans[P[k].i] = j - i + 1;
    }
    i = j;
  }

  REP(i, N) { cout << ans[i] << " " << endl; }

  return 0;
}


#include<bits/stdc++.h>
//ios::sync_with_stdio(false);
//cin.tie(0);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<pii,int> ppii;
typedef pair<int,pii> pipi;
typedef pair<ll,ll> pll;
typedef pair<pll,ll> ppll;
typedef pair<ll,pll> plpl;
typedef tuple<ll,ll,ll> tl;
ll mod=1000000007;
ll mod2=998244353;
ll inf=1000000000000000000;
double pi=2*acos(0);
#define rep(i,m,n) for(ll i=m;i<n;i++)
#define rrep(i,n,m) for(ll i=n;i>=m;i--)
int dh[4]={1,-1,0,0};
int dw[4]={0,0,1,-1};
int ddh[8]={-1,-1,-1,0,0,1,1,1};
int ddw[8]={-1,0,1,-1,1,-1,0,1};
ll lmax(ll a,ll b){
    if(a<b)return b;
    else return a;
}
ll lmin(ll a,ll b){
    if(a<b)return a;
    else return b;
}
ll gcd(ll a,ll b){
    if(a<b)swap(a,b);
    if(a%b==0)return b;
    return gcd(b,a%b);
}
ll Pow(ll n,ll k){
    ll ret=1;
    ll now=n;
    while(k>0){
        if(k&1)ret*=now;
        now*=now;
        k/=2;
    }
    return ret;
}
ll gya[200010];
ll kai[200010];
ll m;
ll beki(ll n,ll k){
  ll ret=1;
  ll now=n;
  while(k>0){
    if(k%2==1){
      ret*=now;
      ret%=m;
    }
    now*=now;
    now%=m;
    k/=2;
  }
  return ret;
}
ll gyaku(ll n){
  return beki(n,mod-2);
}
void nckinit(ll n){
  kai[0]=1;
  kai[1]=1;
  for(int i=2;i<=n;i++){
    kai[i]=kai[i-1]*i;
    kai[i]%=mod;
  }
  gya[n]=gyaku(kai[n]);
  for(int i=n-1;i>=1;i--){
    gya[i]=gya[i+1]*(i+1);
    gya[i]%=mod;
  }
  gya[0]=1;
}
ll nck(ll n,ll k){
  if(k<0)return 0;
  if(k==0||n==k)return 1;
  ll ret=kai[n];
  ret*=gya[n-k];
  ret%=mod;
  ret*=gya[k];
  ret%=mod;
  return ret;
}
ll npk(ll n,ll k){
  if(k<0)return 0;
  if(k==0)return 1;
  ll ret=kai[n];
  ret*=gya[n-k];
  ret%=mod;
  return ret;
}
random_device rnd;
mt19937 mt(rnd());
uniform_real_distribution<double> ra(1,6);
int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n,m;cin>>n>>m;
  string s[n];
  rep(i,0,n)cin>>s[i];
  vector<int> v[m+1];
  rep(i,0,n-1){
    rep(j,0,m-1){
      int cnt=0;
      if(s[i][j]=='#')cnt++;
      if(s[i+1][j]=='#')cnt++;
      if(s[i][j+1]=='#')cnt++;
      if(s[i+1][j+1]=='#')cnt++;
      if(cnt%2==1)v[j+1].push_back(i+1);
    }
  }
  rep(i,0,m+1){
    v[i].push_back(n);
    sort(v[i].begin(),v[i].end());
  }
  int now[m+1];
  fill(now,now+m+1,0);
  int ans=0;
  rep(i,0,n){
    rep(j,0,m+1){
      if(v[j][now[j]]==i)now[j]++;
      //cout<<v[j][now[j]]<<" ";
    }

    deque<pii> dq;
    dq.push_back({0,0});
    rep(j,1,m+1){
      int f=v[j][now[j]];
      f-=i;
      while(dq.back().first>=f){
        int y=dq.size();
        ans=max(ans,((int)j-dq[y-2].second)*dq[y-1].first);
        dq.pop_back();
      }
      ans=max(ans,((int)j-dq.back().second)*f);
      dq.push_back({f,j});
    }
    while(dq.size()>1){
      int y=dq.size();
      ans=max(ans,(m-dq[y-2].second)*dq[y-1].first);
      dq.pop_back();
    }
  }
  cout<<ans<<endl;
}

#pragma region header
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rev(i, n) for(int i = (int)(n - 1); i >= 0; i--)
#define rev1(i, n) for(int i = (int)(n); i > 0; i--)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define resort(v) sort((v).rbegin(), (v).rend())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
using ll = long long;
using P = pair<int, int>;
/* ----------------よく使う数字や配列----------------- */
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
constexpr ll mod = 1e9+7;
constexpr ll inf = INT32_MAX/2;
constexpr ll INF = LLONG_MAX/2;
constexpr long double eps = DBL_EPSILON;
constexpr long double pi = 3.141592653589793238462643383279;
/* ----------------------end----------------------- */

/* --------------------テンプレート------------------ */
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
/* ----------------------end----------------------- */

/* --------------------ライブラリ-------------------- */
ll fact(int i) {       //階乗
    if (i == 0) return 1;
    return (fact(i - 1)) * i % mod;
}
ll gcd(ll a, ll b) {        //最大公約数
    if(b == 0) return a;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) {      //最小公倍数
    return a * b / gcd(a, b);
}
int keta(ll n) {        //桁数を求める
    if(n == 0) return 1;
    int count = 0;
    while(n != 0) {
        n /= 10;
        count++;
    }
    return count;
}
ll ketasum(ll n) {    //各桁の和
    ll sum = 0;
    while(n != 0) {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}
/* ----------------------end----------------------- */
#pragma endregion
signed main() {
    int h,w;cin >> h >> w;
    vvb bo(h,vb(w));
    vector<P> v;
    rep(i, h) {
        rep(j, w) {
            char c;cin >> c;
            if(c=='#') {
                bo[i][j]=true;
                v.pb({i,j});
            }
            else bo[i][j]=false;
        }
    }
    bool ok = true;
    rep(i, v.size()) {
        rep(j, 4) {
            int x=v[i].first+dx[j],y=v[i].second+dy[j];
            if(x<0||x>=h||y<0||y>=w||!bo[x][y]) {
                // cout << i << endl;
                if(j==3) {
                    cout << "No" << endl;
                    return 0;
                }
                continue;
            }
            break;
        }
    }
    cout << "Yes" << endl;
    return 0;
}


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

int main()
{
	vector<int> d(10),e(10);
	for(int i = 1;i<10;i++)
	{
		int now = i*2;
		int num = 2;
		int cnt = 1;
		while(now != i)
		{
			if(now>9)
			{
				now = now%10+now/10;
				cnt++;
				if(now==i)break;
			}
			now += i;
			num++;
			cnt++;
			if(now==i)break;
			if(now>9)
			{
				now = now%10+now/10;
				cnt++;

			}
		}
		//cout<<i << ' ' << num << ' ' << cnt<<endl;
		d[i]= num;
		e[i]= cnt;
		d[i]--;
	}
	d[0] = 1;
	e[0]=1;
	int n;
	cin >> n;
	ll ans = 0;
	int now = 0;
	map<ll,ll> mp;
	for(int i = 0;i<n;i++)
	{
		ll x,y;
		cin >> x >> y;
		mp[x] += y;
	}
	for(int i = 0;i<10;i++)
	{
		if(mp[i]>0)
		{
			mp[i]--;
			ans += mp[i]/d[i]*e[i];
			//cout << mp[i]<<endl;
			//cout<< "ans" << ans << endl;
			//cout << d[i] <<';'<< e[i]<<endl;;
			mp[i]%=d[i];
			mp[i]++;
			for(int j = 0;j<mp[i];j++)
			{
				now += i;
				ans++;
				if(now>9)
				{
					now = now/10+now%10;
					ans++;
				}
			}
		}
	}
	cout << ans -1<< endl;

}


#include<bits/stdc++.h>
using namespace std;
#define int  long long int
#define mod 1000000007
#define b1(n) __builtin_popcount(n)
vector<int>sie;
 inline int gEd(int a, int b, int *ax, int *ay)
{
	int x,y;
	if (a==0)
	{   *ax=0;
		*ay = 1;
		return b;
	}
    int g = gEd(b%a,a,&x,&y);
    *ax=y-(b/a)*x;
	*ay=x;
	return g;
}
inline int mli(int a,int m) //mli(a,m)
{
	int ax,ay;
	     gEd(a,m,&ax,&ay);
	    int val1=(ax%m+m)%m;
	    return val1;
}
inline int powerm(int a,int n)   //powerm(a,n)
{
	int ans=1;
	while(n>0)
	{
		int last_bit=(n&1);
		if(last_bit)
		ans=((ans%mod)*(a%mod))%mod;
		a=((a%mod)*(a%mod))%mod;
		n=n>>1;
	}
	return ans;
}
inline int power(int a,int n)   //power(a,n)
{
	int ans=1;
	while(n>0)
	{
		int last_bit=(n&1);
		if(last_bit)
		ans=ans*a;
		a=a*a;
		n=n>>1;
	}
	return ans;
}
inline int add(int a,int b)
{
	int s=((a%mod)+(b%mod))%mod;
	return s;
}
inline int sub(int a,int b)
{
	int s=((a%mod)-(b%mod))%mod;
	return s;
}
inline int mul(int a,int b)
{
	int s=((a%mod)*(b%mod))%mod;
	return s;
}
inline int ncr(int n, int k)  //ncr(a,b)
{
    int res = 1;

    if (k > n - k)
        k = n - k;

    for (int i = 0; i < k; ++i) {
        res *= (n - i);
        res /= (i + 1);
    }

    return res;
}
inline void sieve()   //sieve vector->sie
{
	const int n=10000000;
	bitset<10000000>b;
	b.set();
	b[0]=b[1]=0;
	for(int i =2;i<=n;i++)
	{
		if(b[i])
		{
			sie.push_back(i);
			for(int j=i*i;j<=n;j=j+i)
			b[j]=0;
		}
	}
}

int32_t main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
     string s;
     cin>>s;
   if(s=="SUN"){
	   cout<<"7"<<endl;
	   }
   if(s=="MON"){
	   cout<<"6"<<endl;
	   }
   if(s=="TUE"){cout<<"5"<<endl;}
   if(s=="WED"){cout<<"4"<<endl;}
   if(s=="THU"){cout<<"3"<<endl;}
   if(s=="FRI"){cout<<"2"<<endl;}
   if(s=="SAT"){cout<<"1"<<endl;}

}

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

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

int n;
string s;
map<P,ll> a,b;

int main(){
  cin >> n >> s;
  rep(U,0,1<<n){
    string t,w;
    rep(i,0,n){
      if(U>>i & 1) t.push_back(s[i]);
      else w.push_back(s[i]);
    }
    a[P(t,w)]++;
  }
  rep(U,0,1<<n){
    string t,w;
    rep(i,0,n){
      if(U>>i & 1) t.push_back(s[n*2-i-1]);
      else w.push_back(s[n*2-i-1]);
    }
    b[P(t,w)]++;
  }
  ll ans = 0;
  for(auto i : a){
    ans += i.second * b[i.first];
  }
  cout << ans << endl;
  return 0;
}


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

int	main()
{
  int		nb_min, nb_max, nb;
  int		count = 0;
  string	str, rev;

  cin >> nb_min >> nb_max;
  nb = nb_min;
  while (nb <= nb_max)
  {
    str = to_string(nb);
    rev = str;
    reverse(rev.begin(), rev.end());
    if (str == rev)
      count++;
    nb++;
  }
  cout << count << endl;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

template <typename T> void cmin(T &x, const T &y)
{
	if(y < x) x = y;
}

template <typename T> void cmax(T &x, const T &y)
{
	if(y > x) x = y;
}

template <typename T> void read(T &x)
{
	x = 0; char c = getchar(); bool f = 0;
	while(!isdigit(c) && c!='-') c = getchar();
	if(c == '-') f = 1, c = getchar();
	while(isdigit(c)) x = x*10+c-'0', c = getchar();
	if(f) x = -x;
}

ll pw10[19], D, ans;
int bit[19];

void dfs(int l, int r, ll d, ll way)
{
	if(l > r)
	{
		if(d == -D) ans += way;
	}
	else
	{
		if(l == r) dfs(l+1, r-1, d, way*10);
		else
		{
			for(int i=-9; i<=9; i++)
			{
				ll base = d+i*pw10[r]-i*pw10[l];
				ll upb = base + pw10[r];
				ll lob = base - pw10[r];
				if(lob<=-D && -D<=upb) bit[l] = i, dfs(l+1, r-1, base, way*(10-abs(i)-(i<=0&&l==0)));
			}
		}
	}
}

int main()
{
	read(D);
	pw10[0] = 1;
	for(int i=1; i<=18; i++) pw10[i] = pw10[i-1] * 10;
	for(int i=1; i<=18; i++) dfs(0, i, 0, 1);
	printf("%lld\n", ans);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string a;
    getline(cin,a);
    if(a.size()==2 && a[0]==a[1]){
        cout<<"1 2"<<endl;
        return 0;
    }
    long long i,flag=0;
    for(i=0;i<a.size()-2;i++)
    {
        if(a[i]==a[i+1] || a[i]==a[i+2] || a[i+1]==a[i+2]){
            cout<<i+1<<" "<<i+3<<endl;
            flag=1;
            break;
        }

    }
    if(!flag)
        cout<<"-1 -1"<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
  int a,b;cin>>a>>b;
  int ans=0;
  if(a>=b){
    ans+=a;
    a-=1;
  }
  else{
    ans+=b;
    b-=1;
  }
  if(a>=b){
    ans+=a;
    a-=1;
  }
  else{
    ans+=b;
    b-=1;
  }
  cout<<ans<<endl;
}

//This Code was made by Chinese_zjc_.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <set>
#include <ctime>
// #include<windows.h>
#define int long long
#define double long double
using namespace std;
const double PI = acos(-1);
const double eps = 0.0000000001;
const int INF = 0x3fffffffffffffff;
int n, k, dp[55][55];
signed main()
{
    ios::sync_with_stdio(false);
    cin >> n >> k;
    cout << n - k + 1 << endl;
    return 0;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <iomanip>
#include <utility>
#include <cstdlib>
#include <sstream>
#include <bitset>
#include <vector>
#include <cstdio>
#include <ctime>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

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

#define dump(x)  cerr << #x << " = " << (x) << endl
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define sz size()

const double EPS = 1e-10;
const int INF = 100000000;

int main(){
    int n,q;
    while(cin>>n>>q){
        if(n==0 && q==0)break;
        int date[200];
        rep(i,200)date[i]=0;

        rep(i,n){
            int m;
            cin>>m;
            rep(j,m){
                int a;
                cin>>a;
                date[a]++;
            }
        }
        int maxi=-1;
        int day=-1;
        for(int i=1;i<100;i++){
            if(maxi<date[i]){
                day=i;
                maxi=date[i];
            }
        }
        if(maxi>=q)cout<<day<<endl;
        else cout<<"0"<<endl;
    }

    return 0;
}

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

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

        if (n==0 && m==0) {
            break;
        }

        int p[n];
        int s = 0;

        for (int i=0; i<n; i++) {
            cin >> p[i];
            s += p[i];
        }

        sort(p, p+n, greater<int>());

        for (int i=1; i*m-1<n; i++) {
            s -= p[i*m-1];
        }

        cout << s << endl;
    }
}


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

typedef long long ll;

const ll inf = 10000000;

int main() {
    string s;
    cin >> s;
    for (int i = 1; i < s.size(); i++) {
        if (s[i-1] == s[i])  {
            cout << i << " " << i+1 << endl;
            return 0;
        }
    }
    for (int i = 0; i+2 < s.size(); i++) {
        if (s[i] == s[i+2]) {
            cout << i+1 << " " << i+3 << endl;
            return 0;
        }
    }
    cout << -1 << " " << -1 << endl;
}

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

string s;

int main () {
	cin >> s;
	for (char c = 'a'; c <= 'z'; c++){
		if (s.find(c) == -1){
			cout << c;
			return 0;
		}
	}
	cout << "None";
}


#include<iostream>
#include<cstdio>
using namespace std;
int main(){
  int n,ground,ou,point;
  string s;

  scanf("%d",&n);

  for(int cas=0;cas<n;cas++){
    ground = ou = point = 0;
    while(1){
      cin >> s;
      if(s == "OUT"){
	ou++;
	if(ou == 3) break;
      } else if(s == "HIT"){
	ground<<=1;
	ground|=1;
	if(ground >= (1<<3)){
	  ground&=(~(1<<3));
	  point++;
	}
      } else{
	for(int i=0;i<3;i++){
	  if(ground&(1<<i)) point++;
	}
	ground=0;
	point++;
      }
    }
    cout << point << endl;
  }

  return 0;
}

#include<iostream>
#include<string>

using namespace std;

int main(){
	int n=0,ct=0, score=0, out=0,run=0;
	string x;
	cin >> n;
	while (1){
		int  score = 0, out = 0, run = 0;
		while (1){
			cin >> x;
			if (x == "OUT"){
				out++;
			}
			if (x == "HOMERUN"){
				score+=run;
				score++;
				run = 0;
			}
			if (x == "HIT"){
				run++;
			}
			if (run > 3){
				run--;
				score++;
			}
			if (out == 3){
				cout << score << endl;
				ct++;
				break;
			}
		}
		if (ct == n)break;
	}
	return 0;

}


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

#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (ll i = m; i < (n); i++)

using ll = long long;
using pii = pair<int, int>;
using Vi = vector<int>;

int main() {
  int N,y;
  cin >> N >> y;
  for(int i = 0; i<= N; i++){
    for(int j = 0; i+j<= N; j++){
      if(10000*i+5000*j+(N-i-j)*1000==y){
        cout << i << " " << j << " " << N-i-j << endl;
        return 0;
      }
    }
  }

  cout << "-1 -1 -1" << endl;
}

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

using namespace std;

typedef long long ll;
#define FOR(i,a,b)   for(ll i = (a); i < (b); i++ )
#define REP(i, n)    FOR(i,0,n)
typedef pair< ll, ll >   cp2;
typedef pair< string, cp2 >   cp3;
#define fi first
#define se second
#define sec  se.fi
#define thr  se.se
const ll mod = 1000000007;
//              123456789

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

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

int N;
ll Y;
bool ans = false;
ll A, B, C;

int main(){

    cin>>N>>Y;

    REP( i, N+1 ){
        REP( j, N+1-i ){
            A = i;
            B = j;
            C = N-i-j;
            if( A*1000+B*5000+C*10000 == Y ){
                ans = true;
                i = N;
                break;
            }
        }
    }

    if( ans ){
        printf("%lld %lld %lld\n", C, B, A);
    }
    else{
        puts("-1 -1 -1");
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a) { std::cout << #a << " = " << a << "\n"; }
template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }
template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }
template<class T, class V> istream& operator>> (istream& ist, pair<T, V>& p) { return ist >> p.first >> p.second; }
template<class T> ostream& operator<< (ostream& ost, pair<T, T>& p) { return ost << p.first << ", " << p.second; }
template<class T> istream& operator>> (istream& ist, vector<T>& vs) { for(auto& e: vs) ist >> e; return ist; }

typedef long long ll;
int const inf = INT_MAX / 2;

int main() {
  int N, K; cin >> N >> K;
  vector<int> A(N); cin >> A;
  rep(_, min(K, 50)) {
    vector<int> acc(N + 1);
    rep(i, N) {
      acc[max(0, i - A[i])]++;
      acc[min(N, i + A[i] + 1)]--;
    }
    REP(i, 1, N + 1) {
      acc[i] += acc[i - 1];
    }
    rep(i, N) {
      A[i] = acc[i];
    }
  }
  rep(i, N) {
    if (i) cout << " ";
    cout << A[i];
  }
  cout << "\n";
}

#include<iostream>
using namespace std;

int main(){
	int n;
	cin >> n;
	int a[n];
	int min, max;
	long sum;
	cin >> a[0];
	min = a[0];
	max = a[0];
	sum += a[0];
	for(int i = 1; i < n; i++){
		cin >> a[i];
		if(min > a[i]){
			min = a[i];
		}else if(max < a[i]){
			max = a[i];
		}
		sum += a[i];
	}
	cout << min << " " << max << " " << sum << endl;
}


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

#define PB push_back
#define PF push_front
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vpi vector<pii>
#define vll vector<ll>
#define vpl vector<pll>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define MX(x) *max_element(all(x))
#define MN(x) *min_element(all(x))
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pr_d(x) cout << fixed << setprecision(15) << x << endl
#define ud(c, x) distance(c.begin(), upper_bound(all(c), x))
#define ld(c, x) distance(c.begin(), lower_bound(all(c), x))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define rep3(i, n) for (int i = (n - 1); i >= 0; --i)
#define rep4(i, a, b) for (int i = (a); i > (b); --i)
#define pb push_back
#define out(x) cout << x << "\n"
bool odd(int i) { return i % 2; }

#define all(v) v.begin(), v.end()
#define size(x) int(x.size())

int gcd(int a, int b) { return __gcd(a, b); }
int lcm(int a, int b) { return a * (b / gcd(a, b)); }
void Yes_No(bool f) {
  if (f)
    printf("Yes\n");
  else
    printf("No\n");
}
void YES_NO(bool f) {
  if (f)
    printf("YES\n");
  else
    printf("NO\n");
}

template <typename T>
void deb1(T x) {
  cout << x << "\n";
}

template <typename T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T& a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
//-------------------ここから回答する-----------------------

void solve(void) {
  int n, k;
  cin >> n >> k;
  vi l(n);
  rep(i, n) cin >> l[i];
  sort(all(l), greater<int>());
  int ans = 0;
  rep(i, k) ans += l[i];
  out(ans);
}
int main(void) { solve(); }

/*
 * a.cc:
 */

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

using namespace std;

/* constant */

const int MAX_N = 100;

/* typedef */

struct Edge {
  int i, j, c;
  Edge() {}
  Edge(int _i, int _j, int _c): i(_i), j(_j), c(_c) {}
  bool operator<(const Edge &e) const { return c < e.c; }
};

typedef vector<Edge> ve;

struct UFT {
  int links[MAX_N], ranks[MAX_N], sizes[MAX_N];

  void clear(int n) {
    for (int i = 0; i < n; i++) links[i] = i, ranks[i] = sizes[i] = 1;
  }
  UFT() {}
  UFT(int n) { clear(n); }

  int root(int i) {
    int i0 = i;
    while (links[i0] != i0) i0 = links[i0];
    return (links[i] = i0);
  }

  int rank(int i) { return ranks[root(i)]; }
  int size(int i) { return sizes[root(i)]; }
  bool same(int i, int j) { return root(i) == root(j); }

  int merge(int i0, int i1) {
    int r0 = root(i0), r1 = root(i1), mr;
    if (r0 == r1) return r0;
    if (ranks[r0] == ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      ranks[r0]++;
      mr = r0;
    }
    else if (ranks[r0] > ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      mr = r0;
    }
    else {
      links[r0] = r1;
      sizes[r1] += sizes[r0];
      mr = r1;
    }
    return mr;
  }
};

/* global variables */

/* subroutines */

/* main */

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

  ve es;

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      int ai;
      cin >> ai;
      if (ai >= 0) es.push_back(Edge(i, j, ai));
    }
  sort(es.begin(), es.end());

  UFT uft(n);
  int en = es.size(), sum = 0;

  for (int i = 0; i < en; i++) {
    Edge &ei = es[i];
    if (! uft.same(ei.i, ei.j)) {
      uft.merge(ei.i, ei.j);
      sum += ei.c;
    }
  }

  printf("%d\n", sum);
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef long long ll;
struct edge { ll s, t, x; };
typedef pair<ll, ll> P;
typedef pair<double, P> event;
const ll MAX = 510000;
const ll MOD = 1000000007;
#define INF (ll)10000000000000000

int main() {
  ll N,Q;
  cin>>N>>Q;
  priority_queue<event>pq;
  set<ll>st;
  //eventはP(P(a,b),c)で書き、
  //aに時刻をもち,b=0→工事開始,1→終了,2→歩行者とする
  rep(i,N){
    double s,t;
    ll x;
    cin>>s>>t>>x;
    s=(double)x-s+0.5;
    t=(double)x-t+0.5;
    pq.push(event(s,P(0ll,x)));
    pq.push(event(t,P(1ll,x)));
  }
  rep(i,Q){
    double g;
    cin>>g;
    pq.push(event(-1*g,P(2ll,0ll)));
  }
  while(!pq.empty()){
    auto t=pq.top();
    pq.pop();
    if(t.second.first==0){
      st.insert(t.second.second);
    }
    if(t.second.first==1){
      st.erase(t.second.second);
    }
    if(t.second.first==2){
      if(st.empty()){cout<<-1<<endl;}
      else{
        cout<<*begin(st)<<endl;
      }
    }
  }
}

#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll longinf=1LL<<60;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define F first
#define S second

#define bit(n,k) ((n>>k)&1) //*n no k bit me 1 or 0*/

const int mx=100010;
const ll mod=1e9+7;

int main(){
  int n,q;
  cin >> n >> q;
  vector<pair<ll,pair<int,ll>>> a;
  rep(i,n){
    int s,t,x; cin >> s >> t >> x;
    a.push_back({s-x,{-1,x}});
    a.push_back({t-x,{0,x}});
  }
  rep(i,q){
    int d; cin >> d;
    a.push_back({d,{1,i}});
  }
  sort(a.begin(),a.end());

  vector<ll> res(q,-1);
  multiset<int> st;
  for(auto it:a){
    if(it.S.F == -1){
      st.insert(it.S.S);
    } else if(it.S.F == 0){
//      st.erase(it.S.S);   //同じ数字を全部消してしまう
      st.erase(st.lower_bound(it.S.S)); //1つだけ消したい
    } else {
      if(!st.empty()) res[it.S.S] = *st.begin();
    }
  }

  for(auto ans:res){
    cout << ans << endl;
  }
  return 0;
}


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

#define fs first
#define sc second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define pb emplace_back

using pii = pair<int, int>;
using vi = vector<int>;
using lint = long long;

const int inf = 1001001001;
const lint linf = 1001001001001001001ll;
const int mod = 1e9 + 7;
const int dx[]{0, 1, 0, -1, -1, -1, 1, 1}, dy[]{1, 0, -1, 0, -1, 1, -1, 1};

template<typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; } return a > b; }
template<typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; } return a < b; }
template<typename T> inline void print(const T &x, string s = "\n") { cout << x << s; }
template<typename T> inline void print(const vector<T> &v, string s = " ")
{ rep(i, v.size()) cout << v[i] << (i + 1 == v.size() ? "\n" : s); }
inline bool inside(int y, int x, int H, int W) { return 0 <= y && y < H && 0 <= x && x < W; }
inline lint in() { lint x; std::cin>>x; return x; }

int main() {
	int l[10], u, v;
	char c;

	while (cin >> l[0] >> c) {
		rep(i, 9) cin >> l[i + 1] >> c;
		cin >> u >> c >> v;

		for (int i = 1; i < 10; ++i) {
			l[i] += l[i - 1];
		}

		double d = (double)l[9] / (u + v) * u;
		for (int i = 0; i < 10; ++i) {
			if (l[i] >= d) {
				cout << i + 1 << endl;
				break;
			}
		}
		//rep(i, 10) cout << l[i] << endl;
		//cout << u << endl;
	 	//cout << v << endl;
	}
}

#include <iostream>
using namespace std;

using ll = long long;
const ll MOD = 1000000007;
const int MAXXYZ = 17;

int n,x,y,z;

inline bool hasXYZ(int a){
	return (a >> (z - 1)) & (a >> (y + z - 1)) & (a >> (x + y + z - 1)) & 1;
}

signed main(){
	ll dp[50][1 << MAXXYZ] = {};
	cin >> n >> x >> y >> z;
	dp[0][0] = 1;
	for(int i = 0;i < n;i++){
		for(int j = 0;j < (1 << MAXXYZ);j++){
			for(int k = 1;k <= 10;k++){
				int nxt = ((j << k) + (1 << (k - 1))) % (1 << MAXXYZ);
				if(!hasXYZ(nxt)) (dp[i + 1][nxt] += dp[i][j]) %= MOD;
			}
		}
	}
	ll ans = 1;
	for(int i = 0;i < n;i++) ans = ans * 10 % MOD;
	for(int i = 0;i < (1 << MAXXYZ);i++) ans = (ans - dp[n][i] + MOD) % MOD;
	cout << ans << endl;
}


#include<iostream>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
int n,s;
struct que
{
	int x;
	long long p;
}a[100005];
long long calc(int l,int r,int last)
{
	if(a[l].x>=s) return a[r].x-s;
	if(a[r].x<=s) return s-a[l].x;
	if(a[l].p>=a[r].p){a[l].p+=a[r].p;return calc(l,r-1,l)+((last==r)?a[r].x-a[l].x:0);}
	else {a[r].p+=a[l].p;return calc(l+1,r,r)+((last==l)?a[r].x-a[l].x:0);}
}
int main()
{
	scanf("%d%d",&n,&s);
	for(int i=1;i<=n;i++) scanf("%d%lld",&a[i].x,&a[i].p);
	printf("%lld\n",calc(1,n,a[1].p<a[n].p?1:n));
}

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

struct UnionFind {
	vector<int> par;
	UnionFind(int n) :par(n) {
		for (int i = 0; i < n; i++) {
			par[i] = i;
		}
	}
	int root(int x) {
		return par[x] == x ? x : par[x] = root(par[x]);
	}
	bool same(int x, int y) {
		return root(x) == root(y);
	}
	void merge(int x, int y) {
		int rx = root(x), ry = root(y);
		if (rx == ry)return;
		par[rx] = ry;
	}
};

int main() {
	int n; cin >> n;
	vector<long long>x(n), y(n);
	for (int i = 0; i < n; i++)cin >> x[i] >> y[i];
	using P = pair<int, int>; using edge = pair<long long, P>;
	vector<edge>edges;
	vector<int>ids(n);
	for (int i = 0; i < n; i++)ids[i] = i;
	sort(ids.begin(), ids.end(), [&](int i, int j) {return x[i] < x[j]; });
	for (int i = 0; i < n - 1; i++) {
		int u = ids[i], v = ids[i + 1];
		edges.push_back(edge(x[v] - x[u], P(u, v)));
	}
	sort(ids.begin(), ids.end(), [&](int i, int j) {return y[i] < y[j]; });
	for (int i = 0; i < n - 1; i++) {
		int u = ids[i], v = ids[i + 1];
		edges.push_back(edge(y[v] - y[u], P(u, v)));
	}
	sort(edges.begin(), edges.end());
	UnionFind uf(n);
	long long ans = 0;
	for (auto e : edges) {
		int u = e.second.first, v = e.second.second;
		long long cost = e.first;
		if (uf.same(u, v))continue;
		uf.merge(u, v);
		ans += cost;
	}
	cout << ans << endl;
	return 0;
}


#include <iostream>

using i64 = long long;

i64 abs(i64 x) {
	return x < 0 ? -x : x;
}

const int N = 100000 + 7;

int n, s;
int x[N], p[N];

i64 dfs(int l, int r, i64 u, i64 v) {
	if (s < x[l]) return x[r] - s;
	if (s > x[r]) return x[l] - s;
	if (u + p[l] >= v + p[r]) {
		i64 t = dfs(l, r - 1, u + v + p[r], 0);
		return x[r] - (t > 0 ? x[r - 1] : x[l]) + abs(t);
	}
	if (u + p[l] < v + p[r]) {
		i64 t = dfs(l + 1, r, 0, u + v + p[l]);
		return x[l] - (t > 0 ? x[r] : x[l + 1]) - abs(t);
	}
}

int main() {
//	freopen("code.in", "r", stdin);
//	freopen("code.out", "w", stdout);

	scanf("%d%d", &n, &s);
	for (int i = 1; i <= n; ++i)
		scanf("%d%d", x + i, p + i);
	printf("%lld\n", abs(dfs(1, n, 0, 0)));
	return 0;
}

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

vector<bool> visited, exhausted;
stack<int> cycle;
int closing_point;

void DFS(const vector<vector<int>> &graph, int v, int previous_v){
    visited[v] = true;
    cycle.push(v);

    for(auto next_v : graph[v]){
        if(next_v == previous_v) continue;

        if(exhausted[next_v] == false){
            if(visited[next_v] == true)  closing_point = next_v;
            if(visited[next_v] == false) DFS(graph, next_v, v);

            if(closing_point != -1) return;
        }
    }

    // if
    // 1. no next_v (dead-end) or
    // 2. all next_v lead to dead-end
    cycle.pop();
    exhausted[v] = true;
}

int main(){
    int V, E;
    cin >> V >> E;

    vector<vector<int>> graph(V);
    for(int i=0; i<E; i++){
        int s, t;
        cin >> s >> t;
        s -= 1;
        t -= 1;
        graph[s].push_back(t);
        // graph[t].push_back(s);
    }

    for(int i=0; i<V; i++){
        visited.assign(V, false);
        exhausted.assign(V, false);
        while(cycle.empty() == false){
            cycle.pop();
        }
        closing_point = -1;
        DFS(graph, i, -1);

        vector<int> prov_ans;
        while(cycle.empty() == false){
            int t = cycle.top(); cycle.pop();
            prov_ans.push_back(t);
            if(t == closing_point) break;
        }

        int nvc = prov_ans.size();// number of vertices in the cycle
        if(nvc != 0){
            reverse(prov_ans.begin(),prov_ans.end());
            map<int, int> vc;
            for(int j=0; j<nvc; j++){
                vc[prov_ans[j]] = j;
            }

            bool flag = false;
            int start = 0, end = nvc-1;
            for(int j=1; j<nvc; j++){
                for(auto adjacent_v : graph[prov_ans[j]]){
                    // since directed
                    if(j <  nvc - 1 && adjacent_v == prov_ans[j+1]) continue;
                    if(j == nvc - 1 && adjacent_v == prov_ans[0])   continue;

                    if(vc.count(adjacent_v)){
                        //cout << prov_ans[j] << " -- " << adjacent_v << endl;
                        flag = true;
                        end = j;
                        start = max(start, vc[adjacent_v]);
                    }
                }
                if(flag == true){
                    cout << end - start + 1 << endl;
                    for(int k=start; k<=end; k++){
                        cout << prov_ans[k] + 1 << endl;
                    }
                    return 0;
                }
            }
            cout << end - start + 1 << endl;
            for(int k=start; k<=end; k++){
                cout << prov_ans[k] + 1 << endl;
            }
            return 0;
        }
    }
    cout << -1 << endl;
    return 0;
}


#include<bits/stdc++.h>
#include<ctype.h>
# define pb push_back
#define fst first
#define sec second
#define For(i,a,b) for(int i=a;i<b;i++)
#define ll long long int
#define ull unsigned long long int
#define mod 1000000007
#define fo(i,n) for(ll i=0;i<n;i++)
#define endl "\n"
#define rev(i,n) for(ll i=n-1;i>=0;i--)
#define fo1(i,n) for(ll i=1;i<=n;i++)
#define boolsize 1000001
#define pi pair<ll,ll>
#define vi vector<ll>
#define vii vector<pi>
using namespace std;
template<typename T>
void showvector(vector <T> v)
{
    for(T x:v)
        cout<<x<<" ";
    cout<<endl;

}
template<typename T>
void showvector1(vector <T> v)
{
    ll n=v.size();
    fo1(i,n-1)
        cout<<v[i]<<endl;

}
template<typename T>
void showset(set <T> s)
{
    for(T x: s)
        cout<<x<<" ";
    cout<<endl;
}
template<class T>
void showvectorpair(vector<T> v)
{
    for(auto it=v.begin();it!=v.end();it++)
        cout<<it->first<<" "<<it->second<<endl;
    cout<<endl;
}
template<typename T,typename P>
void showmap(map <T,P> m)
{
    for(auto it=m.begin();it!=m.end();it++)
        cout<<it->first<<" "<<it->second<<endl;
    cout<<endl;
}
template<typename T>
bool comp(T a,T b)
{
    return (a>b);
}
template<class T>
bool comppair(T a,T b)
{
    if(a.first==b.first)
        return(a.second>b.second);
    return (a.first>b.first);
}
bool sameparity(ll a,ll b)
{
    return (a%2==b%2);
}
bool difparity(ll a,ll b)
{
    return !(a%2==b%2);
}
bool isprime(ll x)
{   if(x<=1)
     return false;
    for(ll i=2;i<=sqrt(x);i++)
    {
        if(x%i==0)
            return false;
    }
    return true;
}
bool iseven(ll x)
{
    return !(x%2);
}
bool isodd(ll x)
{
    return (x%2);
}
/// check for test case before submitting
void vfun()
{
    ll n,k;
    cin>>n;
    vector <ll> v(n);
    fo(i,n)
    cin>>v[i];

}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0); ///cant use scanf, printf
    cin.tie(0);cout.tie(0); /// no longer auto flush cout before each cin, remove for interactive
    //cout << fixed << setprecision(11); /// no scientific output
    ll test=1;
    //cin>>test;
    while(test--)
    {

        ll n;
        cin>>n;
        ll ans=n/11;
        ans=2*ans;
        if(n%11>=7)
            ans+=2;
        else if(n%11>0)
            ans+=1;
        cout<<ans<<endl;

//         if(vfun())
//            cout<<"YES\n";
//         else
//            cout<<"NO\n";
    }
}
///before sub
/// check for value of zero and single input in array
///loop vars,1LL in mult, equal, one, bounds, int v ll, endl, finish taking inputs
/// check whether test cases are given or not

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast ios::sync_with_stdio(0); cin.tie(0);
#define endl '\n'
int main()
{
    fast;
    int n; cin>>n;
    int ans=0;
    vector<int> v;
    for(int i=0;i<2*n;i++)
    {
        int x; cin>>x;
        v.push_back(x);
    }
    sort(v.begin(),v.end());
    for(auto i=v.begin();i!=v.end();i=i+2)
    {
       // cout<<*i<<endl;
        ans+=*i;
    }
   cout<<ans<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main(void){
  int k;string s;cin>>k>>s;
  if(s.size()>k) {for(int i=0; i<k; ++i) cout<<s[i]; cout<<"..."<<endl;}
  else cout<<s<<endl;
  return 0;
}

#include <bits/stdc++.h>
#define rep(i,m,n) for(int i=m; i<n; i++)
#define co(n) cout << n << endl
using namespace std;
int main(){
  string s;
  cin >> s;
  char a[s.size()];
  int c=0, d=0;
  rep(i,0,s.size()-8) cout << s.at(i);
  cout << endl;

  return 0;
}


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

int main() {
	int n;
	cin >> n;
	vector<int> primes;
	vector<int> is_prime(55556, -1);
	for (int i = 2; i <= 55555; i++) {
		if (is_prime[i] != -1) continue;
		primes.push_back(i);
		is_prime[i] = 1;
		for (int j = i+i; j <= 55555; j+=i) {
			is_prime[j] = 0;
		}
	}
	int k = 0;
	for (int x: primes) {
		if (k == n) break;
		if (x % 5 == 1) {
			cout << x << " ";
			k++;
		}
	}
	cout << endl;
}

#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
const int MAX = 55556;
vector<bool> ok(MAX, true);
signed main(void)
{
	int n;
	vector<int> vec;

	cin >> n;
	ok[0] = ok [1] = false;
	rep(i, sqrt(MAX))
	{
		if (ok[i])
		{
			for (int j = 0; i * (j + 2) < MAX; j++)
			{
				ok[i * (j + 2)] = false;
			}
		}
	}
	for(int i = 6; i < MAX; i += 5)
	{
		if (ok[i])
			vec.push_back(i);
	}
	sort(vec.begin(), vec.end());
	rep(i, n)
	{
		printf("%d%c", vec[i], i == n - 1?'\n':' ');
	}
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <string.h>
using namespace std; using ll = long long; using pll = pair<ll, ll>;
using vl = vector<ll>; using vll = vector<vl>; using vpll = vector<pll>;
int ctoi(char c) {
	switch (c) {
	case '0': return 0; case '1': return 1; case '2': return 2;
	case '3': return 3; case '4': return 4; case '5': return 5;
	case '6': return 6; case '7': return 7; case '8': return 8;
	case '9': return 9;  default: return 0;
	}
}
bool pairCompare(const pll firstElof, pll secondElof)
{
	return firstElof.first > secondElof.first;
}
//**Snippetリスト**//
//rep, vin, all, iteLoop//
//gcdlcm, isPrime, eratos, divisor, modinv, bitSearch, bitList, dfs, bfs//
//dijkstra, UnionFind, COM, digitDP//
ll i, j, k, l; ll N, M, K, H, W, L, X, Y, Z;
ll MOD = 1000000007; ll INF = 9999999999999; ll ans = INF;
vl flag, color, D; vll path;
//***********//
int main() {
	cin >> H >> W >> K;
	if (W == 1) cout << 1;
	else {
		vll sub_dp(W + 1, vl(2, 0));
		sub_dp[0][1] = 1;
		for (i = 0; i < W; i++) {
			sub_dp[i + 1][0] = sub_dp[i][0] + sub_dp[i][1];
			sub_dp[i + 1][1] = sub_dp[i][0];
		}
		vl sub(W + 1);
		for (i = 0; i < W + 1; i++) {
			sub[i] = (sub_dp[i][0] + sub_dp[i][1]) % MOD;
			//cout << sub[i] << endl;
		}
		vll dp(H + 1, vl(W, 0));
		dp[0][0] = 1;
		for (i = 0; i < H; i++) {
			for (j = 0; j < W; j++) {
				if (j == 0) {
					dp[i + 1][j] = (dp[i][j + 1] * sub[W - 2] + dp[i][j] * sub[W - 1]) % MOD;
				}
				else if (j == W - 1) {
					dp[i + 1][j] = (dp[i][j - 1] * sub[W - 2] + dp[i][j] * sub[W - 1]) % MOD;
				}
				else {
					dp[i + 1][j] = (dp[i + 1][j] + dp[i][j - 1] * sub[j - 1] * sub[W - j - 1]) % MOD;
					dp[i + 1][j] = (dp[i + 1][j] + dp[i][j] * sub[j] * sub[W - j - 1]) % MOD;
					dp[i + 1][j] = (dp[i + 1][j] + dp[i][j + 1] * sub[j] * sub[W - j - 2]) % MOD;
				}
			}
		}
		cout << dp[H][K - 1];
	}
}

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

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

const int N = 2e5 + 5, mod = 998244353;

int fact[N], rev[N];

int mult(int a, int b) {
	return (1LL * a * b) % mod;
}

int pw(int a, int b) {
	if(!b)
		return 1;
	int ans = pw(a, b / 2);
	ans = mult(ans, ans);
	if(b & 1)
		ans = mult(ans, a);
	return ans;
}

void prep() {
	fact[0] = 1;
	for (int i = 1; i < N; i++)
		fact[i] = mult(fact[i - 1], i);
	rev[N - 1] = pw(fact[N - 1], mod - 2);
	for (int i = N - 2; ~i; i--)
		rev[i] = mult(rev[i + 1], i + 1);
}

int sum(int a, int b) {
	a += b;
	if(a >= mod)
		a -= mod;
	if(a < 0)
		a += mod;
	return a;
}

int c(int n, int k) {
	if(k > n)
		return 0;
	return mult(fact[n], mult(rev[k], rev[n - k]));
}

int n, m, k;

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	prep();
	cin >> n >> m >> k;
	int ans = 0;
	for (int i = 0; i <= k; i++)
		ans = sum(ans, mult(m, mult(pw(m - 1, n - 1 - i), c(n - 1, i))));
	cout << ans << "\n";
}


#include<bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb emplace_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define sz(x) (ll)x.size()
#define F first
#define S second
#define FOR(i,a,b) for(ll i=a;i<=b;++i)
#define ROF(i,a,b) for(ll i=a;i>=b;--i)
typedef long long ll;
using namespace std;

const int N = 1000001;

bool cmp(const pair<int,int>&a, const pair<int,int>&b){
    return a.S > b.S;
}

int n;
vector<pair<int,int>>v1, v2;

int main(){
    IOS

    int n;
    cin >> n;
    FOR(i,1,n){
        string s;
        cin >> s;
        int m = s.length();
        int open = 0, closed = 0;
        FOR(j,0,m-1){
            if(s[j] == '(')
                open++;
            else{
                if(open > 0)
                    open--;
                else
                    closed++;
            }
        }
        if(open > closed)
            v1.pb(mp(open, closed));
        else
            v2.pb(mp(open, closed));
    }
    int remmatch = 0;

    sort(all(v1), cmp);
    sort(all(v2));

    for(auto p:v2){
        if(p.F > remmatch){
            cout << "No";
            return 0;
        }
        remmatch -= p.F;
        remmatch += p.S;
    }
    for(auto p:v1){
        if(p.F > remmatch){
            cout << "No";
            return 0;
        }
        remmatch -= p.F;
        remmatch += p.S;
    }

    if(remmatch != 0)
        cout << "No";
    else
        cout << "Yes";

    return 0;
}

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

#define int long long
#define PII pair < int , int >
const int seed = 233 , MOD = 20190816170251;
int N; char str[43];
map < PII , int > cnt;

signed main(){
	scanf("%d %s" , &N , str + 1);
	for(int i = 0 ; i < 1 << N ; ++i){
		int p = 0 , q = 0;
		for(int j = 1 ; j <= N ; ++j)
			if(i >> (j - 1) & 1) p = (p * seed + str[j]) % MOD;
			else q = (q * seed + str[j]) % MOD;
		++cnt[PII(p , q)];
	}
	reverse(str + 1 , str + 2 * N + 1); int ans = 0;
	for(int i = 0 ; i < 1 << N ; ++i){
		int p = 0 , q = 0;
		for(int j = 1 ; j <= N ; ++j)
			if(i >> (j - 1) & 1) p = (p * seed + str[j]) % MOD;
			else q = (q * seed + str[j]) % MOD;
		ans += cnt[PII(p , q)];
	}
	cout << ans; return 0;
}


#pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int cm = 1 << 17;
char cn[cm], * ci = cn + cm, ct;
inline char getcha() {
	if (ci - cn == cm) { fread_unlocked(cn, 1, cm, stdin); ci = cn; }
	return *ci++;
}
inline int getint() {
	int A = 0;
	if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';
	else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';
	return A;
}
const int dm = 1 << 21;
char dn[dm], * di = dn;
const int MAX = 1000;
class unko {
public:
	char C[MAX * 6 + 10];
	int K[MAX + 10];
	constexpr unko() : C(), K() {
		auto di = C + 1;
		C[0] = '0';
		K[0] = 0;
		K[1] = 1;
		rep1(i, MAX) {
			int X = i;
			int keta = 0;
			char c[10] = {};
			while (X) {
				*(c + keta) = '0' + X % 10;
				X /= 10;
				keta++;
			}
			for (int j = keta - 1; j >= 0; j--)* di++ = (*(c + j));
			K[i + 1] = K[i] + keta;
		}
	}
};
constexpr unko f;
inline void putint(ll X) {
	int ue = X / 1000;
	if (ue) {
		for (int i = f.K[ue]; i < f.K[ue + 1]; i++)* di++ = f.C[i];
		int sita = X - ue * 1000;
		rep(i, 3 - f.K[sita + 1] + f.K[sita])* di++ = '0';
		for (int i = f.K[sita]; i < f.K[sita + 1]; i++)* di++ = f.C[i];
	}
	else for (int i = f.K[X]; i < f.K[X + 1]; i++)* di++ = f.C[i];
	*di++ = '\n';
}

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

	int N = getint(), K = getint();

	int A[200001];
	rep(i, N) A[i] = getint();
	ll owa = ll(N) * N;

	rep(p, K) {
		int B[200001] = {};
		rep(i, N) {
			B[max(0, i - A[i])]++;
			B[min(N, i + A[i] + 1)]--;
		}
		ll kei = B[0];
		A[0] = B[0];
		rep1(i, N - 1) {
			kei +=(A[i] = A[i - 1] + B[i]);
		}
		if (kei == owa) break;
	}

	rep(i, N) putint(A[i]);
	fwrite(dn, 1, di - dn, stdout);

	Would you please return 0;
}

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

int main(){
  int A,B,K;
  cin>>A>>B>>K;
  int temp=1;
  for(int i=0;i<K;i++){
    if(temp==1){
      if(A%2 == 1){
        A--;
      }
        B+=A/2;
        A=A/2;
      temp=temp*(-1);
      continue;
    }
    if(temp==(-1)){
      if(B%2 == 1){
        B--;
      }
        A+=B/2;
        B=B/2;

      temp=temp*(-1);
    }
  }
  cout<<A<<" "<<B<<endl;
  return 0;
}


#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>;
typedef vector<int> vi;

int main() {
  int N, x;
  cin >> N >> x;
  int a[N];
  rep(i, N) cin >> a[i];
  sort(a, a+N);

  int ans = 0;

  for (int i = 0; i < N; i++) {
    if (x >= a[i]) {
      ++ans;
      x -= a[i];
    }
  }

  if (ans == N && x > 0) --ans;

  cout << ans << endl;

  return 0;
}

#include <bits/stdc++.h>

// #undef DEBUG  // Uncomment this line to forcefully disable debug print.
#if DEBUG
template <typename T>
void debug(T value) {
  std::cerr << value;
}
template <typename T, typename... Ts>
void debug(T value, Ts... args) {
  std::cerr << value << ", ";
  debug(args...);
}
#define dbg(...)                              \
  do {                                        \
    cerr << #__VA_ARGS__ << ": ";             \
    debug(__VA_ARGS__);                       \
    cerr << " (L" << __LINE__ << ")" << endl; \
  } while (0)
#else
#define dbg(...)
#endif

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T& value, Ts&... args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define in(type, ...) \
  type __VA_ARGS__;   \
  read_from_cin(__VA_ARGS__);

template <typename T>
void write_to_cout(const T& value) {
  std::cout << value << std::endl;
}
template <typename T, typename... Ts>
void write_to_cout(const T& value, const Ts&... args) {
  std::cout << value << ' ';
  write_to_cout(args...);
}
#define out(...) write_to_cout(__VA_ARGS__);

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

using ll = long long;

using namespace std;

int main() {
  in(int, p, q, r);
  out(p + q + r - max({p, q, r}));
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<stack>

using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,n) for(int i=1;i<=n;i++)

vector<int> card[10];
map<stack<int> ,int> check;
int visit[10];

stack<int> ub;
int cont;

void saiki(int dep,int k,int n){
	if(dep==k){
		if(check[ub]==0){
			check[ub]=1;
			cont++;
		}
		return;
	}

	rep(i,n){
		if(visit[i]==0){
			visit[i]=1;
			rep(j,card[i].size()){
				ub.push(card[i][j]);
			}

			saiki(dep+1,k,n);

			rep(j,card[i].size()){
				ub.pop();
			}
			visit[i]=0;
		}
	}
}

int main(){
	while(1){
		int n;
		int k;

		scanf("%d%d",&n,&k);
		if(n==0)break;

		rep(i,10){
			card[i].clear();
			visit[i]=0;
		}

		check.clear();
		cont=0;

		rep(i,n){
			int a;
			scanf("%d",&a);

			if(a<10){
				card[i].push_back(a);
			}else{
				card[i].push_back(a/10);
				card[i].push_back(a%10);
			}
		}

		saiki(0,k,n);

		printf("%d\n",cont);
	}
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for(int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  int n, m;
  cin >> n >> m;
  vi a(m);
  rep(i, m) cin >> a[i];
  sort(all(a), greater<int>());
  vi v = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  vi dp(n + 10, -1);
  dp[0] = 0;
  rep(i, n) rep(j, m) {
    if(dp[i] == -1) continue;
    dp[i + v[a[j]]] = max(dp[i + v[a[j]]], dp[i] + 1);
  }
  string ans;
  int i = n;
  while(i) {
    for(int j : a) {
      if(i - v[j] >= 0 && dp[i - v[j]] == dp[i] - 1) {
        i -= v[j];
        ans += char('0' + j);
        break;
      }
    }
  }
  cout << ans << endl;
}


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

#define INF 10000000
typedef long long ll;

//x^t
ll mypow(ll x, ll t){
  return (ll)pow((double)x, (double)t);
}

ll ans[1000001];

int main(){
  for(ll i=0; i<=1000000; i++)ans[i]=INF;
  for(ll z=0; mypow(z, 3)<=1000000; z++){
    ll tmp;
    for(ll y=0; (tmp=mypow(z,3)+mypow(y,2))<=1000000; y++)
      if(ans[tmp]>y+z)
        ans[tmp]=y+z;
  }
  while(true){
    ll e;
    cin>>e;
    if(e==0)break;
    ll mn=INF;
    for(int i=0; i<=e; i++)
      if(mn>ans[i]+(e-i))mn=ans[i]+(e-i);
    cout<<mn<<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};

int main()
{
  while (int e = in()){
    int m = 1e8;
    for (int z = 0; z <= 100; ++z){
      for (int y = 0; y <= 1000; ++y){
        int x = e - (sq(z) * z + sq(y));
        if (x < 0) continue;
        m = min(m, z + y + x);
      }
    }
    printf("%d\n", m);
  }
  return (0);
}


#include <bits/stdc++.h>
#pragma GCC optimize("O3")

#define fi first
#define se second
#define em emplace
#define eb emplace_back
#define mp make_pair
#define pi pair<int,int>
#define vi vector<int>
#define vpi vector<pair<int,int>>
#define vll vector<ll>
#define vpll vector<pair<ll,ll>>
#define endl '\n'

#define si(v) int(v.size())
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= (m); ++i)
#define rep3(i, n, m) for (ll i = n; i >= (m); --i)

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

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

constexpr ll MOD = 1000000007;

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

  int a; cin >> a;
  cout << a + a*a + a*a*a << endl;

  return (0);
}

#include<bits/stdc++.h>
// Begin Header {{{
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i,n) for(ll i=0; i<n; i++)
#define loop(i, j, n) for(ll i=j; i<n; i++)
#define all(x) (x).begin(), (x).end()
constexpr int INF  = 0x3f3f3f40;
const long long mod=2e9+7;
const long double PI = acos(0);
// }}} End Header
int main() {
	ll x, ans=0;
	cin >> x;
	ans+=1000*(x/500);
	x%=500;
	ans+=5*(x/5);
	cout << ans << endl;
	return 0;
}


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

using namespace std;

vector<int> articulation_points;
vector<int> graph[100000];
int ord[100000],low[100000];
bool visited[100000];

int visit_time = 0;

void dfs(int v,int p){
  visited[v] = true;
  ord[v] = visit_time;
  low[v] = ord[v];
  visit_time++;
  bool is_articulation = false;
  int cnt = 0;
  for(int i=0;i<graph[v].size();++i){
    if(!visited[graph[v][i]]){
      cnt++;
      dfs(graph[v][i],v);
      low[v] = min(low[v],low[graph[v][i]]);
      if(p!=-1&&ord[v]<=low[graph[v][i]]) is_articulation = true;
    }else if(graph[v][i]!=p){
      low[v] = min(low[v],ord[graph[v][i]]);
    }
  }

  if(p==-1&&cnt > 1) is_articulation = true;
  if(is_articulation) articulation_points.emplace_back(v);
}

int main(){
  int N , M ;
  cin >> N >> M;
  for(int i=0;i<M;++i){
    int s,t;
    cin >> s >> t;
    graph[s].push_back(t);
    graph[t].push_back(s);
  }

  for(int i=0;i<N;++i){
    if(!visited[i]) dfs(i,-1);
  }

  sort(articulation_points.begin(),articulation_points.end());
  for(const auto &a:articulation_points){
    cout << a << endl;
  }
  return 0;
}

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

static const int NIL = -1;

struct Vertex {
    vector<int> link;
    int parent = NIL;
    int ord;
    int lowlink;
    bool is_visit = false;
};

class ArticulationPoint {
private:
    int n;
    vector<Vertex> graph;
    int timer = 1;

public:
    ArticulationPoint() {}

    ArticulationPoint(int n)
    {
        this->n = n;
        graph.resize(n);
        int edge;
        cin >> edge;
        int s, t;
        for (int e = 0; e < edge; e++)
        {
            cin >> s >> t;
            graph[s].link.push_back(t);
            graph[t].link.push_back(s);
        }
    }

    void dfs(int current)
    {
        graph[current].is_visit = true;
        graph[current].ord = graph[current].lowlink = timer;
        timer++;

        for(int next : graph[current].link)
        {
            if(!graph[next].is_visit)
            {
                graph[next].parent = current;
                dfs(next);
                graph[current].lowlink = min(graph[current].lowlink, graph[next].lowlink); // 3.
            }
            else if(next != graph[current].parent)
            {
                // １回の移動で行くことのできる訪問済みの頂点は，Backedgeを除けば自分の親しかいない
                // つまりこのブロックにくるのはnextがBackedgeを通った訪問済みの頂点のとき
                graph[current].lowlink = min(graph[current].lowlink, graph[next].ord);
            }

        }
    }

    void articulation_point()
    {
        set<int> art_point;
        int root = 0;

        dfs(root);

        int root_child_num = 0;
        for(int u = 0; u < n; u++)
        {
            int p = graph[u].parent;
            if(p == NIL) continue;
            if(p == root)
            {
                root_child_num++;
                if(root_child_num >= 2)
                    art_point.insert(root);
            }
            else if(graph[p].ord <= graph[u].lowlink)
            {
                art_point.insert(p);
            }
        }

        for(int x : art_point)
            cout << x << endl;
    }
};

int main()
{
    int n;
    cin >> n;
    ArticulationPoint articulationPoint(n);
    articulationPoint.articulation_point();
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
//#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#define INF 0x3f3f3f
typedef long long ll;
const int maxn = 1e3 + 5;
const ll mod = 1e9 + 7;
const double pi = acos(-1.0);
int n, m;
int dis[maxn][maxn];
std::vector<int> v[maxn];

void GetDistance(int u) {
  std::queue<int> q;
  q.push(u);
  dis[u][u] = 0;

  while (!q.empty()) {
    int cur = q.front();
    q.pop();

    for (int i=0; i < v[cur].size(); i++) {
      if (dis[u][cur] + 1 < dis[u][v[cur][i]]) {
        dis[u][v[cur][i]] = dis[u][cur] + 1;
        q.push(v[cur][i]);
      }
    }
  }
}

void FindPath(int s, int t, std::set<int> &S) {
  int pre[maxn];
  memset(pre, 0, sizeof(pre));
  S.insert(s);
  std::queue<int> q;
  q.push(s);
  pre[s] = s;

  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    if (cur == t) {
      int u = cur;
      while (pre[u] != s) {
        u = pre[u];
        S.insert(u);
      }
      return;
    }

    for (int i=0; i < v[cur].size(); i++) {
      if (!pre[v[cur][i]]) {
        pre[v[cur][i]] = cur;
        q.push(v[cur][i]);
      }
    }
  }
}

int main(int argc, char **argv){
  std::cin >> n >> m;
  int x, y;
  for (int i=1; i <= m; i++) {
    std::cin >> x >> y;
    v[x].push_back(y);
  }

  memset(dis, INF, sizeof(dis));
  for (int i=1; i <= n; i++) {
    GetDistance(i);
  }

  int ans = n+2;
  int s = -1, t = -1;
  for (int i=1; i <= n; i++) {
    for (int j=i+1; j <= n; j++) {
      if (dis[i][j] + dis[j][i] < ans) {
        ans = dis[i][j] + dis[j][i];
        s = i;
        t = j;
      }
    }
  }

  // std::cout << s << " " << t << " " << ans << std::endl;
  if (s != -1 && t != -1) {
    std::cout << ans << std::endl;
    std::set<int> S;
    FindPath(s, t, S);
    FindPath(t, s, S);
    for (auto& it: S) {
       std::cout << it << std::endl;
    }
  } else {
    std::cout << "-1" << std::endl;
  }
  return 0;
}

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#define chmin(a, b) ((a)=min((a), (b)))
#define chmax(a, b) ((a)=max((a), (b)))
#define fs first
#define sc second
#define eb emplace_back
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

const ll MOD=1e9+7;
const ll INF=1e18;
const double pi=acos(-1);
const double eps=1e-10;

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

int main(){
    int n, m; cin>>n>>m;
    vector<int> p(m), q(m), c(m);
    vector<P> v;
    for(int i=0; i<n; i++){
        v.push_back(P(i, -1));
    }

    for(int i=0; i<m; i++){
        cin>>p[i]>>q[i]>>c[i];
        p[i]--, q[i]--;
        v.push_back(P(p[i], c[i]));
        v.push_back(P(q[i], c[i]));
    }

    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    int nn = v.size();

    map<P, int> p2v;
    for(int i=0; i<nn; i++){
        p2v[v[i]] = i;
    }

    vector<vector<P>> edge(nn);
    for(int i=0; i<nn; i++){
        if(v[i].second == -1) continue;

        int station = v[i].first;
        int out = p2v[P(station, -1)];
        edge[i].push_back(P(out, 0));
        edge[out].push_back(P(i, 1));
    }

    for(int i=0; i<m; i++){
        int s = p2v[P(p[i], c[i])];
        int t = p2v[P(q[i], c[i])];
        edge[s].push_back(P(t, 0));
        edge[t].push_back(P(s, 0));
    }

    int s = p2v[P(0, -1)];
    int g = p2v[P(n-1, -1)];
    vector<int> dist(nn, 1e9);
    dist[s] = 0;

    priority_queue<P, vector<P>, greater<P>> que;
    que.push(P(0, 0));
    while(que.size()){
        int cdist, cv;
        tie(cdist, cv) = que.top();
        que.pop();

        if(dist[cv] < cdist) continue;

        for(auto i:edge[cv]){
            int nv, ndist;
            tie(nv, ndist) = i;

            if(dist[cv] + ndist < dist[nv]){
                dist[nv] = dist[cv] + ndist;
                que.push(P(dist[nv], nv));
            }
        }
    }

    cout << (dist[g] == 1e9 ? -1 : dist[g]) << endl;
}


#include <iostream>
#include <string>
#include <map>
using namespace std;

int main(void) {
    map<char,int> a = {{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};
    string str;
    while(getline(cin, str)) {
        int b = 0, c = a[str[0]], d = a[str[0]];
        for(int i = 1; i < str.length(); ++i) {
            int e = a[str[i]];
            if (e == d) {
                c += e;
            }
            else if (d < e) {
                b  -=  (c);
                c = e;
            }
            else if (e < d) {
                b += (c);
                c = e;
            }
            d = e;
        }
        b += c;
        cout << b << endl;;
    }
    return 0;
}

#include <iostream>
using namespace std;

int main()
{
	int date[13][32];
	for(int i = 1;i<=31;i++)date[1][i] = i;
	for(int i = 1;i<=28;i++)date[2][i] = i+3;
	for(int i = 1;i<=31;i++)date[3][i] = i+4;
	for(int i = 1;i<=31;i++)date[4][i] = i;
	for(int i = 1;i<=31;i++)date[5][i] = i+2;
	for(int i = 1;i<=31;i++)date[6][i] = i+5;
	for(int i = 1;i<=31;i++)date[7][i] = i;
	for(int i = 1;i<=31;i++)date[8][i] = i+3;
	for(int i = 1;i<=31;i++)date[9][i] = i+6;
	for(int i = 1;i<=31;i++)date[10][i] = i+1;
	for(int i = 1;i<=31;i++)date[11][i] = i+4;
	for(int i = 1;i<=31;i++)date[12][i] = i+6;

	int month,day;
	while(true)
	{
		cin >> month;
		cin >> day;
		if(month==0&&day==0)break;
		switch (date[month][day] % 7)
		{
			case 0:
			cout << "Wednesday" << endl;
			break;
			case 1:
			cout << "Thursday" << endl;
			break;
			case 2:
			cout << "Friday" << endl;
			break;
			case 3:
			cout << "Saturday" << endl;
			break;
			case 4:
			cout << "Sunday" << endl;
			break;
			case 5:
			cout << "Monday" << endl;
			break;
			case 6:
			cout << "Tuesday" << endl;
			break;
		}

	}

	return 0;
}


#pragma GCC optimize ("Ofast")
#pragma GCC target ("avx2")

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

#define NMAX 200001

int a[NMAX];
int imos[NMAX];

void reset(int n) {
    for(int i = 0; i < n; i++) imos[i] = 0;
}

int max(int a, int b) {
    if(a > b) return a;
    else return b;
}

int min(int a, int b) {
    if(a > b) return b;
    else return a;
}

int get_int() {
    int res = 0;
    char c = getchar();
    while(c != EOF && c != ' ' && c != '\n') {
        res *= 10;
        res += c - '0';
        c = getchar();
    }
    return res;
}

void put_int(int n) {
    char c[30];
    int sz = 0;
    c[sz++] = (n % 10) + '0';
    n /= 10;
    while(n > 0) {
        c[sz++] = (n % 10) + '0';
        n /= 10;
    }
    for(int i = sz-1; i >= 0; i--) {
        putchar(c[i]);
    }
    putchar('\n');
}

int main(){

    int n, k;
    n = get_int();
    k = get_int();
    for(int i = 0; i < n; i++) a[i] = get_int();

    for(int itr = 0; itr < k; itr++) {
        int f = 0;
        reset(n+1);
        for(int i = 0; i < n; i++){
            imos[max(0, i - a[i])]++;
            imos[min(n, i + a[i] + 1)]--;
        }
        int acc = 0;
        for(int i = 0; i < n; i++){
            acc += imos[i];
            a[i] = acc;
        }
        int sum = 0;
        for(int i = 0; i < n; i++) sum += a[i];
        if(sum == n * n) break;
    }

    for(int i = 0; i < n; i++) put_int(a[i]);
    return 0;
}

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
        int a[5001] = { 0 };
        int n;
    while (1) {
        cin >> n;
        if (n == 0)break;
       // a[0] = 0;
        int maxv = -100000;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        for (int i = 1; i <= n; i++) {
            a[i] = max(a[i], a[i] + a[i - 1]);
            maxv = max(maxv, a[i]);
        }
        cout << maxv << 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> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

int main(){
    int n;
    ll x;
    cin >> n >> x;
    vector<ll> a(n);
    rep(i,n) cin >> a[i];
    sort(a.begin(),a.end());
    ll now = 0;
    int ans = n;
    rep(i,n){
        now += a[i];
        if(now > x){
            ans = i;
            break;
        }
    }

    if(ans==n&&now!=x) cout << (n-1) << endl;
    else cout << ans << endl;
    return 0;
}

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

int main()
{
    int N, x;
    cin >> N >> x;
    vector<int> v(N);

    for (int i = 0; i < N; i++)
    {
        cin >> v.at(i);
    }
    sort(v.begin(), v.end());

    int cnt = 0;
    if (x < v.at(0))
    {
        cout << 0;
    }
    else
    {
        for (int i = 0; i < N && x >= v.at(i); i++)
        {
            cnt++;
            x -= v.at(i);
        }

        if (cnt == N && x > 0)
            cnt--;

        cout << cnt;
    }
}


#include<bits/stdc++.h>
#define N 110
using namespace std;
char s[N][N];
int sum[N][N],h,w,eh,ew;
int dp[N*N/4][N*N/4];
inline int rg(int l,int r,int d,int u){
	if(l>r||d>u) return 0;
	return sum[r][u]-sum[l-1][u]-sum[r][d-1]+sum[l-1][d-1];
}
int solve(int ch,int cw){
	if(!ch&&!cw) return 0;
	if(dp[ch][cw]>=0) return dp[ch][cw];
	int l=ch%eh,r=ch/eh,d=cw%ew,u=cw/ew;
	if(l){
		dp[ch][cw]=max(dp[ch][cw],solve(ch-1,cw)+ (eh-l>r?rg(eh-l,eh-l,max(1+u,ew-d),min(w-d,ew+u)):0) );
	}
	if(r){
		dp[ch][cw]=max(dp[ch][cw],solve(ch-eh,cw)+ (eh+r<=h-l?rg(eh+r,eh+r,max(1+u,ew-d),min(w-d,ew+u)):0) );
	}
	if(d){
		dp[ch][cw]=max(dp[ch][cw],solve(ch,cw-1)+ (ew-d>u?rg(max(1+r,eh-l),min(h-l,eh+r),ew-d,ew-d):0) );
	}
	if(u){
		dp[ch][cw]=max(dp[ch][cw],solve(ch,cw-ew)+ (ew+u<=w-d?rg(max(1+r,eh-l),min(h-l,eh+r),ew+u,ew+u):0) );
	}
	return dp[ch][cw];
}
int main(){
	scanf("%d%d",&h,&w);
	for(int i=1;i<=h;i++){
		scanf("%s",s[i]+1);
		for(int j=1;j<=w;j++){
			sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
			if(s[i][j]=='o') sum[i][j]++;
			else if(s[i][j]=='E') eh=i,ew=j;
		}
	}
	memset(dp,-1,sizeof(dp));
	printf("%d\n",solve(eh*(h-eh)+eh-1,ew*(w-ew)+ew-1));
	return 0;
}

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

int n,m,Ex,Ey,Ans,f[2][110][110][110];
int g[110][110],row[110][110],line[110][110];

inline int Row(int id,int L,int R) {return row[id][R]-row[id][L-1];}
inline int Line(int U,int D,int id) {return line[D][id]-line[U-1][id];}

int main()
{
//#ifdef h10
//	freopen("robots.in","r",stdin);
//	freopen("robots.out","w",stdout);
//#endif
	int i,j,U,D,L,R;
	char c;
	scanf("%d%d",&n,&m);
	for (i=1;i<=n;i++)
	for (j=1;j<=m;j++)
	{
		c=getchar();
		while (c!='.'&&c!='o'&&c!='E') c=getchar();
		if (c=='o') g[i][j]=1;
		if (c=='E') Ex=i,Ey=j;
		row[i][j]=row[i][j-1]+g[i][j];
		line[i][j]=line[i-1][j]+g[i][j];
	}
	for (U=0;U<Ex;U++)
	for (D=0;D<=n-Ex;D++)
	for (L=0;L<Ey;L++)
	for (R=0;R<=m-Ey;R++)
	{
		int X=U&1,Y=X^1,&val=f[X][D][L][R];
		int u=max(D+1,Ex-U),d=min(Ex+D,n-U);
		int l=max(R+1,Ey-L),r=min(Ey+R,m-L);
		if (U&&Ex-U-D>0) val=max(val,f[Y][D][L][R]+Row(Ex-U,l,r));
		if (D&&Ex+D+U<=n) val=max(val,f[X][D-1][L][R]+Row(Ex+D,l,r));
		if (L&&Ey-L-R>0) val=max(val,f[X][D][L-1][R]+Line(u,d,Ey-L));
		if (R&&Ey+R+L<=m) val=max(val,f[X][D][L][R-1]+Line(u,d,Ey+R));
		Ans=max(Ans,val);
	}
	printf("%d\n",Ans);
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
int main(){
  int sx,sy,tx,ty;
  cin >> sx >> sy >> tx >> ty;
  string ans;
  int zyoge=abs(ty-sy);
  int sayu=abs(tx-sx);
  char rl,ud;
  //1
  if(tx-sx>=0){
    rl='R';
  }else{
    rl='L';
  }
  if(ty-sy>=0){
    ud='U';
  }else{
    ud='D';
  }
  int i,j;
  for(i=0;i<zyoge;i++){
    ans=ans+ud;
  }
  for(j=0;j<sayu;j++){
    ans+=rl;
  }
  //2
  if(tx-sx>=0){
    rl='L';
  }else{
    rl='R';
  }
  if(ty-sy>=0){
    ud='D';
  }else{
    ud='U';
  }
  for(i=0;i<zyoge;i++){
    ans=ans+ud;
  }
  for(j=0;j<sayu;j++){
    ans+=rl;
  }
  //3
  if(tx-sx>=0){
    rl='R';
    ans+='D';
  }else{
    rl='L';
    ans+='D';
  }
  if(ty-sy>=0){
    ud='U';
  }else{
    ud='D';
  }
  for(j=0;j<sayu+1;j++){
    ans+=rl;
  }
  for(i=0;i<zyoge+1;i++){
    ans=ans+ud;
  }
  //4
  if(tx-sx>=0){
    ans+='L';
    rl='L';
    ans+='U';
  }else{
    ans+='R';
    rl='R';
    ans+='U';
  }
  if(ty-sy>=0){
    ud='D';
  }else{
    ud='U';
  }
  for(j=0;j<sayu+1;j++){
    ans+=rl;
  }
  for(i=0;i<zyoge+1;i++){
    ans=ans+ud;
  }
   if(tx-sx>=0){
    ans+='R';
  }else{
    ans+='L';
  }
  cout << ans;
}

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

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
using ll = long long;
string char_to_string(char val) {
  return string(1, val);
}
int char_to_int(char val) {
  return val - '0';
}
template<class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template<class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance( vec.begin(), itr );
  if (index != vec.size()) { // 発見できたとき
    return 1;
  }
  else { // 発見できなかったとき
    return 0;
  }
}

int main() {
  int sx, sy, tx, ty; cin >> sx >> sy >> tx >> ty;
  int x = tx - sx; int y = ty - sy;
  vector<string> ans;
  REP(i, y) ans.push_back("U");
  REP(i, x) ans.push_back("R");
  REP(i, y) ans.push_back("D");
  REP(i, x) ans.push_back("L");
  ans.push_back("L");
  REP(i, y + 1) ans.push_back("U");
  REP(i, x + 1) ans.push_back("R");
  ans.push_back("D");
  ans.push_back("R");
  REP(i, y + 1) ans.push_back("D");
  REP(i, x + 1) ans.push_back("L");
  ans.push_back("U");
  REP(i, ans.size()) cout << ans[i];
  cout << endl;
}


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

int main() {
	int N, K;
	cin >> N >> K;
	vector<int> v(N);
	rep(i, N) {
		cin >> v.at(i);
	}

	int a=0, b=0, c=0, x, y, z;
    rep(i, min(K,50)) {
        vector<int> vv(N+1);
        rep(j, N) {
            x = v.at(j);
            vv.at(max(j-x,  0)) += 1;
            vv.at(min(j+x+1,N)) -= 1;
        }
        v.at(0) = vv.at(0);
        rep1(j, N) {
            v.at(j) = v.at(j-1) + vv.at(j);
        }
    }

    rep(i, N) {
        cout << v.at(i) << " ";
    }
}

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

#ifdef LOCAL
#define debug(var) cerr << (#var) << " = " << (var) << endl;
#else
#define debug(var)
#endif

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

void solve() {
  int n, k; scanf("%d%d", &n, &k);
  int g = -1, m = 0;
  for (int a, i = 0; i < n; ++i) {
    scanf("%d", &a);
    m = max(m, a);
    if (g == -1) g = a;
    else g = __gcd(g, a);
  }
  if (k > m || k % g) puts("IMPOSSIBLE");
  else puts("POSSIBLE");
}

int main() {
  init();
  int t = 1; //scanf("%d", &t);
  while (t--) {
    solve();
  }
  return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define mod 998244353
#define ld long double
#define pb push_back
#define inf INT_MAX
#define N 1000000
using namespace std;
int n,m,k;
ll ans=0;
ll inv[N+3];
ll fact_inv[N+3];
ll fact[N+3];
//modulo inverse
void inverse(){
	inv[1]=1;//MMI
	int i;
	for(i=2;i<=N;i++){
		inv[i]=mod-(mod/i)*inv[mod%i]%mod;
	}
}
void factinv(){
	int i;
	fact_inv[0]=1;
	for(i=1;i<=N;i++){
		fact_inv[i]=fact_inv[i-1]*inv[i]%mod;
	}
}
void facto(){
	int i;
	fact[0]=1;
	for(i=1;i<=N;i++){
		fact[i]=(fact[i-1]*i)%mod;
	}
}
ll power(int a,int b){
	if(b==0)
	return 1;
	else if(b==1)
	return a;
	else{
		if(b&1)
		return ((a%mod)*(power(a,b-1)%mod))%mod;
		else
		return ((power(a,b/2)%mod)*(power(a,b/2)%mod))%mod;
	}
}
void solve(int p){
	ll a=((fact_inv[n-p-1]%mod)*(fact_inv[p]%mod))%mod;
	ll b=(m*power(m-1,n-p-1)%mod)%mod;
	ll c=(a*b)%mod;
	ans+=(fact[n-1]*c)%mod;
	ans%=mod;
}
int main(void){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int i;
	cin>>n>>m>>k;
	facto();
	inverse();
	factinv();
	//cout<<inv[123]<<endl;
	for(i=0;i<=k;i++)
	solve(i);
	cout<<ans<<endl;
}


#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};

int n,m=250;
int S[300][300];
vector<P> v;

void plot(ll x,ll y){
    int cnt2=0,cnt5=0;
    while(x%2==0){
        x/=2;
        cnt2+=1;
    }
    while(y%2==0){
        y/=2;
        cnt2-=1;
    }
    while(x%5==0){
        x/=5;
        cnt5+=1;
    }
    while(y%5==0){
        y/=5;
        cnt5-=1;
    }
    S[cnt2+100][cnt5+100]+=1;
    v.push_back(P(cnt2,cnt5));
}

void solve(){
    cin >> n;
    rep(i,n){
        ll a=0,b=1;string s;
        cin >> s;
        bool flag=false;
        rep(i,s.length()){
            if(s[i]=='.') flag=true;
            else{
                if(flag) b*=10;
                a*=10;
                a+=s[i]-'0';
            }
        }
        plot(a,b);
    }
    rep(i,m) rep(j,m) {
        S[i][j+1]=S[i][j]+S[i][j+1];
    }
    rep(j,m+1) rep(i,m) {
        S[i+1][j]=S[i][j]+S[i+1][j];
    }
    ll ans=0;
    rep(i,n){
        int x=v[i].first,y=v[i].second;
        ans+=S[m][m]-S[-x+99][m]-S[m][-y+99]+S[-x+99][-y+99];
        if(x>=0 && y>=0) ans-=1;
    }
    cout << ans/2 << endl;
}

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

#include <bits/stdc++.h>
#define ll long long
using namespace std;
void solve() {
    int n;
    cin >> n;
    map<pair<int, int>, int> cnt;
    for (int i = 1; i <= n; ++i) {
        long double ai;
        cin >> ai;
        ll val = (ll) (ai * 1000000000.0 + 0.5);
        ll cnt2 = 0;
        ll cnt5 = 0;
        while (val % 2 == 0) {
            cnt2++;
            val /= 2;
        }
        while (val % 5 == 0) {
            cnt5++;
            val /= 5;
        }
        cnt[make_pair(cnt2, cnt5)]++;
    }

    ll ans = 0;
    for (const auto &pi: cnt) {
        for (const auto &pj: cnt) {
            if (pi.first.first + pj.first.first >= 18 &&
                pi.first.second + pj.first.second >= 18) {
                if (pi == pj) {
                    ans += pi.second * (pi.second - 1ll);
                } else {
                    ans += pi.second * 1ll * pj.second;
                }
            }
        }
    }
    cout << ans / 2 << endl;
}
int main(){
	solve();
}


//
//AtCoder Beginner Contest 125
//D
//https://atcoder.jp/contests/abc125/tasks/abc125_d
//

#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <queue>
#include <map>
#include <set>
#include <tuple>
#include <functional>
#include <cassert>
#include <bitset>
#include <numeric>

using namespace std;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)
#define ALL(x) (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define SIZE(x) (int)(x).size()
#define SIZEL(x) (ll)(x).size()
#define MSG(a) cout << #a << " " << a << endl;

using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;

const double PI = 3.14159265358979323846;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll gcd(ll x, ll y) { return (x % y)? gcd(y, x % y): y; }    //最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }            //最小公倍数

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

    vector<int> a(n);

    int mCnt=0;
    int mMax=1e9*-1;
    int pMin=1e9;
    REP(i, n) {
        cin >> a[i];
        if (a[i]<0) {
            mCnt++;
            mMax = max(mMax, a[i]);
        }else{
            pMin = min(pMin, a[i]);
        }

    }

    ll ans=0;
    REP(i,n) ans+=abs(a[i]);
    if (mCnt%2==1){
        ans -= 2*min(pMin, abs(mMax));
    }
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define irep(i, n) for (int i = (n); i >= 0; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1 << 25;
const int MOD = 1e9+7;
int a[int(1e5+10)];
ll dp[100010][2];
int main() {
	int n;
	cin >> n;
	rep(i,n) cin >> a[i];
	dp[0][0] = 0;
	dp[0][1] = -(1LL<<60);
	rep(i,n) {
		dp[i+1][0]=max(dp[i][0]+a[i],dp[i][1]-a[i]);
		dp[i+1][1]=max(dp[i][0]-a[i],dp[i][1]+a[i]);
	}
	cout << dp[n][0] << endl;
	return 0;
}


#include <iostream>
#include <string>
#include <vector>

enum face {
  TOP,
  FRONT,
  RIGHT,
  LEFT,
  BACK,
  BOTTOM
};

class dice {
  public:
    dice();
    void setDice(int spot, int eye);
    int getSpot(int top);
    void rollNorth();
    void rollSouth();
    void rollEast();
    void rollWest();
    void rollRight();
    void rollLeft();
    std::vector<dice> all_rolls();

    int& operator[] (face f) {return id[f];}
    const int& operator[] (face f) const {return id[f];}
    bool operator==(const dice &b) const {
      const dice &a = *this;
      return a[TOP] == b[TOP] && a[BOTTOM] == b[BOTTOM] &&
             a[FRONT] == b[FRONT] && a[BACK] == b[BACK] &&
             a[LEFT] == b[LEFT] && a[RIGHT] == b[RIGHT];
    }

    bool equivalent_to(const dice &di);

  private:
    void roll(face a, face b, face c, face d);
    std::vector<int> id;
};

dice::dice() {
  id.resize(6);
}

void dice::setDice(int spot, int eye) {
  switch(eye) {
    case TOP:
      id[TOP] = spot;
      break;
    case BOTTOM:
      id[BOTTOM] = spot;
      break;
    case FRONT:
      id[FRONT] = spot;
      break;
    case BACK:
      id[BACK] = spot;
      break;
    case LEFT:
      id[LEFT] = spot;
      break;
    case RIGHT:
      id[RIGHT] = spot;
      break;
  }
}

int dice::getSpot(int top) {
  return id[top];
}

void dice::rollNorth() {
  roll(TOP, FRONT, BOTTOM, BACK);
}

void dice::rollSouth() {
  roll(TOP, BACK, BOTTOM, FRONT);
}

void dice::rollEast() {
  roll(TOP, LEFT, BOTTOM, RIGHT);
}

void dice::rollWest() {
  roll(TOP, RIGHT, BOTTOM, LEFT);
}

void dice::rollRight() {
  roll(FRONT, LEFT, BACK, RIGHT);
}

void dice::rollLeft() {
  roll(FRONT, RIGHT, BACK, LEFT);
}

std::vector<dice> dice::all_rolls() {
  std::vector<dice> ret;
  for(int k = 0; k < 6; (k&1 ? rollEast(): rollNorth()), ++k) {
    for(int i = 0; i < 4; rollRight(), ++i) {
      ret.push_back(*this);
    }
  }
  return ret;
}

bool dice::equivalent_to(const dice &di) {
  for(int k = 0; k < 6; (k & 1 ? rollEast(): rollNorth()), ++k) {
    for(int i = 0; i < 4; rollRight(), ++i) {
      if(*this == di) {
        return true;
      }
    }
  }
  return false;
}

void dice::roll(face a, face b, face c, face d) {
  int tmp = id[a];
  id[a] = id[b];
  id[b] = id[c];
  id[c] = id[d];
  id[d] = tmp;
}

int main() {
  int d, n;
  std::cin >> n;
  std::vector<dice> dices(n);

  for(int i = 0; i < n; ++i) {
    dice di;
    for(int j = 0; j < 6; ++j) {
      std::cin >> d;
      di.setDice(d, j);
    }
    dices[i] = di;
  }

  for(int i = 0; i < n; ++i) {
    for(int j = i + 1; j < n; ++j) {
      if(dices[i].equivalent_to(dices[j])) {
        std::cout << "No" << std::endl;
        return 0;
      }
    }
  }
  std::cout << "Yes" << std::endl;

  return 0;
}

#include <iostream>
#include <vector>

using namespace std;

int main(){
	int N, Q;
	while(cin >> N >> Q, N){
		vector<int> cnt(101, 0);
		for(int i=0;i<N;i++){
			int M; cin >> M;
			for(int j=0;j<M;j++){
				int d; cin >> d;
				cnt[d]++;
			}
		}
		int res = 0;
		for(int i=0;i<=100;i++){
			if(cnt[res] < cnt[i]) res = i;
		}
		cout << (cnt[res] >= Q ? res : 0) << endl;
	}
}


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

#define MAX 256
#define S(x) (x*x)

int f(int z){
	if (z < 10)return 1;
	return 1 + f(z / 10);
}

int main(void){

	int ans[MAX];
	for (int i = 0; i < MAX; i++){
		ans[i] = 0;
	}

	int d;
	int i = 0;

	while (scanf("%d", &d) != EOF){
		for (int j = d; j <= 600 - d; j += d){
			ans[i] += S(j)*d;
		}
		i++;
	}

	for (int j = 0; j < i; j++){
		printf("%d\n", ans[j]);
	}

	return 0;
}

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

int f(int x)
{
	return pow(x, 2);
}

int main()
{
	int n;
	while (cin >> n) {
		long long int sum = 0;
		for (int i = n; i <= 600 - n; i += n)
			sum += f(i)*n;
		cout << sum << endl;
	}
	return 0;
}