This page is still under construction.

Parts of this page are still being built. What you see may change.

Train Ticket Allocation

Time limit1sMemory limit256 MB

Summary
Decide how many tickets to sell for each station pair so paid and free riders fit capacity P on every segment and total income is maximal.
Level

Medium7 of 10

Topics
Graph, Shortest path, Intervals
Solved
No attempts yet

Problem

A train starts at station 1, passes the stations in increasing order and stops at station NN. A ticket from station ii to station jj can be sold whenever i<ji < j.

The law fixes the price CijC_{ij} of a ticket from station ii to station jj in advance, and the exact demand DijD_{ij} for that pair is known before the trip, so you may sell anywhere from 0 to DijD_{ij} tickets for it. The government also sets aside OijO_{ij} free tickets from station ii to station jj. A passenger holding one of those takes a seat but brings no income.

The train carries PP passengers at most. On every segment between two adjacent stations the number of passengers on board, counting the ones with government tickets, must not exceed PP. Selling beyond the capacity is not allowed.

Find the largest income the trip can produce.

Input

The first line contains the number of test cases TT.

Each test case begins with a line holding the number of stations NN and the train capacity PP. The next N−1N-1 lines give the ticket prices. Line ii of that block holds N−iN-i numbers, and its jj-th number is Ci,i+jC_{i,i+j}, the price of a ticket from station ii to station i+ji+j. The next N−1N-1 lines give the demands DijD_{ij} in the same format, and the N−1N-1 lines after those give the number of free government tickets OijO_{ij}, again in the same format.

  • 0<T≤1000 < T \le 100
  • 3≤N≤163 \le N \le 16
  • 0<P≤2000 < P \le 200
  • 0<Cij≤10000 < C_{ij} \le 1000
  • 0≤Dij≤2500 \le D_{ij} \le 250
  • 0≤Oij≤200 \le O_{ij} \le 20
  • The government tickets alone never exceed the capacity.

Output

For each test case, print the maximum possible income on its own line.

Examples2

  1. Example 1

    Input
    1
    3 4
    6 7
    3
    4 1
    1
    2 1
    0
    
    Expected output
    10
    
  2. Example 2

    Input
    1
    3 5
    5 9
    5
    5 5
    5
    0 0
    0
    
    Expected output
    50