#QHD0000097. Tổng chia hết (Zero Remainder Sum)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tổng chia hết (Zero Remainder Sum)
Zero Remainder Sum
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a matrix with rows and columns. Every cell contains a positive integer . You may select some cells, but in each individual row you must select at most cells. You may select fewer, including zero. Different cells count as different choices even when they contain equal values.
Let be the sum of the values in the selected cells. Find the largest possible such that
Different rows may contribute different numbers of cells. The row limit is applied independently to every row. You may select no cells from the entire matrix; in that case , which is divisible by . Therefore the answer always exists and is nonnegative.
For instance, if , at most two cells may be selected from each row, even if selecting three would yield a larger sum. If , the row limit is zero and the answer must be regardless of the matrix entries.
Input
- The first line contains three integers , the number of rows, the number of columns, and the modulus.
- Each of the next lines contains exactly integers .
Output
Output a single integer: the maximum possible sum that is divisible by and satisfies all row limits.
Subtasks
- Subtask 1 (20 points): , .
- Subtask 2 (30 points): , .
- Subtask 3 (50 points): , .
Examples
Example 1
Input
3 4 3
1 2 3 4
5 2 2 2
7 1 1 4
Output
24
Explanation: Pick values and from row one, and one from row two, and and from row three. This chooses exactly two cells per row, as allowed by . The sum is divisible by and is optimal.
Example 2
Input
5 5 4
1 2 4 2 1
3 5 1 2 4
1 5 7 1 2
3 8 7 1 2
8 4 7 1 6
Output
56
Explanation: A row has five cells, so at most two cells may be selected from it. Combining the best per-row totals for each remainder modulo four yields the maximum feasible sum .