#P2050. [NOI2012] 美食节

[NOI2012] 美食节

Description

To welcome students from all over the country, CZ City is hosting a grand food festival.

As a foodie who loves trying new things, Xiao M would not miss this feast. He quickly tasted all the foods at the festival. However, the desire to try new dishes is hard to satisfy. Although all dishes are delicious and the chefs cook fast, Xiao M still feels it is unbearable if there is no dish on his table that has already appeared on someone else’s table. So he begins to study the problem of cooking order, that is, arranging a cooking sequence so that the students’ waiting time is minimized.

Xiao M finds that there are nn different types of dishes. Each time they order, each student may choose exactly one dish. There are mm chefs to prepare these dishes. After all students finish ordering, the dish-making tasks are assigned to the chefs. Then all chefs start cooking simultaneously. The chefs follow the required sequence and can cook only one serving at a time.

In addition, Xiao M finds another interesting fact — although the mm chefs can all make all nn types of dishes, for the same dish, different chefs may take different amounts of time. He numbers the dishes 1,2,,n1, 2, \ldots, n and the chefs 1,2,,m1, 2, \ldots, m, and lets ti,jt_{i,j} be the time for chef jj to make dish ii.

Xiao M defines the waiting time of each student as the duration from when all chefs start cooking until that student’s serving is completed. In other words, if a student’s dish is the kk-th dish made by some chef, then the student’s waiting time is the sum of the times of that chef’s first kk dishes. The total waiting time is the sum of all students’ waiting times.

Now Xiao M knows all the ordering information — there are pip_i students who ordered dish ii for i=1,2,,ni = 1, 2, \ldots, n. He wants to know the minimum possible total waiting time.

Input Format

The first line contains two positive integers nn and mm, the number of dish types and the number of chefs.

The second line contains nn positive integers; the ii-th number is pip_i, the number of students who ordered dish ii.

Then there are nn lines, each containing mm non-negative integers. In these nn lines, the jj-th number on the ii-th line is ti,jt_{i,j}, the time needed for chef jj to make dish ii.

In the input file, adjacent numbers on each line are separated by a single space, and there are no extra spaces at the end of any line.

Output Format

Output a single line containing one integer: the minimum total waiting time.

3 2 
3 1 1 
5 7 
3 6 
8 9
47

Hint

Chef 11 first makes 11 serving of dish 22, then makes 22 servings of dish 11. The waiting times of the 33 students who ordered these 33 servings are 33, 3+5=83+5=8, and 3+5+5=133+5+5=13.

Chef 22 first makes 11 serving of dish 11, then makes 11 serving of dish 33. The waiting times of the 22 students who ordered these 22 servings are 77 and 7+9=167+9=16.

The total waiting time is 3+8+13+7+16=473+8+13+7+16=47.

Although dishes 11 and 33 are faster if made by chef 11, the total waiting time would be longer if all these dishes were made by chef 11. If, as above, we reassign 11 serving of dish 11 and 11 serving of dish 33 to chef 22, then chef 22 will not be idle, and the total waiting time is shorter.

It can be proved that there is no better ordering and assignment.

For each test set, the values of nn, mm, and pp are as follows:

  • Test point 11: n=5n = 5, m=5m = 5, p=10p = 10.
  • Test point 22: n=40n = 40, m=1m = 1, p=400p = 400.
  • Test point 33: n=40n = 40, m=2m = 2, p=300p = 300.
  • Test point 44: n=40n = 40, m=40m = 40, p=40p = 40.
  • Test point 55: n=5n = 5, m=40m = 40, p=100p = 100.
  • Test point 66: n=10n = 10, m=50m = 50, p=200p = 200.
  • Test point 77: n=20n = 20, m=60m = 60, p=400p = 400.
  • Test point 88: n=40n = 40, m=80m = 80, p=600p = 600.
  • Test point 99: n=40n = 40, m=100m = 100, p=800p = 800.
  • Test point 1010: n=40n = 40, m=100m = 100, p=800p = 800.

Constraints for 100%100\% of the testdata: n40n \leq 40, m100m \leq 100, p800p \leq 800, ti,j1000t_{i,j} \leq 1000 (where p=pip = \sum p_i).

Translated by ChatGPT 5