PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#QHD0000019. Định giá vé máy bay (Plane Ticket Pricing)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Định giá vé máy bay (Plane Ticket Pricing)

Plane Ticket Pricing

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

An airline has NN unsold seats for a flight departing in WW weeks. It may set a ticket price each week and wants to maximize revenue by departure.

Each of the W+1W+1 pricing decisions offers several prices with associated demand. Choosing price pp with demand ss sells min⁡(s,r)\min(s,r) tickets if rr seats remain, and unsold seats carry over to the following week.

Find the maximum total revenue. Among all optimal strategies, report the smallest possible price at the first pricing decision.

Input

The first line contains NN and WW.

The next W+1W+1 blocks describe weeks WW down to 00. Each block starts with KK, the number of price options, followed by a line of KK increasing prices p1,…,pKp_1,\ldots,p_K and a line of KK corresponding demands s1,…,sKs_1,\ldots,s_K.

Output

Print the maximum revenue on the first line.

Print the smallest optimal first-week price on the second line.

Subtasks

  • Subtask 1 (20 points): 1≤N≤151\le N\le 15; 0≤W≤520\le W\le 52; 1≤K≤1001\le K\le 100; 1≤p1<⋯<pK≤9991\le p_1<\cdots<p_K\le 999; 0≤si≤N0\le s_i\le N; There are W+1W+1 pricing decisions and each week’s prices are increasing.
  • Subtask 2 (30 points): 1≤N≤901\le N\le 90; 0≤W≤520\le W\le 52; 1≤K≤1001\le K\le 100; 1≤p1<⋯<pK≤9991\le p_1<\cdots<p_K\le 999; 0≤si≤N0\le s_i\le N; There are W+1W+1 pricing decisions and each week’s prices are increasing.
  • Subtask 3 (50 points): 1≤N≤3001\le N\le 300; 0≤W≤520\le W\le 52; 1≤K≤1001\le K\le 100; 1≤p1<⋯<pK≤9991\le p_1<\cdots<p_K\le 999; 0≤si≤N0\le s_i\le N; There are W+1W+1 pricing decisions and each week’s prices are increasing.

Examples

Input

50 2
1 437 47
3 357 803 830 13 45 46
1 611 14

Output

23029
437

Explanation

There are 5050 seats and three pricing decisions because W=2W=2. The first week offers price 437437 with demand 4747. Later prices depend on the remaining seats. The first week sells 4747 seats at 437437, earning 47×437=2053947\times437=20539, with 33 seats remaining. In the next week all 33 can be sold at 830830, earning another 24902490. Total revenue is 20539+2490=2302920539+2490=23029, and the first-week price is 437437. The output contains these values on separate lines.