#include <bits/stdc++.h>
using namespace std;
const int maxn = int(2e4) + 5, maxk = 55, inf = int(1e9) + 5;
int A[maxn], P[maxn], mxval[maxn], dp[maxk][maxn];
inline int INTMOD(int a, int b) { return ((a % b) + b) % b; }
int main(void) {
  int n, k, p;
  scanf("%d%d%d", &n, &k, &p);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &A[i]);
    P[i] = (A[i] % p + P[i - 1]) % p;
  }
  for (int i = 1; i < n; i++) dp[1][i] = INTMOD(P[n] - P[i - 1], p);
  for (int kk = 2; kk <= k; kk++) {
    for (int i = 0; i < p; i++) mxval[i] = -inf;
    mxval[P[n - kk + 1]] = dp[kk - 1][n - kk + 2];
    for (int i = n - kk + 1; i >= 0; i--) {
      for (int j = 0; j < p; j++)
        dp[kk][i] = max(dp[kk][i], mxval[j] + INTMOD(j - P[i - 1], p));
      mxval[P[i - 1]] = max(mxval[P[i - 1]], dp[kk - 1][i]);
    }
  }
  printf("%d\n", dp[k][1]);
}
