#include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

class dice{
  int* me;
public:
  dice(int me_[6]){
    me = me_;
  }
  int Top(){
    return me[0];
  }
  void East(){
    swap(me[0], me[3]);
    swap(me[3], me[5]);
    swap(me[5], me[2]);
  }
  void West(){
    swap(me[0], me[2]);
    swap(me[2], me[5]);
    swap(me[5], me[3]);
  }
  void North(){
    swap(me[0], me[1]);
    swap(me[1], me[5]);
    swap(me[5], me[4]);
  }
  void South(){
    swap(me[0], me[4]);
    swap(me[4], me[5]);
    swap(me[5], me[1]);
  }
};

int main(){ _;
  int me[6];
  REP(i,6) cin>>me[i];
  dice d{me};
  char c;
  while(cin>>c){
    if(c=='E'){
      d.East();
    }else if(c=='W'){
      d.West();
    }else if(c=='N'){
      d.North();
    }else if(c=='S'){
      d.South();
    }
  }
  cout<<d.Top()<<endl;
}

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

struct dice{
	int top;
	int front;
	int left;
	int right;
	int back;
	int bottom;
};

int main(){
	//????????????????????¢?????°????????\?????????
	struct dice X;
	cin >> X.top;
	cin >> X.front;
	cin >> X.right;
	cin >> X.left;
	cin >> X.back;
	cin >> X.bottom;
	//?????¢??????????????\?????????
	string Rotation;
	cin >> Rotation;
	//????????????????????¢?????????
	for(int i = 0; i < Rotation.size(); i++){
		if(Rotation[i] == 'N'){
			struct dice Y;
			Y.top    = X.front;
			Y.front  = X.bottom;
			Y.left   = X.left;
			Y.right  = X.right;
			Y.back   = X.top;
			Y.bottom = X.back;
			X = Y;
		}
		if(Rotation[i] == 'E'){
			struct dice Y;
			Y.top    = X.left;
			Y.front  = X.front;
			Y.left   = X.bottom;
			Y.right  = X.top;
			Y.back   = X.back;
			Y.bottom = X.right;
			X = Y;
		}
		if(Rotation[i] == 'S'){
			for(int j = 0; j < 3; j++){
				struct dice Y;
				Y.top    = X.front;
				Y.front  = X.bottom;
				Y.left   = X.left;
				Y.right  = X.right;
				Y.back   = X.top;
				Y.bottom = X.back;
				X = Y;
			}
		}
		if(Rotation[i] == 'W'){
			for(int j = 0; j < 3; j++){
				struct dice Y;
				Y.top    = X.left;
				Y.front  = X.front;
				Y.left   = X.bottom;
				Y.right  = X.top;
				Y.back   = X.back;
				Y.bottom = X.right;
				X = Y;
			}
		}
	}
	//?????¢??????????????¨?????????
	cout << X.top << endl;
	return 0;
}


#include<iostream>
using namespace std;

int n,res=0;
bool map[20][20];
bool fh[10],fc[10],fl[20],fr[20];

void print()
{
	for(int i=0;i!=8;++i)
	{
		for(int j=0;j!=8;++j)
		{
			if(map[i][j])	cout<<'Q';
			else			cout<<'.';
		}
		cout<<endl;
	}
}

bool check(int x,int y)
{
	return !(fh[x] || fc[y] || fr[x+y] || fl[x+n-y+1]);
}

void pcfz(int x,int y,bool type)
{
	fh[x]=type; fc[y]=type;
	fr[x+y]=type;
	fl[x+n-y+1]=type;
	map[x][y]=type;
}

void dfs(int r)
{
	if(r==8)		print();
	else if(fh[r])	dfs(r+1);
	else
	{
		for(int i=0;i!=8;++i)
			if(check(r,i))
			{
				pcfz(r,i,1);
				dfs(r+1);
				pcfz(r,i,0);
			}
	}
}

int main()
{
	cin>>n;
	for(int i=0;i!=n;++i)
	{
		int tx,ty;
		cin>>tx>>ty;
		pcfz(tx,ty,1);
	}
	dfs(0);
	return 0;
}

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

int main() {
  vector<int> A(5);
  int k;
  for(int &i:A)
    cin>>i;
  cin>>k;
  sort(A.begin(),A.end());
  if(A.at(4)-A.at(0)>k)
    cout<<":("<<endl;
  else
    cout<<"Yay!"<<endl;
}


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

int n, k;
int array[10];

int c[4];
bool use[10];
char str[10000][100];
int m;

int number[10000];
int p;

void Search(int i, int j)
{
	if (j == k){
		for (int l = 0; l < k; l++){
			char nstr[3];
			sprintf(nstr, "%d", c[l]);
			strcat(str[m], nstr);
		}

		int num;
		sscanf(str[m], "%d", &num);
//		printf("%d\n", num);

		bool f = false;
		for (int l = 0; l < p; l++){
			if (number[l] == num){
				f = true;
				break;
			}
		}

		if (!f){
			number[p] = num;
			p++;
		}

		m++;
		return;
	}

	if (i == n){
		return;
	}

	Search(i + 1, j);

	if (use[i]){
		return;
	}

	c[j] = array[i];

	use[i] = true;

	Search(0, j + 1);

	use[i] = false;
}

int main()
{
	while (scanf("%d %d", &n, &k), n + k){
		memset(array, 0, sizeof(array));
		memset(c, 0, sizeof(c));
		memset(use, 0, sizeof(use));
		memset(str, 0, sizeof(str));
		memset(number, 0, sizeof(number));
		m = 0;
		p = 0;

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

		Search(0, 0);

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

	return 0;
}

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

class Dice {
  private:
    int *numbers;
  public:
    Dice(int *numbers) {
      this->numbers = numbers;
    }

    int getCurrentNumber() {
      return this->numbers[0];
    }

    void turn(char direction) {
      switch (direction) {
        case 'N':
          this->turnNorth();
          break;
        case 'S':
          this->turnSouth();
          break;
        case 'E':
          this->turnEast();
          break;
        case 'W':
          this->turnWest();
          break;
      }
    }

    void turnNorth() {
      swap(this->numbers[0], this->numbers[1]);
      swap(this->numbers[1], this->numbers[5]);
      swap(this->numbers[5], this->numbers[4]);
    }

    void turnSouth() {
      swap(this->numbers[0], this->numbers[4]);
      swap(this->numbers[4], this->numbers[5]);
      swap(this->numbers[5], this->numbers[1]);
    }

    void turnEast() {
      swap(this->numbers[0], this->numbers[3]);
      swap(this->numbers[3], this->numbers[5]);
      swap(this->numbers[5], this->numbers[2]);
    }

    void turnWest() {
      swap(this->numbers[0], this->numbers[2]);
      swap(this->numbers[2], this->numbers[5]);
      swap(this->numbers[5], this->numbers[3]);
    }
};

int main() {
  int numbers[6];
  char direction;
  string directions;

  for (int i = 0; i < 6; i++) {
    cin >> numbers[i];
  }
  cin >> directions;

  Dice dice(numbers);
  for (int i = 0; i < directions.size(); i++) {
    dice.turn(directions[i]);
  }

  cout << dice.getCurrentNumber() << endl;

  return 0;
}


#include<bits/stdc++.h>

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

using ll = long long;

using namespace std;

ll gcd(ll C, ll D) {
	if (C < D) gcd(D, C);

	if (C%D == 0)return D;
	else return gcd(D, C%D);
}

const int MOD = 1000000007;

int main() {
	int N;
	cin >> N;
	vector<int> A(N);
	ll sum = 0;
	rep(i, N) {
		cin >> A[i];
		sum += A[i];
	}
	ll ans = 0;

	rep(i, N) {
		sum -= A[i];
		ans += ((A[i]%MOD) * (sum%MOD))%MOD;
		ans %= MOD;
	}

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

#include<iostream>
using namespace std;
long N,A[2<<17],ans;
main()
{
	cin>>N;
	for(int i=0;i<N;i++)cin>>A[i];
	int i=0;
	while(i<N)
	{
		while(i+1<N&&A[i]+1==A[i+1])i++;
		if(A[i]>i||i+1<N&&A[i]+1<A[i+1])
		{
			cout<<-1<<endl;
			return 0;
		}
		ans+=A[i];
		i++;
	}
	cout<<ans<<endl;
}


#include <bits/stdc++.h>

#define fr first
#define sc second
#define OK puts("OK");
#define pb push_back
#define mk make_pair

using namespace std;

typedef long long ll;

const int inf = (int)1e9 + 7;
const int N = (int)1e5 + 7;

int n,m;
int top,mx;
int st[N],b[N];

int t[2001][2001];
char a[2001][2001];

int f (int i,int j) {
        return (a[i][j] == '#') ? 1 : 0;
}

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

        mx = max(n,m);

        for (int i = 1; i <= n; i ++)
                for (int j = 1; j <= m; j ++)
                        cin >> a[i][j];

        n --;
        m --;

        for (int i = 1; i <= n; i ++)
                for (int j = 1; j <= m; j ++)
                        t[i][j] = f(i,j) ^ f(i + 1,j) ^ f(i,j + 1) ^ f(i + 1,j + 1);

        for (int i = 1; i <= n; i ++) {
                for (int j = 1; j <= m; j ++)
                        if(t[i][j]) b[j] = 0;
                        else b[j] ++;

                top = 0;
                for (int j = 1; j <= m; j ++) {
                        while (top && b[st[top]] >= b[j]) {
                                mx = max (mx,(j - st[top - 1]) * (b[st[top]] + 1));
                                top --;
                        }
                        st[++top] = j;
                }
                while (top) {
                        mx = max(mx,(m - st[top - 1] + 1) * (b[st[top]] + 1));
                        top --;
                }
        }
        cout << mx << endl;
}

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

int p[1010];
int sum[1002010];

int main(void){
	int n,m;

	while(scanf("%d%d",&n,&m) && (n||m)){
		for(int i=0;i<n;i++){
			scanf("%d",&p[i]);
		}
		p[n++] = 0; //0ツ点ツづ個スツコツアツづーツ催ャツづゥ

		int size=0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				sum[size++] = p[i] + p[j];
			}
		}
		sort(sum,sum+size);

		int ans = 0;

		for(int i=0;i<size;i++){
			int rem = m - sum[i];
			if(rem < 0) continue;
			int left = 0, right = size;

			while(left < right){
				int center = (left + right) / 2;
				if(rem > sum[center]){
					left = center + 1;
				}
				else if(rem < sum[center]){
					right = center;
				}
				else {
					break;
				}
			}

			ans = max(ans, sum[i] + sum[right-1]);
		}

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

	return 0;
}


#include <bits/stdc++.h>

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

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

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

int main()
{
    ll H, W;
    cin >> H >> W;
    mat A(H, vec(W, 0));
    REP(i,0,H) {
        REP(j,0,W) {
            cin >> A[i][j];
            A[i][j] %= 2;
        }
    }

    mat Z;
    REP(i,0,H) {
        bool f = false;
        REP(j,0,W-1) {
            vec z = {i+1, j+1, i+1, j+2};
            if(A[i][j] == 1) {
                if(!f) {
                    A[i][j] = 0;
                    f = true;
                }
                else {
                    A[i][j] = 0;
                    f = false;
                }
            }
            if(f) Z.pb(z);
        }
        if(f) A[i][W-1] = 1 - A[i][W-1];
    }

    bool f = false;
    REP(i,0,H-1) {
        vec z = {i+1, W, i+2, W};
        if(A[i][W-1] == 1) {
            if(!f) {
                A[i][W-1] = 0;
                f = true;
            }
            else {
                A[i][W-1] = 0;
                f = false;
            }
        }
        if(f) Z.pb(z);
    }
    if(f) A[H-1][W-1] = 1 - A[H-1][W-1];

    PR(SZ(Z));
    REP(i,0,SZ(Z)) {
        PS(Z[i][0]); PS(Z[i][1]); PS(Z[i][2]); PR(Z[i][3]);
    }

    return 0;
}

/*

*/

#include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <functional>
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
using namespace std;
#define ll long long int
#define rep(i,n) for( int i = 0; i < n; i++ )
#define rrep(i,n) for( int i = n; i >= 0; i-- )
#define REP(i,s,t) for( int i = s; i <= t; i++ )
#define RREP(i,s,t) for( int i = s; i >= t; i-- )
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
typedef pair<int, int> P;
typedef pair<P, P> PP;
vector<PP> ans;

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int h, w; cin >> h >> w;
    int a[510][510];
    rep(i, h) {
        rep(j, w) {
            cin >> a[i][j];
        }
    }

    rep(i, h) {
        rep(j, w - 1) {
            if(a[i][j] % 2 == 1) {
                a[i][j]--;
                a[i][j + 1]++;
                ans.push_back({{i , j}, {i, j + 1}});
            }
        }
    }
    rep(i, h - 1) {
        if(a[i][w - 1] % 2 == 1) {
            a[i][w - 1]--;
            a[i + 1][w - 1]++;
            ans.push_back({{i , w - 1}, {i + 1, w - 1}});
        }
    }
    cout << ans.size() << endl;
    for(auto e: ans) {
        cout << e.first.first  + 1 << " " << e.first.second + 1 << " " << e.second.first + 1 << " " << e.second.second + 1 << endl;

    }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
typedef long long ll;
int main() {
    int N;
    cin>>N;
    vector<int>A(N+2);
    rep(i,0,N) cin>>A[i+1];
    int sum=0;
    rep(i,0,N+2){
      sum+=abs(A[i]-A[i+1]);
    }
    rep(i,0,N){
      int tmp=sum-abs(A[i+1]-A[i])-abs(A[i+1]-A[i+2])+abs(A[i]-A[i+2]);
      cout<<tmp<<endl;
    }
    return 0;
}

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

int n,k,card[10],count;
int check[10001];

// used[i]==1ÈçÎ iÍgÁÄ¢é
// used[i]==1ÈçÎ card[i]ÍgÁÄ¢é

void func(int a[],int now)
{
	int i,j,temp,keta=0,flg;

	if(now == k){
		char str[9];
		for(i=0;i<k;i++){
			temp=sprintf(str+keta,"%d",a[i]);
			keta+=temp;
		}
		temp=atoi(str);
		check[count]=temp;
		count++;
	}

	else {
		int used[10] = { 0 };

		for(i=0; i<now; i++){
			for(j=0;j<n;j++){
				if(a[i]==card[j] && used[j]==0){
					used[j]=1;
					break;
				}
			}
		}

		for(i=0;i<n;i++){
			if(used[i] == 0){
				a[now] = card[i];
				func(a,now+1);
			}
		}
	}
}

int main(void)
{
	int i,j,array[5];
	while(scanf("%d",&n),n){
		scanf("%d",&k);
		for(i=0;i<n;i++){
			scanf("%d", &card[i]);
		}

		func(array,0);
		for(i=0;i<count;i++){
			for(j=0;j<count;j++){
				if(check[i]==check[j] && i!=j && check[i]!=0){
					check[j]=0;
				}
			}
		}

		count=0;
		for(i=0;i<10000;i++){
			if(check[i]!=0){
				count++;
			}
		}
		printf("%d\n",count);

		for(i=0;i<10001;i++){
			check[i]=0;
		}
		count=0;
		for(i=0;i<10;i++){
			card[i]=0;
		}

	}
	return 0;
}


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

using namespace std;
//using namespace atcoder;

int main(){
    int a,b,c;
    cin>>a>>b>>c;
    cout<<a+b+c-max(a,max(b,c))<<endl;
}

#include<iostream>
#include<string>

using namespace std;

#define DICETOP 0
#define DICEFRONT 1
#define DICERIGHT 2
#define DICELEFT 3
#define DICEBACK 4
#define DICEBOTTOM 5

class Dice {
private:
	int *diceface;
public:
	Dice( int top, int front, int right, int left, int back, int bottom ) {
		diceface = new int[6];
		diceface[DICETOP] = top;
		diceface[DICEFRONT] = front;
		diceface[DICERIGHT] = right;
		diceface[DICELEFT] = left;
		diceface[DICEBACK] = back;
		diceface[DICEBOTTOM] = bottom;
	}
	~Dice( ) {
		delete [ ] diceface;
	}
	void roll( string cmd ) {
		int t;
		for( int i=0; i<cmd.length(); i++ ) {
			if( 'E' == cmd[i] ) {
				t = diceface[DICETOP];
				diceface[DICETOP] = diceface[DICELEFT];
				diceface[DICELEFT] = diceface[DICEBOTTOM];
				diceface[DICEBOTTOM] = diceface[DICERIGHT];
				diceface[DICERIGHT] = t;
			} else if( 'N' == cmd[i] ) {
				t = diceface[DICETOP];
				diceface[DICETOP] = diceface[DICEFRONT];
				diceface[DICEFRONT] = diceface[DICEBOTTOM];
				diceface[DICEBOTTOM] = diceface[DICEBACK];
				diceface[DICEBACK] = t;
			} else if( 'S' == cmd[i] ) {
				t = diceface[DICETOP];
				diceface[DICETOP] = diceface[DICEBACK];
				diceface[DICEBACK] = diceface[DICEBOTTOM];
				diceface[DICEBOTTOM] = diceface[DICEFRONT];
				diceface[DICEFRONT] = t;
			} else if( 'W' == cmd[i] ) {
				t = diceface[DICETOP];
				diceface[DICETOP] = diceface[DICERIGHT];
				diceface[DICERIGHT] = diceface[DICEBOTTOM];
				diceface[DICEBOTTOM] = diceface[DICELEFT];
				diceface[DICELEFT] = t;
			}
		}
		return;
	}
	int getDiceFace( int face ) {
		return diceface[ face ];
	}
};

int main( )
{
	int t, f, r, l, ba, bo;
	cin>>t>>f>>r>>l>>ba>>bo;
	Dice *dice = new Dice( t, f, r, l, ba, bo );

	string cmd;
	cin>>cmd;
	dice->roll( cmd );
	cout<<dice->getDiceFace( DICETOP )<<endl;

	delete dice;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
//#define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define dbv(v) cerr << #v << "="; for (auto _x : v) cerr << _x << ", "; cerr << endl
#define dba(a, n) cerr << #a << "="; for (int _i = 0; _i < (n); ++_i) cerr << a[_i] << ", "; cerr << endl
typedef long long ll;
typedef long double ld;
const ll MOD = 998244353;
void add(ll& x, ll y) {
	x += y;
	if (x >= MOD) x -= MOD;
}
void sub(ll& x, ll y) {
	x -= y;
	if (x < 0) x += MOD;
}
int main() {
	int n;
	scanf("%d", &n);
	vector<ll> ndp(90001), dp(90001);
	vector<ll> ndp2(90001), dp2(90001);
	dp[0] = dp2[0] = 1;
	ll sum = 0;
	for (int i = 0; i < n; ++i) {
		int a;
		scanf("%d", &a);
		sum += a;
		fill(ndp.begin(), ndp.end(), 0);
		fill(ndp2.begin(), ndp2.end(), 0);
		for (int s = 0; s <= i * 300; ++s) {
			add(ndp[s], dp[s]);
			add(ndp[s], dp[s]);
			add(ndp[s + a], dp[s]);
			add(ndp2[s], dp2[s]);
			add(ndp2[s + a], dp2[s]);
		}
//		for (int s = 0; s <= 10; ++s) db2(s, ndp[s]);
		swap(dp, ndp);
		swap(dp2, ndp2);
	}
	ll ans = 1;
	for (int i = 1; i <= n; ++i) ans = ans * 3 % MOD;
	for (int i = (sum + 1) / 2; i <= sum; ++i) {
//		db2(i, dp[i]);
		sub(ans, dp[i] * 3 % MOD);
	}
	if (sum % 2 == 0) {
		add(ans, dp2[sum / 2] * 3 % MOD);
	}
	printf("%lld\n", ans);
}

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <math.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> P;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REPR(i, n) for (ll i = (n) - 1; i >= 0; --i)
#define FOR(i, n, m) for (ll i = (n); i < (m); ++i)
#define FORR(i, n, m) for (ll i = (m) - 1; i >= (n); --i)
#define FORE(x, xs) for (auto &x: (xs))
#define ALL(v) v.begin(), v.end()
#define ZERO(a) memset(a,0,sizeof(a))

const int INF = 1000000009;
const int MOD = 998244353;
const int HALF = (MOD + 1) / 2;
const double EPS = 1e-14;
const double PI = acos(-1);

const int MAX = 310;
const int MAX_VAL = MAX*MAX;

int N;
int A[MAX];
ll dp[MAX][MAX_VAL]; // dp[i][x] : i個目まで使ってxをつくるパターン数
ll dp1[MAX_VAL]; // dp1[i] : iをつくるパターン数

ll pw (ll n, ll k) {
    ll r = 1;
    while (k) {
        if (k&1) r = r*n%MOD;
        k >>= 1;
        n = n*n%MOD;
    }
    return r;
}

int main() {
    cin >> N;
    REP (i, N) cin >> A[i];
    ll ans = pw(3, N);
    int sm = A[0];
    dp[0][0] = 2;
    dp[0][A[0]] = 1;
    FOR (i, 1, N) {
        REP (j, sm+1) if (dp[i-1][j] > 0) {
            dp[i][j] = (dp[i][j] + 2*dp[i-1][j]) % MOD;
            dp[i][j+A[i]] = (dp[i][j+A[i]] + dp[i-1][j]) % MOD;
        }
        sm += A[i];
    }
    int half = sm/2;
    if (sm&1) half++;

    FOR (i, half, sm+1) {
        ans = (ans + 3ll * MOD - 3*dp[N-1][i]) % MOD;
    }

    dp1[0] = 1;
    int mx = 0;
    REP (i, N) {
        REPR (j, mx+1) if (dp[j]) {
            dp1[j+A[i]] = (dp1[j+A[i]] + dp1[j]) % MOD;
        }
        mx += A[i];
    }
    if (sm%2 == 0) ans = (ans + 3*dp1[sm/2]) % MOD;

    cout << ans << endl;
}


#include <bits/stdc++.h>
#define debug(x) cout << #x << " = " << x << endl
#define REP(i , n) for(Long i = 0; i < (Long)n ; i++)
#define fore(i, a, b) for (Long i = a, to = b; i < to; i++)
#define foref(i, a, b) for (Long i = b - 1, to = a; i >= to; i--)
#define all(v) v.begin(), v.end()
#define SZ(v) (int)v.size()
#define pb push_back

using namespace std;
typedef long long Long;
typedef pair<Long, Long> Pair;

const int MX=(2e5);
const Long INF=(1e18);
Long A[MX+2];
Long acum[MX+2];
Long acum2[MX+2];
int n;
set<Long> S1,S2;
Long ans = INF;
void solve(vector<Pair> izq,vector<Pair> der){
	fore(i,0,SZ(izq)){
		fore(j,0,SZ(der)){
			vector<Long> asd;
			asd.pb(izq[i].first);
			asd.pb(izq[i].second);
			asd.pb(der[j].first);
			asd.pb(der[j].second);
			//fore(k,0,4)cout<<asd[k]<<(char)(k+1==4?10:32);
			sort(all(asd));
			ans = min(ans,asd[3] - asd[0]);
		}
	}
}

void show(){
	cout<<"der"<<endl;
	for(auto x:S1){
		cout<<x<<" ";
	}
	cout<<endl;
	cout<<"izq"<<endl;
	for(auto x:S2){
		cout<<x<<" ";
	}
	cout<<endl;
}

void prueba(Long current){
	//show();
	Long search1 = current/2;
	auto it = S1.lower_bound(search1);
	vector<Pair> izq;
	if(it!=S1.end()){
		Long p = (*it);
		Long q = current - p;
		izq.pb({p,q});
	}
	if(it!=S1.begin()){
		it--;
		Long p = (*it);
		Long q = current - p;
		izq.pb({p,q});
	}

	Long total = acum[n-1] - current;
	Long search2 = total/2;
	auto it2 = S2.lower_bound(search2);
	vector<Pair> der;
	if(it2!=S2.end()){
		Long p = (*it2);
		Long q = total - p;
		der.pb({p,q});
	}
	if(it2!=S2.begin()){
		it2--;
		Long p = (*it2);
		Long q = total - p;
		der.pb({p,q});
	}
	solve(izq,der);
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n;
	fore(i,0,n) cin>>A[i];
	acum[0] = A[0];
	fore(i,1,n) acum[i]=A[i] + acum[i-1];

	acum2[n-1] = A[n-1];
	foref(i,0,n-1){
		acum2[i] = acum2[i+1] + A[i];
	}
	fore(i,0,n) S2.insert(acum2[i]);

	fore(i,0,n){
		S1.insert(acum[i]);
		S2.erase(acum2[i]);
		if(i>0 && i<n-2)prueba(acum[i]);
	}
	cout<<ans<<endl;

	return 0;
}

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

typedef unsigned long ul;
typedef array<ul, 6> dice;

bool f(int n)
{
    static const array<dice, 6> tbl = {
            0,1,2,3,4,5, 1,5,2,3,0,4, 2,1,5,0,4,3,
            3,1,0,5,4,2, 4,0,2,3,5,1, 5,1,3,2,4,0};
    dice d;
    map<ul, bool> db;
    array<ul, 24> r;

    while (n--) {
        for (ul &a: d)
            cin >> a;
        int i = 0;
        for (dice t: tbl) {
            for (int j = 0; j < 4; j++) {
                ul u = 0;
                for (int k = 0; k < 6; k++)
                    u += (d[t[k]] + 1) << k * 10;
                if (db.count(u))
                    return true;
                r[i * 4 + j] = u;
                ul x = t[1]; t[1] = t[2]; t[2] = t[4]; t[4] = t[3]; t[3] = x;
            }
            i++;
        }
        for (ul x : r)
            db[x] = true;
    }
    return false;
}

int main()
{
    int n;
    cin >> n;
    cout << (f(n) ? "No" : "Yes") << endl;
}


#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <deque>     // deque
#include <iomanip>   //setprecsion
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <utility>   // pair, make_pair
#include <vector>    // vector

#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cmath>  //sin, cos, tan

using namespace std;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define blank(ans) cout << (ans) << " ";
#define mp(p, q) make_pair(p, q)
#define mt(p, q, r) make_tuple(p, q, r)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef string str;

typedef vector<ll> vll;
typedef vector<ld> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<str> vs;
typedef vector<vector<ll>> vvll;
typedef vector<vector<ld>> vvd;
typedef vector<vector<bool>> vvb;
typedef vector<vector<char>> vvc;
typedef vector<vector<str>> vvs;
typedef vector<pair<ll, ll>> vpll;
typedef vector<tuple<ll, ll, ll>> vtlll;

const ld PI = acos(-1.0);
const ll MAX = 9000000000000000000;
const ll MIN = -9000000000000000000;
const ld DMAX = 4500;
const ld DMIN = -4500;
const ll MOD = 1000000007;

template <typename T>
void fin(T a)
{
  cout << a << endl;
  exit(0);
}

//桁数
ll GetDigit(ll num)
{
  return log10(num) + 1;
}

//約数全列挙
vector<ll> enum_div(ll n)
{
  vector<ll> ret;
  for (ll i = 1; i * i <= n; ++i)
  {
    if (n % i == 0)
    {
      ret.push_back(i);
      if (i * i != n)
      {
        ret.push_back(n / i);
      }
    }
  }
  sort(begin(ret), end(ret));
  return ret;
}

//累乗(xのn乗%mod)
ll modpow(ll x, ll n, ll mod = MAX)
{
  ll res = 1;
  while (n > 0)
  {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

//階乗
ll factorial(ll n)
{
  if (n > 0)
    return n * factorial(n - 1);
  else
    return 1;
}

ll modinv(ll a, ll mod)
{
  ll b = mod, u = 1, v = 0;
  while (b)
  {
    ll t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= mod;
  if (u < 0)
    u += mod;
  return u;
}

ll gcd(ll a, ll b)
{
  return b ? gcd(b, a % b) : a;
}

ll lcm(ll a, ll b)
{
  return a * b / gcd(a, b);
}

void Main()
{
  str s;
  cin >> s;
  set<char> flag;
  rep(i, s.size())
  {
    flag.insert(s[i]);
  }
  for (char i = 'a'; i <= 'z'; i++)
  {
    if (!flag.count(i))
      fin(s + i);
  }
  if (s == "zyxwvutsrqponmlkjihgfedcba")
    fin(-1);
  vc SS;
  SS.pb(s[s.size() - 1]);
  s.pop_back();
  while(1)
  {
    sort(all(SS));
    for(ll i = 0;i < SS.size();i++)
    {
      if(s[s.size() - 1] < SS[i])
      {
        s.pop_back();
        cout << s << SS[i] << endl;
        return;
      }
    }
    SS.pb(s[s.size() - 1]);
    s.pop_back();
  }
}

//前処理
int main()
{
  //ios::sync_with_stdio(false);
  //cin.tie(0);
  cout << fixed << setprecision(20); //高精度少数表示
  //cout << setfill('0') << internal << setw(4); 0埋め４桁
  Main();
  return 0;
}

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

void input(int& rnTrgSu, int& rnTrgKin)
{
  cin >> rnTrgSu >> rnTrgKin;
}

void searchMoney(int nTrgSu, int nTrgKin)
{
  vector<int> cnvnMoney{10000, 5000, 1000};

  int nCalKin;
  int i, j;
  for (i = 0; i <= nTrgSu; i++)
  {
    for (j = 0; j <= (nTrgSu - i); j++)
    {
      nCalKin = nTrgKin;

      nCalKin -= cnvnMoney[0] * i;
      nCalKin -= cnvnMoney[1] * j;
      nCalKin -= cnvnMoney[2] * (nTrgSu - i - j);
      if ( !nCalKin )
        break;
    }
    if ( !nCalKin )
      break;
  }
  if ( !nCalKin )
    cout << i << " " << j << " " << (nTrgSu - i - j) << endl;
  else
    cout << -1 << " " << -1 << " " << -1 << endl;
}

int main()
{
  int nTrgSu, nTrgKin;

  input(nTrgSu, nTrgKin);
  searchMoney(nTrgSu, nTrgKin);

  return 0;
}


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

#define li          long long int
#define uli         unsigned long long int
#define rep(i,to)   for(li i=0;i<((li)(to));i++)
#define repp(i,start,to)    for(li i=(li)(start);i<((li)(to));i++)
#define pb          push_back
#define sz(v)       ((li)(v).size())
#define bgn(v)      ((v).begin())
#define eend(v)     ((v).end())
#define allof(v)    (v).begin(), (v).end()
#define dodp(v,n)       memset(v,(li)n,sizeof(v))
#define bit(n)      (1ll<<(li)(n))
#define mp(a,b)     make_pair(a,b)
#define rin rep(i,n)
#define EPS 1e-12
#define ETOL 1e-8
#define MOD 1000000007
typedef pair<li, li> PI;

#define INF bit(60)

#define DBGP 1

#define idp if(DBGP)
#define F first
#define S second
#define p2(a,b)     idp cout<<a<<"\t"<<b<<endl
#define p3(a,b,c)       idp cout<<a<<"\t"<<b<<"\t"<<c<<endl
#define p4(a,b,c,d)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<endl
#define p5(a,b,c,d,e)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<endl
#define p6(a,b,c,d,e,f)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<endl
#define p7(a,b,c,d,e,f,g)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<endl
#define p8(a,b,c,d,e,f,g,h)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<endl
#define p9(a,b,c,d,e,f,g,h,i)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<"\t"<<i<<endl
#define p10(a,b,c,d,e,f,g,h,i,j)        idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<"\t"<<i<<"\t"<<j<<endl
#define foreach(it,v)   for(__typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it)
#define p2p(x)      idp p2((x).F, (x).S)
#define dump(x,n)   idp{rep(i,n){cout<<x[i]<<" ";}puts("");}
#define dump2(x,n)  idp{rep(i,n){cout<<"["<<x[i].F<<" , "<<x[i].S<<"] ";}puts("");}
#define dumpi(x)    idp{foreach(it, x){cout<<(*it)<<" ";}puts("");}
#define dumpi2(x)   idp{foreach(it, x){cout<<"["<<(it)->F<<" , "<<(it)->S<<"] ";}puts("");}

#define read2d(a,w,h)   rep(i,h)rep(j,w)cin>>a[i][j]
#define dump2d(a,w,h)   rep(i,h){rep(j,w)cout<<a[i][j]<<" ";puts("");}

typedef pair<li, li> PI;

li n, m;
li a[333][333];

inline void process(deque<li>& dq, set<li>& banned) {
    while (!dq.empty()) {
        if (banned.count(dq.front()) > 0) {
            dq.pop_front();
        } else {
            return;
        }
    }
}

inline bool check(li x) {
    vector<deque<li>> qs(n);
    rin{
        rep(j, m) {
            qs[i].pb(a[i][j]);
        }
    }
    set<li> banned;
    while (true) {
        map<li, li> cnt;
        rin{
            process(qs[i], banned);
            if (qs[i].empty()) {
                return false;
            }
            cnt[qs[i].front()]++;
        }
        bool ok = true;
        for (auto& kv : cnt) {
            if (kv.S > x) {
                banned.insert(kv.F);
                ok = false;
            }
        }
        if (ok) {
            return true;
        }
    }
    return false;
}

int main() {
    cin >> n >> m;
    rin{
        rep(j, m) {
            cin >> a[i][j];
            a[i][j]--;
        }
    }
    li l = 0;
    li r = n;
    while (l + 1 < r) {
        li med = (l + r) / 2;
        if (check(med)) {
            r = med;
        } else {
            l = med;
        }
    }
    cout << r << endl;

    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int main(){
  int road1[111][111];
  int road2[111][111];
  int n,m;
  int a,b,cost,time;
  int p,q,r;
  int x;
  while(1){
    for(int i=0;i<111;i++){
      for(int j=0;j<111;j++){
	road1[i][j]=road2[i][j]=111111111;
      }
    }
    cin >> n >> m;
    if(n==0 && m==0)break;
    for(int i=0;i<n;i++){
      cin >> a >> b >> cost >> time;
      road1[a][b]=road1[b][a]=cost;
      road2[a][b]=road2[b][a]=time;
    }

    for(int k=1;k<=m;k++){
      for(int i=1;i<=m;i++){
	for(int j=1;j<=m;j++){
	  if(road1[i][j]>road1[i][k]+road1[k][j]){
	    road1[i][j]=road1[i][k]+road1[k][j];
	  }
	  if(road2[i][j]>road2[i][k]+road2[k][j]){
	    road2[i][j]=road2[i][k]+road2[k][j];
	  }
	}
      }
    }

    cin >> x;
    for(int i=0;i<x;i++){
      cin >> p >> q >> r;
      cout << (r==0 ? road1[p][q] : road2[p][q]) << endl;
    }
  }
}

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


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

#define LL long long
#define DD long double

#define M 1000000007
#define INF 1e18

void setIO(string s = "") {
  cin.sync_with_stdio(0); cin.tie(0);
  if (s.size()) s += ".in", freopen(s.c_str(), "r", stdin);
}

int tt = 1;
void solve() {
  set<int> nums;
  int a;
  while (cin >> a) nums.insert(a);
  cout << ((nums.size() == 2) ? "Yes" : "No") << endl;
}

int main() {
  setIO();
  while (tt--) {
    solve();
  }
  return 0;
}

#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <math.h>
#define fi first
#define se second
#define rep(i,n) for(ll i=0;i<(n);i++)
#define rrep(i,n) for(ll i = 1; i <= (n); ++i)
#define drep(i,n) for(ll i = (n)-1; i >= 0; --i)
#define all(v) v.begin(),v.end()
#define len(x) (ll)(x).length()
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define pb push_back
#define sz(x) (ll)(x).size()
#define v(T) vector<T>
#define vv(T) vector<vector<T>>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
ll gcd(ll a,ll b){if(a%b==0){return b;}else{return(gcd(b,a%b));}}
ll lcm(ll a,ll b){return a*b/gcd(a,b);}
const int INF=1e9;
const ll MX = 1e18;
const int MOD=INF+7;
const int di[] = {-1,0,1,0};
const int dj[] = {0,-1,0,1};
const double PI=acos(-1);

int main() {
  vi a(3);
  map<int,int> mp;
  rep(i,3) {
    cin>>a[i];
    mp[a[i]]++;
  }
  bool ja=true;
  for(auto x:mp){
    if(x.second==2)ja=false;
  }
  if(ja) cout<<"No"<<endl;
  else cout<<"Yes"<<endl;
}


#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR(i,k,n) for(int(i)=(k);(i)<(n);++(i))
#define rep(i,n) FOR(i,0,n)
#define all(v) begin(v),end(v)
#define debug(x) std::cerr<<#x<<": "<<x<<"\n"
#define debug2(x,y) std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<"\n"
#define debug3(x,y,z) std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<", "<<#z<<": "<<z<<"\n"

using ll=long long;
using vi=std::vector<int>;
using vvi=std::vector<vi>;
using vll=std::vector<ll>;
using vvll=std::vector<vll>;
template<typename T> using vvec=std::vector<std::vector<T>>;
template<typename T>
auto make_v(size_t sz){return std::vector<T>(sz);}
template<typename T,typename... Ts>
auto make_v(size_t sz,Ts...ts){return std::vector<decltype(make_v<T>(ts...))>(sz,make_v<T>(ts...));}
template<typename T>
void fill_v(T&var,const T&x){var=x;}
template<typename V,typename T>
void fill_v(V&v,const T&x){for(auto&& w:v){fill_v(w,x);}}

template<typename T> std::ostream& operator<<(std::ostream&s,const std::vector<T>&v){
  int sz=v.size();s<<"\n";rep(i,sz){s<<v[i];if(i<sz-1){s<<"\t";}}s<<"\n";return s;}
template<typename T> std::ostream& operator<<(std::ostream&s,const std::vector<std::vector<T>>&v){
  for(auto&& w:v){s<<w;}return s;}
template<typename T> std::ostream& operator<<(std::ostream&s,const std::deque<T>&v){
  int sz=v.size();s<<"\n";rep(i,sz){s<<v[i];if(i<sz-1){s<<"\t";}}s<<"\n";return s;}
template<typename T> std::ostream& operator<<(std::ostream&s,const std::deque<std::deque<T>>&v){
  for(auto&& w:v){s<<w;}return s;}
template<typename T> std::ostream& operator<<(std::ostream&s, const std::set<T>&v){
  s<<"\n";for(auto&& elm:v){s<<elm<<"\t";}s<<"\n";return s;}

inline void scan(int&a){scanf("%d",&a);}
inline void scan(ll&a){scanf("%lld",&a);}
inline void scan(char&a){scanf(" %c",&a);}
inline void scan(double&a){scanf("%lf",&a);}
inline void scan(std::string&s){char BUF[3000000];scanf(" %s",BUF);s=std::string(BUF);}
template<typename T>
inline void scan(std::vector<T>&v){for(auto&& sv:v){scan(sv);}}
template<typename First,typename...Args>
inline void scan(First&f,Args&...args){scan(f);scan(args...);}
inline void print(int a){printf("%d\n",a);}
inline void print(ll a){printf("%lld\n",a);}
inline void print(double a){printf("%.12f\n",a);}
inline void print(std::string s){std::cout<<s<<"\n";}

using namespace std;

int mod = 1000000007;

ll calc(int x, set<int>& s) {
  if (s.empty()) {
    return x;
  }
  auto s_ = s;
  ll ans = 0;
  for (int elm : s) {
    s_.erase(elm);
    ans = (ans + calc(x % elm, s_)) % mod;
    s_.insert(elm);
  }
  return ans;
}

ll po(ll k, ll x) {
  if (x == 0) {
    return 1;
  }
  if (x == 1) {
    return k % mod;
  }
  ll y = po(k, x/2);
  y = y * y % mod;
  if (x % 2 == 1) {
    y = y * k % mod;
  }
  return y;
}

int n;
vi s;
vvll dp;
vll frac, finv;
ll solve(int m, int x) {
  if (dp[m][x] >= 0) {
    return dp[m][x];
  }
  if (m == 0) {
    dp[m][x] = x;
    return x;
  }
  ll ret = 0;
  rep (i, m) {
    ret = (ret + solve(i, x % s[i]) * frac[m-1] % mod * finv[i] % mod) % mod;
  }
  dp[m][x] = ret;
  return ret;
}

int main() {
  int x;
  scan(n, x);
  s.resize(n);
  scan(s);
  sort(all(s));
  int n_max = 100001;
  dp.assign(n+1, vll(n_max, -1));

  frac.resize(n_max);
  finv.resize(n_max);
  frac[0] = 1;
  finv[0] = 1;
  FOR (i, 1, n_max) {
    frac[i] = frac[i-1] * i % mod;
    finv[i] = po(frac[i], mod-2);
  }

  ll ans = solve(n, x);
  print(ans);

  return 0;
}

#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INFLL LLONG_MAX // 9223372036854775807
#define EPS 1e-9
#define rep(i, n) for(ll i=0, i##_len=(ll)(n); i<i##_len; i++)
#define repf(i, n) for(ll i=1, i##_len=(ll)(n+1); i<i##_len; i++)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init() std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout<<fixed<<setprecision(15);
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 namespace std;

int main(){
    Init();
    ll n, x; cin >> n >> x;
    vi a(n);
    rep(i, n) cin >> a[i];
    sort(all(a));
    ll ans = 0;
    for(ll i=0; i<n; i++){
        if(x >= a[i]){
            if(i == n-1){
                x -=a[i];
                if(x == 0) ans++;
            }
            else{
                x -= a[i];
                ans++;
            }
        }
        else break;
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define MOD 1000000007

int main(){

  int T,A,n;
  cin >> n >> T >> A;
  T--,A--;
  vector<vector<int>> v(n);
  rep(i,n-1){
    int a,b;cin >> a >> b;
    a--,b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  ll res = 0;
  bool is = true;
  vector<int> next(n,T);
  {
    vector<int> dt(n,-1);
    dt[T] = 0;
    queue<int> que;
    que.push(T);
    while(!que.empty()){
      int ov = que.front();que.pop();
      for(int nv : v[ov]){
        if(dt[nv] != -1)continue;
        next[nv] = ov;
        dt[nv] = dt[ov] + 1;
        que.push(nv);
      }
    }
    if(dt[A] & 1){
      int d = dt[A];
      while(dt[next[A]] >= d-d/2)A = next[A];
      T = A;
      while(dt[next[T]] >= d/2 && next[T] != T)T = next[T];
      res += d/2;
    }else{
      int d = dt[A];
      while(dt[next[A]] >= d-(d-2)/2)A = next[A];
      T = A;
      while(dt[next[T]] >= (d-2)/2 && next[T] != T)T = next[T];
      res += (d-2)/2;
      is = false;
    }
  }
  int L = -1;
  {
    for(int ov : v[T])for(int nv : v[ov])if(nv == A){
      L = ov;
      break;
    }
  }
  queue<int> que;
  que.push(T);
  vector<int> d(n,-1);
  d[T] = 0;
  while(!que.empty()){
    int ov = que.front();que.pop();
    for(int nv : v[ov]){
      if(nv == A || d[nv] != -1 || nv == L)continue;
      d[nv] = d[ov]+1;
      que.push(nv);
    }
  }
  int K = T;
  rep(i,n)if(d[i] > d[K])K = i;
  if(is)res += d[K];
  else res += d[K]+1;
  cout << res << endl;

  /*////////////
  cout << "L :" << L << endl;
  printf("T :%d A :%d\n",T+1,A+1);

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

  return 0;
}
/*
7
4 7
1 2
1 3
2 4
2 5
3 6
3 7
*/

#include <cstdio>
#include <cmath>
using namespace std;
const int N=1e5+5;
const int E=N<<1;
const int mod=1e9+7;
int n,u,v;
int a[N],b[N];
int d1[N],d2[N];
int h[N],nx[E],to[E];
int ec;
int ans;
void ae(int u,int v)
{
	ec++;
	nx[ec]=h[u];
	to[ec]=v;
	h[u]=ec;
}
void dfs(int u,int f,int d[])
{
	d[u]=d[f]+1;
	for (int i=h[u];i;i=nx[i])
		if (to[i]!=f)
			dfs(to[i],u,d);
}
int main()
{
	scanf("%d%d%d",&n,&u,&v);
	for (int i=1;i<n;i++)
	{
		scanf("%d%d",&a[i],&b[i]);
		ae(a[i],b[i]);
		ae(b[i],a[i]);
	}
	dfs(u,0,d1);
	dfs(v,0,d2);
	for (int i=1;i<=n;i++)
		if (d1[i]<d2[i]&&d2[i]-2>ans)
			ans=d2[i]-2;
	printf("%d\n",ans);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<ctime>
#include<map>
#include<bitset>
#include<set>
#define LL long long
#define mp(x,y) make_pair(x,y)
#define pll pair<long long,long long>
#define pii pair<int,int>
using namespace std;
inline LL read()
{
	LL f=1,x=0;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int stack[20];
template<typename T>inline void write(T x)
{
	if(x<0){putchar('-');x=-x;}
    if(!x){putchar('0');return;}
    int top=0;
    while(x)stack[++top]=x%10,x/=10;
    while(top)putchar(stack[top--]+'0');
}
template<typename T>inline void pr1(T x){write(x);putchar(' ');}
template<typename T>inline void pr2(T x){write(x);putchar('\n');}
const int mod=998244353;
const int MAXN=3000005;
int pre[MAXN],inv[MAXN];
int C(int n,int m)
{
	if(n<m)return 0;
	return 1LL*pre[n]*inv[m]%mod*inv[n-m]%mod;
}
int n,m;
void ad(int &x,int y){x+=y;if(x>=mod)x-=mod;}
void dl(int &x,int y){x-=y;if(x<0)x+=mod;}
int pow_mod(int a,int b)
{
	int ret=1;
	while(b)
	{
		if(b&1)ret=1LL*ret*a%mod;
		a=1LL*a*a%mod;b>>=1;
	}
	return ret;
}
int main()
{
	pre[0]=1;for(int i=1;i<MAXN;i++)pre[i]=1LL*pre[i-1]*i%mod;
	inv[MAXN-1]=pow_mod(pre[MAXN-1],mod-2);
	for(int i=MAXN-2;i>=0;i--)inv[i]=1LL*inv[i+1]*(i+1)%mod;
	n=read();m=read();
	int ans=0;
	for(int i=0;i<=m;i++)
		if(!((3*m-i)&1))
			ad(ans,1LL*C(n,i)*C(((3*m-i)>>1)+n-1,n-1)%mod);
//	dl(ans,1LL*n*C(m-2+n-1,n-1)%mod);
	dl(ans,1LL*n*C(m-1+n-1,n-1)%mod);
	pr2(ans);
	return 0;
}


#include <iostream>
#include <algorithm>

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

using namespace std;

int n;
int p[100];

int main(){
	cin >> n;
	rep(i, n * (n - 1) / 2){
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		--a;
		--b;
		if(c == d){
			--p[a];
			--p[b];
		}
		else{
			if(c < d){
				swap(a, b);
			}
			p[a] -= 3;
		}
	}

	vector<int> v(p, p + n);
	sort(v.begin(), v.end());
	rep(i, n){
		cout << lower_bound(v.begin(), v.end(), p[i]) - v.begin() + 1 << endl;
	}
	return 0;
}

#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=1;i<=n;i++)
#define drep(i,n) for(int i=n;i>=0;i--)
#define INF 100000005
#define MAX 100001
#define mp make_pair
#define pb push_back
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

pair<int,int> a[101];
int b[101];
bool comp(pair<int,int> x, pair<int,int> y){
	return x.fi > y.fi;
}

int main(){
	int n, m, t[4], p, s = INF;
	scanf("%d", &n);
	m = (n*(n-1))/2;
	rrep(i,n)a[i].se = i;
	rep(i,m){
		rep(j,4)scanf("%d", &t[j]);
		if(t[2] > t[3])a[t[0]].fi += 3;
		if(t[2] < t[3])a[t[1]].fi += 3;
		if(t[2] == t[3])a[t[0]].fi++, a[t[1]].fi++;
	}
	sort(a+1, a+n+1, comp);
	for(int i = 1;i <= n;i++){
		if(s > a[i].fi){
			s = a[i].fi;
			p = i;
		}
		a[i].fi = p;
		b[a[i].se] = i;
	}
	rrep(i,n)printf("%d\n", a[b[i]].fi);
	return 0;
}


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

int main(){
    int n, m;
    cin >> n >> m;
    vector<int> c(n+2);
    rep(i,m){
        int l, r;
        cin >> l >> r;
        c[l]++;
        c[r+1]--;
    }
    rep(i,n+1) c[i+1] += c[i];
    int ans = 0;
    rep(i,n+1) if(c[i] == m) ans++;
    cout << ans << endl;
}

#include <bits/stdc++.h>

using namespace std;

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

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

const long long INF = 1LL << 60;
#define PI 3.14159265359

template<typename T>
void remove(std::vector<T>& vector, unsigned int index)
{
    vector.erase(vector.begin() + index);
}

int gcd(int a, int b){
    if(b == 0) return a;
    else return gcd(b, a%b);
}

long long lcm (int a, int b){
    return  (long long) a*b /gcd(a,b);
}

long long GCD(long long a, long long b) { return b ? GCD(b, a%b) : a; }

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

    int low = 0;
    int high = N;
    rep(i,M){
        int a, b;
        cin >> a >> b;

        if(low<a)low = a;
        if(b<high)high = b;
    }

    if(high-low<0)cout << 0 << endl;
    else cout << high-low+1<< endl;
}


#include<functional>
#include<iostream>
#include<vector>
#include<string>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template<typename T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return 0;}
template<typename T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return 0;}

template <typename T>
class BIT {
    // 1-indexed
    private:
        size_t node_size_;
        T identity_;
        function<T(T, T)> operation_;
        function<T(T, T)> update_;
        vector<T> node_;
    public:
        BIT(size_t node_size, T identity, function<T(T, T)> operation, function<T(T, T)> update):
            node_size_(node_size), identity_(identity), operation_(operation), update_(update), node_(vector<T>(node_size + 1, identity)) {}
        void change(int idx, T new_value) {
            node_[idx] = update_(node_[idx], new_value);
            while (idx <= (int)node_size_) {
                idx += idx & -idx;
                if (idx > (int)node_size_) {
                    break;
                }
                node_[idx] = operation_(node_[idx], new_value);
            }
        }
        T query(int idx) {
            T res = identity_;
            while (idx > 0) {
                res = operation_(res, node_[idx]);
                idx -= idx & -idx;
            }
            return res;
        }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, Q;
    cin >> N >> Q;
    BIT<ll> bit(N, 0, [](ll a, ll b) {return a + b;}, [](ll a, ll b) {return a + b;});
    ll a;
    rep(i, 0, N) {
        cin >> a;
        bit.change(i + 1, a);
    }
    int q, p, l, r;
    ll x;
    rep(i, 0, Q) {
        cin >> q;
        if (q) {
            cin >> l >> r;
            cout << bit.query(r) - bit.query(l) << endl;
        } else {
            cin >> p >> x;
            bit.change(p + 1, x);
        }
    }
    return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <atcoder/all>
using ll=long long;
using namespace std;
using namespace atcoder;

int main(){
    int N,Q;
    cin>>N>>Q;
    fenwick_tree<ll> fw(N);
    ll a;
    for(int i=0;i<N;i++){
        cin>>a;
        fw.add(i,a);
    }

    int q,x,y;
    for(int i=0;i<Q;i++){
        cin>>q>>x>>y;
        if(q){
            cout<<fw.sum(x,y)<<endl;
        }else{
            fw.add(x,y);
        }
    }

    return 0;
}


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

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

int n, k;
set<string> st;
string a[10];
bool done[10];

void dfs(string s, int c) {
    if (c == k) {
        st.insert(s);
        return;
    }

    rep(i, n) {
        if (!done[i]) {
            done[i] = true;
            dfs(s + a[i], c + 1);
            done[i] = false;
        }
    }
}

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

    while (cin >> n >> k, n) {
        rep(i, n) cin >> a[i];

        st.clear();
        fill(done, done + 10, false);
        dfs("", 0);
        cout << st.size() << endl;
    }

}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int MOD = 1000000007;
const int iINF = 1001001001;
const long long llINF = 1LL << 60;
map<int, int> mp;
int main()
{
	int n, k;
	cin >> n >> k;
	vector<int> x(n);
	rep(i, n)	cin >> x[i];
	int lower, upper;
	lower = 0;
	upper = k - 1;
	int ans = iINF, tmpans = iINF;
	while (upper < n) {
		tmpans = iINF;
		if (x[lower] <= 0 && x[upper] <= 0)			tmpans = abs(x[lower]);
		else if (0 <= x[lower] && 0 <= x[upper])	tmpans = x[upper];
		else {
			tmpans = 2 * min(abs(x[lower]), abs(x[upper]));
			tmpans += max(abs(x[lower]), abs(x[upper]));
		}
		if (tmpans < ans)	ans = tmpans;
		lower++;
		upper++;
	}
	cout << ans << endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll,ll>;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep2(i,m,n) for(int (i)=(m);(i)<(n);(i)++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
const ll INF = 1LL<<60;
const ll MOD = 1e9+7;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,k;cin>>n>>k;
    vector<ll> x(n);rep(i,n)cin>>x[i];
    ll ans = INF;
    for(int i=0; i+k-1<n; i++){
        ll l = x[i], r = x[i+k-1];
        ll a;
        if(r<0)a=-l;
        else if(l<0)a=min(r-2*l, 2*r-l);
        else a=r;
        ans = min(ans, a);
    }
    cout<<ans<<endl;
    return 0;
}


/**
DufauX-XuafuD
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define reset(x) memset(x,-1,sizeof(x))
#define CheckBit(a,b) (a&(1ll<<b))
#define SetBit(a,b) a=(a|(1ll<<b))
#define UnSetBit(a,b) a=(a&(~(1ll<<b)))
#define maxx 10000006
#define PI 2*acos(0.0)
const long long INF = 2000000000000000000LL;	// 2e18
const int inf = 0x3f3f3f3f;	// 1061109567
const long double EPS = 1e-9;
//bitset<maxx/2>vis;
//bitset<maxx>pr;
//int mobi[maxx+10];
//vector<ll>prime;
//void sieve(){
//    ll x=maxx/2, y=sqrt(maxx)/2;
//    for(ll i=1;i<=y;i++){
//        if(vis[i]==0){
//            for(ll j=(i*(i+1)*2);j<x;j+=(2*i)+1)
//                vis[j]=1;
//        }
//    }
//    prime.push_back(2);
//    for(ll i=3;i<maxx;i+=2)
//        if(vis[i/2]==0)
//        prime.push_back(i);
//}
//void generate_mobius()
//{
//    for(int i=0;i<maxx;i++)mobi[i]=1;
//    for(int i=2;i<maxx;i++){
//        if(!pr[i]){
//            mobi[i]*=-1;
//            for(int j=i+i;j<maxx;j+=i){
//                pr[j]=1;
//                mobi[j]*=(j%(i*i)?-1:0);
//            }
//        }
//    }
//}
//bool isprime(ll a)
//{
//    if(a==1)return false;
//    if(a==2)return true;
//    if(a%2==0)return false;
//    for(ll i=0;i<prime.size() && prime[i]*prime[i]<=a;i++)
//        if(a%prime[i]==0)return false;
//    return true;
//}
/*Used path compression technique*/
/*-------------------------------------*/
//ll finds(ll a)
//{
//    vector<ll>vec;
//    while(parent[a]>0){
//        vec.pb(a);
//        a=parent[a];
//    }
//    for(int i=0;i<vec.size();i++)parent[vec[i]]=a;
//    return a;
//}
//void unions(ll a,ll b)
//{
//    parent[a]+=parent[b];
//    parent[b]=a;
//}
/*------------------------------*/
/* Calculate ( a*b ) %c */
inline ll mulmod(ll a, ll b, ll c) {
    ll ret = 0 , cur = a % c ;
    while(b) { if(b%2) ret=(ret+cur)%c; cur=(cur<<1)%c; b=b>>1; }
    return ret%c;
}
//Calculate (b^p)%m || modular inverse: b^-1 = bigmod(b, m-2, m)
inline ll bigmod(ll b, ll p, ll m){
    ll ret = 1, cur = b % m;
    while(p) { if(p&1) ret=(ret*cur)%m; cur=(cur*cur)%m; p=p>>1; }
    return ret;
}
inline ll modulo(ll a,ll b,ll c) { //Same as big mod, use for numbers over 1e10
    ll ret = 1 , cur = a%c ;
    while(b) { if(b%2) ret=mulmod(ret,cur,c); cur=mulmod(cur,cur,c); b=b>>1; }
    return ret%c ;
}
inline ll power(ll b, ll p){
    ll ret = 1, cur = b;
    while(p) { if(p&1) ret=(ret*cur); cur=(cur*cur); p=p>>1; }
    return ret;
}
ll lcm(ll a,ll b)
{
    return (a/__gcd(a,b))*b;
}
ll gcd(ll a,ll b)
{
    return __gcd(a,b);
}
ll arr[100];
void Dufaux()
{
    ll n,k,sum=0;
    cin>>n>>k;
    for(int i=0;i<n;i++)cin>>arr[i];
    sort(arr,arr+n,greater<ll>());
    for(int i=0;i<k;i++)sum+=arr[i];
    cout<<sum<<endl;
}
int main()
{
    FastIO;
	///sieve();
	///generate_mobius();
	int test=1;
	while(test--){
        Dufaux();
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main()
  {int N, K; cin >> N >> K;
  int L[N]; for (int &l : L) cin >> l; sort(L, L + N);
  cout << accumulate(L + N - K, L + N, 0);}


#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long MX = 1e10 + 10;;
unsigned long long fac[200001];

#include <math.h>
#define PI 3.14159265358979323846264338327950L

long long gcd(long long a, long long b) {
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
long long lcm(long long a, long long b) {
    return (a * b) / gcd(a, b);
}
long long digits(long long m) {
    long long cnt = 0;
    while (m) {
        cnt++;
        m /= 10;
    }
    return cnt;
}

unsigned long long power(unsigned long long x, unsigned long long y){
    unsigned long long res = 1;
    x = x % MOD;

    while (y > 0) {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res * x) % MOD;

        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % MOD;
    }
    return res;
}
unsigned long long modInverse(unsigned long long n) {
    return power(n, MOD - 2);
}

long long nCr(long long n, long long r) {
    if (r > n) return 0;
    return (fac[n] * modInverse(fac[r]) % MOD * modInverse(fac[n - r]) % MOD) % MOD;
}

long long bfs(vector<int> g, queue<int> q) {
    return 1ll;
}

int main() {

    int n, l;
    cin >> n >> l;
    vector<int> a(n);
    long long sum = 0;
    for (int i = 0; i < n; i++) {
        a[i] = l + i;
        sum += a[i];
    }
    int mn = 999999;
    int pos = -1;
    for (int i = 0; i < n; i++) {
        if (abs(a[i]) < mn) {
            mn = abs(a[i]);
            pos = i;
        }
    }
    sum -= a[pos];
    cout << sum;
    return 0;
}

#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 ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define FS              ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll              long long int
#define ld              long double
#define pb              push_back
#define bp              __builtin_popcount
#define sz              size()
#define ff              first
#define ss              second
#define vll             vector<ll>
#define vbool           vector<bool>
#define vpll            vector<pair<ll,ll>>
#define pll             pair<ll,ll>
#define vllv            vector<vector<ll>>
#define setpri(x)       cout<<setprecision(x)<<fixed;
#define all(v)          v.begin(),v.end()
#define allr(v)         v.rbegin(),v.rend()
#define yesr {cout<<"YES"<<endl;return;}
#define nor {cout<<"NO"<<endl;return;}
// getline (std::cin,name);
ll MOD=1e9+7;
ll ceil1(ll n,ll x){return (n-1)/x+(n>0);}

ll gcd(ll a,ll b){return __gcd(a,b);}
ll lcm(ll a,ll b){return (max(a,b)/gcd(a,b))*min(a,b);}

ll pow1(ll n,ll m ,ll mod=MOD );
ll pow2(ll n,ll k);

ll modinv(ll n,ll mod=MOD){  return pow1(n,mod-2,mod);}

bool func(pair<ll,ll> &a,pair<ll,ll> &b ){
        if(a.ff != b.ff)return a.ff < b.ff;
        return a.ss > b.ss;
}
ll const N=(ll)3e2+11;
ll const LG=(ll)log2(N)+1;

void solve(){
        ll i,j,k,l,n,m,x,y,z,r;

        cin>>n>>l;
        ll tt=0;
        for(i=1;i<=n;i++){
                tt+=l+i-1;
        }
        if(n==1){
                cout<<0<<endl;
                return;
        }
        ll ans=-100000;
        for(i=1;i<=n;i++){
                ll t1=tt-l+1-i;
                if(abs(t1-tt)<abs(tt-ans)){
                        ans=t1;
                }
        }
        cout<<ans<<endl;

}
int main(){
        FS;

        ll i,j,k,n,m,x,y,z,q;
        q=1;
        // cin>>q;
        while(q--){
                solve();
        }
        return 0;
}

ll pow1(ll n,ll m,ll mod){
        if(m==0)return 1;

        if(m%2==0)return pow1((n*n)%mod,m/2,mod);

    	return (pow1((n*n)%mod,m/2,mod)*n)%mod;
}

ll pow2(ll n,ll k){
        ll ans=1;
            while(k>0){
                if(k%2==1)ans=ans*n;
                n=n*n;
                k/=2;
            }
            return ans;
}


#include <bits/stdc++.h>
#define REP(i, n) for (long long i = 0; i < (long long)n; i++)
//#define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Vl = vector<ll>;
using vi = vector<int>;
using Graph = vector<vi>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(15); //"for float-base
  // problem"
  string s, t;
  int N;
  cin >> N >> s >> t;
  REP(i, N) {
    if (s.substr(i, N - i) == t.substr(0, N - i)) {
      cout << 2 * N - (N - i) << endl;
      return 0;
    }
  }
  cout << 2 * N << endl;
}

#include <stdio.h>
#include <algorithm>
int k, t;
int a[100];
int main() {
	scanf("%d%d", &k, &t);
	for (int i = 0; i < t; i++) {
		scanf("%d", a + i);
	}
	std::sort(a, a + t);
	k = k - a[t - 1];
	k = a[t - 1] - k - 1;
	if (k < 0)k = 0;
	printf("%d\n", k);
}


#include <iostream>
#include <set>

using namespace std;

int main() {
  long long N, L[2]{};
  cin >> N;
  set<int> S;
  while (cin >> N) {
    if (!S.insert(N).second) {
      S.erase(N);
      if (N > L[0]) L[1] = L[0], L[0] = N;
      else if (N > L[1]) L[1] = N;
    }
  }
  cout << L[0] * L[1] << endl;
}

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

#define int         long long
#define endl        "\n"
#define pii         pair<int, int>

const int mod = 1e9+7;
const int MAXN = 1e5+5;
const int N = 1e5+5;

int fac[N];
int spf[MAXN];

void pre(int p) {
    fac[0] = 1;
    for(int i=1; i<N; i++) {
        fac[i] = (fac[i-1] * i) % p;
    }
}

void sieve()
{
    spf[1] = 1;
    for (int i=2; i<MAXN; i++) spf[i] = i;
    for (int i=4; i<MAXN; i+=2) spf[i] = 2;

    for (int i=3; i*i<MAXN; i++)
    {
        if (spf[i] == i)
        {
            for (int j=i*i; j<MAXN; j+=i) if (spf[j]==j) spf[j] = i;
        }
    }
}

vector<int> getFactorization(int x)
{
    vector<int> ret;
    while (x != 1)
    {
        ret.push_back(spf[x]);
        x = x / spf[x];
    }
    return ret;
}

unsigned long long power(unsigned long long x,
                         int y, int p)
{
    unsigned long long res = 1; // Initialize result

    x = x % p; // Update x if it is more than or
    // equal to p

    while (y > 0) {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res * x) % p;

        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}

// Returns n^(-1) mod p
unsigned long long modInverse(unsigned long long n, int p)
{
    return power(n, p - 2, p);
}

// Returns nCr % p using Fermat's little
// theorem.
unsigned long long nCr(unsigned long long n,
                                 int r, int p)
{
    // Base case
    if (r == 0) return 1;
    return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}

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

void solve(int TC) {
   int n; cin >> n;
   map<int, int> cnt;
   for(int i=0; i<n; i++) {
       int x; cin >> x;
       cnt[x]++;
   }
   vector<int> a;
   for(auto it : cnt) {
       if(it.second >= 2) {
           int times = it.second / 2;
           for(int i=0; i<times; i++) {
               a.push_back(it.first);
           }
       }
   }
   sort(a.rbegin(), a.rend());
   if((int)a.size() < 2) {
       cout << 0; return;
   }
   int ans = a[0]*a[1];
   cout << ans;
}

signed main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cout << fixed << setprecision(12);

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

}


#include <bits/stdc++.h>
#define ALL(a)  (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
#define MOD 1000000007LL

int main(){
  ll H, W, K;
  cin >> H >> W >> K;

  vll count1(W-1, 0); //count1.at(i)...i~i+1に線があるとき
  vll count2(W, 0); //count2.at(i)...i番目の左右に線がないとき(i-1~i, i~i+1)
  for(int tmp = 0; tmp < (1 << (W-1)); tmp++){
    //1が隣あってないか
    bool ok = true;
    for(int i = 0; i < W-2; i++){
      if((tmp >> i) & (tmp >> (i+1))){
        ok = false;
        break;
      }
    }
    if(!ok) continue;

    for(int i = 0; i < W-1; i++){
      if((tmp >> i) & 1) count1.at(i)++;
    }

    for(int i = 0; i < W; i++){
      bool flag = true;
      if(i > 0){
        if((tmp >> (i-1)) & 1) flag = false;
      }
      if(i < W-1){
        if((tmp >> i) & 1) flag = false;
      }
      if(flag) count2.at(i)++;
    }
  }

  vvll dp(H+2, vll(W, 0));
  dp.at(1).at(0) = 1;
  for(int i = 2; i < H+2; i++){
    for(int j = 0; j < W; j++){
      if(j > 0) dp.at(i).at(j) += dp.at(i-1).at(j-1) * count1.at(j-1) % MOD;
      dp.at(i).at(j) %= MOD;
      dp.at(i).at(j) += dp.at(i-1).at(j) * count2.at(j) % MOD;
      dp.at(i).at(j) %= MOD;
      if(j < W-1) dp.at(i).at(j) += dp.at(i-1).at(j+1) * count1.at(j) % MOD;
      dp.at(i).at(j) %= MOD;
    }
  }
  cout << dp.at(H+1).at(K-1) << endl;
}

#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <list>
#include <numeric>
#include <stack>
#include <unordered_map>
#include <map>
#include <set>

using namespace std;

#define sz(a) int((a).size())
#define all(x) x.begin(), x.end()
#define pb push_back
#define endl '\n'
#define watch(x) cout << #x << " : " << x << endl;

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

typedef long long ll;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > ii;
typedef vector<ii> vii;

const int N = int(2e5)+10;
const int K = 10005;
// const int MOD = int(1e9)+7;
const int INF = int(1e9)+5;
const ll INF64 = 1e18;

const ll MOD = 998244353;
inline ll add(ll a, ll b, ll m) { return (((a % m + b % m) % m + m) % m); }
inline ll mul(ll a, ll b, ll m) { return (((a % m * b % m) % m + m) % m); }

ll inv[N],fact[N],ifact[N];

//! Modular Exponentiation.
ll modpow (ll x,ll n) {
    if (n == 0ll) return 1ll;
    ll u = modpow(x,n/2);
    u = mul(u,u,MOD);
    if (n % 2 == 1) u = mul(u,x,MOD);
    return u;
}

void pre () {
    inv[1] = fact[0] = fact[1] = ifact[0] = ifact[1] = 1;
    for (int i=2;i<N;i++) {
        inv[i] = mul(MOD - MOD / i, inv[MOD % i], MOD);
        fact[i] = mul(i * 1ll, fact[i - 1], MOD);
        ifact[i] = mul(inv[i], ifact[i - 1], MOD);
    }
    return;
}

ll nCr (ll n,ll r) {
    if (n >= 0 && r >= 0 && n >= r) {
        return mul(fact[n],mul(ifact[r],ifact[n-r],MOD),MOD);
    }
    return 0;
}

void solve () {
    pre();
    ll n,m,k;cin>>n>>m>>k;
    ll ans = 0;
    // k1 are same as left.
    for (int k1=0;k1<=k;k1++) {
        // n-1Ck1 * m * (m-1) ^ (n-1-k1).
        ll temp = nCr(n-1,k1);
        temp = mul(temp,m,MOD);
        temp = mul(temp,modpow(m-1,n-1-k1),MOD);
        ans = add(ans,temp,MOD);
    }
    cout<<ans<<endl;
}

int main () {
    FastIO
// #ifndef ONLINE_JUDGE
//     freopen("input.txt", "r", stdin);
//     freopen("output.txt", "w", stdout);
// #endif
    int t;
    // cin>>t;
    t = 1;
    while (t--) {
        solve();
    }
    return 0;
}


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

const int64_t MOD = 1000000007;
typedef int64_t ll;
typedef uint64_t ull;

#define FOR(i, start, end) for(uint64_t i=start; i<end; i++)
#define REP(i, n) FOR(i, 0, n)

// 最大公約数gcd
// 最小公倍数lcm=m*n/gcd
uint64_t gcd(uint64_t m, uint64_t n) {
  uint64_t temp;
  while (m % n != 0){
    temp = n;
    n = m % n;
    m = temp;
  }
  return n;
}

uint64_t lcm(uint64_t m, uint64_t n) {
  return (m*n)/gcd(m,n);
}

// vector<vector<uint64_t> > v(n+1, vector<uint64_t>(n+1, 0))
// v[n][k]に組み合わせ数が入る。
void comb(vector<vector <uint64_t> > &v){
  for(uint64_t i = 0;i <v.size(); i++){
    v[i][0]=1;
    v[i][i]=1;
  }
  for(uint64_t k = 1;k <v.size();k++){
    for(uint64_t j = 1;j<k;j++){
      v[k][j]=(v[k-1][j-1]+v[k-1][j]);
    }
  }
}

// 掛け算オーバーフロー判定
bool is_product_overflow(uint64_t a, uint64_t b) {

	uint64_t prod = a * b;

	return (prod / b != a);
}

//素因数分解
void primeFactorization(uint64_t a, list<uint64_t> &factors){ //素因数分解を出力するプログラム
  long i,sq;
  if(a%2==0){ //偶数の場合
    factors.push_back(2);
    primeFactorization(a/2,factors); //2で割った値で再帰
    return;
  }
  sq = sqrt(a);
  for(i=3;i<=sq;i+=2){ //3以上√a以下の奇数の場合
	if(a%i==0){
	  factors.push_back(i);
      primeFactorization(a/i,factors); //割れた値で再帰
      return;
	}
  }
  //偶数でも3以上√a以下の奇数の場合でも割り切れない場合
  if(a!=1){ //aが1でないなら、a自身は素数
    factors.push_back(a);
  }
}

// フェルマーの小定理
// mod. m での a の逆元 a^{-1} を計算する
// (a/b)%m = a*movinv(b,m)%m
int64_t modinv(int64_t a, int64_t m) {
    int64_t b = m, u = 1, v = 0;
    while (b) {
        int64_t t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

// 円周率
// M_PI

// #include <iomanip> // setprecisionを使用するのに必要
// cout << std::fixed << std::setprecision(15) << y << endl;

// 昇順
// priority_queue<int, vector<int>, greater<int> > queue;

ll pCount(int x){
  if(x==0) return 1;
  return pCount(x-1)*2 + 1;
}

ll allCount(int x){
  if(x==0) return 1;
  return allCount(x-1)*2 + 3;
}

signed main() {
  ull n,m;
  cin >> n >> m;

  ll ans = 1;
  for(ll i=1;i*i<=m;i++){
    if(m%i!=0)continue;
    ll j = m/i;
    if(i*n<=m) ans = max(ans,i);
    if(j*n<=m) ans = max(ans,j);
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  vector<int> v;
  int x;
  cin >> x;
  v.push_back(x);
  cin >> x;
  v.push_back(x);
  cin >> x;
  v.push_back(x);
  sort(v.begin(), v.end());
  cout<<v[0] +v[1];
	return 0;
}


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

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

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

  vector<pair<ll,pair<ll,ll>>> p;
  rep(i,n){
    ll s,t,x;
    cin>>s>>t>>x;
    p.pb({s-x,{-1,x}});
    p.pb({t-x,{0,x}});
  }

  rep(i,m){
    ll d;
    cin>>d;
    p.pb({d,{1,i}});
  }

  sort(all(p));

  vector<ll> ans(m);
  multiset<ll> se;

  for(auto w : p){
    ll x=w.fi;
    ll y=w.se.fi;
    ll z=w.se.se;
    if(y==-1) se.insert(z);
    else if(y==0) se.erase(se.lower_bound(z));
    else {
      if(se.empty()) ans[z]=-1;
      else ans[z]=*se.begin();
    }
  }

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

}

#include <bits/stdc++.h>

using namespace std;

#define ii pair<int,int>
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define vii vector<ii>
#define all(X) (X).begin(),(X).end()
#define xx first
#define yy second
#define FOR(i,l,r) for( int i = l-(l>r); i != r-(l>r); i += 1-2*(l>r) )

int main() {
    int n,q; cin>>n>>q;
    vector<pair<int,ii>> v;
    FOR(i,0,n) {
        int s,t,x; cin>>s>>t>>x;
        v.pb({x,{s-x,t-1-x}});
    }
    sort(all(v));
    set<ii> s;
    FOR(i,0,q) {
        int u; cin>>u;
        s.insert({u,i});
    }
    int ans[q];
    memset(ans,-1,sizeof(ans));
    for( auto e : v ) {
        if( !s.size() ) break;
        auto i1 = s.lb({e.yy.xx,-1});
        auto i2 = s.ub({e.yy.yy,n+5});
        auto i = i1;
        vii temp;
        while( i != i2 ) {
            temp.pb(*i);
            i++;
        }
        for( auto ee : temp ) {
            s.erase(ee);
            ans[ee.yy] = e.xx;
        }
    }
    for( auto e : ans ) cout<<e<<"\n";
    return 0;
}


#include <bits/stdc++.h>

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

//#define int long long
//signed main(){
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    string s;
    cin >> s;
    vector<int> cnt(26,0);
    for(int i = 0; i < s.size(); i++) {
        cnt[s[i]-'a']++;
    }
    if(s.size()==26) {
        int idx=25;
        while(idx>0 && s[idx-1]>s[idx]) idx--;
        if(idx==0) {
            cout << "-1" << "\n";
            return 0;
        }

        char e='z'+1;
        for(int i = idx; i < s.size(); i++) if(s[idx-1]<s[i]) e=min(e,s[i]);
        for(int i = 0; i < idx-1; i++) cout << s[i];
        cout << e << "\n";
    }
    else{
        cout << s;
        int idx=0;
        while(cnt[idx]!=0) idx++;
        cout << char('a'+idx) << "\n";
    }

    return 0;
}

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;
#define PI 3.141592653589793

int main(){
  string S; cin >> S;
  int N = S.size(); bool acan=true, bcan=true;
  if(N%2==1){
    cout << "No" << endl; return 0;
  }
  rep(i, N){
    if(i%2==1 && S[i]!='h') acan=false;
    if(i%2==0 && S[i]!='i') acan=false;
  }
  rep(i, N){
    if(i%2==0 && S[i]!='h') bcan=false;
    if(i%2==1 && S[i]!='i') bcan=false;
  }
  if(acan || bcan) cout << "Yes" << endl;
  else cout << "No" << endl;
}


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

int main() {
    int k;
    string s;
    string kq;

    cin >> k;
    cin >> s;

    int dodai = s.length();

    if (k < 1 || k > 100)
        return 0;

    if (dodai < 1 || dodai > 100)
        return 0;

    for (int i = 0; i < k; ++i)
    {
        if (k > dodai)
        {
            cout << s;
            return 0;
        }
        if (s[i] >= 'a' && s[i] <= 'z')
            kq += s[i];
        else
            return 0;
    }

    if (k < dodai)
        kq += "...";

    cout << kq;

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

//#define int long long
using ll = long long;
#define all(v) (v).begin(), (v).end()
#define rep(i,n) for(ll i=0;i<n;i++)
#define reps(i,n) for(ll i=0;i<=n;i++)

ll MOD = 1000000007;
const long long L_INF = 1LL << 60;
const int INF = 2147483647; // 2^31-1
const double PI = acos(-1);

const ll dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

int main(){
    ios_base::sync_with_stdio(false);
    ll k;
    string s;
    cin >> k >> s;
    if(s.size()<=k){
        cout << s << endl;
    }else{
        for(int i=0;i<k;i++){
            cout << s[i];
        }
        cout << "..." << endl;
    }
    return 0;
}


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

using namespace std;

const long long MOD = (long long)1e9 + 7;

int main(){
    int n, m;
    cin >> n >> m;
    vector<long long> x(n), y(m);
    for(int i = 0; i < n; i++){
        cin >> x[i];
    }
    for(int i = 0; i < m; i++){
        cin >> y[i];
    }
    long long a = 0, b = 0;
    for(int i = 1; i < n; i++){
        a += i * x[i] - (n - i) * x[i - 1];
        if(a < 0){
            a += MOD * (long long)((abs(a) + MOD - 1) / MOD);
        }
        a %= MOD;
    }
    for(int i = 1; i < m; i++){
        b += i * y[i] - (m - i) * y[i - 1];
        if(b < 0){
            b += MOD * (long long)((abs(b) + MOD - 1) / MOD);
        }
        b %= MOD;
    }
    cout << (a * b) % MOD << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define inf (ll)1e9
#define mod (ll)(1e9+7)
#define d(x) cerr<<#x<<"="<<x<<endl;
#define p(x) cout<<x<<endl
#define pfix(d,x) cout << fixed << setprecision(d) << x << endl;
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define minel(v) *min_element(all(v))
#define minind(v) distance((v).begin(), min_element(all(v)))
#define maxel(v) *max_element(all(v))
#define maxind(v) distance((v).begin(), max_element(all(v)))
typedef vector<int> vi;

ll n,m,x[100001],y[100001];
ll xsum,ysum;
int main(void){
    cin>>n>>m;
    rep(i,n)cin>>x[i];
    rep(i,m)cin>>y[i];
    rep(i,n){xsum=(xsum+i*x[i]-(n-1-i)*x[i])%mod;d(xsum)}
    rep(i,m){ysum=(ysum+i*y[i]-(m-1-i)*y[i])%mod;d(ysum)}
    p((xsum*ysum)%mod);
    return 0;
}


#include<bits/stdc++.h>
#define mod 1000000007
using namespace std;
int dy[4]={1,0,-1,0};
int dx[4]={0,1,0,-1};
vector<vector<int>> graph;

bool sort_sec(const pair<int,int> &a,const pair<int,int> &b){
   if(a.second<b.second){
      return true;
   }
   return false;
}

void swap(int *a,int *b){
   int temp=*a;
   *a=*b;
   *b=temp;
}

int gcd(int a,int b){
   if(a%b==0) return b;
   return gcd(b,a%b);
}
int digit(int num){
   int cnt=0;
   while(num!=0){
      cnt+=num%10;
      num=num/10;
   }
   return cnt;
}
int main(void){
   int N;
   cin>>N;
   long long x;
   cin>>x;
   vector<long long> a(N);
   for(int i=0;i<N;i++){
      cin>>a[i];
   }
   sort(a.begin(),a.end());
   long long sum=0;
   int ans=0;
   for(int i=0;i<N;i++){
      sum+=a[i];
      if(sum<=x) ans++;
      else break;
   }
   if(sum<x) ans--;
   cout<<ans<<endl;
   return 0;
}

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

typedef long long ll;

int main() {
    int N, K, ans = 1;
    cin >> N >> K;
    rep(i, N) {
        if (ans < K) ans += ans;
        else ans += K;
    }
    cout << ans << "\n";
}


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

int main() {
	int d, g;
	cin >> d >> g;
	vector<pair<int,int>>pc(d);
	for (int i = 0; i < d; i++)cin >> pc.at(i).first >> pc.at(i).second;
	int ans = 1e10;
	for (int bit = 0; bit < (1 << d); bit++) {
		vector<pair<int, int>>pcc = pc;
		vector<int> l(0);
		for (int i = 0; i < d; i++) if (bit & (1 << i))l.push_back(i);
		int res = 0;
		int point = 0;
		for (int i = 0; i < l.size(); i++) {
			point += pcc.at(l.at(i)).second;
			res += pcc.at(l.at(i)).first;
			point += pcc.at(l.at(i)).first * (l.at(i) + 1) * 100;
			pcc.at(l.at(i)).first = 0;
		}
		int ct = d - 1;
		while (true) {
			if (ct == -1)break;
			if (point >= g)break;
			if (pcc.at(ct).first == 0) {
				ct--;
			}
			else {
				pcc.at(ct).first--;
				res++;
				point += (ct + 1) * 100;
			}
		}
		ans = min(ans, res);
	}
	cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, from, to) for (int i = from; i < (to); ++i)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define pb push_back
using ll = long long;
using vin=vector<int>;
using vll=vector<ll>;
using vst=vector<string>;
using P = pair<ll,ll>;
const int inf=1e9+7;
const ll INF=9e18;
template <typename T> bool chmin(T &a, const T& b){if(a > b){a = b;return true;}return false;}
template <typename T> bool chmax(T &a, const T& b){if(a < b){a = b;return true;}return false;}
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };

int d,g;
vin p,c;

bool check(int y){

 bool ans=false;
  vin order(d);
  rep(i,0,d)order[i]=i;
  do{
    int x=y;
    int rest=g;

    rep(i,0,d){
      int now=order[i];
      if(x>=p[now]){
        rest-=p[now]*(now+1)*100;
        rest-=c[now];
        x-=p[now];
      }
      else{
        rest-=x*(now+1)*100;
        x=0;
        break;
      }
    }
    if(rest<=0)ans=1;

  }while(next_permutation(all(order)));

  return ans;
}

int main(){cout<<fixed<<setprecision(20);
		   //二分探索かな？
           //順列探索かな？
           //O(10^6*log1000)*10
           cin>>d>>g;
           p.resize(d);c.resize(d);
           rep(i,0,d)cin>>p[i]>>c[i];
           int ng=0,ok=1000;
           while(ok-ng>1){
            int mid=(ok+ng)/2;
             if(check(mid))ok=mid;
             else ng=mid;
           }
           cout<<ok<<endl;
}


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

int main(){

    int n;
    cin >> n;
    int a[n];
    double ave = 0;
    rep(i,n) {
        cin >> a[i];
        ave += a[i];
    }
    ave /= n;
    double d = 1e5;
    int ans = 0;
    rep(i,n){
        if(d > abs(ave-a[i])){
            ans = i;
            d = abs(ave-a[i]);
        }
    }

    cout << ans << endl;

    return 0;
}

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

int dp[34];
int main(){
int N,x;

  cin >> x;
  dp[0] = 1;
  for(int i = 0; i < 31; i++){

	  dp[i+1] += dp[i];
	  dp[i+2] += dp[i];
	  dp[i+3] += dp[i];

  }

  while(x != 0){

	  cout << dp[x]/3650 + 1 << endl;;

	  cin >> x;
  }

	return 0;
}


#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end())
#define VRSORT(v) sort(v.rbegin(), v.rend())
#define ll long long
#define pb(a) push_back(a)
#define INF 1000000000
#define LINF 1e18
#define MOD 1000000007
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

typedef vector<unsigned int>vec;
typedef vector<vec> mat;

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 add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};

const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const double PI = acos(-1.0);
const double EPS = 1e-10;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    string S;
    cin>>N>>S;

    string mae,ushi;
    mae = S.substr(0,N);
    ushi = S.substr(N,N);

    map<pair<string,string>,ll> mp;
    //後ろ半分の文字列(反転させる)
    for(int i=0;i<(1<<N);i++){
        string a="",b="";
        for(int j=0;j<N;j++){
            if(i>>j & 1) a += ushi[j];
            else b += ushi[j];
        }
        reverse(a.begin(),a.end());
        reverse(b.begin(),b.end());
        auto p = make_pair(a,b);
        ++mp[p];
    }

    ll res = 0;
    for(int i=0;i<(1<<N);i++){
        string a="",b="";
        for(int j=0;j<N;j++){
            if(i>>j & 1) a += mae[j];
            else b += mae[j];
        }
        auto p = make_pair(a,b);
        res += mp[p];
    }
    cout<<res<<endl;
}

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <iomanip>

using namespace std;

typedef long long ll;

const int MAXN = 18;

int n;
int LOG[1 << MAXN];

ll ans;
ll f[2][1 << MAXN][MAXN + 1];
ll g[2][1 << MAXN][MAXN + 1];

string S,s,t;

int popcount(int v)
{
	return v ? popcount(v - (v & -v)) + 1 : 0;
}

int main()
{
	cin >> n >> S;
	LOG[0] = -1;
	for (int i = 1;i < (1 << n);i++)
		LOG[i] = LOG[i >> 1] + 1;
	s = S.substr(0,n);
	reverse(S.begin(),S.end());
	t = S.substr(0,n);
	f[0][0][0] = 1;
	for (int j = 0;j <= n;j++)
	{
		memset(f[~j & 1],0,sizeof(f[~j & 1]));
		memset(g[~j & 1],0,sizeof(g[~j & 1]));
		for (int i = 0,I = 0;i < (1 << n);I = popcount(++i))
		{
			if (j > I)
				continue;
			for (int k = 0;k < n;k++)
			{
				if (f[j & 1][i][k])
				{
					for (int l = LOG[i] + 1;l < n;l++)
						if (t[l] == s[I])
							f[~j & 1][i | (1 << l)][k + (l == k)] += f[j & 1][i][k];
					if (k == LOG[i] + 1)
					{
						for (int l = k + 1;l < n;l++)
							if (t[l] == s[I])
								g[j & 1][i | (1 << l)][k + (l == k)] += f[j & 1][i][k];
					}
					if (t[k] == s[I])
					{
						int K = k + 1;
						while (K < n && (i >> K & 1))
							K++;
						int el = LOG[i] + 1 + (LOG[i] + 1 == k);
						if (K > el)
							g[j & 1][i | (1 << k)][el] += f[j & 1][i][k];
						else
							f[j & 1][i | (1 << k)][K] += f[j & 1][i][k];
					}
				}
				if (g[j & 1][i][k])
				{
					for (int l = LOG[i] + 1;l < n;l++)
						if (t[l] == s[I])
							g[j & 1][i | (1 << l)][k] += g[j & 1][i][k];
					if (t[k] == s[I])
					{
						int K = k + 1;
						while (K < n && (i >> K & 1))
							K++;
						int el = LOG[i] + 1 + (LOG[i] + 1 == k);
						if (K >= el)
							f[~j & 1][i | (1 << k)][el] += g[j & 1][i][k];
						else
							g[~j & 1][i | (1 << k)][K] += g[j & 1][i][k];
					}
				}
			}
		}
		ans += f[j & 1][(1 << n) - 1][n];
	}
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef  long long ll;

int main()
{
    ios::sync_with_stdio(false);
    ll n;
    cin >> n;
    string res;
    while (n>0){
        int i = (n-1) % 26;
        char c = 'a' + i;

        res = c + res;

        n = (n-1) / 26;
    }cout << res;
    return 0;
}

#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yn(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YN(ans) if(ans)printf("YES\n");else printf("NO\n")
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }

int main(){
    INT(n);
    vector<ll> vec(n),vec2(n);
    int zero=0,minus=0;
    ll sum=0;
    rep(i,n){
        in(vec[i]);
        if(vec[i]==0)zero=1;
        else if(vec[i]<0){
            minus++;
        }
        sum+=abs(vec[i]);
        vec2[i]=abs(vec[i]);
    }
    if(minus%2==0||zero==1){
        out(sum);
    }else{
        sort(all(vec2));
        out(sum-2*vec2[0]);
    }
}


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

int main(){
  ll n, m;
  cin >> n >> m;
  ll mx = m / n;
  ll ans;
  repr(i, mx+1){
    if (m % i == 0){
      ans = i;
      break;
    }
  }
  cout << ans << endl;

  return 0;
}

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

// 約数列挙
vector<ll> divisor(ll n) {
    vector<ll> ret;
    for(ll i = 1; i * i <= n; i++) {
        if(n % i == 0) {
            ret.push_back(i);
            if(i * i != n) ret.push_back(n / i);
            }
    }
    sort(begin(ret), end(ret));
    return (ret);
}

int main(){
    ll n, m;
    cin >> n >> m;
    vector<ll> div = divisor(m);

    ll ans = 1;

    for(auto v : div){
        ll k = m / v;
        if (k >= n) ans = max(ans, v);
    }

    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<n; i++)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define fi first
#define se second
long long mo = 1000000007;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
typedef pair<ll,Pll> PlP;
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
template<class A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}cout << "\n";}
ld PI=3.14159265358979323846;

int main(){
    ll N;
    cin >> N;
    vector<ll> a(N), b(N), c(N), d(N);
    vector<Pll> p(N), q(N);
    rep(i,N){
        cin >> a[i] >> b[i];
        p[i] = Pll(a[i],b[i]);
    }
    rep(i,N){
        cin >> c[i] >> d[i];
        q[i] = Pll(c[i],d[i]);
    }
    sort(p.begin(),p.end());
    sort(q.begin(),q.end());
    vector<ll> memo(N);
    ll ans = 0;
    rep(i,N){
        ll Mb = -1;
        ll id = -1;
        rep(j,N){
            if(q[i].fi > p[j].fi && q[i].se > p[j].se && memo[j] == 0){
                if(Mb < p[j].se){
                    Mb = p[j].se;
                    id = j;
                }
            }
        }
        if(Mb != -1){
            memo[id] = 1;
            ans++;
        }
    }
    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=1001001001001001;
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;}
vi level,memo;
vvi v;
ll n;
void bfs(ll s){
    level=vi(n,inf);
    queue<P> q;
    q.push(P(s,0));
    level[s]=0;
    while(!q.empty()){
        auto t=q.front();q.pop();
        rep(i,n)if(v[t.fi][i]){
            if(chmin(level[i],t.se+1))q.push(P(i,t.se+1));
        }
    }
}
ll dfs(ll i,ll t,ll f){
    if(i==t)return f;
    REP(j,memo[i],n){
        memo[i]++;
        if(v[i][j]==0||level[i]>=level[j])continue;
        ll d=dfs(j,t,min(f,v[i][j]));
        if(d>0){
            v[i][j]-=d;
            v[j][i]+=d;
            return d;
        }
    }
    return 0;
}
ll max_flow(ll s,ll t){
    ll flow=0;
    while(true){
        bfs(s);
        if(level[t]==inf)return flow;
        memo=vi(n);
        while(true){
            ll f=dfs(s,t,inf);
            if(f==0)break;
            flow+=f;
        }
    }
    return flow;
}
int main(){
    cin>>n;
    v=vvi(n*2+2,vi(n*2+2));
    vp point(2*n);
    rep(i,2*n)cin>>point[i].fi>>point[i].se;
    rep(i,n)rep(j,n)if(point[i].fi<=point[j+n].fi&&point[i].se<=point[j+n].se){
        v[i+1][j+n+1]++;
    }
    rep(i,n)v[0][i+1]++;
    rep(i,n)v[i+n+1][n*2+1]++;
    n=n*2+2;
    outs(max_flow(0,n-1),-1);
}


/* {{{ Shinobu kawaii */
/*
  ______   __        _                  __
.' ____ \ [  |      (_)                [  |
| (___ \_| | |--.   __   _ .--.   .--.  | |.--.   __   _
 _.____`.  | .-. | [  | [ `.-. |/ .'`\ \| '/'`\ \[  | | |
| \____) | | | | |  | |  | | | || \__. ||  \__/ | | \_/ |,
 \______.'[___]|__][___][___||__]'.__.'[__;.__.'  '.__.'_/

*/
/* }}} */

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

// #define int long long
struct Fast {Fast(){std::cin.tie(0);ios::sync_with_stdio(false);}} fast;

/* cpp template {{{ */

/* short */
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()
#define X real()
#define Y imag()

/* REPmacro */
#define REPS(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (int i = (a); i >= n; --i)
#define DEP(i, n) DEPS(i, n, 0)

/* debug */
#define debug(x) cerr << x << " " << "(L:" << __LINE__ << ")" << '\n';

/* alias */
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using D = double;
using P = complex<D>;

/* const */
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
const D EPS = 1e-9;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

/* func */
inline bool inside(int y, int x, int H, int W) {return y >= 0 && x >= 0 && y < H && x < W;}
inline int in() {int x; std::cin >> x; return x;}
template <typename T> void print(T x) {std::cout << x << '\n';}
template <typename T>
void print(std::vector<T>& v, std::string s = " ") {
  REP(i, v.size()) {
    if (i != 0) std::cout << s;
    std::cout << v[i];
  }
  std::cout << '\n';
}

/* }}} */

int n, m;

void warshall_floyd(vvi& d)
{
  REP(k, m) REP(i, m) REP(j, m) {
    d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
  }
}

signed main()
{
  while (cin >> n >> m, n || m) {
    vvi dc, dt;
    dc = dt = vvi(m, vi(m, INF));
    REP(i, n) {
      int a = in() - 1, b = in() - 1;
      dc[a][b] = dc[b][a] = in();
      dt[a][b] = dt[b][a] = in();
    }
    warshall_floyd(dc);
    warshall_floyd(dt);
    int k = in();
    REP(i, k) {
      int a = in() - 1, b = in() - 1, r = in();
      print(r ? dt[a][b] : dc[a][b]);
    }
  }

  return 0;
}

#include <cstdio>
#include <map>

using namespace std;

typedef pair<int, int> pii;

int c[101][101] = { { 0 } };
int t[101][101] = { { 0 } };
int n, m;

int calculate(int mat[101][101], int from, int to){
	multimap<int, int> locations;
	for(int i = 1; i <= m; i++){
		if(mat[from][i] != 0){
			locations.insert(pii(mat[from][i], i));
		}
	}
	while(!locations.empty()){
		multimap<int, int>::iterator it = locations.begin();
		int d = (*it).first, f = (*it).second;
		if(f == to){ break; }
		for(int i = 1; i <= m; i++){
			if(mat[f][i] != 0 && (d + mat[f][i] < mat[from][i] || mat[from][i] == 0)){
				int dist = d + mat[f][i];
				mat[from][i] = mat[i][from] = dist;
				locations.insert(pii(dist, i));
			}
		}
		locations.erase(it);
	}
	return mat[from][to];
}

int main(){
	while(true){
		scanf("%d%d", &n, &m);
		if(n == 0 || m == 0){ break; }
		for(int i = 0; i <= m; i++){
			for(int j = 0; j <= m; j++){
				c[i][j] = t[i][j] = 0;
			}
		}
		for(int i = 0; i < n; i++){
			int a, b, cost, time;
			scanf("%d%d%d%d", &a, &b, &cost, &time);
			if(c[a][b] == 0 || c[a][b] > cost){ c[a][b] = c[b][a] = cost; }
			if(t[a][b] == 0 || t[a][b] > time){ t[a][b] = t[b][a] = time; }
		}
		int k;
		scanf("%d", &k);
		while(k--){
			int p, q, r;
			scanf("%d%d%d", &p, &q, &r);
			printf("%d\n", calculate((r == 0) ? c : t, p, q));
		}
	}
	return 0;
}


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

int main() {

	int n; cin >> n;
	int p = n + 5;
	vector<ll> num(p, 0);
	ll initial;
	ll maximumValue = 0;
	for (int i = 0; i < n; i++) {
		ll d; cin >> d;
		num[d]++;
		maximumValue = max(maximumValue, d);
		if (i == 0) initial = d;
	}
	if (initial != 0 || 2 <= num[0]) {
		cout << 0 << endl;
		return 0;
	}
	const ll mod = 998244353;
	ll ans = 1;
	for (int i = 2; i <= maximumValue; i++) {
		int tmpCnt = num[i];
		while (tmpCnt--) {
			ans *= num[i - 1];
			ans %= mod;
		}
	}
	cout << ans << endl;
	return 0;
}

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

ll Ep(int x, int y){
  if(y == 1) return x;
  ll r = Ep(x,y/2);
  if(y%2 == 1) return r *r%MOD *x%MOD;
  else return r *r%MOD;
}

int main(){
  int n, x;
  cin >> n;
  cin >> x;
  if(x != 0){
    cout << 0 << endl;
    return 0;
  }
  map<int, int> mp;
  for(int i=1; i<n; ++i){
    cin >> x;
    if(x == 0){
      cout << 0 << endl;
      return 0;
    }
    ++mp[x];
  }
  ll ans = 1;
  for(auto p: mp){
    if(p.first == 1) continue;
    ans *= Ep(mp[p.first-1], p.second);
    ans %= MOD;
  }
  cout << ans << endl;
}


#include <bits/stdc++.h>
#include <ctime>
#define rep(i,a,b) for(int i =a;i<b;i++)
using namespace std;
typedef unsigned long long ll;
inline int cine() {int a; return scanf("%d", &a) ? a : 1e9;}

void solve() {
    int train, bus, mine = 1e9, sum = 0 ;
    rep(i, 0, 2) {
        cin >> train;
        mine = min(mine, train);
    }
    sum += mine;
    mine = 1e9;
    rep(i, 0, 2) {
        cin >> bus;
        mine = min(mine, bus);
    }
    sum += mine;
    cout << sum;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("i.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    solve();
#ifndef ONLINE_JUDGE
    cout << "\nTime Elapsed :" << 0.1 * clock() / CLOCKS_PER_SEC << "s\n";
#endif
}

#include <bits/stdc++.h>
using namespace std;
inline void ci(void){
  return;
}
template <typename First, typename... Rest>
void ci(First& first, Rest&... rest){
  cin >> first;
  ci(rest...);
  return;
}
inline void co(void){
  cout << "\n";
  return;
}
template <typename First, typename... Rest>
void co(First first, Rest... rest){
  cout << first << " ";
  co(rest...);
  return;
}
#define ll long long
#define ld long double
#define st string
#define dl double
#define wh while
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define tp tuple
#define ft first
#define sc second
#define ve vector
#define tos to_string
#define len length
#define rt return 0
#define br break
#define con continue
#define lcm(n, m) (n * m / gcd(n, m))
#define np next_permutation
#define repi(i, m, n) for(int i=(int)(m);i<(int)(n);i++)
#define rep(i, n) repi(i, 0, n)
#define repill(i, m, n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define repll(i, n) repill(i, 0, n)
#define revi(i, m, n) for(int i=(int)(m);i>(int)(n);i--)
#define rev(i, n) revi(i, 0, n)
#define revill(i, m, n) for(ll i=(ll)(m);i>(ll)(n);i--)
#define revll(i, n) revill(i, 0, n)
#define sz(v) ((int)(v).size())
#define each(i, n) for(auto&& i: n)
#define all(n) (n.begin(), n.end())
#define sp(n) cout << fixed << setprecision(n)
// sqrt ルート, int 2*10^9, gcd　最大公約数
// __builtin_popcount bitの個数

int main() {
  int A, B, C, D;
  ci(A, B, C, D);
  int a, b;
  a = min(A, B);
  b = min(C, D);
  co(a + b);
}