This page is still under construction.

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

Plane Ticket Pricing

Time limit2sMemory limit256 MB

Summary
Set each week's ticket price from that week's demand estimates to maximize total revenue over the remaining seats and weeks.
Level

Medium4 of 10

Topics
Dynamic programming
Solved
No attempts yet

Problem

Plane ticket prices swing from one week to the next, and travellers cannot predict them. Some buy early and watch the price drop a few days later. Some wait and watch the price climb right before they pay. Only the airline comes out happy.

There is a system behind the swings. An airline prices a flight dynamically, using the number of seats still available and the number of weeks left before departure. When few seats remain, the price can stay high until the final weeks and then fall to fill the cabin. The airline wants the largest possible revenue from the flight.

The International Contrived Pricing Corporation has hired you to set the ticket price each week. The airline studied its historical data and can estimate how many seats sell at a given price with a given number of weeks left. Given the seats still available and the number of weeks left before the flight, set this week's price so that the revenue collected from this week through the week of the flight is as large as possible.

Assume that the number of tickets sold matches the estimate exactly, unless fewer seats remain. In that case every remaining seat sells. Assume also that the price of every later week is chosen optimally.

A higher price does not always sell fewer tickets. A higher price sometimes sells more, because travellers worry that the price will climb even further.

Input

The input describes one flight. The first line contains two integers NN and WW, the number of seats left and the number of weeks left before the flight (0<N≤3000 < N \le 300, 0≤W≤520 \le W \le 52).

The next W+1W + 1 lines hold the estimates for W,W−1,…,0W, W - 1, \dots, 0 weeks left, in that order. The line for 00 weeks left is the last week. Each of these lines starts with an integer KK, the number of prices to consider that week (0<K≤1000 < K \le 100). Then come KK integers p1,…,pKp_1, \dots, p_K, the prices in dollars (0<p1<⋯<pK<10000 < p_1 < \cdots < p_K < 1000). Then come KK more integers s1,…,sKs_1, \dots, s_K (0≤si≤N0 \le s_i \le N), where sis_i is the number of tickets that sell at price pip_i.

Output

On the first line, print the largest total revenue the airline can collect from ticket sales, counted from the current week through the week of the flight. On the second line, print the price to set for the current week, which is WW weeks before the flight.

If several sets of prices reach that maximum revenue, print the smallest price for week WW.

Examples2

  1. Example 1

    Input
    50 2
    1 437 47
    3 357 803 830 13 45 46
    1 611 14
    
    Expected output
    23029
    437
    
  2. Example 2

    Input
    100 3
    4 195 223 439 852 92 63 15 1
    2 811 893 76 27
    1 638 3
    1 940 38
    
    Expected output
    83202
    852