Coinful Escapade
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 .
Given a row of coins with respective values , your task is to help Captain Noir choose a subset (without picking adjacent coins) so that the sum, , of the chosen coins satisfies . Of all such possible selections, determine the maximum achievable sum. If it is impossible to obtain a sum divisible by , output .
Input Format
- The first line contains two space-separated integers and , where and .
- The second line contains space-separated positive integers , each representing the value of a coin. It is guaranteed that for each valid .
Output Format
Output a single integer: the maximum sum obtainable by picking non-adjacent coins such that the sum is divisible by . If no valid selection exists, print .
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.