Problem: Rebel spy Heidi has just obtained the plans for the Death Star from the Empire and, now on her way to safety, she is trying to break the encryption of the plans (of course they are encrypted – the Empire may be evil, but it is not stupid!). The encryption has several levels of security, and here is how the first one looks.

Heidi is presented with a screen that shows her a sequence of integers A and a positive integer p. She knows that the encryption code is a single number S, which is defined as follows:

Define the score of X to be the sum of the elements of X modulo p.

Heidi is given a sequence A that consists of N integers, and also given an integer p. She needs to split A into 2 parts such that: 

  * Each part contains at least 1 element of A, and each part consists of contiguous elements of A. 
  * The two parts do not overlap. 
  * The total sum S of the scores of those two parts is maximized. This is the encryption code. 



Output the sum S, which is the encryption code.

Input

The first line of the input contains two space-separated integer N and p (2 ≤ N ≤ 100 000, 2 ≤ p ≤ 10 000) – the number of elements in A, and the modulo for computing scores, respectively.

The second line contains N space-separated integers which are the elements of A. Each integer is from the interval [1, 1 000 000].

Output

Output the number S as described in the problem statement.

Examples

Input

4 10
3 4 7 2


Output

16


Input

10 12
16 3 24 13 9 8 7 5 12 12


Output

13

Note

In the first example, the score is maximized if the input sequence is split into two parts as (3, 4), (7, 2). It gives the total score of <image>.

In the second example, the score is maximized if the first part consists of the first three elements, and the second part consists of the rest. Then, the score is <image>.

Solution (in JAVA):

Solution:
import java.util.*;
import java.io.*;
import java.math.*;
import java.util.HashMap;

public class Main 
{
    static class Reader 
    { 
        private InputStream mIs;private byte[] buf = new byte[1024];private int curChar,numChars;public Reader() { this(System.in); }public Reader(InputStream is) { mIs = is;} 
        public int read() {if (numChars == -1) throw new InputMismatchException();if (curChar >= numChars) {curChar = 0;try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();}if (numChars <= 0) return -1; }return buf[curChar++];} 
        public String nextLine(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isEndOfLine(c));return res.toString() ;} 
        public String s(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isSpaceChar(c));return res.toString();} 
        public long l(){int c = read();while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }long res = 0; do{ if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read();}while(!isSpaceChar(c));return res * sgn;} 
        public int i(){int c = read() ;while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }int res = 0;do{if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read() ;}while(!isSpaceChar(c));return res * sgn;} 
        public double d() throws IOException {return Double.parseDouble(s()) ;}
        public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } 
        public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } 
    } 
    
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
    static int n;
    static int k;
    static int mod;
    static int cache[][][]=new int[53][101][20001];
    static int arr[];
    /*public static int dp(int taken,int sum,int pos)
    {
        if(pos==n-1)
        {
            if(taken!=k-1)
            {
                return -1000000000;
            }
            return (sum+arr[pos])%mod;
        }
        if(cache[taken][sum][pos]!=-1)
        return cache[taken][sum][pos];
        if(taken==k-1)
        return cache[taken][sum][pos]=dp(taken,(sum+arr[pos])%mod,pos+1);
        return cache[taken][sum][pos]=Math.max((sum+arr[pos])%mod+dp(taken+1,0,pos+1),dp(taken,(sum+arr[pos])%mod,pos+1));
    }*/
    public static void main(String args[])
    {
        Reader sc=new Reader();
        PrintWriter out=new PrintWriter(System.out);
        n =sc.i();
        k=sc.i();
        mod=sc.i();
        arr=new int[n];
        for(int i=0;i<n;i++)
        arr[i]=sc.i();
        for(int taken=0;taken<53;taken++)
        {
            for(int sum=0;sum<101;sum++)
            {
                if(taken!=k-1)
                cache[taken][sum][n-1]=-1000000000;
                else
                cache[taken][sum][n-1]=(sum+arr[n-1])%mod;
            }
        }
        for(int pos=n-2;pos>=0;pos--)
        {
            for(int taken=0;taken<52;taken++)
            {
                for(int sum=0;sum<101;sum++)
                {
                        if(taken==k-1)
                        cache[taken][sum][pos]=cache[taken][(sum+arr[pos])%mod][pos+1];
                        else
                        cache[taken][sum][pos]=Math.max((sum+arr[pos])%mod+cache[taken+1][0][pos+1],cache[taken][(sum+arr[pos])%mod][pos+1]);
                    
                }
            }
                    
       }
        System.out.println(cache[0][0][0]);
    }
}