#P8663. [蓝桥杯 2018 省 A] 倍数问题

[蓝桥杯 2018 省 A] 倍数问题

Problem Description

As everyone knows, student Xiaocong is good at calculations, especially at checking whether one number is a multiple of another. However, Xiaocong is only good at the case with two numbers, and becomes troubled when there are many numbers. Now Xiaocong gives you nn numbers and hopes that you can find three numbers among them such that the sum of these three numbers is a multiple of KK, and this sum is as large as possible. The testdata guarantees that a solution always exists.

Input Format

Read input from standard input.

The first line contains 22 positive integers, nn and KK.

The second line contains nn positive integers, representing the given nn numbers.

Output Format

Output one line containing one integer, the required sum.

4 3
1 2 3 4
9

Hint

Sample Explanation

Choose 22, 33, and 44.

Constraints

For 30%30\% of the testdata, n100n \le 100.

For 60%60\% of the testdata, n1000n \le 1000.

For the other 20%20\% of the testdata, K10K \le 10.

For 100%100\% of the testdata, 1n1051 \le n \le 10^5, 1K1031 \le K \le 10^3, and each of the given nn numbers does not exceed 10810^8.

Time limit: 1 second, 256 MB. Lanqiao Cup 2018, 9th Provincial Contest.

Translated by ChatGPT 5