CF BUDDY
← Problems·

1433F · Zero Remainder Sum

2100 · dp

Problem: You are given a matrix aa of size n×mn \times m consisting of integers.

You can choose no more than m2\left\lfloor\frac{m}{2}\right\rfloor elements in each row. Your task is to choose these elements in such a way that their sum is divisible by kk and this sum is the maximum.

In other words, you can choose no more than a half (rounded down) of elements in each row, you have to find the maximum sum of these elements divisible by kk.

Note that you can choose zero elements (and the sum of such set is 00).

Input Format: The first line of the input contains three integers nn, mm and kk (1n,m,k701 \le n, m, k \le 70) — the number of rows in the matrix, the number of columns in the matrix and the value of kk. The next nn lines contain mm elements each, where the jj-th element of the ii-th row is ai,ja_{i, j} (1ai,j701 \le a_{i, j} \le 70).

Output Format: Print one integer — the maximum sum divisible by kk you can obtain.

Note: In the first example, the optimal answer is 22 and 44 in the first row, 55 and 22 in the second row and 77 and 44 in the third row. The total sum is 2+4+5+2+7+4=242 + 4 + 5 + 2 + 7 + 4 = 24.

Sample Cases

Case 1

Input

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

Output

24

Case 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

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback