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.io.*;
import java.util.*;

public class C2 implements Runnable{
	public static void main (String[] args) {new Thread(null, new C2(), "_cf", 1 << 28).start();}

	int n, k, p, oo = (int)2e9;
	int[][][][] dp;
	int[] a;

	public void run() {
		DeoxysS fs = new DeoxysS();
		PrintWriter out = new PrintWriter(System.out);
		System.err.println("Go!");

		n = fs.nextInt();
		k = fs.nextInt();
		p = fs.nextInt();
		int[][][] dp = new int[2][k][p];
		for(int i = 0; i < 2; i++) {
			for(int j = 0; j < k; j++) {
				for(int ii = 0; ii < p; ii++) {
					dp[i][j][ii] = -1;
				}
			}
		}
		dp[0][0][0] = 0;
		for(int idx = 0; idx < n; idx++) {
			int v = fs.nextInt();
			
			int[][][] ndp = new int[2][k][p];
			for(int i = 0; i < 2; i++) {
				for(int j = 0; j < k; j++) {
					for(int ii = 0; ii < p; ii++) {
//						ndp[i][j][ii] = dp[i][j][ii];
						ndp[i][j][ii] = -1;
					}
				}
			}
			
//			System.out.println("Working " + (idx + 1) + " " + v);
			
			for(int i = 0; i < 2; i++) {
				for(int j = 0; j < k; j++) {
					for(int ii = 0; ii < p; ii++) {
						if(dp[i][j][ii] == -1) continue;
						//consider adding v to this part
						//or add v to the next part
						
//						System.out.printf("%d %d %d = %d\n", i, j, ii, dp[i][j][ii]);
						
						//this part
						int nsum = (ii + v) % p;
						ndp[1][j][nsum] = Math.max(ndp[1][j][nsum], dp[i][j][ii] - ii + nsum);
						
						//next part
						if(j < k - 1 && i > 0) {
							ndp[1][j+1][v%p] = Math.max(ndp[1][j+1][v%p], dp[i][j][ii] + (v % p));
						}
						
					}
				}
			}
			
			dp = ndp;
		}
		
		int res = -oo;
		for(int i = 0; i < p; i++) res = Math.max(res, dp[1][k-1][i]);
		out.println(res);

		out.close();
	}

	int solve(int nonzero, int part, int sum, int at) {
		if(at == n) {
			if(part != k - 1) {
				return -oo;
			}
			return sum;
		}
		int ret = dp[nonzero][part][sum][at];
		if(ret != -1) return ret;

		int res = -oo;
		if(nonzero > 0 && part < k - 1) {
			int go = solve(0, part + 1, 0, at);
			if(go != -oo) res = Math.max(res, go + sum);
		}
		int go = solve(1, part, (sum + a[at]) % p, at + 1);
		if(go != -oo) res = Math.max(res, go);
		
		return dp[nonzero][part][sum][at] = res;
	}

	void sort (int[] a) {
		int n = a.length;
		for(int i = 0; i < 50; i++) {
			Random r = new Random();
			int x = r.nextInt(n), y = r.nextInt(n);
			int temp = a[x];
			a[x] = a[y];
			a[y] = temp;
		}
		Arrays.sort(a);
	}

	class DeoxysS {
		public int BS = 1<<16;
		public char NC = (char)0;
		byte[] buf = new byte[BS];
		int bId = 0, size = 0;
		char c = NC;
		double num = 1;
		BufferedInputStream in;

		public DeoxysS() {
			in = new BufferedInputStream(System.in, BS);
		}

		public DeoxysS(String s) throws FileNotFoundException {
			in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
		}

		public char nextChar(){
			while(bId==size) {
				try {
					size = in.read(buf);
				}catch(Exception e) {
					return NC;
				}                
				if(size==-1)return NC;
				bId=0;
			}
			return (char)buf[bId++];
		}

		public int nextInt() {
			return (int)nextLong();
		}

		public long nextLong() {
			num=1;
			boolean neg = false;
			if(c==NC)c=nextChar();
			for(;(c<'0' || c>'9'); c = nextChar()) {
				if(c=='-')neg=true;
			}
			long res = 0;
			for(; c>='0' && c <='9'; c=nextChar()) {
				res = (res<<3)+(res<<1)+c-'0';
				num*=10;
			}
			return neg?-res:res;
		}

		public double nextDouble() {
			double cur = nextLong();
			return c!='.' ? cur:cur+nextLong()/num;
		}

		public String next() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c>32) {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}

		public String nextLine() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c!='\n') {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}

		public boolean hasNext() {
			if(c>32)return true;
			while(true) {
				c=nextChar();
				if(c==NC)return false;
				else if(c>32)return true;
			}
		}

		public int[] nextIntArray(int n) {
			int[] res = new int[n];
			for(int i = 0; i < n; i++) res[i] = nextInt();
			return res;
		}

	}

}