#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void chkmax(T &x, T y) {
  if (x < y) x = y;
}
template <class T>
inline void chkmin(T &x, T y) {
  if (x > y) x = y;
}
int n, k, p, a, dp[2][111], mini[2][111];
long long s;
bool b;
int re(int i) { return i % 2; }
int main() {
  scanf("%d %d %d", &n, &k, &p);
  for (int i = 1; i <= n; i++, b ^= 1) {
    scanf("%d", &a), a %= p, s += a;
    dp[b][1] = mini[b][1] = s % p;
    if (i <= k) dp[b][i] = s, mini[b][i] = a;
    for (int j = 2; j <= k && j < i; j++) {
      int s1 = dp[b ^ 1][j - 1], s2 = dp[b ^ 1][j];
      if (s1 < s2) {
        if (mini[b ^ 1][j] + a < p)
          dp[b][j] = s2 + a, mini[b][j] = a + mini[b ^ 1][j];
        else
          dp[b][j] = s2 + a - p, mini[b][j] = a + mini[b ^ 1][j] - p;
      } else
        dp[b][j] = s1 + a, mini[b][j] = a;
    }
  }
  printf("%d\n", dp[b ^ 1][k]);
  return 0;
}
