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

const int N = 50 + 5, mod = 1e9 + 7;
int n, k;
int a[N], dp[N][N][102];

int solve(int l, int r, int left)
{
    if (l > r)
        return 0;
    if (dp[l][r][left] != -1)
        return dp[l][r][left];
    int ans = 0;
    if (a[l] < 0)
    {
        if (left > 0)
            ans = max(ans, solve(l + 1, r, left - 1) + a[l]);
        if (left > 1)
            ans = max(ans, solve(l + 1, r, left - 2));
    }
    if (a[l] >= 0 && left > 0)
        ans = max(ans, solve(l + 1, r, left - 1) + a[l]);
    if (a[r] < 0)
    {
        if (left > 0)
            ans = max(ans, solve(l, r - 1, left - 1) + a[r]);
        if (left > 1)
            ans = max(ans, solve(l, r - 1, left - 2));
    }
    if (a[r] >= 0 && left > 0)
        ans = max(ans, solve(l, r - 1, left - 1) + a[r]);
    return dp[l][r][left] = ans;
}

int main()
{
    //ios::sync_with_stdio(false);cin.tie(nullptr);ios_base::sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    memset(dp, -1, sizeof dp);
    cout << solve(0, n - 1, k);
    return 0;
}

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rrep(i, n) for(int i = 0; i <= (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const ll INF = 1LL<<60;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using Graph = vector<vector<int>>;

typedef pair<int, int> P;
typedef priority_queue<int, vector<int>, greater<int>> PQ;

int main(){
 int N, K; cin >> N >> K;
    int ans = 0;
    deque<int> deq;
    vector<int> v(N,0);
    rep(i,N){
        cin >> v[i];
    }
    // 左から取る個数
    for (int l = 0; l <= K; l++){
        // 右から取る個数
        for (int r = 0; r <= K; r++){
            // 取る回数がKを越した場合はNG
            if (l + r > K) continue;
            int d = K - l - r;
            priority_queue<int, vector<int>, greater<int>> tmp;
            // 左から取る
            rep(i,l){
                // dequeの右端に達した場合
                if (i >= N) break;
                tmp.push(v[i]);
            }
            // 右から取る
            rep(i,r){
                int index = N-1-i;
                // dequeの左端もしくはすでに左から取られている場合
                if (index < 0 || index <= l) break;
                tmp.push(v[index]);
            }
            // 手から宝石をつめる
            rep(i,d){
                // 手に持っていない場合
                if (tmp.empty()) break;
                // 価値は正の場合はつめない
                if (tmp.top() >= 0) break;
                tmp.pop();
            }

            int now = 0;
            while (!tmp.empty()){
                now += tmp.top(); tmp.pop();
            }

            ans = max(now, ans);
        }
    }

    cout << ans << endl;
}


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

    }else{
        return a.second < b.second;
    }
}
bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}

const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};
int main() {
    ll H,W;
    cin>>H>>W;
    vector<vector<char>> G(H,vector<char>(W));
    ll cnt = 0;
    rep(i,H){
        rep(j,W){
            cin>>G[i][j];
            if(G[i][j] == '.'){
                cnt++;
            }
        }
    }

    vector<vector<ll>> dist(H,vector<ll>(W,-1));
    queue<Pi> que;

    dist[0][0] = 0;
    que.push(mp(0,0));
    ll dis=-1;

    while(!que.empty()){
        Pi v =que.front();
        que.pop();
        if(v.first == H-1 && v.second == W-1){
            //flag =1;
            dis = dist[v.first][v.second];
            break;
        }
        rep(dir,4){
            ll ny = v.first + dy[dir];
            ll nx = v.second + dx[dir];

            if(!In_map(ny,nx,H,W)) continue;
            if(dist[ny][nx] != -1) continue;
            if(G[ny][nx] == '#') continue;
            dist[ny][nx] = dist[v.first][v.second]+1;
            que.push(mp(ny,nx));
        }

    }

    if(dis == -1){
        cout<<-1<<endl;
    }else{
        cout<<cnt-dis-1<<endl;
    }

    return 0;

}

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

using namespace std;

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

int main() {
	vector<int> result(1000);
	for (int i = 0; i < 1000; ++i) {

		vector< vector<int> > map(21, vector<int>(21, 0));
		vector< vector<int> > map2(21, vector<int>(21, 0));

		int px = 10, py = 10;

		int N;
		cin >> N;

		if (N == 0) {
			break;
		}

		vector<int> tx(N);
		vector<int> ty(N);

		rep(i, N) {
			cin >> tx[i];
			cin >> ty[i];
			map2[ty[i]][tx[i]] = 1;
		}

		int M;
		cin >> M;

		string str;
		int v;

		rep(i, M) {
			cin >> str >> v;

			if (str == "N") {
				for (int i = py; i <= py + v; i++) {
					map[i][px] = 1;
				}
				py += v;
			}
			else if (str == "E") {
				for (int i = px; i <= px + v; i++) {
					map[py][i] = 1;
				}
				px += v;
			}
			else if (str == "S") {
				for (int i = py; i >= py - v; i--) {
					map[i][px] = 1;
				}
				py -= v;
			}
			else if (str == "W") {
				for (int i = px; i >= px - v; i--) {
					map[py][i] = 1;
				}
				px -= v;
			}
			// rep(i,21){
			//     rep(j,21){
			//         cout << map[i][j] << " ";
			//     }
			//     cout << endl;
			// }
			// cout << endl;
		}

		int sum = 0;

		rep(i, 21) {
			rep(j, 21) {
				if (map[i][j] == 0 && map2[i][j] == 1) {
					sum++;
				}
			}
		}

		if (sum == 0) {
			cout << "Yes" << endl;
		}
		else {
			cout << "No" << endl;
		}
		result[i] = sum;

	}

	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<cstdio>
#include<queue>
#include<deque>
#include<map>
#include<stack>
#include<set>
#include<utility>
using namespace std;
typedef pair<int,int> pii;
typedef pair<int,pii> pipii;
typedef long long ll;
typedef pair<ll,ll> pll;
int dy[]={1,0,-1,0};
int dx[]={0,1,0,-1};
const int MAXN=100000;
const int MAXE=100000;
const int MAXV=10000;
const ll INF=2e9;
const ll MOD=1e9+7;
struct edge{
    int to;
    int cost;
};

int main(){
    int N,M,Q;cin>>N>>M>>Q;
    vector<vector<int> > S(N+1,vector<int>(N+1,0));
    for(int i=0;i<M;++i){
        int L,R;cin>>L>>R;
        S[L][R]++;
    }
    for(int i=0;i<N;++i){
        for(int j=0;j<=N;++j){
            S[i+1][j]+=S[i][j];
        }
    }
    for(int j=0;j<N;++j){
        for(int i=0;i<=N;++i){
            S[i][j+1]+=S[i][j];
        }
    }
    while(Q--){
        int p,q;cin>>p>>q;
        cout<<S[q][q]-S[p-1][q]-S[q][p-1]+S[p-1][p-1]<<endl;
    }
    return 0;
}

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

int N,M,Q,m;
const int maxn=600000+5;
int siz;
int C[maxn],a[maxn],ret[maxn];
struct node{
	int l,r,id;
	bool operator<(const node& other)const{
		return other.r > r;
	}
}x[maxn],y[maxn];

void add(int x,int v){
	for(int i=x;i<=m;i+=i&-i) C[i]+=v;
}

int sum(int x){
	int ret=0;
	for(int i=x;i;i-=i&-i) ret+=C[i];
	return ret;
}

int main(){
	cin>>N>>M>>Q;
	for(int i=0;i<M;i++){
		cin>>x[i].l>>x[i].r;
		a[siz++]=x[i].l;
		a[siz++]=x[i].r;
	}
	for(int i=0;i<Q;i++){
		cin>>y[i].l>>y[i].r;
		y[i].id=i;
		a[siz++]=y[i].l;
		a[siz++]=y[i].r;
	}
	sort(a,a+siz);
	m=unique(a,a+siz)-a;
	for(int i=0;i<M;i++){
		x[i].l=lower_bound(a,a+m,x[i].l)-a+1;
		x[i].r=lower_bound(a,a+m,x[i].r)-a+1;
	}
	for(int i=0;i<Q;i++){
		y[i].l=lower_bound(a,a+m,y[i].l)-a+1;
		y[i].r=lower_bound(a,a+m,y[i].r)-a+1;
	}
	sort(x,x+M);
	sort(y,y+Q);
	int j=0;
	for(int i=0;i<Q;i++){
		while(j<M&&x[j].r<=y[i].r) add(x[j++].l,1);
		ret[y[i].id]=j-sum(y[i].l-1);
	}
	for(int i=0;i<Q;i++) cout<<ret[i]<<endl;
	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <cmath>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <iostream>
#include <random>
#include <map>
#include <iomanip>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include <fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
#include <cctype>
#include <bitset>
using namespace std;

using ll = long long;
using vll = vector<long long>;
using pll = pair<long long, long long>;
#define rep(i,n) for(long long i(0);(i)<(n);(i)++)

void solve(long long N, std::string Str){
    //* left half record
    map< pair<string, string>, ll > rec;
    rep(S, 1<<N){
        string red, blue;
        rep(i, N){
            if(S >> i & 1){
                red.push_back(Str.at(i));
            }
            else{
                blue.push_back(Str.at(i));
            }
        }
        rec[make_pair(red, blue)]++;
    }
    ll cnt = 0;
    rep(S, 1<<N){
        string red, blue;
        rep(i, N){
            if(S >> i &1){
                red.push_back(Str.at(i+N));
            }
            else{
                blue.push_back(Str.at(i+N));
            }
        }
        reverse(red.begin(), red.end());
        reverse(blue.begin(), blue.end());

        if(rec.count(make_pair(blue, red)))
            cnt += rec[make_pair(blue, red)];

    }

    cout << cnt << endl;
}

int main(){
    long long N;
    scanf("%lld",&N);
    std::string S;
    std::cin >> S;
    solve(N, S);
    return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#define vv(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
typedef unsigned long long ull;
#define vvi std::vector<std::vector<int> >
#define vvu std::vector<std::vector<ull> >
#define MODs 1000000007;
typedef long long int ll;
using namespace std;
int main(int argc, char const *argv[]) {
  int N;
  ull B = 10000007;
  ll ans = 0;
  string s, S, t, T="";
  std::cin >> N;
  std::cin >> s;
  S = s.substr(0, N);
  t = s.substr(N);
  for(int i=0;i<N;i++) T+=t[N-1-i];
  std::vector<int> num(26, 0);
  for(int i=0;i<N;i++) num[S[i]-'a']++, num[T[i]-'a']--;
  bool flag = true;
  for(int i=0;i<26;i++) if(num[i]!=0) flag = false;
  vvu hash = vv(0, 2, 0, ull);
  for(int i=0;i<(1<<N);i++){
    int n = i+(1<<20), count=0;
    ull ah = 0, bh = 0;
    while(count<N){
      if(n%2==1) ah = ah * B + T[count];
      if(n%2==0) bh = bh * B + T[count];
      count++;
      n/=2;
    }
    hash.push_back(std::vector<ull>{ah, bh});
  }
  sort(hash.begin(), hash.end());
  for(int i=0;i<(1<<N);i++){
    int n = i + (1<<20), count=0;
    ull ah = 0, bh = 0;
    while(count<N){
      if(n%2==1) ah = ah * B + S[count];
      if(n%2==0) bh = bh * B + S[count];
      count++;
      n/=2;
    }
    auto itr = lower_bound(hash.begin(), hash.end(), std::vector<ull> {ah, bh});
    auto itr2 = lower_bound(hash.begin(), hash.end(), std::vector<ull> {ah, bh+1});
    ans += itr2 - itr;
  }
  std::cout << (!flag?0:ans) << '\n';
  return 0;
}


    /**   Created by: Marcos Portales
          Codeforces User: marcosportales
          Country: Cuba
                         **/
#include<bits/stdc++.h>
using namespace std;
/// Macros:
#define int long long
#define f first
#define s second
#define db(x) cerr << #x << ": " << (x) << '\n';
#define pb push_back
#define lb lower_bound
#define up upper_bound
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
#define enl '\n'
#define vi vector<int>
#define sz size
#define rep(i,n) for(int i=(0);i<(n);i++)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
typedef pair<int,int> ii;
typedef long double ld;
typedef unsigned long long ull;
/// Constraints:
const int maxn = 1e5+1;
const int mod = 1000000009;
const ld eps = 1e-9;
const int inf = ((1ll<<31ll)-1ll);
const int INF = 10000000000000000ll;
const ld pi = acos(-1);
/// Prime Numbers:
// 2, 173, 179, 181, 197, 311, 331, 737, 1009, 2011, 2027, 3079, 4001, 10037, 10079, 20011, 20089;
// 100003, 100019, 100043, 200003, 200017, 1000003, 1000033, 1000037, 1000081;
// 2000003, 2000029, 2000039, 1000000007, 1000000021, 2000000099;
/// Functions:
#define lg2(x) 31 - __builtin_clz(x)
#define lgx(x,b) ( log(x) / log(b) )
/// Red-Black Tree Template ---------------------------------
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree < long long ,  null_type ,  less<long long> ,  rb_tree_tag ,  tree_order_statistics_node_update > ordered_set;
/// Quick Pow------------------------------------------------
int qpow(int b,int e){
    if(!e)return 1;
    if(e&1) return qpow(b,e-1)*b;
    int pwur=qpow(b,e>>1);
    return pwur*pwur;
}

/// My Code -------------------------------------------------

int32_t main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    cout.setf(ios::fixed); cout.precision(0);
    srand(time(NULL));
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);
    int n,m;
    cin>>n;
    vector<string>s(n);
    map<string,int>mp;
    rep(i,n){cin>>s[i];mp[s[i]]++;}
    cin>>m;
    vector<string>t(m);
    rep(i,m){cin>>t[i];mp[t[i]]--;}
    int ans=0;
    for(auto it:mp)ans=max(ans,it.s);
    cout<<ans<<enl;
    return 0;
}

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

int main(){
    int N, M;
    cin >> N >> M;
    vector<int> A[300];
    for(int i=0; i<N; i++){
        for(int j=0; j<M; j++){
            int a;
            cin >> a;
            A[i].push_back(a-1);
        }
        reverse(A[i].begin(), A[i].end());
    }
    int ans = 1e9;
    bitset<300> removed;
    int rem = 0;
    while(rem < M){
        vector<int> num(M);
        for(int i=0; i<N; i++){
            while(removed[A[i].back()]) A[i].pop_back();
            num[A[i].back()]++;
        }
        int mx = max_element(num.begin(), num.end()) - num.begin();
        ans = min(ans, num[mx]);
        removed[mx] = 1;
        rem++;
    }
    cout << ans << endl;
}


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

class union_find  // union find.
{
    int* U;
public:
    union_find(int n){ U = new int [n + 1]; for(int i = 1; i <= n; i++) U[i] = i; }
    int root(int a){ if(U[a] == a){ return a; } U[a] = root(U[a]); return U[a]; }
    bool same(int a, int b){ return root(a) == root(b); }
    void join(int a, int b){ int x = root(a), y = root(b); U[x] = y; }
    ~union_find(){ delete [] U; }
};

struct edge{ int v1, v2; int weight; };  // edge.

edge init(){
    edge e;
    e.v1 = 0; e.v2 = 0; e.weight = -1;
    return e;
}

edge init(int a, int b, int w){
    edge e;
    e.v1 = a; e.v2 = b; e.weight = w;
    return e;
}

void swap_e(edge e1, edge e2)
{
    swap(e1.v1, e2.v1); swap(e1.v2, e2.v2); swap(e1.weight, e2.weight);
}

class edge_heap  // heap.
{
    edge* E;
    int edge_n;
public:
    edge_heap(int n){ E = new edge [n + 1]; edge_n = 0; }
    void min_heap(int i);
    void insert_heap(edge e);   // sorting.
    void insert_nl(edge e);     // no sorting.
    edge delete_min();
    bool isEmpty(){ return edge_n == 0; }
    int total_weight();
    ~edge_heap(){ delete [] E; }
};

void edge_heap::min_heap(int i)
{
    int l = 2 * i, r = 2 * i + 1;
    if(l > edge_n && r > edge_n) return;
    int minid = i;
    if(l <= edge_n){ if(E[l].weight < E[minid].weight) minid = l; }
    if(r <= edge_n){ if(E[r].weight < E[minid].weight) minid = r; }
    if(minid == i) return;
    swap(E[minid], E[i]);
    min_heap(minid);
}

void edge_heap::insert_heap(edge e)
{
    edge_n++;
    int w = e.weight;
    int i = edge_n;
    while(i > 1){
        if(w < E[i / 2].weight){
        E[i] = E[i / 2];
        }else{ break; }
        i /= 2;
    };
    E[i] = e;
}
void edge_heap::insert_nl(edge e){ edge_n++; E[edge_n] = e; }

edge edge_heap::delete_min()
{
    edge e = E[1];
    E[1] = E[edge_n]; edge_n--;
    min_heap(1);
    return e;
}

int edge_heap::total_weight()
{
    int tw = 0;
    for(int i = 1; i <= edge_n; i++) tw += E[i].weight;
    return tw;
}

int main()
{
    int n, i, j, w;
    scanf("%d", &n);
    edge_heap EH(n * (n + 1) / 2); // All Edge heap.
    edge e;

    for(i = 1; i <= n; i++){
        for(j = 1; j <= n; j++){
            scanf("%d", &w);
            if(j <= i || w < 0) continue;
            e = init(i, j, w);
            EH.insert_heap(e);
        }
    }

    edge_heap TH(n);   // Tree heap.

    union_find VG(n);  // vertex group.

    while(!EH.isEmpty()){
        e = EH.delete_min();
        if(!VG.same(e.v1, e.v2)){
            VG.join(e.v1, e.v2);
            TH.insert_nl(e);
        };
    };

    printf("%d\n", TH.total_weight());

    return 0;
}

#include <iostream>

using namespace std;

int main() {
    int n, matrix[100][100];

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

    bool visit[100] = {
        true
    };
    int sum = 0;

    while (1) {
        int min = 2001, mini = -1;
        for (int i = 0; i < n; i++) {
            if (visit[i] == true) {
                for (int j = 0; j < n; j++) {
                    if (matrix[i][j] > -1 && matrix[i][j] < min && visit[j] == false) {
                        min = matrix[i][j];
                        mini = j;
                    }
                }
            }
        }
        if (mini == -1) {
            break;
        }
        else {
            sum += min;
            visit[mini] = true;
        }
    }

    cout << sum << endl;

    return 0;
}


#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

using vi = vector<int>;

constexpr char newl = '\n';
constexpr double eps = 1e-10;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--)
#define RF0(i,b) RFO(i,0,b)
#define fi first
#define se second
#define show(x) cout << #x << " = " << x << '\n';
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define YesNo {cout<<"Yes";}else{cout<<"No";}
#define YESNO {cout<<"YES";}else{cout<<"NO";}
#define v(T) vector<T>

template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
template<class T> bool lcmp(const pair<T, T>& l, const pair<T, T>& r) {
  return l.first < r.first;
}
template<class T> istream& operator>>(istream& i, v(T)& v) {
  F0R(j, sz(v)) i >> v[j];
  return i;
}
template<class A, class B> istream& operator>>(istream& i, pair<A, B>& p) {
  return i >> p.first >> p.second;
}
template<class A, class B, class C> istream& operator>>(istream& i, tuple<A, B, C>& t) {
  return i >> get<0>(t) >> get<1>(t) >> get<2>(t);
}
template<class T> ostream& operator<<(ostream& o, const vector<T>& v) {
  F0R(i, v.size()) {
    o << v[i] << ' ';
  }
  o << newl;
  return o;
}
template<class T1, class T2> ostream& operator<<(ostream& o, const map<T1, T2>& m) {
  for (auto& p : m) {
    o << p.first << ": " << p.second << newl;
  }
  o << newl;
  return o;
}

#if 1

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int n;
  cin >> n;

  unordered_map<int, int> a, b;

  F0R(i, n) {
    int v;
    cin >> v;
    if (i & 1) {
      ++a[v];
    }
    else {
      ++b[v];
    }
  }
  vector<pair<int,int>> c, d;
  for (auto& p : a) c.emplace_back(p.second, p.first);
  for (auto& p : b) d.emplace_back(p.second, p.first);
  sort(rrng(c), lcmp<int>);
  sort(rrng(d), lcmp<int>);
  c.emplace_back(0, -1);
  d.emplace_back(0, -1);

  int r;
  if (c[0].second != d[0].second)
    r = n - c[0].first - d[0].first;
  else {
    r = n - max(c[0].first + d[1].first, c[1].first + d[0].first);
  }
  cout << r;
}
#endif

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;

static const double EPS = 1e-8;
static const double PI = 4.0 * atan(1.0);
static const ll INF = 1023456789;

#define REP(i,n)	for(int i=0;i<n;++i)
#define REPR(i,n)	for(int i=n-1;i>=0;--i)
#define FOR(i,s,n)	for(int i=s;i<n;++i)
#define FORR(i,s,n)	for(int i=n-1;i>=s;--i)
#define ALL(c)		(c).begin(),(c).end()
#define CLEAR(v)	memset(v,0,sizeof(v))
#define MP(a,b)		make_pair((a),(b))
#define ABS(a)		((a)>0?(a):-(a))
#define F			first
#define S			second

ll n, v[100000];
P a[100001], b[100001];

int main(int argc, char **argv) {
	cin >> n;
	REP(i, n) cin >> v[i];
	REP(i, 100001) a[i] = b[i] = P{0, i};
	REP(i, n/2) ++a[v[i*2]].F;
	REP(i, n/2) ++b[v[i*2+1]].F;
	sort(a, a+100001); reverse(a, a+100001);
	sort(b, b+100001); reverse(b, b+100001);
	if (a[0].S == b[0].S) cout << min(n - a[0].F - b[1].F, n - b[0].F - a[1].F) << endl;
	else cout << (n - a[0].F - b[0].F) << endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define sidha(i,a,b) for(int i=a;i<b;i++)
#define vi vector<int>
#define pb push_back

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
	int n,m,p;cin>>n>>m>>p;
	cout<<min({n+m,m+p,n+p})<<endl;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int MAX_N=1e6;
int seg_tree[2*MAX_N+5],n;
void build()
{
for(int i=n-1; i>0; --i)
seg_tree[i]=__gcd(seg_tree[i<<1],seg_tree[i<<1|1]);
}
void modify(int pos,int val)
{
for(seg_tree[pos+=n]=val; pos>1; pos>>=1)
seg_tree[pos>>1]=__gcd(seg_tree[pos],seg_tree[pos^1]);
}
int query(int l,int r)
{
int ans(1);
for(l+=n,r+=n; l<r; l>>=1,r>>=1)
{
if(l&1)
ans=__gcd(ans,seg_tree[l++]);
if(r&1)
ans=__gcd(ans,seg_tree[--r]);
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int a,b,c;
cin>>a>>b>>c;
cout<<min({a,b,c})+(a+b+c-min({a,b,c})-max({a,b,c}));
}


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

using namespace std;

typedef long long ll;

int main()
{
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  int s = accumulate(a.begin(), a.end(), 0);
  int best = 1 << 30;
  int bestI = -1;
  for (int i = 0; i < n; ++i) {
    int x = a[i];
    int cur = abs(x * n - s);
    if (cur < best) {
      best = cur;
      bestI = i;
    }
  }
  cout << bestI << endl;
  return 0;
}

#include <algorithm>
#include <bitset>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <cctype>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define ArraySizeOf(array)     (sizeof(array) / sizeof(array[0]))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define REV(i,n) for(int i=n-1;i>0;i--)
#define req(i,n,m) for(int i=n;i<m;i++)
#define REQ(i,n,m,l) for(int i=n;i<m;i+=l)
#define _GLIBCXX_DEBUG
int INF = 1e9 + 7;
unsigned NthDayOfWeekToDay(unsigned n, unsigned dow, unsigned dow1)
{
    unsigned day;
    if (dow < dow1) dow += 7;
    day = dow - dow1;
    day += 7 * n - 6;
    return day;
}
signed gcd(int x, int y) {
    if (y == 0)return x;
    return gcd(y, x % y);
}
signed lcm(int x, int y) {
    return x / gcd(x, y) * y;
}
unsigned DayToWeekNumber(unsigned day)
{
    return (day - 1) / 7 + 1;
}
unsigned AnotherDayOfWeek(unsigned day, unsigned day0, unsigned dow0)
{
    return (dow0 + 35 + day - day0) % 7;
}
using namespace std;
signed main() {
    int N; cin >> N;
    vector<double>a(N);
    long double count = 0;
    rep(i, N) {
        cin >> a[i];
        count += a[i];
    }
    vector<long double>b(N);
    rep(i, N)b[i] = abs(a[i] - count/N);
    sort(b.begin(), b.end());
    double ans1 = abs(b[0] - count / N);
    double ans2 = b[0] + count / N;
    rep(i, N) {
        if (a[i]==ans1 || a[i]==ans2) {
            cout << i << endl;
            return 0;
        }
    }
}


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

#define DEBUG 0

#if DEBUG
  #define DLOG(...) fprintf(stderr, __VA_ARGS__)
#else
  #define DLOG(...)
#endif

void debug(auto f) {
#if DEBUG
  f();
#endif
}

ll mPrime = 1000*1000*1000 + 7;
ll n, x;
vector<ll> s;
vector<ll> facts;
vector<ll> invFacts;
vector<int> tbl;

ll mAdd(ll x, ll y) {
  ll z = x + y;
  if (z < mPrime)  return z;
  else             return (z - mPrime);
}

ll mSub(ll x, ll y) {
  ll z = x - y;
  if (z >= 0)  return z;
  else         return (z + mPrime);
}

ll mMul(ll x, ll y) {
  return (x * y) % mPrime;
}

ll eGCD(ll a, ll b, ll& s, ll& t) {
  if (a == 0) {
    s = 0;
    t = 1;
    return b;
  }
  ll u;
  ll g = eGCD(b % a, a, t, u);
  s = u - (b / a) * t;
  return g;
}

ll mInv(ll x) {
  ll s, t;
  eGCD(x, mPrime, s, t);
  return s < 0 ? s + mPrime : s;
}

int main() {
  cin >> n >> x;
  s.resize(n);
  for (ll i = 0; i < n; i++) {
    cin >> s.at(i);
  }
  facts.resize(n+1);
  invFacts.resize(n+1);
  facts.at(0) = invFacts.at(0) = 1;
  for (ll i = 1; i <= n; i++) {
    facts.at(i) = mMul(i, facts.at(i-1));
    invFacts.at(i) = mInv(facts.at(i));
  }
  sort(s.begin(), s.end());

  tbl.resize(x+1, 0);
  tbl.at(x) = 1;

  ll lim = x;
  for (ll k = n; k >= 1; k--) {
    ll newLim = 0;
    for (ll r = 1; r <= lim; r++) {
      ll weight = tbl.at(r);
      if (weight == 0)  continue;
      newLim = r;
      ll rem = k;
      for (ll t : s) {
	if (r < t)  break;
	rem--;
	ll newR = r % t;
	tbl.at(newR) = mAdd(tbl.at(newR), weight);
      }
      tbl.at(r) = mMul(tbl.at(r), rem);
    }
    lim = newLim;
  }
  ll ans = 0;
  for (ll r = 1; r <= lim; r++) {
    ans = mAdd(ans, mMul(tbl.at(r), r));
  }
  cout << ans << endl;

  return 0;
}

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

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

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

/// check for test case before submitting
void vfun()
{
    ll n,k;
    cin>>n>>k;
    vi v(n);
    fo(i,n)
    cin>>v[i];
    sort(v.begin(),v.end(),comp<ll>);
    ll sum=0;
    fo(i,k)
    sum+=v[i];
    cout<<sum<<endl;

}

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

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


#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <stdio.h>
using namespace std;
int main(){
	string str;
	while(cin>>str){
		int num=0;
		vector<int> roman;
		for(int i=0;i<str.length();i++){
			if(str[i]=='I'){
				roman.push_back(1);
			}else if(str[i]=='V'){
				roman.push_back(5);
			}else if(str[i]=='X'){
				roman.push_back(10);
			}else if(str[i]=='L'){
				roman.push_back(50);
			}else if(str[i]=='C'){
				roman.push_back(100);
			}else if(str[i]=='D'){
				roman.push_back(500);
			}else if(str[i]=='M'){
				roman.push_back(1000);
			}
		}
		for(int i=0;i<roman.size();i++){
			if(i==roman.size()-1){
				num += roman[roman.size()-1];
			}else{
				if(roman[i]<roman[i+1]){
					num += roman[i+1]-roman[i];
					i++;
				}else{
					num += roman[i];
				}
			}
		}
		cout<<num<<endl;
	}
	return 0;
}

//Author: Fuadul Hasan(fuadul202@gmail.com)
#include<bits/stdc++.h>
using namespace std;

#define ll long long
const int N   = 2e5 + 5;

int main(){

	int n,k;
	cin>>n>>k;
	int sum = 0;
	if(k < 0){
		int last = (n + k - 1);
		if(last>=0){
			for(int i=k;i<=last;i++)
				sum += i;
		}else{
			for(int i=k;i<last;i++){
				sum += i;
			}
		}
	}else{
		for(int i=k+1;i<n+k;i++){
			sum+=i;
		}
	}

	cout<<sum<<endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

class StronglyConnectedComponentDecomposition
{
private:
	int V; // ????????°
	vector<vector<int>> G; // ??°???????????£??\???????????¨???
	vector<vector<int>> rG; // ?????????????????????????????°??????
	vector<int> vs; // ??°?????????????????????
	vector<bool> used; // ?????§???????????????

public:
	vector<int> cmp; // ?±????????????£????????????????????????????????????
	StronglyConnectedComponentDecomposition( int N = 0 ) {
		V = N;
		G = vector<vector<int>>( V );
		rG = vector<vector<int>>( V );
		used = vector<bool>( V );
		cmp = vector<int>( V );
	}
	void add_edge( int from, int to ) {
		G[from].push_back( to );
		rG[to].push_back( from );
	}
	void dfs( int v ) {
		used[v] = true;
		for( int i = 0; i < G[v].size(); i++ ) {
			if( !used[G[v][i]] ) dfs( G[v][i] );
		}
		vs.push_back( v );
	}
	void rdfs( int v, int k ) {
		used[v] = true;
		cmp[v] = k;
		for( int i = 0; i < rG[v].size(); i++ ) {
			if( !used[rG[v][i]] ) rdfs( rG[v][i], k );
		}
	}
	int scc() {
		used = vector<bool>( V );
		vs.clear();
		for( int v = 0; v < V; v++ ) {
			if( !used[v] ) dfs( v );
		}
		used = vector<bool>( V );
		int k = 0;
		for( int i = vs.size() - 1; i >= 0; i-- ) {
			if( !used[vs[i]] ) rdfs( vs[i], k++ );
		}
		return k;
	}
};

int main() {
	long long int V, E;
	cin >> V >> E;
	StronglyConnectedComponentDecomposition SCCD( V );
	for( size_t i = 0; i < E; i++ ) {
		int s, t;
		cin >> s >> t;
		SCCD.add_edge( s, t );
	}
	SCCD.scc();
	long long int N;
	cin >> N;
	for( size_t i = 0; i < N; i++ ) {
		int u, v;
		cin >> u >> v;
		cout << (SCCD.cmp[u] == SCCD.cmp[v]) << endl;
	}
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main() {

    int n, m, v;
    int dx[] = {0, 1, 0, -1};
    int dy[] = {1, 0, -1, 0};
    char d;
    char ds[] = "NESW";

    while(cin >> n, n) {

        int rx = 10, ry = 10, cnt = 0;
        int graph[21][21];

        memset(graph, 0, sizeof(graph));

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

        cin >> m;

        for(int i = 0; i < m; ++i) {

            cin >> d >> v;

            for(int j = 0; j < 4; ++j) {
                if(d == ds[j]) {
                    for(int k = 1; k <= v; ++k) {
                        rx += dx[j];
                        ry += dy[j];
                        if(graph[ry][rx] == 1) {
                            graph[ry][rx] = 0;
                            ++cnt;
                        }
                    }
                    break;
                }
            }

        }

        cout << (cnt == n ? "Yes" : "No") << endl;

    }

}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <utility>
#include <tuple>
#include <math.h>
#include <set>
#include <map>
using namespace std ;
using ll = long long ;
using ld = long double ;
using vll = vector<ll> ;
using vvll = vector<vll> ;
using vc = vector<char> ;
using vvc = vector<vc> ;
using vb = vector<bool> ;
using vvb = vector<vb> ;
using pll = pair<ll,ll> ;
#define all(v) v.begin(),v.end()
const ll INF = 1e18 ;
const ll mod = 1e9+7 ;
const double pie = acos(-1);
vll dx4 = {-1,0,1,0} ;
vll dy4 = {0,-1,0,1} ;
vll dx8 = {-1,0,1,1,1,0,-1,-1} ;
vll dy8 = {-1,-1,-1,0,1,1,1,0} ;

void fix_cout(){cout << fixed << setprecision(20) ; }
ll gcd(ll a,ll b){if(b==0) return a ; return gcd(b,a%b) ; }
ll lcm(ll a,ll b){return a/gcd(a,b)*b ; }
void chmax(ll &a,ll b){if(a<b) a = b ; }
void chmin(ll &a,ll b){if(a>b) a = b ; }

int main(){
	ll h,w ;
	cin >> h >> w ;
	map<ll,ll> gs,dis ;
	for(int i=1;i<=w;i++) gs[i] = i ;
	gs[w+10] = -1 ;
	dis[0] = w ;
	for(int i=1;i<=h;i++){
		ll a,b ;
		cin >> a >> b ;
		ll mx = -1 ;
		for(auto it=gs.lower_bound(a);it->first<=b+1;it=gs.erase(it)){
			ll g = it->first ;
			ll s = it->second ;
			chmax(mx,s) ;
			if(dis.count(g-s)){
				dis[g-s]-- ;
				if(dis[g-s]==0) dis.erase(g-s) ;
			}
		}
		if(mx>=0&&b<w){
			gs[b+1] = mx ;
			dis[b+1-mx]++ ;
		}
		ll ans = -1 ;
		if(!dis.empty()){
			ans = i+dis.begin()->first ;
		}
		cout << ans << endl ;
	}
}

#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<cstdio>

using namespace std;

#define ll long long
#define rep(i, n) for(int i = 0; i < n; i++)
#define P pair<int, int>

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

int main(){
    int n, K;
    cin >> n >> K;
    int v[n];
    rep(i, n) cin >> v[i];
    int mx = min(n, K);
    int ans = 0;
    for (int i = 0; i <= mx; i++){
        for (int j = 0; j <= i; j++){
            vec v_s;
            for (int k = 0; k < j; k++){
                v_s.push_back(v[k]);
            }
            for (int k = n - 1; k > (n - 1) - (i - j); k--){
                v_s.push_back(v[k]);
            }
            sort(v_s.begin(), v_s.end());
            // for (int k = 0; k < v_s.size(); k++){
            //     cout << v_s[k] << " ";
            // }cout << endl;

            int sm = 0;
            rep(k, v_s.size()){
                if(k < K - i && v_s[k] < 0) continue;
                sm += v_s[k];
            }
            // cout << sm << endl;
            ans = max(sm, ans);
        }
    }
    cout << ans << endl;
}


//BISMILLAHIR RAHMANIR RAHIM
//SOTO MEAW
//SOTO SONDESH
#include<bits/stdc++.h>

#define ls ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define __ <<" "<<
#define loop(m,n) for(m=0;m<n;m++)
#define rloop(m,n) for(m=n-1;m>=0;m--)
#define case(z) "Case " << z++ << ": "
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define quit return 0

using namespace std;

bool isprime(int n)
{
    for (int i = 2; i <= n/2; i++)
    {
        if (n % i == 0)
            return false;
    }
    return true;
}

void showmyDS(set<int> ds)
{
    set<int> ::iterator it;
    for (it=ds.begin();it!=ds.end();it++)
    {
        cout << *it << endl ;
    }
    cout << endl;
}
int main()
{
    ls
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //int t,l=1;cin>>t;while(t>0){
        vector<char>s;
        int j;
        string s1;
        cin>>s1;
        loop(j,s1.size()) s.push_back(s1[j]);
        int n,i,c=0;
        cin>>n;
        char a[n];
        int p,index[255];
        memset(index,0,sizeof(index));
        loop(i,n)
        {
            cin>>a[i];
            p=(int)a[i];
            index[p]=1;
        }
        loop(i,s.size())
        {
            p=(int)s[i];
            if(c==1)
                p=48;
again:
            if(index[p]==1 && p==57)
            {
                if(i!=0)
                    p=48;
                else
                    p=49;
                s.push_back('0');
                c=1;
            }
            if(index[p]==1)
            {
                p++;
                c=1;
                goto again;
            }
            else
            {
                cout<<(char)p;
            }
        }
        cout<<endl;
    //t--;}

    quit;
}

/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <unordered_map>
#include <queue>
#define   mod  998244353
#define   int  long long
#define   ld   long double
#define   pb   push_back
#define   vi  vector<int>
#define   dbg(x)  cerr << #x << " = " << x << '\n'
#define   sz(x)  (int)x.size()
#define   all(a) (a.begin(),a.end())
#define   ff   first
#define   ss   second
#define   pii  pair<int,int>
#define   lcm(a,b) (a*b)/__gcd(a,b)
using namespace std;

int n,k;
map <int,bool> mp;

bool fun(int a){
	while (a){
		if (mp[a%10]) return false;
		a /= 10;
	}
	return true;
}

inline void solve(){
    cin >> n >> k ;
    for (int i=1;i<=k;i++) {
    	int a; cin >> a;
    	mp[a] = true;
    }
    while (true){
    	if (fun(n)){
    		cout << n << endl;
    		return;
    	} n++ ;
    }
}

signed main()
{
    int n=1; // cin>>n;
    while (n--) solve();
    return 0;
}


#include <bits/stdc++.h>

#define For(i, j, k) for (int i = j; i <= k; ++ i)
#define Forr(i, j, k) for (int i = j; i >= k; -- i)

using namespace std;

inline int read() {
	int x = 0, p = 1; char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') p = -1;
	for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
	return x * p;
}

inline void File() {
	freopen("D.in", "r", stdin);
	freopen("D.out", "w", stdout);
}

const int N = 1e2 + 10;
int a[N], sta[N], tp, n, m, lst, sum, cnt, ans[N];

int main() {

	n = read(), m = read();
	For(i, 1, m) {
		a[i] = read();
		if (a[i] & 1) sta[++ tp] = i;
	}

	if (tp > 2) return puts("Impossible"), 0;

	if (tp > 0) swap(a[1], a[sta[1]]);
	if (tp > 1) swap(a[m], a[sta[2]]);

	if (m == 1) {
		if (a[1] == 1) return printf("1\n1\n1\n"), 0;
		return printf("%d\n2\n%d %d\n", a[1], a[1] - 1, 1), 0;
	}
	For(i, 1, m) printf("%d ", a[i]); puts("");

	ans[cnt = 1] = lst = a[1] - 1; sum = 1;
	For(i, 1, m) sum += a[i], ans[++ cnt] = sum - lst, lst = sum;
	ans[cnt] -= 1;

	int tt = 0; For(i, 1, cnt) if (ans[i] == 0) ++ tt;

	printf("%d\n", cnt - tt);
	For(i, 1, cnt) if (ans[i]) printf("%d ", ans[i]); puts("");

	return 0;
}


#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "math.h"
#include "utility"
#include "string"
#include "map"
#include "unordered_map"
#include "iomanip"
#include "random"

using namespace std;
const long long int MOD = 1000000007;

long long int power(long long int x, long long int n, long long int M) {
	long long int tmp = 1;

	if (n > 0) {
		tmp = power(x, n / 2, M);
		if (n % 2 == 0) tmp = (tmp*tmp) % M;
		else tmp = (((tmp*tmp) % M)*x) % M;
	}
	return tmp;
}

long long int N, M, K, Q, W, H, L, R;
long long int ans;

int main() {
	ios::sync_with_stdio(false);
	cin >> N;
	while (N) {
		set<pair<long double, string> >s;
		for (int i = 0; i < N; i++) {
			string t;
			long double  p, a, b, c, d, e, f, n, m;
			cin >> t >> p >> a >> b >> c >> d >> e >> f >> n >> m;
			s.insert({ 1000-((f*n*m - p) / (a + b + c + d*m + e*m)) ,t });
		}
		for (auto i : s) {
			cout << i.second << endl;
		}
		cout << "#\n";
		cin >> N;
	}
	return 0;
}

#include <cassert>

#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
//#include <boost/foreach.hpp>
//#include <boost/range/algorithm.hpp>
#define rep(i,j,k) for(int i=(int)j;i<(int)k;i++)
#define ll long long
#define Sort(v) sort(all(v))
#define INF 1e9
#define LINF 1e18
#define END return 0
#define pb push_back
#define se second
#define fi first
#define pb push_back
#define all(v) (v).begin() , (v).end()
#define MP make_pair
#define MOD 1000000007LL
#define int long long
using namespace std;
int day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
struct edge{int to,cost;};
typedef pair<int,int> P;

bool isupper(char c){if('A'<=c&&c<='Z')return 1;return 0;}
bool islower(char c){if('a'<=c&&c<='z')return 1;return 0;}
bool isPrime(int x){if(x==1)return 0;if(x==2)return 1;if(x%2==0)return 0;for(int i=3;i*i<=x;i++)if(x%i==0)return 0;return 1;}
bool iskaibun(string s){for(int i=0;i<s.size()/2;i++)if(s[i]!=s[s.size()-i-1])return 0;return 1;}
bool isnumber(char c){return ('0'<=c&&c<='9');}
bool isalpha(char c){return (isupper(c)&&islower(c));}
void printvi(vector<int> v){rep(i,0,v.size()){if(i)cout<<" ";cout<<v[i];}cout<<endl;}
void printvil(vector<int> v){rep(i,0,v.size()){cout<<v[i]<<endl;}}
void printvvi(vector<vector<int>> v){
    rep(i,0,v.size()){
        rep(j,0,v[i].size())cout<<v[i][j];
        cout<<endl;
    }
}
void printvstr(vector<string> v){
    rep(i,0,v.size()){
        cout<<v[i]<<endl;
    }
}

struct S{
    string name;
    double value;
};

signed main (){
    int n;
    while(1){
        cin>>n;
        if(n==0)break;
        vector<S> v(n);
        rep(i,0,n){
            cin>>v[i].name;
            v[i].value=0;
            double p,a,b,c,d,e,f,s,m;
            cin>>p>>a>>b>>c>>d>>e>>f>>s>>m;
            double time=(a+b+c+d+e)+(m-1)*(d+e);
            double profit=f*s*m-p;
            v[i].value=profit/time;
        }

        auto cmp=[&](S a,S b){
            return (a.value==b.value?a.name<b.name:a.value>b.value);
        };
        sort(all(v),cmp);
        rep(i,0,n){
            cout<<v[i].name<<endl;
        }
        cout<<"#"<<endl;
    }

}
/*
Aで目が出る
Bでわかば
Cで葉
Dで花
Eで実
F個の実
１つあたりSで売れる
M回実をつける
収入効率＝収入/時間

多作期の時、(M-1)(D+E)の時間 + A+B+C+D+Eの時間
収入はF*M*S

降順ソート
*/


#include <cstdio>
#include <cstring>
#include <algorithm>
const int MOD = 998244353;
const int MAXN = 5e5 + 10;

int n, m;
namespace {
  inline int add(int x) { return x >= MOD ? x - MOD : x; }
  inline int sub(int x) { return x < 0 ? x + MOD : x; }
  inline int mul(int x, int y) { return 1ll * x * y % MOD; }
  inline int pwr(int x, int y) {
    int ans = 1;
    for (; y; y >>= 1, x = mul(x, x)) {
      if (y & 1) ans = mul(ans, x);
    }
    return ans;
  }
}
int fact[MAXN << 1], invf[MAXN << 1];
int choose(int x, int y) {
  return x < y ? 0 : mul(fact[x], mul(invf[y], invf[x - y]));
}
int main() {
  scanf("%d%d", &n, &m);
  fact[0] = 1, invf[0] = 1;
  if (n < m) std::swap(n, m);
  for (int i = 1; i <= 2 * n; i++) {
    fact[i] = mul(fact[i - 1], i);
    invf[i] = pwr(fact[i], MOD - 2);
  }

  int ans = 0;
  for (int i = 1; i <= m; i++) {
    ans = add(ans + mul(choose(2 * i - 1, i), choose(n + m - 2 * i, n - i)));
  }
  printf("%d\n", add(n + mul(ans, pwr(choose(n + m, n), MOD - 2))));
}

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

int main(){
  int K;
  string S;
  cin >> K >> S;
  if(S.size()<=K){
    cout << S << endl;
    return 0;
  }
  string T=S.substr(0,K);
  T+="...";
  cout << T << endl;
}


#include <bits/stdc++.h>
#define clr(x) memset(x,0,sizeof (x))
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define Fod(i,b,a) for (int i=(b);i>=(a);i--)
#define fi first
#define se second
#define kill _z_kill
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define outval(x) cerr<<#x" = "<<x<<endl
#define outv(x) cerr<<#x" = "<<x<<"  "
#define outtag(x) cerr<<"--------------"#x"---------------"<<endl
#define outarr(a,L,R) cerr<<#a"["<<L<<".."<<R<<"] = ";\
	For(_x,L,R) cerr<<a[_x]<<" ";cerr<<endl;
#define User_Time ((double)clock()/CLOCKS_PER_SEC)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned uint;
typedef long double LD;
typedef vector <int> vi;
typedef pair <int,int> pii;
LL read(){
	LL x=0,f=0;
	char ch=getchar();
	while (!isdigit(ch))
		f=ch=='-',ch=getchar();
	while (isdigit(ch))
		x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return f?-x:x;
}
const int mod=998244353;
int Pow(int x,int y){
	int ans=1;
	for (;y;y>>=1,x=(LL)x*x%mod)
		if (y&1)
			ans=(LL)ans*x%mod;
	return ans;
}
void Add(int &x,int y){
	if ((x+=y)>=mod)
		x-=mod;
}
void Del(int &x,int y){
	if ((x-=y)<0)
		x+=mod;
}
int Add(int x){
	return x>=mod?x-mod:x;
}
int Del(int x){
	return x<0?x+mod:x;
}
void ckmax(int &x,int y){
	if (x<y)
		x=y;
}
void ckmin(int &x,int y){
	if (x>y)
		x=y;
}
const int N=305;
int n,k;
char s[N];
int dp[N][N][N];
int a[N],b[N];
int c0=0,c1=0;
int main(){
	cin>>(s+1);
	n=strlen(s+1);
	k=read();
//	n=300;
//	For(i,1,n)
//		s[i]=rand()%2?'1':'0';
//	k=300;
	For(i,1,n)
		if (s[i]=='1')
			c0++;
		else
			a[++c1]=c0,b[c1]=a[c1]-a[c1-1];
	k=min(k,c0);
	dp[0][0][0]=1;
//	outval(c0),outval(c1);
//	outarr(a,0,c1);
	For(i,1,c1)
		For(j,0,c0)
			For(t,0,k){
//				outval(i);
				int v=dp[i-1][j][t];
				if (!v||j<a[i-1])
					continue;
//				printf("dp[%d][%d][%d] = %d\n",i-1,j,t,v);
				int ub=min(c0-j,k-t+b[i]);
				For(now,0,ub){
					if (now<=b[i])
						Add(dp[i][j+now][t],v);
					else
						Add(dp[i][j+now][t+now-b[i]],v);
				}
			}
	int ans=0;
	For(j,a[c1],c0)
		For(t,0,k){
//			if (dp[c1][j][t])
//				printf("dp[%d][%d][%d] = %d\n",c1,j,t,dp[c1][j][t]);
			Add(ans,dp[c1][j][t]);
		}
	cout<<ans<<endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<int, int>;
using VI = vector<int>;

const int mod = 998244353;
inline int add(int u, int v) {
  return u + v >= mod ? u + v - mod : u + v;
}
inline int sub(int u, int v) {
  return u >= v ? u - v : u - v + mod;
}
inline int mul(ll u, int v) {
  return u * v % mod;
}

const int N = 303, M = 1000;
int choose[M][M];

inline int partition(int n, int k) {
  return choose[n + k - 1][k - 1];
}

int ways[N][N][N]; // ways[i][j][f] = last i, j moves, f free ones

int solve(VI& ones, int k) {
  const int n = ones.size();
  if (n == 0) return 1;

  ways[n][0][0] = 1;
  for (int i = n; i >= 1; i--) {
    const int len = ones[i - 1];
    for (int j = 0; j <= k; j++) {
      for (int f = 0; f < N; f++) {
        ways[i - 1][j][f] = ways[i][j][f];
      }
    }
    for (int j = 0; j <= k; j++) {
      for (int f = N - 2; f >= 0; f--) {
        ways[i - 1][j][f] = add(ways[i - 1][j][f], ways[i - 1][j][f + 1]);
      }
    }
    for (int j = 0; j <= k; j++) {
      for (int cur = 1; cur <= len && j + cur <= k; cur++) {
        for (int f = 0; f + cur < N; f++) {
          ways[i - 1][j + cur][f + cur] = add(ways[i - 1][j + cur][f + cur], ways[i][j][f]);
        }
      }
    }
  }

  int ans = 0;
  for (int j = 0; j <= k; j++) {
    for (int f = 0; f < N; f++) {
      ans = add(ans, ways[0][j][f]);
    }
  }

  return ans;
}

int main() {
#ifdef LOCAL_EXEC
//	freopen("sample.in", "r", stdin);
//	freopen("sample.out", "w", stdout);
#else
  ios_base::sync_with_stdio(false); cin.tie(NULL);
#endif

  string s; cin >> s;
  int k; cin >> k;
  vector<int> ones;
  int f = 0;
  bool seen = false;
  for (char c: s) {
    if (c == '1') {
      f++;
    }
    if (c == '0') {
      if (seen) {
        ones.push_back(f);
      }
      f = 0;
      seen = true;
    }
  }
  if (f > 0 && seen) {
    ones.push_back(f);
  }

  for (int n = 0; n < M; n++) {
    choose[n][0] = choose[n][n] = 1;
    for (int r = 1; r < n; r++) {
      choose[n][r] = add(choose[n - 1][r], choose[n - 1][r - 1]);
    }
  }

  k = min(k, 300);
  cout << solve(ones, k) << endl;

  return 0;
}


#include<bits/stdc++.h>

#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = (n)-1; i >= 0; i--)
#define rep1(i,n) for(int i = 1; i <= (n); i++)
#define rrep1(i,n) for(int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

#define MAXN 88
#define MAXS 13000

int dp[MAXN][MAXN][MAXS];

int main(){
  int h,w;cin>>h>>w;
  vector<vector<int>> scores(h);
  rep(i, h)rep(j, w){
    int a;cin>>a;
    scores[i].push_back(a);
  }
  rep(i, h)rep(j, w){
    int a;cin>>a;
    scores[i][j] -= a;
    if(scores[i][j] < 0)scores[i][j] *= -1;
  }

  dp[0][0][scores[0][0]] = 1;
  rep(i, h)rep(j, w)rep(k, MAXS){
    int s = scores[i][j];
    int mpre = k-s;
    if(mpre<0)mpre *= -1;
    if(i && dp[i-1][j][mpre]==1)dp[i][j][k]=1;
    if(j && dp[i][j-1][mpre]==1)dp[i][j][k]=1;

    int ppre = k+s;
    if(ppre<=7000){
      if(i && dp[i-1][j][ppre]==1)dp[i][j][k]=1;
      if(j && dp[i][j-1][ppre]==1)dp[i][j][k]=1;
    }
  }

  int result = 0;
  rrep(i, MAXS)if(dp[h-1][w-1][i])result = i;

  cout<<result<<endl;

  return 0;

  rep(i, h){
    rep(j, w)cout<<scores[i][j]<<' ';cout<<endl;
  }
cout<<"###"<<endl;

rep(k, 15){
cout<<k<<endl;
  rep(i, h){
    rep(j, w)cout<<dp[i][j][k]<<' ';cout<<endl;
  }
  cout<<"---"<<endl;
}

  return 0;
}


#include<stdio.h>

int main()
{
	int A, B;
	scanf("%d %d", &A, &B);
	int sum = A + B + 1;
	char str[sum];
	scanf("%s", &str);
	int ctr = 0;
	for(int i = 0; i < sum; i++){
		if(i == A){
			if(str[i] != '-'){
				printf("No\n");
				return 0;
			}
		}else{
			if(str[i] >= '0' && str[i] <= '9'){
				++ctr;
			}
		}
	}
	if(ctr == A+B){
		printf("Yes\n");
	}else{
		printf("No\n");
	}
}

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cmath>
#include <numeric>
#include <iomanip>
#include <stack>
#include <complex>
#include <functional>
#include <tuple>

using namespace std;

#define Rep(i,a,b) for(ll i = a; i < b; ++i)
#define rep(i,b) Rep(i,0,b)
#define allof(a) (a).begin(), (a).end()

using ll = long long;

constexpr int inf = 1e9 + 7;
constexpr ll infll = 1ll << 60ll;
constexpr ll mod = 1e9 + 7;

// 0~3までは右左下上 4~7までは斜め
constexpr int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
constexpr int dy[] = { 0, -1, 0, 1, 1, -1, -1, 1 };

template<typename T> void chmax(T & a, T b) { a = std::max(a, b); }
template<typename T> void chmin(T & a, T b) { a = std::min(a, b); }

int main() {

    int a, b;
    cin >> a >> b;

    if (abs(a - b) <= 1) cout << "Yay!" << endl;
    else cout << ":(" << endl;

    return 0;
}


// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2")

#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <chrono>
#include <vector>
#include <map>
#include <random>
#include <set>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <stdio.h>
#include <queue>
#include <bitset>
#include <cstdlib>
#include <deque>
#include <cassert>
#include <stack>
using namespace std;

#define mp make_pair
#define f first
#define se second
#define pb push_back
#define ppb pop_back
#define ll long long
#define ull unsigned long long
#define cntbit(x) __builtin_popcount(x)
#define endl '\n'
#define uset unordered_set
#define umap unordered_map
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define ld long double
#define pll pair<long long, long long>

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

inline void setin(string s) {
	freopen(s.c_str(), "r", stdin);
}

inline void setout(string s) {
	freopen(s.c_str(), "w", stdout);
}

template <typename T> inline T range(T l, T r) {
	return uniform_int_distribution <T>(l, r)(rng);
}

template <typename T> void Min(T &a, T b) {
    a = min(a, b);
}

template <typename T> void Max(T &a, T b) {
    a = max(a, b);
}

const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 45;
const int K = 17;
int n, x, y, z;
bool good[1 << K];

int pw[K];

int dp[N][1 << K];

int ans;

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

inline int mt(int a, int b) {
	return 1ll * a * b % mod;
}

inline void add_t(int &a, int b) {
	a = add(a, b);
}

inline void precalc() {
	for(int mask = 0; mask < (1 << K); ++mask) {
		good[mask] = true;
		string s = "";
		for(int j = 0; j < K; ++j) {
			if(1 << j & mask)
				s += '1';
			else
				s += '0';
		}
		while(s.back() == '0')
			s.ppb();
		// reverse(s.begin(), s.end());
		// if(mask == (1 << 4) + (1 << 11) + (1 << 16))
		// 	cout << s << endl;
		int cur = 0;
		// if(mask % 1000 == 0)
		// 	cout << s << endl;
		vector <int> is = {};
		for(int i = 0; i < s.size(); ++i) {
			int j = i;
			while(j < s.size() && s[j] != '1')
				++j;
			// if(s[j] != '0')
			is.pb(j - i + 1);
			// if(mask % 1000 == 0)
			// 	cout << j - i + 1 << ' ';
			// if(mask == (1 << 4) + (1 << 11) + (1 << 16))
			// 	cout << j - i + 1 << ' ';
			i = j;
		}
		reverse(is.begin(), is.end());
		for(int i = 1; i < is.size(); ++i)
			is[i] += is[i-1];
		for(int i = 0; i < is.size(); ++i) {
			for(int j = i + 1; j < is.size(); ++j) {
				for(int k = j + 1; k < is.size(); ++k) {
					int sum1 = is[j-1] - (i == 0 ? 0 : is[i-1]);
					int sum2 = is[k-1] - is[j-1];
					int sum3 = is.back() - is[k-1];
					if(sum1 == x && sum2 == y && sum3 == z) {
						good[mask] = false;
						break;
					}
				}
			}
		}
		// if(mask % 1000 == 0)
		// 	cout << endl;
		// if(mask == (1 << 4) + (1 << 11) + (1 << 16))
		// 	cout << endl;
	}
}

main() {
	for(int i = 1; i < K; ++i)
		pw[i] = 1 << (i - 1);
	ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0);
	// setin("input.txt");
	cin >> n >> x >> y >> z;
	precalc();
	for(int i = 1; i <= 10; ++i)
		if(good[pw[i]])
			dp[1][pw[i]] = 1;
	for(int i = 2; i <= n; ++i) {
		for(int mask = 0; mask < (1 << K); ++mask) {
			for(int j = 1; j <= 10; ++j) {
				int new_mask = ((1 << K) - 1) & ((mask << j) | pw[j]);
				if(good[new_mask])
					add_t(dp[i][new_mask], dp[i-1][mask]);
			}
		}
	}
	int ans = 1;
	for(int i = 1; i <= n; ++i)
		ans = mt(ans, 10);
	for(int mask = 0; mask < (1 << K); ++mask) {
		add_t(ans, -dp[n][mask]);
	}
	// cout << dp[n][(1 << 4) + (1 << 11) + (1 << 16)] << endl;
	cout << ans << endl;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define SZ(x) (int)(x.size())

using ll = long long;
using ld = long double;
const ld eps = 1e-9;
const ll MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;

template <typename T1, typename T2>
void chmax(T1 &a, const T2 &b) {
  if(a < b) a = b;
}

template <typename T1, typename T2>
void chmin(T1 &a, const T2 &b) {
  if(a > b) a = b;
}

template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}

template<typename T1, typename T2>
ostream& operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}

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

  int n, m, q; cin >> n >> m >> q;
  vector<string> s(n);
  for(int i=0;i<n;++i) {
    string tmp; cin >> tmp;
    s[i] = tmp;
  }

  vector<vector<int>> ver(n+1, vector<int>(m+1, 0));
  vector<vector<int>> tate(n+1, vector<int>(m+1, 0)), yoko(n+1, vector<int>(m+1, 0));
  for(int i=0;i<n;++i) {
    for(int j=0;j<m;++j) {
      ver[i+1][j+1] = s[i][j] == '1';
    }
  }
  for(int i=0;i<n;++i) {
    for(int j=0;j<m;++j) {
      ver[i+1][j+1] += ver[i+1][j] + ver[i][j+1] - ver[i][j];
    }
  }

  for(int i=0;i<n-1;++i) {
    for(int j=0;j<m;++j) {
      tate[i+1][j+1] = s[i][j] == '1' && s[i+1][j] == '1';
    }
  }
  for(int i=0;i<n-1;++i) {
    for(int j=0;j<m;++j) {
      tate[i+1][j+1] += tate[i+1][j] + tate[i][j+1] - tate[i][j];
    }
  }

  for(int i=0;i<n;++i) {
    for(int j=0;j<m-1;++j) {
      yoko[i+1][j+1] = s[i][j] == '1' && s[i][j+1] == '1';
    }
  }
  for(int i=0;i<n;++i) {
    for (int j = 0; j < m - 1; ++j) {
      yoko[i + 1][j + 1] += yoko[i + 1][j] + yoko[i][j + 1] - yoko[i][j];
    }
  }

  while(q--) {
    int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;

    int tmpv = ver[x2][y2] - ver[x2][y1-1] - ver[x1-1][y2] + ver[x1-1][y1-1];
    int tmptate = tate[x2-1][y2] - tate[x2-1][y1-1] - tate[x1-1][y2] + tate[x1-1][y1-1];
    int tmpyoko = yoko[x2][y2-1] - yoko[x2][y1-1] - yoko[x1-1][y2-1] + yoko[x1-1][y1-1];

    cout << tmpv - tmptate - tmpyoko << endl;
  }
}


#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define REP1(i, n) for(int i=1; i<=(n); i++)
#define RREP1(i, n) for(int i=(n); i>=1; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#if DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

void solve();
signed main()
{
#if DEBUG
    std::ifstream in("input.txt");
    std::cin.rdbuf(in.rdbuf());
#endif
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}
/*================================*/
#if DEBUG
#define SIZE 1000
#else
#define SIZE 423450
#endif

int N,Q,K;

set<int> um[2][SIZE],dm[2][SIZE];
int BASE = SIZE/2;

struct Item2 {
    int x,y;
    char u;
};
vector<Item2> V;

set<int> ds[SIZE],ls[SIZE];

void solve() {
    cin>>N;
    int x,y;
    char u;
    REP(i,N) {
        cin>>x>>y>>u;
        if (u=='D') {
            dm[0][y-x+BASE].insert(x);
            dm[1][y+x].insert(x);
        }
        if (u=='U') {
            um[0][y-x+BASE].insert(x);
            um[1][y+x].insert(x);
        }
        V.push_back({x,y,u});
        if (u=='D') ds[x].insert(y);
        if (u=='L') ls[y].insert(x);
    }
    int ans = INT_MAX;
    REP(i,N) {
        auto p = V[i];
        if (p.u=='R') {
            {
                auto s = &dm[0][p.y-p.x+BASE];
                auto it = s->upper_bound(p.x);
                if (it != s->end()) chmin(ans, (*it - p.x) * 10);
            }
            {
                auto s = &um[1][p.y+p.x];
                auto it = s->upper_bound(p.x);
                if (it != s->end()) chmin(ans, (*it - p.x) * 10);
            }
            {
                auto s = &ls[p.y];
                auto it = s->upper_bound(p.x);
                if (it != s->end()) {
                    chmin(ans, (*it - p.x) * 5);
                }
            }
        }
        if (p.u=='L') {
            {
                auto s = &um[0][p.y-p.x+BASE];
                auto it = s->lower_bound(p.x);
                if (it != s->begin()) chmin(ans, (p.x - *(--it))*10);
            }
            {
                auto s = &dm[1][p.y+p.x];
                auto it = s->lower_bound(p.x);
                if (it != s->begin()) chmin(ans, (p.x - *(--it))*10);
            }
        }
        if (p.u=='U') {
            auto s = &ds[p.x];
            auto it = s->upper_bound(p.y);
            if (it != s->end()) {
                chmin(ans, (*it - p.y)*5);
            }
        }
    }

    if (ans==INT_MAX) {
        cout << "SAFE" << endl;
    } else {
        cout << ans << endl;
    }
}

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair <int , int> pii;
const int MAXN = 4e5 + 10 , INF = 202020;
int ans = 1e9;
vector <pii> v[MAXN];
int x[MAXN] , y[MAXN] , t[MAXN];
int tu(char c){
	if(c == 'U') return 0;
	else if(c == 'R') return 1;
	else if(c == 'D') return 2;
	return 3;
}
void check(int m){
	for(int i = 0 ; i <= 400000 ; ++ i)
		if((int)v[i].size() > 0){
			sort(v[i].begin() , v[i].end());
			int la = -INF;
			for(int k = 0 ; k < (int)v[i].size() ; ++ k)
				if(v[i][k].second == -1){
					if(la != -INF)
						ans = min(ans , (v[i][k].first - la) * m);
				}
				else
					la = v[i][k].first;
			v[i].resize(0);
		}
}
int main(){
	int N;
	scanf("%d" , &N);
	for(int i = 1 ; i <= N ; ++ i){
		char s[2];
		scanf("%d%d%s" , &x[i] , &y[i] , s);
	//	cout << i << " " << x[i] + y[i] << " " << x[i] - y[i] << " " << t[i] << endl;
		t[i] = tu(s[0]);
	}
	for(int i = 1 ; i <= N ; ++ i)
		if(t[i] % 2 == 0)
			v[x[i]].push_back(make_pair(y[i] , (t[i] == 0 ? 1 : -1)));
	check(5);
	for(int i = 1 ; i <= N ; ++ i)
		if(t[i] % 2 == 1)
			v[y[i]].push_back(make_pair(x[i] , (t[i] == 1 ? 1 : -1)));
	check(5);
	for(int i = 1 ; i <= N ; ++ i)
		if(t[i] == 0 || t[i] == 3)
			v[y[i] - x[i] + 200000].push_back(make_pair(x[i] , (t[i] == 0 ? 1 : -1)));
	check(10);
	for(int i = 1 ; i <= N ; ++ i)
		if(t[i] == 1 || t[i] == 2)
			v[y[i] - x[i] + 200000].push_back(make_pair(x[i] , (t[i] == 1 ? 1 : -1)));
	check(10);
	for(int i = 1 ; i <= N ; ++ i)
		if(t[i] == 0 || t[i] == 1)
			v[x[i] + y[i]].push_back(make_pair(x[i] , (t[i] == 1 ? 1 : -1)));
	check(10);
	for(int i = 1 ; i <= N ; ++ i)
		if(t[i] == 2 || t[i] == 3)
			v[x[i] + y[i]].push_back(make_pair(x[i] , (t[i] == 2 ? 1 : -1)));
	check(10);
	if(ans == 1e9)
		cout << "SAFE" << endl;
	else
		cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define rrep(i, n) for (int (i) = 1; (i) <= (n); (i++))
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
#define yn {puts("Yes");}else {puts("No");}
#define printd(x) printf("%.12f\n", ans);
template<class T> bool chmin(T& a, const T& b) {if (a > b) {a = b; return 1;} return 0;}
template<class T> bool chmax(T& a, const T& b) {if (a < b) {a = b; return 1;} return 0;}
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

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

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

const ll N = 4e5 + 5;
const ll mod = 998244353;

ll fac[N], inv[N];

ll power(ll x, ll y)
{
    ll res = 1;
    while (y)
    {
        if (y & 1)
            res = res * x % mod;
        x = x * x % mod;
        y >>= 1;
    }
    return res;
}

void init()
{
    fac[0] = inv[0] = 1;
    for (ll i = 1; i < N; i++)
    {
        fac[i] = i * fac[i - 1] % mod;
        inv[i] = power(fac[i], mod - 2);
    }
}

ll ncr(ll n, ll r)
{
    if (r < 0 || r > n)
        return 0;
    return fac[n] * inv[r] % mod * inv[n - r] % mod;
}
// https://betrue12.hateblo.jp/entry/2020/05/11/093054
signed main()
{
    init();

    ll n, m, k;
    cin >> n >> m >> k;

    ll ans = 0;
    for (ll i = 0; i <= k; i++)
    {
        ll curr = ncr(n - 1, i) * m % mod * power(m - 1, n - 1 - i) % mod;
        ans = (ans + curr) % mod;
    }
    cout << ans << endl;
    return 0;
}


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

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

typedef long long ll;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)
//定数
#define INF 1000000000 //10^9:極めて大きい値,∞
//略記
#define PB push_back //vectorヘの挿入
#define MP make_pair //pairのコンストラクタ
#define F first //pairの一つ目の要素
#define S second　//pairの二つ目の要素

int main(){
    int n;
    cin>>n;
    vector<pair<int,int>> a(n),b(n);
    bool ac[n]={};
    rep(i,n) cin>>a[i].first>>a[i].second;
    rep(i,n) cin>>b[i].first>>b[i].second;
    sort(a.begin(),a.end());
    sort(b.begin(),b.end());
    int j=0;
    int ans=0;
    rep(i,n){
        pair<int,int> sel_b=b[i];
        int max_y=-1;
        int sel_a;
        rep(j,n){
            if(ac[j])continue;
            if(a[j].first<sel_b.first && a[j].second<sel_b.second){
                if(a[j].second>max_y){
                    max_y=a[j].second;
                    sel_a=j;
                }
            }
        }
        if(max_y>-1){
            ans++;
            ac[sel_a]=true;
        }
    }
    cout<<ans<<endl;
    return 0;
}

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <functional>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <vector>
#include <climits>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;

struct EdgeMF
{
	int to;
	ll capacity;
	int reverse;
};

void addEdgeForFlow(vector<vector<EdgeMF>>& G, int from, int to, ll capacity, ll reverseCapacity)
{
	G[from].push_back({ to, capacity, (int)G[to].size() });
	G[to].push_back({ from, reverseCapacity, (int)G[from].size() - 1 });
}

ll dfsForFlow(vector<vector<EdgeMF>>& G, vector<bool>& used, int u, int t, ll f)
{
	if (u == t)
	{
		return f;
	}

	used[u] = true;
	for (auto& e : G[u])
	{
		if (used[e.to] || e.capacity <= 0)
		{
			continue;
		}

		auto d = dfsForFlow(G, used, e.to, t, min(f, e.capacity));
		if (d > 0)
		{
			e.capacity -= d;
			G[e.to][e.reverse].capacity += d;
			return d;
		}
	}

	return 0;
}

ll calcMaxFlow(vector<vector<EdgeMF>>& G, int s, int t)
{
	auto f = 0ll;
	while (true)
	{
		vector<bool> used((int)G.size());
		auto d = dfsForFlow(G, used, s, t, LLONG_MAX);
		if (d <= 0)
		{
			break;
		}
		f += d;
	}

	return f;
}

int main()
{
	int N;
	cin >> N;
	vector<int> x(2 * N);
	vector<int> y(2 * N);
	for (auto i = 0; i < 2 * N; ++i)
	{
		cin >> x[i] >> y[i];
	}

	vector<vector<EdgeMF>> G(2 * N + 2);
	for (auto i = 0; i < N; ++i)
	{
		for (auto j = N; j < 2 * N; ++j)
		{
			if (x[i] < x[j] && y[i] < y[j])
			{
				addEdgeForFlow(G, i, j, 1, 0);
			}
		}
	}
	int s = 2 * N;
	int t = s + 1;
	for (auto i = 0; i < N; ++i)
	{
		addEdgeForFlow(G, s, i, 1, 0);
		addEdgeForFlow(G, i + N, t, 1, 0);
	}

	cout << calcMaxFlow(G, s, t) << endl;

	return 0;
}


#define _USE_MATH_DEFINES
#include  <iostream>
#include  <algorithm>
#include  <climits>
#include  <vector>
#include  <string>
#include  <cstring>
#include  <stack>
#include  <queue>
#include  <cmath>
#include  <iomanip>
#include  <set>
#include  <map>
#include  <new>
#include <cstdint>
#include <cctype>
#include  <cstdio>
using namespace std;
#define rep(i,n)  for(int i = 0; i < n; i++)
#define lrep(i,n,m)  for(int i = n; i < m; i++)

using ll = long long;
using ld = long double;
using Vii = vector<vector<int> >;
using Gjudge = vector<vector<bool>>;
using Vi = vector<int>;
using Vl = vector<ll>;
using Vd = vector<double>;
using Vld = vector<long double>;
using Vll = vector<vector<ll> >;
using Vldd = vector<vector<ld> >;
using Vs = vector<string>;
using Vb = vector<bool>;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Pli = pair<ll, int>;
using VPi = vector<Pi>;
using VPl = vector<Pl>;
using VPli = vector<Pli>;
using Ti = tuple<int, int, int>;
using Tl = tuple<ll, ll, ll>;

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

const int Mod = static_cast<int>(1e9 + 7);
const int INF = INT_MAX / 10;
const ll LINF = LLONG_MAX / 10;
const int dx[4] = { 1,0,-1,0 };
const int dy[4] = { 0,1,0,-1 };

int main()
{
	int n, r;
	cin >> n >> r;
	if (n >= 10) cout << r << endl;
	else {
		r += 100 * (10 - n);
		cout << r << endl;
	}
}


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

ll INF = 1LL << 60;
using G = vector<map<int, int>>;

void
dfs(int node, ll count, G &arr, vector<bool> seen, vector<long> &to_K)
{
  seen[node] = true;
  for(auto child: arr[node]){
    if(seen[child.first]) continue;
    to_K[child.first] = count + child.second;
    dfs(child.first, count + child.second, arr, seen, to_K);
  }
}

int
main() {
  int N; cin >> N;
  G arr(N + 1);
  rep(i, 0, N - 1){
    int a, b, c; cin >> a >> b >> c;
    arr[a][b] = c;
    arr[b][a] = c;
  }
  vector<long> to_K(N + 1);

  int Q, K; cin >> Q >> K;
  vector<pair<int, int>> q(Q);
  rep(i, 0, Q){
    cin >> q[i].first;
    cin >> q[i].second;
  }

  vector<bool> seen(N + 1, false);
  //dfs(K, 0, arr, seen, to_K);

  queue<int> que;
  que.push(K);
  seen[K] = true;
  to_K[K] = 0;
  while(!que.empty()){
    int node = que.front();
    que.pop();
    for(auto child : arr[node]){
      if(seen[child.first]) continue;
      to_K[child.first] = arr[node][child.first] + to_K[node];
      seen[child.first] = true;
      que.push(child.first);
    }
  }

  rep(i, 0, Q){
    cout << to_K[q[i].first] + to_K[q[i].second] << endl;
  }

}

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

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

    vector<vector<int>> tree(N);
    vector<int> n_in(N);
    for (int i = 0; i < N - 1 + M; i++)
    {
        int Ai, Bi;
        cin >> Ai >> Bi;
        Ai--;
        Bi--;
        tree.at(Ai).push_back(Bi);
        n_in.at(Bi) += 1;
    }

    int r;
    for (int i = 0; i < N; i++)
    {
        if (n_in.at(i) == 0)
        {
            r = i;
            break;
        }
    }

    vector<int> l;
    queue<int> q;
    q.push(r);
    while (q.size())
    {
        int n = q.front();
        q.pop();

        l.push_back(n);
        for (auto &&e : tree.at(n))
        {
            n_in.at(e)--;
            if (n_in.at(e) == 0)
            {
                q.push(e);
            }
        }
    }

    vector<int> p(N, 0);
    for (auto &&n : l)
    {
        for (auto &&e : tree.at(n))
        {
            p.at(e) = n + 1;
        }
    }

    for (int i = 0; i < N; i++)
    {
        cout << p.at(i) << endl;
    }
}


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

using namespace std;

int main(void)
{
    int wsd[10], ko[10];
    for(int i=0; i<10; i++) cin>>wsd[i];
    for(int i=0; i<10; i++) cin>>ko[i];
    sort(wsd, wsd+10);
    sort(ko, ko+10);
    printf("%d %d\n",wsd[9]+wsd[8]+wsd[7], ko[9]+ko[8]+ko[7]);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n,k;
const LL mof=1e9+7;
LL p=mof;
const LL maxn=200007;
inline LL pow_mod(LL a,LL k,LL mo)
{
    LL ans=1;
    while(k)
    {
        if(k%2) ans=ans*a%mo;
        a=a*a%mo;
        k>>=1;
    }
    return ans%mo;
}
LL jc[maxn+7];
inline LL inv(LL x)
{
    return pow_mod(x,mof-2,mof);
}
LL Inv[maxn+7];
LL C(LL N,LL k)
{
    if(k>N||k<0)return 0;
    if(k==0||k==N)return 1;
    LL ans=1;
    ans=ans*jc[N]*Inv[k]%mof*Inv[N-k]%mof;
    return ans;
}
void init()
{
    jc[1]=jc[0]=1;
    for(LL i=2; i<=maxn; ++i) jc[i]=jc[i-1]*i%mof;
    for(int i=0; i<=maxn; ++i) Inv[i]=inv(jc[i]);
}
const long long MOD = 1000000007;

void solve(long long H, long long W, long long A, long long B){
    LL ans = 0;
    LL h = H-A;
    for(LL w=B+1;w<=W;++w)
    {
        ans += C(h+w-2,h-1) * C(H-h+W-w-1,H-h-1);
        ans %= mof;
    }
    cout<<ans<<endl;
}

int main(){
    init();
    long long H;
    scanf("%lld",&H);
    long long W;
    scanf("%lld",&W);
    long long A;
    scanf("%lld",&A);
    long long B;
    scanf("%lld",&B);
    solve(H, W, A, B);
    return 0;
}

#include <iostream>
#include <cmath>
#include <iomanip>
int main() {
    double x1, y1, x2, y2;
    std::cin >> x1 >> y1 >> x2 >> y2;
    std::cout << std::fixed << std::setprecision(6)
        << std::sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))
        << std::endl;
}


#include<iostream>
#include<cassert>
#include<stdexcept>
#include<utility>
#include<functional>
#include<numeric>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<array>
#include<stack>
#include<queue>
#include<deque>
#include<vector>
#include<complex>
#include<set>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<string>
#include<bitset>
#include<memory>
#include<iomanip>
using namespace std;
using ll=long long;

#include<complex>
namespace Geometry{
    using namespace std;
    using ld=double;
    using Point=complex<ld>;

    struct Line{
        Point a,b;
    };

    struct Circle{
        Point p;
        ld r;
    };
    const ld EPS=1e-6;
    inline bool EQ(Point a,Point b){return abs(a-b)<EPS;}
    ld dot(Point a,Point b){return real(a)*real(b)+imag(a)*imag(b);}
    ld cross(Point a,Point b){return real(a)*imag(b)-imag(a)*real(b);}

    const int CCW=1;
    const int CW=-1;
    const int BAC=2;
    const int ACB=-2;
    const int ABC=0;
    int ccw(Point a,Point b,Point c){
        b-=a,c-=a;
        if(cross(b,c)>EPS) return CCW; // counter clockwise
        if(cross(b,c)<-EPS) return CW; //clockwise
        if(dot(b,c)<-EPS) return BAC; //b-a-c
        if(abs(b)<abs(c)) return ACB; //a-c-b
        return ABC; //a-b-c (abs(b)>abs(c))
    }
}
#include<vector>
#include<algorithm>

namespace Geometry{
    vector<Point> convexFull(const vector<Point>& _ps){
        vector<Point> ps=_ps;
        int n=ps.size();
        if(n<=1) return ps;
        auto cmp=[](const Point &lhs,const Point &rhs){
            return real(lhs)==real(rhs) ? imag(lhs)<imag(rhs) : real(lhs)<real(rhs);
        };
        sort(ps.begin(),ps.end(),cmp);
        int k=0;
        vector<Point> qs(n*2);
        for(int i=0;i<n;i++){
            while(k>=2 && ccw(qs[k-2],qs[k-1],ps[i])<=0) --k;
            qs[k++]=ps[i];
        }
        int t=k;
        for(int i=n-2;i>=0;i--){
            while(k>t && ccw(qs[k-2],qs[k-1],ps[i])<=0) --k;
            qs[k++]=ps[i];
        }
        qs.resize(k-1);
        return qs;
    }
}
using namespace Geometry;

const double pi=acos(-1);
int main(){
    int n;
    cin>>n;
    vector<Point> ps(n);
    for(int i=0;i<n;i++){
        int x,y;
        cin>>x>>y;
        ps[i]={x,y};
    }
    cout<<setprecision(10)<<fixed;

    vector<Point> cps=convexFull(ps);
    vector<double> res(n);
    if(cps.size()<=2){
        for(int j=0;j<cps.size();j++){
            for(int i=0;i<n;i++){
                if(cps[j]==ps[i]){
                    res[i]=1./cps.size();
                }
            }
        }
        for(int i=0;i<n;i++) cout<<res[i]<<endl;

        return 0;
    }
    auto divd=[&](Point lhs,Point rhs){
        return (lhs-rhs)*Point(0,1);
    };

    for(int i=0;i<cps.size();i++){
        auto pre=cps[(i+cps.size()-1)%cps.size()];
        auto nex=cps[(i+1)%cps.size()];
        auto t1=divd(pre,cps[i]);
        auto t2=divd(cps[i],nex);
        auto theta=arg(t2/t1);
        for(int j=0;j<n;j++){
            if(cps[i]==ps[j]){
                res[j]=theta/(2*pi);
            }
        }
    }
    for(int i=0;i<n;i++){
        cout<<res[i]<<endl;
    }

    return 0;
}


#include <bits/stdc++.h>

#define R register
#define ll long long
#define sum(a, b, mod) (((a) + (b)) % mod)

const int mod = 1e9 + 7;
const int MaxN = 3e5 + 10;

char s[MaxN];
ll n, f[MaxN][3][3];
void add(ll &a, ll b) {a += b, ((a > mod) ? (a -= mod) : 0); }

int main()
{
    f[0][0][0] = 1;
    scanf("%s", s + 1), n = strlen(s + 1);
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            for(int k = 0; k < 3; k++)
            {
                if(s[i + 1] != '0')
                {
                    if(k)
                        add(f[i + 1][j][k - 1], f[i][j][k]);
                    else add(f[i + 1][std::min(j + 1, 2)][k], f[i][j][k]);
                }
                if(s[i + 1] != '1')
                {
                    if(k == 2)
                        add(f[i + 1][j][1], f[i][j][k]);
                    else add(f[i + 1][j][k + 1], f[i][j][k]);
                }
            }
        }
    }
    ll ans = 0;
    for(int i = 0; i < 3; i++)
        for(int j = 0; j <= i; j++)
            add(ans, f[n][i][j]);
    printf("%lld\n", ans);
    return 0;
}


#include<bits/stdc++.h>

template< typename G >
struct StronglyConnectedComponents {
    using UnWeightedGraph=std::vector<std::vector<int>>;
  const G &g;
  UnWeightedGraph gg, rg;
  std::vector< int > comp, order, used;

  StronglyConnectedComponents(G &g) : g(g), gg(g.size()), rg(g.size()), comp(g.size(), -1), used(g.size()) {
    for(int i = 0; i < g.size(); i++) {
      for(auto e : g[i]) {
        gg[i].emplace_back((int) e);
        rg[(int) e].emplace_back(i);
      }
    }
  }

  int operator[](int k) {
    return comp[k];
  }

  void dfs(int idx) {
    if(used[idx]) return;
    used[idx] = true;
    for(int to : gg[idx]) dfs(to);
    order.push_back(idx);
  }

  void rdfs(int idx, int cnt) {
    if(comp[idx] != -1) return;
    comp[idx] = cnt;
    for(int to : rg[idx]) rdfs(to, cnt);
  }

  void build(UnWeightedGraph &t) {
    for(int i = 0; i < gg.size(); i++) dfs(i);
    reverse(begin(order), end(order));
    int ptr = 0;
    for(int i : order) if(comp[i] == -1) rdfs(i, ptr), ptr++;

    t.resize(ptr);
    for(int i = 0; i < g.size(); i++) {
      for(auto &to : g[i]) {
        int x = comp[i], y = comp[to];
        if(x == y) continue;
        t[x].push_back(y);
      }
    }
  }
};
int main(){
    int N,M;
    std::cin>>N>>M;
    std::vector<std::vector<bool>> to(N,std::vector<bool>(N,false));
    std::vector<std::vector<int>> graph(N),t;
    std::vector<int> ans;
    for(int _=0;_<M;++_){
        int u,v;
        std::cin>>u>>v;
        u--;v--;
        if(ans.empty()){
            graph[u].push_back(v);
            StronglyConnectedComponents<std::vector<std::vector<int>>> scc(graph);
            scc.build(t);
            std::vector<std::vector<int>> tmp(N);
            for(int i=0;i<N;++i) tmp[scc[i]].push_back(i);
            int ok=-1;
            for(int i=0;i<N;++i) if(tmp[i].size()>1) ok=i;
            if(ok!=-1){
                int now=v;
                while(now!=u){
                    ans.push_back(now);
                    for(auto e:graph[now]) if(scc[e]==ok){
                        now=e;
                        break;
                    }
                }
                ans.push_back(u);
            }
            for(int i=0;i<N;++i) if(to[v][i]) to[u][i]=true;
        }
        else if(std::find(ans.begin(),ans.end(),u)!=ans.end()&&
                std::find(ans.begin(),ans.end(),v)!=ans.end()){
            int ch=std::find(ans.begin(),ans.end(),v)-ans.begin();
            int sz=ans.size();
            std::vector<int> tmp;
            while(ans[ch]!=u){
                tmp.push_back(ans[ch]);
                ch=(ch+1)%sz;
            }
            tmp.push_back(u);
            ans=tmp;
        }
    }
    if(ans.empty()){
        std::cout<<-1<<std::endl;
        return 0;
    }
    std::cout<<ans.size()<<std::endl;
    for(auto a:ans) std::cout<<a+1<<std::endl;
}

#include <stdio.h>
#include <algorithm>
#include <tuple>
#include <vector>
#include <queue>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int n, m;
vector<int> e[1010];

int v[1010], last[1010];
vector<int> r(2000, 0);
void bfs(int node) {
	queue<int> q;
	for (int x : e[node])
		q.emplace(x), v[x] = last[x] = node;
	while (!q.empty()) {
		int cur = q.front();
		q.pop();

		for (int x : e[cur])
			if (v[x] != node)
				q.emplace(x), v[x] = node, last[x] = cur;
	}
	if (v[node] != node)
		return;
	int pop = node;
	vector<int> path;
	path.emplace_back(node);
	while (last[pop] != node) {
		path.emplace_back(last[pop]);
		pop = last[pop];
	}
	if (path.size() < r.size())
		r.swap(path);
}

int main() {
	scanf("%d %d", &n, &m);
	for (int i = 0; i < m; i++) {
		int a, b;
		scanf("%d %d", &a, &b);
		e[a].emplace_back(b);
	}
	for (int i = 1; i <= n; i++)
		bfs(i);
	if (n < r.size())
		printf("-1\n");
	else {
		printf("%d\n", r.size());
		for (int v : r)
			printf("%d\n", v);
	}
}


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

int main() {

  long long n;
  cin >> n;

  long long ans = 0;

  for(int i = 1; i <= n; i++){
    if(i % 3 == 0 && i % 5 == 0) {
      //FizzBuzz
    } else if(i % 3 == 0) {
      //Fizz
    } else if(i % 5 == 0) {
      //Buzz
    } else {
      ans = ans + i;
    }
  }

  cout << ans;

}

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

long long f(long long n, long long x){
  long long y = x / n;
  if(x < n){
    return 0;
  } else {
    return y * (n + y * n) / 2;
  }
}

int main(void){
  long long n;
  cin >> n;
  cout << f(1,n) - f(3,n) - f(5,n) + f(15,n) << endl;
  return 0;
}


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

/*
提出前に確認!
- setとmultisetを間違えない
*/

/*
26進数で考えればいい
*/

void	solve(long long num)
{
	if (num == 0) return ;
	num--;
	solve(num / 26);
	cout << (char)(num % 26 + 'a');
}

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

#include <bits/stdc++.h>
#include <set>
using namespace std;
using pii = pair<int,int>;
using pll = pair<long long, long long>;
const long long INF = 1<<29;

const int MOD = 1000000007;
long long ruizyou(long long m,long long n){
  if(m == 0)return 0;
  if(m == 1)return 1;
  long long ans = 1;
  long long tmp = m;
  for(int i=0;i<=30;i++){
    if(n & (1<<i)){
      ans *= tmp;
      ans %= MOD;
    }
    tmp *= tmp;
    tmp %= MOD;
  }
  return ans;
}
long long kaizyou(long long x){
  if(x == 0)return 1;
  return x * kaizyou(x-1) % MOD;
}
long long comb(long long x,long long y){
  long long bunsi = kaizyou(x);
  long long bunbo = kaizyou(x-y) * kaizyou(y) % MOD;
  return bunsi * ruizyou(bunbo,MOD-2) % MOD;
}
struct unionfind{
  vector<int> par;
  vector<vector<int>> children;
  vector<int> hukasa;
  vector<int> kazu;
  unionfind(int n){
    par = vector<int>(n);
    hukasa = vector<int>(n,0);
    kazu = vector<int>(n,1);
    for(int i=0;i<n;i++){
      par.at(i) = i;
    }
  }
  int root(int x){
    if(par.at(x) == x)return x;
    return root(par.at(x));
  }
  void unite(int x,int y){
    int rx = root(x);
    int ry = root(y);
    if(rx == ry)return;
    if(hukasa.at(rx) >= hukasa.at(ry)){
      par.at(ry) = rx;
      hukasa.at(rx) = max(hukasa.at(ry) + 1,hukasa.at(rx));
      kazu.at(rx) += kazu.at(ry);
    }
    else{
      par.at(rx) = ry;
      kazu.at(ry) += kazu.at(rx);
    }
  }
  void newkazu(){
    int n = kazu.size();
    for(int i=0;i<n;i++){
      kazu.at(i) = kazu.at(root(i));
    }
  }
  bool same(int x,int y){
    return root(x) == root(y);
  }
};
int ctoi(char a){
  return a - '0';
}
long long gcd(long long a,long long b){
  long long c = max(a,b);
  long long d = min(a,b);
  if(d % c == 0)return c;
  return gcd(c,d%c);
}
long long lcm(long long a,long long b){
  return a * b / gcd(a,b);
}
void hukasa(vector<vector<int>> s,vector<vector<int>> table,int &ans,pii frm,int n,int m){
  int x,y;
  tie(x,y) = frm;
  set<pii> se;
  if(x-1 >= 0)se.insert(pii(x-1,y));
  if(x+1 < n)se.insert(pii(x+1,y));
  if(y-1 >= 0)se.insert(pii(x,y-1));
  if(y+1 < m)se.insert(pii(x,y+1));
  for(auto t:se){
    int tx,ty;
    tie(tx,ty) = t;
    if(s.at(tx).at(ty) && table.at(tx).at(ty) == -1){
      table.at(tx).at(ty) = table.at(x).at(y) + 1;
      ans = max(ans,table.at(tx).at(ty));
      hukasa(s,table,ans,t,n,m);
      table.at(tx).at(ty) = -1;
    }
  }
}
void daiku(int frm,vector<vector<pii>> g,vector<int> &cost,int n){
  priority_queue<pii,vector<pii>,greater<pii>> q;
  q.push(pii(cost.at(frm),frm));
  vector<bool> kakutei(n,false);
  while(!q.empty()){
    pii now = q.top();
    q.pop();
    int n1,n2;
    tie(n1,n2) = now;
    if(kakutei.at(n2))continue;
    kakutei.at(n2) = true;
    for(auto t:g.at(n2)){
      int tx,ty;
      tie(tx,ty) = t;
      if(cost.at(tx) > n1 + ty){
        cost.at(tx) = n1 + ty;
        q.push(pii(cost.at(tx),tx));
      }
    }
  }
}
int main(){
  long long n;
  cin >> n;
  long long tmp = 26;
  int num = 1;
  while(n > tmp){
    n -= tmp;
    tmp *= 26;
    num++;
  }
  n--;
  vector<int> c(num,0);
  tmp = 0;
  while(n / 26 > 0){
    c.at(tmp) = (n % 26);
    n /= 26;
    tmp++;
  }
  c.at(tmp) = (n);
  reverse(c.begin(),c.end());
  for(int i=0;i<c.size();i++){
    cout << (char)('a' + c.at(i));
  }
  cout << endl;
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <complex>
#include <utility>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <tuple>
#include <cmath>
#include <bitset>
#include <cctype>
#include <set>
#include <map>
#include <unordered_map>
#include <numeric>
#include <functional>
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(ll i=ll(a);i<ll(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(),(V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
inline void Yes(bool condition){ if(condition) PRINT("Yes"); else PRINT("No"); }
template<class itr> void cins(itr first,itr last){
    for (auto i = first;i != last;i++){
        cin >> (*i);
    }
}
template<class itr> void array_output(itr start,itr goal){
    string ans = "",k = " ";
    for (auto i = start;i != goal;i++) ans += to_string(*i)+k;
    if (!ans.empty()) ans.pop_back();
    PRINT(ans);
}
ll gcd(ll a, ll b) {
    return a ? gcd(b%a,a) : b;
}
const ll INF = 1e15;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
typedef pair<ll,ll> P;
typedef pair<double,double> point;
const ll MAX = 200005;
constexpr ll nx[8] = {1,0,-1,0,1,-1,1,-1};
constexpr ll ny[8] = {0,1,0,-1,1,1,-1,-1};
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n,m;
    cin >> n >> m;
    vector<bool> a(m,1);
    vector<vector<ll>> sport(n,vector<ll>(m,0));
    rep(i,n){
        rep(j,m){
            cin >> sport[i][j];
            sport[i][j]--;
        }
    }
    ll ans = INF;
    rep(i,m){
        vector<ll> cnt(m,0);
        ll ma = 0;
        rep(j,n){
            rep(k,m){
                ll s = sport[j][k];
                if (a[s]){
                    cnt[s]++;
                    chmax(ma,cnt[s]);
                    break;
                }
            }
        }
        chmin(ans,ma);
        rep(j,m){
            if (cnt[j] == ma){
                a[j] = 0;
                break;
            }
        }
    }
    PRINT(ans);
}

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
using ll = long long;
using uint = unsigned int;
using graph = std::vector<std::vector<ll>>;
using wGraph = std::vector<std::vector<std::pair<ll,ll>>>;
#define rep(i,n) for (int i=0; i < int(n); i++)

using namespace std;
ll MOD7 = 1000000007;
ll MOD9 = 1000000009;
vector<ll> InputVec(ll N){
    vector<ll> A(N);
    for (int i = 0; i < N; ++i) {
        cin >> A[i];
    }
    return A;
}
void OutputVec(vector<ll> A){
    for (int i = 0; i < A.size(); ++i) {
        cout << A[i] << ",";
    }
    cout << endl;
}
vector<vector<ll>> InputVec2d(ll H,ll W){
    vector<vector<ll>> A(H);
    for (int yi = 0; yi < H; ++yi) {
        A[yi] = vector<ll>(W);
    }
    for (int yi = 0; yi < H; ++yi) {
        for (int xi = 0; xi < W; ++xi) {
            cin >> A[yi][xi];
        }
    }
    return A;
}
vector<vector<char>> InputVec2dStr(ll H,ll W){
    vector<vector<char>> A(H);
    for (int yi = 0; yi < H; ++yi) {
        A[yi] = vector<char>(W);
    }
    for (int yi = 0; yi < H; ++yi) {
        string S;
        cin >> S;
        for (int xi = 0; xi < W; ++xi) {
            A[yi][xi] = S[xi];
        }
    }
    return A;
}
void OutputVec2d(vector<vector<ll>> A){
    for (int yi = 0; yi < A.size(); ++yi) {
        for (int xi = 0; xi < A[yi].size(); ++xi) {
            cout << A[yi][xi] << ",";
        }
        cout << endl;
    }
}
graph InputGraph(ll N,ll M){
    graph G(N);
    for (int i = 0; i < M; ++i) {
        ll a,b;
        cin >> a >> b;
        a--;b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    return G;
}
graph InputDGraph(ll N,ll M){
    graph G(N);
    for (int i = 0; i < M; ++i) {
        ll a,b;
        cin >> a >> b;
        a--;b--;
        G[a].push_back(b);
    }
    return G;
}
void OutputGraph(graph G){
    for (int i = 0; i < G.size(); ++i) {
        cout << i << ":";
        for (int j = 0; j < G[i].size(); ++j) {
            cout << G[i][j]<< ",";
        }
        cout << endl;
    }
}
void OutputAnswerBool(bool b,bool isUpperCase = false){
    if(b){
        if(isUpperCase){
            cout << "YES" << endl;
        }else{
            cout << "Yes" << endl;
        }
    }else{
        if(isUpperCase){
            cout << "NO" << endl;
        }else{
            cout << "No" << endl;
        }
    }
}

int main() {
    ll N,M;
    cin >> N >> M;
    auto A = InputVec2d(N,M);
    vector<bool> b(M);
    for (int i = 0; i < M; ++i) {
        b[i] = true;
    }
    ll m = N;
    for (int i = 0; i < M; ++i) {
        vector<ll> bucket(M);
        for (int ni = 0; ni < N; ++ni) {
            for (int mi = 0; mi < M; ++mi) {
                if(b[A[ni][mi]-1]){
                    bucket[A[ni][mi]-1]++;
                    break;
                }
            }
        }
        ll del = -1;
        for (int j = 0; j < M; ++j) {
            if(b[j]){
                if(del == -1){
                    del = j;
                }
                if(bucket[j] >= bucket[del]){
                    del = j;
                }
            }
        }
        m = min(m,bucket[del]);
        b[del] = false;
    }
    cout << m << endl;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
  int m;
  cin >> m;
  vector<int> nodes;
  vector<pair<int, int>> child;
  int n = 0;
  for (int i = 0; i < m; i++){
    string q;
    cin >> q;
    if (q == "insert"){
      int k;
      cin >> k;
      if (n != 0){
        int v = 0;
        while (1){
          if (k < nodes[v]){
            if (child[v].first != -1){
              v = child[v].first;
            } else {
              child[v].first = nodes.size();
              break;
            }
          } else {
            if (child[v].second != -1){
              v = child[v].second;
            } else {
              child[v].second = nodes.size();
              break;
            }
          }
        }
      }
      nodes.push_back(k);
      child.push_back(make_pair(-1, -1));
      n++;
    }
    if (q == "find"){
      int k;
      cin >> k;
      if (n == 0){
        cout << "no" << endl;
      } else {
        int v = 0;
        while (1){
          if (k < nodes[v]){
            if (child[v].first == -1){
              cout << "no" << endl;
              break;
            } else {
              v = child[v].first;
            }
          } else if (k > nodes[v]){
            if (child[v].second == -1){
              cout << "no" << endl;
              break;
            } else {
              v = child[v].second;
            }
          } else {
            cout << "yes" << endl;
            break;
          }
        }
      }
    }
    if (q == "delete"){
      int k;
      cin >> k;
      int v = 0;
      int u;
      while (1){
        if (k < nodes[v]){
          u = v;
          v = child[v].first;
        } else if (k > nodes[v]){
          u = v;
          v = child[v].second;
        } else {
          break;
        }
      }
      if (child[v].first != -1 && child[v].second != -1){
        u = v;
        int w = child[v].second;
        while (child[w].first != -1 || child[w].second != -1){
          u = w;
          if (child[w].first != -1){
            w = child[w].first;
          } else {
            w = child[w].second;
          }
        }
        swap(nodes[v], nodes[w]);
        v = w;
      }
      if (child[v].first != -1 && child[v].second == -1){
        int w = child[v].first;
        nodes[v] = nodes[w];
        child[v].first = child[w].first;
        child[v].second = child[w].second;
      } else if (child[v].second != -1 && child[v].first == -1){
        int w = child[v].second;
        nodes[v] = nodes[w];
        child[v].first = child[w].first;
        child[v].second = child[w].second;
      } else {
        if (child[u].first == v){
          child[u].first = -1;
        } else {
          child[u].second = -1;
        }
      }
      n--;
    }
    if (q == "print"){
      //Inorder
      if (n != 0){
        stack<int> S;
        vector<int> t(nodes.size(), 0);
        S.push(0);
        while (!S.empty()){
          int v = S.top();
          S.pop();
          if (t[v] == 1){
            cout << ' ' << nodes[v];
          }
          if (t[v] == 0){
            S.push(v);
            if (child[v].second != -1){
              S.push(child[v].second);
            }
            S.push(v);
            if (child[v].first != -1){
              S.push(child[v].first);
            }
          }
          t[v]++;
        }
      }
      cout << endl;
      //Preorder
      if (n != 0){
        stack<int> S;
        vector<int> t(nodes.size(), 0);
        S.push(0);
        while (!S.empty()){
          int v = S.top();
          S.pop();
          if (t[v] == 0){
            cout << ' ' << nodes[v];
          }
          if (t[v] == 0){
            S.push(v);
            if (child[v].second != -1){
              S.push(child[v].second);
            }
            S.push(v);
            if (child[v].first != -1){
              S.push(child[v].first);
            }
          }
          t[v]++;
        }
      }
      cout << endl;
    }
  }
}

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct node{
    int key;
    node *parent,*left,*right;
};
node *NIL,*root;

void inorder(struct node *u){
    if(u==NIL)return;
    inorder(u->left);
    printf(" %d",u->key);
    inorder(u->right);
}
void preorder(struct node *u){
    if(u==NIL)return;
    printf(" %d",u->key);
    preorder(u->left);
    preorder(u->right);
}
void pr(){
    inorder(root);
    printf("\n");
    preorder(root);
    printf("\n");
}
void insert(int key){
    node *y = NIL;
    node *x = root;
    node *z;
    z = (node *)malloc(sizeof(node));
    z->key = key;
    z->left = NIL;
    z->right = NIL;

    while(x!=NIL){//when x==NIL break
        y = x;
        if(z->key < x->key)x = x->left;
        else x = x->right;
    }
    z->parent = y;
    if(y == NIL)root = z;//z became root,insert root
    else if(z->key < y->key)y->left = z;
    else y->right = z;
}
node * find(node *u,int k){
    while((u != NIL) && (k != u->key)){
    //when you exchange the position of '(u != NIL)' with '(k != u->key)',the program will run failed
    //you must make sure that u!=NIl firstly,otherwise k will compare with NIL->key(which is not defined)
        if(k < u->key)u = u->left;
        else u = u->right;
    }
    return u;
}
node * treeMinmum(node *x){
	while(x->left!=NIL)x = x->left;//"leftmost"
	return x;
}
node * treeSuccessor(node *x){
	if(x->right != NIL)return treeMinmum(x->right);//exist right tree,find the next node inorder

	node *y = x->parent;//not exist,return parent node which acts as left-child node firstly
	while(y!=NIL && y->right == x){
		x = y;
		y = y->parent;
	}
	return y;
}
void treedelete(node *z){
	node *y;//the node to delete
	node *x;//child-node of the node needed to be deleted

	//make sure wheter z delete or not
	if(z->left == NIL || z->right == NIL)y = z;
	else y = treeSuccessor(z);

	if(y->left != NIL)x = y->left;
	else x = y->right;

	if(x != NIL )x->parent = y->parent; // make y->parent as x new parent

	// make x as y->parent new child
	if(y->parent == NIL)root = x;//y was root
	else if(y == y->parent->left)y->parent->left = x;
	else y->parent->right = x;

	if(y!=z)z->key = y->key;
	free(y);
}
int main()
{
    int n,k;
    scanf("%d",&n);
    char s[20];
    for(int i=0;i<n;++i){
        scanf("%s",s);
        if(s[0]=='i'){
            scanf("%d",&k);
            insert(k);
        }else if(s[0]=='f'){
            scanf("%d",&k);
            node *t = find(root,k);
            if(t != NIL )printf("yes\n");
            else printf("no\n");
        }
        else if(s[0]=='d'){
        	scanf("%d",&k);
        	treedelete(find(root,k));
		}
		else pr();
    }
    return 0;
}