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

#QHD0000097. Tổng chia hết (Zero Remainder Sum)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

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 aa with nn rows and mm columns. Every cell contains a positive integer ai,ja_{i,j}. You may select some cells, but in each individual row you must select at most ⌊m/2⌋\lfloor m/2\rfloor cells. You may select fewer, including zero. Different cells count as different choices even when they contain equal values.

Let SS be the sum of the values in the selected cells. Find the largest possible SS such that

S mod k=0.S\bmod k=0.

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 S=0S=0, which is divisible by kk. Therefore the answer always exists and is nonnegative.

For instance, if m=4m=4, at most two cells may be selected from each row, even if selecting three would yield a larger sum. If m=1m=1, the row limit is zero and the answer must be 00 regardless of the matrix entries.

Input

  • The first line contains three integers n,m,kn,m,k, the number of rows, the number of columns, and the modulus.
  • Each of the next nn lines contains exactly mm integers ai,ja_{i,j}.

Output

Output a single integer: the maximum possible sum that is divisible by kk and satisfies all row limits.

Subtasks

  • Subtask 1 (20 points): 1≤n,m,k≤51\le n,m,k\le5, 1≤ai,j≤101\le a_{i,j}\le10.
  • Subtask 2 (30 points): 1≤n,m,k≤201\le n,m,k\le20, 1≤ai,j≤301\le a_{i,j}\le30.
  • Subtask 3 (50 points): 1≤n,m,k≤701\le n,m,k\le70, 1≤ai,j≤701\le a_{i,j}\le70.

Examples

Example 1

Input

3 4 3
1 2 3 4
5 2 2 2
7 1 1 4

Output

24

Explanation: Pick values 22 and 44 from row one, 55 and one 22 from row two, and 77 and 44 from row three. This chooses exactly two cells per row, as allowed by ⌊4/2⌋=2\lfloor4/2\rfloor=2. The sum 2+4+5+2+7+4=242+4+5+2+7+4=24 is divisible by 33 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 5656.