Travelling Merchant

Given a directed weighted graph and per-market buy/sell prices for K items, find the maximum profit-to-duration ratio of a closed walk trading at most one item at a time, floor it.

Hard9GraphShortest pathBinary searchDynamic programmingNo attempts yetTime limit2sMemory limit512 MB

Problem

After a long journey through the Australian outback, you arrive in the city of Cobar with nothing but a small backpack. Fascinated by the energy of its markets, you decide to settle there as a merchant. Cobar has NN markets numbered from 11 to NN, connected by MM one-way footpaths, where walking along each path takes a given number of minutes.

The markets trade KK different items numbered from 11 to KK. Each market sets a buying price and a selling price for each item. Not every market handles every item, and for a given item a market may support only buying or only selling. Assume that a market offering an item for sale has an unlimited supply, and that a market willing to buy an item keeps buying it without limit.

To earn money as quickly as possible, you want the most efficient profit cycle. A profit cycle is a walk that starts at some market vv with an empty backpack, follows footpaths while optionally buying and selling items, and returns to vv with an empty backpack. It may pass through the same market or footpath many times. A bought item must go into the backpack at once, and the backpack holds at most one item at any time. Assume you can always buy an available item regardless of how much money you hold, and that you cannot sell an item you do not hold.

The profit of a cycle is the total money received from sales minus the total money spent on purchases. Its duration is the sum of walking minutes along its footpaths. Its efficiency is profit divided by duration. A cycle with no trades has efficiency 00.

Among all profit cycles with strictly positive duration, find the maximum efficiency, rounded down to an integer. If no such cycle exists, report 00.

Input

Your program should read from standard input.

The first line contains three integers NN, MM and KK, the numbers of markets, footpaths and items.

Then NN lines follow. The iith of these lines contains 2K2K integers Bi,1,Si,1,Bi,2,Si,2,,Bi,K,Si,KB_{i,1}, S_{i,1}, B_{i,2}, S_{i,2}, \dots, B_{i,K}, S_{i,K} describing a market. For every 1jK1 \le j \le K, the pair Bi,jB_{i,j} and Si,jS_{i,j} are the prices at which you can buy and sell item jj at market ii. If an item cannot be bought or sold, 1-1 is used as a placeholder.

Then MM lines follow. The ppth of these lines contains three integers VpV_p, WpW_p and TpT_p, describing a one-way footpath from market VpV_p to market WpW_p that takes TpT_p minutes.

Output

Your program should write to standard output.

Print a single integer, the maximum efficiency among all profit cycles, rounded down to the nearest integer.

Constraints

For all subtasks, 1N1001 \le N \le 100, 1M99001 \le M \le 9900 and 1K10001 \le K \le 1000. For every item that can be bought or sold, 0<Si,jBi,j10000000000 < S_{i,j} \le B_{i,j} \le 1000000000 for all 1iN1 \le i \le N and all 1jK1 \le j \le K. In addition, VpWpV_p \ne W_p and 1Tp100000001 \le T_p \le 10000000 for all 1pM1 \le p \le M, and there is no pair of paths 1p<qM1 \le p < q \le M with (Vp,Wp)(V_p, W_p) equal to (Vq,Wq)(V_q, W_q). That is, no directed pair of markets appears twice.

Hint

In the sample case there are two cycles to consider, one returning from 11 through 22 and 33 to 11, and one returning from 11 through 44 and 33 to 11.

The first cycle takes 3+3+1=73 + 3 + 1 = 7 minutes to walk. Its most profitable sequence of trades is to buy item 22 at market 11, sell it at market 22, then buy item 11 at market 22, carry it through market 33, and finally sell it at market 11. The profit is 5+156+9=13-5 + 15 - 6 + 9 = 13, and 13/713/7 rounded down gives efficiency 11.

The second cycle takes 1+1+1=31 + 1 + 1 = 3 minutes to walk. Its most profitable sequence of trades is to buy item 22 at market 11, sell it at market 44, and then pass through market 33 back to market 11. The profit is 5+11=6-5 + 11 = 6, and 6/3=26/3 = 2, so the efficiency is 22.

Therefore the best efficiency of any profit cycle is 22.