#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1000000007;
//const ll MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const double pi = acos(-1.0);
const double EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

int N;
int A[100000];

int res(){
    int K = 0;
    rep(i, N){
        if(A[i]%2 == 0) K++;
    }
    if(K&1) return 1;
    if(K != N-1) return 0;
    int g = A[0]&~1;
    rep(i, N){
        if(A[i] == 1) return 0;
        if(A[i]%2 == 1) A[i]--;
        g = gcd(g, A[i]);
    }
    rep(i, N) A[i] /= g;
    return res()^1;
}

int main(){
    cin >> N;
    rep(i, N) cin >> A[i];
    cout << (res()? "First" : "Second") << endl;
}

#define _CRT_SECURE_NO_WARNINGS

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <array>
#include <cassert>
#include <bitset>
using namespace std;
using LL = long long;

LL N;
vector<LL>A;
vector<LL>xmos;
const LL MOD = 1000000007;

LL solvedp(vector<pair<LL, int>>& vec, LL lasval) {
	LL zero = 1, notz = 0;
	LL zpar = 1, npar = 0;
	for (auto p : vec) {
		if (p.first == 0) {
			zpar = (notz * p.second) % MOD;
			zero += zpar;
			zero %= MOD;
		}
		else {
			npar = (zero * p.second) % MOD;
			notz += npar;
			notz %= MOD;
		}
	}
	if (lasval == 0) {
		return zpar;
	}
	return npar;
}

int main() {
	cin >> N;
	LL xsum = 0;
	xmos.push_back(0);
	for (int i = 0; i < N; ++i) {
		LL tmp;
		cin >> tmp;
		A.push_back(tmp);
		xsum ^= tmp;
		xmos.push_back(xsum);
	}
	/*vector<LL>dp(N + 1);
	dp[1] = 1;
	LL odd = 1, eve = 0;
	for (int i = 2; i <= N; ++i) {
		if (i % 2) {
			dp[i]++;
			dp[i] += eve;
			dp[i] %= MOD;
			odd += dp[i];
			odd %= MOD;
		}
		else {
			dp[i] += odd;
			eve += dp[i];
			eve %= MOD;
		}
	}*/
	if (xsum != 0) {
		vector<pair<LL, int>>nowarr;
		for (int i = 1; i <= N; ++i) {
			if (xmos[i] == 0) {
				nowarr.push_back({ 0,1 });
			}
			if (xmos[i] == xsum) {
				nowarr.push_back({ xsum,1 });
			}
		}
		LL ans = solvedp(nowarr, xsum);
		cout << ans << endl;
	}
	else {
		map<LL, int>zerolas;
		map<LL, vector<pair<LL, int>>>arrs;
		int zerocnt = 0;
		LL zerotwo = 1;
		for (int i = 1; i <= N; ++i) {
			if (xmos[i] == 0) {
				zerocnt++;
				if (i < N) {
					zerotwo *= 2;
					zerotwo %= MOD;
				}
			}
			else {
				if (zerocnt != zerolas[xmos[i]]) {
					arrs[xmos[i]].push_back({ 0,zerocnt - zerolas[xmos[i]] });
					zerolas[xmos[i]] = zerocnt;
				}
				arrs[xmos[i]].push_back({ xmos[i],1 });
			}
		}
		LL ans = zerotwo;
		for (auto& elm : arrs) {
			if (elm.second.back().second != 0) {
				elm.second.push_back({ 0,1 });
			}
			ans += solvedp(elm.second, 0);
			ans %= MOD;
		}
		cout << ans << endl;
	}
	return 0;
}


#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main(){

	int N,Q,M,Date[101],i,l,k;

	while(0<=scanf("%d%d",&N,&Q)){
		for(i=0;i<101;i++){
			Date[i]=0;
		}

		if((N==0) && (Q==0)){
			break;
		}

		for(k=0;k<N;k++){
			scanf("%d",&M);
			if(M==0){
				continue;
			}else{
				for(i=0;i<M;i++){
					scanf("%d",&l);
					++Date[l];
				}
			}
		}
		l=0;
		i=0;
		for(k=0;k<100;k++){
			if(i<Date[k] && Date[k]>=Q){
				i=Date[k];
				l=k;
			}
		}

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

	return 0;
}

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init));

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

vector<string> Split(char c,string s){
  vector<string> res(0);
  string str = "";
  rep(i,s.size()){
    if(s[i] == c){
      res.push_back(str);
      str.clear();
    }
    else{
      str += s[i];
    }
  }
  res.push_back(str);
  return res;
}
template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,m;
  while(cin >> n >> m && n && m){
    vector<ll> v(101);
    ll k,tmp;
    rep(i,n){
      cin >> k;
      rep(j,k){
        cin >> tmp;
        v[tmp]++;
      }
    }
    ll ans = 0,cnt = m-1;
    rep(i,101){
      if(v[i] >= cnt){
        if(v[i] > cnt)
          ans = i;
        cnt = v[i];
      }
    }
    if(ans) cout << ans << endl;
    else cout << 0 << endl;
  }
  return 0;
}


#include <cstdio>
#include <cstring>
#include <utility>
using namespace std;

int main() {
	char str[21];
	scanf("%s",str);
	int length = strlen(str);
	for(int i=0;i<length/2;++i) {
		swap(str[i],str[(length-1)-i]);
	}
	puts(str);
	return 0;
}

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

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

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define what_is(x) cerr << #x << " is " << x << endl;
#define MT make_tuple
#define eb emplace_back
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

#define REP(i,a,b) for (int i = a; i <= b; i++)
#define FOR(i,n) for (int i=0;i < n ; i++)
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

int main(){
	ll n;
	ll i;
	cin >> n;
	i=0;
	while((i+1)*(i+1)<=n) i++;
	cout << i*i;

	return 0;
}


#include <bits/stdc++.h>

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

using namespace std;

int main(){
	int n,h,w; scanf("%d%d%d",&n,&h,&w);
	printf("%d\n",(n-h+1)*(n-w+1));
	return 0;
}

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

int main() {

    int64_t n,w,h;
    cin>>n>>h>>w;

    int cnt=0;
    for(int i=1;i<=n;i++){
      for(int j=1;j<=n;j++){
        if(n-i+1>=h&&n-j+1>=w)
          cnt++;
      }
    }

    cout<<cnt<<endl;

	return 0;
}


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

int main() {
  int n,q;
  vector<int> S,T;

  cin >> n;
  for(int i=0;i<n;i++) {
    int s;
    cin >> s;
    S.push_back(s);
  }
  cin >> q;
  for(int i=0;i<q;i++) {
    int t;
    cin >> t;
    T.push_back(t);
  }

  // O(n^2)
  int C = 0;
  for(int t_itr=0;t_itr<q;t_itr++) {
    int key = T[t_itr];
    for(int s_itr=0;s_itr<n;s_itr++) {
      if(S[s_itr] == key) {
        C++;
        break;
      }
    }
  }

  cout << C << endl;
}

#pragma region
#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <list>
#include <iomanip>
#include <cstdint>
#include <bitset>

using namespace std;
typedef long long ll;
//#define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s) - 1; (i) >= 0; --(i))
#define all(x) x.begin(),x.end()

#pragma region UnionFind
struct UnionFind
{
	vector<int> par;

	UnionFind(int n) : par(n, -1) {}
	void init(int n) { par.assign(n, -1); }

	int root(int x)
	{
		if (par[x] < 0) return x;
		else return par[x] = root(par[x]);
	}

	bool issame(int x, int y)
	{
		return root(x) == root(y);
	}

	bool merge(int x, int y)
	{
		x = root(x); y = root(y);
		if (x == y) return false;
		if (par[x] > par[y]) swap(x, y);
		par[x] += par[y];
		par[y] = x;
		return true;
	}

	int size(int x)
	{
		return -par[root(x)];
	}
};
#pragma endregion
#pragma region GCD
int gcd(int a, int b)
{
	if (a%b == 0)return b;
	return gcd(b, a%b);
}
#pragma endregion
#pragma region chmin
template<class T> inline bool chmin(T& a, T b)
{
	if (a > b)
	{
		a = b;
		return true;
	}
	return false;
}
#pragma endregion
#pragma region chmax
template<class T> inline bool chmax(T& a, T b)
{
	if (a < b)
	{
		a = b;
		return true;
	}
	return false;
}
#pragma endregion
#pragma endregion

int main()
{
	int n; cin >> n;
	vector<ll> a(n);
	map<ll, int> mp;
	rep(i, n)
	{
		cin >> a[i];
		++mp[a[i]];
	}
	sort(all(a), greater<ll>());
	int res = 0;
	rep(i, n)
	{
		if (!mp[a[i]])continue;
		--mp[a[i]];
		ll tmp = 1;
		while (tmp <= a[i])tmp = tmp << 1;
		if (mp[tmp - a[i]])
		{
			--mp[tmp - a[i]];
			++res;
		}
	}
	cout << res << endl;
}


#include <bits/stdc++.h>

using namespace std;

const int MAXN = 305;
int dp[MAXN][MAXN][MAXN];

int main()
{
    string s;
    cin >> s;
    int k;
    cin >> k;
    for(int sz = 1; sz <= s.size(); sz++){
        for(int l = 0; l < s.size() - sz + 1; l++){
            int r = l + sz - 1;
            for(int j = 0; j <= k; j++){
                if(r == l){
                    dp[l][r][j] = 1;
                    continue;
                }
                if(r - l == 1){
                    if(j == 0){
                        if(s[l] == s[r]){
                            dp[l][r][j] = 2;
                        }
                        else{
                            dp[l][r][j] = 1;
                        }
                    }
                    else{
                        dp[l][r][j] = 2;
                    }
                    continue;
                }
                dp[l][r][j] = max(dp[l + 1][r][j], dp[l][r - 1][j]);
                if(s[l] == s[r]){
                    dp[l][r][j] = max(dp[l][r][j], dp[l + 1][r - 1][j] + 2);
                }
                else if(j != 0){
                    dp[l][r][j] = max(dp[l][r][j], dp[l + 1][r - 1][j - 1] + 2);
                }
            }
        }
    }
    cout << dp[0][s.size() - 1][k];
}


#include <iostream>
#include <algorithm>

using namespace std;
static const int SIZE = 101;
static const long long int INF = 1e18;
long long int d[SIZE][SIZE] = { 0 };

void solve()
{
	for (int i = 0; i < SIZE; ++i)
	{
		for (int j = 0; j < SIZE; ++j)
		{
			d[i][j] = INF;
		}
		d[i][i] = 0;
	}
	int V, E;
	cin >> V >> E;
	for (int i = 0; i < E; ++i)
	{
		int from, to, cost;
		cin >> from >> to >> cost;
		d[from][to] = cost;
	}
	for (int k = 0; k < V; ++k)
	{
		for (int i = 0; i < V; ++i)
		{
			for (int j = 0; j < V; ++j)
			{
				if (d[i][k] != INF && d[k][j] != INF)
				{
					d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
				}
			}
		}
	}
	bool isNegative = false;
	for (int i = 0; i < V; ++i)
	{
		if (d[i][i] < 0)
		{
			cout << "NEGATIVE CYCLE" << endl;
			return;
		}
	}
	for (int i = 0; i < V; ++i)
	{
		for (int j = 0; j < V; ++j)
		{
			if (j)
			{
				cout << " ";
			}
			if (d[i][j] == INF)
			{
				cout << "INF";
			}
			else
			{
				cout << d[i][j];
			}
		}
		cout << endl;
	}
}

int main()
{
	solve();
	return(0);
}


#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv){
	int **array;
	int n;

	while(1){
		scanf("%d", &n);
		if(n == 0){
			break;
		}

		array = (int **)calloc(n+1, sizeof(int));
		for(int i = 0; i <= n; i++){
			array[i] = (int *)calloc(n+1, sizeof(int));
		}

		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				scanf("%d", &array[i][j]);

				array[i][n] += array[i][j];
			}
		}

		for(int i = 0; i <= n; i++){
			for(int j = 0; j < n; j++){
				array[n][i] += array[j][i];
			}
			array[n][n] += array[n][i];
		}
		array[n][n] /= 4;

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

		for(int i = 0; i <= n; i++){
			free(array[i]);
		}
		free(array);
	}

	return 0;
}

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

int main() {
  int n = 0, tmp0 = 0, tmp1 = 0;
  int tmp2 = 0, tmp3, out1 = 0, out2 = 0;
  int a[100];
  scanf("%d", &n);
  while (n) {
    for (tmp3=0;tmp3<100;tmp3++) a[tmp3] = 0;
    tmp0 = 0, out2 = 0;
    while (tmp0 < n) {
      tmp1 = 0, out1 = 0;
      while (tmp1 < n) {
        scanf("%d", &tmp2);
        a[tmp1] += tmp2;
        printf("%5d", tmp2);
        out1 += tmp2;
        tmp1++;
      }
      printf("%5d\n", out1);
      a[tmp1] += out1;
      tmp0++;
    }
    tmp0 = 0;
    while (tmp0 < n) {
      printf("%5d", a[tmp0]);
      tmp0++;
    }
    printf("%5d\n", a[tmp0]);
    scanf("%d", &n);
  }
  return 0;
}


#include <iostream>
#include <algorithm>
#define flush fflush(stdout)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pl;
const int mod = (int)1e9 + 7, INF = (int)1e9;
const int di[4] = { 1,0,-1,0 }, dj[4] = { 0,1,0,-1 };

int main(void) {
    int l, r, i, j, ans;

    scanf("%d%d", &l, &r);

    if (r - l >= 2019 || l % 2019 > r % 2019)
    {
        printf("0\n");
        return (0);
    }
    ans = 2019;
    i = l % 2019;
    while (i < r % 2019)
    {
        j = i + 1;
        while (j <= r % 2019)
        {
            if (ans > i * j % 2019)
                ans = i * j % 2019;
            j++;
        }
        i++;
    }

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

    return 0;
}

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

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

long long remindersReps[2019];

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

    for(int i = 0; i < 2019; ++i)
    {
        remindersReps[i] = -1;
    }

    int el_lim = L +2020;
    if(R < el_lim)
        el_lim = R;
    for(int i = L; i <= el_lim ; ++i )
    {
        remindersReps[i%2019] = i;
    }

    long long reminders[2019];

    for(int i = 0; i< 2019;++i)
    {
        if(remindersReps[i]!=1)
            reminders[i] = remindersReps[i]%2019;
    }

    long long min = 3000;

    long long respL, respR;

    for(int i = 0; i < 2019; ++i)
    {
        for(int j = 0; j < 2019; ++j)
        {
            if(remindersReps[i] >= remindersReps[j] ||  remindersReps[i] == -1 || remindersReps[j] == -1)
                continue;

            long long temp = (reminders[i]*reminders[j])%2019;
            if( temp < min )
            {
                min = temp;
                respL = remindersReps[i];
                respR = remindersReps[j];
            }
        }
    }

    cout << min;
}


#include<bits/stdc++.h>
#define ll long long
#define re register
#define ull unsigned ll
using namespace std;
inline int read(){
	int s=0,t=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')t=-1;ch=getchar();}
	while(ch>='0'&&ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s*t;
}
const int N=41;
const int M=(1<<17)+5;
const int P=1e9+7;
int n,A,B,C,D,U,tr[M][11],f[N][M];
int Mod(int x){return x>=P?x-P:x;}
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	n=read(),A=read(),B=A+read(),C=B+read(),U=(1<<C);
	for(int S=1;S<=U;S++)
	for(int i=1;i<=10;i++){
		tr[S][i]=1;
		for(int j=0;j<=C;j++)if(S>>j&1){
			if(j<A&&j+i>A)continue;
			if(j<B&&j+i>B)continue;
			if(j+i<=C)tr[S][i]|=1<<(j+i);
		}if((S==U)||(tr[S][i]>=U))tr[S][i]=U;
	}f[0][1]=1;
	for(int i=1;i<=n;i++)
	for(int S=1;S<=U;S++)
	for(int j=1;j<=10;j++)f[i][tr[S][j]]=Mod(f[i][tr[S][j]]+f[i-1][S]);
	printf("%d",f[n][U]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i = (a); i <= (b); ++ i)
#define rrp(i,a,b) for (int i = (a); i >= (b); -- i)
#define gc() getchar()
#define fir first
#define sec second
typedef pair<int,int> pii;
typedef double db;
typedef long long ll;
template <typename tp>
inline void read(tp& x) {
  x = 0; char tmp; bool key = 0;
  for (tmp = gc(); !isdigit(tmp); tmp = gc())
    key = (tmp == '-');
  for (; isdigit(tmp); tmp = gc())
    x = (x << 3) + (x << 1) + (tmp ^ '0');
  if (key) x = -x;
}
template <typename tp>
inline void ckmn(tp& x,tp y) {
  x = x < y ? x : y;
}
template <typename tp>
inline void ckmx(tp& x,tp y) {
  x = x < y ? y : x;
}

const int N = 100010;
int n, s, x[N], m;
ll p[N];
pair<int,ll> solve(int l,int r) {
  if (l > m) return make_pair(x[r], x[r] - s);
  if (r <= m) return make_pair(x[l], s - x[l]);
  if (p[r] <= p[l]) {
    p[l] += p[r];
    pair<int,ll> tmp = solve(l, r-1);
    return make_pair(x[r], tmp.sec + x[r] - tmp.fir);
  } else {
    p[r] += p[l];
    pair<int,ll> tmp = solve(l+1, r);
    return make_pair(x[l], tmp.sec + tmp.fir - x[l]);
  }
}
int main() {
  read(n), read(s);
  rep (i, 1, n)
    read(x[i]), read(p[i]);
  m = 0;
  while (x[m+1] < s && m < n) ++ m;
  cout << solve(1, n).sec << endl;
  return 0;
}

#include <iostream>
#include <algorithm>
#include <utility>
#include <fstream>
#include <iomanip>
#include <complex>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <sstream>
#include <map>
#include <set>
#include <queue>

#define IO ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
typedef long long ll;
typedef long double ld;
const ll INF = 1e18;
const int N = 3e2 + 5, inf = 1e9, mod = 998244353;
using namespace std;

int n, a, freq[4] = {};
ld dp[N][N][N] = {};
bool vis[N][N][N] = {};

ld solve ( int c1 , int c2 , int c3 )
{
    if ( c1 + c2 + c3 == 0 )
        return 0;
    if ( vis[c1][c2][c3] )
        return dp[c1][c2][c3];
    vis[c1][c2][c3] = true;
    ld ret = 1.0, c0 = n - ( c1 + c2 + c3 );
    if ( c1 )
        ret += solve ( c1 - 1 , c2 , c3 ) * ( 1.0 * c1 / n );
    if ( c2 )
        ret += solve ( c1 + 1 , c2 - 1 , c3 ) * ( 1.0 * c2 / n );
    if ( c3 )
        ret += solve ( c1 , c2 + 1 , c3 - 1 ) * ( 1.0 * c3 / n );
    ret /= ( 1 - ( c0 / n ) );
    return dp[c1][c2][c3] = ret;
}

int main()
{
    IO
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        cin >> a;
        freq[a]++;
    }
    cout << fixed << setprecision(10) << solve( freq[1] , freq[2] , freq[3] ) << '\n';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
	int a[3];
	for (int i = 0; i < 3; ++i)
	{
		cin >> a[i];
	}
	sort(a, a + 3);
	cout << a[0] + a[1];
	return 0;
}

#include <iostream>
using namespace std;

int main(){
    string ss;
    int num[128];
    num['M'] = 1000;
    num['D'] = 500;
    num['C'] = 100;
    num['L'] = 50;
    num['X'] = 10;
    num['V'] = 5;
    num['I'] = 1;
    while( cin >> ss ){
        int answer = 0;
        string::iterator it = ss.begin();
        while( it != ss.end() ){
            if( it+1 == ss.end() || num[ *it ] >= num[ *(it+1) ] ){
                answer += num[ *it ];
            }else{
                answer += num[ *(it+1) ] - num[ *it ];
                it++;
            }
            it++;
        }
        cout << answer << endl;
    }
    return 0;
}


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

typedef long long ll;
typedef long double ld;
typedef vector<vector<int>> Graph;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
#define pi 3.14159265359
#define inf 2147483647
#define INF 9223372036854775807
#define mod 1000000007
#define mod2 998244353

int main() {
    int N; cin >> N;
    vector<ll> A(N);
    map<ll, ll> M;
    for(int i = 0; i < N; i++) {
        cin >> A[i];
        if(!M.count(A[i])) M[A[i]] = 1;
        else M[A[i]]++;
    }
    ll Max = 0, preMax = 0;
    for(auto p: M) {
        if(p.second >= 4) {
            if(p.first > Max) {
                preMax = p.first;
                Max = p.first;
            }
            else if(p.first > preMax) {
                preMax = p.first;
            }
        }
        else if(p.second >= 2) {
            if(p.first > Max) {
                preMax = Max;
                Max = p.first;
            }
            else if(p.first > preMax) {
                preMax = p.first;
            }
        }
    }
    ll ans = Max * preMax;
    cout << ans << endl;
    return 0;
}

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define ll long long
#define maxn 1000005
#define inf 1e18
#define pb push_back
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;

inline ll read()
{
	ll x=0,w=1; char c=getchar();
	while(c<'0'||c>'9') {if(c=='-') w=-1; c=getchar();}
	while(c<='9'&&c>='0') {x=(x<<1)+(x<<3)+c-'0'; c=getchar();}
	return w==1?x:-x;
}

ll a[maxn],dp[maxn][5],n,pre[maxn];

int main()
{
	n=read(); rep(i,1,n) a[i]=read();
	rep(i,1,n) rep(j,0,3) dp[i][j]=-inf;
	dp[1][0]=a[1]; dp[2][1]=a[2]; dp[3][2]=a[3];
	for(int i=3;i<=n;i++)
	{
		dp[i][0]=dp[i-2][0]+a[i];
		dp[i][1]=max(dp[i-2][1],dp[i-3][0])+a[i];
		if(i>=4) dp[i][2]=max(max(dp[i-2][2],dp[i-3][1]),dp[i-4][0])+a[i];
	}
	if(n%2==1) cout<<max(max(dp[n][2],dp[n-1][1]),dp[n-2][0])<<endl;
	else cout<<max(dp[n][1],dp[n-1][0])<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000000000000002

int main(){

	int N,M;
	cin>>N>>M;

	vector<map<int,vector<int>>> E(N);

	for(int i=0;i<M;i++){
		int p,q,c;
		scanf("%d %d %d",&p,&q,&c);
		p--;q--;
		E[p][c].push_back(q);
		E[q][c].push_back(p);
	}

	map<pair<int,int>,int> dis;
	dis[make_pair(0,0)]=0;
	deque<pair<int,int>> Q;
	Q.push_back(make_pair(0,0));
	set<pair<int,int>> visited;
	while(Q.size()!=0){
		int x = Q.back().first;
		int u = Q.back().second;
		Q.pop_back();
		if(visited.count(make_pair(x,u)))continue;
		if(x==0){
			for(auto a:E[u]){
				int v = a.first;
				if(dis.count(make_pair(v,u)))continue;
				dis[make_pair(v,u)] = dis[make_pair(x,u)]+1;
				Q.emplace_front(v,u);
			}
		}
		else{
			for(int i=0;i<E[u][x].size();i++){
				int v = E[u][x][i];
				if(dis.count(make_pair(x,v))&&dis[make_pair(x,v)]<=dis[make_pair(x,u)])continue;
				dis[make_pair(x,v)]=dis[make_pair(x,u)];
				Q.emplace_back(x,v);
			}
			int v = u;
			if(dis.count(make_pair(0,v))&&dis[make_pair(0,v)]<=dis[make_pair(x,u)])continue;
			dis[make_pair(0,v)]=dis[make_pair(x,u)];
			Q.emplace_back(0,v);
		}
	}

	pair<int,int> ans(0,N-1);
	if(dis.count(ans))cout<<dis[ans]<<endl;
	else cout<<-1<<endl;

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define f1(a,b,c) for(int c=a;c<=b;c++)
#define f2(a,b,c) for(int c=a;c>=b;c--)
#define f3(a,b,c) for(int c=a;c;c=b)
#define so1(a,n) sort(a+1,a+n+1,mycmp);
#define so2(a,n) sort(a+1,a+n+1);
#define ll long long
#define itn int
#define ubt int
#define pii pair<int,int>
#define mp make_pair
const int twx=1e6+100;
const int inf=0x7fffffff;
ll read()
{
    ll sum=0;
    ll flag=1;
    char c=getchar();
    while(c<'0'||c>'9')
    {
        if(c=='-')
        {
            flag=-1;
        }
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        sum=((sum*10)+c-'0');
        c=getchar();
    }
    return sum*flag;
}
int n,m;
struct LV
{
    int Next;
    int y;
    int v;
}a[twx<<1];
int Link[twx];
int len;
map<pii,int> p;
int cnt=0;
bool v[twx];
int d[twx];
void Insert(int x,int y,int z)
{
    a[++len].y=y;
    a[len].Next=Link[x];
    a[len].v=z;
    Link[x]=len;
}
int get(int x,int y)
{
    if(p.find(mp(x,y))!=p.end())
    {
        return p[mp(x,y)];
    }
    else
    {
        return p[mp(x,y)]=++cnt;
    }
}
void spfa()
{
    memset(d,0x3f,sizeof d);
    queue<int> q;
    q.push(1);
    v[1]=1;
    d[1]=0;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        f3(Link[x],a[i].Next,i)
        {
            int y=a[i].y;
            int vs=a[i].v;
            if(d[y]>d[x]+vs)
            {
                d[y]=d[x]+vs;
                if(!v[y])
                {
                    v[y]=1;
                    q.push(y);
                }
            }
        }
        v[x]=0;
    }
}
void init()
{
	n=read();
    m=read();
    cnt=n;
    f1(1,m,i)
    {
        int x=read();
        int y=read();
        int z=read();
        int xz=get(x,z);
        int yz=get(y,z);
        Insert(xz,yz,0);
        Insert(yz,xz,0);
        Insert(x,xz,1);
        Insert(xz,x,1);
        Insert(y,yz,1);
        Insert(yz,y,1);
    }
    spfa();
    printf("%d\n",d[n]==0x3f3f3f3f?-1:d[n]/2);
}
void work()
{

}
void print()
{

}
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
    init();
	return 0;
}


#include<iostream>
using namespace std;

 int InsertionSort(int* a, int n, int g)
{
	int tmp, j, count = 0;
	for (int i = g; i < n; i++)
	{
		tmp = a[i];
		for (j = i - g; j >= 0 && a[j] > tmp; j -= g)
		{
			a[j + g] = a[j];
			count++;
		}
		a[j + g] = tmp;
	}
	return count;
}

int ShellSort(int* a, int n)
{
	int m, g[100],count = 0;
	for (m = 1; m * m * m <= n; m++)
		continue;
	m--;
	cout << m << endl;
	for (int i = 0; i < m; i++)
	{
	    g[i] = (m - i) * (m - i);
		if (i)
			cout << " ";
		cout << g[i];
	}
	for (int i = 0; i < m; i++)
		count += InsertionSort(a, n, g[i]);

	return count;
}

int main()
{
	int n, a[1000000], count;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	count = ShellSort(a, n);
	cout << endl << count << endl;
	for (int i = 0; i < n; i++)
		cout << a[i] << endl;

	return 0;
}

#include <iostream>
#include <utility>
#include <cstdio>
using namespace std;

void insertionSort(int A[], const int n, const int g, int *cnt)
{
    for( int i = g; i < n; ++i ){
        int tmp = A[i];
        int j = i - g;
        while( -1 < j && tmp < A[j] ){
            //cout << A[j+g] << "<-->" << A[j] << endl;
            A[j+g] = A[j];
            j = j - g;
            ++*cnt;
        }
        A[j+g] = tmp;
    }
}

void shellSort(int A[], const int n)
{
    int cnt = 0;
    int m;
    int G[100];
    for( int i = 0; i < 100; ++i ){
        if( i == 0 ) G[i] = n / 2 + 1;
        else G[i] = G[i-1] / 2 - 1;
        if( G[i] < 0 ){
            G[i-1] = 1;
            G[i] = 0;
            m = i;
            break;
        }
    }

    for( int i = 0; i < m; ++i )
        insertionSort(A, n, G[i], &cnt);

    //出力
    printf("%d\n", m);
    for( int i = 0; i < m; ++i ){
        if( i ) printf(" ");
        printf("%d", G[i]);
    }
    printf("\n%d\n", cnt);
    for( int i = 0; i < n; ++i )
        printf("%d\n", A[i]);
}

int main()
{
    //入力
    int n;
    scanf("%d", &n);
    int A[n];
    for( int i = 0; i < n; ++i )
        scanf("%d", &A[i]);

    //シェルソート
    shellSort(A, n);

    return 0;
}


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

int main() {
	string s;cin>>s;
	auto pos = s.find("AC");
	if(pos!=string::npos){
		cout << "Yes" << endl;
	} else {
		cout << "No" << endl;
	}
	return 0;
}


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

int main()
{
    string s;
    cin >> s;
    if(s.size() == 2) cout << s << endl;
    else {
        reverse(s.begin(), s.end());
        cout << s << endl;
    }
    return 0;
}

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

#define NDEBUG
#ifdef DEBUG
#include "../cout11.h"
#undef NDEBUG
#endif
#include <cassert>

typedef long long ll;
typedef long double Double;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<ll,ll> llll;
typedef pair<double,double> dd;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<long double> vD;

#define sz(a)  int((a).size())
#define pb  push_back
#define FOR(var,from,to) for(int var=(from);var<=(to);++var)
#define rep(var,n)  for(int var=0;var<(n);++var)
#define rep1(var,n)  for(int var=1;var<=(n);++var)
#define repC2(vari,varj,n)  for(int vari=0;vari<(n)-1;++vari)for(int varj=vari+1;varj<(n);++varj)
#define ALL(c)  (c).begin(),(c).end()
#define RALL(c)  (c).rbegin(),(c).rend()
#define tr(i,c)  for(auto i=(c).begin(); i!=(c).end(); ++i)
#define found(s,e)  ((s).find(e)!=(s).end())
#define mset(arr,val)  memset(arr,val,sizeof(arr))
#define mid(x,y) ((x)+((y)-(x))/2)
#define IN(x,a,b) ((a)<=(x)&&(x)<=(b))

ll gcd(ll a, ll b) { while(a) swap(a, b%=a); return b; }

const ll MOD=1000000007LL;

ll ADD(ll x, ll y) { return (x+y) % MOD; }
ll SUB(ll x, ll y) { return (x-y+MOD) % MOD; }
ll MUL(ll x, ll y) { return x*y % MOD; }
ll POW(ll x, ll e) { ll v=1; for(; e; x=MUL(x,x), e>>=1) if (e&1) v = MUL(v,x); return v; }
ll DIV(ll x, ll y) { /*assert(y%MOD!=0);*/ return MUL(x, POW(y, MOD-2)); }
// ll comb(ll n, ll k) { ll v=1; for(ll i=1; i<=k; i++) v = DIV(MUL(v, n-i+1),i); return v; }
//

void solve(ll K) {
    int N = 50;
    vector<ll> a(N, N-1);
    ll q = K / N, r = K % N;
    rep(i,N) a[i] += q;
    rep(i,N){
        if (i < r) a[i] += (N - (r-1));
        else a[i] -= r;
    }

    cout << N << endl;
    rep(i,N) {
        cout << a[i];
        if (i < N-1) cout << " ";
    }
    cout << endl;
}

int main() {
    ll K; cin >> K;
    solve(K);
    return 0;
}


#include <stdio.h>

int main(){
    int h,w,n;
    scanf("%d", &h);
	scanf("%d", &w);
	scanf("%d", &n);

    int ans = 0, count = 0;

    while (n > ans){
        if(w >= h){
            ans += w;
            count++;
        }else if(h>w){
            ans += h;
            count++;
        }
    }
    printf("%d\n", count);
    return 0;
}

#include<stdio.h>
#include<iostream>
#include<vector>
#include<math.h>
#include<queue>
#include<map>
#include<algorithm>
#include<string.h>
#include<functional>
#include<limits.h>
#include<stdlib.h>
#include<string>
#include<unordered_map>
#include<set>
using namespace std;

#define intmax INT_MAX
#define lmax LONG_MAX
#define uintmax UINT_MAX
#define ulmax ULONG_MAX
#define llmax LLONG_MAX
#define ll long long
#define rep(i,a,N) for((i)=(a);(i)<(N);(i)++)
#define rrp(i,N,a) for((i)=(N)-1;(i)>=(a);(i)--)
#define llfor ll i,j,k
#define sc(a) cin>>a
#define pr(a) cout<<a<<endl
#define pY puts("YES")
#define pN puts("NO")
#define py puts("Yes")
#define pn puts("No")
#define pnn printf("\n")
#define sort(a) sort(a.begin(),a.end())
#define push(a,b) (a).push_back(b)
#define llvec vector<vector<ll>>
#define charvec vector<vector<char>>
#define sizeoof(a,b) (a,vector<ll>(b))
#define llpvec vector<pair<ll,ll>>
/*繰り上げ除算*/ll cei(ll x,ll y){ll ans=x/y;if(x%y!=0)ans++;return ans;}
/*最大公約数*/ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
/*最小公倍数*/ll lcm(ll x,ll y){return x/gcd(x,y)*y;}
/*n乗*/ll llpow(ll x,ll n){ll i,ans=1;rep(i,0,n)ans*=x;return ans;}
/*階乗*/ll fact(ll x){ll i,ans=1;rep(i,0,x)ans*=(x-i);return ans;}
/*nCr*/ll ncr(ll n,ll r){return fact(n)/fact(r)/fact(n-r);}
/*nPr*/ll npr(ll n,ll r){return fact(n)/fact(n-r);}
/*primejudge*/bool prime(ll a){if(a<=1)return false;ll i;for(i=2;i*i<=a;i++){if(a%i==0)return false;}return true;}
llfor;///////////////////////////////////////////////////////////
int main(){
 ll h,w,n;
 sc(h);sc(w);sc(n);
 ll a=max(h,w);
 a=cei(n,a);
 pr(a);
return 0;}


#include <bits/stdc++.h>
typedef  long double ll;
#define fr(i,a,b) for(long long i=a; i<b; i++)
#define rf(i,a,b) for(ll i=a; i>=b; i--)
typedef std::vector<long long > vi ;
#define F first
#define S second
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod 1000000007
#define PB push_back
#define MP make_pair
#define PI 3.14159265358979323846
#define all(a) a.begin(),a.end()
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))
#define endl '\n'
#define show(a) for(auto el:a)cout<<el<<" "
const ll INF=LLONG_MAX/2;
const ll N=2e5+1;
#define yesno(f) yes(f);no(f);
#define yes(f) if(f)cout<<"YES\n";
#define no(f) if(!f)cout<<"NO\n";
using  namespace std;
int main()
{
    fast;
   ll t;
   t=1;
   while(t--)
   {
       long long n,x=0,k;
       std::cin >> n>>k;
       ll a[n],maxi=0,sum;
       fr(i,0,n)
       std::cin >> a[i];
       fr(i,0,k)
       maxi+=((a[i]+1)/2);
       sum=maxi;
       fr(i,k,n)
       {
           sum=sum-((a[i-k]+1)/2)+(a[i]+1)/2;
           maxi=max(maxi,sum);
       }
       cout<<fixed;
       cout<<setprecision(12);
       cout<<maxi<<"\n";

   }
}

#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

int main() {
    int W[10], K[10];
    for (int i = 0; i < 10; i++) {
        cin >> W[i];
    }
    for (int i = 0; i < 10; i++) {
        cin >> K[i];
    }
    sort(W, W+10, greater<int>());
    sort(K, K+10, greater<int>());
    cout << W[0] + W[1] + W[2] << ' ';
    cout << K[0] + K[1] + K[2] << endl;
    return 0;
}


#include <iostream>
#include <vector>

int main()
{
    int n;
    std::cin >> n;
    std::vector<std::vector<int>> memo(60, std::vector<int> (25));
    for (int i = 0; i < n; ++i)
    {
        long double a;
        std::cin >> a;
        long long b = (long long) (a * 1e9 + 0.5);
        int count2 = 0, count5 = 0;
        while (b % 2 == 0)
        {
            ++count2;
            b /= 2;
        }
        while (b % 5 == 0)
        {
            ++count5;
            b /= 5;
        }
        ++memo[count2][count5];
    }
    long long ans = 0;
    for (int i = 0; i < 60; ++i)
    {
        for (int j = 0; j < 25; ++j)
        {
            for (int ii = std::max(0, 18 - i); ii < 60; ++ii)
            {
                for (int jj = std::max(0, 18 - j); jj < 25; ++jj)
                {
                    if (i != ii || j != jj)
                    {
                        ans += 1LL * memo[i][j] * memo[ii][jj];
                    }
                }
            }
        }
    }
    ans /= 2;
    for (int i = 0; i < 60; ++i)
    {
        for (int j = 0; j < 25; ++j)
        {
            if (i + i >= 18 && j + j >= 18)
            {
                ans += 1LL * memo[i][j] * (memo[i][j] - 1) / 2;
            }
        }
    }
    std::cout << ans << '\n';
    return 0;
}

#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define repdi(i,a,b) for(ll i=(a)-1;i>=(b);--i)
#define repd(i,a) repdi(i,a,0)
#define itr(it,a) for( auto it = (a).begin(); it != (a).end(); ++it )
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define endl '\n'
#define debug(x) std::cerr << #x << " = " << (x) << endl;

using ll = long long;
using P = std::pair<ll, ll>;

constexpr ll INF = 1ll<<60;

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

template<class S, class T>
std::ostream& operator<< ( std::ostream& out, const std::pair<S,T>& a )
{ std::cout << '(' << a.first << ", " << a.second << ')'; return out; }

template<class T>
std::ostream &operator<< ( std::ostream& out, const std::vector<T>& a )
{ std::cout << '['; rep( i, a.size() ){ std::cout << a[i]; if( i != a.size()-1 ) std::cout << ", "; } std::cout << ']'; return out; }

template<typename T>
class BinaryIndexedTree2D {
    int H;
    int W;
    std::vector<std::vector<T> > data2d;
public:
    BinaryIndexedTree2D(int H, int W) : H(H), W(W) {
        data2d.resize(H + 1, std::vector<T>(W + 1, 0));
    }

    T sum(int h, int w) {
        T res = 0;
        for (int i = h; i > 0; i -= i & -i) {
            for (int j = w; j > 0; j -= j & -j) {
                res += data2d[i][j];
            }
        }
        return res;
    }

    /**
     * 左上の座標(h1, w1), 右下の座標(h2, w2) に含まれる値の累積和
     */
    T sum(int h1, int w1, int h2, int w2) {
        return sum(h2, w2) - sum(h1 - 1, w2) - sum(h2, w1 - 1) + sum(h1 - 1, w1 - 1);
    }

    void add(int h, int w, ll x) {
        for (int i = h; i <= H; i += i & -i) {
            for (int j = w; j <= W; j += j & -j) {
                data2d[i][j] += x;
            }
        }
    }
};

ll N;
BinaryIndexedTree2D<ll> bit( 100, 100 );
constexpr ll base = 20;

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

  ll ans = 0;

  rep( i, N ) {
    std::string A;
    std::cin >> A;

    bool fst = true;
    ll p = A.size();
    ll ten = 1;
    ll s = 0, num = 0;

    if( A.find('.') != std::string::npos ) repd( i, A.size() ) {
      if( A[i] != '0' ) {
        if( fst ) {
          p = i;
        }

        fst = false;
      }

      if( !fst && A[i] != '.' ) {
        num += (A[i]-'0')*ten;
        ten *= 10;
      }

      if( A[i] == '.' ) {
        s = p-i;
      }
    } else {
      rep( i, A.size() ) {
        num *= 10;
        num += A[i]-'0';
      }

      s = 0;
    }

    //std::cout << P(num, s) << endl;

    ll two = 0, five = 0;

    while( num % 2 == 0 ) {
      num /= 2;
      ++two;
    }

    while( num % 5 == 0 ) {
      num /= 5;
      ++five;
    }

    //std::cout << P(two-s, five-s) << endl;

    ans += bit.sum( -(two-s)+base, -(five-s)+base, 98, 98 );
    //std::cout << "sum: " << P(-(two-s)+base, -(five-s)+base) << endl;

    bit.add( two-s+base, five-s+base, 1 );
    //std::cout << "added: " << P(two-s+base, five-s+base) << endl;
  }

  std::cout << ans << endl;

  return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <fstream>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;

int main() {
    ios::sync_with_stdio(false);
    int n;
    ll a[300005] = {}, z = 0;
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < 60; i++) {
        ll b = 0;
        for (int j = 0; j < n; j++) if (a[j] & (1ll << i)) b++;
        z = (z + (1ll << i) % mod * b % mod * (n - b)) % mod;
    }
    cout << z;
}

#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>;
const int INF = 1001001001;
const ll LINF = 1001001001001001;
const int MOD = 1000000007;

template <typename T>
void print(const T &v);

int main()
{
    int n;
    cin >> n;
    vector<int> cs(60, 0); // 立っているビットの個数を記録する
    for (int i = 0; i < n; i++)
    {
        ll a;
        cin >> a;
        bitset<60> bs(a);
        for (int j = 0; j < 60; j++)
        {
            if (bs.test(j))
            {
                ++cs[j];
            }
        }
    }
    ll ans = 0;
    for (int i = 0; i < 60; i++)
    {
        ll tmp = pow(2, i);
        tmp %= MOD;
        tmp *= cs[i];
        tmp %= MOD;
        tmp *= (n - cs[i]);
        tmp %= MOD;
        ans += tmp;
        ans %= MOD;
    }
    cout << ans << endl;
    return 0;
}

// Use For Debug
template <typename T>
void print(T const &v)
{
    for (int i = 0; i < v.size(); i++)
    {
        if (i)
            cout << ' ';
        cout << v[i];
    }
    cout << endl;
};


#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <bitset>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(),v.end()
template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; }
template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; }
#define DEBUG_VLL(vec) REP(sz, vec.size()) std::cerr<<vec[sz]<<(sz==vec.size()-1?'\n':' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

int t;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> t;
    while (t-->0) {
        int n; cin >> n;
        vll a(n);
        for (int i = 0; i < n; i++) cin >> a[i];
        string s; cin >> s;
        vll basis;
        int ans = -1;
        for (int i = n - 1; i >= 0; i--) {
            for (ll base: basis) chmin(a[i], a[i] ^ base);

            // もし a[i] があとで表される（a[i]==0）なら
            // どちらにせよ操作の必要なし？（打ち消せるので）
            // a[i] があとで表せられない（a[i]!=0）なら
            // 1 なら勝ち（0 に出来ないので）
            // 0 なら基底に加えてもっと前（<i）を見る
            if (a[i] != 0) {
                if (s[i] == '1') {
                    ans = 1;
                    break;
                } else basis.emplace_back(a[i]);
            }
        }
        if (ans == -1) ans = 0;
        cout << ans << '\n';
    }
    return 0;
}

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=205;
ll a[maxn],p[maxn];
char s[maxn];
int n,m;

void solve(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		for(int i=1;i<=n;i++)	scanf("%lld",&a[i]);
		scanf("%s",s+1);
		if(s[n]=='1')	printf("1\n");
		else{
			int flag=0;
			memset(p,0,sizeof(p));
			for(int i=n;i>=1;i--){
				if(s[i]=='0'){
					ll tmp=a[i];
					for(int j=62;j>=0;j--){
						if((1ll<<j)&tmp){
							if(!p[j]){
								p[j]=tmp;break;
							}
							tmp^=p[j];
						}
					}
				}
				else if(s[i]=='1'){
					ll tmp=a[i];
					for(int j=62;j>=0;j--){
						if((1ll<<j)&tmp){
							if(!p[j])	break;
							tmp^=p[j];
						}
					}
					if(tmp){
						flag=1;break;
					}
				}
			}
			printf("%d\n",flag);
		}
	}
}
int main(){
	solve();
	return 0;
}


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

using namespace std;

#define debug(x) (cout << # x ": " << x << endl)
#define ll long long int
#define repeat(n) for (int i = 0; i < n; ++i)
#define all(x) x.begin(), x.end()

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

    vector<pair<int, int>> s1, s2;

    repeat(n) {
        string s;
        cin >> s;

        for(auto it = s.find("()"); it != string::npos; it = s.find("()")) {
            s.erase(it, 2);
        }

        int lc = count(all(s), ')');
        int rc = count(all(s), '(');

        if (rc - lc >= 0) {
            s1.push_back(make_pair(lc, rc));
        } else {
            s2.push_back(make_pair(lc, rc));
        }
    }

    sort(all(s1), [](const auto& lhs, const auto& rhs){
        return lhs.first < rhs.first;
    });
    sort(all(s2), [](const auto& lhs, const auto& rhs){
        return lhs.second > rhs.second;
    });

    int le = 0, re = 0;
    for (auto& s: s1) {
        le = le + s.first - min(s.first, re);
        re = re - min(s.first, re) + s.second;
    }
    for (auto& s: s2) {
        le = le + s.first - min(s.first, re);
        re = re - min(s.first, re) + s.second;
    }

    if (le == 0 && re == 0) {
        cout << "Yes";
    } else {
        cout << "No";
    }

    return 0;
}

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

typedef long long ll;

int main()
{
    int N;
    cin >> N;
    vector<int> L;
    for (int i = 0; i < 2 * N; ++i)
    {
        int temp;
        cin >> temp;
        L.push_back(temp);
    }
    int ans = 0;
    sort(L.begin(), L.end());

    for (int i = 0; i < 2 * N; ++i)
    {
        ans += min(L[i], L[i + 1]);
        ++i;
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
#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 per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;}
template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ~ ";
	dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {";  \
	for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif
struct UnionFind{
	vector<int> par;
	UnionFind(int N){
		par.assign(N,0);
		rep(i,N) par[i]=i;
	}
	int find(int x){
		if(par[x]==x) return x;
		return par[x]=find(par[x]);
	}
	bool same(int x,int y){
		return find(x)==find(y);
	}
	void unite(int x,int y){
		x=find(x),y=find(y);
		if(x==y) return;
		par[y]=x;
	}
};
int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);		//DON'T USE scanf/printf/puts !!
	cout << fixed << setprecision(20);

	int N; cin >> N;
	V<int> x(N),y(N);
	rep(i,N) cin >> x[i] >> y[i];
	VV<ll> es;
	rep(_,2){
		V<int> idx(N); iota(all(idx),0);
		sort(all(idx),[&](int l,int r){return x[l]<x[r];});
		rep(i,N-1){
			int a = idx[i], b = idx[i+1];
			es.push_back({x[b]-x[a],a,b});
		}
		rep(i,N) swap(x[i],y[i]);
	}
	sort(all(es));
	UnionFind UF(N);
	ll ans=0;
	for(auto e: es){
		if(!UF.same(e[1],e[2])){
			UF.unite(e[1],e[2]);
			ans += e[0];
		}
	}
	cout << ans << endl;
}

#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef struct{
	int x,y,id;
} ii;
typedef tuple<int,ii,ii> t;
int dist(ii a,ii b){
	return min(abs(a.x-b.x),abs(a.y-b.y));
}
vector<int> up;
int fp(int u){
	if(up[u]<0) return u;
	else return up[u] = fp(up[u]);
}
void unite(int u,int v){
	int x = fp(u);
	int y = fp(v);
	if(x!=y) up[x]=y;
}
signed main() {
	int n;
	cin>>n;
	up.resize(n,-1);
	vector<ii> p(n);
	int i=0;
	for(auto &v:p)
	{
		v.id=i++;
		cin>>v.x>>v.y;
	}
	auto cmp= [](t a,t b){
		return get<0>(a)>get<0>(b);
	};
	priority_queue<t,vector<t>,decltype (cmp)> pq(cmp);
	sort(p.begin(),p.end(),[](ii lhs,ii rhs){return lhs.x<rhs.x;});
	for(auto it = p.begin();it!=prev(p.end());it++){
		int d = dist(*it,*next(it));
		pq.push(make_tuple(d,*it,*next(it)));
	}
	sort(p.begin(),p.end(),[](ii lhs,ii rhs){return lhs.y<rhs.y;});
		for(auto it = p.begin();it!=prev(p.end());it++){
			int d = dist(*it,*next(it));
			pq.push(make_tuple(d,*it,*next(it)));
		}
	int cnt = n;
	int res = 0;
	while(!pq.empty()){
		int d;
		ii a,b;
		tie(d,a,b) = pq.top();
		pq.pop();
		if(fp(a.id)!=fp(b.id)){
			n--;
			res+=d;
			unite(fp(a.id), fp(b.id));
		}
	}
	cout<<res<<endl;
}


#include <iostream>
using namespace std;

int main()
{
	int n, a, b, count = 0, pointX = 0, pointY = 0;
	cin >> n;
	do {
		cin >> a >> b;
		if (a > b) {
			pointX += (a + b);
		}
		else if (a < b) {
			pointY += (a + b);
		}
		else {
			pointX += a;
			pointY += b;
		}
		count++;
		if (count == n) {
			cout << pointX << " " << pointY << endl;
			pointX = pointY = count = 0;
			cin >> n;
		}
	} while (n != 0);
	return 0;
}

#include <iostream>
using namespace	std;

int main ()
{
    int	N;
    while (cin >> N, N) {
	int n1, n2;
        int a =	0, b = 0;
        for (int i = 0; i < N; i++) {
            cin >> n1 >> n2;
            if (n1 > n2) a += n1 + n2;
            else if (n1 < n2) b	+= n1 +	n2;
            else {
                a += n1;
                b += n2;
            }
        }
        cout <<	a << " " << b << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
const int MOD=1e9+7;
const long double Pi=acos(-1);
using  P=pair<ll,ll>;
ll gcd(ll a,ll b){//aとbの最大公約数を返す
  if(a%b==0){return b;}
  else{return gcd(b,a%b);}
}
ll lcm(ll a,ll b){//aとbの最小公倍数を返す
  return a*b/gcd(a,b);
}
ll pow(ll x,ll y){//x^yを返す
  ll tmp=x,res=1;
  while(y){
    if(y%2){res=res*tmp%MOD;}
    y>>=1;
    tmp=tmp*tmp%MOD;
  }
  return res;
}
ll nature(ll a){//絶対値を返す
  if(a>=0){return(a);}
  else{return(-1*a);}
}
ll prime(ll a){//素数なら1を返す
  if(a==2){return 1;}
  else if(a%2==0 || a==1){return 0;}
  for(ll i=3;i*i<=a;i+=2){if(a%i==0){return 0;}}
  return 1;
}
int main(){
  string S;
  cin>>S;
  if(S=="zyxwvutsrqponmlkjihgfedcba"){cout<<-1<<endl;}
  else if(S.size()==26){
    ll maxcharacter=S.at(25);
    ll rock;
    for(ll i=0;i<25;i++){
      if(S.at(24-i)<maxcharacter){
        for(ll j=0;j<24-i;j++){cout<<S.at(j);}
        rock=24-i;
        unordered_map<ll,ll> mp;
        for(ll i=0;i<rock;i++){
          mp[S.at(i)]++;
        }
        char start=S.at(rock)+1;
        while(mp[start]>0){start++;}
        cout<<start<<endl;
        return 0;
      }
      else{maxcharacter=S.at(24-i);}
    }
  }
  else{
    unordered_map<ll,ll> mp;
    for(ll i=0;i<(ll)S.size();i++){
      mp[S.at(i)]++;
    }
    cout<<S;
    if(mp['a']==0){cout<<"a"<<endl;return 0;}
    if(mp['b']==0){cout<<"b"<<endl;return 0;}
    if(mp['c']==0){cout<<"c"<<endl;return 0;}
    if(mp['d']==0){cout<<"d"<<endl;return 0;}
    if(mp['e']==0){cout<<"e"<<endl;return 0;}
    if(mp['f']==0){cout<<"f"<<endl;return 0;}
    if(mp['g']==0){cout<<"g"<<endl;return 0;}
    if(mp['h']==0){cout<<"h"<<endl;return 0;}
    if(mp['i']==0){cout<<"i"<<endl;return 0;}
    if(mp['j']==0){cout<<"j"<<endl;return 0;}
    if(mp['k']==0){cout<<"k"<<endl;return 0;}
    if(mp['l']==0){cout<<"l"<<endl;return 0;}
    if(mp['m']==0){cout<<"m"<<endl;return 0;}
    if(mp['n']==0){cout<<"n"<<endl;return 0;}
    if(mp['o']==0){cout<<"o"<<endl;return 0;}
    if(mp['p']==0){cout<<"p"<<endl;return 0;}
    if(mp['q']==0){cout<<"q"<<endl;return 0;}
    if(mp['r']==0){cout<<"r"<<endl;return 0;}
    if(mp['s']==0){cout<<"s"<<endl;return 0;}
    if(mp['t']==0){cout<<"u"<<endl;return 0;}
    if(mp['u']==0){cout<<"u"<<endl;return 0;}
    if(mp['v']==0){cout<<"v"<<endl;return 0;}
    if(mp['w']==0){cout<<"w"<<endl;return 0;}
    if(mp['x']==0){cout<<"x"<<endl;return 0;}
    if(mp['y']==0){cout<<"y"<<endl;return 0;}
    if(mp['z']==0){cout<<"z"<<endl;return 0;}
  }
}

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

int main() {
  string s;
  int alp[27];
  for (int i = 0; i < 26; i++) {
    alp[i] = 0;
  }
  alp[26] = 1;

  cin >> s;
  if (s == "zyxwvutsrqponmlkjihgfedcba") {
    puts("-1");
  } else {
    string ans = "";
    for (int i = 0; i < (int)s.size(); i++) {
      alp[s[i] - 'a']++;
    }

    if (s.size() < 26) {
      for (int i = 0; i < 26; i++) {
        if (alp[i] == 0) {
          ans = s + (char)('a' + i);
          break;
        }
      }
    } else {
      string s_next = s;
      next_permutation(s_next.begin(), s_next.end());
      for (int i = 0; i < (int)s.size(); i++) {
        ans += s_next[i];
        if (s[i] != s_next[i]) break;
      }
    }
    cout << ans << endl;
  }

  return 0;
}


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

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

    ll N, M;
    cin >> N >> M;
    vector<ll> A(N), sum(N + 1, 0);
    rep(i, N) { cin >> A[i]; }

    map<ll, ll> cnt;
    ll ans = 0;
    rep(i, N) { sum[i + 1] = sum[i] + A[i]; }

    rep(i, N + 1) { cnt[sum[i] % M]++; }

    for (auto c : cnt) {
        ans += (c.second * (c.second - 1)) / 2;
    }

    cout << ans << endl;
}

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

typedef long long LL;

const int N=200000;

int n,m,a[N+9];
int cnt[N+9];

bool Get_a(){
  for (int i=1;i<=n;++i) cnt[i]=0;
  for (int i=1;i<=n;++i){
	int l=max(i-a[i],1),r=min(i+a[i],n);
	++cnt[l];--cnt[r+1];
  }
  for (int i=1;i<=n;++i) a[i]=cnt[i]+=cnt[i-1];
  for (int i=1;i<=n;++i)
	if (a[i]<n) return 0;
  return 1;
}

int main(){
  scanf("%d%d",&n,&m);
  for (int i=1;i<=n;++i)
	scanf("%d",&a[i]);
  for (int i=1;i<=m;++i)
	if (Get_a()) break;
  for (int i=1;i<=n;++i)
	printf("%d ",a[i]);
  puts("");
  return 0;
}


#include <bits/stdc++.h>
#define rep(i,a,b) for(ll i=ll(a);i<ll(b);i++)
#define irep(i,a,b) for(ll i=ll(a);i>=ll(b);i--)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
using ll=long long;
using ld=long double;
const ll INF = 1LL<<60;
const ll mod=1e9+7;
using namespace std;
ll GCD(ll a, ll b) { return b ? GCD(b, a%b) : a; }

int main(){
    ld n,m,d,x=2;
    cin>>n>>m>>d;
    if(d==0)x=1; // n/n^2
    //ans=2*(n-2*d) d+1~n-d[i+d,i-d]   2通り
    //ans+=2*d      1~d[i+d] n-d~n[i-d] 1通り
    cout<<fixed<<setprecision(12)<<(m-1)*x*(n-d)/(n*n);;
}

//#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <tuple>
#include <string>
#include <algorithm>
#include <functional>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>

using namespace std;
using ll=long long;
using intpair=pair<int,int>;
using intpv=vector<intpair>;
using llpair=pair<ll,ll>;
using llpv=vector<llpair>;
using intvec=vector<int>;
using llvec=vector<ll>;
using intq=queue<int>;
using llq=queue<ll>;
using intmat=vector<intvec>;
using llmat=vector<llvec>;
using pairmat=vector<llpv>;

template<typename T>
using matrix=vector<vector<T>>;
template<typename T>
using pque=priority_queue<T>;
template<typename T>
using lpque=priority_queue<T,vector<T>,greater<T>>;

#define PI 3.141592653589793
#define INTINF 1<<30
#define LLINF 1LL<<60
#define MPRIME 1000000007
#define MPRIME9 998244353

#define pushb push_back
#define fi first
#define se second

#define setpr fixed<<setprecision(15)
#define all(name) name.begin(),name.end()
#define rall(name) name.rbegin(),name.rend()
#define gsort(vbeg,vend) sort(vbeg,vend,greater<>())
#define init(v,size) v.resize(size); for(auto &a__: v) cin>>a__
#define matin(mat,H,W) for(int i__=0; i__<H; i__++)for(int j__=0; j__<W; j__++) cin>>mat[i__][j__]
#define out(n) cout<<n<<endl

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;
}

template<class T> inline void outmat(T& a, int y, int x) {
	for(int i=0; i<y; i++) {
		for(int j=0; j<x; j++) cout<<a[i][j]<<" ";
		cout<<endl;
	}
}

ll GCD(ll a, ll b) { //最大公約数
	if(a==0||b==0) return 0;
	if(a<b) swap(a,b);
	ll tmp = a%b;
	while(tmp!=0) {
		a = b;
		b = tmp;
		tmp = a%b;
	}
	return b;
}

ll binpow(ll a, ll ex, ll p) { //繰り返し二乗法
	ll result=1;
	while(ex>0) {
		if(ex&1) result=result*a%p;
		ex>>=1;
		a=a*a%p;
	}
	return result;
}

ll Fact(ll x, ll p) { //階乗
	ll f=1;
	for(ll i=2; i<=x; i++) {
		f*=i;
		f%=p;
	}
	return f;
}

ll nPr(ll n, ll r) {
	if(n<r) return 0;

	ll result=1LL;
	for(ll i=0; i<r; i++) result*=n-i;
	return result;
}

ll nPrP(ll n, ll r, ll p) { // mod pにおけるnPr
	if(n<r) return 0;

	ll result=1LL;
	for(int i=0; i<r; i++) {
		result*=n-i;
		result%=p;
	}
	return result;
}

ll nCr(ll n, ll r) {
	if (n == r) { return 1; }
	if (r > n) { return 0; }

	if (r > n / 2) { r = n - r; }

	if (n == 0) { return 0; }
	if (r == 0) { return 1; }
	if (r == 1) { return n; }

	double result = 1;
	for (double i = 1; i <= r; i++) {
		result *= (n - i + 1) / i;
	}

	return (ll)result;
}

int DigitNum(ll n) { //桁数
	int digit=0;
	ll div=1LL;
	while(n/div) {
		digit++;
		div*=10;
	}
	return digit;
}

bool Palind(string s) { //回文判定
	return s == string(s.rbegin(), s.rend());
}

double n,m,d;

void input() {
	cin>>n>>m>>d;
}

void solve() {
	double D;
	if(d) D=2.*(n-d)/n/n;
	else  D=1/n;

	cout<<setpr<<(m-1)*D<<endl;
}

int main() {
	while(1) {
		input();
		solve();
		break;
	}
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repn(i,n) for(int i = 0; i <= (int)(n); i++)
#define srep(i,l,n) for(int i = l; i < (int)(n); i++)
#define srepn(i,l,n) for(int i = l; i <= (int)(n); i++)
#define drep(i,n) for(int i = (int)(n-1); i >= 0; i--)
#define drepn(i,n) for(int i = (int)(n); i >= 0; i--)
#define size(s) (int)s.size()
#define debug(var)  do{std::cout << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cout << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } }
template<typename T> inline istream& operator>>(istream &i, vector<T> &v) {rep(j, size(v)) i >> v[j]; return i;}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class T> T gcd(T a, T b) {if(b==0)return a; else return gcd(b,a%b);}
template<class T> T lcm(T a, T b) {return a/gcd(a,b)*b;}
template<class T = int> using V = vector<T>;
template<class T = int> using VV = vector<V<T>>;
bool isIn(int i, int j, int h, int w) {return i >= 0 && i < h && j >= 0 && j < w;}
void Yes(){cout << "Yes" << endl;}
void No(){cout << "No" << endl;}
void YES(){cout << "YES" << endl;}
void NO(){cout << "NO" << endl;}
void err() {cout << -1 << endl;}
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define ep emplace_back

const int MOD = 1000000007;
const int INF = 1e9;

#define PI acos(-1);
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
int ddx[8] = {1,1,1,-1,-1,-1,0,0};
int ddy[8] = {0,1,-1,0,1,-1,1,-1};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, k;
    cin >> n >> k;
    V<> a(n);
    cin >> a;
    sort(rall(a));
    int ans = accumulate(a.begin(), a.begin() + k, 0);
    cout << ans << endl;
}

#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <string.h>

using namespace std;

int main() {
  int n,k,L[100];
  cin >> n >> k;
  for(int i = 0; i < n; i++)
    cin >> L[i];

  sort(L,L+n);
  int sum = 0;
  for(int i = n-1; i >= n-k; i--)
    sum += L[i];

  cout << sum << endl;
}


#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll long long
#define vll vector<ll>
#define ld long double
#define pll pair<ll,ll>
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define oset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define osetll tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define ook order_of_key
#define fbo find_by_order
const int MOD=1000000007;
long long int inverse(long long int i){
    if(i==1) return 1;
    return (MOD - ((MOD/i)*inverse(MOD%i))%MOD+MOD)%MOD;
}
ll POW(ll a,ll b)
{
    if(b==0) return 1;
    if(b==1) return a%MOD;
    ll temp=POW(a,b/2);
    if(b%2==0) return (temp*temp)%MOD;
    else return (((temp*temp)%MOD)*a)%MOD;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll n;
    cin>>n;
    string s[n];
    ll a[n],b[n];
    vector<pair<ll,ll> > v1,v2;
    for(int i=0;i<n;i++)
    {
        cin>>s[i];
        ll mx=0,temp=0;
        for(int j=0;j<s[i].size();j++)
        {
            if(s[i][j]==')') temp++;
            else temp--;
            mx=max(temp,mx);
        }
        a[i]=mx;
        temp=0;
        ll mn=0;
        for(int j=s[i].size()-1;j>=0;j--)
        {
            if(s[i][j]=='(') temp++;
            else temp--;
            mn=max(mn,temp);
        }
        b[i]=mn;
        //cout<<a[i]<<" "<<b[i]<<" ";
        if( (b[i]-a[i]) >0) v1.PB(MP(a[i],i));
        else v2.PB(MP(-b[i],i));
    }
    sort(v1.begin(),v1.end());
    sort(v2.begin(),v2.end());
    stack<ll> st;
    ll flag=0;
    for(int i=0;i<v1.size();i++)
    {
        string k=s[v1[i].S];
        for(int j=0;j<k.size();j++)
        {
            if(k[j]=='(') st.push(1);
            else
            {
                if(st.empty()) {flag=1; break;}
                else st.pop();
            }
        }
        if(flag==1) break;
    }
    if(flag==0)
    {
        for(int i=0;i<v2.size();i++)
        {
            string k=s[v2[i].S];
            for(int j=0;j<k.size();j++)
            {
                if(k[j]=='(') st.push(1);
                else
                {
                    if(st.empty()) {flag=1; break;}
                    else st.pop();
                }
            }
            if(flag==1) break;
        }
    }
    if(flag==0 && st.empty()) cout<<"Yes";
    else cout<<"No";
}

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

int* mallocint(int si)
{
    return (int*) malloc(si * sizeof(int));
}

int* _minn;
int* _total;

bool compare(int i1, int i2)
{
    if( _minn[i1] != _minn[i2] ) return _minn[i1] > _minn[i2];
    return _total[i1] > _total[i2];
}

bool comparen(int i1, int i2)
{
    return _minn[i1] < _minn[i2];
}

bool check(int &count, vector<int> &l)
{
    sort(l.begin(), l.end(), compare);
    queue<int> q;
    int current = 0;
    for( auto itr = l.begin(); itr != l.end(); itr++ )
    {
        if( current + _minn[*itr] < 0 )
        {
            return false;
        }
        current += _total[*itr];
    }
    count = current;
    return true;
}

bool check(vector<int> &pindex, vector<int> &nindex, int lminmin, int rminmin)
{
    int c1 = 0;
    int c2 = 0;
    if( !check(c1, pindex) ) return false;
    if( !check(c2, nindex) ) return false;
    if( c1 != c2 ) return false;
    if( lminmin + c1 < 0 ) return false;
    if( rminmin + c2 < 0 ) return false;
    return true;
}

int main()
{
    int n;
    string s;
    cin >> n;
    getline(cin, s);
    int* minn = mallocint(n);
    int* total = mallocint(n);
    _minn = minn;
    _total = total;
    vector<int> pindex;
    vector<int> nindex;
    int lminmin = 0;
    int rminmin = 0;
    for( int i = 0; i < n; i++ )
    {
        getline(cin, s);
        int lcount = 0;
        int lmin = 0;
        int rcount = 0;
        int rmin = 0;
        for( int j = 0; j < s.length(); j++ )
        {
            if (s.at(j) == '(') lcount++;
            if (s.at(j) == ')') lcount--;
            if( lcount < lmin ) lmin = lcount;
        }

        for( int j = s.length() - 1; j >= 0; j-- )
        {
            if (s.at(j) == ')') rcount++;
            if (s.at(j) == '(') rcount--;
            if( rcount < rmin ) rmin = rcount;
        }

        if( lcount > 0 )
        {
            minn[i] = lmin;
            total[i] = lcount;

            pindex.push_back(i);
        }
        else if( rcount > 0 )
        {
            minn[i] = rmin;
            total[i] = rcount;
            nindex.push_back(i);
        }
        else
        {
            if( lmin < lminmin ) lminmin = lmin;
            if( rmin < rminmin ) rminmin = rmin;
        }

    }

    if(check(pindex, nindex, lminmin, rminmin))
    {
        cout << "Yes";
    }
    else
    {
        cout << "No";
    }
}


#include<iostream>
#include<cstdio>
using namespace std;
int main(){
	long long Min,Max,sum=0,n,m;
	scanf("%lld%lld",&n,&Max);
	Min=sum=Max;
	for(int i=1;i<n;i++){
		scanf("%lld",&m);
		if(m>Max) Max=m;
		if(m<Min) Min=m;
		sum+=m;
	}
	printf("%lld %lld %lld\n",Min,Max,sum);
	return 0;
}

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

int main(){
    int n;
    scanf("%d\n", &n);
    int i = 0;
    int x, min, max;
    long sum;
    min = 1000000;
    max = - 1000000;
    sum = 0;
    while (i <= n){
        if(i == n){
            printf("%d %d %ld\n", min, max, sum);
            break;}
        scanf("%d", &x);
        if(x < min){
            min = x;
            }
        if(max < x){
            max = x;
            }
        sum += x;
        i += 1;
    }
    return 0;
}


// 2020-08-26 16:05:35
// clang-format off
#include<bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
using P = pair<int, int>;
template<class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } else return false; }
template<class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }

// clang-format on
void answer() {
  int n;
  cin >> n;
  vector<int> p(55555 + 55, 1), ans;
  p[0] = p[1] = 0;
  REP(i, 2, 55555 + 5) if (p[i]) {
    for (int j = 2 * i; j < 55555 + 5; j += i) p[j] = 0;
    if (i % 5 == 1) {
      ans.push_back(i);
      if (ans.size() == n) break;
    }
  }
  rep(i, ans.size()) {
    if (i) cout << ' ';
    cout << ans[i];
  }
  cout << '\n';
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}


// God put a smile upon your face <3

#include <bits/stdc++.h>

#define slld(longvalue) scanf("%lld", &longvalue)

#define ll long long
#define ull unsigned long long
#define pll pair < long long, long long >

#define fastio ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0)

#define pb push_back

#define bug printf("BUG\n")

#define mxlld LLONG_MAX
#define mnlld -LLONG_MAX

#define mxd 2e8
#define mnd -2e8

#define pi 3.14159265359

using namespace std;

bool check(ll n, ll pos)
{
	return n & (1LL << pos);
}

ll Set(ll n, ll pos)
{
	return n = n | (1LL << pos);
}

ll dp[200005][4];

vector < ll > vec;

const ll mn = -1e18;

ll solve(ll pos, ll skip)
{
	ll &ret = dp[pos][skip];

	if(ret != mn) return ret;

	if(pos >= vec.size())
	{
		if(vec.size() & 1)
			if(skip == 2) return ret = mn + 2;

		return ret = 0;
	}

	ret = solve(pos + 2, skip) + vec[pos];

	if(skip) ret = max(ret, solve(pos + 1, skip - 1));

	return ret;
}
int main()
{
    ll i, j, k, l, m, n, o, r, q;
    ll testcase;
    ll input, flag, tag, ans;

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

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

    while(cin >> n)
    {
		vec.clear();
		vec.resize(n);

		for(ll i = 0; i < n; i++) cin >> vec[i];

		for(ll i = 0; i <= n + 3; i++) for(ll j = 0; j < 4; j++) dp[i][j] = mn;

		ans = solve(0,(n % 2) + 1);

		cout << ans << "\n";

    }

}

#include <stdio.h>
#include <assert.h>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
#include <queue>
#include <set>
//#include <cmath>
#include <unordered_map>
//#include <unordered_set>
//#include <boost/container/static_vector.hpp>
//#include <boost/unordered_set.hpp>
//#include <boost/unordered_map.hpp>
//#include <unistd.h>

//#include <cv.h>
//#include <highgui.h>
#include <stdlib.h>
#include <time.h>

#include <string>

const int MAX_N = 2050;
const int MAX_Q = 200000;
int N, M, Q;

char S[MAX_N][MAX_N];
int x1[MAX_Q], y1[MAX_Q], x2[MAX_Q], y2[MAX_Q];

int ac_node[MAX_N][MAX_N] = {};
int ac_edge_hor[MAX_N][MAX_N] = {};
int ac_edge_ver[MAX_N][MAX_N] = {};

int main(int argc, char **argv) {
    std::cin >> N >> M >> Q;

    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= M; j++) {
            std::cin >> S[i][j];
        }
    }

    for (int i = 1; i <= Q; i++) {
        std::cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
    }

    for (int i = 1; i <= N; i++) {
        int ac_tmp = 0;
        for (int j = 1; j <= M; j++) {
            if (S[i][j] == '1') {
                ac_tmp++;
            }
            ac_node[i][j] = ac_node[i-1][j] + ac_tmp;
        }

        int ac_tmp_edge_hor = 0;
        int ac_tmp_edge_ver = 0;
        for (int j = 1; j <= M; j++) {
            if (S[i][j] == '1' && S[i][j-1] == '1') {
                ac_tmp_edge_ver++;
            }
            if (S[i][j] == '1' && S[i-1][j] == '1') {
                ac_tmp_edge_hor++;
            }
            ac_edge_ver[i][j] = ac_edge_ver[i-1][j] + ac_tmp_edge_ver;
            ac_edge_hor[i][j] = ac_edge_hor[i-1][j] + ac_tmp_edge_hor;
        }
    }

    for (int i = 1; i <= Q; i++) {
        int node_num = ac_node[x2[i]][y2[i]] - ac_node[x2[i]][y1[i] - 1] - ac_node[x1[i] - 1][y2[i]] + ac_node[x1[i] - 1][y1[i] - 1];
        int edge_num_hor = ac_edge_hor[x2[i]][y2[i]] - ac_edge_hor[x2[i]][y1[i] - 1] - ac_edge_hor[x1[i]][y2[i]] + ac_edge_hor[x1[i]][y1[i] - 1];
        int edge_num_ver = ac_edge_ver[x2[i]][y2[i]] - ac_edge_ver[x2[i]][y1[i]] - ac_edge_ver[x1[i] - 1][y2[i]] + ac_edge_ver[x1[i] - 1][y1[i]];

        //std::cout << "check:" << node_num << " " << edge_num << std::endl;

        std::cout << node_num - edge_num_hor - edge_num_ver << std::endl;
    }

    return 0;
}


# include <iostream>
using namespace std;

int gcd(int a, int b){
  return (b==0) ? a : gcd(b, a%b);
}

int main(){

  int n, k;
  cin >> n >> k;

  int g = 0, m = 0;
  for(int i=0; i<n; i++){
    int x;
    cin >> x;
    g = gcd(x, g);
    m = max(x, m);
  }

  if(k <= m && k % g == 0) cout << "POSSIBLE";
  else cout << "IMPOSSIBLE";
  cout << endl;
  return 0;
}

#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
#include<stdio.h>
#include<locale.h>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long int ll;

int main()
{
 int N,M;
 scanf("%d %d",&N,&M);
 int ans=1;
 for(int i=1;i<=min(100000,M);i++)
 {
	if(M%i==0)
	{
		//約数はiとM/i
		if(i>=N){ans=max(ans,M/i);}
		if((M/i)>=N){ans=max(ans,i);}
	}
 }
 printf("%d",ans);
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<deque>
#include<functional>
#include<iterator>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<utility>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define a first
#define b second
#define sz(x) (ll)((x).size())
#define pb push_back
#define mp make_pair
#define bg begin()
#define ed end()
#define all(x) (x).bg,(x).ed
#define rep(i,n) for(ll i=0;i<(n);i++)
#define rep1(i,n) for(ll i=1;i<=(n);i++)
#define rrep(i,n) for(ll i=(n)-1;i>=0;i--)
#define rrep1(i,n) for(ll i=(n);i>=1;i--)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
const ll MOD=998244353;
const ll INF=1000000000000000;
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;}
ll maxx(ll x,ll y,ll z){return max(max(x,y),z);}
ll minn(ll x,ll y,ll z){return min(min(x,y),z);}
ll gcd(ll x,ll y){if(x%y==0) return y;else return gcd(y,x%y);}
ll lcm(ll x,ll y){return x*(y/gcd(x,y));}
vector<ll> pw2(51,1);vector<ll> pw10(19,1);
ll fac[500001],finv[500001],inv[500001];

void table() {
    fac[0]=fac[1]=1;
    finv[0]=finv[1]=1;
    inv[1]=1;
    for(ll i=2;i<500001;i++){
        fac[i]= fac[i-1]*i%MOD;
        inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;
        finv[i]=finv[i-1]*inv[i]%MOD;
    }
}

ll dev(long long a,long long b){
    return (a*inv[b])%MOD;
}

ll comb(ll n, ll k){
    if(n<k) return 0;
    if(n<0 || k<0) return 0;
    return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD;
}
ll mod_pow(ll x,ll n,ll mod){
    ll res=1;
    while(n>0){
        if(n&1) res=res*x%mod;
        x=x*x%mod;
        n>>=1;
    }
    return res;
}

int main(){
    table();
    {rep1(i,50) pw2[i]=2*pw2[i-1];}
    {rep1(i,18) pw10[i]=10*pw10[i-1];}

    ll N,M,K; cin>>N>>M>>K;
    ll ans=0;
    rep(i,K+1){
        ans+=(((M*comb(N-1,i))%MOD)*mod_pow(M-1,N-i-1,MOD))%MOD;
        ans%=MOD;
    }
    cout<<ans<<endl;
}

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

int mod_pow(int n, int e, int mod){
    int ans = 1;
    n = n % mod;
    while(e != 0){
        if(e % 2 == 1){
            ans = (ans * n) % mod;
        }
        e /= 2;
        n = (n * n) % mod;
    }
    return ans;
}

int mod_inv(int n, int mod){
    return mod_pow(n, mod-2, mod);
}

int mod_combination(int n, int k, int mod){
    if(n < k || k < 0) return 0;
    if(n-k < k) k = n-k;
    int ans = 1;
    for(int i=1; i<=k; i++){
        ans = (((ans * (n+1-i)) % mod) * mod_inv(i, mod)) % mod;
    }
    return ans;
}

signed main(){
    // cout << fixed << setprecision(10) << flush;

    int mod = 998244353;
    int n, m, k;
    cin >> n >> m >> k;

    int ans = 0;
    int combi = 1;
    if(m == 1){
        if(k == n-1){
            cout << 1 << endl;
        }
        else{
            cout << 0 << endl;
        }
        return 0;
    }
    int m_1_pow = mod_pow(m-1, n-1, mod);
    int m_1_inv = mod_inv(m-1, mod);
    for(int l=0; l<=k; l++){
        ans = (ans + (((combi * m_1_pow) % mod) * m) % mod) % mod;
        m_1_pow = (m_1_pow * m_1_inv) % mod;
        combi = (((combi * (n-1-l)) % mod) * mod_inv(l+1, mod)) % mod;
    }
    cout << ans << endl;

    return 0;
}


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

using namespace std;

int main(void)
{
	int n, m;

	while (cin >> n >> m, n || m){
		vector <int> p(n + 1);
		for (int i = 0; i < n; i++){
			scanf("%d", &p[i]);
		}
		p[n] = 0;
		n++;

		vector <int> sum(n * n);
		int cnt = 0;
		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				sum[cnt] = p[i] + p[j];
				// cout << sum[cnt] << " " << p[i] << " " << p[j] << endl;
				cnt++;
			}
		}

		sort(sum.begin(), sum.end());
		int ret = 0;
		for (int i = 0; i < n * n; i++){
			int diff = m - sum[i];
			if (diff < 0) continue;
			int up = *(upper_bound(sum.begin(), sum.end(), diff)-1);
			ret = max(ret, sum[i] + up);
		}

		cout << ret << endl;
	}

	return 0;
}

#include <bits/stdc++.h>
typedef long long LL;
using namespace std;
inline int rd() {
    char ch=getchar(); int i=0,f=1;
    while(!isdigit(ch)) {if(ch=='-')f=-1; ch=getchar();}
    while(isdigit(ch)) {i=(i<<1)+(i<<3)+ch-'0'; ch=getchar();}
    return i*f;
}
const int N=2e5+50;
const LL INF=0x3f3f3f3f3f3f3f3f;
int n,q;
LL bit[2][N],tag,mn=0;
inline void inc(LL *a,int i,LL v) {for(;i<=n;i+=(i&(-i))) a[i]=min(a[i],v);}
inline LL ask(LL *a,int i,LL v=INF) {for(;i;i-=(i&(-i))) v=min(a[i],v); return v;}
int main() {
    n=rd(), q=rd();
    int a=rd(), b=rd();
    memset(bit[0],0x3f,sizeof(bit[0]));
    memset(bit[1],0x3f,sizeof(bit[1]));
    inc(bit[0],a,-a); inc(bit[1],n-a+1,a);
    for(int i=1;i<=q;i++) {
        int x=rd();
        LL v=min(ask(bit[0],x)+x,ask(bit[1],n-x+1)-x);
        tag+=abs(x-b); v-=abs(x-b); mn=min(mn,v);
        inc(bit[0],b,v-b); inc(bit[1],n-b+1,v+b);
        b=x;
    }
    printf("%lld\n",mn+tag);
}


#include <bits/stdc++.h>
#define rep(i,n) for (ll i = 0; i < n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++)
using namespace std;

typedef long long ll;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline void dump(vector<T> v) { for (auto& x : v) cerr << x << " "; cerr << endl; }
template<class T> inline void dump(vector<pair<T, T>> v) { for (auto& p : v) cerr << p.first << " " << p.second << endl; }
template<class T> inline void dump(vector<vector<T>> vv) { for (auto& v : vv) {for (auto& x : v) cerr << x << " "; cerr << endl;} }

const ll INF = 1e9;
const long long INFL = 1LL<<60;

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

  map<ll, ll> mp;

  rep(i, m) {
    ll d, c;
    cin >> d >> c;
    mp[d] += c;
  }

  ll ans = 0;

  bool flag = true;
  while (flag) {
    flag = false;
    rep(i, 10) {
      if (mp[i] > 1) flag = true;
      ll num = mp[i] / 2;
      ans += num;
      mp[i] -= num * 2;
      if (i + i < 10) mp[i+i] += num;
      else {
        mp[1] += num;
        mp[(i + i) % 10] += num;
      }
    }
  }

  vector<ll> a;
  rep(i, 10) {
    if (mp[i] != 0) a.push_back(i);
  }

  while (a.size() > 1) {
    int x = a.back(); a.pop_back();
    int y = a.back(); a.pop_back();
    if (x + y < 10) a.push_back(x + y);
    else {
      a.push_back(1);
      a.push_back((x + y) % 10);
    }
    ans++;
  }

  cout << ans << endl;

  return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using piii = pair<int, pair<int, int>>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
template <typename T>
using Graph = vector<vector<T>>;
const int MOD = 1e9 + 7;

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

    int M;
    cin >> M;
    ll ans = 0, sum = 0;
    for (int i = 0; i < M; ++i) {
        int d;
        ll c;
        cin >> d >> c;
        sum += d * c;
        ans += c;
    }
    ans += (sum - 1) / 9;

    cout << ans - 1 << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pp pair<int,int>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ll long long
ll MOD=1000000007;
int inf=1000000000;
ll INF=10000000000000000;
int main(){
    int n;
    ll x;
    cin >> n >> x;
    vector<ll> b(n),l(n),u(n);
    vector<pair<ll,ll>> a(n);
    ll uu=0;
    rep(i,n){
        cin >> b.at(i) >> l.at(i) >> u.at(i);
        a.at(i).first=u.at(i)*(x-b.at(i))+l.at(i)*b.at(i);
        a.at(i).second=i;
        uu+=l.at(i)*b.at(i);
    }
    sort(a.begin(),a.end());
    reverse(a.begin(),a.end());
    ll sum=0,v;
    rep(i,n){
        sum+=a.at(i).first;
        if (sum>uu) {sum-=a.at(i).first;v=i;break;}
        if (sum==uu) {cout << (i+1)*x << endl;return 0;}
    }
    ll ans=INF;
    rep(i,n){
        ll y=sum;
        int p=a.at(i).second;
        if (i<v) y=sum-a.at(i).first+a.at(v).first;
        ll t=uu-y+(u.at(p)-l.at(p))*b.at(p);
        ll o=t/u.at(p);
        if (t%u.at(p)!=0) o++;
        ans=min(ans,v*x+o);
        t=uu-y;
        o=t/l.at(p);
        if (t%l.at(p)!=0) o++;
        ans=min(ans,v*x+o);
    }
    cout << ans << endl;
}

/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(LL &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const LL &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
 #define DEBUG(...) {printf("# ");printf(__VA_ARGS__);puts("");}
#else
 #define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1e6+10;
int N,X;
LL b[SIZE],l[SIZE],u[SIZE];
bool used[SIZE];
int main(){
    R(N,X);
    LL need=0;
    priority_queue<PLL>h0,h1,h2;
    REP(i,N){
        R(b[i],l[i],u[i]);
        need+=b[i]*l[i];
        h0.push({l[i]*b[i]+u[i]*(X-b[i]),i});
    }
    LL an=0;
    while(need>=h0.top().F){
        an+=X;
        used[h0.top().S]=1;
        need-=h0.top().F;
        h0.pop();
    }
    if(need<=0){
        W(an);
        return 0;
    }
    int add_id=h0.top().S;
    LL add_v=l[add_id]*b[add_id]+u[add_id]*(X-b[add_id]);
    LL mi=1e18;
    REP(i,N){
        if(used[i]){
            LL need2=need-add_v+l[i]*b[i]+u[i]*(X-b[i]);

            if(l[i]*b[i]>=need2){
                mi=min(mi,(need2+l[i]-1)/l[i]);
            }
            else{
                mi=min(mi,(need2-l[i]*b[i]+u[i]-1)/u[i]+b[i]);
            }
        }
        else{
            if(l[i]*b[i]+u[i]*(X-b[i])<need)continue;
            if(l[i]*b[i]>=need){
                mi=min(mi,(need+l[i]-1)/l[i]);
            }
            else{
                mi=min(mi,(need-l[i]*b[i]+u[i]-1)/u[i]+b[i]);
            }
        }
    }
    an+=mi;
    W(an);
    return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <string>
#include <string.h>

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)

using namespace std;
int dp[1001][1001];//dp[y][x];
//??????????????????
string x,y;
void solve(){
		cin >> x >> y ;
		memset(dp,10000,1001*1001);
		REP(i,x.size()+1){
			  dp[0][i] = i;
		}
		REP(i,y.size()+1){
			  dp[i][0] = i;
		}
		FOR(i,1,x.size()+1){
			FOR(j,1,y.size()+1){
				if(x[i-1]==y[j-1]){
					dp[j][i] = dp[j-1][i-1];
				}
				else {
					dp[j][i] = min ( 1 + min(dp[j-1][i],dp[j][i-1]),dp[j-1][i-1]+1);
				}

			}
		}
		/*REP(i,y.size()+1){
			REP(j,x.size()+1){
				cout << dp[i][j] << " ";
			}
			cout << endl;
		}
		*/
		cout << dp[y.size()][x.size()] << endl;
}

int main(){
solve();
}

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

using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int MAX_N=55555;
bool is_prime[MAX_N];
int prime[MAX_N];
int main(){
  int dp[1001][1001];
  for(int nn=0;nn<1;nn++){
    string s1,s2;
    cin>>s1>>s2;
    for(int i=0;i<1001;i++)fill(dp[i],dp[i]+1001,0);
    for(int i=0;i<s1.size()+1;i++)dp[i][0]=i;
    for(int i=0;i<s2.size()+1;i++)dp[0][i]=i;
    for(int i=0;i<s1.size();i++){
      for(int j=0;j<s2.size();j++){
        int x=1;
        if(s1[i]==s2[j]) x=0;
        dp[i+1][j+1]=min(dp[i][j]+x,min(dp[i][j+1]+1,dp[i+1][j]+1));
      }
    }
    cout<<dp[s1.size()][s2.size()]<<endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(void){
    int N, X, T, time;
    cin >> N >> X >> T;
    time = N/X;

    if (N%X != 0) time += 1;

    cout << time*T << endl;
}

#include<iostream>
#include<algorithm>
#include<functional>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<deque>
using namespace std;
#define ll long long
const int mod = 1000000007;
const ll INF = 1000000000000000000;

int main()
{
	int A[3], K;
	cin >> A[0] >> A[1] >> A[2] >> K;
	sort(A, A + 3);
	int ans = A[0] + A[1];
	for (int i = 0; i < K; i++) A[2] *= 2;
	ans += A[2];
	cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n)  for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define all(a) a.begin(),a.end()
#define pb push_back
#define eb emplace_back
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define ub(v,k) (upper_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
typedef multiset<ll> S;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const int INF=1001001001;
const int mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void noyes(T b){if(b)out("no");else out("yes");}
template<class T> void NoYes(T b){if(b)out("No");else out("Yes");}
template<class T> void NOYES(T b){if(b)out("NO");else out("YES");}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){a%=mod;if(b==0)return 1;if(b&1)return a*modpow(a,b-1)%mod;ll k=modpow(a,b/2);return k*k%mod;}
ll ans=0;
const int l=20;
vi dep;
vvi table;
vvi g;
void dfs0(int i,int p,int d){
    table[i][0]=p;
    dep[i]=d;
    for(ll x:g[i])if(x!=p)dfs0(x,i,d+1);
}
void init(ll n){
    g=vvi(n);
    dep=vi(n);
    table=vvi(n,vi(l,-1));
    rep(i,n-1){
        ll a;cin>>a;a--;
        g[a].pb(i+1);
        g[i+1].pb(a);
    }
    dfs0(0,-1,0);
}
vb done;
void dfs(int i){
    done[i]=true;
    for(ll x:g[i])if(x!=table[i][0]){
        if(!done[x])dfs(x);
    }
}
int main(){
    ll n,k,t;cin>>n>>k>>t;
    if(t!=1)ans++;
    if(k==1){
        rep(i,n-1){
            ll a;cin>>a;if(a!=1)ans++;
        }
        dame(ans);
    }
    init(n);
    REP(i,1,l){
        rep(j,n){
            ll id=table[j][i-1];
            if(id!=-1)table[j][i]=table[id][i-1];
        }
    }
    k--;
    vp v(n);
    rep(i,n)v[i]=P(dep[i],i);
    sort(all(v));reverse(all(v));
    done=vb(n,false);
    rep(tt,n){
        ll i=v[tt].se;
        if(dep[i]<=k+1)break;
        if(done[i])continue;
        ll w=i;
        rep(j,l)if(k>>j&1)w=table[w][j];
        ans++;
        dfs(w);
    }
    out(ans);
}

//#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
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 Would
#define you
#define please

int N, K;
int kotae;
int ne[100001], H[100001], D;
int dfs(int A) {
	int d = 1;
	D++;
	for (int v = H[A]; v; v = ne[v]) {
		d = max(d, dfs(v) + 1);
	}
	D--;
	if (d >= K && D > 1) {
		kotae++;
		return 0;
	}
	else return d;
}

const int cm = 1 << 17;
char cn[cm], * ci = cn + cm, ct;
inline char getcha() {
	if (ci - cn == cm) { fread(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;
}

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

	N = getint();
	K = getint();

	if (getint() != 1) kotae++;
	for (int i = 2; i <= N; i++) {
		int a = getint();
		ne[i] = H[a];
		H[a] = i;
	}

	dfs(1);
	printf("%d", kotae);

	Would you please return 0;
}


#include <bits/stdc++.h>

#define PI 3.14159265359
#define rep(i,a,n) for(int i=a;i<(int)n;++i)
#define SZ(x) ((int)(x).size())	//size() unsigned -> int
#define descSort(a) sort(a.begin(),a.end(),std::greater<int>())
using namespace std;
typedef long long ll;
const ll INF = 1e9 + 7;
ll gcd(ll x,ll y){
    if(x%y==0)return y;
    return gcd(y,x%y);
}
ll LCM(int a, int b){
    return a*b/gcd(a,b);
}

int main(void)
{
    int n;
    cin>>n;
    map<ll,int,greater<int>> a;
    rep(i,0,n){
        int in;
        cin>>in;
        a[in]++;
    }

    vector<ll> num;
    ll ans=0;
    for(auto i:a){
        if(i.second>=4){
            ans=max(ans,i.first*i.first);
        }
        if(i.second>=2){
            num.push_back(i.first);
        }
        if(num.size()==2){
            ans=max(ans,num[0]*num[1]);
        }
    }
    cout<<ans<<endl;
    return 0;
}

#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll inf = 1000000007;
int main() {
	ll n;
	cin >> n;
	vector<ll>a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	sort(a.begin(), a.end());
	ll x = 0,y = 0;
	ll index = 0;
	for (int i = n - 1; i > 0; i--) {
		if (a[i] == a[i - 1]) {
			x = a[i];
			index = i - 2;
			break;
		}
	}

	for (int i = index; i > 0; i--) {
		if (a[i] == a[i - 1]){
			y = a[i];
			break;
		}
	}
	cout << x * y << endl;
	return 0;
}


//kruskal tree
#include <cstdio>
#include <algorithm>
using namespace std;
#define M 100000
int parent[M],a[M],b[M];
pair<int,int>node[M];
int root(int a){return parent[a]==a?a:parent[a]=root(parent[a]);}
int unite(int a,int b){
	int x=root(a),y=root(b);
	if(x==y)return 0;
	parent[x]=y;
	return 1;
}
int main(){
	int i,s,n,m;
	for(;scanf("%d%d",&n,&m)>1;printf("%d\n",s)){
		for(i=0;i<m;i++)scanf("%d%d%d",a+i,b+i,&node[i].first),node[i].second=i;
		sort(node,node+m);
		for(i=0;i<n;i++)parent[i]=i;
		for(s=i=0;i<m;i++)if(unite(a[node[i].second],b[node[i].second]))s+=node[i].first;
	}
}

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

struct edge{
	int from,to,weight;
};

bool asc(const edge &e,const edge &f){
	return e.weight < f.weight;
}

int init(vector<int> &U){
	for (int i=0;i<U.size();i++){
		U[i]=i;
	}
}

int root(vector<int> &U,int x){
	if(x==U[x]){
		return x;
	}else{
		return U[x]=root(U,U[x]);
	}
}

void unite(vector<int> &U,int x,int y){
	int px=root(U,x);
	int py=root(U,y);
	U[px]=py;
}

bool same(vector<int> &U,int x,int y){
	return root(U,x)==root(U,y);
}

int main(){
	int V,E,s,t,w;
	cin >> V >> E;

	vector<int> U(V);
	init(U);

	vector<edge> EdgeList(E);

	for (int i=0;i<E;i++){
		edge e;
		cin >> e.from >> e.to >> e.weight;
		EdgeList[i]=e;
	}

	sort(EdgeList.begin(),EdgeList.end(),asc);

	int W=0;

	for (int i=0;i<E;i++){
		edge e=EdgeList[i];
		if(same(U,e.from,e.to)){
			continue;
		}else{
			unite(U,e.from,e.to);
			W+=e.weight;
		}
	}

	cout << W << endl;

	return 0;
}


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

using namespace std;

#define MAX(a,b) ((a)>(b)?(a):(b))

int main()
{
    int N;
    long long M;

    cin >> N >> M;

    while(N!=0 && M!=0) {
        vector<long long> p;

        long long ans=0;

        for(int i=0; i<N; ++i) {
            long long temp;
            cin >> temp;
            p.push_back(temp);
            ans=MAX(ans,temp);
        }

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

        for(int i=0; i<N; ++i) {
            for(int j=i; j<N; ++j) {
                if(p[i]+p[j]>M) break;
                p.push_back(p[i]+p[j]);
                ans=MAX(ans,p[i]+p[j]);
            }
        }

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

        for(int i=0; i<(int)p.size(); ++i) {
            auto itr=upper_bound(p.begin(),p.end(),M-p[i]);
            if(itr==p.begin()) break;
            else --itr;
            ans=MAX(ans,p[i]+*itr);
        }

        cout << ans << endl;
        cin >> N >> M;
        p.clear();
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
  while(1){
    int m,nmin,nmax,ans=0,max=0,p[200]={};
  cin>>m>>nmin>>nmax;
  if(m+nmin+nmax==0)break;
  for(int i=0;i<m;i++)cin>>p[i];
  for(int i=nmin;i<=nmax;i++)
    if(p[i-1]-p[i]>=max){
      max=p[i-1]-p[i];
      ans=i;
    }
  cout<<ans<<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>;
const int INF = 1001001001;
const int MOD = 1000000007;

template <typename T>
void print(const T &v);

int main()
{
    int m;
    cin >> m;
    ll total = 0;
    ll cnt = 0;
    for (int i = 0; i < m; i++)
    {
        ll d, c;
        cin >> d >> c;
        total += d * c;
        if (total >= 10)
        {
            cnt += total / 9;
            total %= 9;
            if (total == 0)
            {
                --cnt;
                total = 9;
            }
        }
        cnt += c;
    }
    cout << cnt - 1 << endl;
    return 0;
}

// Use For Debug
template <typename T>
void print(T const &v)
{
    for (int i = 0; i < v.size(); i++)
    {
        if (i)
            cout << ' ';
        cout << v[i];
    }
    cout << endl;
}

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

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

using ll = long long;
using P = pair<int,int>;
using graph = vector<vector<int>>;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll INF = 1LL<<60;
const ll mod = 1000000007LL;

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

int main(){
    vector<vector<P>> v(10);
    rep(i,10){
        v[i].push_back(make_pair(0,0));
        v[i].push_back(make_pair(0,i));
    }
    rep(i,10){
        while(true){
            P p = *v[i].rbegin();
            int t = i*10 + p.second;
            int cnt = 0;
            while(t>=10){
                t = t/10+t%10;
                cnt++;
            }
            v[i].push_back(make_pair(p.first+cnt,t));
            if(t==i) break;
        }
    }
    v[0][2].first = 1;
    ll M;
    cin>>M;
    vector<ll> d(M),c(M);
    rep(i,M) cin>>d[i]>>c[i];
    ll ans = 0;
    vector<ll> rest(M);
    rep(i,M){
        ll t = d[i],s=c[i];
        ll l = v[t].size();
        ans+=v[t][l-1].first*((s-1)/(l-2));
        s = s - ((s-1)/(l-2))*(l-2);
        ans += v[t][s].first;
        rest[i]=v[t][s].second;
    }
    ll cnt = 0;
    while(cnt!=M-1){
        while(rest[cnt]!=0){
            ll a = rest[cnt],b=rest[cnt+1];
            rest[cnt] = (a+b)/10;
            rest[cnt+1] = (a+b)%10;
            ans++;
        }
        cnt++;
    }
    cout<<ans<<endl;
}