#include <bits/stdc++.h>
using namespace std;
const int N = 20010;
int n, k, p, a[N], f[110][55], g[N][55];
int main() {
  scanf("%d%d%d", &n, &k, &p);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), a[i] %= p;
  memset(f, -0x7f, sizeof(f));
  f[0][0] = 0;
  int pre = 0;
  for (int i = 1; i <= n; i++) {
    pre = (pre + a[i]) % p;
    for (int j = k; j; j--) {
      g[i][j] = -(1 << 30);
      for (int s = 0; s < p; s++) {
        int cur = (pre - s + p) % p;
        g[i][j] = max(g[i][j], f[cur][j - 1] + s);
      }
      f[pre][j] = max(f[pre][j], g[i][j]);
    }
  }
  printf("%d\n", g[n][k]);
  return 0;
}
