Problem: Problem

Tomorrow is finally the day of the excursion to Maizu Elementary School. Gatcho, who attends Maizu Elementary School, noticed that he had forgotten to buy tomorrow's sweets because he was so excited. Gaccho wants to stick to sweets so that he can enjoy the excursion to the fullest.


Gaccho gets a $ X $ yen allowance from his mother and goes to buy sweets. However, according to the rules of elementary school, the total amount of sweets to bring for an excursion is limited to $ Y $ yen, and if it exceeds $ Y $ yen, all sweets will be taken up by the teacher.

There are $ N $ towns in the school district where Gaccho lives, and each town has one candy store. Each town is assigned a number from 1 to $ N $, and Gaccho lives in town 1. Each candy store sells several sweets, and the price per one and the satisfaction level that you can get for each purchase are fixed. However, the number of sweets in stock is limited. Also, Gaccho uses public transportation to move between the two towns, so to move directly from town $ i $ to town $ j $, you only have to pay $ d_ {i, j} $ yen. It takes.

At first, Gaccho is in Town 1. Gaccho should make sure that the sum of the total cost of moving and the price of the sweets you bought is within $ X $ yen, and the total price of the sweets you bought is within $ Y $ yen. I'm going to buy sweets. You must have arrived at Town 1 at the end. Gaccho wants to maximize the total satisfaction of the sweets he bought. Find the total satisfaction when you do the best thing.

Constraints

The input satisfies the following conditions.

* $ 1 \ leq N \ leq 14 $
* $ 1 \ leq X \ leq 10000 $
* $ 1 \ leq Y \ leq min (1000, X) $
* $ 1 \ leq K \ leq 300 $
* $ 1 \ leq a_i \ leq 1000 $
* $ 1 \ leq b_i \ leq 1000 $
* $ 1 \ leq c_i \ leq 1000 $
* $ 0 \ leq d_ {i, j} \ leq 10000 $
* $ d_ {i, i} = 0 $

Input

All inputs are given as integers in the following format:

$ N $ $ X $ $ Y $
Information on candy stores in town 1
Information on candy stores in town 2
...
Information on candy stores in town $ N $
$ d_ {1,1} $ $ d_ {1,2} $ ... $ d_ {1, N} $
$ d_ {2,1} $ $ d_ {2,2} $ ... $ d_ {2, N} $
...
$ d_ {N, 1} $ $ d_ {N, 2} $ ... $ d_ {N, N} $


On the first line, the number of towns $ N $, the amount of money you have $ X $, and the maximum amount you can use to buy sweets $ Y $ are given, separated by blanks.
From the second line, information on each $ N $ candy store is given.
In the following $ N $ row and $ N $ column, the amount $ d_ {i, j} $ required to go back and forth between town $ i $ and town $ j $ is given, separated by blanks.


Information on candy stores in each town is given in the following format.

$ K $
$ a_1 $ $ b_1 $ $ c_1 $
$ a_2 $ $ b_2 $ $ c_2 $
...
$ a_K $ $ b_K $ $ c_K $


The first line gives $ K $, the number of types of sweets sold at the candy store. In the following $ K $ line, the price of one candy $ a_i $, the satisfaction level $ b_i $ per candy, and the number of stocks $ c_i $ are given, separated by blanks.

Output

Output the maximum value of the total satisfaction level on one line.

Examples

Input

1 10 10
3
1 10 1
2 20 2
3 30 3
0


Output

100


Input

2 10 10
3
1 10 1
2 20 2
3 30 3
1
5 200 1
0 2
3 0


Output

200


Input

3 10 10
1
1 1 1
1
3 3 3
1
5 5 5
0 1 0
1 0 0
0 1 0


Output

10


Input

4 59 40
1
7 6 3
1
10 3 9
2
9 8 5
7 6 10
4
8 2 9
1 7 1
7 7 9
1 2 3
0 28 7 26
14 0 10 24
9 6 0 21
9 24 14 0


Output

34

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

struct data{
	int bit;
	int cost;
	int v;
	data(){}
	data(int vv,int bb,int cc){
		v=vv;
		cost=cc;
		bit=bb;
	}
	bool operator<(const data &dat)const{
		return cost>dat.cost;
	}
};

int n,x,y;
int dp[1<<14][14];
int dp2[14][1001];
int dp3[1<<14][1001];
int dp4[1<<14][1001];
int tmp[1001][1001];
int tmp2[1001];
int tmp3[15][1001];
int dist[14][14];
int a[14][301],b[14][301],c[14][301];

void dijk(){
	priority_queue<data> que;
	for(int i=0;i<(1<<n);i++){
		for(int j=0;j<n;j++){
			dp[i][j]=100000;
		}
	}
	dp[1][0]=0;
	que.push(data(0,1,0));
	while(que.size()){
		data d=que.top();
		que.pop();
		if(dp[d.bit][d.v]<d.cost)continue;
		for(int nec=0;nec<n;nec++){
			int nbit=d.bit|(1<<nec);
			int ncost=d.cost+dist[d.v][nec];
			if(ncost<dp[nbit][nec]){
				dp[nbit][nec]=ncost;
				que.push(data(nec,nbit,ncost));
			}
		}
	}
}

int main(void){
	scanf("%d%d%d",&n,&x,&y);
	for(int i=0;i<n;i++){
		int k;
		scanf("%d",&k);
		for(int j=0;j<k;j++){
			scanf("%d%d%d",&a[i][j],&b[i][j],&c[i][j]);
		}
		memset(tmp,0,sizeof(tmp));
		for(int j=0;j<k;j++){
			memset(tmp3,0,sizeof(tmp3));
			for(int cs=0;cs<=y;cs++){
				tmp3[0][cs]=tmp[j][cs];
			}
			int cnt2=0;
			for(int ncs=0;ncs<=10;ncs++){
				int cs=min(c[i][j],(int)(1<<ncs));
				c[i][j]-=cs;
				cnt2++;
				for(int l=0;l<=y;l++){
					tmp3[cnt2][l]=max(tmp3[cnt2][l],tmp3[cnt2-1][l]);
					if(l+cs*a[i][j]>y)continue;
					tmp3[cnt2][l+cs*a[i][j]]=max(tmp3[cnt2][l+cs*a[i][j]],tmp3[cnt2-1][l]+b[i][j]*cs);
				}
			}
			for(int l=0;l<=y;l++){
				tmp[j+1][l]=tmp3[cnt2][l];
			}
			
		}
		for(int j=0;j<=y;j++){
			dp2[i][j]=tmp[k][j];
		}
		for(int j=1;j<=y;j++){
			dp2[i][j]=max(dp2[i][j],dp2[i][j-1]);
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			scanf("%d",&dist[i][j]);
		}
	}
	dijk();
	for(int i=0;i<n/2;i++){
		for(int j=0;j<(1<<i);j++){
			for(int k=0;k<=y;k++){
				for(int l=k;l<=y;l++){
					dp3[j|(1<<i)][l]=max(dp3[j|(1<<i)][l],dp3[j][k]+dp2[i][l-k]);
				}
			}
		}
	}
	for(int i=0;i<(n+1)/2;i++){
		for(int j=0;j<(1<<i);j++){
			for(int k=0;k<=y;k++){
				for(int l=k;l<=y;l++){
					dp4[j|(1<<i)][l]=max(dp4[j|(1<<i)][l],dp4[j][k]+dp2[n/2+i][l-k]);
				}
			}
		}
	}
	for(int i=0;i<(1<<(n/2));i++){
		for(int j=1;j<=y;j++){
			dp3[i][j]=max(dp3[i][j],dp3[i][j-1]);
		}
	}
	for(int i=0;i<(1<<((n+1)/2));i++){
		for(int j=1;j<=y;j++){
			dp4[i][j]=max(dp4[i][j],dp4[i][j-1]);
		}
	}
	int res=0;
	for(int i=0;i<(1<<n);i++){
		int yosan=0;
		int bit1=0;
		int bit2=0;
		for(int bit=n-1;bit>=0;bit--){
			bit1*=2;
			if(bit>=n/2)bit2*=2;
			if(i>>bit & 1){
				if(bit<n/2){
					bit1++;
				}else{
					bit2++;
				}
			}
		}
		for(int j=0;j<n;j++){
			yosan=max(yosan,x-dp[i][j]-dist[j][0]);
		}
		yosan=min(y,yosan);
		for(int j=0;j<=yosan;j++){
			res=max(res,dp3[bit1][j]+dp4[bit2][yosan-j]);
		}
	}
	printf("%d\n",res);
	return 0;
}