#P1130. 红牌

红牌

Description

To obtain long-term residency, temporary residents must apply for a red card. The process consists of NN steps. Each step is handled by a government staff member who checks whether your submitted materials meet the requirements. To accelerate the process, at each step the government assigns MM staff members to check materials. Unfortunately, not every staff member is efficient. Nevertheless, to embody the policy of “open government,” the department publicly releases the number of days each staff member takes to handle one application.

To prevent all applicants from flocking to the most efficient staff, these M×NM \times N staff members are divided into MM groups. Each group has exactly one staff member at each step. An applicant may choose any one group and may also switch groups. However, switching is strictly limited: it must occur between two adjacent steps, not during a step that has already started but not yet finished; moreover, you can only switch from your current group II to group I+1I+1, and from group MM you may switch to group 11. There is no limit on the number of switches.

For example, here are 33 groups, with working days for 44 steps per group:

  • Group 11: 2,6,1,82, 6 ,1 ,8;
  • Group 22: 3,6,2,63,6, 2, 6;
  • Group 33: 4,2,3,6 4, 2 ,3 ,6.

In this example, you can choose group 11 for the entire process, which takes 2+6+1+8=172+6+1+8=17 days. Alternatively, you may start with group 22 for step one, then switch to group 33 for step two, to group 11 for step three, and to group 22 for step four, for a total of 3+2+1+6=123+2+1+6=12 days. You can see that no choice is more efficient than this.

Your task is to find the minimum number of days needed to complete the application.

Input Format

The first line contains two positive integers NN and MM, the number of steps and the number of groups.

Then there are MM lines, each containing NN non-negative integers. On the ii-th of these lines (1iM)(1 \le i \le M), the jj-th number denotes the number of days group ii needs to complete step jj. All day counts do not exceed 10000001000000.

Output Format

Output a single positive integer, the minimum number of days required to complete all steps.

4 3 
2 6 1 8
3 6 2 6
4 2 3 6

12

Hint

Constraints: For 100%100\% of the testdata, 1N,M20001 \le N,M \le 2000.

Translated by ChatGPT 5