#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, flag = 1;
  char ch = getchar();
  for (; ch > '9' || ch < '0'; ch = getchar())
    if (ch == '-') flag = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar())
    x = (x << 3) + (x << 1) + ch - '0';
  return x * flag;
}
const long long MAXN = 5e5 + 50;
long long n, m, p;
long long A[MAXN], sum[MAXN], dp[MAXN][2];
long long q[105];
signed main() {
  n = read(), m = read(), p = read();
  for (long long i = 1; i <= n; i++) A[i] = read(), sum[i] = sum[i - 1] + A[i];
  for (long long i = 1; i <= n; i++) dp[i][1] = sum[i] % p;
  dp[0][1] = -1e15;
  long long now = 1;
  for (long long j = 2; j <= m; j++) {
    memset(q, 0, sizeof(q));
    now ^= 1ll;
    dp[0][now ^ 1ll] = -1e15;
    for (long long i = 1; i <= n; i++) {
      dp[i][now] = 0;
      long long Q = sum[i] % p;
      for (long long k = 0; k <= p - 1; k++)
        dp[i][now] = max(dp[q[(Q - k + p) % p]][now ^ 1ll] + k, dp[i][now]);
      q[sum[i] % p] = i;
    }
  }
  cout << dp[n][now];
  return 0;
}
