#include <bits/stdc++.h>
using namespace std;
const long long N = 20010, M = 55;
inline long long read() {
  long long s = 0, w = 1;
  register char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();
  return s * w;
}
long long n, K, P, a[N], F[M][N], G[M * 2], qz[N];
signed main() {
  n = read(), K = read(), P = read();
  for (register long long i = 1; i <= n; i++)
    a[i] = read(), qz[i] = (qz[i - 1] + a[i]) % P;
  memset(F[0], -0x3f, sizeof(F[0])), F[0][0] = 0;
  const long long INF = F[0][1];
  for (register long long i = 1; i <= K; i++) {
    memset(G, -0x3f, sizeof(G));
    for (register long long j = 0; j <= n; j++) {
      F[i][j] = INF;
      for (register long long k = 0; k < P; k++) {
        if (G[k] == INF) continue;
        F[i][j] = max(F[i][j], G[k] + (qz[j] - k + P) % P);
      }
      G[qz[j]] = max(G[qz[j]], F[i - 1][j]);
    }
  }
  printf("%lld\n", F[K][n]);
  return 0;
}
