Universal Cup Judging System

Universal Cup

시간 제한: 3.0 s 메모리 제한: 256 MB 총점: 100 해킹 가능 ✓
통계

As you might know, the best way to come up with problems is misreading statements. So here you have one problem created in such a way.

There are $m$ stones and $k$ of their distributions into $n$ boxes. Each distribution of stones can be represented as an array of non-negative integers $a$ such that $a_1 + \dots + a_n = m$. Alice will construct a distribution of stones $b$ for which she will define $\operatorname{cost}(b, a)$ as the minimal number of operations described below needed to make $a$ equal to $b$:

  • Take $1$ stone from box $i$ ($1 \le i \le n$) with a positive number of stones and put it in box $j$ ($1 \le j \le n$).

She wants to find the minimal sum of costs for the optimal distribution $b$ to all given distributions.

Input

First line contains $3$ integers $n, m, k$ ($1 \le n, k \le 400, 1 \le m \le 10^9$) — number of boxes, stones, and distributions.

Each of the next $k$ lines contains $n$ non-negative integers $a_{i,j}$ ($a_{i,1} + \dots + a_{i,n} = m$) — description of the distributions.

Output

Output a single integer — answer to the problem.

Examples

Sample Input 1

5 12 3
3 0 4 1 4
5 2 3 1 1
1 2 3 5 1

Sample Output 1

8

Sample Input 2

1 1 2
1
1

Sample Output 2

0

Note

For the first sample case, one possible optimal distribution is $b = \{3, 2, 3, 2, 2\}$. For this distribution $b$ we have:

  • For $a = \{3, 0, 4, 1, 4\}$ we have $\operatorname{cost}(b, a) = 3$ since we can apply operations in the following order: $\{3, 0, 4, 1, 4\} \to \{3, 1, 4, 1, 3\} \to \{3, 2, 4, 1, 2\} \to \{3, 2, 3, 2, 2\}$.
  • For $a = \{5, 2, 3, 1, 1\}$ we have $\operatorname{cost}(b, a) = 2$ since we can apply operations in the following order: $\{5, 2, 3, 1, 1\} \to \{4, 2, 3, 1, 2\} \to \{3, 2, 3, 2, 2\}$.
  • For $a = \{1, 2, 3, 5, 1\}$ we have $\operatorname{cost}(b, a) = 3$ since we can apply operations in the following order: $\{1, 2, 3, 5, 1\} \to \{1, 2, 3, 4, 2\} \to \{2, 2, 3, 3, 2\} \to \{3, 2, 3, 2, 2\}$.

So the sum of costs is $8$. It can be shown that it is not possible to obtain a smaller sum of costs.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#371EditorialOpen题解jianglyView

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.