#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
template <typename T>
inline void Min(T &x, T y) {
  if (x > y) x = y;
}
template <typename T>
inline void Max(T &x, T y) {
  if (x < y) x = y;
}
const int maxn = 2e4 + 10;
const int maxk = 55;
const int maxp = 105;
int n, k, p, a[maxn];
int f[maxn][maxk], nxt[maxn][maxp], sum[maxn], nw[maxp];
int main() {
  n = read(), k = read(), p = read();
  for (int i = 1; i <= n; i++) a[i] = read(), sum[i] = (sum[i - 1] + a[i]) % p;
  for (int i = 0; i < p; i++) nw[i] = n + 1;
  for (int i = n; i >= 1; i--) {
    for (int j = 0; j < p; j++) {
      nxt[i][j] = nw[j];
    }
    nw[sum[i]] = i;
  }
  for (int i = 1; i <= n; i++) f[i][1] = sum[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < min(i + 1, k); j++) {
      for (int q = 0; q < p; q++) {
        int cur = nxt[i][(q + sum[i]) % p];
        if (cur == n + 1)
          continue;
        else {
          Max(f[cur][j + 1], f[i][j] + q);
        }
      }
    }
  }
  int ans = 0, S = 0;
  for (int i = n; i >= 1; i--) {
    if (S) break;
    Max(ans, f[i][k]);
    (S += a[i]) %= p;
  }
  cout << ans << endl;
  return 0;
}
