Coinful Escapade

Time limit: 2 secMemory limit: 256 MBMath, Dynamic Programming

Problem Statement

Captain Noir is on the run after his latest daring heist, and fate has lined his escape route with a row of shining coins. Rumor has it that the coins are cursed—if two adjacent coins are picked, an alarm will sound. Ever the risk-taker, Noir devises a meticulous plan: he must select a subset of coins such that no two are consecutive. However, there’s an extra twist imposed by an ancient decree: the total value of the stolen coins must be divisible by an integer kk.

Given a row of nn coins with respective values c1,c2,,cnc_1, c_2, \dots, c_n, your task is to help Captain Noir choose a subset (without picking adjacent coins) so that the sum, SS, of the chosen coins satisfies S0(modk)S \equiv 0 \pmod{k}. Of all such possible selections, determine the maximum achievable sum. If it is impossible to obtain a sum divisible by kk, output 1-1.

Input Format

  • The first line contains two space-separated integers nn and kk, where 1n1051 \le n \le 10^5 and 1k1001 \le k \le 100.
  • The second line contains nn space-separated positive integers c1,c2,,cnc_1, c_2, \dots, c_n, each representing the value of a coin. It is guaranteed that 1ci1091 \le c_i \le 10^9 for each valid ii.

Output Format

Output a single integer: the maximum sum obtainable by picking non-adjacent coins such that the sum is divisible by kk. If no valid selection exists, print 1-1.

Examples

Input

5 3
3 1 4 1 5

Output

12

Input

4 5
2 3 2 3

Output

5

Code Editor

Submit a solution. Authentication is required to create submissions.

Language
Theme
Source code is submitted to the judge for testing.