using namespace std;
#include<bits/stdc++.h>
//typedef long long   LL;
typedef int   LL;
typedef long long   ll;
typedef   pair<LL,LL>  PII;
typedef   vector< PII > VPII;
typedef   vector<LL>   VI;
typedef   map<string,LL>  MSI;
typedef   map<LL,LL>  MII;
///Print
#define casep(no,val) printf("Case %lld: %lld\n",++no,val)
#define casenl(no) printf("Case %lld:\n",++no)
#define RR(n,m)       scanf("%lld%lld", &n,&m)
#define case(no) printf("Case %lld: ",++no)
#define R(n)       scanf("%lld", &n)
#define P(a)  printf("%lld\n",a)
#define NL printf("\n")
///LOOP
#define FORSTL(x,n) for(__typeof(n.begin()) x=n.begin();x!=n.end();x++)
#define repp(x,n) for(__typeof(n) x=1;x<=(n);x++)
#define reps(i,x) for(int  i=0;i<(x.size());i++)
#define rep(x,n) for(__typeof(n) x=0;x<(n);x++)
///Shortcut
#define Unique(store) store.resize(unique(store.begin(),store.end())-store.begin())
#define fastinput ios_base::sync_with_stdio(false); cin.tie(false);
#define mem(ar,value) memset(ar,value,sizeof(ar))
#define all(x) x.begin(),x.end()
#define len(s) s.size()
#define mp make_pair
#define pb push_back
#define SS second
#define FF first
///Binary_search
#define LB(a,x) (lower_bound(all(a),x)-a.begin())
#define UB(a,x) (upper_bound(all(a),x)-a.begin())
///Min AND Max
#define isEq(a,b) (fabs((a)-(b))<eps)
#define MIN3(a,b,c) min(a,min(b,c))
#define MAX3(a,b,c) max(a,max(b,c))
#define PI acos(-1.0)
#define EPS (1e-9)
///DEBUG
#define DDD(x,y,z) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<" | "#z" ="<<z<<endl;
#define DD(x,y) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<" | "#y" = "<<y<<endl;
#define D(x) cerr<<__FUNCTION__<<":"<<__LINE__<<": "#x" = "<<x<<endl;

///Gcd and Lcm
template<class T>T gcd(T a,T b){return b == 0 ? a : gcd(b, a % b);}
template<typename T>T lcm(T a, T b) {return a / gcd(a,b) * b;}
///Bigmod && Pow
template<class T>T my_pow(T n,T p){if(p==0)return 1;T x=my_pow(n,p/2);x=(x*x);if(p&1)x=(x*n);return x;} ///n to the power p
template<class T>T big_mod(T n,T p,T m){if(p==0)return (T)1;T x=big_mod(n,p/2,m);x=(x*x)%m;if(p&1)x=(x*n)%m;return x;}
///string to int
template <class T> T extract(string s, T ret) {stringstream ss(s); ss >> ret; return ret;}///
string itos(LL n){string s;while(n){s+=(n%10+48);n/=10;}reverse(all(s));return s;}
LL stoi(string s){LL n=0;rep(i,len(s))n=n*10+(s[i]-48);return n;}
/// geometry
double DEG(double x) { return (180.0*x)/(PI);}
double RAD(double x) { return (x*(double)PI)/(180.0);}
template<typename T> T DIS(T x1,T y1,T x2, T y2){return sqrt( (double)( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ) );}
template<typename T> T ANGLE(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}
template<typename T> LL isLeft(T a,T b,T c) { return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y); }
const  int N=5e5+5,MOD=1e9+7;

//struct info{LL x,y;}arr[N];bool com(info a,info b){return (a.x<b.x) or (a.x==b.x and a.y<b.y);}
LL ar[N],br[N];LL a=0,b=0,c=0,r=0,rr=0,res=0,n,m,t=0,ks=0;
string  s;

struct info

{
    LL x,y;
}T[4*N];

void insert(LL idx,LL st,LL end)
{
    if(st==end)
    {
        T[idx].x=ar[st];
        T[idx].y=st;
        return ;
    }

    LL mid=(st+end)>>1,lf=idx*2,rg=lf+1;
    insert(lf,st,mid);
    insert(rg,mid+1,end);

    if(T[lf].x<T[rg].x)  T[idx]=T[lf];
    else  T[idx]=T[rg];
    //return ;
}

void up(LL idx,LL st,LL end,LL pos,LL val)
{
    if(st==pos  and end==pos)
    {
        T[idx].x=val;
       // T[idx].y=st;
        return ;
    }
    LL mid=(st+end)>>1,lf=idx*2,rg=lf+1;

    if(pos<=mid)
    up(lf,st,mid,pos,val);
    else
    up(rg,mid+1,end,pos,val);
    if(T[lf].x<T[rg].x)  T[idx]=T[lf];
    else  T[idx]=T[rg];
    return ;
}

info read(LL idx,LL st,LL end,LL i,LL j)
{
    if(st==i  and end==j)
    {
        return T[idx];
    }
    LL mid=(st+end)>>1,lf=idx*2,rg=lf+1;
    if(j<=mid)
    return read(lf,st,mid,i,j);
    else if(i>mid)  return read(rg,mid+1,end,i,j);

    else
    {
        info aa=read(lf,st,mid,i,mid);
        info bb= read(rg,mid+1,end,mid+1,j);
        if(aa.x<bb.x)  return aa;
        else  return bb;
    }

}

LL par[N][2];
LL find(LL idx,LL f)
{
    if(idx==par[idx][f])  return idx;
    else    return par[idx][f]=find(par[idx][f],f);
}

int main()
{
  // fastinput;

    cin>>n;  repp(i,n)  cin>>ar[i];

    insert(1,1,n);

   // D(read(1,1,n,1,n).y);

    repp(i,n+1000)  par[i][0]=i;
    repp(i,n+1000)  par[i][1]=i;

    repp(i,n-2)
    {

        LL aa=read(1,1,n,1,n).y;
        LL bb=find(aa,0);
        LL cc=find(aa,1);

       // D(aa);

       // DD(bb,cc);

        LL dd=find(bb-1,0);
        LL ee=find(cc+1,1);

        par[bb][0]=dd;
        par[cc][1]=ee;

       // DD(dd,ee);

        //cout<<endl;

        //if((dd)+(n-ee+1)<=1)  break;

       if(min(ar[dd],ar[ee])==0)  r+=ar[aa];
       else  r+=min(ar[dd],ar[ee]);

        up(1,1,n,aa,10000000);

    }

    cout<<r<<endl;

   return 0;

}


#include<bits/stdc++.h>
using namespace std;
const int N=25*100000;
int dp[50][N+1];
int n;
int a[50];
int val;
int DP(int i,int w){
    if(i==n)return 0;
    if(dp[i][w]!=-1)return dp[i][w];
    int p1=0,p2=0;
    if(w+a[i]<=val)p1=a[i]+DP(i+1,w+a[i]);
    p2=DP(i+1,w);
    return dp[i][w]=max(p1,p2);
}
int main(){
    scanf("%d",&n);
     int sum=0;
    memset(dp,-1,sizeof dp);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
        sum+=a[i];
    }
    val=sum/2;
    int ans=DP(0,0);
    printf("%d %d\n",ans,sum-ans);
}


#include <cstdio>
#include <cstring>

typedef long long ll;
const int mod = 1000000007;

ll pw2[99];

ll f_rlen[99][99];

inline void upda(ll &a, ll b) {
    a = (a + b) % mod;
}

inline void init_f_rlen() {
    pw2[0] = 1;
    for(int i = 1; i <= 91; ++i) pw2[i] = pw2[i - 1] * 2 % mod;
    f_rlen[0][0] = 1;
    for(int rlen = 1; rlen <= 90; ++rlen) for(int rlen1 = 0; rlen1 <= rlen; ++rlen1) {
            if(rlen1) f_rlen[rlen][rlen1] = f_rlen[rlen - 1][rlen1 - 1] * pw2[rlen - rlen1]) % mod;
            if(rlen1 < rlen) f_rlen[rlen][rlen1] = (f_rlen[rlen][rlen1] + f_rlen[rlen - 1][rlen1]) % mod;
        }
}

ll g_pf[44][44];

inline ll calc_prefix(int pf, int rlen) {
    ll ans = 0;
    for(int rlen1 = 0; rlen1 <= rlen; ++rlen1) {
        ll t1ans = 0;
        memset(g_pf, 0, sizeof(g_pf));
        g_pf[0][0] = 1;
        for(int pfmv = 0; (pf >> pfmv); ++pfmv) for(int pfmv1 = 0; pfmv1 <= pfmv; ++pfmv1) {
                if((pf >> pfmv) == 1) t1ans = (t1ans + g_pf[pfmv][pfmv1]) % mod;
                else {
                    upda(g_pf[pfmv + 1][pfmv1], g_pf[pfmv][pfmv1]);
                    if(pf >> pfmv & 1) upda(g_pf[pfmv + 1][pfmv1 + 1], g_pf[pfmv][pfmv1] * pw2[pfmv - pfmv1 + rlen - rlen1]);
                }
            }
        ans = (ans + t1ans * pw2[rlen - rlen1] % mod * f_rlen[rlen][rlen1]) % mod;
    }
    return ans;
}

inline ll calc_ans(int n) {
    if(n == 0) return 1;
    ll ans = 1; // max = 0;
    int len0;
    for(len0 = 0; (1 << (len0 + 1)) <= n; ++len0) ans = (ans + calc_prefix(1, len0)) % mod;
    int t_pf = 1;
    for(int i = len0 - 1; i >= 0; --i) {
        int a = n >> i & 1;
        if(!a) t_pf <<= 1;
        else {
            ans = (ans + calc_prefix(t_pf << 1, i)) % mod;
            t_pf = t_pf << 1 | 1;
        }
    }
    ans = (ans + calc_prefix(n, 0)) % mod;
    return ans;
}

int main() {
    init_f_rlen();
#ifdef TEST
    printf("[DEBUG] calc_ans(3) = %I64d (expected: 5)\n", calc_ans(3));
#else
    int k; scanf("%d", &k);
    printf("%I64d\n", calc_ans(k));
#endif
    return 0;
}


#pragma warning(disable:4996)

#include <iostream>

using namespace std;

enum
{
	Circle = 0,
	Triangle = 1,
	Square = 2
};

int main()
{
	int N;
	int a[105];
	bool inf = false;
	int res = 0;
	cin >> N;
	for (int i = 0; i < N; ++i)
		cin >> a[i];
	for (int i = 1; i < N; ++i)
	{
		switch ((a[i - 1] - 1) * 3 + (a[i] - 1))
		{
		case 1:
			res += 3; break;
		case 2:
			res += 4; break;
		case 3:
			res += 3; break;
		case 5:
			inf = true; break;
		case 6:
			res += 4; break;
		case 7:
			inf = true; break;
		}
	}

	if (inf)
		cout << "Infinite" << endl;
	else
		cout << "Finite" << endl << res << endl;

	//system("pause");
}


#include <bits/stdc++.h>

using namespace std;

unordered_map < long long, int > before;

void Backt(int &cur, long long &cur_cost, int &n, int &fin, vector < int > &cost, long long &best) {

  if (before[cur_cost] > cur) {
    return;
  }
  before[cur_cost] = cur;

  if (cur >= fin) {
    best = min(best, cur_cost);
    return;
  }

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

    cur += (1 << (i - 1));
    cur_cost += cost[i];
    Backt(cur, cur_cost, n, fin, cost, best);
    cur_cost -= cost[i];
    cur -= (1 << (i - 1));
  }
}

int main() {

  ios::sync_with_stdio (false);

  int n, l;
  cin >> n >> l;

  vector < int > cost(n + 1);
  for (int i = 1; i <= n; i ++) {
    cin >> cost[i];
  }

  int cur = 0;
  long long cur_cost = 0;
  long long best = 2e9;
  Backt(cur, cur_cost, n, l, cost, best);
  cout << best << '\n';
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll powr(ll k){
   return 10*pow(10,k-1);
}
int main(){
   ll n,k,a[100];
   scanf("%lld%lld",&n,&k);
   if(k==0){
      printf("%lld",n);
      return 0;
   }
  if(k==1){
     printf("%d",n*10);
     return 1;
  }
   ll d2=2,d5=5,cnt=0,x=n;
   while(true){
      if(n%2==1 && n%5!=0) break;
      if(n%2==0)n/=2;
      if(n%5==0)n/=5;
   }
 printf("%lld",n*powr(k));
}


#include <iostream>

using namespace std;

int gcd(int x, int y)
{
    while(x * y != 0)
        if(x < y)
            y %= x;
        else
            x %= y;
    return x + y;
}

int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n;
    scanf("%d", &n);
    int x = 0;
    int y = 0;
    for(int i = 2; i < n; i++)
    {
        y++;
        int m = n;
        while(m != 0)
        {
            x += m % i;
            m /= i;
        }
    }
    int g = gcd(x, y);
    printf("%d/%d", x / g, y / g);
}


#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstring>
#include <cassert>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <cmath>
#include <set>
#include <map>
#include <functional>
#include <bitset>
#include <iomanip>

using namespace std;
#define     loop(n,i,a)     for(int i=a;i<n;i++)
#define     all(arr,n)      arr,arr+n
#define     allv(v)         (v).begin(),(v).end()
#define     rallv(v)        (v).rbegin(),(v).rend()
#define     m_p             make_pair
#define     ll              long long
#define     vi              vector<int>
#define     vll             vector<ll>
#define		vii 			vector<pair<ll,ll>>
#define     sz(x)           (int)x.size()
#define     pb              push_back
int dx[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int dy[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

int main() {
	string s,t="CODEFORCES";
	cin>>s;
	loop(10,i,0){
		string temp=s.substr(0,i)+s.substr(sz(s)-(sz(t)-i));
		if(temp==t)
			return cout<<"YES",0;
	}
	cout<<"NO";
}


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

int r,c;
int main(){

    while(scanf("%d %d",&r,&c)){
        int sum(0),cnt(0);

        char cake[r+1][c+1];

        for(int i=0;i<r;i++)
            scanf("%s",cake[i]);

        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                    if(cake[i][j]=='.'){
                        cnt++;
                        cake[i][j]='E';
                    }
                    else if(cake[i][j]=='S'){
                        cnt=0;
                        for(int rev=j;rev>=0;rev--){
                            if(cake[i][rev]=='E')cake[i][rev]='.';
                        }
                        break;
                    }
            }
            sum+=cnt;
        }
        for(int j=0;j<c;j++){
            for(int i=0;i<r;i++){
                    if(cake[i][j]=='.'){
                        cnt++;
                        cake[i][j]='E';
                    }
                    else if(cake[i][j]=='S'){
                        cnt=0;
                        for(int rev=i;rev>=0;rev--){
                            if(cake[rev][j]=='E')cake[rev][j]='.';
                        }
                        break;
                    }
            }
            sum+=cnt;

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

    }

}


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


#include<bits/stdc++.h>
#include<vector>
using namespace std;
int main()
{
    int n,m,i=0,c=0,s,b;
    cin>>n>>m;
    int a[n];
    for(i=0;i<n;i++)
    {
        cin>>a[i];
        if(a[i]>=c)
        c=a[i];
    }
        s= (c-m);
    for(i=n-1;i>=0;i--)
    {
        if(a[i]>s)
        {
            b=i+1;
            break;
        }
    }
    cout<<b;
    return 0;
}


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

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#endif

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

	int arr[n];
	for (int i = 0; i < n; i++) {
		arr[i] = i + 1;
	}

	for (int i = 0; i < k; i++) {
		swap(arr[i], arr[n - 1 - i]);
	}

	int64_t count = 0;
	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			if (arr[i] > arr[j])count++;
		}
	}
	cout << count;

	return 0;
}


//*
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <numeric>
#include <functional>
#define MOD 1000000007
#define MAX 0x3f3f3f3f
#define MAX2 0x3f3f3f3f3f3f3f3fll
#define ERR 1e-10
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#pragma warning(disable:4996)
using namespace std;

typedef long long ll;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

struct SAT
{
	int n, m;
	vector<int> edge[3000];
	vector<int> redge[3000];
	int gi;
	int g[3000];
	int bgi;
	int bg[3000];
	vector<int> gmem[3000];
	int check[3000];
	vector<int> ord;
	int rord[3000];
	int ans[3000];

	int getnum(int x)
	{
		if(x > 0) return x-1;
		else
		{
			x*=-1;
			x--;
			return x+n;
		}
	}
	int not(int x)
	{
		if(x >= n) return x-n;
		else return x+n;
	}
	void input()
	{
		int i, j;
		for(i=0;i<m;i++)
		{
			int x, y;
			scanf("%d%d", &x, &y);
			x=getnum(x), y=getnum(y);
			edge[not(x)].push_back(y);
			edge[not(y)].push_back(x);
		}
		for(i=0;i<n*2;i++)
		{
			for(auto e : edge[i]) redge[e].push_back(i);
		}
	}
	void back(int now)
	{
		if(check[now]) return;
		check[now]=1;
		for(auto e : edge[now]) back(e);
		ord.push_back(now);
	}
	void assignfalse(int x)
	{
		if(ans[x] == 1) return;
		ans[x]=0;
		ans[not(x)]=1;
	}
	void rback(int now)
	{
		if(check[now]) return;
		assignfalse(now);
		check[now]=1;
		g[now]=gi;
		gmem[gi].push_back(now);
		for(auto e : redge[now])
		{
			rback(e);
		}
	}
	int scc()
	{
		int i, j;
		for(i=0;i<n*2;i++)
		{
			back(i);
		}
		fill(check, check+n*2, 0);
		reverse(all(ord));
		for(auto e : ord)
		{
			if(check[e]) continue;
			rback(e);
			gi++;
		}
		for(i=0;i<n;i++)
		{
			if(g[i] == g[not(i)]) return false;
		}
		return true;
	}
	void bback(int now)
	{
		if(check[now]) return;
		check[now]=1;
		bg[now]=bgi;
		for(auto e : edge[now]) bback(e);
		for(auto e : redge[now]) bback(e);
	}
	void markans(int x, int y)
	{
		int i, j;
		vector<pii> w;
		w.emplace_back(y, x);
		w.emplace_back(y^1, not(x));
		ans[x]=y;
		ans[not(x)]=y^1;
		for(j=0;j<w.size();j++)
		{
			int val=w[j].first;
			int now=w[j].second;
			if(val == 1)
			{
				for(auto e : edge[now])
				{
					if(ans[e] != -1) continue;
					ans[e]=1;
					w.emplace_back(1, e);
				}
			}
			else
			{
				for(auto e : redge[now])
				{
					if(ans[e] != -1) continue;
					ans[e]=0;
					w.emplace_back(0, e);
				}
			}
		}
	}
	void dodo()
	{
		int i, j, k;
		fill(check, check+n*2, 0);
		for(i=0;i<n*2;i++)
		{
			if(check[i]) continue;
			bback(i);
			bgi++;
		}
		fill(ans, ans+n*2, -1);
		for(i=0;i<n*2;i++) rord[ord[i]]=i;
		fill(check, check+n*2, 0);
		for(i=0;i<n;i++)
		{
			if(ans[i] != -1) continue;
			if(bg[i] == bg[not(i)]) // 확정
			{
				int u=i, v=not(i);
				if(rord[i] > rord[not(i)]) swap(u, v);
				markans(u, 0);
			}
		}
	}
	void rback2(int now)
	{
		if(check[now]) return;
		assignfalse(now);
		check[now]=1;
		for(auto e : redge[now])
		{
			rback2(e);
		}
	}
	void fillremain()
	{
		fill(check, check+n*2, 0);
		for(auto e : ord)
		{
			if(check[e]) continue;
			rback2(e);
		}
	}

	int tclo[2005][2005];
	map<int, int> mq;
	int tn;

	void maketclo()
	{
		int i, j, k;

		for(i=0;i<n*2;i++)
		{
			if(ans[i] == -1)
			{
				mq[i]=tn++;
			}
		}
		for(i=0;i<n*2;i++)
		{
			if(ans[i] != -1) continue;
			for(auto e : edge[i])
			{
				if(ans[e] != -1) continue;
				tclo[mq[i]][mq[e]]=1;
			}
		}
		for(i=0;i<tn;i++) tclo[i][i]=1;
		for(k=0;k<tn;k++) for(i=0;i<tn;i++) for(j=0;j<tn;j++) tclo[i][j]|=tclo[i][k]&tclo[k][j];
	}
} s1, s2;

int main()
{
	int i, j, k, l;
	int n, m1, m2;
	cin>>n>>m1>>m2;
	s1.n=n, s1.m=m1;
	s2.n=n, s2.m=m2;
	s1.input(), s2.input();
	int u1=s1.scc(), u2=s2.scc();
	if(!u1 && !u2) return !printf("SIMILAR");
	else if(u1 && !u2)
	{
		for(i=0;i<n;i++) printf("%d ", s1.ans[i]);
		return 0;
	}
	else if(!u1 && u2)
	{
		for(i=0;i<n;i++) printf("%d ", s2.ans[i]);
		return 0;
	}
	s1.dodo(), s2.dodo();
	for(i=0;i<n;i++)
	{
		if(s1.ans[i] != s2.ans[i])
		{
			if(s1.ans[i] == -1)
			{
				s1.markans(i, s2.ans[i]^1);
				s1.fillremain();
				for(i=0;i<n;i++) printf("%d ", s1.ans[i]);
				return 0;
			}
			else
			{
				if(s2.ans[i] == -1) s2.markans(i, s1.ans[i]^1);
				s2.fillremain();
				for(i=0;i<n;i++) printf("%d ", s2.ans[i]);
				return 0;
			}
		}
	}
	s1.maketclo(), s2.maketclo();
	for(i=0;i<s1.tn;i++)
	{
		for(j=0;j<s1.tn;j++)
		{
			if(s1.tclo[i][j] != s2.tclo[i][j])
			{
				if(s1.tclo[i][j] == 1)
				{
					s2.markans(i, 1);
					s2.markans(j, 0);
					s2.fillremain();
					for(i=0;i<n;i++) printf("%d ", s2.ans[i]);
					return 0;
				}
				else
				{
					s1.markans(i, 1);
					s1.markans(j, 0);
					s1.fillremain();
					for(i=0;i<n;i++) printf("%d ", s1.ans[i]);
					return 0;
				}
			}
		}
	}
	printf("SIMILAR");
	return 0;
}
//*/


#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int N = 2000 + 10;

int n, m1, m2;

struct SAT
{
	int n, m;
	vector<int> adj[N];
	vector<int> new_adj[N];
	bitset<N> reach[N];
	int qin[N]; //0? 1:yes! -1:not!
	int have_sol;
	int flag, totc;
	int instack[N], stack[N], top;
	int low[N], dfn[N], color[N];
	int tarjan(int u) {
		instack[u] = true;
		stack[++ top] = u;
		dfn[u] = low[u] = flag ++;
		for(int e = 0; e < adj[u].size(); ++ e) {
			int v = adj[u][e];
			if (dfn[v] < 0) {
				low[u] = min(low[u], tarjan(v));
			} else if (instack[v]) {
				low[u] = min(low[u], dfn[v]);
			}
		}
		if (dfn[u] == low[u]) {
			int v;
			do {
				v = stack[top --];
				instack[v] = false;
				color[v] = totc;
			} while(v != u);
			totc ++;
		}
		return low[u];
	}
	void construct(int _n, vector<pair<int, int>> edges) {
		n = _n; m = edges.size();
		for(int i = 0; i < m; ++ i) {
			int u = edges[i].first, v = edges[i].second;
			if (u < n) u = -u + n;
			if (v < n) v = -v + n;
			--u, --v;
			adj[u < n ? u + n : u - n].push_back(v);
			adj[v < n ? v + n : v - n].push_back(u);
		}
		flag = totc = top = 0;
		fill(dfn, dfn + n + n, -1);
		fill(instack, instack + n + n, false);
		for(int i = 0; i < n + n; ++ i) {
			if (dfn[i] < 0) {
				tarjan(i);
			}
		}
		have_sol = true;
		for(int i = 0; i < n; ++ i) {
			if (color[i] == color[i + n]) {
				have_sol = false;
			}
		}
		if (! have_sol) return;
		for(int i = 0; i < n + n; ++ i) {
			for(int e = 0; e < (int)adj[i].size(); ++ e) {
				int j = adj[i][e];
				if (color[i] != color[j]) {
					new_adj[color[i]].push_back(color[j]);
				}
			}
		}
		for(int i = 0; i < totc; ++ i) {
			reach[i].reset();
			reach[i][i] = true;
			for(int e = 0; e < (int)new_adj[i].size(); ++ e) {
				int j = new_adj[i][e];
				reach[i] |= reach[j];
			}
		}
		for(int i = 0; i < n; ++ i) {
			int u = color[i], v = color[i + n];
			if (reach[u][v]) {
				qin[v] = 1;
				qin[u] = -1;
			}
			if (reach[v][u]) {
				qin[u] = 1;
				qin[v] = -1;
			}
		}
		for(int i = 0; i < totc; ++ i) {
			if (qin[i] > 0) {
				for(int j = 0; j < totc; ++ j) {
					if (reach[i][j]) qin[j] = 1;
				}
			}
			if (qin[i] < 0) {
				for(int j = 0; j < totc; ++ j) {
					if (reach[j][i]) qin[j] = -1;
				}
			}
		}
	}
	bool can_have_sol(int r1, int r2) {
		if (r1 < 0) r1 = -r1 + n;
		if (r2 < 0) r2 = -r2 + n;
		--r1, --r2;
		if (qin[color[r1]] < 0 || qin[color[r2]] < 0) return false;
		if (reach[color[r1]][color[r2 < n ? r2 + n : r2 - n]]) {
			return false;
		}
		if (reach[color[r2]][color[r1 < n ? r1 + n : r1 - n]]) {
			return false;
		}
		return true;
	}
	void output_sol() {
		for(int i = 0; i < n; ++ i) {
			if (color[i + n] < color[i]) {
				printf("0 ");
			} else {
				printf("1 ");
			}
		}
		printf("\n");
	}
} s1, s2;

void constuct_sol(vector<pair<int, int>> edges, int r1, int r2) {
	if (r1) {
		edges.push_back(make_pair(r1, r1));
		edges.push_back(make_pair(r2, r2));
	}
	SAT s;
	s.construct(n, edges);
	s.output_sol();
}

void solve()
{
	cin >> n >> m1 >> m2;
	vector<pair<int, int>> e1, e2;
	for(int i = 0; i < m1; ++ i) {
		int x, y;
		scanf("%d%d", &x, &y);
		e1.push_back(make_pair(x, y));
	}
	for(int i = 0; i < m2; ++ i) {
		int x, y;
		scanf("%d%d", &x, &y);
		e2.push_back(make_pair(x, y));
	}
	s1.construct(n, e1);
	s2.construct(n, e2);
	if (s1.have_sol != s2.have_sol) {
		if (s1.have_sol) {
			constuct_sol(e1, 0, 0);
			return;
		}
		if (s2.have_sol) {
			constuct_sol(e2, 0, 0);
			return;
		}
	}
	if (s1.have_sol == false) {
		puts("SIMILAR");
	}
	for(int i = 0; i < m2; ++ i) {
		if (s1.can_have_sol(-e2[i].first, -e2[i].second)) {
			constuct_sol(e1, -e2[i].first, -e2[i].second);
			return;
		}
	}
	for(int i = 0; i < m1; ++ i) {
		if (s2.can_have_sol(-e1[i].first, -e1[i].second)) {
			constuct_sol(e2, -e1[i].first, -e1[i].second);
			return;
		}
	}
	puts("SIMILAR");
}

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


 #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
const int mod = 1e9 + 7;
typedef long long ll;

int x[5], y[5];

int main(){
  for(int i = 1; i <= 3; i++)
    cin >> x[i] >> y[i];
  sort(x + 1 + 3, y + 1 + 3);
  int cntx = 0, cnty = 0;
  if(x[1] == x[2]){
    cntx = 2;
    if(x[2] == x[3])
      cntx++;
  }
  else if(x[2] == x[3])
    cntx = 2;
  else if(x[1] == x[3])
    cntx = 2;
  else
    cntx = 1;
  if(y[1] == y[2]){
      cnty = 2;
      if(y[2] == y[3])
        cnty++;
    }
    else if(y[2] == y[3])
      cnty = 2;
    else if(y[1] == y[3])
      cnty = 2;
      else cnty = 1;
  int cnt = max(cntx, cnty);
  cout << 3 - cnt + 1;
}


#include <stdio.h>
#include <string.h>
const int MAXN = 5e5 + 10;
int f[MAXN];
int main()
{
    int n,d;
    while(~scanf("%d%d",&n,&d))
    {
        int v,sum = 0,day = 0;
        memset(f,false,sizeof(f));
        for(int i = 0;i < n;++i){
            scanf("%d",&v);
            f[v] = true;
            sum += v;
            for(int j = sum;j >= v;--j)
                f[j] |= f[j-v];
        }
        int i,res = 0;
        for(;;day++){
            for(i = d;i;i--){
                if(f[i+res]){
                    res += i;
                    break;
                }
            }
            if(!i)break;
        }
        printf("%d %d\n",res,day);
    }
    return 0;
}


/*
     ________          __           _______       ________
    |_____  |         /  \         |  ____ \     |  ____  \
         / /         / /\ \        | |    \ \    | |____| /
        / /         / /  \ \       | |     \ \   |  ___  /
       / /         / /____\ \      | |     \ \   | |   \ \
      / /         / ________ \     | |     | |   | |    \ \
     / /_____    / /        \ \    | |____| |    | |     \ \
    |_______|   /_/          \_\   |_______/     |_|     |_|
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <fstream>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>

using namespace std;

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

#define F first
#define S second
#define mkp make_pair
#define pb push_back
#define ppb pop_back
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()

const int N = 123;
const int INF = 1e4 + 123;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;

int n, a[N], ans = INF, mx = -1;

int main () {
    ios_base :: sync_with_stdio(0);
    cin >> n;
    for(int i = 1; i <= n; ++i) {
    	cin >> a[i];
	}
	for(int i = 1; i < n; ++i)
		mx = max(mx, a[i + 1] - a[i]);
	for(int i = 2; i < n; ++i) {
		mx = max(mx, a[i + 1] -  a[i - 1]);
		ans = min(ans, mx);
	}
	cout << ans;
	return 0;
}


#include<iostream>
#include<string>
#include<vector>
#include<tuple>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#include<utility>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<float> vf;
typedef pair<int,int> pii;
typedef vector<pii> vpi;
typedef pair<int,float> pif;
typedef pair<float,int> pfi;
typedef pair<float,float> pff;
#define F first
#define S second
#define PB push_back
#define MP make_pair
int main(){
ios:: sync_with_stdio(0);
cin.tie(0);
int a,b,c,d,e,f;
std::cin >> a;
std::cin >> b;
std::cin >> c;
std::cin >> d;
std::cin >> e;
std::cin >> f;
int mi = min(b,c);
int ma = 0;
for (int i = 0; i <= a; i++) {
  for (int j = 0; j <= mi; j++) {
    if (i+j<=d) {
      ma = max(ma,i*e+j*f);
    }
  }
}
std::cout << ma << '\n';
return 0;
}
//to run the program use asd file.cpp


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

#define MAX_N 200000

int n;
int numbers[MAX_N];
int exists[MAX_N + 1];

struct Node {
    int l, r, sum;
    Node *left, *right;
    int first, last;
};

deque<Node> nodes;

Node *root;

Node* newNode() {
    nodes.emplace_back();
    return &nodes.back();
}

Node* fillNode(int first, int last) {
    if (first >= last) {
        return nullptr;
    }
    int mid = (first + (last - first) / 2);
    Node * result = newNode();
    result->first = first;
    result->last = last;
    result->l = first;
    result->r = last - 1;
    result->left = fillNode(l, mid);
    result->right = fillNode(mid + 1, r);
    result->sum = 0;
    if (!result->left && !result->right) {
        result->sum = l;
    } else {
        if (result->left) {
            result->sum += result->left->sum;
        }
        if (result->right) {
            result->sum += result->right->sum;
        }
    }
    return result;
}

void fixup(Node * node, Node * parent) {
    if (node->r < parent->l) {
        node->sum = (node->last - node->first) * parent->l;
        node->l = node->r = parent->l;
    }
}

int update(Node *node, int first, int last, int size, Node * parent) {
    if (first >= node->last || last <= node->first) return 0;
    if (parent)
        fixup(node, parent);
    if (node->l >= size) return 0;

    if (first <= node->first) {
        node->l = size;
    }
    if (node->last <= last) {
        node->r = max(node->r, size);
    }

    if (first <= node->first && node->last <= last && node->r <= size) {
        int new_sum = (node->last - node->first) * size;
        int result = new_sum - node->sum;
        node->sum = new_sum;
        return result;
    }

    int sum_from_child = 0;
    if (node->left) {
        sum_from_child += update(node->left, first, last, size, node);
    }
    if (node->right) {
        sum_from_child += update(node->right, first, last, size, node);
    }

    node->sum += sum_from_child;
    return sum_from_child;
}

int set_from(int first, int last) {
    if (first >= last) return 0;
    int barSize = last;
    int barIdx = first;
    return update(root, barIdx, n, barSize, nullptr);
}

int main() {
    std::ios_base::sync_with_stdio(false);
    cin >> n;
    root = fillNode(0, n);
    fill(begin(exists), end(exists), -1);
    for (int i = 0; i < n; ++i) {
        cin >> numbers[i];
        exists[numbers[i]] = i;
    }

    int64_t result = 0;
    for (int gcd = MAX_N; gcd >= 1; --gcd) {
        std::vector<int> idxs;
        for (int i = gcd; i <= MAX_N; i += gcd) {
            if (exists[i] >= 0)
                idxs.push_back(exists[i]);
        }
        if (idxs.size() < 2) continue;
        nth_element(begin(idxs), begin(idxs) + 2, end(idxs));
        int x1 = idxs[0];
        int x2 = idxs[1];
        nth_element(begin(idxs), begin(idxs) + idxs.size() - 2, end(idxs));
        int x3 = idxs[idxs.size() - 2];
        int x4 = idxs[idxs.size() - 1];

        result += gcd * set_from(0, x3);
        result += gcd * set_from(x2, idxs.size());
        result += gcd * set_from(x1 + 1, x4);
    }
}


#include<iostream>
using namespace std;
int main ()
{
    int a,arr[16];
    cin >> a ;
    for(int i=0;i<a;i++){
        cin >> arr[i];
    }
    if(a==1&&arr[0]==0){
        cout<<"UP";
        return 0;
    }
    else if(a==1&&arr[0]!=0){
        cout<<"-1";
        return 0;
    }
    if(arr[a-1]<15&&arr[a-2]>arr[a-3])
        cout<<"UP";
    else
        cout<<"DOWN";
}


#include <algorithm>
#include <iostream>
#include <map>
#include <functional>
#include <set>
using namespace std;

void init(string s, map<char, int>& numOfLessCoins) {

	if (s[1] == '>') {

		if (numOfLessCoins.find(s[0]) != numOfLessCoins.end()) {
			numOfLessCoins[s[0]]++;
		}
		else {
			numOfLessCoins.insert(pair<char, int>(s[0], 0));
			numOfLessCoins[s[0]]++;
		}

		if (numOfLessCoins.find(s[2]) == numOfLessCoins.end()) {
			numOfLessCoins.insert(pair<char, int>(s[2], 0));
		}
	}

	else {

		if (numOfLessCoins.find(s[2]) != numOfLessCoins.end()) {
			numOfLessCoins[s[2]]++;
		}
		else {
			numOfLessCoins.insert(pair<char, int>(s[2], 0));
			numOfLessCoins[s[2]]++;
		}

		if (numOfLessCoins.find(s[1]) == numOfLessCoins.end()) {
			numOfLessCoins.insert(pair<char, int>(s[1], 0));
		}

	}
}

int main() {
	string s;
	map<char, int> numOfLessCoins;
	vector<int> a;

	cin >> s;
	while (s != "0") {
		init(s, numOfLessCoins);
		cin >> s;
	}

	typedef std::function<bool(pair<char, int>, pair<char, int>)> Comparator;

	Comparator compFunctor =
		[](pair<char, int> elem1, pair<char, int> elem2)
	{
		return elem1.second < elem2.second;
	};

	// Declaring a set that will store the pairs using above comparision logic
	std::set<std::pair<char, int>, Comparator> setOfWords(
		numOfLessCoins.begin(), numOfLessCoins.end(), compFunctor);

	// Iterate over a set using range base for loop
	// It will display the items in sorted order of values
	int j = 0;
	string output = "";
	for (std::pair<char, int> element : setOfWords) {
		if (element.second == j) {
			output += element.first;
		}
		else {
			output = "IMPOSSIBLE";
			break;
		}
		j++;
	}

	cout << output;

}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int a,b;
    int h;
    h=a+b;
    cout << h;
}


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

int main()
{
	while (true)
	{
		int children;
		int times;
		cin >> children;
		cin >> times;
		char q[50];
		for (int i = 0; i < children; i++)
		{
			cin >> q[i];
		}
		for (int i = 0; i < times; i++)
		{
			for (int j = 0; j < children; j++)
			{
				if (q[j] == 'B' & q[j + 1] == 'G')
				{
					swap(q[j], q[j + 1]);
					j = j + 1;
				}
			}

		}
		for (int i = 0; i < children; i++)
		{
			cout << q[i];
		}

	}

	return 0;
}


#include <list>
#include <fstream>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <map>
#include <iostream>
#include <vector>
#include <iomanip>
#include <queue>
#include <deque>
#include <set>
#include <stack>
#include <sstream>
//#include "sqrt.cpp"
//#include "tree.cpp"
//#include "funcs.cpp"

#define all(x)          (x).begin(), (x).end()
//#define max(x, y)       ((x)>(y))?(x):(y)
#define forn(N)         for(int i = 0; i<N; i++)
#define fornj(N)         for(int j = 0; j<N; j++)
#define PI 3.1415926535897932384626433
using namespace std;

//#define ONLINE_JUDGE

    long long N, M;

struct MATRIX
{
    int dim;
    long long MAT[100][100];

    MATRIX (int N, int x = -1)
    {
        dim = N;
        for (int i = 0; i<dim; i++)
            for (int j = 0; j<dim; j++)
            {
                if (x!=-1 && i==j)
                    MAT[i][j] = x;
                else
                    MAT[i][j] = 0;
            }
    }

    void operator+=(const MATRIX& a)
    {
        for (int i = 0; i<a.dim; i++)
        {
            for (int j = 0; j<a.dim; j++)
            {
                MAT[i][j] += a.MAT[i][j];
            }
        }
    }

    void show()
    {
        for (int i = 0; i<dim; i++)
        {
            for (int j = 0; j<dim; j++)
                cout<<MAT[i][j]<<" ";
            cout<<endl;
        }
        cout<<endl;
    }

};
MATRIX operator*(MATRIX a, MATRIX b)
{
    MATRIX res(a.dim);
    for (int i = 0; i<a.dim; i++)
    {
        for (int j = 0; j<b.dim; j++)
        {
            long long sum=0;
            for (int z=0; z<a.dim; z++)
            {
                sum += a.MAT[i][z] * b.MAT[z][j];
            }
            sum %=M;
            res.MAT[i][j] = sum;
        }
    }
    return res;
}

MATRIX binpow (MATRIX a, long long n) {
    MATRIX res = a;

    while (n) {
        if (n & 1)
            res = res * a;
        a = a * a;
        n >>= 1;
    }
    return res;
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    #endif

    cin>>N>>M;

    MATRIX ans(3);

    ans.MAT[0][0] = 3;
    ans.MAT[0][1] = 0;
    ans.MAT[0][2] = 2;

    ans.MAT[1][0] = 0;
    ans.MAT[1][1] = 3;
    ans.MAT[1][2] = 2;

    ans.MAT[2][0] = 0;
    ans.MAT[2][1] = 0;
    ans.MAT[2][2] = 1;

    MATRIX lol = binpow (ans, N-1);

    vector<long long> B(3);

    B[0] = 8;
    B[1] = 2;
    B[0] = 1;

    long long sum = 0;

    forn(3)
    {
        sum+=lol.MAT[0][i] * B[i];
    }
    cout<<(sum-1)%M;

    //printf("\n\ntime-%.3lf", clock()*1e-3);
    return 0;
}


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

using namespace std;

const int N = 110;

int n;
int t[N];
int T;

bool ok(int cnt){
	for (int i = 1; i <= n; i++){
		int j = i;
		int c = 0;
		while (j <= n){
			if (t[i] + T < t[j]) break;
			j++;
			c++;
		}
		if (c >= cnt) return 1;
	}
	return 0;
}

int main(){

	ios_base::sync_with_stdio(false);
	cin.tie(0);

#ifndef ONLINE_JUDGE
	freopen("b.in", "r", stdin);
#endif
		cin >> n;
		for (int i = 1; i <= n; i++){
			cin >> t[i];
		}
		cin >> T;
		sort(t + 1, t + n + 1);
		for (int i = n; i >= 1; i++){
			if (ok(i)){
				cout << i;
				return 0;
			}
		}

	return 0;

}


#include<bits/stdc++.h>
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
using namespace std;
int n,k,phi[1000005];
ll ans;
int main(){
	scanf("%d%d",&n,&k);
	if (k==1) return puts("3"),0;
	For(i,1,n) phi[i]=i;
	For(i,2,n) if (phi[i]==i)
		For(j,1,n/i) phi[i*j]-=phi[i*j]/i;
	sort(phi+1,phi+n+1);
	For(i,1,k+2) ans+=phi[i];
	printf("%lld\n",ans);
}


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

#define MAXN 200010
#define MOD 998244353
#define int long long

int mulmod(int a, int b){ // multiplicacao modular
    // multiplicar (a*b) equivale a sum(a * 2^i * b_i), onde (a*2^i) é representado
    // na linha do meio, b_i na ultima linha, e o passo de sum na primeira linha
    int ans = 0;
    a %= MOD;
    while(b > 0){
        if(b%2) ans = (ans + a) % MOD;
        a = (a * 2) % MOD;
        b >>= 1;
    }
    return ans % MOD;
}

int exp(int a, int b){ // recursivo
	if(b == 0) return 1;
	if(b == 1) return a;
    auto y = exp(mulmod(a, a), b/2);
	if(b&1) return mulmod(y, a);
	return y;
}

int inv(int x){
    return exp(x, MOD - 2);
}

int fat[MAXN];
int c(int p, int q){
    int num = fat[p];
    int den = mulmod(fat[p - q], fat[q]);
    return mulmod(num, inv(den)) % MOD;
}

int32_t main(){
    fat[0] = 1;
    for(int i = 1; i < MAXN; ++i){
        fat[i] = mulmod(i, fat[i - 1]);
    }

    if(k > n){
        cout << 0 << endl;
        return 0;
    }

    int n, k; cin >> n >> k;
    // suponho que estou analsiando colunas e dps
    // multiplico por 2
    int numActive = n - k;
    // se eu fizer C(n, numActive) vou estar contando combinacoes
    // em que mais q numEmpty colunas podem estar vazias
    // ex: 1111111111
    // para isso, vai ser usado o principio de inclusao e exclusao
    // que vai contar coluna por coluna, compensando confs contadas
    // multiplas vezes
    // dentre as colunas que vao ser usadas, cada passo vai escolher
    // i colunas que nao serao mais utilizadas, a fim de evitar o problema
    // acima
    int sum = 0;
    for(int i = 0; i < numActive; ++i){
        cerr << sum << endl;
        // exatamente i colunas inativas
        int conb = c(numActive, i); // escolhe as colunas que serao inativas
        int confs = exp(numActive - i, n); // distribui n torres em nas colunas ocupadas

        int x = mulmod(conb, confs) % MOD;
        if(i%2) sum = ((sum - x) + MOD) % MOD;
        else sum = (sum + x) % MOD;
    }

    sum = mulmod(sum, c(n, numActive)); // as confs de escolher as exatas numActive colunas que serao ativas)

    int x = 2; if(k == 0) x = 1;
    cout << mulmod(sum, x)<< endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 1e9;
int r, g, b;

class Problem{
public:
	int solve(int r, int g, int b){
		vector<int> T = {r, g, b};
		int i = 0;
		while(true){
			if(T[0]==0&&T[1]==0&&T[2]==0)break;
			if(T[i%3]>=2)T[i%3]-=2;
			else if(T[i%3])T[i%3]=0;
			i++;
		}
		return i + 30 - 1;
	}
	void parse_statement();
};

int main(){
	Problem A;
	A.parse_statement();
	return -1;
}

void Problem::parse_statement(){
	cin>>r>>g>>b;
	cout<<solve(r, g, b)<<endl;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
long long a[4][4];
long long dp[50][4][4];
long long main()
{
    long long f(long long n,long long s,long long e,long long tr);
    for(long long i=1; i<=3; i++)
    {
        for(long long j=1; j<=3; j++)
        {
            cin>>a[i][j];
        }
    }
    long long n;
    cin>>n;
    memset(dp,-1,sizeof(dp));
    long long k=f(n,1,3,2);
    cout<<k<<endl;
}
long long f(long long n,long long s,long long e,long long tr)
{
    long long res=0;
    if(dp[n][s][e]!=-1)
    {
        return dp[n][s][e];
    }
    if(n==1)
    {
        long long k = min(a[s][e],a[s][tr]+a[tr][e]);
        res+=k;
    }
    else
    {
        long long val1 = f(n-1,s,tr,e)+a[s][e];
        val1+=f(n-1,tr,e,s);
        long long val2 = f(n-1,s,e,tr)+a[s][tr];
        val2+=f(n-1,e,s,tr)+a[tr][e];
        val2+=f(n-1,s,e,tr);
        res+=min(val1,val2);
    }
    dp[n][s][e] = res;
    return res;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <sstream>
#include <string>
#include <map>
using namespace std;
typedef long long int LL;
typedef long double LD;
const int MAX = 100;
const int N = 3e6;
const LD PI = 3.14159;
LL a,b,f,k;
LL num[N][2];
int main() {
#ifdef CDZSC_OFFLINE
	freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
#endif
	while(scanf("%lld%lld%lld%lld", &a, &b, &f, &k) == 4){
		memset(num,-1,sizeof(num));
		LL x = f;
		LL y = a + (a - f);
		LL d = a * k;
		a *= 2;
		LL ans = 0;
		LL len = b;
		bool flag = true;
		bool flag1 = true;
		while(1){
			if(len >= d)
				break;
			LL jd = len % a;
			if(flag1){
				if(num[jd][0] == -1){
					num[jd][0] = len;
					num[jd][1] = ans;
				}
				else{
					LL x = len - num[jd][0];
					LL dnum = (ans + 1) - num[jd][1];
					LL pp = (d - len) / x;
					ans += dnum*pp;
					len = len + pp * x;
					flag1 = false;
				}
			}
			if(jd < min(x,y)){
				LL len1 = ((len / a) - 1) * a + y;
				if(len1 <= len - b){
					flag = false;
					break;
				}
				len = len1;
			}
			else if(jd >= y){
				len = (len / a) * a + y;
			}
			else{
				len = (len / a) * a + x;
			}
			len += b;
			ans++;
		}
		if(flag)
			printf("%lld\n",ans);
		else
			printf("-1\n");
	}
	return 0;
}


#include<iostream>
#include<cstdio>

#define int long long

long long w[300000],a[300000];
int X,Y;
int N,Q;
struct tarr{
	long long T[300000];
	inline int lowbit(int k){return k&-k;}
	void modify(int k,long long v){
		while (k<=N){
			T[k]+=v;
			k+=lowbit(k);
		}
	}
	long long query(int k){
		long long ret=0;
		while (k){
			ret+=T[k];
			k-=lowbit(k);
		}
		return ret;
	}
}W,WA,iW;

long long calc(int l,int r){
	return iW.query(r)-iW.query(l-1)-l*(W.query(r)-W.query(l-1));
}
/*
long long calcdown(int l,int r){
	if (l>=r) return 0;
	int mdl=(l+r)>>1;
	return calcdown(l,mdl)+calcdown(mdl+1,r)+(W.query(mdl)-W.query(l-1))*(r-mdl);
}
*/
int main(){
	scanf("%d%d",&N,&Q);
	for (int i=1;i<=N;i++) scanf("%I64d",&a[i]);
	for (int i=1;i<=N;i++) scanf("%I64d",&w[i]);
	for (int i=1;i<=N;i++){
		W.modify(i,w[i]);
		WA.modify(i,w[i]*a[i]);
		iW.modify(i,i*w[i]);
	}
	for (int i=1;i<=Q;i++){
		scanf("%d%d",&X,&Y);
		if (X<0){
			X=-X;
			W.modify(X,Y-w[X]);
			WA.modify(X,(Y-w[X])*a[X]);
			iW.modify(X,(Y-w[X])*X);
			w[X]=Y;
		}
		else{
			long long pre=W.query(X-1);
			long long sumW=W.query(Y)-pre;
			int l=X,r=Y,mdl;
			while (l<r){
				mdl=(l+r)>>1;
				long long sumpre=W.query(mdl)-pre;
				if ((sumpre<<1)<sumW) l=mdl+1;
				else r=mdl;
			}
			int L=a[l]-l+X;
			long long Ans=calc(X,l-1)-calc(l+1,Y)+L*(W.query(l-1)-W.query(X-1))-(L+l-X+1)*(W.query(Y)-W.query(l))-WA.query(l-1)+WA.query(X-1)+WA.query(Y)-WA.query(l);
			printf("%I64d\n",Ans%1000000007);
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b;
string s;
cin>>a>>b>>s;
while(b--){for(int i=0;i=s.size();i++){
if(s[i]=='B'&&s[i+1]=='G'){
swap(s[i],s[i+1]);
}}}cout<<s;
return 0;
}


//Problem: Mister B and Boring Game
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
   // freopen("test.inp","r",stdin);
   // freopen("test.out","w",stdout);
	int a,b,len;
	char c;
	long l,r;
	cin>>a>>b>>l>>r;
	string s,newstr,suffix;
	for(c = 'a'; c < 'a' + a;c++)
	    s += c;
	if(r >= 2*(a+b))
		if(r == l)
		{
			r = 2*(a+b);
			l = r;
		}

	len = s.length();
	for(int i = len; i <= r;i += a+b)
	{
		for(int j = 0; j < b;j++) //turn of Mister B
			s += s[len - 1];
		len = s.length();
		if(len >= r)
			continue;
		c = 'a';
		suffix.append(s,len-a,a);
		for(int k = 0; k < a;k++) //turn of Computer
		{
			while(suffix.find(c)!=-1) c++;
			s += c;
			c++;
		}

	}
	for(int i = l-1; i < r;i++)
		if(newstr.find(s[i]) == -1)
			newstr += s[i];
	cout<<newstr.length();
	/*for(int i = 0; i < s.length();i++)
		cout<<s[i];*/
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{ int n,i,s,t,p,z=0;
  cin>>n;
  int a[n];
  for(i=0;i<n;i++)
    cin>>a[i];
    s=accumulate(a,a+n,0);
    if(n%2==0)
       t=n/2;
    else
        t=n/2+1;
    s=s%n;
    if(s>=t)
       cout<<z;
    else
    {
        while(1)
        {   z++;
            p=min_element(a,a+n)-a;
            a[p]=5;
            s=accumulate(a,a+n,0);
            s=s%n;
            if(s>=t)
            {
                cout<<z;break;
            }
            }}
}


#include<bits/stdc++.h>
using namespace std;
typedef long double ll;
map<ll,int>mp;

int main(){
	ios::sync_with_stdio(false);
    cin.tie(NULL);
	ll i,j,k,nb,x,y,l,r;
	cin>>l>>r>>x>>y>>k;
	for(i=l;i<=r;i++)mp[i/k]=55;
	for(i=x;i<=y;i++)if(mp[i]==55){
		cout<<"YES";return 0;
	}
	cout<<"NO";
}


#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define uint ungigned
#define db double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define vi vector<int>
#define vpi vector<pii >
#define IT iterator

#define PB push_back
#define MK make_pair
#define LB lower_bound
#define UB upper_bound
#define y1 wzpakking
#define fi first
#define se second
#define BG begin
#define ED end

#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define UPD(x,y) (((x)+=(y))>=mo?(x)-=mo:233)%s
#define CLR(a,v) memset(a,v,sizeof(a))
#define CPY(a,b) memcpy(a,b,sizeof(a))
#define sqr(x) (1ll*x*x)

#define LS3 k*2,l,mid
#define RS3 k*2+1,mid+1,r
#define LS5 k*2,l,mid,x,y
#define RS5 k*2+1,mid+1,r,x,y
#define GET pushdown(k);int mid=(l+r)/2
#define INF (1ll<<60)
using namespace std;

void move(vector<int> p){
	ans.push_back(p);
	int sz=p.size()-1;
	Rep(i,sz-2,0);{
		swap(pos[p[i]],pos[p[sz-1]]);
		pii x;
		x=pos[p[i]];
		a[x.fi][x.se]=p[i];
		x=pos[p[sz-1]];
		a[x.fi][s.se]=p[sz-1];
	}
}
void roL(int x,int y){
	move(vector<int>{
		a[x][y],a[x+1][y],
		a[x+1][y+1],a[x][y+1]
	});
}
void roR(int x,int y){
	move(vector<int>{
		a[x][y],a[x][y+1],
		a[x+1][y+1],a[x+1][y]
	});
}
void goL(int v){
	int x=pos[x].fi,y=pos[x].se;
	move(vector<int>{
		a[x][y],a[x][y-1],
		a[(x==n?x+1:x-1)][y-1],a[(x==n?x+1:x-1)][y]
	});
}
void goR(int v){
	int x=pos[x].fi,y=pos[x].se;
	move(vector<int>{
		a[x][y],a[x][y+1],
		a[(x!=n?x+1:x-1)][y+1],a[(x!=n?x+1:x-1)][y]
	});
}
void goU(int v){
	int x=pos[x].fi,y=pos[x].se;
	move(vector<int>{
		a[x][y],a[x-1][y],
		a[x-1][(y!=m?y-1:y+1)],a[x][(y!=m?y-1:y+1)];
	});
}
void goD(int v){
	int x=pos[x].fi,y=pos[x].se;
	move(vector<int>{
		a[x][y],a[x+1][y],
		a[x+1][(y!=m?y-1:y+1)],a[x][(y!=m?y-1:y+1)];
	});
}

int main(){
	scanf("%d%d",&n,&m);
	For(i,1,n*m) per[i]=i;
	random_shuffle(per+1,per+n*m+1);
	For(i,1,n){
		scanf("%d",&a[i][j]);
		pos[a[i][j]]=pii(i,j);
	}
	For(i,1,n-2){
		For(j,1,m-1){
			int id=(i-1)*m+j;
			for (;pos[x].se>j;) goL(x);
			for (;pos[x].se<j;) goR(x);
			for (;pos[x].fi>i;) goU(x);
		}
		int id=i*m;
		if (pos[x]==pii(i,m)) continue;
		if (pos[x]==pii(i+1,m-1)) roL(i+1,m-1);
		roR(i,m-1);
		for (;pos[x].se<m;goR(x));
		for (;pos[x].fi>i+1;goU(x));
		roL(i,m-1);
	}
	For(j,1,m-2){
		int x=(n-2)*m+j;
		for (;pos[x].se>j;) goL(x);
		for (;pos[x].se<j;) goU(x);
		x=(n-1)*m+j;
		if (pos[x]==pii(n,j)) continue;
		if (pos[x]==pii(n-1,j+1)) roR(n-1,j+1);

	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    cin>>n>>m;
    vector<int>a;
    vector<int>b;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(int i=0;i<m;i++)
    {
        cin>>b[i];
    }
    for(int i=0;i<b.size();i++)
    {
        for(int j=0;j<n;j++)
        {
            if(b[i]==a[j])
              cout<<b[i]<<" ";
        }
    }
}


#include <iostream>
#include<bits/stdc++.h>
#include<algorithm>
#include<cstdlib>
using namespace std;
int main()
{
    long long n;
    cin>>n;
    long long max_sum=(n*(n+1))/2;
    vector<bool> dp(max_sum+1,false);
    dp[0]=true;
    int j,i;
    for(i=0;i<=n;i++)
    for(j=max_sum-i;j>=0;--j){
            dp[j+i]=dp[j+i]|dp[j];
    }
    int min1,max1,ans;
    for(int k=0;k<=max_sum/2;k++){
    if(dp[max_sum/2-k]==true){
        min1=max_sum/2;
        max1=max_sum-min1;
        ans=abs(max1-min1);
    }
    }
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()

{
    int x,y ;
    while(cin>>x>>y)
    {
        int one{0},two{12},three{0},temp ;

        temp=abs(x-y) ;

        if(temp%2!=0)
        {
            two = 0 ;
        }else {

            two=temp/2 ;
        }
        while(one + two + three != 6)
        {
            for(int i{1};i<7;i++)
                {
                    if(abs(x-i)<abs(y-i)){
                        one+=1 ;
                    }else if(abs(x-i)>abs(y-i)){
                        three += 1;
                    }
                }
        }
        cout<<one<<" "<<two<<" "<<three ;

    }

    return 0;
}


	#include <bits/stdc++.h>
	#include <cstdio>
	#include <cstring>
	#include <cmath>
	#include <cstring>
	#include <chrono>
	#include <complex>
	#define endl "\n"
	#define ll long long int
	#define ld long double
	#define vi vector<int>
	#define vll vector<ll>
	#define vvi vector < vi >
	#define pii pair<int,int>
	#define pll pair<long long, long long>
	#define mod 1000000007
	#define inf 1000000000000000001;
	#define all(c) c.begin(),c.end()
	#define mp(x,y) make_pair(x,y)
	#define mem(a,val) memset(a,val,sizeof(a))
	#define eb emplace_back
	#define pb push_back
	#define f first
	#define s second
	using namespace std;
	ll power[100000001];
	void pwr()
	{
		ll i,ans = 1;
		power[0]= 1;
		for(i=1;i<=100000000;i++)
		{
			ans *= 2;
			power[i] =ans;
		}
	}
	int main()
	{
		std::ios::sync_with_stdio(false);
		int T;
		//cin>>T;
		// cin.ignore(); must be there when using getline(cin, s)
		T = 1;
		pwr();
		while(T--)
		{
			ll n,m;
			cin>>n>>m;
			cout<<m%power[n]<<endl;

		}
		return 0;
	}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fs first
#define se second
#define mp make_pair
#define pb push_back
#define precise(n,k) fixed<<setprecision(k)<<n
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> OST;
//typedef long long ll;
#define ll int
#define mod 1000000007
typedef long double lf;
ll n,T,dp[32770][4][230];
pair<ll,ll> arr[20];
ll solve(ll mask,ll g,ll tim)
{
	if(tim>T)
	return 0;
	if(tim == T)
	return 1;
	if(dp[mask][g][tim] != -1)
	return dp[mask][g][tim];
	ll ans=0;
	for(ll i=1;i<=n;i++)
	{
		ans%=mod;
		if(arr[i].se != g && (mask&(1<<i))==0)
		ans=(ans+solve(mask|(1<<i),arr[i].se,tim+arr[i].fs))%mod;
	}
	return dp[mask][g][tim]=ans%mod;
}
int main() {
	// your code goes here
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n>>T;
	for(ll i=1;i<=n;i++)
	cin>>arr[i].fs>>arr[i].se;
	memset(dp,-1,sizeof(dp));
	cout<<solve(0,0,0);
	return 0;
}


#include <iostream>
#define int long long
using namespace std;

const int MAXN=2e3+5;
const int mod=998244353;

int n,m,k,dp[MAXN][MAXN];

signed main()
{
    cin>>n>>m>>k;
    for(int i=0;i<=n;i++) dp[i][0]=m;
    for(int i=2;i<=n;i++)
    {
    	for(int j=1;j<=k;i++)
    	{
    		dp[i][j]=(dp[i-1][j-1]*(m-1))%mod;
    		dp[i][j]=(dp[i][j]+dp[i-1][j])%mod;
		}
	}
	cout<<dp[n][k];
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b, ll &x,ll &y)
{
     if(b==0)
     {
          x=1;y=0;
          return a;
     }
     ll x1,y1;
     ll d=gcd(b,a%b,x1,y1);
     x=y1;
     y=x1-y1*(a/b);
     return d;
}
bool find(ll a, ll b,ll c,ll &x0, ll&y0,ll &g)
{
    g= gcd(a,b,x0,y0);
    if(c%g)
    return false;
    x0*=c/g;
    y0*=c/g;
    if(a<0)
     x0=-x0;
     if(b<0)
     y0=-y0;
     return true;
}
int main()
{
        ll a,b,c,x,y,g;
        //cin>>a>>b>>c;
         if(find(a,b,c,x,y,g))
         {
             cout<<x<<y;
             if(x<0 || y<0)
              {
                  while(1)
                  {
                       if(x>=0)
                        break;
                    x=x+b/g;
                    y=y-a/g;
                  }
              }
              if(y<0)
              {
                   while(1)
                   {
                        if(y>=0)
                         break;
                        x=x-b/g;
                        y=y+a/g;
                   }
              }
              if(x<0 || y<0)
               cout<<"No\n";
               else
               cout<<"Yes\n";
         }
         else
         cout<<"No\n";
}


#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int sum = 0,maxn = 0, arr[4];
for(int i = 0 ; i < 4 ; i++){
cin>>arr[i];
maxn = max(maxn,arr[i]);}
bool *barr = new bool[maxn];
for(int i = 0 ; i < maxn ; i++)
barr[i] = false;
for(int i = 0 ; i < 4 ; i++){
barr[arr[i]] = true;
}
for(int i = 0 ; i < maxn ; i++)
if(barr[i])sum++;
cout<<3-sum;
return 0;}


#include <bits/stdc++.h>
using namespace std;
int n,k,m,pw[1005],dp[1005][1005][105];
int dfs(int node,int ok,int num)
{
    int a=0,b=0,res=0;
    if(node==n+1) return 0;
    if(~dp[node][ok][num]) return dp[node][ok][num];
    if(node==n)
    {
        for(int i=1;i<=9;i++)
        {
            if(ok==1)
            {
                //printf("CEVAP:%d\n",num);
            }
            a=dfs(node+1,((num+i*pw[node-1])%k==0?ok+1:ok),(num+i*pw[node-1])%k)+(ok>0 || ((num+i*pw[node-1])%k==0)?1:0);
            //printf("A:%d\n",a);
            res=(res+a)%m;
        }
    }
    else
    {
        for(int i=0;i<=9;i++)
        {
            a=dfs(node+1,((((num+i*pw[node-1])%k==0) && ((num+i*pw[node-1])!=0))?ok+1:ok),(num+i*pw[node-1])%k)/*+((ok>0 || (((num+i*pw[node-1])%k==0) && ((num+i*pw[node-1])!=0)))?1:0)*/;
            //printf("B:%d\n",b);
            res=(res+a)%m;
        }
    }
    return dp[node][ok][num]=res%m;
}
void read()
{
    scanf("%d %d %d",&n,&k,&m);
}
void solve()
{
    int ans=dfs(1,0,0);
    printf("%d",ans);
}
void init()
{
    pw[0]=1;
    for(int i=1;i<=1000;i++)
    {
        pw[i]=(pw[i-1]*10)%k;
    }
}
int main()
{
    memset(dp,-1,sizeof(dp));
    read();
    init();
    solve();

    return 0;
}


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

int main()
{

  char a[101];
  string s = "";
  int n;
  cin >> n;
  for(int i = 0; i < n; ++i){
    cin >> a[i];
    s += a[i];
  }
  while(1) {
    int x = s.find("RU");
    int y = s.find("UR");
    if(x == -1 && y == -1) break;
    if(x < y) {
        s.replace(x, 2, "D");
        continue;

    } else {
        s.replace(y, 2, "D");
        continue;
    }
  }
  cout<< s.length();

  return 0;
}


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

const int maxn = 3005;
vector<ll> pr[maxn];
vector<int> w;
vector<ll> c;
int k;
ll ans = 0;

void apply(vector<ll>& d, int w, ll c) {
  for (int i = (int) d.size() - w - 1; i >= 0; --i) {
    d[i + w] = max(d[i + w], d[i] + c);
  }
}

void rec(int l, int r, vector<ll> d) {
  if (l + 1 == r) {
    for (int i = 0; i <= min(k, w[l]); ++i) {
      ans = max(ans, d[k - i] + pr[l][i]);
    }
    return;
  }
  int c = (l + r) / 2;
  vector<ll> d2 = d;
  for (int i = l; i < c; ++i) {
    apply(d2, w[i], ::c[i]);
  }
  rec(c, r, d2);
  d2 = d;
  for (int i = c; i < r; ++i) {
    apply(d2, w[i], ::c[i]);
  }
  rec(l, c, d2);
}

signed main() {
#ifdef LOCAL
  assert(freopen("c.in", "r", stdin));
#endif
  int n;
  cin >> n >> k;
  w.resize(n);
  c.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> w[i];
    pr[i].resize(w[i] + 1);
    for (int j = 0; j < w[i]; ++j) {
      int x;
      cin >> x;
      c[i] += x;
      pr[i][j + 1] = pr[i][j] + x;
    }
  }
  vector<ll> d(k + 1);
  rec(0, n, d);
  cout << ans << '\n';
}


#include<bits/stdc++.h>
using namespace std;
#define REP(i,j,k) for(register int (i)=(j);(i)<(k);++(i))

struct pfx{int s[3];};
int operator-(pfx x,pfx y){
	int ret=0;
	REP(i,0,3)ret+=max(0LL,x.s[i]-y.s[i]);
	return ret;
}

const int maxn=80;
const int inf=1ll<<50;
int n,dp[maxn][maxn][maxn][3];
char s[maxn],t[maxn];
pfx vkx[3][maxn];

inline void MIN(int&x,int y){x=min(x,y);}

main(){
	cin.tie(0),cout.tie(0),ios_base::sync_with_stdio(false);
	cin>>n;
	pfx tt;tt.s[0]=tt.s[1]=tt.s[2]=0;
	REP(i,0,n){
		char c;int x;
		cin>>c,x=(c=='V'?0:c=='K'?1:2);
		++tt.s[x],vkx[x][tt.s[x]]=tt;
	}
	REP(v,0,n+1)REP(k,0,n+1)REP(x,0,n+1)REP(i,0,3)dp[v][k][x][i]=inf;
	dp[0][0][0][2]=0;
	REP(v,0,n)REP(k,0,n)REP(x,0,n){
		if(v+k+x>=n)continue;
		REP(i,0,3)REP(j,0,3)if(!(i==0&&j==1)){
			pfx nn;nn.s[0]=v+(j==0),nn.s[1]=k+(j==1),nn.s[2]=x+(j==2);
			if(nn.s[0]>tt.s[0]||nn.s[1]>tt.s[1]||nn.s[2]>tt.s[2])continue;
			MIN(dp[nn.s[0]][nn.s[1]][nn.s[2]][j],dp[v][k][x][i]+(nn-vkx[j][nn.s[j]]));
		}
	}
	int mn=inf;
	REP(i,0,3)mn=min(mn,dp[tt.s[0]][tt.s[1]][tt.s[2]][i]);
	cout<<mn<<'\n';
}


#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;                // int k=arr[i]*(abs(i-x)+i)*2+(x*arr[i]*2);
int main(){
    int floors;
    cin >>floors;
    std::vector<int> arr(floors);
    for(int i=0 ; i<floors ; i++) cin>>arr[i];
    //cout<<"x = "<<x<<endl;
    int total=0;
    std::vector<int> arry;
    for(int x=1 ; x<floors ; x++){
        for(int i=1 ; i<floors ; i++){
            int k=abs(i-x)+i;
            //cout<<"k1 = "<<k<<endl;
            k = k*arr[i]*2;
            //cout<<"k2 = "<<k<<endl;
            k = k+(x*arr[i]*2);
            //cout<<"k3 = "<<k<<endl;
            //cout<<i<< "fainly k = "<<k<<endl;
            total += k;
    }
        arry.push_back(total);
        total = 0;
    }
    sort(arry.begin(), arry.end());
    cout<<arry[0];
    return 0;
}


# include <iostream>
# include <string>
# include <cstring>
# include <cstdlib>
# include <cstdio>
# include <ctime>
# include <clocale>
# include <fstream>
# include <sstream>
# include <memory>
# include <cmath>
# include <deque>
# include <list>
# include <set>
# include <map>
# include <stack>
# include <queue>
# include <iomanip>
# include <algorithm>
# include <iterator>

# define mp make_pair
# define fi first
# define se second

using namespace std;

typedef long long  ll;

int main()
{
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    ll n,r1=0,r2=0;
    cin>>n;
    for(ll i=0;i<n;i++)
    {
        ll h;
        cin>>h;
        if (h==100)
            r1+=h;
        if (h==200)
            r2+=h;

    }
    if (r1%r2==0&&r2%r1==0)
        cout<<"YES";
    else
        cout<<"NO";
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<stdlib.h>
#include<ctime>
#include<string>
#include<cmath>
#include<algorithm>
#include<complex>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define LL long long
#define FOR(i,a,b) for (int i=a;i<=b;++i)
#define FORD(i,a,b) for (int i=a;i>=b;--i)
using namespace std;
typedef pair<int,int> pa;
void getint(int &v){
    char ch,fu=0;
    for(ch='*'; (ch<'0'||ch>'9')&&ch!='-'; ch=getchar());
    if(ch=='-') fu=1, ch=getchar();
    for(v=0; ch>='0'&&ch<='9'; ch=getchar()) v=v*10+ch-'0';
    if(fu) v=-v;
}
LL f[30][300010],ans,fac[300010],inv[300010];
int n,k,MO,s,a[55],S[1000010];
LL pw(LL x,int y){
	LL t=1;
	for (;y;y>>=1){
		if (y&1) t=t*x%MO;
		x=x*x%MO;
	}
	return t;
}
int main(){
	scanf("%d%d%d",&n,&k,&MO);
	if (n%2==0) return cout<<pw(k,n)<<endl;
	fac[0]=1;
	FOR(i,1,n) fac[i]=fac[i-1]*i%MO;
	inv[n]=pw(fac[n],MO-2);
	FORD(i,n-1,0) inv[i]=inv[i+1]*(i+1)%MO;
	int t=n;
	FOR(i,0,20){
		if (t&1) a[++s]=1<<i;
		t>>=1;
	}
	f[0][(1<<s)-1]=1;
	FOR(i,0,(1<<s)-1)
		FOR(j,0,s-1)
			if ((i>>j)&1) S[i]+=a[j+1];
	FOR(i,0,k-1)
		FOR(j,0,(1<<s)-1)
			if (f[i][j]){
				for (int k=j;k>=0;k=(k==0)?-1:(k-1)&j){
					(f[i+1][j^k]+=f[i][j]*inv[S[k]])%=MO;
				}
			}
	ans=(pw(k,n)-f[k][0]*fac[n])%MO;
	cout<<(ans+MO)%MO<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,i;
    int cnt=0;
    if(n<m-1)
    cout<<n;
    else
    {
        int d=n/m;

        d=d+n;
        for(i=n+1;i<=d;i++)
        if(i%m==0)
        cnt++;
        cnt=cnt*cnt;
        d+=cnt;
        cout<<d;
    }
    return 0;
}


#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cassert>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
using namespace std;
#define DB(x) { cerr << #x << ": " << x << " "; }
#define forn(i, n)  for (int i = 0; i < (int)(n); ++i)
#define sqr(x) ((x)*(x))
typedef long double ld;
typedef long long ll;
typedef vector <int> vi;
typedef pair <int,int> pii;
const ld PI = acos(-1.0);

bool solve(ll ax, ll ay, ll bx, ll by, ll cx, ll cy) {
    ll val1 = (bx - ax) * cx + (by - ay) * cy;
    ll val2 = cx*cx + cy*cy;

    if (val2 == 0) {
        if (ax == bx && ay == by) { cout << "YES" << endl; return 1; }
        return 0;
    }

    if (val1 % val2 != 0) { return 0; }

    ll t1 = val1 / val2;

    ll val3 = (bx - ax) - t1 * cx;
    ll val4 = cy;
    if (val3 == 0 || val3 % val4 == 0) { return 1; }
    return 0;
}

int main() {
  //freopen("in", "r", stdin);
  //freopen("out", "w", stdout);
  //ios_base::sync_with_stdio(0);

    ll ax, ay, bx, by, cx, cy;
    cin >> ax >> ay >> bx >> by >> cx >> cy;

    if (solve(ax, ay, bx, by, cx, cy)) { cout << "YES" << endl; return 0; }
    if (solve(ax, ay, -bx, -by, cx, cy)) { cout << "YES" << endl; return 0; }
/bin/bash: Ж: command not found
    cout << "NO" << endl;

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,m,c=0,d,cnt=0,b,x,y,z;
  cin>>n>>m;
  int a[m];
  for(int i=0;i<m;i++){
    cin>>a[i];
  }
  vector<int>v;
  d=n+1;
  for(int i=0;i<m;i++){
    if(i==0&&a[i]!=1){
            b=d-a[i];
        while(b--){
        v.push_back(a[i]);
    }
    }
    else if(i>0&&a[i]==1){
        x=a[i-1]-a[i];
        while(x--){
            v.push_back(a[i]);
        }
        break;
    }
    else if(i==0&&a[i]==1){
        y=d-1;
        while(y--){
        v.push_back(a[i]);
        }
        break;
    }
    else{
            z=a[i-1]-a[i];
    while(z--){
        v.push_back(a[i]);
    }

  }
  }
  for(int i=n-1;i>=0;i--){
    cout<<v[i]<<" ";
  }
}


#define _CRT_SECURE_NO_WARNINGS

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

using namespace std;

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

	int n, m;

	cin >> n >> m;

	vector<int>x(n), b(n);

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

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

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			if (x[i] == b[j])
			{
				cout << x[i] << ' ';
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
vector<vector<int>>vi(11);
int c[11] ,t[11];
vector<int>ex;
int main() {
	int n ;cin>>n;
	for(int i =0 ;i < n; i++){
		string s;cin>>s;
		cin>>c[i]>>t[i];
		for(int j = 0; j< 4 ;j++){
			int x = (int)s[j] - '0';
			vi[i].push_back(x);
		}
	}
	if(n==1&&c[0]==0&&t[0]==0)return cout<<"Need more data",0;
	vector<string>fi;
	for(int i = 0 ;i <= 9 ;i++)
		ex.push_back(i);
	do{
		vector<int>curr;
		for(int i = 0; i < 4 ; i++)
			curr.push_back(ex[i]);
		bool ok = true;
		for(int i = 0; i < n; i++){
			int cows = c[i];
			bool up[4] = {false};
			for(int j = 0; j < 4 ;j++){
				if(curr[j]==vi[i][j]){
					cows--;up[j] =true;
				}
			}
			if(cows!=0){
				ok = false;break;
			}
			cows = t[i];
			if(cows==0)continue;
			for(int j = 0 ;j <4 ;j++){
				for(int k = 0 ; k < 4 ;k++){
					if(k==j)continue;
					if(!up[k]&&curr[k]==vi[i][j]){
						cows--;up[k] = true;
					}
				}
			}
			if(cows!=0){
				ok = false;break;
			}
		}
		if(ok){
			bool found = false;
			string s = "";
			for(int i =0 ; i < 4 ; i++){
				s+=(char)(curr[i] + '0');
			}
			for(int i=  0 ;i < fi.size() ; i++){
				if(s==fi[i]){
					found =true;
					break;
				}
			}
			if(!found){
				fi.push_back(s);
			}
		}
	}while(next_permutation(ex.begin() , ex.end()));
	if(fi.size()==1)cout<<fi[0];
	else if(fi.size()==0)cout<<"Incorrect data";
	else cout<<"Need more data";
	return 0;
}


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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<to;x++)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,M;
ll mo=1000000007;

ll dp[2002][2002],dp2[2002][2002],dp3[2002][2002];

void solve() {
    int i,j,k,l,r,x,y; string s;

    cin>>N>>M;

    for(x=2;x<=2000;x++) dp[x][0]=dp2[x][0]=dp3[x][0]=1;
    for(y=1;y<=2000;y++) {
        ll tot=0;
        for(x=2;x<=2000;x++) {
            tot+=dp[x][y-1];
            dp[x][y]=(dp[x-1][y]+tot)%mo;
            dp2[x][y]=(dp[x][y]+mo-dp[x][y-1])%mo;
            dp3[x][y]=(dp3[x][y-1]+dp2[x][y])%mo;
        }
    }

    int y1,y2,w;
    ll ret=0;
    FOR(y1,N) for(y2=y1;y2<N;y2++) {
        for(w=2;w<=M;w++) ret+=dp3[w][y1]*dp3[w][N-1-y2]%mo(M+1-w)%mo;
        ret%=mo;
    }
    cout << ret << endl;
}

int main(int argc,char** argv){
    string s;int i;
    if(argc==1) ios::sync_with_stdio(false);
    FOR(i,argc-1) s+=argv[i+1],s+='\n';
    FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
    solve(); return 0;
}


#include <iostream>
#include <queue>
#include <stack>
#include <set>
#include <cstring>
#include <stdio.h>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <iterator>
#include <algorithm>
#include <functional>
#include <map>
#include <cassert>
#include <bitset>
#include <iomanip>

using namespace std;

typedef long long ll;
#define pii pair<ll, ll>
#define mp make_pair

ll read() {
    int f = 1;
    ll res = 0;
    char ch;
    do {
        ch = getchar();
        if (ch == '-') f = -f;
    } while (ch < '0' || ch > '9');
    do {
        res = res * 10 + ch - '0';
        ch = getchar();
    } while (ch >= '0' && ch <= '9');
    return f == 1 ? res : -res;
}

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

const int N = 100010;
const int mod = 1000000007;
const ll INF = 1e18;
int n,m,q;
int A[N], B[N];
char str[N];

int head[N*2], to[N * 2], nxt[N * 2], tot;
void addEdge(int u, int v) {
    tot++;
    nxt[tot] = head[u];
    to[tot] = v;
    head[u] = tot;
}

template <class T>
T mmax(T a, T b) {
    return a < b ? b: a;
}

template <class T>
T mmin(T a, T b) {
    return a < b? a: b;
}

int countOne(int set) {
    int res = 0;
    while (set) {
        res++;
        set &= set - 1;
    }
    return res;
}

bool contain(int set, int i) {
    return (set & (1 << i)) > 0;
}

ll myPow(ll a, int p) {
    if (p == 0) return 1;
    ll res = myPow(a, p / 2);
    res *= res;
    res %= mod;
    if (p % 2 == 1) {
        res *= a;
        res %= mod;
    }
    return res;
}

void addMode(int &a, ll b) {
    a = (a+b) % mod;
}

ll mul(ll a, ll b) {
    return a * b % mod;
}

template <class T>
void mySwap(T &a, T &b) {
    T tmp = a;
    a = b;
    b = tmp;
}

struct Query {
    int time, l, r, type;
    int speed, vol;
};

vector<int> added;
vector<Query> query;

const int M = N * 4;
struct ST {
    pii f[M], e[M];
    ll res[M], mn[M];

    void init() {
        fill(mn, mn+M, INF);
    }
    void build(int x, int nl, int nr) {
        if (nl == nr) {
            mn[x] = INF;
            return;
        }
        int m = (nl + nr) / 2;
        build(x*2, nl, m);
        build(x*2+1, m+1, nr);
        mn[x] = INF;
    }

    void update(int x) {
        if (mn[x*2] == INF) {
            int child = x*2+1;
            f[x] = f[child];
            e[x] = e[child];
            res[x] = res[child];
            mn[x] = mn[child];
            return;
        }
        if (mn[x*2+1] == INF) {
            int child = x*2;
            f[x] = f[child];
            e[x] = e[child];
            res[x] = res[child];
            mn[x] = mn[child];
            return;
        }
        int l = x*2;
        int r = x*2+1;
        f[x] = f[l];
        e[x] = e[r];
        ll diff = e[l].second * (f[r].first - e[l].first);
        res[x] = res[l] + diff + res[r];
        mn[x] = mmin(mn[l], res[l] + diff);
        mn[x] = mmin(mn[x], res[l] + diff + mn[r]);
    }

    void set(int l, int time, int speed, int x, int nl ,int nr) {
        if (nl == nr) {
            f[x] = mp(time, speed);
            e[x] = mp(time, speed);
            res[x] = 0;
            mn[x] = 0;
            return;
        }
        int m = (nl + nr) / 2;
        if (l <= m) set(l, time, speed, x*2, nl, m);
        else set(l, time, speed, x*2+1, m+1, nr);
        update(x);
    }

    void del(int l, int x, int nl , int nr) {
        if (nl == nr) {
            mn[x] = INF;
            return;
        }
        int m = (nl + nr) / 2;
        if (l <= m) del(l, x*2, nl, m);
        else del(l, x*2+1, m+1, nr);
        update(x);
    }

    struct node {
        double to;
        ll v;
    };
    node query(int l, int r, ll v, int ss, int rr, int x, int nl, int nr) {

        if (mn[x] == INF) return {-1, v};
        if (l > r) return {-1, v};
//        if (rr <= f[x].first) {
//            return {-1, v};
//        }
        if (l == nl && r == nr) {
            if (f[x] == e[x]) {
                ll nv = res[x] + v + (rr - e[x].first) * e[x].second;
                if (nv > 0) return {-1, nv};
                else return {e[x].first - (res[x] + v) * 1.0 / e[x].second, 0LL};
            }
            int lchild = x*2;
            int rchild = x*2+1;
            int m = (nl + nr) / 2;
            if (mn[rchild] == INF) return query(l, m, v, ss, rr, x*2, nl, m);
            if (mn[lchild] == INF) return query(m+1, r, v, ss, rr, x*2+1, m+1, nr);

            if (mn[lchild] + v <= 0) return query(l, m, v, ss, mmin(rr, f[x*2+1].first), x*2, nl, m);
            ll diff = e[lchild].second * (f[rchild].first - e[lchild].first);
            if (v + res[lchild] + diff <= 0) {
                return {e[lchild].first - (v + res[lchild]) * 1.0 / e[lchild].second, 0};
            }

            if (mn[x] + v > 0) {
                ll nv = res[x] + v + (rr - e[x].first) * e[x].second;
                if (nv <= 0) {
                    return {e[x].first - (v + res[x]) * 1.0 / e[x].second, 0};
                } else {
                    return {-1, nv};
                }
            } else {
                v = v + res[lchild] + diff;
                return query(m+1, r, v, ss, rr, x*2+1, m+1, nr);
            }
        }

        int lchild = x*2;
        int rchild = x*2+1;
        int m = (nl + nr) / 2;
        if (mn[rchild] == INF) return query(l, mmin(m, r), v, ss, rr, x*2, nl, m);
        if (mn[lchild] == INF) return query(mmax(l, m+1), r, v, ss, rr, x*2+1, m+1, nr);
        node res = query(l, mmin(r, m), v, ss, f[x*2+1].first, x*2, nl, m);
        if (res.to != -1) return res;
        return query(mmax(l, m+1), r, res.v, ss, rr, x*2+1, m+1, nr);
    }
}st;

int main() {

    fast_io();
    cout << fixed << setprecision(10);

    cin >> n;
    for (int i = 0; i < n; i++) {
        int tp;
        cin >> tp;
        Query q;
        q.type = tp;
        if (tp == 1) {
            int time, speed;
            cin >> time >> speed;
            q.time = time;
            q.speed = speed;
            added.push_back(time);
        } else if (tp == 2) {
            int time;
            cin >> time;
            q.time = time;
        } else {
            int l, r, v;
            cin >> l >> r >> v;
            q.l = l;
            q.r = r;
            q.vol = v;
        }
        query.push_back(q);
    }
    if (added.size() == 0) added.push_back(0);

    sort(added.begin(), added.end());
    added.erase(unique(added.begin(), added.end()), added.end());

    int m = added.size();
    st.init();
    st.build(1, 1, m);

    for (int i = 0; i < n; i++) {
        Query q = query[i];
        if (q.type == 1) {
            int idx = lower_bound(added.begin(), added.end(), q.time) - added.begin();
            idx++;
            int time = q.time;
            int speed = q.speed;
            st.set(idx, time, speed, 1, 1, m);
        } else if (q.type == 2) {
            int idx = lower_bound(added.begin(), added.end(), q.time) - added.begin();
            idx++;
            st.del(idx, 1, 1, m);
        } else {
            if (q.vol == 0) {
                cout << q.l << '\n';
            } else {
                int l = lower_bound(added.begin(), added.end(), q.l) - added.begin();
                l++;
                int r = lower_bound(added.begin(), added.end(), q.r) - added.begin();
                cout << st.query(l, r, q.vol, q.l, q.r, 1, 1, m).to << '\n';
            }
        }
    }
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
	int n,L=0;
	cin>>n;
	int i=0;
	while (i<=n)
	{
		for (int j=1; j-1<=i; j++)
		{
			i += j;
		}
		if (i<=n)
		{
			L++;
		}
	}
	cout<<L;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define p 998244853
#define N 100003
#define reg register
using namespace std;

int n,m,ans;
int fac[N],ifac[N],s[N];

inline int power(int a,int t){
    int res = 1;
    while(t){
        if(t&1) res = (ll)res*a%p;
        a = (ll)a*a%p;
        t >>= 1;
    }
    return res;
}

inline int C(int x,int y){
    if(x<y) return 0;
    return (ll)fac[x]*ifac[y]%p*ifac[x-y]%p;
}

inline int dec(int x,int y){
    return x<y?x-y+p:x-y;
}

inline int add(int x,int y){
    return x+y>=p?x+y-p:x+y;
}

int main(){
    scanf("%d%d",&n,&m);
    fac[0] = fac[1] = ifac[0] = ifac[1] = 1;
    for(reg int i=2;i<=4000;++i) ifac[i] = fac[i] = (ll)fac[i-1]*i%p;
    ifac[4000] = power(fac[4000],p-2);
    for(reg int i=3999;i>1;--i) ifac[i] = (ll)ifac[i+1]*(i+1)%p;
    for(reg int i=0;i<=n;++i){
        if(i+m>n) s[i] = C(n+m,m+i);
        else s[i] = C(n+m,m);
    }
    for(reg int i=0;i<n;++i) s[i] = dec(s[i],s[i+1]);
    for(reg int i=0;i<=n;++i) ans = (ans+(ll)s[i]*i)%p;
    printf("%d",ans);
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
struct Node{
  Node* next[100];
};
int num,n,k,d;
int ans;
bool cd;
void trv(Node*root){
  if(root==0){
    return;
  }
   if(num==n&&cd) {
    ans++;
    ans%=mod;
    return;
  }
  for(int i=0;i<k;++i){
    num+=i+1;
    if(i+1>=d) cd=1;
    trv(root->next[i]);
    num-=i+1;
    if(i+1>=d) cd=0;
  }
}
void k_tree(int mx,Node*&root){
  if(mx>=n){
    for(int i=0;i<k;++i){
      root->next[i]=0;
    }
    return;
  }
  for(int i=0;i<k;++i){
    root->next[i]=new Node;
    k_tree(mx+i+1,root->next[i]);
  }
}
void dk_tree(Node*&root){
  if(root==0) return;
  for(int i=0;i<k;++i){
    dk_tree(root->next[i]);
  }
  delete root;
}
int main(){
  Node*root=new Node;
  cin>>n>>k>>d;
  k_tree(0,root);
  trv(root);
  dk_tree(root);
  cout<<ans;
  return 0;
}


/*          ****vestovia
*/
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long
#define pb push_back
#define mp make_pair
#define pi pair<int,int>
#define vi vector<int>
#define sc(x) scanf("%d",&x)
#define sc2(x,y) scanf("%d%d",&x,&y)
#define sc3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define sc4(x,y,z,u) scanf("%d%d%d%d",&x,&y,&z,&u)
#define scll(x) scanf("%lld",&x)
#define scll2(x,y) scanf("%lld%lld",&x,&y)
#define scll3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define pr(x) printf("%d\n",x)
#define prll(x) printf("%lld\n",x)
int main()
{
    int y,i,j,k,l;
    ios::sync_with_stdio(0);
    while(1){
    cin>>y;
    if(y%400==0||(y%4==0&&y%100!=0)&&((y+28)%100!=0||(y+28)%400==0))
        cout<<y+28;
    else if((y+28)%100==0)
        cout<<y+68;
    else
    {
        l=y%4;
        k=y+4-l;
        i=4-l;
        if((y+4-l)%4==0&&(y+4-l)%100==0)
            k=y+8-l;
        if((l==1)&&k==y+4-l)
            cout<<y+6;
        else if((l==1||l==2)&&k==y+8-l)
            cout<<y+12;
        else if(l==3&&k==y+8-l)
            cout<<y+6;
        else if((l==3||l==2)&&k==y+4-l)
            cout<<y+11;
        else
            cout<<y+6;

    }
    cout<<endl;
}
}


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

using namespace std;

int f[51][1515];

int main() {
    int n, s, k;
    scanf("%d %d %d", &n, &s, &k);
    s--;
    vector<int> a(n);
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    string c;
    cin >> c;

    memset(f, 0xff, sizeof(f));
    for (int i = 0; i < n; i++)
        f[i][abs(i - s)] = a[i];

    for (int t = 0; t < 1414; t++) {
        for (int i = 0; i < n; i++)
            if (f[i][t] > 0) {
                if (f[i][t] >= k) {
                    printf("%d\n", t);
                    return 0;
                }
                for (int j = 0; j < n; j++)
                    if (c[i] != c[j] && a[j] > a[i]) {
                        f[j][t + abs(i - j)] = max(f[j][t + abs(i - j)], f[i][t] + a[j]);
                    }
            }
    }

    printf("-1\n");

    return 0;
}


#include <iostream>
#include <ios>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <unordered_map>
#include <boost/functional/hash.hpp>

int const maxt = 10000;
std::unordered_map<std::pair<int, int>, int, boost::hash<std::pair<int, int>>> d;
std::vector<std::pair<int, int>> p;
std::string str;
int start = 0;

int min_time(int i, int s) {
//	std::cout << "i = " << i << " s = " << s << "\n";
	std::pair<int, int> x {i, s};
	if (! d.count(x)) {
//		std::cout << p[i].first << " " << s << "\n";
		int res = (p[i].first >= s) ? 0 : maxt;
		for (int j = 0; j < i; ++j)
			if (str[p[i].second] != str[p[j].second])
				res = std::min(res, min_time(j, (s > p[i].first) ? s - p[i].first : 0) + std::abs(p[i].second - p[j].second));
		d[x] = res;
	}
//	std::cout << "d[" << i << "," << s << "] = " << d[x] << "\n";
	return d[x];
}

main() {
	std::ios_base::sync_with_stdio(false);
	int n = 0, k = 0;
	std::cin >> n >> start >> k;
	int a[n];
	for (int i = 0; i < n; ++i)
		std::cin >> a[i];
	std::cin >> str;
	for (int i = 0; i < n; ++i)
		p.push_back({a[i], i});
	std::sort(p.begin(), p.end());
	std::reverse(p.begin(), p.end());
//	std::cout << "sorted\n";
//	for(int i = 0; i < n; ++i)
//		std::cout << p[i].first << " " << p[i].second << "\n";
/*	for (int i = 0; i < n; ++i)
		d[{i, 0}] = std::abs(p[i].second - start + 1);*/
	int res = maxt;
	for (int i = 0; i < n; ++i)
		res = std::min(res, min_time(i, k) + std::abs(p[i].second - start + 1));
	if (res < maxt)
		std::cout << res << "\n";
	else
		std::cout << "-1\n";
}


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

#define si(n) scanf("%lld",&n)
#define ll long long
#define MAX 200005

const int mod=(int) 1e9+7;
ll fact[MAX],inv[MAX];

int mod_add(ll a,ll b) {
  a+=b;
  if(a>=mod) a-=mod;
  return a;
}

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

int mod_mul(ll a, ll b) {
    a%=mod;
    b%=mod;
  return (a*b)%mod;
}

int bigmod(long long a,long long p)
{
    long long ret = 1;
    a%=mod;
    while(p) {
        if(p & 1) ret = mod_mul(ret,a);
        a = mod_mul(a,a);
        p >>= 1;
    }
    return ret;
}

int mod_div(ll a,ll b)
{
    return mod_mul(a,bigmod(b,mod-2));
}

int mod_power(int a,int b) {
  int res=1;
  while (b>0) {
    if (b&1) {
      res=mod_mul(res,a);
    }
    a=mod_mul(a,a);
    b>>=1;
  }
  return res;
}

void factorial()
{
    fact[0]=1;
    inv[0]=1;
    for(int i=1;i<50;i++){
        fact[i]=mod_mul(fact[i-1],i);
        inv[i]=mod_power(fact[i],mod-2);
    }
}

ll nCr(ll n,ll r)
{
    if(r>n)return 0;
    ll ret=1;
    for(ll i=n-r+1;i<=n;i++)ret=mod_mul(ret,i);
    ret=(ret*inv[r])%mod;
    return ret;
}

int main()
{
    //freopen("input.txt","r",stdin);
    factorial();
    ll n,s,f[25];
    int i,j;

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

    ll ans=nCr(s+n-1,n-1);

    for(i=0;i<n;i++){
        ll now=s;
        int cnt=0;
        for(j=0;j<n;j++){
            if((i & (1<<j))){
                now-=(f[j]+1);
                cnt++;
            }
        }
        if(now<0 || !cnt)continue;
        if(cnt%2){
            ans=mod_sub(ans,nCr(now+n-1,n-1));
        }
        else ans=mod_add(ans,nCr(now+n-1,n-1));
    }

    cout<<ans<<endl;

    return 0;
}


#include<stdio.h>

int main()
{
    int a,b;
    while(scanf("%d %d", &a,&b)){
        int count=0;
        while(a<=b){
            a = 3*a;
            b = 2*b;
            count++;
        }
        printf("%d",count);
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M=1e9+7;
const int N=1510;
int mak[N][N],n,m,a,b,kk,g[N][N],f[N][N];
int fp(int x,int y){
	if (y<0) return 0;
	int ret=1;
	for (; y; y>>=1,x=(ll)x*x%M) if (y&1) ret=(ll)ret*x%M;
	return ret;
}
bool inter(int a,int b,int c,int d){
	return !((b<c)||(a>d));
}
int fac[N],inv[N];
int c(int x,int y){
	return (ll)fac[x]*inv[x-y]%M*inv[y]%M;
}
int main(){
	fac[0]=1; for (int i=1; i<=1500; ++i) fac[i]=(ll)fac[i-1]*i%M;
    inv[1500]=fp(fac[1500],M-2); for (int i=1499; i>=0; --i) inv[i]=(ll)inv[i+1]*(i+1)%M;
	scanf("%d%d",&n,&m);
	scanf("%d%d",&a,&b);
	scanf("%d",&kk);
	int p=(ll)a*fp(b,M-2)%M,pp=M+1-p;
	//cerr<<(ll)p*10%M<<" "<<pp<<endl;
	for (int i=1; i<=m; ++i)
		for (int j=i; j<=m; ++j){
		    mak[i][j]=(ll)fp(p,i-1)*fp(pp,kk-(i-1))%M*fp(p,m-j)%M*fp(pp,kk-(m-j))%M*
				c(kk,i-1)%M*c(kk,m-j)%M;
			//cout<<i<<" "<<j<<" "<<mak[i][j]<<endl;
		}
	g[1][m]=1;
	//cerr<<"!!!"<<endl;
	for (int i=1; i<=n; ++i){
		memset(f,0,sizeof(f));
		for (int j=1; j<=m; ++j)
			for (int k=j; k<=m; ++k){
				for (int l=1; l<=m; ++l)
					for (int z=l; z<=m; ++z){
						if (inter(l,z,j,k)) (f[j][k]+=g[l][z])%=M;
						//cerr<<l<<" "<<z<<" "<<j<<" "<<k<<" "<<inter(l,z,j,k)<<endl;
					}
				f[j][k]=(ll)f[j][k]*mak[j][k]%M;
			}
		memcpy(g,f,sizeof(f));
	}
	int ans=0;
	for (int i=1; i<=m; ++i)
		for (int j=i; j<=m; ++j)
			(ans+=f[i][j])%=M;
	cout<<ans;
}


#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define in freopen("A.in", "r", stdin);
#define out freopen("A.out", "w", stdout);

using namespace std;

const int N = 1000;

int TN = 1;

int n;
int a[N];
int total;
map<int, bool> us;

bool check(int k) {
    int sum = 0;
    for (int i = 1; i <= n; i++) {
        sum += a[i];
        if (sum == k) {
            sum = 0;
        }
        else if (sum > k) {
            return 0;
        }
    }
    return !sum;
}

void solve() {
    cin >> n; set<int> st;
    for (int i = 1; i <= n; i++) {
        char c; cin >> c; a[i] = c - 48;
        total += a[i];
    }
    if (!total) {
        puts("YES"); return;
    }
    for (int i = 1; i * i <= total; i++) {
        if (total % i == 0) {
            if (check(i) || check(total / i) && i > 1) {
                puts("YES"); return;
            }
        }
    }
    puts("NO");
    return;
}

signed main() {
    // ios_base::sync_with_stdio(0);
    // in; out; // cin >> TN;
    while (TN--) solve();
    return 0;
 }


#include <bits/stdc++.h>

using namespace std;

string s;
int check(int i,string x)
{
    int flag=1;
    for(int j=0;j<x.size();j++){
        if(s[i]!=x[j])
            flag=0;
        i++;
    }
    return flag;
}
int main(int argc, char const *argv[])
{

    cin >> s;
    string a,b,c,d,e;
    a="Danil";b="Olya";c="Ann";d="Nikita",e="Slava";
    int A,B,C,D,E;
    A=B=C=D=E=0;
    for(int i=0;i<s.size()-a.size()+1;i++){
        if(check(i,a)){
            A++;
        }
    }
    for(int i=0;i<s.size()-b.size()+1;i++){
        if(check(i,b)){
            B++;
        }
    }
    for(int i=0;i<s.size()-c.size()+1;i++){
        if(check(i,c)){
            C++;
        }
    }
    for(int i=0;i<s.size()-d.size()+1;i++){
        if(check(i,d)){
            D++;
        }
    }
    for(int i=0;i<s.size()-e.size()+1;i++){
        if(check(i,e)){
            E++;
        }
    }
    if(A+B+C+D+E==1)
        printf("YES\n");
    else printf("NO\n");
    return 0;
}


#include <iostream>

using namespace std;

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

    if((n-m)%2==0)
    k=(n-m)/2;
    if((n-m)%2!=0)
    {
    if(m==0)
        k=1;
    else
    k=n-m;
    }

    return k;
}


#include<iostream>
#include<stdio.h>
using namespace std;
int   r,h;
int main(){

    while(scanf("%d%d",&r,&h)){
        int ans = h/r;
        ans = ans * 2;
        h = h % r;

       if(h*2>=r){
        if( 4*h*h >= 3*r*r ){
            ans += 3;}
            else {ans += 2;}
        }
        else ans++;

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


#include <iostream>
#include <algorithm>

using namespace std;

int n;

const int MODULUS = 1000000007;

int f(int x, int y)
{
    int z = n;
    for (int i = 0; i < x; ++i)
    {
        z /= 2;
    }
    for (int i = 0; i < y; ++i)
    {
        z /= 3;
    }
    return z;
}

int answer[1000000 + 1][20][2];

int max_x;
int max_y;

int get_answer(int len, int x, int y)
{
    long long ans = 0;

    if (answer[len][x][y] != -1)
    {
        return answer[len][x][y];
    }

    long long p = f(x, y) - (len - 1);
    if (p > 0)
    {
        ans += p * get_answer(len - 1, x, y) % MODULUS;
        if (ans >= MODULUS)
            ans -= MODULUS;
    }
    if (x < max_x && f(x + 1, y) >= len - 1)
    {
        p = f(x, y) - f(x + 1, y);
        ans += p * get_answer(len - 1, x + 1, y) % MODULUS;
        if (ans >= MODULUS)
            ans -= MODULUS;
    }

    if (y == 0 && f(x, y + 1) >= len - 1)
    {
        p = f(x, y) - f(x, y + 1);
        ans += p * get_answer(len - 1, x, y + 1) % MODULUS;
        if (ans >= MODULUS)
            ans -= MODULUS;
    }
    return answer[len][x][y] = ans;
}

int solve()
{
    int z = n;
    while (z != 1)
    {
        z >>= 1;
        ++max_x;
    }
    answer[1][max_x][0] = 1;
    if ((1 << (max_x - 1)) * 3 <= n)
    {
        answer[1][max_x - 1][1] = 1;
        max_y = 1;
    }
    for (int i = 1; i < n; ++i)
    {
        for (int x = 0; x <= max_x; ++x)
        {
            for (int y = 0; y <= max_y; ++y)
            {
                int z = answer[i][x][y];
                if (z)
                {
                    answer[i + 1][x][y] += max(0, (f(x, y) - i)) * z % MODULUS;
                    if (answer[i + 1][x][y] >= MODULUS)
                        answer[i + 1][x][y] -= MODULUS;
                    if (x > 0)
                    {
                        answer[i + 1][x - 1][y] += (f(x - 1, y) - f(x, y)) * z % MODULUS;
                        if (answer[i + 1][x - 1][y] >= MODULUS)
                            answer[i + 1][x - 1][y] -= MODULUS;
                    }
                    if (y > 0)
                    {
                        answer[i + 1][x][y - 1] += (f(x, y - 1) - f(x, y)) * z % MODULUS;
                        if (answer[i + 1][x][y - 1] >= MODULUS)
                            answer[i + 1][x][y - 1] -= MODULUS;
                    }
                }
            }
        }
    }
    return answer[n][0][0];
}

int main()
{
    cin >> n;
    cout << solve() << endl;
    return 0;
}


#include <iostream>
#include <cassert>

#define lint long long int
using namespace std;

int main()
{
    lint x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;

    lint pe_rand = (y2 - y1 + 1) / 2;
    if (y1 % 2 != y2 % 2) {
        assert(0);
        cout << (x2 - x1 + 1) * pe_rand << endl;
        return 0;
    }

    //Daca trece prin 2 centre
    if ((x1 + y1) % 2 == 0)
        cout << (x2 - x1 + 1ll) * pe_rand + (x2 - x1) / 2 + 1ll << endl;
    else {
        cout << (x2 - x1 + 1ll) * pe_rand + (x2 - x1) / 2 << endl;
    }
    return 0;
}


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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<to;x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

ll X1,X2,Y1,Y2;
ll ret;

void solve() {
	int i,j,k,l,r,x,y; string s;
	cin>>X1>>Y1>>X2>>Y2;
	assert(X1<X2);
	if((Y2-Y1)%2==1) ret=(X2-X1+1)*((Y2-Y1+1)/2);
	else ret=(X2-X1+1)*((Y2-Y1)/2)+(X2-X1)/2+(abs(X1%2)==abs(Y1%2));
	cout<<ret<<endl;
}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}


#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int bit[31];
int n,p;
int sm,minn;

bool dfs(int ii,int n,int now,int dep)
{
	//if(ii==3)

	if(dep==ii&&n==0)
	{
		return true;
	}
	if(dep>ii||n<0)return false;
	for(int i=now;i<=30&&bit[i]<=n;i++)
	{
		if(
		dfs(ii,n-bit[i],i,dep+1))return true;
	}
	return false;
}
int main(int argc, char** argv) {
	minn=1e9;
	bit[0]=1;
	for(int i=1;i<=30;i++)bit[i]=bit[i-1]*2;

	scanf("%d%d",&n,&p);int flag=0;
	n-=p;
	for(int i=1;i<=50;i++)
	{
	int k=n;//cout<<i<<endl;
	if(dfs(i,k,0,0))
		{flag=1;
		printf("%d\n",i);return 0;
		}
	n=n-p;
	}
	if(!flag)printf("-1\n");

	return 0;
}


// Author - Rahil Malhotra
#include <bits/stdc++.h>
using namespace std;

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

int nn;
int arr[10];
int fact[102];
int mod=1000000007;
int cache[11][102];
int n;
int inv(int x)
{
    int r,y;
    for(r=1,y=mod-2;y>0;x=(x*x)%mod,y/=2)
        if(y%2==1)
            r=r*x%mod;
    return r;
}
int nCr(int n, int m)
{
    return fact[n] *inv(fact[m])%mod *inv(fact[n-m])%mod ;
}
int dp(int pos,int taken)
{
    if(pos>10 || (taken<n && pos==10))
        return 0;
    if(taken==n)
    {
        if(pos!=10 && arr[pos]==0)
            return dp(pos+1,taken);
        return pos==10;
    }
    int &ans=cache[pos][taken];
    if(ans!=-1)
        return ans;
    ans=0;
    for(int i=arr[pos];i<=n-taken;i++)
        ans+=nCr(n-taken,i)*dp(pos+1,taken+i),ans%=mod;
    return ans;
}
int32_t main()
{
    IOS;
    fact[0]=1;
    for(int i=1;i<=101;i++)
        fact[i]=i*fact[i-1],fact[i]%=mod;
    cin>>nn;
    for(int i=0;i<10;i++)
        cin>>arr[i];
    int ans=0;
    for(int k=1;k<=nn;k++)
    {
        memset(cache,-1,sizeof(cache));
        n=k;
        for(int i=arr[0];i<=n-1;i++)
            ans+=nCr(n-1,i)*dp(1,i),ans%=mod;
    }
    cout<<ans<<endl;
}


	#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long int ll;
#define vec vector<int>
#define pb push_back
#define ma map<int,int>
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    ll i,j,k,l;
    ll n;
    cin>>n;
    if(n>=11&&n<20 || n==21)
    {
        cout<<4;
    }
    if(n==20)
    {
        cout<<15;
    }
    else{
        cout<<0;
    }
}


//
//  cc.cpp
//  CF
//
//  Created by Mohammadreza Raddanipour on 8/11/18.
//  Copyright © 2018 Mohammadreza Raddanipour. All rights reserved.
//

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

string ITS(int a){
    string s = "";
    while(a > 0){
        s += (a % 10) + 48;
        a -= a % 10;
        a /= 10;
    }
    return s;
}

int main(){
    int a;
    string tmp = "";
    cin >> a;
    for(int i = 1; i <= a; i++)
        tmp += ITS(i);
    cout << tmp.size() << '\n';
}


/*   -----------------------------
    | If you see my code,         |
    | You are BC (best coder -_- )|
    |                             |
     -----------------------------
*/
#include<bits/stdc++.h>
#include<iomanip>
typedef long long ll;
typedef unsigned long long ul;
#define to_lower(x)  transform(x.begin(), x.end(), x.begin(), ::tolower)
#define to_upper(x)  transform(x.begin(), x.end(), x.begin(), ::toupper)
#define pi acos(-1)
#define ll long long
#define ull unsigned long long
#define vec vector<ll>
#define f1(i,n) for(i=1;i<n;i++)
#define input  ll nn;cin>>nn;ll arr[nn];for(i=0;i<nn;i++)cin>>arr[i];
#define test   ll ttt;cin>>ttt;while(ttt--)
#define faster  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define faster2 ios_base::sync_with_stdio(0);
#define en '\n'
#define eps 10e9
#define pb push_back
#define print(v) for(auto u:v)cout<<u<<" ";
ll i,j;
using namespace std;
bool ar[1000000006];
vec v;
void seive()
{
    ar[0]=1;ar[1];
    for(i=2;i<sqrt(10000006);i++)
    {
        if(ar[i]==0)
        for(j=2;i*j<1000000006;j++)
        {
            ar[i*j]=true;
        }
    }
  /* for(j=2;j<10000006;j++)
   {
       if(ar[j]==0)v.push_back(j);
   }*/

}
int main()
{
    faster;
    seive();

    test{

  ll a,b;cin>>a>>b;
  if(a-b==1)
  {
      if(ar[a+b]==0)cout<<"YES\n";
      else cout<<"NO\n";
  }
  else cout<<"NO\n";}

}


#include<stdio.h>
int max(int a,int b)
{if(a>b)
return a;
return b;}
int main()
{int a,b,c;
scanf("%d%d%d",&a,&b,&c);
b--;c-=2;
int ans=max(a,max(b,c));
printf("%d",(ans+1)*3);}


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

int main()
{
	int n,k;
	cin>>n>>k;
	int a[n];
	int i,sum=0;
	int s[k];
	int j;
	for(i=0;i<n;i++)
	{
		cin>>a[j];
		sum=sum+a[j];
	}
	for(i=0;i<k;i++)
	{
		for(j=i;j<n;j+=k)
		{
			s[i]=sum-a[j];
		}
	}
	sort(s,s+k);
	cout<<s[k-1]<<endl;
	return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	char a[1000];
	while(~scanf("%s",a))
	{
		int count=0;
		for(int i=strlen(a)-1,j=0;;i--,j++)
		{
			if(a[i]!=a[j]) count++;
		}
		if(strlen(a)%2==0&&count==1) printf("YES\n");
		else if(strlen(a)%2==1&&count<=1) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}


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

int main()
{
   int a[4], c, l;
   list <int> v;
   for(int i = 0;i < 4; i++)
   {
       cin >> a[i];
   }
   cin >> c;
   for(int i = 0, k = 1;i < 4; )
   {
       if(k > c)
       {
           k = 1;
           i++;
       }
       else
       {
           l =  k % a[i];
           if(l == 0)
           {
               if(!(std::find(v.begin(), v.end(), k) != v.end()))
               {
                    v.push_back(k);
               }

           }
       }
       k++;
   }

   cout << v.size();

}


#include<bits/stdc++.h>

using namespace std;
template < typename T > inline T read()
{
	T x = 0, f = 0;
	char c = getchar();
	while (!isdigit(c)) f = c == '-', c = getchar();
	while (isdigit(c)) x = x * 10 + c - 48, c = getchar();
	return f ? -x : x;
}
namespace run
{
	typedef long long ll;
	const int N = 500009,
	          inf = 1e9;
	int mx, _mx;
	inline void upd(int x)
	{
		if (x > mx) _mx = mx, mx = x;
		else if (x > _mx) _mx = x;
	}

	int x, c, k, n, a[N], sum[N << 1], pre[N << 1], A;
	ll ans, cnty;
	int main()
	{
		n = read < int > (), k = read < int > ();
		for (int i = 1; i <= n; ++i)
			++sum[a[i] = read < int > ()], A = max(A, a[i]);
		pre[0] = -1;
		for (int i = 1; i <= 2 * A; ++i)
			pre[i] = sum[i] ? i : pre[i - 1], sum[i] += sum[i - 1];

		for (int i = 1; i <= A; ++i)
		{
			cnty = 0;
			for (int j = A / i * i; j; j -= i) cnty += sum[A] - sum[j - 1];
			mx = -inf, _mx = -inf;
			for (int j = A / i * i; j >= 0; j -= i)
			{
				x = pre[j + i - 1];
				if (x >= j)
				{
					c = sum[x] - sum[x - 1];
					upd(x % i);
					if (c > 1) upd(x % i);
					else if (pre[x - 1] >= j) upd(pre[x - 1] % i);
				}

				x = (j + mx) / 2;
				if (x > 1 && cnty - j / i > 1)
					ans = max(ans, 1 ll * i * min(1 ll * x, cnty - j / i));

				if (mx >= 0 && _mx >= 0)
				{
					x = j + _mx;
					if (x > 1 && cnty - j / i * 2 > 1)
						ans = max(ans, 1 ll * i * min(1 ll * x, cnty - j / i * 2));
					x = j + mx;
					if (x > 1 && cnty - j / i * 2 - 1 > 1 && sum[A] - sum[x - 1] >= 2)
						ans = max(ans, 1 ll * i * min(1 ll * x, cnty - j / i * 2 - 1));
				}
			}
		}
		printf("%lld", ans);
		return 0;
	}
}
int main()
{
	return run::main();
}


#include<bits/stdc++.h>
#define N 250005
using namespace std;
int n,k,mo,ans;
int hr[N],fac[N],inv[N],f[27][N];
int power(int x,int y){
	int s=1;
	for (;y;y/=2,x=1ll*x*x%mo)
		if (y&1) s=1ll*s*x%mo;
	return s;
}
int main(){
	scanf("%d%d%d",&n,&k,&mo);
	inv[0]=inv[1]=fac[0]=1;
	for (int i=2;i<=n;i++)
		inv[i]=1ll*(mo-mo/i)*inv[mo%i]%mo;
	for (int i=1;i/2<=n;i*=2)
		for (int j=i/2;j<i&&j<=n;j++)
			hr[j]=i;
	for (int i=1;i<=n;i++){
		fac[i]=1ll*fac[i-1]*i%mo;
		inv[i]=1ll*inv[i-1]*inv[i]%mo;
	}
	ans=power(k,n);4
	if (n%2==0){
		f[0][0]=1;
		for (int i=0;i<26;i++)
			for (int j=0;j<=n;j++)
				if (f[i][j]&&(j&n)==j)
					for (int k=j+hr[j];k<=n;k=(k+1)|j)
						if ((k&n)==k)
							f[i+1][k]=(f[i+1][k]+1ll*f[i][j]*inv[k-j]%mo);
		int tmp=0;
		for (int i=1;i<=26;i++)
			tmp=(tmp+1ll*f[i][n]*inv[k-i])%mo;
		ans=(ans+mo-1ll*tmp*fac[n]%mo*fac[k]%mo)%mo;
	}
	printf("%d",ans);
}


/*
	ID: sixathofpain
	TASK: Sonya and Informatics
	LANG: C++

*/

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define setbits(x) __builtin_popcount(x)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<db, db> pdd;
typedef vector<int> vi;
#define m1 make_pair
#define in insert
#define pb push_back
#define IOS                           \
    std::ios::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define endl "\n"
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

inline ll gcd(ll a, ll b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
inline ll power(ll a, ll n, ll m)
{
    if (n == 0)
        return 1;
    ll p = power(a, n / 2, m);
    p = (p * p) % m;
    if (n % 2)
        return (p * a) % m;
    else
        return p;
}
const ll MOD = 1000000007;
const int N = int(3e5) + 999;
#define int long long int

//matrix multiplication

#define FOR(x,to) for(x=0;x<(to);x++)
#define ZERO(a) memset(a,0,sizeof(a))
const int MAT=101;
//MAT is the size of the matrix
ll mo =MOD;

struct Mat {
 ll v[MAT][MAT];
 Mat(){
 	ZERO(v);
 };
};

//structure Mat defines the matrix

Mat mulmat(Mat& a,Mat& b,int n=MAT) {

	ll mo2=4*mo*mo;
	int x,y,z; Mat r;
	FOR(x,n) FOR(y,n) r.v[x][y]=0;
	FOR(x,n) FOR(z,n) FOR(y,n) {
		r.v[x][y] += a.v[x][z]*b.v[z][y];
		if(r.v[x][y]>mo2) r.v[x][y] -= mo2;
	}
	FOR(x,n) FOR(y,n) r.v[x][y]%=mo;
	return r;
}

//mulmat mulitplies the matrix a and b with size n

Mat powmat(ll p,Mat a,int n=MAT) {
	int i,x,y; Mat r;
	FOR(x,n) FOR(y,n) r.v[x][y]=0;
	FOR(i,n) r.v[i][i]=1;
	while(p) {
		if(p%2) r=mulmat(r,a,n);
		a=mulmat(a,a,n);
		p>>=1;
	}
	return r;
}

//p is the power
//n is the size
//a is the matrix to be exponentiated
int modi;
Mat A;
int nc2(int x){
	int ans = (x*((x-1+MOD)%MOD))%MOD;
	ans%=MOD;
	ans*=modi;
	ans%=MOD;
	return ans;
}

int32_t main(){
    IOS;

    int n,k;
    cin>>n>>k;
    int s[n];
    for(int i=0;i<n;i++)
    	cin>>s[i];
    int l = 0,c=0;
    for(int i=0;i<n;i++)
    	if(s[i]==0)
    		l++;
    for(int i=0;i<l;i++){
    	if(s[i]==0)
    		c++;
    }
    if(l==0 or l==n){
    	cout<<1<<endl;
    	return 0;
    }
    modi = power(2,MOD-2,MOD);
    // int dp[101] = {0};
    // dp[c] = 1;
    for(int i=0;i<=l;i++){
    	if(i-1>=0){
    		A.v[i][i-1] = ((l-i+1)*(l-i+1))%MOD;

    	}
    	A.v[i][i] = (nc2(l) + nc2(n-l) + i*(l-i) + (l-i)*(n-2*l+i))%MOD;// + nc2(n-l);
    	if(i+1<=l){
    		A.v[i][i+1] = ((i+1)*(n-2*l+i+1))%MOD;//    	A.v[i][i-1] = A.v[i][i] = A.v[i][i+1] = 0;
    	}
    // }
    // for(int i=0;i<=l;i++){
    // 	for(int j=0;j<=l;j++){
    // 		cout<<A.v[i][j]<<" ";
    // 	}
    // 	cout<<endl;
    // }
    A= powmat(k,A,101);
    // for(int i=0;i<=l;i++){
    // 	for(int j=0;j<=l;j++){
    // 		cout<<A.v[i][j]<<" ";
    // 	}
    // 	cout<<endl;
    // }
    int ans = 0;
    for(int i = 0;i<=l;i++){
    	ans = ans + A.v[i][c];
    	ans%=MOD;
    }

    int sol = A.v[l][c];
    sol = sol*power(ans,MOD-2,MOD);
    sol%=MOD;
    cout<<sol<<endl;

    return 0;
}