#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
double fRand(double fMin, double fMax) {
  double f = (double)rand() / RAND_MAX;
  return fMin + f * (fMax - fMin);
}
template <class T>
T min(T a, T b, T c) {
  return min(a, min(b, c));
}
template <class T>
T max(T a, T b, T c) {
  return max(a, max(b, c));
}
int n, k, p, a[20005], dp[55][20005], maxDP[105];
int main() {
  scanf("%d%d%d", &n, &k, &p);
  for (int i = (1); i <= (n); ++i) scanf("%d", &a[i]);
  dp[0][0] = 0;
  for (int i = (1); i <= (n); ++i) dp[0][i] = -1000000007;
  for (int j = (1); j <= (k); ++j) {
    for (int x = (0); x <= (p - 1); ++x) maxDP[x] = -1000000007;
    maxDP[0] = dp[j - 1][0];
    dp[j][0] = -1000000007;
    int sum = 0;
    for (int i = (1); i <= (n); ++i) {
      sum = (sum + a[i]) % p;
      dp[j][i] = -1000000007;
      for (int x = (0); x <= (p - 1); ++x)
        dp[j][i] = max(dp[j][i], maxDP[x] + (sum - x + p) % p);
      maxDP[sum] = max(maxDP[sum], dp[j - 1][i]);
    }
  }
  printf("%d", dp[k][n]);
  return 0;
}
