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

int n, k;
int a[55];
long long c[55][55][2][2];
bool visited[55][55][2][2];

long long solve(int l, int r, int can10, int can11) {
	//if there has been 0---0, we can start to put 1---1 because rev-inv will make a larger number. (0---1 and 1---0 keep same)
	//if there has been 0---1, we can start to put 1---0 because rev will make a larger number. (0---0 and 1---1 keep same)
	if (l > r) {
		return 1;
	}
	if (visited[l][r][can10][can11]) {
		return c[l][r][can10][can11];
	}
	visited[l][r][can10][can11] = true;
	long long& ans = c[l][r][can10][can11];
	for (int nl = 0; nl < 2; ++nl) {
		if (a[l] != -1 && a[l] != nl) { //already fixed.
			continue;
		}
		for (int nr = 0; nr < 2; ++nr) {
			if (a[r] != -1 && a[r] != nr) { //already fixed.
				continue;
			}
			if (!can10 && nl > nr) {
				continue;
			}
			if (!can11 && nl && nr) {
				continue;
			}
			if (l == r && nl != nr) {
				continue;
			}
			ans += solve(l + 1, r - 1, can10 || nl < nr, can11 || (!nl && !nr));
		}
	}
	return ans;
}

int main() {
	cin >> n >> k;
	k++; // because 0000000..000 doesn't count
	memset(a, -1, sizeof(a));
	a[0] = 0; //first one must 0.
	if (solve(0, n - 1, 0, 0) < k) {
		cout << -1 << endl;
		return 0;
	}
	for (int i = 1; i < n; ++i) {
		memset(visited, 0, sizeof(visited));
		a[i] = 0;
		if (k > solve(0, n - 1, 0, 0)) {
			k -= solve(0, n - 1, 0, 0); //remove all a[i] == 0 possibilities.
			a[i] = 1; //so it has to be 1.
		}
	}
	for (int i = 0; i<n; i++)
	{
		cout << a[i];
	}
	cout << endl;
	return 0;
}


#define _ijps 0
#define _CRT_SECURE_NO_DEPRECATE
#pragma comment(linker, "/STACK:667772160")
#include <iostream>
#include <cmath>
#include <vector>
#include <time.h>
#include <map>
#include <set>
#include <deque>
#include <cstdio>
#include <cstdlib>
#include <unordered_map>
#include <bitset>
#include <algorithm>
#include <string>
#include <fstream>
#include <assert.h>
#include <list>
#include <cstring>
using namespace std;

#define name ""
typedef unsigned long long ull;
typedef long long ll;
#define mk make_pair
#define forn(i, n) for(ll i = 0; i < (ll)n; i++)
#define fornn(i, q, n) for(ll i = (ll)q; i < (ll)n; i++)
#define times clock() * 1.0 / CLOCKS_PER_SEC

struct __isoff{
	__isoff(){
		if (_ijps)
			freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);//, freopen("test.txt", "w", stderr);
		//else freopen(name".in", "r", stdin), freopen(name".out", "w", stdout);
		//ios_base::sync_with_stdio(0);
		//srand(time(0));
		srand('C' + 'T' + 'A' + 'C' + 'Y' + 'M' + 'B' + 'A');
	}
	~__isoff(){
		//if(_ijps) cout<<times<<'\n';
	}
} __osafwf;
const ull p1 = 31;
const ull p2 = 29;
const double eps = 1e-8;
const double pi = acos(-1.0);

const int infi = 1e9 + 7;
const ll inf = 1e18 + 7;
const ll dd = 7e5 + 7;
const ll mod = 1e9 + 7;
const ll mod2 = 1e6 + 3;

ll fac[dd], ifac[dd];

ll pows(ll x, int p){
	if(p == 0)
		return 1;
	if(p % 2)
		return pows(x, p - 1) * x % mod2;
	x = pows(x, p / 2);
	return x * x % mod2;
}

ll cnk(ll a, ll b){
	return fac[a] * ifac[b] % mod2 * ifac[a - b] % mod2;
}

const int fd = 1e8 + 7;

int H[dd], IH[dd], dp[fd];

int main(){
	fac[0] = ifac[0] = 1;
	for(int i = 1; i < dd; i++){
		fac[i] = fac[i - 1] * i % mod2;
		ifac[i] = ifac[i - 1] * pows(i, mod2 - 2) % mod2;
	}
	int c, h, w;
	cin >> c >> w >> h;
	{
		H[0] = 1;
		for(int i = 1; i < dd; i++){
			H[i] = (H[i - 1] * h) % mod2;
		}
		IH[0] = 1;
		for(int i = 1; i < dd; i++){
			IH[i] = (IH[i - 1] * pows(h, mod2 - 2)) % mod2;
		}
	}
	dp[0] = 1;
	deque<ll> G;
	G.push_back(1);
	G.push_back(1);
	ll t = h + 1;
	for(int i = 1; i <= c; i++){
		if(0){
			ll p = 1;
			for(int j = i - 1, l = 0; j >= -1 && l <= w; j--, l++){
				ll t = 1;
				if(j >= 0){
					t = dp[j];
				}
				dp[i] = (dp[i] + t * p) % mod2;
				p = (p * h) % mod2;
			}
		}
		{
			G.push_back(t);
			t = (t * h + t) % mod2;
			if(G.size() > w + 1){
				t = ((t - G.front() * H[w + 1]) % mod2 + mod2) % mod2;
				G.pop_front();
			}
		}
	}
	cout << G.back();
}


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

typedef long long LL ;

const LL MOD = 1000003 ;

LL POW(LL a,LL n){
	LL re=1 ;
	while (n>0){
		if (n&1)re=re*a%MOD ;
		a=a*a%MOD ;
		n>>=1 ;
	}
	return 1 ;
}

LL clear (LL a){
	a%=MOD ;
	if (a<0)a+=MOD ;
	return a ;
}

LL C ,W ,H[300] ;
LL dp[100000030] ,S ;

int main(){
	cin >> C >> W >> H[1] ;

	H[0]=1 ,S=W+3 ;
	for (int i=2 ;i<=W+1 ;i++ ) H[i] = H[i-1]*H[1] %MOD ;

	dp[0]=1 ;
	for (int i=1 ;i<=W ;i++ ) dp[i] = dp[i-1]*(H[1]+1) %MOD ;

	dp[W+1]=0 ;
	for (int i=0 ;i<=W ;i++ ){
		dp[W+1] += dp[W-i]*H[i]%MOD ;
	}
	dp[W+1]%=MOD ;

	for (int i=W+2 ;i<=C ;i++ ){
		dp[i%S] = dp[(i-1)%S] + clear(dp[(i-1)%S]-dp[(i-W-2)%S]*H[W])*H[1]%MOD ;
		dp[i%S] = clear(dp[i%S]) ;
	}

	cout << dp[C%S] ;
}


#include <iostream>
#include <string>
#include <cstdio>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <stack>
#include <cctype>
#include <cstdlib>
#include <bitset>
#include <utility>
#include <cstring>

using namespace std;

const long double EPS = 1e-8;
const long double PI = 3.1415926535897932384626433832795;
const long double E = 2.7182818284;
const int INF = 1000000000;

typedef long long ll;
typedef long double ld;

#define forn(i,n) for (int i = 0; i < int(n); i++)
#define pb push_back
#define all(a) a.begin(), a.end()
#define mp make_pair;
#define X first
#define Y second

int main(void)
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n, m;
    cin >> n >> m;
    vector <int> colour(n + 1);
    vector <vector<int> > h(n + 1);
    for (int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        h[x].push_back(y);
        h[y].push_back(x);
    }
    for (int t = 0; t < n && w; t++) {
        int w = 0;
        for(int i = 1; i <= n; i++) {
            int z = 0;
            for(int j = 0; j < h[i].size(); j++) {
                if (colour[i] == colour[h[i][j]]) {
                    z++;
                    if (!w) w = 1;
                }
            }
            if (z > 1) {
                colour[i]^= 1;
            }
        }
    }
    for(int i = 1; i <= n; i++) {
        cout << colour[i];
    }
    return 0;
}


# include <bits/stdc++.h>
using namespace std;
# define fi cin
# define fo cout
# define x first
# define y second
# define ll long long
# define db long double
# define IOS ios_base :: sync_with_stdio(0)
# define halt(...) {fo << (__VA_ARGS__) << '\n';exit(0);}
# define CF
# define pp(n) cerr << #n << " = " << n << '\n'
# define ppp(v) for (auto it : v) cerr << it << ' ';cerr << '\n'
# define sc(x) scanf(%d,&x)
int p[1 << 20];
int was[1 << 20];
int ans[1 << 20];
int main(void)
{
    #ifdef CF
    freopen("input","r",stdin);
    #endif // CF
    srand(time(0));
    fo << fixed << setprecision(7);
    cerr << fixed << setprecision(7);
    int n,k;
    sc(n);sc(k);
    for (int i = 1;i <= n;++i) sc(p[i]);
    for (int i = 1;i <= n;++i)
    return 0;
}


#include "stdafx.h"
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <stdio.h>
#include <string>
#include <iomanip>
#include <utility>
#include <algorithm>
#include <queue>
#include <set>

typedef unsigned long long ll;
typedef long double ld;
const ll INF = 9999999999999;

#define forn(ii,kk,nn) for(ll ii = kk; ii < nn; ++ii)

using namespace std;

ll n, m;
vector<vector<ll> > g;
vector<ll> x;
map<ll,ll> ss;
vector<set<ll> > mm;

void brush(int v)
{
    ll col = 0;
    if(x[v] == 0)
        col = 1;
    x[v] = col;

    forn(i,0,g[v].size())
    {
        if(x[g[v][i]] != col)
        {
            ll t = g[v][i];
            mm[ss[t]].erase(mm[ss[t]].find(t));
            ss[t]--;
            if(ss[t]>=0)
                mm[ss[t]].insert(t);
            mm[ss[v]].erase(mm[ss[v]].find(v));
            ss[v]--;
            if(ss[v]>=0)
                mm[ss[v]].insert(v);
        }
        else
        {
            ll t = g[v][i];
            mm[ss[t]].erase(mm[ss[t]].find(t));
            ss[t]++;
            if(ss[t]>=0 && ss[v]<=3)
                mm[ss[t]].insert(t);
            mm[ss[v]].erase(mm[ss[v]].find(v));
            ss[v]++;
            if(ss[v]>=0 && ss[v]<=3);
                mm[ss[v]].insert(v);
        }
    }
}

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

    cin >> n >> m;
    x.resize(n+1,0);
    g.resize(n+1,vector<ll>());
    forn(i,0,m)
    {
        int u,v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
        ss[u]++;
        ss[v]++;
    }
    mm.resize(4,set<ll>());
    for(map<ll,ll>::iterator i = ss.begin(); i!=ss.end();++i)
        mm[i->second].insert(i->first);
    while(1)
    {
        ll v;
        if(mm[3].size() != 0)
            v = *(mm[3].begin());
        else if(mm[2].size() != 0)
            v = *(mm[2].begin());
        else
            break;
        brush(v);
    }
    forn(i,1,n+1) cout << x[i];

return 0;
}


#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>
#include <set>

using namespace std;

// 1 <= k, n <= 10^5
// 1 <= h <= 10^4
// 1 <= m_i <= 10^9
// 1 <= v_i <= 10^9

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

  vector<pair<int, int>> l(n+1);
  for (int i = 1 ; i <= n ; ++i) {
    cin >> l[i].first;
  }
  for (int i = 1 ; i <= n ; ++i) {
    cin >> l[i].second;
  }

  vector<int> order(n+1);
  for (int i = 1 ; i <= n ; ++i) order[i] = i;
  sort(order.begin()+1, order.end(), [&](auto& lhs, auto& rhs) {
    int w_diff = l[lhs].first - l[rhs].first;
    if (w_diff != 0) return (w_diff < 0);
    return (l[lhs].second - l[rhs].second) < 0;
  });

  /*
  for (int i = 1 ; i <= n ; ++i) cout << order[i] << " ";
  cout << endl;
  for (int i = 1 ; i <= n ; ++i) {
    cout << l[order[i]].first << " " << l[order[i]].second << endl;
  }
  cout << endl;
  //*/

  vector<vector<double>> dp_s(n+1, vector<double>(k+1, 1e9));
  vector<vector<int>> dp_k(n+1, vector<int>(k+1, 0));
  vector<vector<int>> dp_p(n+1, vector<int>(k+1, 0));

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

  for (int i = 1 ; i <= n ; ++i) {
    const auto& x = l[order[i]];
    for (int j = min(i, k) ; j >= 1 ; --j) {
      //cout << "i = " << i << ", j = " << j << endl;
      int height = j*h;
      double j_time = (double) height / x.second;
      double new_time = max(dp_s[i-1][j-1], j_time);
      if (new_time < dp_s[i-1][j]) {
        dp_s[i][j] = new_time;
        dp_k[i][j] = order[i];
        dp_p[i][j] = i-1;
      }
      else {
        dp_s[i][j] = dp_s[i-1][j];
        dp_k[i][j] = dp_k[i-1][j];
        dp_p[i][j] = dp_p[i-1][j];
      }
    }

    /*
    cout << "[" << i << "] = " << x.second << endl;
    for (int j = 1 ; j <= min(i, k) ; ++j) {
      cout << dp_s[i][j] << " " << dp_k[i][j] << " " << dp_p[i][j] << endl;
    }
    */
  }

  //cout << "I am here" << endl;

  vector<int> ans(k+1);
  for (int i = k, j = n ; i >= 1 ; --i) {
    ans[i] = dp_k[j][i];
    j = dp_p[j][i];
  }

  for (int i = 1 ; i <= k ; ++i) {
    if (i >= 2) cout << " ";
    cout << ans[i];
  }

  cout << endl;

  return 0;
}


#include <cstdlib>
#include <queue>
#include <cstdio>
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
using namespace std;

typedef long long ll;

vector< pair<pair<int,int>,int> > homs;

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

    int n,k,h;
    scanf("%d%d%d",&n,&k,&h);
    homs.resize(n);
    for(int i=0;i<n;++i)
    {
        scanf("%d",&homs[i].first.first);
    }
    for(int i=0;i<n;++i)
    {
        scanf("%d",&homs[i].first.second);
        homs[i].second = i+1;
    }
    sort(homs.begin(),homs.end());
    long double t,l = 0, r = 2e9,m;
    int st,iter = 0;
    vector<int> ans(k+1);
    while(abs(r-l)>1e-18 && iter<78)
    {
        m = (l+r)/2;
        st = 1;
        for(int i=0;i<n && st<=k;++i)
        {
            t = ((long double)st*h)/((long double)homs[i].first.second);
            if(t<=m)
            {

                temp[st] = homs[i].second;
                ++st;
            }
        }
        if(st<=k) l = m;
        else r = m;
        ++iter;
    }
    for(int i=1;i<=k;++i) printf("%d ",ans[i]);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100009
#define EPS 10e-10
using namespace std;
int n,m,h;
int w[N],v[N],od[N];
int ans[N];
bool cmp(const int &a,const int &b){
    if(w[a]==w[b]) return v[a]<v[b];
    return w[a]<w[b];
}
bool isok(double t){
    int cnt=1;
    long double one=1.0;
    for(int i=1;i<=n&&cnt<=m;i++)
        if(one*t*v[od[i]]>one*cnt*h+EPS){
            ans[cnt++]=od[i];
        }
    if(cnt>m) return true;
    else return false;
}
int main(){
    //freopen("in","r",stdin);
    scanf("%d%d%d",&n,&m,&h);
    for(int i=1;i<=n;i++)
        od[i]=i,scanf("%d",w+i);
    for(int i=1;i<=n;i++)
        scanf("%d",v+i);
    sort(od+1,od+n+1,cmp);
    long double s=0,e=10e20,mid;
    for(int i=1;i<=300;i++){
        mid=(s+e)/2;
        if(isok(mid)) res=mid,e=mid;
        else s=mid;
    }
    //printf("%lf\n",res);
    printf("%d",ans[1]);
    for(int i=2;i<=m;i++) printf(" %d",ans[i]);
    puts("");
    return 0;
}


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

int k;
int n;
int m[111111];
int v[111111];
int id[111111];
int h;
int ans[111111];
int a[111111];

void sort(int l, int r)
{
	int i = l, j = r, x = m[rand() % (r-l+1) + l];
	do
	{
		while(m[i] < x) i++;
		while(m[j] > x) j--;
		if (i <= j)
		{
			swap(m[i], m[j]);
			swap(v[i], v[j]);
			swap(id[i], id[j]);
			i++; j--;
		}
	}
	while(i <= j);
	if (i < r) sort(i, r);
	if (j > l) sort(l, j);
}

bool isok(double t)
{
	int len = 0;
	for (int i=1; i<=n; i++)
	{
		if ((double)h * (len+1) / v[i] <= t)
		{
			a[++len] = id[i];
		}
		if (len >= k) break;
	}
	if (len >= k)
	{
		for (int i=1; i<=k; i++) ans[i] = a[i];
		return true;
	}
	else return false;
}

int main(void)
{
	scanf("%d%d%d", &n, &k, &h);
	for (int i=1; i<=n; i++) scanf("%d", &m[i]);
	for (int i=1; i<=n; i++) scanf("%d", &v[i]);
	for (int i=1; i<=n; i++) id[i] = i;

	sort(1, n);
	double left = 0, right = 10000, mid;
	for (int cnt=1; cnt<=100; cnt++)
	{
		mid = (left + right) / 2;
		if (isok(mid)) right = mid;
		else left = mid;
	}

	printf("%d", ans[1]);
	for (int i=2; i<=k; i++) printf(" %d", ans[i]);
	printf("\n");

	return 0;
}


// #includes {{{
#include <algorithm>
#include <numeric>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include <cstring>
#include <cmath>
#include <complex>
#include <functional>
using namespace std;
// }}}
// pre-written code {{{
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define foreach(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(), (c).end()
#define MP make_pair

typedef long long Int;
typedef long long ll;
typedef long double ld;

typedef pair<int,int> pii;
// }}}

int x[3], m[3], t[3];

struct State{
	char a[3], move[3], above[3], below[3];
};

set<State> st;

bool operator<(const State &s, const State &t){
	rep(i,3)if(s.a[i]<t.a[i])return true;else if(s.a[i]>t.a[i])return false;
	rep(i,3)if(s.move[i]<t.move[i])return true;else if(s.move[i]>t.move[i])return false;
	rep(i,3)if(s.above[i]<t.above[i])return true;else if(s.above[i]>t.above[i])return false;
	rep(i,3)if(s.below[i]<t.below[i])return true;else if(s.below[i]>t.below[i])return false;
	return false;
}

int maxi=-1;

// not grab: above=-1
// end throw: above=3

void dfs(const State &s){
	if(st.find(s)!=st.end())return;
	st.insert(s);
	bool terminate=true;
	rep(i,3){
		if(s.move[i]==0)terminate=false;
		if(s.above[i]!=3)terminate=false;
	}
	maxi=max(maxi,(int)s.a[0]);
	maxi=max(maxi,(int)s.a[1]);
	maxi=max(maxi,(int)s.a[2]);

	if(terminate)return;
	rep(i,3){//who move
		if(s.move[i]==1 or s.above[i]!=-1 or s.below[i]!=-1)continue;
		//movable
		int d=s.a[i];
		for(int d2=d-m[i];d2<=d+m[i];d2++){//where to move
			if(d2<=0)continue;
			bool valid=true;
			rep(j,3){
				if(i==j)continue;
				if(s.a[j]==d2)valid=false;
			}
			if(not valid)continue;
			State s2=s; s2.a[i]=d2;s2.move[i]=1;
			dfs(s2);
		}
	}
	rep(i,3){//who grab
		if(s.above[i]!=-1 or s.below[i]!=-1)continue;
		int d=s.a[i];
		rep(j,3){//who lift
			if(i==j)continue;
			if(abs(s.a[i]-s.a[j])!=1 or s.below[j]!=-1)continue;
			//i grubs j
			State s2=s;
			s2.above[i]=j;s2.below[j]=i;
			s2.a[j]=s2.a[i];
			dfs(s2);
		}
	}
	rep(i,3){//who throw
		if(s.above[i]==-1 or s.above[i]==3 or s.below[i]!=-1)continue;
		int d=s.a[i];
		int j=s.above[i];
		for(int d2=d-t[i];d2<=d+t[i];d2++){
			if(d2<=0)continue;
			bool valid=true;
			rep(j2,3){
				if(s.a[j2]==d2)valid=false;
			}
			if(not valid)continue;
			State s2=s;
			s2.above[i]=3;
			s2.below[j]=-1;s2.a[j]=d2;
			if(s2.above[j]!=-1 and s2.above[j]!=3){
				int k=s2.above[j];s2.a[k]=d2;
			}
			dfs(s2);
		}
	}
}

int main() {
	cin>>x[0]>>m[0]>>t[0]>>x[1]>>m[1]>>t[1]>>x[2]>>m[2]>>t[2];
	State s={x[0],x[1],x[2],0,0,0,-1,-1,-1, -1,-1,-1};
	dfs(s);
	cout<<maxi<<endl;
	return 0;
}


// #includes {{{
#include <algorithm>
#include <numeric>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include <cstring>
#include <cmath>
#include <complex>
#include <functional>
using namespace std;
// }}}
// pre-written code {{{
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define foreach(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define all(c) (c).begin(), (c).end()
#define MP make_pair

typedef long long Int;
typedef long long ll;
typedef long double ld;

typedef pair<int,int> pii;
// }}}

#include<unordered_set>

int x[3], m[3], t[3];

struct State{
	char a[3], move[3], above[3], below[3];
};

State checkPoint0={4,3,4,0,1,0,-1,-1,0,2,-1,-1};
State checkPoint1={3,3,3,0,1,0,-1,2,0,2,-1,1};
State checkPoint2={6,3,6,0,1,0,-1,3,0,2,-1,-1};
State checkPoint3={7,3,6,0,1,0,-1,3,3,-1,-1,-1};
State checkPoint4={7,3,7,0,1,0,2,3,3,-1,-1,0};
State checkPoint5={7,3,11,0,1,0,3,3,3,-1,-1,-1};
State checkPoint6={7,3,12,0,1,1,3,3,3,-1,-1,-1};

const int p=98019;

namespace std{
	template<>
		struct hash<State>{
			size_t operator()(const State& s)const{
				unsigned int v=0;
				rep(i,3)v+=s.a[i],v*=p;
				rep(i,3)v+=s.move[i],v*=p;
				rep(i,3)v+=s.above[i],v*=p;
				rep(i,3)v+=s.below[i],v*=p;
				return v;
			}
		};
};

bool operator<(const State &s, const State &t){
	rep(i,3)if(s.a[i]<t.a[i])return true;else if(s.a[i]>t.a[i])return false;
	rep(i,3)if(s.move[i]<t.move[i])return true;else if(s.move[i]>t.move[i])return false;
	rep(i,3)if(s.above[i]<t.above[i])return true;else if(s.above[i]>t.above[i])return false;
	rep(i,3)if(s.below[i]<t.below[i])return true;else if(s.below[i]>t.below[i])return false;
	return false;
}

bool operator==(const State &s,const State &t){
	return (not ((s<t) or (t<s)));
}

unordered_set<State> st;
int maxi=-1;

// not grab: above=-1
// end throw: above=3

bool opt;

void dfs(const State &s){
	if(st.find(s)!=st.end())return;
	st.insert(s);
	//      if(s==checkPoint5)opt=true,cout<<"nya"<<endl;
	//      else opt=false;
	bool terminate=true;
	rep(i,3){
		if(s.move[i]==0)terminate=false;
		if(s.above[i]!=3)terminate=false;
	}
	maxi=max(maxi,(int)s.a[0]);
	maxi=max(maxi,(int)s.a[1]);
	maxi=max(maxi,(int)s.a[2]);
	if(terminate)return;
	rep(i,3){//who move
		if(s.move[i]==1 or (s.above[i]!=-1 and s.above[i]!=3) or s.below[i]!=-1)continue;
		//movable
		int d=s.a[i];
		for(int d2=d-m[i];d2<=d+m[i];d2++){//where to move
			if(d2<=0)continue;
			bool valid=true;
			rep(j,3)if(s.a[j]==d2)valid=false;
			if(not valid)continue;
			State s2=s; s2.a[i]=d2;s2.move[i]=1;
			dfs(s2);
		}
	}
	rep(i,3){//who grab
		if(s.above[i]!=-1 or s.below[i]!=-1)continue;
		int d=s.a[i];
		rep(j,3){//who lift
			if(i==j)continue;
			if(abs(s.a[i]-s.a[j])!=1 or s.below[j]!=-1)continue;
			//i grubs j
			State s2=s;
			s2.above[i]=j;s2.below[j]=i;
			s2.a[j]=s2.a[i];
			if(opt){
				cout<<i<<" "<<j<<endl;
				cout<<(int)s2.above[j]<<endl;
			}
			if(s2.above[j]!=-1 and s2.above[j]!=3){
				int k=s2.above[j];s2.a[k]=s2.a[i];
			}
			dfs(s2);
		}
	}
	rep(i,3){//who throw
		if(s.above[i]==-1 or s.above[i]==3 or s.below[i]!=-1)continue;
		int d=s.a[i], j=s.above[i];
		for(int d2=d-t[i];d2<=d+t[i];d2++){
			if(d2<=0)continue;
			bool valid=true;
			rep(j2,3)if(s.a[j2]==d2)valid=false;
			if(not valid)continue;
			State s2=s;
			s2.above[i]=3;
			s2.below[j]=-1;s2.a[j]=d2;
			if(s2.above[j]!=-1 and s2.above[j]!=3){
				int k=s2.above[j];s2.a[k]=d2;
			}
			dfs(s2);
		}
	}
}

int main() {
	cin>>x[0]>>m[0]>>t[0]>>x[1]>>m[1]>>t[1]>>x[2]>>m[2]>>t[2];
	State s={x[0],x[1],x[2],0,0,0,-1,-1,-1, -1,-1,-1};
	dfs(s);
	cout<<maxi<<endl;
	return 0;
}


//by yjz
#include<bits/stdc++.h>
using namespace std;
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
#define bged(v) (v).begin(),(v).end()
#define foreach(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
typedef long long ll;
const int Imx=2147483647;
const ll Lbig=2e18;
const int mod=998244353;
//My i/o stream
struct fastio
{
	char s[100000];
	int it,len;
	fastio(){it=len=0;}
	inline char get()
	{
		if(it<len)return s[it++];it=0;
		len=fread(s,1,100000,stdin);
		if(len==0)return EOF;else return s[it++];
	}
	bool notend()
	{
		char c=get();
		while(c==' '||c=='\n')c=get();
		if(it>0)it--;
		return c!=EOF;
	}
}_buff;
#define geti(x) x=getnum()
#define getii(x,y) geti(x),geti(y)
#define getiii(x,y,z) getii(x,y),geti(z)
#define puti(x) putnum(x),putchar(' ')
#define putii(x,y) puti(x),puti(y)
#define putiii(x,y,z) putii(x,y),puti(z)
#define putsi(x) putnum(x),putchar('\n')
#define putsii(x,y) puti(x),putsi(y)
#define putsiii(x,y,z) putii(x,y),putsi(z)
inline ll getnum()
{
	ll r=0;bool ng=0;char c;c=_buff.get();
	while(c!='-'&&(c<'0'||c>'9'))c=_buff.get();
	if(c=='-')ng=1,c=_buff.get();
	while(c>='0'&&c<='9')r=r*10+c-'0',c=_buff.get();
	return ng?-r:r;
}
template<class T> inline void putnum(T x)
{
	if(x<0)putchar('-'),x=-x;
	register short a[20]={},sz=0;
	while(x)a[sz++]=x%10,x/=10;
	if(sz==0)putchar('0');
	for(int i=sz-1;i>=0;i--)putchar('0'+a[i]);
}
inline char getreal(){char c=_buff.get();while(c==' '||c=='\n')c=_buff.get();return c;}
int n,mu[50111];
int g[50111];
int g0[50111],g1[50111],g2[50111];
int ans0[50111],ans1[50111];

unsigned long long f0[50111],f1[50111],f2[50111];
#define func(j) 	f0[i]=(f0[i]+1ll*f0[j]*g0[i-j]+1ll*f1[j]*g1[i-j])%mod,f1[i]=(f1[i]+1ll*f0[j]*g1[i-j]+1ll*f1[j]*g2[i-j])%mod,f2[i]=(f2[i]+1ll*f2[j]*g2[i-j]+1ll*f1[j]*g1[i-j])%mod;
int main()
{
	unsigned long long bound=14e18;
	cin>>n;
//	n=50000;
	g[0]=g[2]=1;
	for(int i=4;i<=n;i+=2)g[i]=(g[i-2]+g[i-4])%mod;
	for(int i=1;i<=n;i++)
	{
		g0[i+1]=1ll*g[i]*i*i%mod;
		g1[i+1]=1ll*g[i-1]*i*i%mod;
		if(i>=2)g2[i+1]=1ll*g[i-2]*i*i%mod;
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j+2<=i;j+=2)
		{
			/*
			f0[i]=(f0[i]+1ll*f0[j]*g0[i-j]+1ll*f1[j]*g1[i-j])%mod;
			f1[i]=(f1[i]+1ll*f0[j]*g1[i-j]+1ll*f1[j]*g2[i-j])%mod;
			f2[i]=(f2[i]+1ll*f2[j]*g2[i-j]+1ll*f1[j]*g1[i-j])%mod;
			*/
			f0[i]+=f0[j]*g0[i-j]+f1[j]*g1[i-j];
			f1[i]+=f0[j]*g1[i-j]+f1[j]*g2[i-j];
			f2[i]+=f2[j]*g2[i-j]+f1[j]*g1[i-j];
			f0[i]+=f0[j+1]*g0[i-j-1]+f1[j+1]*g1[i-j-1];
			f1[i]+=f0[j+1]*g1[i-j-1]+f1[j+1]*g2[i-j-1];
			f2[i]+=f2[j+1]*g2[i-j-1]+f1[j+1]*g1[i-j-1];
			f0[i]=f0[i]>=bound?f0[i]%mod:f0[i];
			f1[i]=f1[i]>=bound?f1[i]%mod:f1[i];
			f2[i]=f2[i]>=bound?f2[i]%mod:f2[i];
		}
		if(i%2==1)
		{
			int j=i-1;
			f0[i]+=f0[j]*g0[i-j]+f1[j]*g1[i-j];
			f1[i]+=f0[j]*g1[i-j]+f1[j]*g2[i-j];
			f2[i]+=f2[j]*g2[i-j]+f1[j]*g1[i-j];
		}
		f0[i]=(f0[i]+g0[i])%mod;
		f1[i]=(f1[i]+g1[i])%mod;
		f2[i]=(f2[i]+g2[i])%mod;
	}
/*	for(int i=0;i<=n;i++)cerr<<g[i]<<" ";cerr<<endl;
	for(int i=0;i<=n;i++)cerr<<g0[i]<<" ";cerr<<endl;
	for(int i=0;i<=n;i++)cerr<<g1[i]<<" ";cerr<<endl;
	for(int i=0;i<=n;i++)cerr<<g2[i]<<" ";cerr<<endl;cerr<<endl;
	for(int i=0;i<=n;i++)cerr<<f0[i]<<" ";cerr<<endl;
	for(int i=0;i<=n;i++)cerr<<f1[i]<<" ";cerr<<endl;
	for(int i=0;i<=n;i++)cerr<<f2[i]<<" ";cerr<<endl;*/
	ll ans=0;
	ans=1ll*(g[n-1]+g[n-3])*n%mod*(n-1)%mod*(n-1)%mod;
	for(int i=2;i<n;i++)
	{
		int l=i,r=n-i;
		ll curans=0;
		curans+=1ll*g[l-1]*f0[r]%mod;
		curans+=1ll*g[l-2]*f1[r]%mod;
		curans+=1ll*g[l-2]*f1[r]%mod;
		if(l>=3)curans+=1ll*g[l-3]*f2[r]%mod;
		curans=curans%mod*(i-1)%mod*(i-1)%mod;
		ans+=1ll*curans*i%mod;
	}
	cout<<ans%mod<<endl;
	return 0;
}


#include<bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
typedef int ll;
typedef long long int li;
const ll MAXN=2e5+51,MOD=998244353;
struct Matrix{
	ll num[16][16];
	Matrix()
	{
		memset(num,0,sizeof(num));
	}
	inline ll* operator [](const ll &x)
	{
		return num[x];
	}
	inline const ll* operator [](const ll &x)const
	{
		return num[x];
	}
};
ll qcnt;
li n;
Matrix mat,x,res;
ll g[16]={
	0,4,8,998244352,16,998244343,4,998244341,
	998244305,26,998244309,15,998244337,998244349,998244349,998244352
};
ll f[16]={
	0,0,0,24,4,240,204,1316,2988,
	6720,26200,50248,174280,436904,1140888,3436404
};
inline li read()
{
    register li num=0;
    register char ch=getchar();
    while(!isdigit(ch))
    {
        ch=getchar();
    }
    while(isdigit(ch))
    {
        num=(num<<3)+(num<<1)+(ch-'0');
        ch=getchar();
    }
    return num;
}
inline Matrix operator *(Matrix x,Matrix y)
{
	Matrix res;
	for(register int i=0;i<16;i++)
	{
		for(register int j=0;j<16;j++)
		{
			for(register int k=0;k<16;k++)
			{
				res[i][j]=(res[i][j]+(li)x[i][k]*y[k][j]%MOD)%MOD;
			}
		}
	}
	return res;
}
inline Matrix qpow(Matrix base,li exponent)
{
	Matrix res;
	for(register int i=0;i<16;i++)
	{
		res[i][i]=1;
	}
	while(exponent)
	{
		if(exponent&1)
		{
			res=res*base;
		}
		base=base*base,exponent>>=1;
	}
	return res;
}
int main()
{
	qcnt=read();
	for(register int i=0;i<16;i++)
	{
		mat[i][0]=g[i],x[0][i]=f[15-i];
	}
	for(register int i=1;i<16;i++)
	{
		mat[i-1][i]=1;
	}
	for(register int i=0;i<qcnt;i++)
	{
		n=read();
		printf("%d\n",n<=15?f[n]:(x*qpow(mat,n-15))[0][0]);
	}
}


#include<bits/stdc++.h>
clock_t t=clock();
namespace my_std{
	using namespace std;
	#define pii pair<int,int>
	#define fir first
	#define sec second
	#define MP make_pair
	#define rep(i,x,y) for (int i=(x);i<=(y);i++)
	#define drep(i,x,y) for (int i=(x);i>=(y);i--)
	#define go(x) for (int i=head[x];i;i=edge[i].nxt)
	#define templ template<typename T>
	#define sz 5050
	#define mod 998244353ll
	typedef long long ll;
	typedef double db;
	mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
	templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}
	templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
	templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
	templ inline void read(T& t)
	{
		t=0;char f=0,ch=getchar();double d=0.1;
		while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
		while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
		if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
		t=(f?-t:t);
	}
	template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}
	char __sr[1<<21],__z[20];int __C=-1,__zz=0;
	inline void Ot(){fwrite(__sr,1,__C+1,stdout),__C=-1;}
	inline void print(register int x)
	{
		if(__C>1<<20)Ot();if(x<0)__sr[++__C]='-',x=-x;
		while(__z[++__zz]=x%10+48,x/=10);
		while(__sr[++__C]=__z[__zz],--__zz);__sr[++__C]='\n';
	}
	void file()
	{
		#ifdef NTFOrz
		freopen("a.in","r",stdin);
		#endif
	}
	inline void chktime()
	{
		#ifdef NTFOrz
		cout<<(clock()-t)/1000.0<<'\n';
		#endif
	}
	#ifdef mod
	ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
	ll inv(ll x){return ksm(x,mod-2);}
	#else
	ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
	#endif
//	inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std;

int n;

ll dp[4][4],tmp[4][4];
ll g[sz],f[sz][2];
void DP()
{
	static int to[4];
	rep(i,1,n-1)
	{
		rep(k,0,3) rep(s,0,3)
		{
			ll w=tmp[k][s];
			if (!w) continue;

			rep(p,0,3) to[p]=0;
			if (s==0) to[1]++;
			else if (s==1) to[3]++;
			else if (s==2) to[0]++,to[3]++;
			else to[2]++;
			rep(t,0,3) if ((t&k)==k) rep(p,0,3) (dp[t][p]+=w*to[p]%mod)%=mod;

			if (k!=3) continue;
			if (!(s>>1&1)) continue;
			(dp[0][(s^2)<<1|1]+=w)%=mod; (f[i+1][s&1]+=w)%=mod;
		}
		rep(k,0,3) rep(s,0,3) tmp[k][s]=dp[k][s],dp[k][s]=0;
	}
	memcpy(dp,tmp,sizeof(dp));
	g[0]=g[2]=1;rep(i,2,n/2) g[i*2]=(g[(i-1)*2]+g[(i-2)*2])%mod;
}

int main()
{
	file();
	read(n);
	ll ans=0;
	memset(dp,0,sizeof(dp)),memset(tmp,0,sizeof(tmp)),memset(f,0,sizeof(f)),memset(g,0,sizeof(g));
	tmp[0][3]=1;
	DP();f[1][1]=1;
	rep(i,1,n-1)
	{
		ll w=0;
		(w+=f[n-i][1]*g[i]%mod)%=mod;
		(w+=f[n-i][0]*g[i-1]%mod)%=mod;
		(ans+=w*(i+1)%mod*i%mod*i%mod)%=mod;
	}
	memset(dp,0,sizeof(dp)),memset(tmp,0,sizeof(tmp)),memset(f,0,sizeof(f)),memset(g,0,sizeof(g));
	rep(i,0,3) dp[i][3]=1;
	DP();f[1][0]=1;
	rep(i,1,n-1)
	{
		ll w=0;
		(w+=f[n-i][1]*g[i-1]%mod)%=mod;
		(w+=f[n-i][0]*(i==1?0ll:g[i-2])%mod)%=mod;
		(ans+=w*(i+1)%mod*i%mod*i%mod)%=mod;
	}
	cout<<ans;
	return 0;
}


#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags
#pragma GCC option("arch=native","tune=native","no-zero-upper") //Enable AVX
#pragma GCC target("avx2") //Enable AVX

#include <bits/stdc++.h>

using namespace std;

#define db(x) cout << (x) << '\n';
#define all(x) x.begin(),x.end()
#define UNIQUE(x) sort(all(x)),x.resize(unique(all(x))-x.begin())
#define LB(v,x) lower_bound(all(v),x)
#define UB(v,x) upper_bound(all(v),x)
#define endl "\n"
#define F first
#define S second
#define PB push_back
#define PF push_front
#define int long long
#define double long double
#define trick int m = (l+r)>>1,lc=n<<1,rc=lc|1

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

int dy[]={0,1,1,1,0,-1,-1,-1};
int dx[]={-1,-1,0,1,1,1,0,-1};
const int maxn = 1e5;
const int OO = 1e18;
const int mod = 1e9 + 7;
const double PI = acos(-1);

void maxself(int &a,int b)
{a=max(a,b);}
void minself(int &a,int b)
{a=min(a,b);}

vector<vi> g(maxn);

int32_t main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

//	freopen("name.in","r",stdin);
//	freopen("name.out","w",stdout);

    int n;
    cin >> n;
    int a[n];
    vi ans;
    int sum = 0;
    int alic = 0;
    for(int i=0;i<n;i++)
    {
        cin >> a[i];
        if(i == 0 or a[i]<=a[0]/2)
        {
            alice+=a[i];
            ans.PB(i+1);
        }
        sum+=a[i];
    }
    if(alice*2>sum)
    {
        cout << v.size() << endl;
        for(auto x:v)
            cout << x << ' ';
    }
    else
        cout << 0;

	return 0;
}


using namespace std;
int main()
{
    int n;
    cin>>n;
    int arr[n+1];int tot=0;
    for(int i=1;i<=n;i++)
    {
        cin>>arr[i];
    }
    for(int i=1;i<=n;i++)
    {
     tot+=arr[i];
    }
    int myp=arr[1];
    for(int i=2;i<=n;i++)
    {
        if(arr[i]>myp/2)
        {
            arr[i]=0;
        }
    }
    int c=1;
    vector<int>ind;
    ind.push_back(1);
    if(myp>tot/2)
    {
        cout<<"1"<<endl;
        cout<<"1";
    }
    else
    {
    for(int i=2;i<=n;i++)
    {
        if(arr[i]!=0)
        {
            c++;
            ind.push_back(i);
        }
    }
    cout<<c<<endl;
    for(int i=0;i<ind.size();i++)
    {
        cout<<ind[i]<<" ";
    }
    }
    cout<<endl;
}


#include <iostream>
#include <vector>

using namespace std;

int main() {
    int N; cin >> N;
    vector<int> A(N); for (int&a:A) cin >> a;
    vector<int> P{1};
    int rest = 0, cur = A[0];
    for (int i = 1; i < N; ++i) {
        if (A[i] <= A[0]/2) {
            cur += A[i];
            P.push_back(i+1);
        } else {
            rest += A[i];
        }
    }

    if (cur > rest) {
        cout << P.size() << endl;
        for (int i = 0; i < P.size(); ++i) cout << P[i] << " \n"[i==P.size()-1];
    } else {
        cout << 0 << endl;
    }


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

   int main()

   {
   int n,sum=0,i=0;
   vector<int> v;
   cin>>n;

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

     }

     int alice=a[0];

     if (2*alice<=sum)
     {

         for(i=1;i<n;i++)
           {
              if(alice>=2*a[i])
                 v.push_back(i+1);

              else
              continue;
           }

           if(v.size()==0)
              cout<<"0";

           else{
      cout<<v.size()+1<<endl;
      cout<<"1 ";

      for(auto it=v.begin();it!=v.end();++it)
          cout<<*it<<" "<<endl;
           }

         }

       else
       cout<<"1"<<endl<<"1":

    return 0;
   }


#include <bits/stdc++.h>

using namespace std;

const int N = 2050;
const int MOD = 1e9 + 7;

int f[N][N][2];

int sol(int x, int y, int z)
{
    if(f[x][y][z]+1)
        return f[x][y][z];
    if(x+y == 1)
        return f[x][y][z] = z;
    if((x|y) == 0)
        return f[x][y][z] = 0;
    if(l > r)
        return f[x][y][z] = 0;
    int tmp = 0;
    if(x)
        tmp += sol(x-1, y, !z);
    if(y)
        tmp += sol(x, y-1, !z);
    return f[x][y][z] = tmp % MOD;
}

int main()
{
    int n;
    scanf("%d", &n);
    return 0*printf("%d\n", sol(n, n, 0));
}


#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define int long long
#define pb push_back
#define sz(i) (int)(i.size())
#define F first
#define S second
#define L long double
#define P pair<int, int>
const int inf = 0x3f3f3f3f3f3f3f3LL;
const int mod = (int)1e9 + 7;
using namespace std;
#if LOCAL
#include "../../tr.h"
#else
#define tr(...)
#define endl '\n'
#endif
template<class T> void smin(T& a, T val) {if (a > val) a = val;}template<class T> void smax(T& a, T val) {if (a < val) a = val;}
const int N = (int)5e3 + 10;

int n, a[N];

int power(int a, int b, int m = mod){if(b < 0)b += m-1; int r = 1; while(b){if(b&1)r=(r*a)%m; a = (a*a)%m; b>>=1;}return r;}

int fact[N+10], ifact[N+10];
void pre(int N){
   fact[0] = 1;
   for (int i = 1; i <= N; ++i)
   {
      fact[i] = (i*fact[i-1])%mod;
   }
   ifact[N] = power(fact[N], -1);
   for (int i = N-1; i >= 0; --i)
   {
      ifact[i] = ((i+1)*ifact[i+1])%mod;
   }
}

int nCr(int n, int r){
   if(n < r)return 0;
   return ((fact[n]*ifact[r])%mod * ifact[n-r])%mod;
}

int data[N];

int32_t main(){_

   cin>>n;

   if (n <= 4){
      int x = power(3, n-1);
      cout<<x;
      return 0;
   }
   pre(N+5);
   int tmp = n;

   for (int i = 3; i <= 1500; ++i)
   {
      int ans = 0;
      n = i;
      for (int j = 0; j <= n-3; ++j)
      {
         int sign = 1;
         if(j % 2)sign *= -1;
         int x = power(2, j);
         x *= sign;
         x %= mod;
         x += mod;
         x %= mod;

         int tmp = x;
         tmp *= (j+1);
         tmp %= mod;

         int tt = nCr(2*n-1, n-3-j);
         tmp *= tt;
         tmp %= mod;
         ans += tmp;
         ans %= mod;
      }

      ans *= 2;
      ans %= mod;

      ans = ans * power(n-1, -1);
      ans %= mod;
      data[i-2] = ans;
   }

   n = tmp;
   int ans = 0;
   for (int i = 1; i <= n; ++i)
   {
      ans += data[i];
      ans %= mod;
   }

   cout<<ans;

   return 0;
}

// Common sense: do smth instead of nothing
// Even a single second counts...
// If other's can do it, even I can do it. It wouldn't be that tough. Let me give it a try.
// Mistakes made in past
// -Ques. ask for max-min => think binary_search, DP, greedy
// -not reading question properly / skipping to read & understand all samples properly & wasting time in wrong direction.
// -Using wrong indexing in nested loops something like a[i], when it is supposed to be a[j](WA)
// -value of const int N is not correct (WA/RTE).
// -Did you memset dp to -1 ?
// -check for small edges cases and work on small test cases manually if WA
// -not deallocating memory to pointers can lead to MLE. Use delete :))
// -not reading problem statements of harder problems E,..., thinking it couldn't be solved, but it isn't the case.
// -Avoid using #define int long long as much as possible when it's dp problem and requires high memory say, O(N^2, N=5000) (MLE)
// -Check Bitwise Operator Precedence
// -Use pragmas to optimize (by constant factor) when necessary..
// -Think before coding & starting to debug. Have patience to look inside code and actually see how it works


#include <bits/stdc++.h>

using namespace std;

#define MAXN 1010

int n;
int dp[MAXN][MAXN][2];

int solve(int x, int y, int op) {
    int &ans = dp[x][y][op];

    if (ans!=-1) {
        return ans;
    }

    if (x+y==2*n) {
        ans = 0;
    }
    else {
        if (x==y) {
            if (op==0) {
                ans = solve(x+1, y, 1)+1;
            }
            else {
                ans = solve(x+1, y, 0);
            }
        }
        else {
            if (x==n) {
                if (op==0) {
                    ans = solve(x, y+1, 1)+1;
                }
                else {
                    ans = solve(x, y+1, 0);
                }
            }
            else {
                if (op==0) {
                    if (x<y) {
                        ans = solve(x+1, y, 1)+1+solve(x, y+1, 0);
                    }
                    else {
                        ans = solve(x, y+1, 0)+solve(x+1, y, 1)+1;
                    }
                }
                else {
                    ans = solve(x+1, y, 0)+solve(x, y+1, 0);
                }
            }
        }
        ans %= 1000000007;
    }
    return ans;
}

int main() {
    scanf("%d", &n);
    memset(dp, -1, sizeof(dp));
    printf("%d\n", max(solve2(0, 0, 0), solve2(0, 0, 1)));
    return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
#define maxn 10005
#define mod 1000000007;
#define ll long long
ll c[maxn][maxn];
ll dp[maxn][maxn],n;
int main() {
    memset(dp,0,sizeof dp);
	rep(i,1,1000)	dp[i][0]=i/2+i%2;
	rep(i,1,1000)	dp[i][0]=i/2+i%2;
	rep(i,1,1000) {
		rep(j,1,i) {
			ll t=(dp[i-1][j]+1)%mod;
			if(j-1>=0&&i-1>=0)
				t=(t+dp[i-1][j-1])%mod;
			if(j-2>=0)
				t=(t+dp[i][j-2])%mod;
			dp[i][j]=t;
		}
	}
	cin>>n;
	cout<<dp[n][n]<<endl;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define all(x) (x).begin(),(x).end()

const int MAXN = 25;
const int MAXW = 25 * 50000;
int p[MAXN][MAXW];

signed main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cout.precision(20);

	int n;
	cin >> n;
	vector<int> a(2 * n);
	for (auto &i : a)
		cin >> i;

	sort(a.beign(), a.end());
	for (int i = 0; i < MAXN; i++)
		for (int j = 0; j < MAXW; j++)
			p[i][j] = -1;

	p[0][0] = 0;
	int sum = 0;

	for (int i = 2; i < 2 * n; i++) {
		int cur = a[i];
		sum += cur;
		for (int j = n - 1; j > 0; j--) {
			for (int k = cur; k < MAXW; k++) {
				if (p[j - 1][k - cur] != -1 && p[j][k] == -1) {
					p[j][k] = i;
				}
			}
 		}
	}

	int best = 10 * MAXW;
	for (int i = 0; i < MAXW; i++) {
		if (abs(best - (abs(sum - best))) > abs(i - abs(sum - i)) && p[n - 1][i]) {
			best = i;
		}
	}

	vector<int> type(2 * n);
	type[1] = 1;
	for (int i = n - 1; i > 0; i--) {
		type[p[i][best]] = 1;
		best -= a[p[i][best]];
	}
	vector<int> ans1, ans2;
	for (int i = 0; i < 2 * n; i++) {
		if (type[i] == 0)
			ans1.push_back(a[i]);
		else
			ans2.push_back(a[i]);
	}
	sort(all(ans1));
	sort(all(ans2));
	reverse(all(ans2));
	for (auto i : ans1)
		cout << i << " ";
	cout << endl;
	for (auto i : ans2)
		cout << i << " ";
	cout << endl;
 	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define range(i, m, n) for(int i = m; i < n; i++)
#define husk(i, m, n) for(int i = m; i > n; i--)

int n;

int main() {
  cin >> n;
  vector<int> a;
  range(i, 0, n * 2) {
    int c;
    cin >> c;
    a.push_back(c);
  }
  sort(a.begin(), a.end(), greater<int>());
  int sum = 0;
  int total = 0;
  range(i, 0, n - 1) sum += a[i];
  range(i, 0, 2 * n) total += a[i];
  reverse(a.begin(), a.end());
  vector<vector<long long>> dp(n, vector<long long>(sum + 1));
  dp[0][0] = 1;
  auto add = [&] (int v) {
    husk(i, n - 1, 0) {
      range(x, v, sum + 1) {
        dp[i][x] += dp[i - 1][x - v];
      }
    }
  };
  range(i, 2, 2 * n) add(a[i]);
  int res = INT_MAX;
  int u = 0, v = 1;
  int c = total - a[0] - a[1];
  range(i, 0, c / 2 + 1) if(dp[n - 1][i] > 0) res = i;
  res = c - s;
  swap(a.back(), a[v]);
  vector<vector<int>> was(n, vector<int>(sum + 1, -1));
  was[0][0] = 1;
  range(i, 1, 2 * n - 1) {
    husk(x, n - 1, 0) {
      range(y, a[i], sum + 1) {
        if(was[x][y] == -1 && was[x - 1][y - a[i]] >= 0) was[x][y] = i;
      }
    }
  }
  vector<int> cur;
  int su = n - 1, sv = res;
  vector<int> alive(2 * n);
  while(su) {
    cur.push_back(was[su][sv]);
    su--;
    sv -= a[cur.back()];
    alive[cur.back()] = 1;
  }
  vector<int> ft;
  range(i, 1, 2 * n - 1) if(alive[i] == 0) ft.push_back(i);
  sort(ft.begin(), ft.end(), [&](int x, int y){ return a[x] < a[y]; });
  sort(cur.begin(), cur.end(), [&](int x, int y){ return a[x] > a[y]; });
  cout << a[0] << " ";
  for(auto i : ft) cout << a[i] << " ";
  cout << endl;
  for(auto i : cur) cout << a[i] << " ";
  cout << a[2 * n - 1];
  return 0;
}


#include "bits/stdc++.h"
#include<vector>
#include<iostream>
#include<queue>
#include<algorithm>
#include<map>
#include<set>
#include<iomanip>
#include<assert.h>
#include<unordered_map>
#include<unordered_set>
#include<string>
#include<stack>
#include<complex>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

#define WHATS(var)..cout<<__LINE__<<' '<<#var<<"="<<var<<endl;

template<class S, class T> ostream& operator <<(ostream &os, const pair<S, T> v){
	os << "( " << v.first << ", " << v.second << ")"; return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<T> &v){
	for(int i = 0; i < v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<vector<T>> &v){
	for(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<set<T>> &v){
	for(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const set<T> &v){
	int i=0;
	for(auto it:v){
		if(i > 0){os << ' ';}
		os << it;
		i++;
	}
	return os;
}
using ll =unsigned long long ;
using MyBitset = vector<ll>;
void add(const MyBitset& a,int offset,MyBitset&b){
	const int mo=offset&63;
	WHATS(a.size())
	WHATS(b.size())
	assert(a.size()==b.size());
	assert(offset>=0);
	const int len=offset>>6;
	for(int l=0;l<a.size()-len;++l){
		b[l+len]|=a[l]<<mo;
		if(mo&&l!=a.size()-len-1)b[l+len+1]|=a[l]>>(64-mo);
	}

}

int main() {
	ios::sync_with_stdio(false);
	int N;cin>>N;
	N*=2;
	vector<int>v(N);
	for(int i=0;i<N;++i)cin>>v[i];
	sort(v.begin(),v.end());
	ll answer=v[0]+v[1];
	int mm=v[0],mmm=v[1];
	v=vector<int>(v.begin()+2,v.end());
	vector<vector<vector<ll>>>memo(N-1,vector<vector<ll>>(N/2,vector<ll>(30000)));
	memo[0][0][0]=1;

	int sum=accumulate(v.begin(),v.end(),0);
	for(int i=0;i<N-2;++i){
		WHATS(i)
		vector<vector<ll>> nmemo=vector<vector<ll>>(N/2,vector<ll>(30000));
		for(int j=0;j<N/2;++j){
			int x=v[i];
			 if(j!=N/2-1){
			add(memo[i][j],x,nmemo[j+1]);

			// 	int mo=x&63;
			// 	int pl=x>>6;
			// 	for(int k=0;k<30000;++k){
			// 		if(k+pl<30000)nmemo[j+1][k+pl]|=memo[i][j][k]<<mo;
			// 		if(k+pl+1<30000&&mo)nmemo[j+1][k+pl+1]|=memo[i][j][k]>>(64-mo);
			// 	}
			 }
			for(int k=0;k<30000;++k){
				nmemo[j][k]|=memo[i][j][k];
			}

		}
			memo[i+1]=nmemo;
	}
	int ad=1e9;
	for(int i=0;i<30000;++i){
		for(int j=0;j<64;++j){
			int x=(i<<6)+j;
			if(memo[N-2][N/2-1][i]&(1ull<<j)){
				ad=min(ad,max(x,sum-x));
			}
		}
	}
	answer+=ad;
	//cout<<answer<<endl;
	vector<int>up_ids,do_ids;
	int now_sum=ad;
	int now_rest=N/2-1;
	WHATS(memo[2][2][0]);

	for(int i=N-2;i>=0;--i){
		if(!i)break;
		int now_mo=now_sum&63;
		int now_pl=now_sum>>6;
		WHATS(i)
		WHATS(now_mo)
		WHATS(now_pl)
		WHATS(now_rest)
		WHATS(memo[i-1][now_rest][now_pl])
		assert(memo[i][now_rest][now_pl]&(1ull<<now_mo));

		if(memo[i-1][now_rest][now_pl]&(1ull<<now_mo)){
			do_ids.push_back(i-1);
		}else{
			up_ids.push_back(i-1);
			now_sum-=v[i-1];
			now_rest--;
		}
	}
	cout<<mm;
	sort(up_ids.begin(),up_ids.end());
	for(int i=0;i<N/2-1;++i){
		cout<<' '<<v[up_ids[i]];
	}
	cout<<endl;
	for(int i=0;i<N/2-1;++i){
		cout<<v[do_ids[i]]<<' ';
	}

	cout<<mmm;
	cout<<endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;

cout<<max(1,4*n*sqrt(2));
}


#include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define sqr(x) ((x)*(x))
#define mp make_pair
#define EPS 1e-9
#define ones(x) __builtin_popcount(x)
#define fast_io() ios_base::sync_with_stdio(0);cin.tie(0);
#define debug(x) cerr << #x << ": " << x << endl;
#define fst first
#define snd second
#define itm1 fst.fst
#define itm2 fst.snd
#define itm3 snd
#define mt(a,b,c) mp(mp(a,b),c)

typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long long ll;
typedef pair<ll,ll> ill;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ii, int> tri;
typedef pair<int,bool> ib;
typedef unsigned int uint;

const double PI = acos(-1);
const double E = 2.718281828459;
const int  INF = 2e9;
const ll LINF = 2e18;
const ll MOD = 1e9 + 7;
const int MAXN = 1e5 + 1;
const int MN = 1e6 + 2;

ll get(ll i, ll n){
	ll st = ll(sqrt(sqr(n) - sqr(i)));
	--st;
	st = max(st, 0LL);
	while(st * st + i * i <= n * n) ++st;
	return max(st - 1, 0LL);
}

int main(){
	ll n; cin >> n;
	ll ans = 0;
	ll last;
	if(n == 0){
		ans = 1;
	}
	else{
		last = n;
		for(int i = 1 ; i < n ; ++i){
			ll ret = get(ll(i),ll(n));
			ans += 1 + max(0, last - ret - 1);
			last = ret;
		}
		ans += last;
		ans *= 4;
	}

	cout << ans << endl;
	return 0;

}


#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <bitset>
#include <sstream>

using namespace std;

typedef long long ll;

mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());

int main()
{
#ifdef ONPC
    freopen("a.in", "r", stdin);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    int was = 0;
    //for (ll n = 1; n <= 30; n++)
    ll n;
    cin >> n;
    {
        ll j = 0;
        vector <ll> ans(n + 1);
        for (ll i = n; i >= 0; i--)
        {
            while (i * i + (j + 1) * (j + 1) <= n * n)
            {
                j++;
            }
            ans[i] = j;
        }
        ll ret = 0;
        for (ll i = 0; i <= n; i++)
        {
            ll delta = 0;
            if (ans[i] != 0) delta = 2;
            else delta = 1;
            if (i != 0) delta *= 2;
            ret += delta;
            if (ans[ans[i]] != i)
            {
                ll x = ans[i], y = i;
                ll delta = 0;
                if (y != 0) delta = 2;
                else delta = 1;
                if (x != 0) delta *= 2;
                ret += delta;
            }
            /*
            if (ans[ans[i]] != i)
            {
                ret += 4;
            }
            else
            {
                ret += 2;
            }
            */
        }
        /*
            q.insert({i, j});
            q.insert({i,-j});
            q.insert({-i,j});
            q.insert({-i,-j});
            swap(i, j);
            q.insert({i, j});
            q.insert({i,-j});
            q.insert({-i,j});
            q.insert({-i,-j});
            swap(i, j);
            //(i, j)
            //(i,-j)
            //(-i,j)
            //(i,-j)
        }
        cout << q.size() - was << ' ' << n << '\n';
        was = q.size();
        */
        cout << ret << '\n';
    }
}


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

int ans;

int main(){
	int n, y, z;
	scanf("%d", &n);
	if(n == 0){
		cout << 1 << endl;
		return;
	}
	int m = sqrt(0.5 * n * n);
	if(m * m + (m + 1) * (m + 1) <= n * n) ans = 8 * m + 4;
	else ans = 8 * m;
	printf("%d", ans);
}


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

struct _flow{
#define oo 1e9
    int src, dst;
    int nedge;
    int point[500], fnet[500], next[500], cap[500], cost[500];
    int head[500], dist[500];
    void clear(){
        memset(head, -1, sizeof(head));
        nedge=result=0;
    }
    inline void addedge(int u, int v, int c, int w){
        point[nedge]=v; fnet[nedge]=0; cap[nedge]=c; cost[nedge]=w; next[nedge]=head[u];
        head[u]=nedge++;
        point[nedge]=u; fnet[nedge]=0; cap[nedge]=0; cost[nedge]=-w; next[nedge]=head[v];
        head[v]=nedge++;
    }
    int np, in[500], pre[500], fc[500];
    int result;
    void spfa(){
        fill(dist, dist+np, 1e8);
        dist[src]=0;
        fc[src]=1e8;
        list<int> Q;
        Q.push_back(src);
        memset(in, 0, sizeof(in));
        while(!Q.empty()){
            int u=Q.front(); Q.pop_front();
            in[u]=0;
            for(int i=head[u]; i>=0; i=next[i])
                if(fnet[i]<cap[i] && dist[point[i]]>dist[u]+cost[i]){
                    dist[point[i]]=dist[u]+cost[i];
                    pre[point[i]]=i;
                    fc[point[i]]=min(fc[u], cap[i]-fnet[i]);
                    if(!in[point[i]]){
                        in[point[i]]=1;
                        if(!Q.empty() && dist[point[i]]<=dist[Q.front()])
                            Q.push_front(point[i]);
                        else Q.push_back(point[i]);
                    }
                }
        }
    }
    int maxflow(){
        memset(dist, 0, sizeof(dist));
        result=0;
        do{
            spfa();
            if(dist[dst]<1e8){
                int f=fc[dst];
                for(int i=dst; i!=src; i=point[pre[i]^1]){
                    result+=f*cost[pre[i]];
                    fnet[pre[i]]+=f;
                    fnet[pre[i]^1]-=f;
                }
            }
        }while(dist[dst]<1e8);
        return result;
    }
}flow;

char pill[11][3]={
    "","BY", "BW", "BR", "BB", "RY", "RW", "RR", "WY", "WW", "YY"
};
char mp[10][10];
struct A{
    int a[10][10];
};
A  sub, type;
int mm=0;
pair<int, int> next(int x, int y){
    if(y==7)
        return make_pair(x+1, 0);
    return make_pair(x, y+1);
}
int code(char a, char b){
    if(a>b) swap(a, b);
    if(a=='B'){
        if(b=='Y') return 1;
        if(b=='W') return 2;
        if(b=='R') return 3;
        return 4;
    }
    if(a=='R'){
        if(b=='Y') return 5;
        if(b=='W') return 6;
        if(b=='R') return 7;
    }
    if(a=='W'){
        if(b=='Y') return 8;
        return 9;
    }
    return 10;
}
char dir[2][2]={{1, 0}, {0, 1}};
int num[15];
int mis[11][11];
int bn[15];
char ans[20][20];
int mat[20][20];
map<long long, int> hash;
const int cs=1e9+7;
void solve(){
    long long id=0, b=1;for(int i=1; i<=10; i++, b*=cs) id+=bn[i]*b;
    if(hash.find(id)!=hash.end()) return;
    hash[id]=1;
    fill(flow.fnet, flow.fnet+flow.nedge, 0);
    int tmp=0;
    for(int i=1; i<=10; i++)
    {
        int j=min(bn[i], num[i]);
        //if(!j) continue;
        flow.fnet[(i-1)*2]+=j;
        flow.fnet[(i-1)*2+1]-=j;
        flow.fnet[40+((i-1)*10+(i-1))*2]+=j;
        flow.fnet[40+((i-1)*10+(i-1))*2+1]-=j;
        flow.fnet[(i-1)*2+20]+=j;
        flow.fnet[(i-1)*2+21]-=j;
        tmp+=j+j; if(bn[i]>j) tmp+=bn[i]-j;
    }
    if(tmp<=mm) return;
    int result=56-flow.maxflow();
    if(result>mm){
        mm=result;
        //cout<<mm<<endl;
        for(int i=1; i<=10; i++) for(int j=1; j<=10; j++)
            mat[i][j]=flow.fnet[40+((i-1)*10+(j-1))*2];
        memset(ans, '.', sizeof(ans));
        for(int i=0; i<7; i++) for(int j=0; j<8; j++) if(sub.a[i][j]==0 || sub.a[i][j]==2){
            int t=sub.a[i][j];
            if(t==2) ans[i*2+1][j*2+2]='-';
            else ans[i*2+2][j*2+1]='|';
            for(int k=1; k<=10; k++) if(mat[k][type.a[i][j]]){
                mat[k][type.a[i][j]]--;
                if((pill[k][0]!=mp[i][j]+pill[k][1]!=mp[i+dir[t/2][0]][j+dir[t/2][1]]) < (pill[k][1]!=mp[i][j]+pill[k][0]!=mp[i+dir[t/2][0]][j+dir[t/2][1]])){
                    ans[i+i+1][j+j+1]=pill[k][0], ans[(i+dir[t/2][0])*2+1][(j+dir[t/2][1])*2+1]=pill[k][1];
                }else{
                    ans[i+i+1][j+j+1]=pill[k][1], ans[(i+dir[t/2][0])*2+1][(j+dir[t/2][1])*2+1]=pill[k][0];
                }
                break;
            }
        }
        //for(int i=1; i<=13; i++){
        //  for(int j=1; j<=15; j++)
        //      printf("%c", ans[i][j]);
        //  printf("\n");
        //}
    }
}
int step=0;
void dfs(int x, int y){
    if(x==7){
        //step++; cout<<step<<"\n";
        solve();
        return;
    }
    if(sub.a[x][y]!=-1){
        dfs(next(x, y).first, next(x, y).second);
    }else{
        if(y+1<8 && sub.a[x][y+1]==-1){
            sub.a[x][y+1]=3;
            sub.a[x][y]=2;
            type.a[x][y+1]=type.a[x][y]=code(mp[x][y], mp[x][y+1]);
            bn[type.a[x][y+1]]++;
            flow.cap[code(mp[x][y], mp[x][y+1])*2+18]++;
            dfs(next(x, y).first, next(x, y).second);
            flow.cap[code(mp[x][y], mp[x][y+1])*2+18]--;
            bn[type.a[x][y+1]]--;
            sub.a[x][y]=-1;
            sub.a[x][y+1]=-1;
        }
        if(x+1<7 && sub.a[x+1][y]==-1){
            sub.a[x][y]=0;
            sub.a[x+1][y]=1;
            type.a[x][y]=type.a[x+1][y]=code(mp[x][y], mp[x+1][y]);
            bn[type.a[x][y]]++;
            flow.cap[code(mp[x][y], mp[x+1][y])*2+18]++;
            dfs(next(x, y).first, next(x, y).second);
            flow.cap[code(mp[x][y], mp[x+1][y])*2+18]--;
            bn[type.a[x][y]]--;
            sub.a[x][y]=-1;
            sub.a[x+1][y]=-1;
        }
    }
}
int main(){
    for(int i=0; i<7; i++)
        scanf(" %s", mp[i]);
    memset(sub.a, -1, sizeof(sub.a));
    for(int i=1; i<=10; i++) cin>>num[i];
    for(int i=1; i<=10; i++) for(int j=1; j<=10; j++){
        mis[i][j]=min((pill[i][0]!=pill[j][0])+(pill[i][1]!=pill[j][1]), (pill[i][0]!=pill[j][1])+(pill[i][1]!=pill[j][0]));
    }
    flow.clear(); flow.np=22; flow.src=0; flow.dst=21;
    for(int i=1; i<=10; i++) flow.addedge(0, i, num[i], 0);
    for(int i=1; i<=10; i++) flow.addedge(i+10, 21, 0, 0);
    for(int i=1; i<=10; i++) for(int j=1; j<=10; j++)
        flow.addedge(i, j+10, 1e8, mis[i][j]);
    dfs(0, 0);
    printf("%d\n", mm);
    for(int i=1; i<=13; i++){
        for(int j=1; j<=15; j++)
            printf("%c", ans[i][j]);
        printf("\n");
    }
    return 0;
}


// Test
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <set>
#define N 100
#define M 1000
#define int65 unsigned long long
#define For(i,x,y) for (i=x;i<=y;i++)
using namespace std;
const char P[4]={'B','R','W','Y'};
struct ww {
	int x,y;
};
struct w {
	ww a,b;
	int c,d,e;
} c[N],q[20][20][50];
int i,j,k,n,m,an,S,T,t,num,ge;
int a[N][N],h[200],f[N][N],b[N][N],len[N][N],id[N][N],st[N][N],d[N][N],
	ba[M],bb[M],head[M],next[M],F[M],g[M],gg[M];
char p[N],s[N][N],re[N][N];
set<int65> Set;
inline void add(int x,int y,int z) {
	ba[++t]=y,bb[t]=z,next[t]=head[x],head[x]=t;
	ba[++t]=x,bb[t]=0,next[t]=head[y],head[y]=t;
}
inline bool bfs() {
	int l,r,v;
	g[l=r=1]=S; gg[S]=0; F[S]=++num;
	for (;l<=r;l++) for (v=head[g[l]];v;v=next[v]) {
		int A=ba[v];
		if (bb[v]&&F[A]!=num) {
			F[A]=num; gg[A]=gg[g[l]]+1; g[++r]=A;
			if (A==T) return 1;
		}
	}
	return 0;
}
int Dfs(int x,int y) {
	if (x==T) return y;
	int v,w=0,re;
	for (v=head[x];v&&w<y;v=next[v]) {
		int A=ba[v];
		if (bb[v]&&gg[A]==gg[x]+1&&F[A]==num&&(re=Dfs(A,min(bb[v],y-w))))
			bb[v]-=re,bb[v^1]+=re,w+=re;
	}
	if (!w) gg[x]=0;
	return w;
}
inline void Print(ww A,int B) {
	re[2*A.x-1][2*A.y-1]=P[B];
}
inline void print(w A,int x,int y) {
	if (A.e) swap(A.a,A.b),swap(A.c,A.d);
	if (x!=A.c&&y!=A.d) swap(x,y);
	Print(A.a,x); Print(A.b,y);
}
inline void work() {
	int i,j,u,r;
	For(i,0,3)For(j,i,3) len[i][j]=0;
	For(i,1,ge) {
		w A=c[i];
		q[A.c][A.d][++len[A.c][A.d]]=A;
	}
	int65 hash=0;
	For(i,0,3)For(j,i,3) hash=hash*97+len[i][j];
	if (Set.count(hash)) return;
	Set.insert(hash);
	S=21,T=22; For(i,1,T) head[i]=0; t=1;
	int sum=0;
	For(i,0,3)For(j,i,3) {
		st[i][j]=min(len[i][j],b[i][j]);
		d[i][j]=b[i][j]-st[i][j];
		sum+=st[i][j]*2;
		if (len[i][j]>b[i][j]) add(S,id[i][j],len[i][j]-b[i][j]);
		if (len[i][j]<b[i][j]) add(id[i][j]+10,T,b[i][j]-len[i][j]);
		For(u,0,3)For(r,u,3) {
			if (i==u&&j==r) continue;
			int A=i==u||i==r||j==u||j==r;
			if (A) add(id[i][j],id[u][r]+10,M);
		}
	}
	for (;bfs();sum+=Dfs(S,M));
	if (sum>an) {
		an=sum;
		//print
		memcpy(re,s,sizeof(re));
		For(i,0,3)For(j,0,3) {
			For(k,1,st[i][j]) {
				w B=q[i][j][k];
				print(B,i,j);
			}
			int B=id[i][j],v;
			for (v=head[B];v;v=next[v]) {
				int C=ba[v];
				if (C<=10||C>20||bb[v]==M) continue;
				For(u,0,3)For(r,u,3) if (id[u][r]+10==C) goto yy1;
				yy1:;
				int o;
				For(o,bb[v]+1,M) print(q[i][j][++st[i][j]],u,r),d[u][r]--;
			}
		}
		For(i,0,3)For(j,i,3)
		for (;st[i][j]<len[i][j];) {
			int A=++st[i][j];
			For(u,0,3)For(r,u,3) if (d[u][r]) goto yy2;
			yy2:;
			print(q[i][j][A],u,r);
			d[u][r]--;
		}
	}
}
inline void get(int A,int B,ww C,ww D) {
	int a=0;
	if (A>B) swap(A,B),swap(C,D),a=1;
	c[++ge]=(w){C,D,A,B,a};
}
void dfs(int x,int y) {
	if (x>7) {
		work();
		return;
	}
	if (y>8) {
		dfs(x+1,1);
		return;
	}
	if (f[x][y]) {
		dfs(x,y+1);
		return;
	}
	if (x<7) {
		f[x][y]=f[x+1][y]=1;
		get(a[x][y],a[x+1][y],(ww){x,y},(ww){x+1,y});
		s[2*x][2*y-1]='|';
		dfs(x,y+1);
		s[2*x][2*y-1]='.';
		ge--;
		f[x][y]=f[x+1][y]=0;
	}
	if (y<8&&!f[x][y+1]) {
		f[x][y]=f[x][y+1]=1;
		get(a[x][y],a[x][y+1],(ww){x,y},(ww){x,y+1});
		s[2*x-1][2*y]='-';
		dfs(x,y+1);
		s[2*x-1][2*y]='.';
		ge--;
		f[x][y]=f[x][y+1]=0;
	}
}
int main() {
	h['B']=0,h['R']=1,h['W']=2,h['Y']=3;
	For(i,1,7) {
		scanf("%s",p+1);
		For(j,1,8) a[i][j]=h[p[j]];
	}
	For(i,0,3)for (j=3;j>=i;j--) scanf("%d",&b[i][j]);
	k=0; For(i,0,3)For(j,i,3) id[i][j]=++k;
	an=-1; memset(s,'.',sizeof(s));
	dfs(1,1);
	printf("%d\n",an);
	For(i,1,13) {
		For(j,1,15) printf("%c",re[i][j]);
		printf("\n");
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>

using namespace std;

struct kraw
{
    int a, cap;
};

vector< vector<int> > podzial(7, vector<int>(8, -1));
vector<int> ilNaObr(10, 0), ilMam(10, 0);
int nr = -1, mam = 0;
string tab[] = {"BB", "BR", "BW", "BY", "RR", "RW", "RY", "WW", "WY", "YY"};
vector<string> P(7);
vector< vector<kraw> > G(22);
const int INF = 100;
int odp = 56;
vector< vector<int> > odpPodzial;
vector<string> odpP1, odpP2;
vector<string> odpP(13);
const int base = 1000000007;
set<int> H;

int Szukaj(string a)
{
    if (a[0] > a[1])
        swap(a[0], a[1]);
    for (int i = 0; i < 10; ++i)
        if (tab[i] == a)
            return i;
}

int R(string a, string b)
{
    return min((a[0] != b[0]) + (a[1] != b[1]), (a[0] != b[1]) + (a[1] != b[0]));
}

void Licz()
{
    for (int i = 1; i <= 10; ++i)
    {
        G[0][i - 1].cap = ilNaObr[i - 1];
        G[i][10].cap = 0;
    }
    for (int i = 11; i <= 20; ++i)
    {
        G[i][0].cap = ilMam[i - 11];
        G[21][i - 11].cap = 0;
    }
    for (int i = 1; i <= 10; ++i)
    {
        for (int j = 11; j <= 20; ++j)
        {
            G[i][j - 11].cap = INF;
            G[j][i].cap = 0;
        }
    }
    vector<string> p1, p2;
    for (int i = 1; i <= 10; ++i)
    {
        for (int j = 11; j <= 20; ++j)
        {
            if (R(tab[i - 1], tab[j - 11]) == 0)
            {
                int m = min(ilNaObr[i - 1], ilMam[i - 1]);
                G[0][i - 1].cap -= m;
                G[j][0].cap -= m;
                for (int k = 0; k < m; ++k)
                {
                    p1.push_back(tab[i - 1]);
                    p2.push_back(tab[i - 1]);
                }
            }
            if (R(tab[i - 1], tab[j - 11]) == 2)
            {
                G[i][j - 11].cap = 0;
                G[j][i].cap = 0;
            }
        }
    }
    int flow = 0;
    while (true)
    {
        vector<int> pop(22, -1);
        queue<int> Q;
        Q.push(0);
        pop[0] = 0;
        while (!Q.empty())
        {
            int akt = Q.front();
            Q.pop();
            for (int i = 0; i < G[akt].size(); ++i)
            {
                if (pop[G[akt][i].a] == -1 && G[akt][i].cap != 0)
                {
                    pop[G[akt][i].a] = akt;
                    Q.push(G[akt][i].a);
                }
            }
        }
        if (pop[21] == -1)
            break;
        vector<int> path;
        int akt = 21;
        while (akt != 0)
        {
            path.push_back(akt);
            akt = pop[akt];
        }
        path.push_back(0);
        reverse(path.begin(), path.end());
        int c = INF;
        for (int i = 0; i < (int) path.size() - 1; ++i)
            for (int j = 0; j < G[path[i]].size(); ++j)
                if (G[path[i]][j].a == path[i + 1])
                    c = min(c, G[path[i]][j].cap);
        for (int i = 0; i < (int) path.size() - 1; ++i)
        {
            for (int j = 0; j < G[path[i]].size(); ++j)
                if (G[path[i]][j].a == path[i + 1])
                    G[path[i]][j].cap -= c;
            for (int j = 0; j < G[path[i + 1]].size(); ++j)
                if (G[path[i + 1]][j].a == path[i])
                    G[path[i + 1]][j].cap += c;
        }
        flow += c;
    }
    for (int i = 11; i <= 20; ++i)
    {
        for (int j = 1; j <= 10; ++j)
        {
            int il = G[i][j].cap;
            for (int k = 0; k < il; ++k)
            {
                p1.push_back(tab[j - 1]);
                p2.push_back(tab[i - 11]);
            }
        }
    }
    int nowy = 2 * (28 - (int) p1.size()) + flow;
    if (nowy < odp)
    {
        odp = nowy;
        odpPodzial = podzial;
        odpP1 = p1;
        odpP2 = p2;
    }
}

void StworzG()
{
    for (int i = 1; i <= 10; ++i)
        G[0].push_back({i, 0});
    for (int i = 1; i <= 10; ++i)
    {
        for (int j = 11; j <= 20; ++j)
            G[i].push_back({j, 0});
        G[i].push_back({0, 0});
    }
    for (int i = 11; i <= 20; ++i)
    {
        G[i].push_back({21, 0});
        for (int j = 1; j <= 10; ++j)
            G[i].push_back({j, 0});
    }
    for (int i = 11; i <= 20; ++i)
        G[21].push_back({i, 0});
}

void rec(int x, int y, int kier)
{
    ++nr;
    podzial[x][y] = nr;
    if (kier == 0)
        podzial[x][y + 1] = nr;
    else
        podzial[x + 1][y] = nr;
    string para = "";
    para += P[x][y];
    if (kier == 0)
        para += P[x][y + 1];
    else
        para += P[x + 1][y];
    ++ilNaObr[Szukaj(para)];
    ++mam;
    if (mam == 28)
    {
        int h = 0;
        for (int i = 0; i < 10; ++i)
            h = h * base + ilNaObr[i];
        if (H.find(h) == H.end())
        {
            Licz();
            H.insert(h);
        }
    }
    int nx = -1, ny = -1;
    for (int i = x; i < 7; ++i)
    {
        for (int j = 0; j < 8; ++j)
        {
            if (podzial[i][j] == -1)
            {
                nx = i;
                ny = j;
                break;
            }
        }
        if (nx != -1)
            break;
    }
    if (nx != -1)
    {
        if (ny + 1 < 8 && podzial[nx][ny + 1] == -1)
            rec(nx, ny, 0);
        if (nx + 1 < 7 && podzial[nx + 1][ny] == -1)
            rec(nx, ny, 1);
    }
    podzial[x][y] = -1;
    if (kier == 0)
        podzial[x][y + 1] = -1;
    else
        podzial[x + 1][y] = -1;
    --ilNaObr[Szukaj(para)];
    --nr;
    --mam;
}

void Wypisz()
{
    multiset<string> wsz;
    for (int i = 0; i < 10; ++i)
        for (int j = 0; j < ilMam[i]; ++j)
            wsz.insert(tab[i]);
    cout << 56 - odp << endl;
    vector<bool> jest(odpP1.size(), true);
    for (int i = 0; i < 28; ++i)
    {
        int a = -1, b, c, d;
        for (int j = 0; j < 7; ++j)
        {
            for (int k = 0; k < 8; ++k)
            {
                if (odpPodzial[j][k] == i)
                {
                    if (a == -1)
                    {
                        a = j;
                        b = k;
                    }
                    else
                    {
                        c = j;
                        d = k;
                    }
                }
            }
        }
        string x = "";
        x += P[a][b];
        x += P[c][d];
        if (x[0] > x[1])
            swap(x[0], x[1]);
        int nr = -1;
        for (int j = 0; j < odpP1.size(); ++j)
        {
            if (odpP1[j] == x && jest[j])
            {
                nr = j;
                break;
            }
        }
        if (nr != -1)
        {
            if (R(x, odpP2[nr]) == 1)
            {
                if (P[a][b] == odpP2[nr][0])
                    P[c][d] = odpP2[nr][1];
                if (P[a][b] == odpP2[nr][1])
                    P[c][d] = odpP2[nr][0];
                if (P[c][d] == odpP2[nr][0])
                    P[a][b] = odpP2[nr][1];
                if (P[c][d] == odpP2[nr][1])
                    P[a][b] = odpP2[nr][0];
            }
            wsz.erase(wsz.find(odpP2[nr]));
            jest[nr] = false;
        }
        else
        {
            P[a][b] = 'Q';
            P[c][d] = 'Q';
        }
    }
    string t(15, '.');
    for (int i = 0; i < 13; ++i)
        odpP[i] = t;
    for (int i = 0; i < 7; ++i)
        for (int j = 0; j < 8; ++j)
            odpP[2 * i][2 * j] = P[i][j];
    for (int i = 0; i < 28; ++i)
    {
        int a = -1, b, c, d;
        for (int j = 0; j < 7; ++j)
        {
            for (int k = 0; k < 8; ++k)
            {
                if (odpPodzial[j][k] == i)
                {
                    if (a == -1)
                    {
                        a = j;
                        b = k;
                    }
                    else
                    {
                        c = j;
                        d = k;
                    }
                }
            }
        }
        if (odpP[2 * a][2 * b] == 'Q')
        {
            string x = *(wsz.begin());
            odpP[2 * a][2 * b] = x[0];
            odpP[2 * c][2 * d] = x[1];
            wsz.erase(wsz.find(x));
        }
        if (a == c)
            odpP[2 * a][2 * b + 1] = '-';
        else
            odpP[2 * a + 1][2 * b] = '|';
    }
    for (int i = 0; i < 13; ++i)
        cout << odpP[i] << endl;
}

int main()
{
    ios_base::sync_with_stdio(0);
    for (int i = 0; i < 7; ++i)
        cin >> P[i];
    cin >> ilMam[3] >> ilMam[2] >> ilMam[1] >> ilMam[0];
    cin >> ilMam[6] >> ilMam[5] >> ilMam[4];
    cin >> ilMam[8] >> ilMam[7];
    cin >> ilMam[9];
    StworzG();
    rec(0, 0, 0);
    rec(0, 0, 1);
    Wypisz();
    return 0;
}


#pragma warning(disable:4786)
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<ctime>
#include<assert.h>

#include<cmath>
#include<iostream>
#include<fstream>

#include<string>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<set>
#include<sstream>
#include<stack>

using namespace std;

#define MAX(a,b) ((a)>(b) ? (a) : (b))
#define min(a,b) ((a)<(b) ? (a) : (b))
#define EPS 1e-9
#define asdf exit(0);

#define MAX_V 350
vector<long> Edge[ MAX_V+7];
long Row,Col;// Row will b left side and col will b in rght side
long nEdge,nVertex;

bool Visit[ MAX_V+7];

//after matching spouse of 'i'  is M[i] ( i think  :S im not sure  )
long M[ MAX_V+7];

void ini()
{
    memset(M,0,sizeof(M));
    memset(Visit,0,sizeof(Visit));
    int i;
    for(i=0;i<(MAX_V+7);i++)
    {
        Edge[i].clear();
    }
}

bool Match( long u)
{
    if( Visit[u]) return false;
    Visit[u] =true;
    long i;
    for( i=0;i<Edge[u].size();i++)
    {
        long v =Edge[u][i];
        if( !M[v]){
            M[u] =v;
            M[v] =u;
            return true;
        }
        else if( Match( M[v]))
        {
            M[u] =v;
            M[v] =u;
            return true;
        }
    }
    return false;
}

int BMatch()
{
    memset( &M[1],0,sizeof(long)*nVertex);
    long i;
    for( i=1;i<=Row;i++)
    {
        if( M[i]) continue;
        memset( &Visit[1],0,sizeof(bool)*( Row+Col));
        Match( i);
    }
    int ret=0;
    int j=Row+1;
    for(i=1;i<=Row;i++)
    {
        if(M[i])
        {
            ret++;
            //sts[ co[i-1] ].push_back(ro[M[i]-Row-1]);
        }
        else
        {
       //     while(M[j]) j++;
         //   sts[ co[i-1] ].push_back(ro[j-Row-1]);
        }
    }
    return ret;
}

int mat[500][500];

int vis[10][10];
char grid[10][10];

int ans;

vector<int> taken(10);

vector <pair<int,int> >O1(28);
vector<int> O2(28);

vector< pair<int,int> > save1(28);
vector< int > save2(28);

int pt[155][155];

string cols[100];

string dic[12];
int dic_cnt[12];

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

map< vector<int>,int > mp;

vector<int> sts[10];

int glo=0;

void get()
{

    int i,sum=0,mn,j;
    if(mp[taken]) return;
    mp[taken]=1;

    vector<int> co,ro;

    for(i=0;i<10;i++)
    {
        mn=min(taken[i],dic_cnt[i]);

        //for(j=0;j<mn;j++) sts[i].push_back(i);
        for(j=0;j<dic_cnt[i]-mn;j++)
        {
            ro.push_back(i);
        }
        for(j=0;j<taken[i]-mn;j++)
        {
            co.push_back(i);
        }
        sum+=2*mn;
    }

    ini();

    Col=co.size();
    Row=ro.size();
    nVertex=Col+Row;

    if( (sum+min(Col,Row)) <= ans   ) return;

    for(i=0;i<Row;i++)
    for(j=0;j<Col;j++)
    {
        if(pt[ co[j] ][ ro[i] ])
        {
            Edge[i+1].push_back(j+1+Row);
            Edge[j+1+Row].push_back(i+1);

        }
    }

    sum+=BMatch();

    //if(glo==223)asdf
    if(sum<=ans) return;

    /*if(sum==40)
    {

        for(i=0;i<Row;i++) cout<<" "<<ro[i];
        cout<<endl;
        for(i=0;i<Col;i++) cout<<" "<<co[i];
        cout<<endl;

        for(i=1;i<=Row;i++) cout<<i<<" "<<M[i]-Row<<endl;
        cout<<endl;
    }*/

    ans=sum;
    save1=O1;
    save2=O2;

    for(i=0;i<10;i++) sts[i].clear();
    for(i=0;i<10;i++)
    {
        mn=min(taken[i],dic_cnt[i]);
        for(j=0;j<mn;j++) sts[i].push_back(i);
    }

    j=1;
    for(i=Row+1;i<=nVertex;i++)
    {
        if(M[i])
        {
            sts[ co[i-1-Row] ].push_back(ro[M[i]-1]);

            /*if(ans==40)
            {
                cout<<co[i-1-Row]<<" "<<ro[M[i]-1]<<endl;
            }*/
        }
        else
        {
            while(M[j]) j++;
            sts[ co[i-1-Row] ].push_back(ro[j-1]);
            j++;
        }
    }
    return;
}

void bttk(int x,int y,int cnt)
{
    int i,X,Y,j;
    if(x>=7)
    {
        get();
        return;
    }

    if(y>=8)
    {
        bttk(x+1,0,cnt);
        return;
    }

    if(vis[x][y])
    {
        bttk(x,y+1,cnt);
        return;
    }

    vis[x][y]=1;
    for(i=0;i<2;i++)
    {
        X=x+dx[i];
        Y=y+dy[i];
        if(x>=0 && X<7 && Y>=0 && Y<8 && !vis[X][Y])
        {
            vis[X][Y]=1;

            O1[cnt].first=x;
            O1[cnt].second=y;
            O2[cnt]=i;

            taken[mat[ grid[x][y] ][ grid[X][Y] ]]++;

            bttk(x,y+1,cnt+1);
            taken[mat[ grid[x][y] ][ grid[X][Y] ]]--;
            vis[X][Y]=0;
        }
    }
    vis[x][y]=0;
}

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

    int i,j,k,l;

    dic[0]="BY";
    dic[1]="BW";
    dic[2]="BR";
    dic[3]="BB";
    dic[4]="RY";
    dic[5]="RW";
    dic[6]="RR";
    dic[7]="WY";
    dic[8]="WW";
    dic[9]="YY";

    for(i=0;i<10;i++)
    {
        mat[ dic[i][0] ][ dic[i][1] ]=i;
        mat[ dic[i][1] ][ dic[i][0] ]=i;
    }

    for(i=0;i<10;i++)
    for(j=0;j<10;j++)
    {
        int ok=0;
        for(k=0;k<2;k++)
        for(l=0;l<2;l++)
        {
            if(dic[i][k]==dic[j][l]) ok=1;
        }
        pt[i][j]=ok;

       // cout<<i<<" "<<j<<" "<<pt[i][j]<<endl;
    }

    //asdf

    for(i=0;i<7;i++) scanf("%s",grid[i]);
    for(i=0;i<10;i++)  scanf("%d",&dic_cnt[i]);

    ans=0;
    bttk(0,0,0);

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

    char out[100][100];
    for(i=0;i<20;i++)
    for(j=0;j<20;j++)
    out[i][j]='.';

    int x,y,X,Y,val;

    int ca1=0,ca2=0,ca0=0;

    for(i=0;i<28;i++)
    {
       // cout<<i<<endl;

        x=save1[i].first;
        y=save1[i].second;
        X=x;
        Y=y;

        if(save2[i]==0)
        {
            Y=y+1;
            out[ 2*save1[i].first  ][ 2*save1[i].second+1 ]  ='-';
        }
        else
        {
            X=x+1;
            out[ 2*save1[i].first+1  ][ 2*save1[i].second ]  ='|';
        }

        val=mat[ grid[x][y] ][grid[X][Y]];

        j=sts[val].back();
        sts[val].pop_back();

        int c1=0,c2=0;

        if(grid[x][y]==dic[j][0]) c1++;
        if(grid[X][Y]==dic[j][1]) c1++;

        if(grid[x][y]==dic[j][1]) c2++;
        if(grid[X][Y]==dic[j][0]) c2++;

        if(c1>c2)
        {
            if(c1==1)ca1++;
            else if(c1==2) ca2++;
            else ca0++;

            grid[x][y]=dic[j][0];
            grid[X][Y]=dic[j][1];
        }
        else
        {
            if(c2==1)ca1++;
            else if(c2==2) ca2++;
            else ca0++;

            grid[x][y]=dic[j][1];
            grid[X][Y]=dic[j][0];
        }

    }

    //cout<<ca1<<" "<<ca2<<" "<<ca0<<endl;
    //cout<<"asdf";

    for(i=0;i<7;i++)
    for(j=0;j<8;j++)
    {
        out[2*i][2*j]=grid[i][j];
    }

    for(i=0;i<13;i++)
    {
        for(j=0;j<15;j++) printf("%c",out[i][j]);
        printf("\n");
    }

    return 0;
}


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

int INF = 1e9;
const int SIZE = 6;
int N, low[SIZE][SIZE], high[SIZE][SIZE], act[SIZE][SIZE];
int in[SIZE];
int bestCost = -1;

void go(int i, int f, int c);

void go2(int i, int j, int f, int c){

	if(j >= N){
		if(!f)
			go(i+1, in[i+1], c);
		return;
	}

	for(int a = low[i][j] ; a <= high[i][j] ; a++){
		if(a > f)break;
		in[j] += a;
		go2(i, j+1, f-a, c+a*a+(a?act[i][j]:0));
		in[j] -= a;
	}
}

void go(int i, int f, int c){

	if(i >= N-1){
		bestCost >?= c;
		return;
	}

	go2(i, i+1, f, c);
}

int main(){

	//freopen("1.in", "rt", stdin);

	cin >> N;
	for(int i = 0 ; i < N*(N-1)/2; i++){
		int a, b; cin >> a >> b; a--; b--;
		cin >> low[a][b] >> high[a][b] >> act[a][b];
	}

	int f;
	for(f = 0 ; f <= 30 ; f++){
		go(0, f, 0);
		if(bestCost != -1)
			break;
	}

	if(f <= 30)
		cout << f << " " << bestCost << endl;
	else
		cout << -1 << " " << -1 << endl;

	return 0;
}


//#pragma comment(linker, "/STACK:16777216")
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <sstream>
#include <queue>
#include <stack>
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define DOWN(i,a,b) for(int i = (a); i >= (b); i--)
#define FORV(i,a) for(typeof(a.begin()) i = a.begin(); i != a.end(); i++)
#define FR(i,a) for(int i = 0; i < (a); i++)
#define DR(i,a) for(int i = (a)-1; i >= 0; i--)
#define REP(i,a) for(int i = 0; i < (a); i++)
#define Rep(i,a) for(int i = 0; i < (a); i++)
#define For(i,a,b) for(int i = (a); i <= (b); i++)

#define sqr(x) ((x)*(x))
#define dout debug && cout
#define ll long long
#define sz size()
#define ull unsigned long long
#define pb push_back
#define oo 1000000009
/* DEBUGGING */
bool debug = false;
/* MAIN PROGRAM */

using namespace std;

/*************************TEMPLATE**********************************/
long long convertToNum(string s)
{
    long long val = 0; FR(i,s.size()) val = val * 10 + s[i] - '0';
    return val;
}
string convertToString(long long a) {
    string res = ""; if (!a) return "0";
    while (a) { res = (char)(a % 10 + 48) + res;  a /= 10; }
    return res;
}
long long GCD(long long x,long long y)  {
    if (!x) return y; if (!y) return x;
    if (x == y) return x; if (x < y) return GCD(x,y%x); else return GCD(x%y,y);
}
long long POW(long long x,long long y,long long Base){
    if (!y) return 1; long long u = POW(x,y/2,Base);
    u = (u * u) % Base;
    if (y & 1) return (u * x) % Base; else return u;
}

//newstate = (newstate-1) & oldstate
/**************************CODE HERE*****************************/
#define maxn 10
long long resx, resy;
int n;
int low[maxn][maxn],high[maxn][maxn],active[maxn][maxn];
vector< pair<int,int> > edge;
long long total = 0;
int flow[maxn][maxn];
int FlowIn[maxn],FlowOut[maxn];

void attempt(int vt) {
    if (vt == n * (n-1) / 2) {
        bool ok = true;
        FOR(i,2,n-1)
        if (FlowIn[i] != FlowOut[i]) ok = false;
        if (!ok) return;
        int F = 0;
        FOR(i,2,n) F += flow[1][i];
        if (F < resx || (F == resx && total > resy))
            resx = F, resy = total;
        return;
    }
    int u = edge[vt].first, v = edge[vt].second;
    if (u != 1) {
        int cost = 0;
        FOR(i,2,n) cost += flow[1][i];
        if (cost > resx) return;
    }
    FOR(i,2,u-1) {
        if (Flow[i] != FlowOut[i]) return;
    }
    FOR(delta,low[u][v],high[u][v]) {
        flow[u][v] = delta;
        total += (delta == 0 ? 0 : (delta * delta + active[u][v]));
        FlowIn[v] += delta;
        FlowOut[u] += delta;
        attempt(vt+1);
        FlowIn[v] -= delta;
        FlowOut[u] -= delta;
        total -= (delta == 0 ? 0 : (delta * delta + active[u][v]));
    }
}
int main() {
//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
    cin >> n;
    int u,v;
    FOR(i,1,n*(n-1)/2) {
        cin >> u >> v;
        cin >> low[u][v] >> high[u][v] >> active[u][v];
    }
    resx = oo;
    FOR(i,1,n)
    FOR(j,i+1,n) edge.push_back( make_pair(i,j) );
    attempt(0);
    if (resx == oo) cout << -1 << " " << -1 << endl;
    else cout << resx << " " << resy << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn = 15;
int l[maxn][maxn],h[maxn][maxn],a[maxn][maxn];
int flow[maxn],n,Max,Min;
void dfs(int x,int y,int cost){
    cout<<x<<" "<<y<<" "<<cost<<endl;
    if(x==n){
        Max = max(cost,Max);
        return;
    }
    if(y>n){
        if(flag[x]==0)dfs(x+1,x+2,cost);
        return;
    }
    for(int i=l[x][y];i<=h[x][y];i++){
        if(flow[x]<i)return;
        int ncost = cost;
        if(i!=0)ncost+=a[x][y]+i*i;
        flow[x]-=i;flow[y]+=i;
        dfs(x,y+1,ncost);
        flow[x]+=i;flow[y]-=i;
    }
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    for(int j=i+1;j<n;j++){
        int A,B;scanf("%d%d",&A,&B);
        scanf("%d%d%d",&l[A][B],&h[A][B],&a[A][B]);
    }
    Max = -1,Min = -1;
    for(Min=0;Min<=26;Min++){
        cout<<Min<<endl;
        flow[1]=Min;
        dfs(1,2,0);
        if(Max!=-1)break;
    }
    if(Max==-1)cout<<"-1 -1"<<endl;
    else cout<<Min<<" "<<Max<<endl;
}


#define _CRT_SECURE_NO_DEPRECATE
#pragma comment(linker, "/STACK:64000000")

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <utility>
#include <memory.h>
#include <cstring>

#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>

#include <iostream>
#include <sstream>

using namespace std;

typedef long long int64;

#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()

const int INF = (int)1E9;
const int64 INF64 = (int64)1E18;
const long double EPS = 1E-8;
const long double PI = 3.1415926535897932384626433832795;

struct edge {
    int u, l, r, a;

    int flow(int value) const {
        int ans = value*value;
        if (value > 0) ans += a;
        return ans;
    }

    edge(int u = -1, int l = -1, int r = -1, int a = -1) : u(u), l(l), r(r), a(a) {}
};

int n, f[10];
vector<edge> g[10];

int solve(int v, int i) {
    if (v == n - 1) return 0;

    if (i == (int)g[v].size())
        if (f[v] == 0) return solve(v + 1, 0);
        else return -INF;

    int ans = -INF;
    for(int value = g[v][i].l; value <= min(g[v][i].r, f[v]); value++) {
        int u = g[v][i].u;
        f[v] -= value;
        f[u] += value;

        int t = solve(v, i + 1) + g[v][i].flow(value);
        ans = max(ans, t);

        f[v] += value;
        f[u] -= value;
    }

    return ans;
}

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

    cin >> n;
    forn(i, n*(n-1)/2) {
        int v, u, l, r, a;
        cin >> v >> u >> l >> r >> a;

        g[v - 1].pb(edge(u - 1, l, r, a));
    }

    forn(i, 30) {
        memset(f, 0, sizeof 0);
        f[0] = i;
        int t = solve(0, 0);
        if (t >= 0) {
            cout << i << " " << t << endl;
            return 0;
        }
    }

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

    return 0;
}

#define _CRT_SECURE_NO_DEPRECATE
#pragma comment(linker, "/STACK:64000000")

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <utility>
#include <memory.h>
#include <cstring>

#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>

#include <iostream>
#include <sstream>

using namespace std;

typedef long long int64;

#define forn(i, n) for(int i = 0; i < (int)(n); i++)
#define ford(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()

const int INF = (int)1E9;
const int64 INF64 = (int64)1E18;
const long double EPS = 1E-8;
const long double PI = 3.1415926535897932384626433832795;

struct edge {
    int u, l, r, a;

    int flow(int value) const {
        int ans = value*value;
        if (value > 0) ans += a;
        return ans;
    }

    edge(int u = -1, int l = -1, int r = -1, int a = -1) : u(u), l(l), r(r), a(a) {}
};

int n, f[10];
vector<edge> g[10];

int solve(int v, int i) {
    if (v == n - 1) return 0;

    if (i == (int)g[v].size())
        if (f[v] == 0) return solve(v + 1, 0);
        else return -INF;

    int ans = -INF;
    for(int value = g[v][i].l; value <= min(g[v][i].r, f[v]); value++) {
        int u = g[v][i].u;
        f[v] -= value;
        f[u] += value;

        int t = solve(v, i + 1) + g[v][i].flow(value);
        ans = max(ans, t);

        f[v] += value;
        f[u] -= value;
    }

    return ans;
}

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

    cin >> n;
    forn(i, n*(n-1)/2) {
        int v, u, l, r, a;
        cin >> v >> u >> l >> r >> a;

        g[v - 1].pb(edge(u - 1, l, r, a));
    }

    forn(i, 30) {
        memset(f, 0, sizeof 0);
        f[0] = i;
        int t = solve(0, 0);
        if (t >= 0) {
            cout << i << " " << t << endl;
            return 0;
        }
    }

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

    return 0;
}


#define maxn 505
#define ll long long
using namespace std;
const int mo=1e9+7;
int n,m,a[maxn],b[maxn],c[maxn],z[maxn],A[maxn],B[maxn];
ll K;
void inc(int &x,int y){x=((x+y>=mo)?x+y-mo:x+y);}
void dec(int &x,int y){x=((x-y<0)?x-y+mo:x-y);}
void mult(int *u,int *v){
    int i,j;
    memset(z,0,sizeof(z));
    for(i=0;i<250;i++)
        for(j=0;j<250;j++)
            z[i+j]=(z[i+j]+1LL*u[i]*v[j])%mo;
    for(i=498;i>=250;i--)
        for(j=1;j<=m;j++)
            inc(z[i-b[j]],z[i]);
    for(i=0;i<250;i++) u[i]=z[i];
}
void fpm(int *c,ll n){
    memset(A,0,sizeof(A));
    A[0]=1;
    while(n){
        if(n&1) mult(A,c);
        n>>=1,mult(c,c);
    }
}
int main(){
    int i,j;
    scanf("%d %d %lld",&n,&m,&K);
    for(i=1;i<=n;i++) scanf("%d",&a[i]);
    for(i=1;i<=m;i++) scanf("%d",&b[i]);
    for(i=1;i<=n;i++){
        memset(c,0,sizeof(c));
        c[1]=1,fpm(c,a[i]);
        for(j=0;j<250;j++) inc(B[j],A[j]);
    }
    fpm(B,K);
    for(i=0;i<250;i++) B[i]=A[i];
    for(i=249;i>0;i--)
        for(j=1;j<=m;j++)
            if(i>=b[j]) inc(B[i-b[j]],B[i]);
    cout<<B[0];
    return 0;
}


#ifdef ONLINE_JUDGE
#define NDEBUG
#endif

#include <algorithm>
#include <cstring>
#include <iostream>
#include <ios>
#include <functional>
#include <map>
#include <set>
#include <tuple>
#include <utility>
#include <vector>
#include <stdio.h>
#include <string>
#include <cassert>
uint32_t readu32() { int c; while((c = getchar()) < '0'); uint32_t n = c & 15U; while((c = getchar()) >= '0') n = n * 10 + (c & 15U); return n; } uint64_t readu64() { int c; while((c = getchar()) < '0'); uint64_t n = c & 15U; while((c = getchar()) >= '0') n = n * 10 + (c & 15U); return n; } int32_t read32() { int c; while((c = getchar()) < '-'); bool neg = c == '-'; if(neg) c = getchar(); uint32_t n = c & 15U; while((c = getchar()) >= '0') n = n * 10 + (c & 15U); return neg ? -(int32_t)n : n; } int64_t read64() { int c; while((c = getchar()) < '-'); bool neg = c == '-'; if(neg) c = getchar(); uint64_t n = c & 15; while((c = getchar()) >= '0') n = n * 10 + (c & 15U); return neg ? -(int64_t)n : n; } std::string read_word() { int c; while ((c = getchar()) <= ' '); std::string s; do { s += (char) c; } while ((c = getchar()) > ' '); return s; } template<typename F, F f> struct no_arg_call { template<typename T> struct stupid_compiler { typedef T type; }; operator typename stupid_compiler<decltype(f())>::type() { return f(); } }; no_arg_call<decltype(&readu32), readu32> IU; no_arg_call<decltype(&read32), read32> II; no_arg_call<decltype(&readu64), readu64> IUL; no_arg_call<decltype(&read64), read64> IL; no_arg_call<decltype(&read_word), read_word> IW;
template<typename T, int N> struct mat { T dat[N*N]; struct id_s {}; static const id_s id; mat(): dat{} { } mat(std::initializer_list<T> l) { int i = 0; for(auto&& x: l) dat[i++] = x; for(; i < N*N; i++) dat[i] = 0; } mat(int m): dat{} { for(int i = 0; i < N; i++) (*this)(i,i) = m; } mat(id_s) { for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) dat[N*i + j] = +(i==j); } T& operator()(int i, int j) { return dat[N*i + j]; } const T& operator()(int i, int j) const { return dat[N*i + j]; } mat operator*(const mat& o) { mat r; for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) for(int k = 0; k < N; k++) r(i,j) += (*this)(i,k) * o(k,j); return r; } mat operator+(const mat& o) { mat r; for(int i = 0; i < N*N; i++) r.dat[i] = dat[i] + o.dat[i]; return r; } };
template<typename T> struct xgcd_s { T m, n, gcd; }; template<typename T> xgcd_s<T> xgcd(T a, T b) { T m[] = {1,0}, n[] = {0,1}, q[] = {0,0}; for(int i = 0; ; i^=1) { m[i] -= m[i^1] * q[i]; n[i] -= n[i^1] * q[i]; if(!b) return xgcd_s<T>{m[i],n[i],a}; q[i] = a/b; a %= b; std::swap(a,b); } } template<unsigned M()> struct _intmod { private: struct _p {unsigned x; }; _intmod(_p x) { n = x.x; } public: unsigned n; _intmod() {n = 0;} _intmod(int x) { n = x<0? M()+x%(int)M(): x%M(); } _intmod(unsigned x) {n = x%M(); } _intmod(long x) {n = unsigned(x<0? M()+x%(long)M(): x%M()); } _intmod(unsigned long x) {n = x % M(); } _intmod(long long x) {n = unsigned(x<0? M()+x%(long long)M(): x%M()); } _intmod(unsigned long long x) {n = x % M(); } _intmod operator+(_intmod b) const {return {n+b.n}; } _intmod operator*(_intmod b) const {return {_p{unsigned((unsigned long long)n * b.n % M())}}; } _intmod operator-(_intmod b) const {return {_p{n>=b.n? n-b.n : M()+n-b.n }}; } _intmod operator-() const {return {_p{n?M()-n:0U}}; } _intmod& operator+=(_intmod b) { return(*this = *this + b); } _intmod& operator-=(_intmod b) { return(*this = *this - b); } _intmod& operator/=(_intmod b) { return(*this = *this / b); } _intmod& operator*=(_intmod b) { return(*this = *this * b); } static _intmod mulinv(_intmod n) { xgcd_s<signed> x = xgcd<signed>(n.n, M()); assert(x.gcd == 1); return x.m; } _intmod operator/(_intmod b) const { return *this * mulinv(b); } bool operator==(_intmod b) const { return n == b.n; } bool operator!=(_intmod b) const { return n != b.n; } explicit operator int() const { return n; } }; template<unsigned M()> std::ostream& operator<<(std::ostream& o, _intmod<M> n) { return o<<n.n; } template<unsigned M()> std::istream& operator>>(std::istream& i, _intmod<M>& n) { long long x; i>>x; n = x; return i; } template<unsigned M> unsigned intmod_unsignedconst() { static_assert(M>0 && M<(1U<<31), "modulus bad"); return M; } template<unsigned*M> unsigned intmod_unsignedptr() { return *M; } template<unsigned M> using intmod = _intmod<intmod_unsignedconst<M> >; template<unsigned*M> using intmodv = _intmod<intmod_unsignedptr<M> >; static_assert(std::is_trivially_copyable<intmod<3> >::value, "");
template<typename T, typename X> T bpow(T b, X x) { T r = 1, p = b; while(x) { if(x & 1) r = r * p; if(x >>= 1) p = p * p; } return r; }

using namespace std;
using I = intmod<1000000007>;

#define D 250

//#define D 25

mat<I, D> m, mm;

int main(int argc, char** argv)
{
    if (argc == 2) freopen(argv[1], "r", stdin);
    ios_base::sync_with_stdio(0);

    int F = IU, B = IU, N = IU;
    vector<int> p(F), b(B);
    for (auto& x : p) x = IU;
    for (auto& x : b) x = IU;

    for (int r = 1; r < D; r++) {
        m(r, r - 1) = 1;
    }
    for (int x : b)
        m(0, x - 1) += 1;

    for(int pp : p) {
        auto tr = bpow(m, pp);
        mm = mm + tr;
    }

    auto a = bpow(mm, N);
    cout << a(0, 0);

    return 0;
}


#ifdef ONLINE_JUDGE
#define NDEBUG
#endif

#include <algorithm>
#include <cstring>
#include <iostream>
#include <ios>
#include <functional>
#include <map>
#include <set>
#include <tuple>
#include <utility>
#include <vector>
#include <stdio.h>
#include <string>
#include <cassert>
uint32_t readu32() { int c; while((c = getchar()) < '0'); uint32_t n = c & 15U; while((c = getchar()) >= '0') n = n * 10 + (c & 15U); return n; } uint64_t readu64() { int c; while((c = getchar()) < '0'); uint64_t n = c & 15U; while((c = getchar()) >= '0') n = n * 10 + (c & 15U); return n; } int32_t read32() { int c; while((c = getchar()) < '-'); bool neg = c == '-'; if(neg) c = getchar(); uint32_t n = c & 15U; while((c = getchar()) >= '0') n = n * 10 + (c & 15U); return neg ? -(int32_t)n : n; } int64_t read64() { int c; while((c = getchar()) < '-'); bool neg = c == '-'; if(neg) c = getchar(); uint64_t n = c & 15; while((c = getchar()) >= '0') n = n * 10 + (c & 15U); return neg ? -(int64_t)n : n; } std::string read_word() { int c; while ((c = getchar()) <= ' '); std::string s; do { s += (char) c; } while ((c = getchar()) > ' '); return s; } template<typename F, F f> struct no_arg_call { template<typename T> struct stupid_compiler { typedef T type; }; operator typename stupid_compiler<decltype(f())>::type() { return f(); } }; no_arg_call<decltype(&readu32), readu32> IU; no_arg_call<decltype(&read32), read32> II; no_arg_call<decltype(&readu64), readu64> IUL; no_arg_call<decltype(&read64), read64> IL; no_arg_call<decltype(&read_word), read_word> IW;
template<typename T, int N> struct mat { T dat[N*N]; struct id_s {}; static const id_s id; mat(): dat{} { } mat(std::initializer_list<T> l) { int i = 0; for(auto&& x: l) dat[i++] = x; for(; i < N*N; i++) dat[i] = 0; } mat(int m): dat{} { for(int i = 0; i < N; i++) (*this)(i,i) = m; } mat(id_s) { for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) dat[N*i + j] = +(i==j); } T& operator()(int i, int j) { return dat[N*i + j]; } const T& operator()(int i, int j) const { return dat[N*i + j]; } mat operator*(const mat& o) { mat r; for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) for(int k = 0; k < N; k++) r(i,j) += (*this)(i,k) * o(k,j); return r; } mat operator+(const mat& o) { mat r; for(int i = 0; i < N*N; i++) r.dat[i] = dat[i] + o.dat[i]; return r; } };
template<typename T> struct xgcd_s { T m, n, gcd; }; template<typename T> xgcd_s<T> xgcd(T a, T b) { T m[] = {1,0}, n[] = {0,1}, q[] = {0,0}; for(int i = 0; ; i^=1) { m[i] -= m[i^1] * q[i]; n[i] -= n[i^1] * q[i]; if(!b) return xgcd_s<T>{m[i],n[i],a}; q[i] = a/b; a %= b; std::swap(a,b); } } template<unsigned M()> struct _intmod { private: struct _p {unsigned x; }; _intmod(_p x) { n = x.x; } public: unsigned n; _intmod() {n = 0;} _intmod(int x) { n = x<0? M()+x%(int)M(): x%M(); } _intmod(unsigned x) {n = x%M(); } _intmod(long x) {n = unsigned(x<0? M()+x%(long)M(): x%M()); } _intmod(unsigned long x) {n = x % M(); } _intmod(long long x) {n = unsigned(x<0? M()+x%(long long)M(): x%M()); } _intmod(unsigned long long x) {n = x % M(); } _intmod operator+(_intmod b) const {return {n+b.n}; } _intmod operator*(_intmod b) const {return {_p{unsigned((unsigned long long)n * b.n % M())}}; } _intmod operator-(_intmod b) const {return {_p{n>=b.n? n-b.n : M()+n-b.n }}; } _intmod operator-() const {return {_p{n?M()-n:0U}}; } _intmod& operator+=(_intmod b) { return(*this = *this + b); } _intmod& operator-=(_intmod b) { return(*this = *this - b); } _intmod& operator/=(_intmod b) { return(*this = *this / b); } _intmod& operator*=(_intmod b) { return(*this = *this * b); } static _intmod mulinv(_intmod n) { xgcd_s<signed> x = xgcd<signed>(n.n, M()); assert(x.gcd == 1); return x.m; } _intmod operator/(_intmod b) const { return *this * mulinv(b); } bool operator==(_intmod b) const { return n == b.n; } bool operator!=(_intmod b) const { return n != b.n; } explicit operator int() const { return n; } }; template<unsigned M()> std::ostream& operator<<(std::ostream& o, _intmod<M> n) { return o<<n.n; } template<unsigned M()> std::istream& operator>>(std::istream& i, _intmod<M>& n) { long long x; i>>x; n = x; return i; } template<unsigned M> unsigned intmod_unsignedconst() { static_assert(M>0 && M<(1U<<31), "modulus bad"); return M; } template<unsigned*M> unsigned intmod_unsignedptr() { return *M; } template<unsigned M> using intmod = _intmod<intmod_unsignedconst<M> >; template<unsigned*M> using intmodv = _intmod<intmod_unsignedptr<M> >; static_assert(std::is_trivially_copyable<intmod<3> >::value, "");
template<typename T, typename X> T bpow(T b, X x) { T r = 1, p = b; while(x) { if(x & 1) r = r * p; if(x >>= 1) p = p * p; } return r; }

using namespace std;
using I = intmod<1000000007>;

#define D 250

//#define D 25

#pragma once

#include <stdint.h>
#include <vector>

/*
    dynamically sized version of matd
    constructors assume default constructor makes 0
    matd(row, col) 0-indexed
*/
template<typename T> struct matd {
    std::vector<T> dat;
    size_t dim;

    matd(): dim(0) { }
    matd(size_t dim): dim(dim), dat(dim * dim) { }

    T& operator()(size_t i, size_t j) {
        return dat[dim*i + j];
    }

    matd operator*(const matd& o) const {
        matd r(dim);
        for(int i = 0; i < dim; i++)
        for(int j = 0; j < dim; j++)
        for(int k = 0; k < dim; k++)
            r(i,j) += (*this)(i,k) * o(k,j);
        return r;
    }
    matd operator+(const matd& o) const {
        matd r(dim);
        for(int i = 0; i < dim*dim; i++)
            r.dat[i] = dat[i] + o.dat[i];
        return r;
    }

};

int main(int argc, char** argv)
{
    if (argc == 2) freopen(argv[1], "r", stdin);
    ios_base::sync_with_stdio(0);

    matd<I> m(250), mm(250);

    int F = IU, B = IU, N = IU;
    vector<int> p(F), b(B);
    for (auto& x : p) x = IU;
    for (auto& x : b) x = IU;

    for (int r = 1; r < D; r++) {
        m(r, r - 1) = 1;
    }
    for (int x : b)
        m(0, x - 1) += 1;

    for(int pp : p) {
        auto tr = bpow(m, pp);
        mm = mm + tr;
    }

    auto a = bpow(mm, N);
    cout << a(0, 0);

    return 0;
}