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>
 
using namespace std;
 
const int INF = 1 << 29;
 
int N, X, Y, G[14][14];
int tsp[1 << 14][14];
vector< tuple< int, int, int > > vs[14];
int dp1[1 << 7][1001];
int dp2[1 << 7][1001];
 
int main()
{
 
  cin >> N >> X >> Y;
  for(int i = 0; i < N; i++) {
    int K;
    cin >> K;
    for(int j = 0; j < K; j++) {
      int a, b, c;
      cin >> a >> b >> c;
      vs[i].emplace_back(a, b, c);
    }
  }
  for(int i = 0; i < N; i++) {
    for(int j = 0; j < N; j++) {
      cin >> G[i][j];
    }
  }
 
 
  fill_n(*tsp, (1 << 14) * 14, INF);
  for(int i = 0; i < N; i++) tsp[1 << i][i] = G[0][i];
  for(int i = 0; i < (1 << N); i++) {
    for(int j = 0; j < N; j++) {
      for(int k = 0; k < N; k++) {
        tsp[i | (1 << k)][k] = min(tsp[i | (1 << k)][k], tsp[i][j] + G[j][k]);
      }
    }
  }
 
  vector< int > st1, st2;
  int mid = N / 2;
  for(int i = 0; i < (1 << mid); i++) st1.emplace_back(i);
  for(int i = 0; i < (1 << (N - mid)); i++) st2.emplace_back(i);
 
  for(int i = 0; i < mid; i++) {
    for(auto &p : vs[i]) {
      int a, b, c;
      tie(a, b, c) = p;
      for(int k = 0; c > 0; k++) {
        int key = min(c, (int) (1 << k));
        c -= key;
        for(int j = 1000; j >= key * a; j--) {
          dp1[1 << i][j] = max(dp1[1 << i][j], dp1[1 << i][j - key * a] + key * b);
        }
      }
    }
 
    for(int j = 1; j <= 1000; j++) {
      dp1[1 << i][j] = max(dp1[1 << i][j], dp1[1 << i][j - 1]);
    }
  }
 
  for(int i = 0; i < N - mid; i++) {
    for(auto &p : vs[i + mid]) {
      int a, b, c;
      tie(a, b, c) = p;
      for(int k = 0; c > 0; k++) {
        int key = min(c, (int) (1 << k));
        c -= key;
        for(int j = 1000; j >= key * a; j--) {
          dp2[1 << i][j] = max(dp2[1 << i][j], dp2[1 << i][j - key * a] + key * b);
        }
      }
    }
 
    for(int j = 1; j <= 1000; j++) {
      dp2[1 << i][j] = max(dp2[1 << i][j], dp2[1 << i][j - 1]);
    }
  }
 
  sort(begin(st1), end(st1), [&](int a, int b)
  {
    return (__builtin_popcount(a) < __builtin_popcount(b));
  });
  sort(begin(st2), end(st2), [&](int a, int b)
  {
    return (__builtin_popcount(a) < __builtin_popcount(b));
  });
 
  for(auto &i : st1) {
 
    if(__builtin_popcount(i) > 1) {
      int idx = -1;
      for(int j = 0; j < mid; j++) {
        if((i >> j) & 1) {
          idx = j;
          break;
        }
      }
 
      int bit = i ^(1 << idx);
 
      for(int j = 0; j <= 1000; j++) {
        dp1[i][j] = dp1[bit][j];
      }
 
      for(auto &p : vs[idx]) {
        int a, b, c;
        tie(a, b, c) = p;
        for(int k = 0; c > 0; k++) {
          int key = min(c, (int) (1 << k));
          c -= key;
          for(int j = 1000; j >= key * a; j--) {
            dp1[i][j] = max(dp1[i][j], dp1[i][j - key * a] + key * b);
          }
        }
      }
      for(int j = 1; j <= 1000; j++) {
        dp1[i][j] = max(dp1[i][j], dp1[i][j - 1]);
      }
    }
  }
 
  for(auto &i : st2) {
 
    if(__builtin_popcount(i) > 1) {
      int idx = -1;
      for(int j = 0; j < N - mid; j++) {
        if((i >> j) & 1) {
          idx = j;
          break;
        }
      }
 
      int bit = i ^(1 << idx);
 
      for(int j = 0; j <= 1000; j++) {
        dp2[i][j] = dp2[bit][j];
      }
 
      for(auto &p : vs[idx + mid]) {
        int a, b, c;
        tie(a, b, c) = p;
        for(int k = 0; c > 0; k++) {
          int key = min(c, (int) (1 << k));
          c -= key;
          for(int j = 1000; j >= key * a; j--) {
            dp2[i][j] = max(dp2[i][j], dp2[i][j - key * a] + key * b);
          }
        }
      }
      for(int j = 1; j <= 1000; j++) {
        dp2[i][j] = max(dp2[i][j], dp2[i][j - 1]);
 
      }
    }
 
  }
 
  int ret = 0;
  for(auto &s : st1) {
    for(auto &t : st2) {
      int bit = (t << mid) | s;
      int rest = X - tsp[bit][0];
      if(rest < 0) continue;
      int cost = min(rest, Y);
      for(int i = 0; i <= cost; i++) {
        ret = max(ret, dp1[s][i] + dp2[t][cost - i]);
      }
    }
  }
 
 
  cout << ret << endl;
}