#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <climits>
#include <limits.h>
#include <string>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <assert.h>
#include <cstring>
#include <bitset>
#include <complex>
using namespace std;
#define rep(i, n) for (int (i) = 0; (i) < (n); (i) ++)
#define rep1(i, n) for (int (i) = 1; (i) <= (n); (i) ++)
#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)
#define db(x) {cout << #x << " = " << (x) << endl;}
#define dba(a, x, y) {cout<<#a<<" :";FOR(i123,x,y)cout<<setw(4)<<(a)[i123];cout<<endl;}
#define clr(x) memset(x,0,sizeof(x));
#define mp make_pair
#define pb push_back
#define sz(x) int((x).size())
#define endl '\n'
typedef long long ll;
typedef long double ld;
const int INF = 3e6;
const ll INFL = LLONG_MAX;
const ld pi = acos(-1);
// const int MOD = ;
//
const int VH = 0;
const int DIAGONAL = 1;
const int KNIGHT = 2;
int dx[8] = {1, -1, 1, -1, 2, -2, -2, 2};
int dy[8] = {2, 2, -2, -2, 1, -1, 1, -1};
int N, X[500], Y[500];

class ShortestPathWeightedGraph {
private:
  int N;
  vector<vector<pair<int,long long> > > adj;
  vector<long long> dist;
public:
  ShortestPathWeightedGraph(int N)
  {
    this->N = N;
    adj.resize(N);
  }
  void add(int u, int v, long long weight)
  {
    adj[u].push_back(make_pair(v, weight));
  }
  int shortest(int from, int to)
  {
    dist.resize(N);
    fill(dist.begin(), dist.end(), -1);
    priority_queue<pair<long long, int> > q;
    q.push(make_pair(0, from));
    dist[from] = 0;
    while (!q.empty())
    {
      int u = q.top().second;
      ll distu = -q.top().first;
      q.pop();
      if (dist[u] < distu) continue;
      for (int i = 0; i < adj[u].size(); i++)
      {
        int v = adj[u][i].first;
        long long weight = adj[u][i].second;
        if (dist[v] == -1 || dist[u] + weight < dist[v])
        {
          dist[v] = dist[u] + weight;
          q.push(make_pair(-dist[v], v));
        }
      }
    }
    return dist[to];
  }
};

int nodenumber(int type, int x, int y)
{
  return 3 * (N * x + y) + type;
}
int main()
{
  ios_base::sync_with_stdio(0); cout.precision(15); cout << fixed; cout.tie(0); cin.tie(0);
  cin >> N;
  rep(x,N) rep(y,N)
  {
    int a;
    cin >> a;
    X[a] = x;
    Y[a] = y;
  }
  ShortestPathWeightedGraph graph(3 * N * N);
  int baseweight = 1000;
  rep(x,N) rep(y,N)
  {
    rep(type1,3) rep(type2,3) if (type1 != type2)
    {
      graph.add(nodenumber(type1, x, y), nodenumber(type2, x, y), baseweight + 1);
    }
    // knight
    rep(i,8)
    {
      int xx = x + dx[i];
      int yy = y + dy[i];
      if (0 <= xx && xx < N && 0 <= yy && yy < N)
      {
        graph.add(nodenumber(KNIGHT, x, y), nodenumber(KNIGHT, xx, yy), baseweight);
      }
    }
    // vertical & horizontal
    rep(i,N)
    {
      if(x != i) graph.add(nodenumber(VH, x, y), nodenumber(VH, i, y), baseweight);
      if(y != i) graph.add(nodenumber(VH, x, y), nodenumber(VH, x, i), baseweight);
    }
    // diagonal
    rep(i,N)
    {
      for (int dirx = -1; dirx <= 1; dirx += 2)
      {
        for (int diry = -1; diry <= 1; diry += 2)
        {
          int xx = x + i * dirx;
          int yy = y + i * diry;
          if (x == xx && y == yy) continue;
          if (0 <= xx && xx < N && 0 <= yy && yy < N)
          {
            graph.add(nodenumber(DIAGONAL, x, y), nodenumber(DIAGONAL, xx, yy), baseweight);
          }
        }
      }
    }
  }
  vector<ll> best;

  rep(i,3) best.pb(0);
  for (int n = 2; n <= N * N; n++)
  {
    vector<ll> nextbest;
    rep(i,3) nextbest.pb(INF);
    rep(prev,3) rep(cur,3)
    {
      ll shortest_dist = graph.shortest(nodenumber(prev, X[n - 1], Y[n - 1]), nodenumber(cur, X[n], Y[n]));
      nextbest[cur] = min(nextbest[cur], best[prev] + shortest_dist);
    }
    best = nextbest;
  }
  ll mn = INFL;
  rep(i,sz(best)) mn = min(mn, best[i]);
  cout << mn / baseweight << " " << mn % baseweight << endl;
}


#include<bits/stdc++.h>
//#include<cstdio>
//#include<iostream>
//#include<algorithm>
using namespace std;
#define int long long
int gcd(int a,int b){
    return b==0?a:gcd(b,a%b);
}
int ppow(int a,int b,int mod){
    int ans=0;
    while(b){
        if(b&1)ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}
void ac(){
    cout<<"YES"<<endl;
}
void wa(){
    cout<<"NO"<<endl;
}
//
const int maxm=105;
int x[maxm],y[maxm];
int ax,ay,bx,by;
int xs,ys,t;
int cnt;
int d(int a,int b,int x,int y){//曼哈顿距离
    return abs(a-x)+abs(b-y);
}
signed main(){
    cin>>x[0]>>y[0]>>ax>>ay>>bx>>by;//注意x[0]和y[0]也是一个点，下面别忘记了
    cin>>xs>>ys>>t;
    while(x[cnt]-xs<t&&y[cnt]-ys<t){
        cnt++;
        x[cnt]=x[cnt-1]*ax+bx;
        y[cnt]=y[cnt-1]*ay+by;
    }
    int ans=0;
    for(int i=0;i<=cnt;i++){//枚举区间起点
        for(int j=i;j<=cnt;j++){//枚举区间终点
            int temp=min(d(xs,ys,x[i],y[i]),d(xs,ys,x[j],y[j]));
            temp+=d(x[i],y[i],x[j],y[j]);
            if(temp<=t){
                ans=max(ans,j-i+1);
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}


#define bug(x) cout<<#x<<" is "<<x<<endl;
#define IO std::ios::sync_with_stdio(0);
#include <bits/stdc++.h>
#define iter ::iterator
using namespace  std;
typedef long long ll;
typedef pair<ll,ll>P;
typedef pair<P,P>P1;
#define mk make_pair
#define pb push_back
#define se second
#define fi first
#define rs o<<1|1
#define ls o<<1
const int N=4e5+5;
ll mod=1e9+7;
ll x,y;
ll d[2][N];
int main(){
	IO;
	cin>>x>>y;
	if(x>y)swap(x,y);
	int cur=0,res=0;
	d[0][0]=1;
	d[1][0]=1;
	for(int i=1;i+res<=x+y;i++){
		res+=i;
		cur=!cur;
		for(int j=1;j<i;j++){
			d[cur][j]=d[!cur][j];
		}
		for(int j=i;j<=res;j++){
			d[cur][j]=(d[!cur][j]+d[!cur][j-i])%mod;
		}
		if(i==x){
			for(i++;i+res<=x+y;i++){
				res+=i;
			}
		}
	}
	ll ans=0;
	for(int i=max(0ll,res-y);i<=x;i++){
		ans=(ans+d[cur][i])%mod;
	}
	cout<<ans<<endl;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n;
    int k;
    cin>>n>>k;
    int a[10];
    int i=0,c=0;
    long long m=n;
    if(m==0)
        cout<<0;
    else
    {

        while(n)
        {
            a[i]=n%10;
            n=n/10;
            i++;
            c++;

        }
        if(c>k)
        {
            int j=0;
            int l=0;
            for(i=0; i<c; i++)
            {
                if(a[i]!=0)
                    j++;
                if(a[i]==0)
                    l++;
                if(l==k)
                    break;
            }
            if(l==k)
                cout<<j;
            else
                cout<<c-1;
        }

        else
        {
            cout<<c-1;

        }
    }

    return 0;
}


#include <iostream>
#include <stdio.h>
#include<cmath>
using namespace std;
long long g1,g2,g3;
int main(){
    //freopen("in.txt","r",stdin);
    int k,f1,f2,f3,n,f4,t1,t2,t3,t4,r,c;
    cin >> g1 >> g2;
    if(g2>g1){
    cout << g1 <<endl;
    return 0;
    }
    g1-=g2;
    g3=ceil((-1+sqrtl(g1*8+1))/2);
    cout << g3+g2 << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
int n,a,b,c;
int main()
{
string s="Danil";string s1="Olya"; string s2="Slava", s3="Ann", s4="Nikita";
string st;
cin>>st;
st="gggggggg"+st+"gggggggggggg";
int cnt=0;
for(int i=0;i<st.size()-4;i++)
{
    if(st.substr(i,5)==s)
        cnt++;
    if(st.substr(i,5)==s2)
        cnt++;
}
for(int i=0;i<st.size()-2;i++)
    if(st.substr(i,3)==s3)
    cnt++;
for(int i=0;i<st.size()-3;i++)
    if(st.substr(i,4)==s1)
    cnt++;
for(int i=0;i<st.size()-5;i++)
    if(st.substr(i,6)==s4)
    cnt++;
if(cnt==1)
    cout<<"YES";
else
    cout<<"NO";
}


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

#define fst first
#define snd second
#define all(x) (x).begin(), (x).end()
#define clr( a , v ) memset( a , v , sizeof(a) )
#define pb push_back
#define mp make_pair
#define sz size()
#define FORN( i , s , n ) for( int i = s ; i < (int)(n) ; i++ )
#define FOR( i , n ) FORN( i , 0 , n )
#define FORIT(i,x) for( typeof x.begin() i = x.begin() ; i != x.end() ; i++ )
#define trace(x)    cerr << #x << ": " << x << endl;
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define read ios::sync_with_stdio(false)

using namespace std;

typedef long long int64;
typedef vector <int> vi;
typedef pair <int64,int64> ii;
typedef vector <string> vs;
typedef vector <ii> vii;

int main(){
    set <ii> S;
    int64 n, a, ans = 0, b = 0;
    cin >> n;
    for( int64 k = 1 ; k < n ; k++ ){
        a = sqrt( 1.0*( n*n - k*k ) );
        if( a > k ) ans++;
        if( a == k ) b = 1;
    }
    if( n == 0 ) cout << "1" << endl;
    else cout << ans * 8 + 4 * ( b + 1 ) << endl;
    return 0;
}


#include<iostream>
#include<iterator>
#include<algorithm>
#include<math.h>
#include<vector>
#include<stack>
#include<string.h>
#include<map>
#include<bitset>
#include<set>
#include<deque>
#include<queue>
#include<list>
#define ln 100005
#define pi acos(-1.0)
#define pb push_back
#define mp nake_pair
using namespace std;
int main()
{
    long long int number;
    cin>>number;
    long long int N=ln;
    long long int arr[ln];
    arr[0]=0;
    for(long long int i=1;i<N;i++){
        arr[i]=arr[i-1]+(i);
    }
    long long int flag=0,akash=1;
    while(akash<N){

        if(binary_search(arr+1,arr+N,number-arr[akash])){
            cout<<"YES\n";
            flag=1;
            break;
        }
        akash++;
    }
    if(flag==0){
        cout<<"NO\n";
    }
    return 0;
}


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

#define F first
#define S second
#define FOR(i,a,b) for (int i = (a); i <= (b); ++i)
#define NFOR(i,a,b) for(int i = (a); i >= (b); --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
typedef long long ll; typedef pair <int, int> ii; typedef vector <int> vi; const int inf = 1e9 + 7;
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class T> void dbs(string str, T t) {cout << str << " : " << t << endl;}
template <class T, class... S> void dbs(string str, T t, S... s) {int idx = str.find(','); cout << str.substr(0, idx) << " : " << t << ","; dbs(str.substr(idx + 1), s...);}
template <class S, class T>ostream& operator <<(ostream& os, const pair<S, T>& p) {return os << "(" << p.first << ", " << p.second << ")";}
template <class T>ostream& operator <<(ostream& os, const vector<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T> void prc(T a, T b) {cout << "["; for (T i = a; i != b; ++i) {if (i != a) cout << ", "; cout << *i;} cout << "]";cout<<endl;}

const int N = 2e6;
int sv[N+1], mu[N+1];
int pw[N+1];
vi dv[N+1];

int expo(int n, int p, int mod = inf) {
    int ans = 1;
    while (p) {
        if (p & 1) {
            ans = (ans * 1LL * n) % mod;
        }
        n = (n * 1LL * n) % mod;
        p >>= 1;
    }
    return ans;
}
void ok(int &x) {
	if (x >= inf) x -= inf;
	if (x < 0) x += inf;
}
int main()
{
	ios::sync_with_stdio(0); cin.tie(0);

	FOR (i, 1, N) sv[i] = i;
	FOR (i, 2, 2000) if (sv[i] == i) for (int j = i * i; j <= N; j += i) sv[j] = i;

	mu[1] = 1;
	FOR (i, 2, N) {
		int x = sv[i];
		int g = (i / x) % x != 0;
		mu[i] = mu[i / x] * -g;
	}

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

	FOR (i, 1, N) {
		if (sv[i] == i) {
			pw[i] = expo(i, n);
		} else {
			pw[i] = (pw[sv[i]] * 1LL * pw[i/sv[i]]) % inf;
		}
	}

	FOR (i, 1, N) if (mu[i]) {
		for (int j = i; j <= N; j += i) dv[j].push_back(i);
	}

	int ans = 0, cur = 1;
	FOR (i, 2, k) {
		for (int x : dv[i]) {
			ok(cur -= pw[(i-1)/x]*mu[x]);
			ok(cur += pw[i/x]*mu[x]);
		}
		ok(ans += (cur^i));
	}
	cout << ans << "\n";

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
main(){
    long long n,k;
    cin>>n>>k;
    long long a[n];
    long long vt=0,vt2=0;
    for(long long i=1; i<=n; i++){
        cin>>a[i];
    }
    for(long long i=1; i<=n; i++){
        if(a[i] > k){ vt=i; break;}
    }
    for(long long i=n; i>=1; i--){
        if(a[i] > k){ vt2=i; break;}
    }
    if(vt==vt2 && vt==0) cout<<n;
    else cout<<n - (vt2 - vt + 1);
}


#include<bits/stdc++.h>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
ll Fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n;n=n*n;}return r;}
ll upd(ll x,ll v){x=x+v>=mod?x+v-mod:x+v;return x;}

int main()
{
	int l,r,a;
	cin>>l>>r>>a;
	int ans=a;
	if(l>r)
	ans-=l-r;
	else
	ans-=r-l;
	if(ans>=0)
	{
		cout<<max(l,r)*2+ans/2*2<<endl;
	}
	else
	{
		cout<<(min(l,r)+a)*2<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int int64_t
const int MAX = 7e5+1;
const int MOD = 1e6+3;

int fac[MAX], infac[MAX];

int Pow(int x, int y) {
	int curr = 1;
	while(y > 0) {
		if(y % 2 != 0) curr = (curr*x) % MOD;
		x = (x*x) % MOD;
		y /= 2;
	}

	return curr;
}

void precompute(int s) {
	fac[0] = 1;
	for(int i = 1; i <= s; i++) fac[i] = (fac[i-1]*i) % MOD;
	infac[s] = Pow(fac[s], MOD-2);
	for(int i = s-1; i >= 0; i--) infac[i] = ((i+1)*infac[i+1]) % MOD;
}

int bi(int n, int r) {
	return ((fac[n]*infac[r]) % MOD * infac[n-r]) % MOD;
}

signed main() {
	int n, c, total = 0;
	cin >> n >> c;
	precompute(n+c-1);
	for(int i = 1; i <= n; i++) total = (total+bi(i+c-1, i)) % MOD;
	cout << total;
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/stack:16777216")
#include <string>
#include <vector>
#include <map>
#include <list>
#include <iterator>
#include <set>
#include <queue>
#include <iostream>
#include <sstream>
#include <stack>
#include <deque>
#include <cmath>
#include <memory.h>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <utility>
#include <time.h>
using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define REP(i, N) FOR(i, 0, N)
#define RREP(i, N) RFOR(i, N, 0)
#define FILL(A,value) memset(A,value,sizeof(A))

#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define PB push_back
#define MP make_pair
#define Pi 3.14159265358979

typedef long long Int;
typedef unsigned long long UINT;
typedef vector <int> VI;
typedef pair <int, int> PII;

const int INF = 1000000000;
const int MAX = 500007;
const int MAX2 = 207;
const int BASE = 1000000000;
const int CNT = 500;
const int MOD = 1000000007;

double dp[MAX][107];

int p[MAX];

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

	int n = 1;
	p[0] = -1;

	FOR(i,0,107)
	{
		dp[0][i] = 1;
	}

	int q;
	cin >> q;
	FOR(qq,0,q)
	{
		int t;
		scanf("%d" , &t);
		if (t == 1)
		{
			int par;
			scanf("%d" , &par);
			--par;
			int v = n;
			p[v] = par;
			++ n;
			FOR(j,0,107)
			{
				dp[v][j] = 1;
			}
			double prevV = dp[par][0];
			dp[par][0] *= 0.5;
			int j = 1;
			while (p[par] != -1 && j <= 102)
			{
				int pp = p[par];
				double pV = dp[pp][j];
				dp[pp][j] /= (0.5 + 0.5 * prevV);

				prevV = pV;
				dp[pp][j] *= (0.5 + 0.5 * dp[par][j - 1]);
				par = pp;
				++j;
			}

		}
		else
		{
			int v;
			scanf("%d" , &v);
			--v;
			double res = 0;

			FOR(i,1,100)
			{
				res += (dp[v][i] - dp[v][i - 1]) * i;
			}
			printf("%.10f\n" , res);
		}
	}

    return 0;
}


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

string s1,s2;
int k = 0;

int main() {
  cin >> s1;
  int c = s1.size();
  int g = 0, b = 0;
  for(int i; i < c-1; i++){
     if (s1[i] != s1[i+1]) b = 1;
  }
    if (b==1){
    if (c%2 != 0){
      g = 1;
      for(int i = 0; i < c; i++){
        if (i != (c/2)){

          s2 += s1[i];
        }
      }
    }
    else s2 = s1;
    c = s2.size();
    for(int i = 0; i < c; i++){
        if (s2[i] != s2[c-i-1]){
          k = 1;
        }
    }
    if(k==0) k = c - 1 + g;
    else k = c + g;
    cout << k << endl;
    }
  else cout << b << endl;
  return 0;
}


#include <bits/stdc++.h>
#ifdef _MSC_VER
#define __builtin_popcount (int)__popcnt
#define __builtin_popcountll (int)__popcnt64
#define __builtin_clz (int)__lzcnt
#define $ system("pause")
#define gp_hash_table unordered_map
#else
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define $
//#define USACO
//#define IOI_style
#endif

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef complex<double> point;

#define MOD 12345
#define MAXN 123
#define LG 20
#define inf int(1e9 + 100)

#define rep(i, j, k) for (int i = j; i < k; i++)
#define for_all(x) x.begin(), x.end()
#define lid id << 1
#define rid id << 1 | 1
#define lch l, mid, lid
#define rch mid, r, rid
#define pb push_back
#define X first
#define Y second
#define re real()
#define im imag()

template <typename S, typename T>
inline ostream &operator<<(ostream &out, const pair<S, T> &p)
{
	return out << "( " << p.X << " , " << p.Y << " )";
}
template <typename S>
inline ostream &operator<<(ostream &out, const vector<S> &p)
{
	for (auto &e : p) out << e; return out;
}
template <typename T, typename S>
inline T smin(T &a, const S &b) { return a > b ? a = b : a; }
template <typename T, typename S>
inline T smax(T &a, const S &b) { return a < b ? a = b : a; }
inline double dot(const point l, const point r) { return l.re * r.re + l.im * r.im; }
inline double cross(const point l, const point r) { return l.re * r.im - l.im * r.re; }
ll po(ll v, ll u) { return u ? (po(v * v % MOD, u >> 1) * (u & 1 ? v : 1) % MOD) : 1; }
template<class T> inline void add(T &l, const T &r) { l = (l + r) % MOD; }
ll gcd(ll v, ll u) { return u ? gcd(u, v % u) : v; }

ll n;
int c;
vector<int> vec, o[30];

struct Mat {
	int n, m;
	ll arr[MAXN][MAXN];
	Mat(int n, int m, int z = 0) : n(n), m(m) {
		memset(arr, 0, sizeof(arr));
		rep(i, 0, min(n, m))
			arr[i][i] = z;
	}
};

inline ostream &operator<<(ostream &out, Mat &p)
{
	rep(i, 0, p.n) {
		rep(j, 0, p.m)
			cout << p.arr[i][j] << ' ';
		cout << endl;
	}
	return out;
}

inline Mat operator*(Mat l, Mat r) {
	Mat res(l.n, r.m);
	rep(i, 0, res.n) rep(j, 0, res.m) rep(k, 0, l.m)
		res.arr[i][j] += l.arr[i][k] * r.arr[k][j];
	rep(i, 0, res.n) rep(j, 0, res.m) res.arr[i][j] %= MOD;
	return res;
}

Mat operator^ (const Mat & v, ll u)
{
	Mat res(v.n, v.m, 1);
	for (int i = 62; ~i; i--) {
		res = res * res;
		if (u & (1ll << i)) res = res * v;
	}
	return res;
}

vector<int> state, f[MAXN];
map<vector<int>, int> rf;
int NOW, sz[MAXN];
void build(int g = 0) {
	if (g == c) {
		f[NOW] = state;
		rf[state] = NOW++;
		return;
	}
	rep(i, 0, sz[vec[g]]) {
		state[g] = i;
		build(g + 1);
	}
	return;
}

#ifndef IOI_style
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
#ifdef USACO
	freopen("gathering.in", "r", stdin);
	freopen("gathering.out", "w", stdout);
#endif
	cin >> n >> c;
	rep(i, 0, c) {
		char v; int m;
		cin >> v >> m;
		v -= 'A';
		vec.pb(v);
		o[v].pb(m);
	}
	sort(for_all(vec));
	vec.resize(unique(for_all(vec)) - vec.begin());
	for (auto e : vec) {
		sort(for_all(o[e]));
		o[e].resize(unique(for_all(o[e])) - o[e].begin());
		sz[e] = 1;
		for (auto z : o[e]) sz[e] *= z;
	}
	state.resize(vec.size());
	c = vec.size(); build();
	Mat M(NOW, NOW);
	rep(i, 0, NOW) {
		auto v = f[i];
		rep(j, 0, c) {
			v[j] = (v[j] + 1) % sz[vec[j]];
			M.arr[rf[v]][i]++;
			v[j] = (v[j] - 1 + sz[vec[j]]) % sz[vec[j]];
		}
	}
	M = M ^ n;
	ll answer = 0;
	rep(i, 0, NOW) {
		auto v = f[i];
		bool bad = false;
		rep(j, 0, c) {
			bool flag = false;
			for (auto z : o[vec[j]])
				if (v[j] % z == 0) flag = true;
			if (!flag) bad = true;
		}
		if (bad) continue;
		add(answer, M.arr[i][0]);
	}
	cout << answer << endl;
	$;
	return 0;
}
#endif


/*
  - Author : Ahmed Araby
  - submitted by an Alien so it's not and ordinary code,
    don't till any one.
*/
#include <bits/stdc++.h>
/*#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <queue>
#include <algorithm>
#include <math.h>*/
#define ll long long
#define pi pair<int , int>
#define pl pair<long long , long long>
using namespace std;

void run()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}
ll mem[50][50][50];
ll solve(int l , int r , int h) // we use the third dimension h to detect the case of restriction of none
{
    if(l>r || l==r)
        return 1;
    ll &ret=mem[l][r][h];
    if(ret!=-1)
        return ret;
    ret=0;
    for(int i=l; i<=r; i++)
    {
        ll tmp=0;
        if(i-l>=h-1 || r-i>=h-1)
        {
            ll r1 , r2 , r3 , r4;
            if(i-l>=h-1)
                r1=solve(l , i-1 , max(h-1 , 0) );
            else
                r1=solve(l , i-1 , 0);
            if(r-i>=h-1)
                r2=solve(i+1 , r , max(h-1 , 0) );
            else
                r2=solve(i+1 , r , 0);
            tmp=r1*r2;
            //cout<<"out"<<" "<<tmp<< endl;
            if(i-l>=h-1 && r-i>=h-1)
            {
                // here we calculates the possible trees that some part of it have no restriction
                r3=solve(l , i-1 , 0); // no res
                r3-=r1; // remove duplication
                r4=solve(i+1 , r , 0);
                r4-=r2; // remove duplication
                // missed calculation
                tmp+=r3*r2;
                tmp+=r4*r1;
                //cout<<endl<<"IN"<<" "<<tmp<< endl;
            }
        }
        ret+=tmp;
    }
    return ret;
}
int main()
{
	run();
	//freopen("input.txt" ,"r" , stdin);
	//freopen("output.txt" , "w", stdout);
	int n , h;
	cin>>n>>h;
	for(int i=0; i<=n; i++)
        for(int j=0; j<=n; j++)
            for(int k=0; k<=n; k++)
        mem[i][j][k]=-1;
	cout<<solve(1 , n , h)<< endl;
	return 0;
}


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

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

	LL a,b,c;
	cin >> a >> b >> c;

	if(!c){

		if(a == b) cout<< "YES\n";
		else cout << "NO\n";
		return 0;
	}
	if(a > 0 && c > 0 && b <= 0){
		cout << "NO\n";
		return 0;
	}
	if(c < 0 && b > a){
		cout << "NO\n";
		return 0;
	}
	if(c > 0 && b < a){
		cout << "NO\n";
		return 0;
	}

	if( (b - a) % c)
		cout << "NO\n";

	else{

		if( ((b - a)/c) +1 > 0)
			cout << "YES\n";
		else
			cout << "NO\n";
	}

    return 0;
}


//  ||  || |____|  //
//  ||  ||   ||   //
//  \\__//   ||  //
#include<bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#include <bitset>
#define mp make_pair
#define F first
#define S second
#define vll vector <ll>
#define mll map <ll,ll>
#define pb push_back
#define pf push_front
#define ub upper_bound
#define lb lower_bound
#define popf pop_front
#define popb pop_back
#define M 1000000007
#define M1 998244353
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test ll t;cin>>(t);while(t--)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

#define f(a, b) for (ll i = a; i < b; i++)
#define fr(a, b) for (ll j = a; j >= b; j--)
#define fi(a, b) for (ll j = a; j < b; j++)
#define fii(a, b) for (ll k = a; k < b; k++)
#define forab(i,a,b,c) for(ll (i) = a ; (i) <= (b) ; (i)+=(c))

using namespace std;
void start()
{
    fast;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
}

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

int main()
{
    start();
    string s; cin>>s;
    ll c=0,ans=0;
    f(0,s.length())
    {
        if(s[i]==s[i+1])
        {
            c++;
            if(c==5)
            {
                ans++;
                c=0;
            }
        }
        else
        {
            c=0;
            ans++;
        }
    }
    cout<<ans;
    return 0;
}


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

int a[55];//jdshfsdssf

int main()//jdsyggdsftsf
{
	ios::sync_with_stdio(0);//dwfeffgsdgdgedhshjhhsddgyegfgfg
	int n,m,i=1;//rutututututut
	cin>>n>>m;
	while(m>=i){
		m-=i;//jddsshshs
		i++;//ajsjdsjdsefggfyrgfygryuruwolpswoksij
		if(i>n) i=1;
	}
	cout<<m;//kxjisdhsdh
	return 0;
}
//2333333333333333333333333333333333333333333333333


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

    int t,i,m,n;

    //for (cin >> t; t--;)
    // {
    cin >> m >> n;
   if (n==0)
   {
     cout << 1 << endl;
   }
           else if ((m-n)>n)
                {
                    cout << n << endl;
                }
            else
                {
                    cout << m-n << endl;
                }

    // }

    return 0;
}


#define fr(i, a, x) for(int(i) = a; i <= x; ++i)
#define rfr(i, a, x) for(int(i) = a; i >= x; --i)
#define all(a) a.begin(), a.end()
#define Min(a, b) (a < b) ? a : b
#define Max(a, b) (a > b) ? a : b
#define pb push_back
//#define MY_DEBUG 1

#include <cstdio>
#include <string>
#include <map>
#include <ctime>
#include <stack>
#include <deque>
#include <cstdlib>
#include <set>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;

#ifdef MY_DEBUG
#include <fstream>
ifstream cin("input.txt");
ofstream cout("output.txt");
#else
#include <iostream>
#endif

const double pi = acos(-1.0);
const double eps = 1e-9;
const int inf = 1e9;
const long long linf = 1e18;
const int prost = 51;

int main()
{
	int n, m;
	cin >> n >> m;
	vector<string> s(m), p;
	set<string> h;
	for(int i = 0; i < m; ++i)
		cin >> s[i];
	for(int i = 0; i < m; ++i)
		for(int j = 0; j < s[i].length(); ++j)
			h.insert(s[i].substr(0, j + 1));
	for(set<string>::iterator i = h.begin(); i != h.end(); ++i)
		p.push_back(* i);
	int t = p.size();
	vector< vector<int> > per(t, vector<int>(4, -1));
	vector< vector<int> > dl(t, vector<int>(4, 0));
	char help[] = {'C', 'T', 'G', 'A'};
	for(int i = 0; i < t; ++i)
		for(int k = 0; k < 4; ++k)
		{
			string st = p[i];
			st += help[k];
			//if (h.find(st) != h.end())
			{
				bool tr = true;
				for(int u = 0; u < st.length() && tr; ++u)
					for(int j = 0; j < t && tr; ++j)
						if (st.substr(u, st.length() - u) == p[j])
						{
							per[i][k] = j;
							tr = false;
						}
				if (per[i][k] != -1)
				{
					string f = p[ per[i][k] ] ;
					int len, sl = f.length(), mx = 0;
					for(int j = 0; j < m; ++j)
					{
						len = s[j].length();
						if (sl - len >= 0 && f.substr(sl - len, len) == s[j] && len > mx)
							mx = len;
					}
					dl[i][k] = mx;
				}
			}
		}
	vector< vector< vector<int> > > dp(n + 1);
	for(int i = 0; i < n + 1; ++i)
	{
		dp[i].resize(i + 1);
		for(int j = 0; j < i + 1; ++j)
			dp[i][j].resize(t);
	}
	for(int i = 0; i < t; ++i)
	{
		if (p[i].length() == 1)
		{
			bool fi = false;
			for(int j = 0; j < m; ++j)
				if (s[j] == p[i])
					fi = true;
			if (fi)
				dp[1][1][i] = 1;
			else
				dp[1][0][i] = 1;
		}
	}
	long long mod = 1000000009, ans = 0;
	for(int i = 1; i < n; ++i)
		for(int j = max(i - 11, 0); j <= i; ++j)
			for(int k = 0; k < t; ++k)
				for(int u = 0; u < 4; ++u)
					if (per[k][u] != -1 && dp[i][j][k] > 0)
					{
						if (dl[k][u] + j > i)
						{
							dp[i + 1][i + 1][ per[k][u] ] += dp[i][j][k];
							dp[i + 1][i + 1][ per[k][u] ] %= mod;
						}
						else
						{
							dp[i + 1][j][ per[k][u] ] += dp[i][j][k];
							dp[i + 1][j][ per[k][u] ] %= mod;
						}
					}
	for(int i = 0; i < t; ++i)
	{
		ans += dp[n][n][i];
		ans %= mod;
	}
	cout << ans << '\n';
}


#include <iostream>
using namespace std;
int n,k,i,j,sum,one,two;
int main(){
	       cin>>n>>k;
	       int a[n+1];
	       for(i=1;i<=n;i++){
	       	cin>>a[i];
		   }
		   for(i=1;i<=k;i++){
		   	one=0; two=0;
		   	for(j=i;j<=n;j=j+k){
		   		if(a[j]==1) one++; else two++;
			   }
			   sum=sum+min(one,two);
		   }
		   cout<<sum;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define Mod 998244353
using namespace std;
long long fpow(long long a,long long b)
{
    long long ans=1,t=a;
    while(b)
    {
        if(b&1)ans=ans*t%Mod;
        t=t*t%Mod;
        b>>=1;
    }
    return ans;
}
long long inv(long long a)
{
    return fpow(a,Mod-2);
}
void add(int& a,int b)
{
    a+=b;
    if(a>=Mod)a-=Mod;
}
int dp[4010][4010][2];
int fac[5010];
int main(){
    int n,k,l;
    scanf("%d %d %d",&n,&k,&l);
    dp[0][0][0]=1;
    for(int i=0;i<=n*2;i++)
        for(int j=0;j<=i;j++)
            for(int o=0;o<=1;o++)
            {
                add(dp[i+1][j+1][o],dp[i][j][o]);
                if(o==0&&j>=k)add(dp[i+1][j][1],dp[i][j][o]);
                if(j)add(dp[i+1][j-1][o],(long long)dp[i][j][o]*j%Mod);
            }
    fac[0]=1;
    for(int i=1;i<=n*2+1;i++)
        fac[i]=(long long)fac[i-1]*i%Mod;
    printf("%lld\n",(long long)dp[n*2+1][0][1]*fpow(2,n)%Mod*fac[n]%Mod*inv(fac[n*2+1])%Mod*l%Mod);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main(){
    int i, j, state, arr[105][105];

    state = 0;

    for(i = 1; i <= 3; i++)
        for(j = 1; j <= 3; j++)
            cin >> arr[i][j];

    for(i = 1; i <= 3; i++){
        for(j = 1; j <= 3; j++){
            state = (1 + arr[i][j] + arr[i - 1][j] + arr[i + 1][j] + arr[i][j - 1] + arr[i][j + 1]) % 2;
            if(state) cout << "1";
            else cout << "0";
        }

        cout << endl;
    }

    return 0;
}


#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<ld,ld>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define INF ((~0) ^ (1 << 31))
#define INFL ((~0) ^ ((ll)1 << 63))
#define FOR(i,j,k) for(int i = j; i < k; i++)
#define REP(i,j,k) for(int i = j; i >= k; i--)
#define setmax(i) const int maxn = (int) i;
#define setmod(i) const int MOD = (int) i;
//srand (time(NULL));

using namespace std;

setmax(1e3+10);
setmod(1e9+7);

ll fact[maxn], rfact[maxn],n,m,k;

ll po(ll a, ll b)
{
    if(!b) return 1;
    ll t = po(a,b/2);
    t = (t*t) % MOD;
    if(b & 1) t = (t*a)%MOD;
    return t;
}

ll cmb(ll a, ll b)
{
    if(b > a) return 0;
    ll t = fact[a] * rfact[b];
    t %= MOD;
    t = (t*rfact[a-b]) % MOD;
    return t;
}

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

    fact[0] = 1;
    FOR(i,1,maxn) fact[i] = (fact[i-1]*i) %MOD;
    FOR(i,0,maxn) rfact[i] = po(fact[i],MOD-2);
    cin >> n >> m >> k;
    cout << (cmb(n-1,2*k) * cmb(m-1,2*k)) % MOD;
}


#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
#define M 1000000007
#define N 55

ll chu(ll a,ll b){
	ll res=1,p=M-2;
	while(p){
		if(p&1){res=res*b%M;}b=b*b%M;p>>=1;
	}return a*res%M;
}

int n,x,y,i,j,k,ac,wa,n0,n1,sb[666],nn;
ll res,jb;

struct Mat
{
    ll mat[55][55]={0};
    void print(){int i,j;for(i=0;i<=n;i++){for(j=0;j<=n;j++){printf("%lld ",mat[i][j]);}puts("");}puts("");}
    //void print(){int i,j;for(i=0;i<=n;i++){for(j=0;j<=n;j++){printf("%lld ",mat[i][j]*chu(1,jb)%M);}puts("");}puts("");}
}a,ans;

Mat operator*(Mat A,Mat B){
    Mat temp;
    for(int i = 0 ; i <= n ; i++)
    {
        for(int j = 0 ; j <= n ;j++)
        {
            //temp.mat[i][j]=0;
            for(int k = 0 ;k <= n ;k++)
            {
                temp.mat[i][j] = (temp.mat[i][j]+A.mat[i][k]*B.mat[k][j])%M;
            }
        }
    }
    return temp;
}

Mat operator^(Mat A, int k){
    Mat base;
    for(int i=0;i<=n;i++) base.mat[i][i]=1;

    while(k)
    {
        if(k & 1) base = base * A;
        A = A * A;
        k = k >> 1;
    }
    return base;
}

int main(){
	scanf("%d%d",&n,&k);
	jb=chu(1,n*(n-1)/2);
	for(i=1;i<=n;i++){
		scanf("%d",&sb[i]);
		if(sb[i]){n1++;}
		else{n0++;}
	}
	for(i=1;i<=n0;i++){
		if(sb[i]){wa++;}
		else{ac++;}
	}
	for(i=n0+1;i<=n;i++){
		if(sb[i]){ac++;}
		else{wa++;}
	}
	ac/=2;wa/=2;n/=2;

	ans.mat[0][wa]=1;
	a.mat[0][1]=n0*n1*jb%M;
	a.mat[0][0]=(1-a.mat[0][1]+M)%M;

	for(i=1;i<=n-1;i++){
		nn++;n0=max(0,n0-1);n1=max(0,n1-1);
		a.mat[i][i-1]=nn*nn*jb%M;
		a.mat[i][i+1]=n0*n1*jb%M;
		a.mat[i][i]=(1-a.mat[i][i+1]-a.mat[i][i-1]+M+M)%M;
	}
	nn++;n0=max(0,n0-1);n1=max(0,n1-1);
	a.mat[n][n-1]=nn*nn*jb%M;
	a.mat[n][n]=(1-a.mat[n][n-1]+M)%M;
	//a.print();
	a=a^k;
	//a.print();
	ans=ans*a;
	//ans.print();
	printf("%lld",ans.mat[0][0]);
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ll2 pair<ll, ll>
#define ll3 pair<ll, ll2>
#define mp make_pair
#define pb push_back
#define space << " " <<
#define print_iterable_range(x, lwr, upr) cout << #x << ": "; for(ll i=lwr; i<upr; i++){cout << x[i] << " ";} cout << endl;
#define debug_var(var) cout << #var << ": " << (var) << endl;
#define debug_vec(x) cout << #x << ": "; for(auto i : x) cout << i << " "; cout << endl;
#define fori(i, a, b) for(ll i=a; i<b; i++)
#define print_vec(x, a, b) fori(i, a, b) cout << x[i] << " "; cout << endl;
#define ford(i, a, b) for(ll i=a; i>=b; i--)
#define all(vec) vec.begin(), vec.end()

const double PI = 3.1415926535897;

ostream& operator << (ostream& oout, ll2 p){
	oout << "(" << p.first << ", " << p.second << ")";
	return oout;
}

istream& operator >> (istream& iin, ll2 &p){
	iin >> p.first;
	iin >> p.second;
	return iin;
}
template <typename T>
istream& operator >> (istream & iin, vector<T> &a){
	for(T &i : a){
		cin >> i;
	}
	return iin;
}

template <typename T>
ostream& operator << (ostream & oout, vector<T> &a){
	for(T &i : a){
		cout << i << " ";
	}
	return oout;
}

template <typename T>
T min(vector<T> a){
	ll ans = a[0];
	for(auto i : a){
		ans = min(ans, i);

	}
	return ans;
}

template <typename T>
T max(vector<T> a){
	ll ans = a[0];
	for(auto i : a){
		ans = max(ans, i);

	}
	return ans;
}

template <typename T>
bool operator == (pair<T, T> a, pair<T, T> b){
	return a.first == b.first and a.second == b.second;
}

const ll MOD = 1000000007;

//template<class T>
inline ll add_mod(ll a, ll b, const ll mod = MOD){
//    if(a<0) a += mod;
//    if(b<0) b += mod;
	return ((a>=0 ? a : a+mod)%mod + (b>=0 ? b : b+mod)%mod)%mod;
}

inline ll sub_mod(ll a, ll b, const ll mod = MOD){
	ll result = ((a>=0 ? : a+mod)%mod - (b>=0 ? b : b+mod)%mod);
	return (result >= 0 ? result : result + mod)%mod;
}

inline ll mult_mod(ll a, ll b, const ll mod = MOD){
	return ((a>=0? a : a+mod)%mod * (b>=0 ? b : b+mod)%mod)%mod;
}

ll fast_expo(ll x, ll y, const ll mod = MOD){
	ll ans = 1;
	while(y > 0){
		if(y%2 == 1)    ans = mult_mod(x, ans, mod);
		x = mult_mod(x, x, mod);
		y /= 2;
	}
	return ans;
}

inline ll div_mod(ll a, ll b, const ll mod = MOD){
	return mult_mod(a, fast_expo(b, mod - 2, mod), mod);
}

ll ncr(ll n, ll r){
	if(n == 0)
	return 1;
	// cout << n space r << endl;
	if(r == 0 or r == n){
		return 1;
	}
	else return ncr(n-1, r) + ncr(n-1, r - 1);
}

void solve(){

	string orig, rec;
	cin >> orig >> rec;
	cout.precision(10);
	ll dest = 0;
	ll unrec = 0;
	ll curr = 0;

	for(auto & i : orig){
		dest = dest + (i == '+' ? 1 : -1);
	}

	for(auto & i : rec){
		if(i == '+'){
			curr ++;
		}
		else if(i == '-'){
			curr--;
		}
		else unrec ++;
	}

	if(unrec < abs(dest - curr)){
		cout << (long double)0 << endl;
		return ;
	}

	ll left = abs(curr - dest);

	if((unrec - left) % 2 != 0){
		cout << (long double)0;
		return;
	}

	// debug_var(dest)
	// debug_var(curr)

	ll num_1 = (unrec - left)/2 + (dest >= curr ? left : 0);

	// debug_var(num_1)
	// debug_var(unrec)

	// cout << "raju\n";

	// debug_var(ncr(2,1))

	long double p = ((long double)(ncr(unrec, num_1)))/((long double)(pow(2LL, unrec)));
	cout << p;

	return;
}

int main(){
	ios_base::sync_with_stdio(NULL);
	cin.tie(0);cout.tie(0);
	ll num_test_cases = 1;
	// cin >> num_test_cases;
	while(num_test_cases--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>
#define fast ios::sync_with_stdio(0); cin.tie(0), cout.tie(0)
#define ll long long int
#define ull unsigned ll
#define mkp make_pair
#define pb push_back
#define pf push_front
#define ppb pop_back()
#define ppf pop_front()
#define lb lower_bound
#define ub upper_bound
#define in insert
#define sz size()
#define mem(a,x) memset(a,x,sizeof(a))
#define mt empty()
#define py puts("YES")
#define pn puts("NO")
#define png puts("-1")
#define pyn(x) x ? py : pn
#define all(x) x.begin(), x.end()
#define ft first
#define sd second
#define prq priority_queue
#define rev(x) reverse(all(x))
using namespace std;

int main() {
	int n;cin>>n;
    cout<<((n>>1)+1)*((n+1)>>1);
}


#include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <map>
using namespace std;
int check(string f)
{
    int z= 0;
    if( (f[0]=='0' && f.size() > 1) || f.size()>6 && f!="1000000" )
        return -10000000;
      z= atoi(f.c_str());
    return z;
}
int main()
{
    string s ;
    int  Max=-1 ;
    cin >> s;
    int n = s.size();
    for(int i = 1 ; i < n-1; i++)
    {
        for(int j = i+1 ; j < n; j++)
        {
            int s1 , s2 , s3;
            s1=check(s.substr(0,i));
            s2=check(s.substr(i,j-i));
            s3=check(s.substr(j,n-j));
          //  cout << s1 << "   " << s2 << "   " << s3<<endl;

            if(s1+s2+s3 > Max)
                Max=s1+s2+s3;
        }
    }
    cout << Max;
    return 0;
}


#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include <assert.h>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <set>
using namespace std;

#define REP(i, n) for(int i = 0; i < (n); ++i)
#define FOR(i, j, k) for(int i = (j); i <= (k); ++i)
#define pii pair<int, int>
#define fr first
#define sc second

long long a, b, c, l;
long long ans;

long long mxSide(long long a, long long b, long long c, long long l) {
  long long sum = 0;
  FOR(la, 0, l) {
    long long x = min(a + la - b - c, l - la);
    if (x < 0) continue;
    sum += (x + 1) * (x + 2) / 2;
  }
  return sum;
}

int main() {
	cin >> a >> b >> c >> l;
	ans = (l + 3) * (l + 2) * (l + 1) / 6;
	ans -= mxSide(a, b, c, l);
	ans -= mxSide(b, a, c, l);
	ans -= mxSide(c, b, a, l);

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


#include <iostream>

int n;

using namespace std;
int main()
{
    cin >> n;
    if(n==1) cout << 1;
    else
    {
    cout << n << " ";
    for(int i=n/2; i>1; i--)
        if(n%i==0)
        {
            cout << i << " ";
            n=i;
        }

    cout << 1;
    }
    return 0;
}


#include <iostream>
#define fr(a,b,c) for(int a = b, _ = c; a < _; a++)
using namespace std;
int main(){
	string address, url;
	int n, domain_l, domain_r;
	getline(cin, address);
	n = address.length();
	if(address[2] == 't'){
		url = "http://";
		domain_l = 4;
	}else{
		url = "ftp://";
		domain_l = 3;
	}
	domain_r = address.find("ru", domain_l);
	if(domain_r-domain_l == 0){
		domain_r = address.find("ru", domain_r+1);
	}
	url += address.substr(domain_l, domain_r-domain_l) + ".ru";
	if(domain_r + 2 < n){
		url += "/" + address.substr(domain_r + 2);
	}
	cout << url << endl;
}

// 1505232044281


#include <iostream>

using namespace std;

int la, lb, ra, rb, ta, tb;

int cmmdc(int a, int b){
    int r = 0;
    while(b){
        r = a % b;
        a = b;
        b = r;
    }
    /// rezultatul se afla in a fiindca b preia r = 0;
    return a;
}

int main()
{
    cin >> la >> ra >> ta;
    cin >> lb >> rb >> tb;

    int t = cmmdc(ta, tb);
    int dif;
    if(lb > la){
        swap(la, lb);
        swap(ra, rb);
        swap(ta, tb);
    }
    dif = la - lb;
    int rest = dif % t;

    int inters1 = min(rb - lb, rest + (ra - la)) - rest + 1;

    rest -= t;

    int inters2 = min(rest + (ra - la), rb - lb) + 1;

    cout << max(max(inters1, inters2), 0) << '\n';
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e6+5,mod=1e9+7;
#define int long long
int n,k,y[N];
inline int ksm(int x,int y){
	int res=1;
	while(y){
		if(y&1)res=res*x%mod;
		x=x*x%mod; y>>=1;
	}
	return res;
}
int pl[N],pr[N],fac[N];
signed main(){
	scanf("%lld%lld",&n,&k);
	for(int i=1;i<=k+2;i++)y[i]=(y[i-1]+ksm(i,k))%mod;
	pl[0]=pr[k+3]=fac[0]=1;

    for(int i=1;i<=k+2;i++)pl[i]=pl[i-1]*(n-i)%mod;
    for(int i=k+2;i>=1;i--)pr[i]=pr[i+1]*(n-i)%mod;
	for(int i=1;i<=k+2;i++)fac[i]=fac[i-1]*i%mod;
	int ans=0;
	for(int i=1;i<=k+2;i++){
		int op=y[i];
		int a=pl[i-1]*pr[i+1]%mod;
		int b=fac[i-1]*((k-i)&1?-1:1)*fac[k+2-i]%mod;
		ans=(ans+op*a%mod*ksm(b,mod-2)%mod)%mod;
	}
	cout<<(ans+mod)%mod<<endl;

}


#include	<bits/stdc++.h>
using namespace std;
int	main()	{
    int hh,mm;
    long a;
    scanf("%d:%d",&hh,&mm);
    scanf("%d",&a);
    mm+=a;
    if(mm>=60)
        hh=(hh+mm/60)%24;
        mm=mm%60;
    printf("%02d:%02d",hh,mm);
	return	0;
}


#include <iostream>
using namespace std;
int f(int m)
{
	while(m)
	{
		if(m%10!=4&&m%10!=7) return 0;
		m/=10;
	}
	return 1;
}
int a(int m)
{
	for(int i=m;i>=1;i--)
		if(m%i==0&&f(i)) return 1;
	return 0;
}
int main()
{
	int n;
	cin>>n;
	if(f(n)||a(n)) cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
	return 0;
}


#include <bits/stdc++.h>
char a[1001],b[1001];
using namespace std;
int main()
{
    scanf("%s%s",a,b);
    int lena=strlen(a),lenb=strlen(b);
    if(lena!=lenb)
    cout<<"NO";
    else
    {
        bool q=1;
        for(int i=0;i<lena;i++)
        if(a[i]!=b[lena-i-1])
        q=0;
        if(q)
        cout<<"YES";
        else
        cout<<"NO";
      }
}


#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <sstream>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cassert>
using namespace std;
typedef long long LL;
#define FOR(x, b, e) for (int x = (b); x <= (e); ++x)
#define FORD(x, b, e) for (int x = (b); x >= (e); --x)
#define REP(x, n) for (int x = 0; x < (n); ++x)
#define VAR(v, n) typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int) (x).size())
#define EACH(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i)
#define REACH(i, c) for (VAR(i,(c).rbegin()); i != (c).rend(); ++i)
#define UNIQUE(v) do { sort(ALL(v)); (v).resize(unique(ALL(v)) - (v).begin()); } while (0)
#define PB push_back
#define ST first
#define ND second
#define MP make_pair
#define skip continue
const int INF = 1000000001;

#define GET(x)			(scanf("%d", &(x)) == 1)
#define GET2(x, y)		(scanf("%d %d", &(x), &(y)) == 2)
#define GET3(x, y, z)	(scanf("%d %d %d", &(x), &(y), &(z)) == 3)
#define GETS(x)			(scanf("%s", (x)) == 1)
#define DGET(x)			int x; GET(x);

int main()
{
	int n, m, x1, y1, x2, y2;
	GET2(n, m);
	GET2(x1, y1);
	GET2(x2, y2);

	int dx = abs(x1-x2);
	int dy = abs(y1-y2);

	if (max(dx, dy) >= 5 || (dx == 4 && dy == 4) || (min(dx, dy) == 3 && max(dx,dy) == 4)) {
		puts("Second");
	} else {
		puts("First");
	}
}


#include <iostream>

using namespace std;

typedef unsigned long long intreg;

#define mod 1000000007

intreg n,s,f[22],rez;
intreg sum;
int i;
bool semn;

void read_data();
intreg combinari(intreg n,intreg k);
intreg put(intreg x, intreg p);
intreg invers[22];
void pre();

int main()
{
    read_data();
    pre();
    rez=combinari(n+s-1,n-1);
    for(int p=(1<<n)-1; p; --p)
    {
        semn=0;//0 pentru - si 1 pentru +
        sum=0;
        for(int aux=p,i=1; aux; aux>>=1,++i) if(aux&1) semn^=1,sum+=f[i]+1;
        if(s>=sum)  if(semn)
            {
                rez=(rez-combinari(n-1+s-sum,n-1)+mod)%mod;
            }
            else rez=(rez+combinari(n-1+s-sum,n-1))%mod;
    }
    cout<<rez%mod;

    return 0;
}

intreg combinari(intreg n,intreg k)
{
//combinari de n luate cate k
    if (k>n) return 0;
    n%=mod;
    intreg rasp=1;
    for(int i=0;i<k;++i)
    {
        rasp=rasp*(n-i)%mod;
        rasp=rasp*invers[i+1]%mod;
    }
    return rasp;
}

intreg put(intreg x, intreg p)
{
    intreg rez=1;
    for(; p; p>>=1)
    {
        if(p&1) rez=rez*x%mod;
        x=x*x%mod;
    }
    return rez;
}

void pre()
{
    for(int i=1;i<=21;++i) invers[i]=put(i,mod-2);
}

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


#include <iostream>
#include <cstdio>
using namespace std;
string rev(string s){
    for(int i=0;i<s.size()/2;i++){
        char tmp;
        tmp=s[i];
        s[i]=s[s.size()-1-i];
        s[s.size()-1-i]=tmp;
    }
    return s;
}
string add(string a1, string b1){
    string sum="";
    int num=0,cnt=b1.size()-1,carry=0;
    for(int i=a1.size()-1;i>=0;i--){
        num=num+a1[i]-'0';
        if(cnt>=0){
            num+=b1[cnt]-'0';
        }
        sum+=(num%10+'0');
        num=num/10;
        cnt--;
    }
    return sum;
}

int main()
{
    string a1,b1,a2="",b2="",sum1="",sum2="",sum3;
    cin>>a1>>b1;
    if(a1.size()<b1.size()){
        swap(a1,b1);
    }
    sum1=add(a1,b1);
    sum1=rev(sum1);
    for(int i=0;i<sum1.size();i++){
        if(sum1[i]!='0'){
            sum2+=sum1[i];
        }
    }
    for(int i=0;i<a1.size();i++){
        if(a1[i]!='0'){
            a2+=a1[i];
        }
    }
    for(int i=0;i<b1.size();i++){
        if(b1[i]!='0'){
            b2+=b1[i];
        }
    }
    sum3=add(a2,b2);
    sum3=rev(sum3);
    //cout<<"sum1 "<<sum1<<" sum2 "<<sum2<<" sum3 "<<sum3<<endl;

    if(sum2==sum3){
        printf("YES\n");
    }
    else{
        printf("NO\n");
    }

    return 0;
}


#include <bits/stdc++.h>

#define t first
#define g second
#define end() exit(0)
#define deb(x) cout << #x << " = " << x << endl;

using namespace std;

typedef long long ll;
typedef pair<int, int> song;

const int D = 1000 * 1000 * 1000 + 7, N = 50, M = 2500, G = 3;

inline int add(int, int);
inline int mul(int, int);
int f(int);
inline istream& operator >> (istream&, song&);
inline int inc(int&, int);
inline int multi(int&, int);
void init();
void inp();
void solve();
void upd();
void calc();

int n, T, dp1[N + 1][M + 1], dp23[N + 1][N + 1][M + 1], jav[N + 1][N + 1][N + 1][G + 1], mf[N + 1], num[G + 1], dur[G + 1], ans;

int main() {
	init();
	inp();
	solve();
	cout << ans << endl;
	return 0;
}
inline int inc(int& x, int y) {
	return (((x += y) < D)? x: (x -= D));
}
inline int add(int x, int y) {
	return (((x + y) < D)? x + y: ((x + y) - D));
}
inline int mul(int x, int y) {
	return ((1ll * x * y) % D);
}
int f(int x) {
	return (~mf[x]? mf[x]: (mf[x] = mul(x, f(x - 1))));
}
inline istream& operator >> (istream &in, song &x) {
	in >> x.t >> x.g;
	return in;
}
inline int multi(int& x, int y) {
	return (x = mul(x, y));
}
void init() {
	memset(mf, -1, sizeof mf);
	mf[0] = 1;
	dp1[0][0] = dp23[0][0][0] = jav[0][0][0][0] = 1;
}
void inp() {
	cin >> n >> T;
	song tmp;
	for (int i = 0; i < n; i++) {
		cin >> tmp;
		if (tmp.g == 1)
			for (int i = num[1]; i >= 0; i--)
				for (int j = dur[1]; j >= 0; j--)
					inc(dp1[i + 1][j + tmp.t], dp1[i][j]);
		else {
			for (int i = num[2]; i >= 0; i--)
				for (int j = num[3]; j >= 0; j--)
					for (int k = dur[2] + dur[3]; k >= 0; k--)
						((tmp.g == 2)? inc(dp23[i + 1][j][k + tmp.t], dp23[i][j][k]): inc(dp23[i][j + 1][k + tmp.t], dp23[i][j][k]));
		}
		dur[tmp.g] += tmp.t;
		num[tmp.g]++;
	}
}
void solve() {
	upd();
	calc();
}
void upd() {
	int i[G + 1];
	for (i[1] = 0; i[1] <= num[1]; i[1]++)
		for (i[2] = 0; i[2] <= num[2]; i[2]++)
			for (i[3] = 0; i[3] <= num[3]; i[3]++)
				for (int prev = 0; prev <= 3; prev++) {
					int tmp = jav[i[1]][i[2]][i[3]][prev];
					if (tmp == 0)
						continue;
					for (int next = 1; next <= 3; next++)
						if (next != prev && i[next] + 1 <= num[next]) {
							i[next]++;
							inc(jav[i[1]][i[2]][i[3]][next], tmp);
							i[next]--;
						}
				}
}
void calc() {
	for (int i = 0; i <= num[1]; i++)
		for (int j = 0; j <= dur[1]; j++) {
			if (T < j)
				break;
			for (int k = 0; k <= num[2]; k++)
				for (int h = 0; h <= num[3]; h++) {
					int nat = mul(dp1[i][j], dp23[k][h][T - j]);
					multi(nat, mul(f(i), mul(f(k), f(h))));
					for (int next = 1; next <= 3; next++)
						inc(ans, mul(nat, jav[i][k][h][next]));
				}
		}
}


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

int main() {
	// your code goes here
	int n,m,p,s;
	cin>>n>>m;
	queue<int>q;
	vector<int>visited(100007);
	q.push(n);
	visited[n]=1;
	int c=0,h=0;
	int maxi=max(n,m);
	while(q.empty()==false)
	{
	    s=q.size();
	    while(s--)
	    {
	    p=q.front();
	    q.pop();
	    if(p==m)
	    {
	        h=1;
	        break;
	    }
	    else
	    {
	        if(p-1<=2*maxi&&visited[p-1]==0)
	        {
	        q.push(p-1);
	        visited[p-1]=1;
	        }
	        if(p*2<=2*maxi&&visited[p*2]==0)
	        {
	        q.push(p*2);
	        visited[p*2]=1;
	        }
	    }
	    }
	    if(h==1)
	    break;
	    c++;
	 }
	 cout<<c;

	return 0;
}


    #include<bits/stdc++.h>
    #define ll long long
    #define pb push_back
    #define r_0 return 0
    #define fr first
    #define se second
    #define TIME ios_base::sync_with_stdio(0);
    using namespace std;
    const ll N= 1e5+1 ;
    ll n  , m ,  cnt  , ans , x , sum ,w ;
    vector< ll >v , v1;
    set< ll >se ;
    vector< pair<ll , ll > >p ;
    string s , s1 ;
    map< char , ll >mp ;
    char h , y;
    int main()
    {
        TIME
        mp[ 'a' ] = 1 ;
        mp[ 'e' ] = 1 ;
        mp[ 'u' ] = 1;
        mp[ 'o' ] = 1 ;
        mp[ 'n' ] = 1 ;
        mp[ 'i' ] = 1 ;
       cin >> s;
       if(s.size() == 1 and mp[s[0]] == 0){
       cout << "NO" ;r_0 ;}

       for( ll i= 0  ;i < s.size( ); i ++ )
       {
           if( mp[s[i]] == 0 and mp[s[i+1]] == 1 and s[i+1] != 'n' )
               continue ;
            if( mp[s[i]] == 0 and mp[s[i+1]] == 0 or mp[s[i]] == 0 and s[i+1] == 'n' )
           {
               cout << "NO";
               r_0 ;
           }

       }
       cout << "YES";
    }


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAX 1000005
const ll MOD = 1000 * 1000 * 1000 + 9;
ll n,m,k;
ll fexp(ll a, ll p)
{
    ll res=1;
    while(p){
        if(p&1){
            res=(res*a)%MOD;
            p--;
        }
        a=(a*a)%MOD;
        p/=2;
    }
    return res;
}
bool f(ll x)
{
//    ll n1 = n-x*k, m1=m-x*k;
//    ll q=n1/k;
    return m-(n%k)-(n-n%k)/k*(k-1)<=x;
}
ll bs(ll l, ll r)
{
//    cout<<"l: "<<l<<" r: "<<r<<endl;
    if(r-l<=1){
        if(f(r))
            return f(l)?l:r;
        return r;
    }
    ll med=(l+r)/2;
    if(f(med))
        return bs(l,med);
    return bs(med,r);
}
int main()
{
//    ios_base::sync_with_stdio(false);
    cin>>n>>m>>k;
    ll p = bs(0,n);
    cout<<((fexp(2,p+1)+MOD-2)%MOD*k%MOD+m-p*k)%MOD;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int dp[10000005];
void pre()
{
    memset(dp , 0 ,sizeof dp);
    dp[5]=1;
    for(int i=6;i<10000001;++i){
        dp[i]=dp[i-1];
        if(i%5==0){
            int j=i;
            while(j%5==0){
                j/=5;
                dp[i]++;
            }
        }
    }

}
int main()
{
int n;
pre();
while(cin>>n){
    vector< int > v;
    for(int i=0;i<10000001;++i){
        if(dp[i]==n)v.push_back(i);
    }
    /*for(int i=26140;i<26160;++i){
        cout<<dp[i]<<" ";
    }*/
    cout<<v.size()<<endl;
    for(int i=0;i<v.size();++i)
        cout<<v[i]<<" ";
    if(v.size())cout<<endl;

}

}


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

int main() {
   char a,b;
   int c=0;
   int x;
   cin>>x>>a;
     for (int i=1;i<x;i++)
     {
         cin>>b;
         if( b==a ) c++;
         else a=b;

         }
         cout <<c<<endl;
    return 0;
}


#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int oo = 0x3fffffff;
const int MAXN = 10;
const int MAXM = 100;

struct SInformation
{
    int a, b, c;
};

struct SEarn
{
    int price;
    int limit;
};

SInformation info[MAXN][MAXM];
SEarn earn[MAXM];
int n, m, cap;

inline bool cmp(const SEarn &a, const SEarn &b)
{
    return a.price > b.price;
}

void init(void)
{
    cin >> n >> m >> cap;
    for (int i = 0; i < n; i++)
    {
        string s;
        cin >> s;
        for (int j = 0; j < m; j++)
        {
            cin >> info[i][j].a >> info[i][j].b >> info[i][j].c;
        }
    }
}

void solve(void)
{
    int ans = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            for (int k = 0; k < m; k++)
            {
                earn[k].price = max(info[j][k].b - info[i][k].a, 0);
                earn[k].limit = info[i][k].c;
            }
            sort(earn, earn + m, cmp);
            int sum = 0;
            int t = cap;
            for (int k = 0; k < m; k++)
            {
                if (t <= earn[k].limit)
                {
                    sum += t * earn[k].price;
                    break;
                }
                sum += earn[k].limit * earn[k].price;
                t -= earn[k].limit;
            }
            ans = max(sum, ans);
        }
    }
    cout << ans << endl;
}

int main(void)
{
    ios::sync_with_stdio(false);
    init();
    solve();
    return 0;
}


#include<cmath>
#include<cstring>
#include<cassert>
#include<cstdlib>
#include<climits>
#include<cfloat>

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

#include<hash_set>
#include<hash_map>
using namespace __gnu_cxx;

// skrocone typy
typedef long long ll;

typedef unsigned int uint;
typedef unsigned short ushort;
typedef unsigned char uchar;
typedef unsigned long long ull;

// int pairs
typedef pair<int,int> pii;typedef pair<uint,uint> puiui;typedef pair<int,uint> piui;typedef pair<uint,int> puii;
// long long pairs
typedef pair< ll,ll> pllll;typedef pair<ull,ull> pullull;typedef pair<ull, ll> pullll;typedef pair<ll, ull> pllull;
// char pairs
typedef pair<char,char> pcc;typedef pair<char,uchar> pcuc;typedef pair<uchar,char> pucc;typedef pair<uchar,uchar> pucuc;
// long long x int pairs
typedef pair<int,ll> pill;typedef pair<ll,int> plli;typedef pair<uint,ll> puill;typedef pair<ll,uint> pllui;typedef pair<ull,int> pulli;typedef pair<int,ull> piull;typedef pair<uint,ull> puiull; typedef pair<ull,uint> pullui;
// long long x char pairs
typedef pair<char,ll> pcll;typedef pair<ll,char> pllc;typedef pair<uchar,ll> pucll;typedef pair<ll,uchar> plluc;typedef pair<ull,char> pullc;typedef pair<char,ull> pcull;typedef pair<uchar,ull> pucull; typedef pair<ull,uchar> pulluc;
// int x char pairs
typedef pair<char,int> pci;typedef pair<int,char> pic;typedef pair<uchar,int> puci;typedef pair<int,uchar> piuc;typedef pair<uint,char> puic;typedef pair<char,uint> pcui;typedef pair<uchar,uint> pucui; typedef pair<uint,uchar> puiuc;

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

#define pb push_back
#define pf push_front
#define ins insert
#define era erase

#define it iterator

#define type(x) __typeof(x)

#define foreach(itr,cont) for(__typeof(cont.begin()) itr = cont.begin(); itr != cont.end(); ++itr)
#define rep(i,n) for(int i=0; i<(n); ++i)
#define per(i,n) for(int i=(n)-1; i>=0; --i)
#define fo(i,a,b) for(int i=(a); i<(b); ++i)
#define of(i,a,b) for(int i=(b)-1; i>=(a); --i)

#define sz(c) (int)(c).size()

struct llhash {size_t operator()(ll  x)const {return (int)x ^ (int)(x>>32);}};
struct piihash{size_t operator()(pii x)const {return x.fi^x.se;}};

int nplanety;
int ntowary;
int ladownosc;
int buy[19][109];
int sel[19][109];
int num[19][109];

int comp4pla(int from, int to)
{
    vector<pii> pro;
    rep(i,ntowary)pro.pb(mp(sel[to][i]-buy[from][i],i));
    sort(pro.begin(),pro.end());
    int pos = sz(pro)-1;
    int wynik = 0;
    int moge = ladownosc;
    while(pos>=0 && pro[pos].fi>0 && moge>0)
    {
        int ile = min(moge,num[from][pro[pos].se]);
        wynik+=pro[pos].fi*ile;
        moge -= ile;
        --pos;
    }
    return wynik;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cout.precision(40);
    cout.setf(ios::fixed, ios::floatfield);

    cin>>nplanety>>ntowary>>ladownosc;

    rep(i,nplanety)
    {
        string s;
        cin>>s;
        rep(j,ntowary)
        {
            cin>>buy[i][j]>>sel[i][j]>>num[i][j];
        }
    }

    int profit = 0;
    rep(i,nplanety)rep(j,nplanety)profit=max(profit,comp4pla(i,j));

    cout<<profit<<endl;

    return 0;
}


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

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n, m, k;
    cin >> n >> m >> k;
    int a[100][200], b[100][200], c[100][200];
    string s;
    for (int i=0; i<n; i++)
    {
        cin >> s;
        for (int j=0; j<m; j++)
            cin >> a[i][j] >> b[i][j] >> c[i][j];
    }
    int ans = 0;
    for (int i=0; i<n; i++)
        for (int j=0; j<n; j++)
        {
            if (i == j)
                continue;
            priority_queue < pair <int, int> > q;
            for (int l=0; l<m; l++)
                    q.push(make_pair(b[j][l] - a[i][l], c[i][l]));
            int t = k;
            int res = 0;
            while (!q.empty() && t > 0)
            {
                int x = q.top().first, y = q.top().second;
                int d = min(t, y);
                q.pop();
                if (x <= 0)
                    continue;
                if (y-d != 0)
                    q.push(make_pair(x, y-d));
                res += x*d;
                t -= d;
            }
            ans = max(ans, res);
        }
    cout << ans;
    return 0;
}


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

int main(){
    int n,m,k;
    cin>>n>>m>>k;
    int a[n][m],b[n][m],c[n][m];
    int profit=0;
    string s[n],s1,s2;
    int i,j;
    for(i=0;i<n;i++){
        cin>>s[i];
        for(int x=0;x<m;x++){
            cin>>a[i][x]>>b[i][x]>>c[i][x];
        }
    }
    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            if(i!=j){
                pair<int, int> p[m];
                for(int x=0;x<m;x++){
                    p[x]=make_pair(b[i][x]-a[j][x],c[j][x]);
        //            cout<<s[i]<<" "<<s[j]<<" "<<p[x].first<<" "<<p[x].second<<endl;
                }
                sort(p,p+m);
                int temp=k,prft=0;
                int ii=m-1;
                while(temp>0&&ii>=0&&p[ii].first>0){
                    prft+=(p[ii].first)*(min(temp,p[ii].second));
                    temp=max(0,temp-p[ii].second);
                    ii--;
                }
                if(prft>profit){
                    profit=prft;
                }
            }
        }
    }
    cout<<profit<<endl;
}


#include <iostream>
#include <string>

using namespace std;

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

int a[11][101];
int b[11][101];
int c[11][101];

int ta[11][101];
int tb[11][101];
int tc[11][101];

int main() {
	int n, m, k;
	cin >> n >> m >> k;
	string s;
	REP(i, n) {
		cin >> s;
		REP(j, m) {
			cin >> ta[i][j] >> tb[i][j] >> tc[i][j];
		}
	}
	int ans = 0;
	REP(i, n) REP(j, n) if ( i != j ) {
		int res = 0;
		REP(ii, n) REP(jj, m) {
			a[ii][jj] = ta[ii][jj];
			b[ii][jj] = tb[ii][jj];
			c[ii][jj] = tc[ii][jj];
		}
		REP(ii, k) {
			int maxn = -1;
			int type = -1;
			REP(t, m) if ( c[i][t] > 0 && b[j][t] - a[i][t] > maxn ) {
				maxn = b[j][t] - a[i][t];
				type = t;
			}
			if ( maxn != -1 ) {
				res += b[j][type] - a[i][type];
				--c[i][type];
			}
		}
		ans = max(ans, res);
	}
	cout << ans << endl;

	//system("pause");
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <cmath>
#include <deque>
#include <stack>

#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define FRD(i, a, b) for(int i = a; i >= b; --i)
#define FR(i, a)     for(int i = 0; i < a; ++i)
#define REP(i, a)    for(int i = 1; i <= a; ++i)
#define FRSZ(i, x)   for(int i = 0; i < (int)x.size(); ++i)
#define bug(x, y)    cout << (x) << " " << (y) << endl
#define bg(a, m, n)  FOR(i, m, n) cout << (a[i]) << " "; cout << endl;
#define MP 	     make_pair
using namespace std;

typedef long long ll;
typedef unsigned long long Qword;
typedef pair <int, int> TPair;
#define x first
#define y second

const int MAXINT = 0x7fffffff;

int n, m, k, a[15][105][5];

vector <TPair> res;
int ans;

int main() {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    scanf("%d %d %d\n", &n, &m, &k);
    FOR(i, 1, n) {
        string tt; getline(cin, tt);
        FOR(j, 1, m)
            FOR(tt, 1, 3) scanf("%d", &a[i][j][tt]);
        scanf("\n");
    }
    FOR(i, 1, n)
        FOR(j, 1, n)
        if (i != j) {

            int pf = 0, cap = k;
            vector <TPair> res;
            FOR(t, 1, m)
                if (a[i][t][1] < a[j][t][2] && a[i][t][3] > 0)
                    res.push_back(TPair(a[j][t][2] - a[i][t][1], a[i][t][3]));
            sort(res.begin(), res.end());
            FRD(t, res.size() -1, 0) {
                int num = min(cap, res[t].y);
                pf += num * res[t].x;
                cap -= num;
                if (cap == 0) { ans = max(ans, pf); break; }
            }
            ans = max(ans, pf);
        }
    cout << ans << endl;
}


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

int table[16][128][4];
int profit[128][16][16];

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

    for ( int i=0 ; i<n ;++i )
    {
        string name;
        cin >> name;
        for ( int j=0 ; j<m ; ++j )
        {
            cin >> table[i][j][0] >> table[i][j][1] >> table[i][j][2];
        }
    }

    for ( int i=0 ; i<m ; ++i )
    {
        for ( int j=0 ; j<n ; ++j )
        {
            for ( int l=0 ; l<n ; ++l )
            {
                profit[i][j][l] = table[l][i][1] - table[j][i][0];
            }
        }
    }

    int res = 0;
    for ( int i=0 ; i<n ; ++i )
    {
        for ( int j=0 ; j<n ; ++j )
        {
            if ( i != j )
            {
                int a[m];
                for ( int ii=0 ; ii<m ; ++ii ) a[ii] = ii;

                //sort
                for ( int ii=0 ; ii<m ; ++ii )
                {
                    for ( int jj=ii+1 ; jj<m ; ++jj )
                    {
                        if ( profit[a[ii]][i][j] < profit[a[jj]][i][j] )
                        {
                            swap(a[ii], a[jj]);
                        }
                    }
                }

                int buy = k;
                int total = 0;
                for ( int ii=0 ; ii<m ; ++ii )
                {
                    if ( profit[a[ii]][i][j] > 0 )
                    {
                        int b = min(buy, table[i][a[ii]][2]);
                        buy -= b;
                        total += b * profit[a[ii]][i][j];
                    }
                }
                res = max(res, total);
            }
        }
    }

    cout << res << endl;

    return 0;
}


#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

typedef unsigned long long ull;
typedef pair<int,int> pii;
#define mp make_pair

struct Planet
{
    vector<int> b, s, c;

    void add(int bb, int ss, int cc)
    {
        b.push_back(bb);
        s.push_back(ss);
        c.push_back(cc);
    }
};

int n, m, k;
vector<Planet> P;

struct good
{
    int s;
    int b;
    int c;

    good(int b, int s, int c) : b(b), c(c), s(s) {}

    bool operator <(good g)
    {
        int p1 = s - b,
            p2 = g.s - g.b;
        if (p1 == p2)
            return c > g.c;
        return p1 > p2;
    }
};

int solve(int i, int j)
{
    vector<good> g;
    for (int q = 0; q < m; ++q)
    {
        if (P[i].c[q] == 0)
            continue;
        g.push_back(good(P[i].b[q], P[j].s[q], P[i].c[q]));
    }

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

    int left(k), need(0), profit(0);
    for (int q=0; q<g.size(); ++q)
    {
        if (left == 0) break;
        if (g[q].b < g[q].s)
        {
            int bo = min(g[q].c, left);
            left -= bo;
            need += bo * g[q].b;
            profit += bo * g[q].s;
        }
    }

    return profit - need;
}

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

    string s;
    cin >> n >> m >> k;
    for (int i=0; i<n; ++i)
    {
        cin >> s;
        P.push_back(Planet());
        for (int j=0; j<m; ++j)
        {
            int s, b, c;
            cin >> b >> s >> c;
            P.back().add(b, s, c);
        }
    }

    int result = 0, r;
    for (int i=0; i<n; ++i)
    {
        for (int j=0; j<n; ++j)
        {
            if (i != j)
            {
                r = solve(i, j);
                result = max(result, r);
            }
        }
    }

    cout << result;
    return 0;
}


// C.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include <cmath>
#include <map>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <iostream>
#include <cstring>
#include <string>

using namespace std;

int a[11][102];
int b[11][102];
int c[11][102];
int d[102],cnt[102];

int dp[101];

int main()
{
	int n,m,k;
	char name[20],b2;
	scanf("%d%d%d",&n,&m,&k);
	int res=-12;
	int maxi,id;

	int a1,b1,c1;
	for(int i=0;i<n;i++)
	{
		scanf("%1c",&b2);
		scanf("%s",&name);
		for(int j=0;j<m;j++)
		{
			scanf("%d%d%d",&a1,&b1,&c1);
			a[i][j]=a1;
			b[i][j]=b1;
			c[i][j]=c1;
		}
	}//for(int i=0;i<n;i++)

	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(i!=j)
			{
				for(int h=0;h<m;h++)
				{
					d[h]=b[j][h]-a[i][h];
					cnt[h]=c[i][h];
				}

				for(int h=0;h<k;h++)
				{
					dp[h]=-1;
				}

				dp[0]=0;

				for(int h=1;h<=k;h++)
				{
					maxi=0;
					id=-1;
					for(int g=0;g<m;g++)
					{
						if(cnt[g]>0 && maxi<d[g])
						{
							maxi=d[g];
							id=g;
						}
					}

					dp[h]=dp[h-1]+maxi;
					cnt[id]--;
				}//for(int h=0;h<k;h++)

				for(int h=0;h<=k;h++)
				{
					if(dp[h]>res)
					{
						res=dp[h];
					}
				}
			}//if(i!=j)
		}
	}

	cout<<res;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define pll pair<ll,ll>

typedef struct trip{
    ll a,b,c;
}trip;

bool comp(pll a,pll b){
    return a.first>b.first;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    ll n,m,k;
    cin>>n>>m>>k;
    // each planet has name and a vector of triplets
    // a=buy,b=sell,c=no.of items
    string sl;
    ll a1,b1,c1;
    vector<vector<trip>> planets;
    for(ll i=0;i<n;i++){
        vector<trip> temp(m);
        cin>>sl;
        for(ll j=0;j<m;j++){
            cin>>temp[j].a>>temp[j].b>>temp[j].c;
        }
        planets.pb(temp);
    }
    ll maxprof = 0;
    // ll i=0,j=1;
    for(ll i=0;i<n;i++){
        for(ll j=0;j<n;j++){
            vector<pll> tv;
            if(i!=j){
                for(ll it = 0;it<m;it++){
                    tv.pb(mp(planets[j][it].b-planets[i][it].a,planets[i][it].c));
                    // cout<<tv[it].first<<' '<<tv[it].second<<endl;
                }
                sort(tv.begin(),tv.end(),comp);
                ll k1=k,it=0,prof=0;
                while(k1>0 && it<m && tv[it].first>0){
                    ll q=min(tv[it].second,k1);
                    prof += q*tv[it].first;
                    k1 -= q;
                    it++;
                }
                // cout<<i<<' '<<j<<' '<<prof<<endl;
                maxprof = max(maxprof,prof);
            }
        }
    }
    cout<<maxprof<<endl;
}


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

using namespace std;

const int nmax = 100111;

int n , m , k , buy[200][200] , sell[200][200] , val[200][200] , maxval = 0;
string s;

int Try(int from , int to)
{
    int used = 0;
    int pr = 0;
    int lf[200];
    for (int i = 1 ; i <= m ; i ++ )
        lf[i] = val[from][i];
    while ( used != k )
    {
        int best = -1;
        for (int i = 1 ; i <= m ; i ++ )
            if ( lf[i] != 0 && ((sell[to][best] - buy[from][best]) < (sell[to][i] - buy[from][i])  || best == -1) && (sell[to][i] - buy[from][i]) > 0 )
                best = i;
        if ( best == -1 )
            break;
        int get = min(lf[best] , k - used);
        used += get;
        lf[best] -= get;
        pr += get * (sell[to][best] - buy[from][best]);
    }
    return pr;
}

int main()
{
//("input.txt","r",stdin);
//("output.txt","w",stdout);
scanf("%d%d%d",&n,&m,&k);
for (int i = 1 ; i <= n ; i ++ )
{
    cin >> s;
    for (int j = 1 ; j <= m ; j ++ )
        scanf("%d%d%d",&buy[i][j],&sell[i][j],&val[i][j]);
}
for (int i = 1 ; i <= n ; i ++ )
    for (int j = 1 ; j <= n ; j ++ )
        if ( i != j )
        maxval = max(maxval , Try(i , j) );
cout << maxval << endl;
return 0;
}


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

struct t{
       int a, b, c;
};

struct delt{
       int delta, kol;
};

bool cmp( delt a, delt b )
{
     return a.delta < b.delta ;
}

main(){
              int n, m, k, i, j, l, ans = 0;
       cin>>n>>m>>k;
       string x;

       vector < vector < t > > tov( n, vector < t > (m) );

       vector < delt > Delta( m );

       for( i=0; i<n; i++ )
       {
            cin>>x;
            for( j=0; j<m; j++ )
              cin>>tov[i][j].a>>tov[i][j].b>>tov[i][j].c ;
       }

       for( i=0; i<n; i++ )
         for( j=0; j<n; j++ )
           if( j!=i )
           {
               int CUR_ANS = 0;

               for( l=0; l<m; l++ )
               {
                 Delta[l].delta = tov[i][l].a - tov[j][l].b ;
                 Delta[l].kol = tov[i][l].c ;
               }

               sort( Delta.begin(), Delta.end(), &cmp );

               int CUR_K = k;

               for( l=0; l<m && CUR_K>0 ; l++ )
               {
                    if( Delta[l].delta > 0 )
                      break;

                    int MAX_NUM_CUR = min( Delta[l].kol, CUR_K );

                    CUR_ANS += MAX_NUM_CUR * (-Delta[l].delta);

                    CUR_K -= MAX_NUM_CUR;

               }

               if( CUR_ANS > ans )
                 ans = CUR_ANS;
           }

       cout<<ans;
}


#include <vector>
#include <numeric>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <limits>
#include <iomanip>

using namespace std;

#define FOR(i,a,b)		for(int i=(a),_b=(b);i<(_b);++i)
#define FORD(i,a,b)		for(int i=(a),_b=(b);i>(_b);--i)
#define pb			push_back
#define mp			make_pair
#define	all(c)			(c).begin(),(c).end()
#define	tr(c,i)	for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define	present(c,x)		((c).find(x) != (c).end())
#define	cpresent(c,x)		(find(all(c),x) != (c).end())

typedef long long			ll;
typedef unsigned long long	ull;
typedef unsigned char	 	byte;
typedef vector<int>			vi;
typedef pair<int, int>		pii;
typedef pair<ll, ll>		pll;
typedef vector<pii>			vpii;

const int N = 25;

typedef struct {
	int a,b,c;
} planet;

int main(int argc, char *argv[])
{
#ifndef ONLINE_JUDGE
	freopen(argv[1],"r",stdin);
#endif
#ifndef ONLINE_JUDGE
	ifstream cin(argv[1]);
#endif
#if 0
	ofstream cout(argv[2]);
#endif
	int n,m,K;
	cin >> n >> m >> K;
	planet p[n][m];
	string s;
	FOR(i,0,n) {
		cin >> s;
		FOR(j,0,m) cin >> p[i][j].a >> p[i][j].b >> p[i][j].c;
	}
	int best = 0;
	FOR(i,0,n) FOR(j,0,n) {
		if (j == i) continue;
		pii tmp[m];
		FOR(k,0,m) {
			tmp[k].first = p[j][k].b-p[i][k].a;
			tmp[k].second = k;
		}
		sort(tmp,tmp+m);
		int s = 0;
		int profit = 0;
		FORD(k,m-1,-1) {
			if (s >= K) break;
			if (tmp[k].first <= 0) break;
			int l = tmp[k].second;
			int buy = min(K-s,p[i][l].c);
			profit += buy*tmp[k].first;
			s += buy;
		}
		best = max(best,profit);
	}
	cout << best << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
struct ban
{
	int a,b,c;
};
struct ban1
{
	int x,d;
};
bool operator<(const ban1& a,const ban1& b)
{
	return a.d>b.d;
}

int n,m,k;
string s;
ban a[12][102];
ban1 b[102];
int ans;
int main()
{
	int i,j,ii,jj;
	cin>>n>>m>>k;
	int ck=k;
	for(i=0;i<n;++i)
	{
		cin>>s;
		for(j=0;j<m;++j)
		{
			cin>>a[i][j].a>>a[i][j].b>>a[i][j].c;
		}
	}
	for(i=0;i<n;++i)
	{
		for(j=0;j<n;++j)
		{
			if(j==i)
				continue;
			k=ck;
			int yans=0;
			for(ii=0;ii<m;++ii)
			{
				b[ii].x=ii;
				b[ii].d=-a[i][ii].a+a[j][ii].b;
			}
			sort(b,b+m);
			for(ii=0;ii<m;++ii)
			{
				if(b[ii].d<=0)
					break;
				int inchqan=a[i][b[ii].x].c;
				if(k>=inchqan)
				{
					k-=inchqan;
					yans+=inchqan*b[ii].d;
				}
				else
				{
					inchqan=k;
					yans+=inchqan*b[ii].d;
					break;
				}
			}
			ans=max(ans,yans);
		}
	}
	cout<<ans<<endl;
//	system("pause");
	return 0;
}


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

struct profit
{
    int p;
    int num;
}pro[105];

bool cmp(profit a,profit b)
{
    return (a.p>b.p);
}

int main()
{
    int n,m,k,buy[15][105],sell[15][105],num[15][105];
    while(cin>>n>>m>>k)
    {
        for (int i=0;i<n;i++)
        {
            string pn;
            cin>>pn;
            for (int j=0;j<m;j++)
                cin>>buy[i][j]>>sell[i][j]>>num[i][j];
        }
        int ans(0);
        for (int i=0;i<n;i++)
            for (int j=0;j<n;j++)
            {
                if (i==j) continue;
                int sum(0);
                for (int z=0;z<m;z++)
                {
                    pro[z].p=sell[j][z]-buy[i][z];
                    pro[z].num=z;
                }
                sort(pro,pro+m,cmp);
                int kk=k;
                for (int z=0;z<m;z++)
                {
                    if (pro[z].p<0) break;;
                    if (num[i][pro[z].num]<=kk)
                    {
                        sum+=pro[z].p*num[i][pro[z].num];
                        kk-=num[i][pro[z].num];
                    }
                    else
                    {
                        sum+=pro[z].p*kk;
                        kk=0;
                    }

                }
                if (sum>ans) ans=sum;
//                cout<<i<<" "<<j<<" "<<sum<<endl;
//                for (int z=0;z<m;z++)
//                    cout<<pro[z].p<<" ";
//                cout<<endl;
            }
        cout<<ans<<endl;
    }
}


//Solution by Zhussupov Nurlan
#include <iostream>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <set>
#include <vector>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <ctime>

using namespace std;

typedef long long LL;
typedef map<string , int> MSI;
typedef vector<int> VI;

#define PB(x) push_back(x)
#define sqr(x) ((x)*(x))
#define F first
#define S second
#define SZ(t) ((int)t.size())
#define len(t) ((int)t.length())
#define base 10
#define fname ""
#define sz 200
#define EPS (1e-8)
#define INF ((int)1e9 + 9)
#define write(xx) printf("%d" , xx);
#define readln(xx) getline(cin , xx)
#define read(xx) scanf("%d" , &xx)
#define for(xx1 , yy1 , zz1) for(int zz1 = xx1 ; zz1 <= yy1 ; zz1++)

const double PI  = acos(-1.0);

int n , m , k , a[sz][sz] , b[sz][sz] , c[sz][sz] , o , ans , kk;
string s;
pair <int , int > d[sz];

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

	cin >> n >> m >> k;

	for (1 , n , i)
	{
		cin >> s;
		for (1 , m , j)
			cin >> a[i][j] >> b[i][j] >> c[i][j];
	}

	for (1 , n , i)
		for (1 , n , j)
		if (i != j)
		{
			for (1 , m , t)
			{
				d[t].F = -(b[j][t] - a[i][t]);
				d[t].S = c[i][t];
			}
			sort(d + 1 , d + 1 + m);
			kk = k;
			o = 0;
			for (1 , m , t)
			{
				d[t].F = -d[t].F;
				if (d[t].F <= 0) break;
				while (kk > 0 && d[t].S > 0)
				{
					o += d[t].F;
					kk--;
					d[t].S--;
				}
			}
			ans = max(o , ans);
		}
	cout << ans;

    	return 0;
}


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

struct stock
{
    int sell,buy,q;
};
struct item
{
    int prof,q;
};

stock planets[11][101];
int n,m,k;

bool SortEm(item a,item b)
{
    return (a.prof>b.prof);
}

int Profit(int buyplanet,int sellplanet)
{
    item objects[101];
    int i;
    int totalprofit=0;
    int usedspace=0;
    int webuy;

    for (i=1;i<=m;i++)
    {
        objects[i].q=planets[buyplanet][i].q;
        objects[i].prof=planets[sellplanet][i].sell-planets[buyplanet][i].buy;
    }
    sort(objects+1,objects+1+m,SortEm);

    for (i=1;i<=m;i++)
    {
        if (objects[i].prof<=0)
        break;

        if (usedspace+objects[i].q<k)
        {
            usedspace=usedspace+objects[i].q;
            totalprofit=totalprofit+objects[i].q*objects[i].prof;
        }
        else
        {
            webuy=k-usedspace;
            totalprofit=totalprofit+objects[i].prof*webuy;
            break;
        }
    }
    return totalprofit;
}

int main()
{
    string useless;
    int i,j;
    int maxprofit=0;
    int currprofit;

    cin>>n>>m>>k;

    for (i=1;i<=n;i++)
    {
        cin>>useless;
        for (j=1;j<=m;j++)
        {
            cin>>planets[i][j].buy;
            cin>>planets[i][j].sell;
            cin>>planets[i][j].q;
        }
    }
    for (i=1;i<=n;i++)
    {
        for (j=1;j<=n;j++)
        {
            if (i==j)
            continue;

            currprofit=Profit(i,j);
            if ( currprofit>maxprofit )
            maxprofit=currprofit;

        }
    }
    cout<<maxprofit<<endl;
    return 0;
}


#include <iostream>
#include <map>
#include <string>
#include <string.h>
#include <algorithm>

using namespace std;

int sell[12][102];
int buy[12][102];
int rem[12][102];

int n,m,cap,prf;

struct ans{
	int pl,jens;
};
//
//int findMax(){
//	int max=0;
//	int maxInd;
//	for(int i=0;i<m;i++){
//		int best=30000;
//		int bestInd;
//		for(int j=0;j<n;j++){
//			if(rem[i][j] && (buy[i][j]<best) ){
//				best=buy[i][j];
//				bestInd=j;
//			}
//		}
//		if(best<3000){
//			if(sell[i]-buy[i][bestInd]>max){
//				max=sell[i][be]-buy[i][bestInd];
//				maxInd=i;
//			}
//		}
//	}
//	if(max>0){
//		return maxInd;
//	}
//	else
//		return -1;
//}

int calc(int from,int to){
	int rcap=cap;
	int ans=0;
	bool used[103]={false};
	while(rcap>0){
		int max=0;
		int maxInd;
		for(int i=0;i<m;i++){
			if(!used[i] && sell[to][i]-buy[from][i]>max){
				max=sell[to][i]-buy[from][i];
				maxInd=i;
			}
		}
		if(max==0)
			return ans;
		used[maxInd]=true;
		int val= rem[from][maxInd]>rcap ? rcap : rem[from][maxInd];
		rcap-=val;
		ans+=val*(sell[to][maxInd]-buy[from][maxInd]);

	}
	return ans;
}

int main(){
	//freopen("inf.txt","r",stdin);

	cin>>n>>m>>cap;

	string inp;
	int sl;

	for(int i=0;i<n;i++){
		cin>>inp;
		for(int j=0;j<m;j++){
			scanf("%d%d%d",&buy[i][j],&sell[i][j],&rem[i][j]);
		}
	}

	int res;
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++){
			if(i==j)
				continue;
			 res=calc(i,j);
			 if(res>prf)
				 prf=res;

		}
		cout<<prf<<endl;
/*
	while(cap>0){
		int ind=findMax();
		if(ind==-1)
			break;
		int min=4000,minInd;
		for(int i=0;i<n;i++){
			if(buy[ind][i]<min && rem[ind][i]){
				min=buy[ind][i];
				minInd=i;
			}
		}
		if(min==4000)
			break;
		prf+=rem[ind][minInd]*(sell[ind]-buy[ind][minInd]);
		cap-=rem[ind][minInd];
		rem[ind][minInd]=0;
	}

	cout<<prf<<endl;
*/

	return 0;
}


#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <utility>
#define pi (2*acos(0))
#define ll long long int

using namespace std;

int c[15][105],s[15][105],cap[15][105],n,m,k;
vector <pair <int,int> > vec;

int calc(int x,int y)
{
    int i,j,r=0,f,sc,q=k;

    vec.clear();

    for(i=0;i<m;i++)
    {
        vec.push_back(   make_pair(s[y][i]-c[x][i] , cap[x][i])  );
    }

    sort(vec.begin(),vec.end());
    reverse(vec.begin(),vec.end());

    for(i=0;i<vec.size() && q;i++)
    {
        f=vec[i].first;
        sc=vec[i].second;

        if(f<0) continue;

        if(sc>=q)
        {
            r+=q*f;
            q=0;
        }

        else
        {
            r+=sc*f;
            q-=sc;
        }
    }

    return r;
}

int main()
{
    int i,j,p,r=0;
    char str[15];

    scanf("%d %d %d",&n,&m,&k);

    for(i=0;i<n;i++)
    {
        scanf("%s",str);

        for(j=0;j<m;j++)
        {
            scanf("%d %d %d",&c[i][j],&s[i][j],&cap[i][j]);
        }
    }

    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            if(i==j) continue;

            r=max(r,calc(i,j));
        }
    }

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

    return 0;
}


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

typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;

int const N = 10 + 5, M = 100 + 20;
int n, m, k, a[N][M], b[N][M], c[N][M], ans;
string s;

int main() {
  ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  cin >> n >> m >> k;
  for (int i = 0; i < n; i ++){
    cin >> s;
    for (int j = 0; j < m; j ++) cin >> a[i][j] >> b[i][j] >> c[i][j];
  }

  for (int p1 = 0; p1 < n; p1 ++){
    for (int p2 = 0; p2 < n; p2 ++){
      vector <pii> v;

      for (int i = 0; i < m; i ++) v.push_back({max(0, b[p2][i] - a[p1][i]), c[p1][i]});

      sort(v.begin(), v.end());
      int rem = k, pr = 0;
      for (int i = v.size() - 1; i >= 0; i --){
	if (rem > v[i].second) pr += v[i].first * v[i].second, rem -= v[i].second;
	else pr += v[i].first * rem, rem = 0;
      }

      ans = max(ans, pr);
    }
  }

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


#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<iomanip>
#include<vector>
#include<map>
#include<deque>
#include<stack>
#include<queue>
#include<list>
#include<algorithm>
using namespace std;
struct sell
{
    int profit,stock;  //盈利，存量
};
sell pro[110];
int main()
{
    string name;
    int n,m,k,kk,i,j,l,t,p,s,num,max_sum;
    int sum;
    int a[11][101],b[11][101],c[11][101];
    while(cin>>n>>m>>kk)
    {
        for(i=0;i<n;i++)
        {
            cin>>name;
            for(j=0;j<m;j++)
            {
                cin>>a[i][j]>>b[i][j]>>c[i][j];
            }
        }
        max_sum=0;
        for(i=0;i<n;i++)
        {
            for(t=0;t<n;t++)
            {
                if(t==i)
                    continue;
                num=0;
                for(j=0;j<m;j++)
                {
                    p=b[t][j]-a[i][j];
                    s=c[i][j];
                    if(p>0&&s>0)
                    {
                        pro[num].profit=p;
                        pro[num].stock=s;
                        num++;
                    }
                }
                sum=0;
                k=kk;
                while(k>0)
                {
                    int max=0,tag=-1;
                    for(l=0;l<num;l++)
                    {
                        if(pro[l].profit>max)
                        {
                            max=pro[l].profit;
                            tag=l;
                        }
                    }
                    if(tag==-1)
                        break;
                    pro[tag].profit=0;
                    s=pro[tag].stock;
                    if(k>=s)
                    {
                        sum=sum+max*s;
                        k=k-s;
                    }
                    else
                    {
                        sum=sum+max*k;
                        k=0;
                    }
                 }
                if(sum>max_sum)
                    max_sum=sum;
            }
        }
        cout<<max_sum<<endl;
    }
    return 0;
}


#define nganggo using
#define nemspes namespace
#define standar std
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<list>
#include<cstdlib>
#include<cctype>
#include<bitset>
#include<string>
#include<map>
#include<complex>
#include<set>

#define FOR(_i,_n,_m) for(int (_i)=(_n),_t=(_m);_i<(_t);++(_i))
#define FORN(_i,_n,_m) for(int (_i)=(_n),_t=(_m);_i<=(_t);++(_i))
#define FORD(_i,_n,_m) for(int (_i)=(_n),_t=(_m);_i>=(_t);--(_i))
#define FORLL(_i,_n,_m) for(long long (_i)=(_n),_t=(_m);_i<(_t);++(_i))
#define FORNLL(_i,_n,_m) for(long long (_i)=(_n),_t=(_m);(_i)<=(_t);++(_i))
#define FORDLL(_i,_n,_m) for(long long (_i)=(_n),_t=(_m);(_i)>=(_t);--(_i))
#define FOREACH(_i,_a) for (__typeof(_a.begin()) _i=_a.begin();_i!=_a.end();++_i)
#define RESET(_a,_value) memset(_a,_value,sizeof(_a))
#define debugln clog << "----OK----" << endl;
#define debug(_a) clog << "Value of " <<  #_a << " is: " << (_a) << endl;
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ff first
#define ss second
#define mp make_pair
#define SIZE(_a) (int)_a.size()
#define VSORT(_a) sort(_a.begin(),_a.end())
#define SSORT(_a,_val) sort(_a,_a+(_val))
#define ALL(_a) _a.begin(),_a.end()
#define MAX(_a,_b) (((_a)>(_b))?(_a):(_b))
#define MIN(_a,_b) (((_a)<(_b))?(_a):(_b))

nganggo nemspes standar;

const int dr[]={ 1, 0,-1, 0, 1, 1,-1,-1};
const int dc[]={ 0, 1, 0,-1, 1,-1,-1, 1};
const double eps=1e-9;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef vector<ll> vll;
typedef pair<double,double> pdd;
typedef vector<pdd> vpdd;
const int INF=0x7FFFFFFF;
const ll INFLL=0x7FFFFFFFFFFFFFFFLL;
const double pi=acos(-1);

struct point_i
{
	int x,y;
	point_i (int _x=0,int _y=0) {x=_x;y=_y;}
};

struct point
{
	double x,y;
	point (double _x=0,double _y=0) {x=_x;y=_y;}
	bool operator < (const point &other) const
	{
		if (fabs(x-other.x)<eps)
			return y<other.y;
		return x<other.x;
	}
};

struct line_s
{
	point beg,end;
	line_s () {beg=point(0,0);end=point(0,0);}
	line_s (point a,point b) : beg(a),end(b) {}
	bool intersect (const line_s &other,point &ans)
	{
		double mua,mub;
		double denom,numera,numerb;

		denom  = (other.end.y-other.beg.y) * (end.x-beg.x) - (other.end.x-other.beg.x) * (end.y-beg.y);
		numera = (other.end.x-other.beg.x) * (beg.y-other.beg.y) - (other.end.y-other.beg.y) * (beg.x-other.beg.x);
		numerb = (end.x-beg.x) * (beg.y-other.beg.y) - (end.y-beg.y) * (beg.x-other.beg.x);

		if (denom==0 && numera==0 && numerb==0) return 0;

		mua=numera/denom;
		mub=numerb/denom;
		if (mua<0 || mua>1 || mub<0 || mub>1)
		{
			ans.x=0;
			ans.y=0;
			return 0;
		}
		ans.x=beg.x+mua*(end.x-beg.x);
		ans.y=beg.y+mua*(end.y-beg.y);
		return 1;
	}
};

struct line
{
	double a,b,c,len;
	line () {a=b=c=0.0;}
	line (const point &a_,const point &b_)
	{
		if (fabs(a_.x-b_.x)<eps)
		{
			a=1.0;
			b=0.0;
			c=-a_.x;
		}
		else
		{
			a=-(double)(a_.y-b_.y)/(a_.x-b_.x);
			b=1.0;
			c=-(double)(a_.x*a)-(double)(a_.y*b);
		}
		if (fabs(a)<eps) a=fabs(a);
		if (fabs(b)<eps) b=fabs(b);
		if (fabs(c)<eps) c=fabs(c);
		double x=a_.x-b_.x,y=a_.y-b_.y;
		len=sqrt((x*x)+(y*y));
	}
	bool parallel (const line &other)
	{
		return (fabs(a-other.a)<eps && fabs(b-other.b)<eps);
	}
	bool same (const line &other)
	{
		return (*this).parallel(other) && fabs(c-other.c)<eps;
	}
};

//using sokokaleb's template v2.2.4

typedef pair<pll,ll> pl3;
typedef vector<pl3> vpl3;

ll n,m,k,a,b,c;
ll ans;
string st;
vector<vpl3> vc;

int main()
{
	// ans=-INFLL+3;
	ans=0LL;
	cin >> n >> m >> k;
	vpl3 z;
	z.clear();
	vc.assign(n+1,z);
	FOR (iz,0,n)
	{
		cin >> st;
		FOR (i,0,m)
		{
			cin >> a >> b >> c;
			vc[iz].pb(mp(mp(a,b),c));
		}
	}
	FOR (iz,0,n)
	{
		FOR (jz,0,n)
		if (iz==jz) continue;
		else
		{
			vpll tmp;
			tmp.clear();
			FOR (i,0,m) tmp.pb(mp(vc[jz][i].ff.ss-vc[iz][i].ff.ff,vc[iz][i].ss));
			sort(ALL(tmp),greater<pll>());
			ll anz=0,tmpk=k;
			FOR (i,0,SIZE(tmp))
			{
				if (tmp[i].ff<0) break;
				if (tmp[i].ss<tmpk)
				{
					tmpk-=tmp[i].ss;
					anz+=tmp[i].ss*tmp[i].ff;
				}
				else
				{
					anz+=tmpk*tmp[i].ff;
					break;
				}
			}
			if (anz>ans) ans=anz;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<cstdio>
#include<algorithm>
int n,m,k;
char w[10001];
int a[11][101],b[11][101],c[11][101];
struct wang
{
	int z;
	int v;
};
bool cmp(wang a,wang b)
{
	if(a.z>b.z) return 1;
	else return 0;
}
wang joy[1001];
int main()
{
	int ans;
	while(scanf("%d%d%d",&n,&m,&k)==3)
	{
		ans=0;
		for(int i=0;i<n;i++)
		{
			scanf("%s",w);
			for(int j=0;j<m;j++)
			{
				scanf("%d%d%d",&a[i][j],&b[i][j],&c[i][j]);
			}
		}
		for(int i=0;i<n;i++)//買
		{
			for(int j=0;j<n;j++)//賣
			{
				 if(i==j) continue;
				 for(int p=0;p<m;p++)
				 {
					joy[p].z=b[j][p]-a[i][p];
					if(joy[p].z<0) joy[p].z=0;
					joy[p].v=c[i][p];
				 }
   			     std::sort(joy,joy+m,cmp);
				 int t=0,x=0;
				 for(int p=0;p<m;p++)
				 {
					if(t+joy[p].v<k)
					{
						x=x+joy[p].z*joy[p].v;
						t=t+joy[p].v;
					}
					else if(t+joy[p].v>=k)
					{
						x=x+joy[p].z*(k-t);
						break;
					}
				}
				if(x>ans)
				{
					ans=x;
				}
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
int n,m,k;
int x,y,z;

int dp[101];
char name[11];

int maxgain=0;
int tekgain=0;

struct diff
{
       int id;
       int amt;
};
diff D[101];
bool comp_less(diff a, diff b)
{
     if (a.amt!=b.amt) return (a.amt>b.amt);
     return (a.id>b.id);
}

struct item
{
       int buy;
       int sell;
       int amt;
};
item It[11][101];
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for (int i=0;i<n;i++)
    {
        scanf("%s",&name);
        for (int j=0;j<m;j++)
        {
            scanf("%d%d%d",&x,&y,&z);
            It[i][j].buy=x;
            It[i][j].sell=y;
            It[i][j].amt=z;
        }
    }
    for (int i=0;i<n;i++)
    {
        for (int j=0;j<n;j++)
        {
            if (i!=j)
            {
                     tekgain=0;
                     for (int K=0;K<m;K++)
                     {
                         D[K].id=K;
                         D[K].amt= It[j][K].sell - It[i][K].buy;
                     }
                     sort(D,D+m,comp_less);
                     int loaded=0;
                     for (int it=0;it<m&&loaded<k;it++)
                     {
                         if (D[it].amt<=0) break;
                         else
                         {
                             int it1=D[it].id;
                             if (It[i][it1].amt<=k-loaded)
                             {
                               tekgain+=D[it].amt*It[i][it1].amt;
                               loaded+=It[i][it1].amt;
                             }
                             else
                             {
                               int delta=k-loaded;
                               tekgain+=D[it].amt*delta;
                               loaded+=delta;
                             }
                         }
                     }
                     if (tekgain>maxgain) maxgain=tekgain;
            }
        }
    }
    printf("%d\n",maxgain);
    return 0;
}


#include<iostream>
#include<string>
#include<vector>
#include <cmath>
#include<algorithm>
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define LL long long
using namespace std;
typedef pair<int, int> pii;
const double eps = 1e-9;
int a[11][111],b[11][111],c[11][111];
int main()
{
	int n,m,k;
	int res = 0;
	cin>>n>>m>>k;
	string s;
	for(int i = 0; i < n; i++)
	{
		cin>>s;
		for(int j = 0; j < m; j++)
			cin>>a[i][j]>>b[i][j]>>c[i][j];
	}
	vector<pii> v(m);
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n; j++)
		{
			if (i == j) continue;
			for(int r = 0; r < m; r++)
			{
				v[r].first = b[j][r] - a[i][r];
				v[r].second = c[i][r];
			}
			sort(all(v));
			int K = k,sum = 0;
			for(int r = m - 1; r >= 0; r--)
			{
				if (v[r].first > 0)
				{
					if (v[r].second >= K)
					{
						sum += K * v[r].first;
						break;
					}
					K -= v[r].second;
					sum += v[r].second * v[r].first;
				} else break;
			}
			res = max(res,sum);
		}
	cout<<res<<endl;
 	return 0;
}


#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <map>
#include <deque>
#include <set>
using namespace std;

#define sz size()
#define FOR(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define ROF(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define rep(i,n) for (int (i) = 0; (i) < (n); ++(i))
#define fe(i,a) for (int (i) = 0; (i) < int((a).size()); ++(i))
#define C(a) memset((a),0,sizeof(a))
#define inf 1000000000
#define pb push_back
#define ppb pop_back
#define all(c) (c).begin(), (c).end()
#define pi 2*acos(0.0)
#define sqr(a) (a)*(a)
#define mp(a,b) make_pair((a), (b))
#define X first
#define Y second
#define y huy

typedef vector<int> vint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int INF=2000000000;
 int a[100][3000],b[100][3000],c[100][3000];
int main( ) {
    //freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
  //  freopen("input.txt","r",stdin);
    int n,cnt=0,m,k;
    cin>>n>>m>>k;
    rep(i,n){
        string s;cin>>s;
        rep(j,m){
            cin>>a[i][j]>>b[i][j]>>c[i][j];
        }

    }
    int ans=0;
    rep(i,n)rep(j,n)if(i^j){
        vector<pii > v(m);
        rep(u,m)v[u]=mp(b[j][u]-a[i][u],c[i][u]);
        sort(all(v));
        reverse(all(v));
        int u=0,h=0;
        for(int g=0;g<k && u<m;){
            while(g<k && v[u].second){
                g++; v[u].second--;
                h+=v[u].first;
                ans=max(ans,h);
            }
            u++;
        }
    }
    cout<<ans;

    return 0;
}


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

#define F first
#define S second
#define MP make_pair
typedef pair<int, int> pii;

const int MAXn = 10, MAXm = 100;
int n, m, k;
int b[MAXn][MAXm], s[MAXn][MAXm], r[MAXn][MAXm];
string str;
long long ans;
pii ez[MAXm];
int rem;
long long prof;

bool cmp(pii x, pii y)
{
	return x.F>y.F;
}

int main()
{
	cin >> n >> m >> k;
	for(int i=0; i<n; i++)
	{
		cin >> str;
		for(int j=0; j<m; j++)
			cin >> b[i][j] >> s[i][j] >> r[i][j];
	}

	for(int i=0; i<n; i++)
		for(int j=0; j<n; j++)
		{
			for(int z=0; z<m; z++)
				ez[z].F = s[j][z]-b[i][z], ez[z].S = r[i][z];
			sort(ez, ez+m, cmp);

			rem = k, prof = 0;
			for(int z=0; z<m and ez[z].F>0; z++)
			{
				prof += min(rem, ez[z].S)*ez[z].F;
				rem -= min(rem, ez[z].S);
			}

			ans = max(ans, prof);
		}

	cout << ans << endl;

	return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<map>
#include<string>
#include<vector>
#include<stack>
#include<list>
#include<queue>
#include<set>
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define INF 987654321
typedef __int64 LL;
using namespace std;
/////////////////////////////////////////////////
#define MAXN 110
int a[MAXN][MAXN],b[MAXN][MAXN],c[MAXN][MAXN];
char s[MAXN];
bool vis[MAXN];
int n,m,t;
int Sale(int x,int y,int res)
{
	bool flag;
	int ans=0,i,big,id,cnt;
	memset(vis,false,sizeof(vis));
	flag=true;
	while(flag&&res)
	{
		big=0;
		flag=false;
		for(i=1;i<=m;i++)
		{
			if(!vis[i])
			{
				flag=true;
				if(b[y][i]-a[x][i]>big)
				{
					big=b[y][i]-a[x][i];
					id=i;
				}
			}
		}
		if(big==0)
			break;
		cnt=MIN(res,c[x][id]);
		ans+=cnt*big;
		res-=cnt;
		vis[id]=true;
	}
	return ans;
}
int main()
{
	int i,j,k,ans,temp;
	while(~scanf("%d%d%d",&n,&m,&t))
	{
		for(i=1;i<=n;i++)
		{
			scanf(" %s",s);
			for(j=1;j<=m;j++)
				scanf("%d%d%d",&a[i][j],&b[i][j],&c[i][j]);
		}
		ans=0;
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
			{
				temp=Sale(i,j,t);
				ans=MAX(ans,temp);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


// rc.cpp
//

#include <math.h>
#include <string.h>

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <algorithm>
#include <numeric>

using namespace std;

typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned __int64 uint64;

#define PI 3.1415926535897932384626433832795
#define EPS 1E-8

int p[101][101],
	ai[1000],
	bi[1000];
uint ci[1000];

int main(int argc, char* argv[])
{
	uint n, m, k;
	cin >> n >> m >> k;

	string u;
	uint o = 0;
	for (uint i = 0; i < n; ++i)
	{
		cin >> u;
		for (uint j = 0; j < m; ++j)
		{
			uint ix = o + j;
			cin >> ai[ix] >> bi[ix] >> ci[ix];
		}

		o += m;
	}

	uint e = n * m;
	int	ma = 0;
	for (uint u = 0; u < e; u += m)
	{
		for (uint v = 0; v < e; v += m)
		{
			if (u == v)
				continue;
			for (uint i = 1; i <= m; ++i)
			{
				uint ix = u + i - 1;
				for (uint j = 1; j <= k; ++j)
				{
					int mx = p[j][i - 1];
					for (uint x = 1; x <= ci[ix] && x <= j; ++x)
					{
						int mm = p[j - x][i - 1] + (bi[v + i - 1] - ai[ix]) * x;
						if (mm > mx)
							mx = mm;
					}
					p[j][i] = mx;
				}
			}

			if (p[k][m] > ma)
				ma = p[k][m];
		}
	}

	cout << ma << endl;
	return 0;
}