#include <bits/stdc++.h>
using namespace std;
int N, K, P;
int A[20004];
int PS[20004];
const long long INF = 10000000000000001LL;
namespace DP {
long long solve() {
  for (int i = (1), _b = (N); i <= _b; ++i) A[i] %= P;
  for (int i = (1), _b = (N); i <= _b; ++i) PS[i] = (PS[i - 1] + A[i]) % P;
  vector<long long> DP(N + 1, -INF);
  for (int i = N; i >= 1; --i) {
    DP[i] = (PS[N] - PS[i - 1]) % P;
    if (DP[i] < 0) DP[i] += P;
  }
  for (int k = (2), _b = (K); k <= _b; ++k) {
    vector<long long> bestDP(P, -INF);
    vector<long long> newDP(N + 1, -INF);
    for (int i = N; i >= 1; --i) {
      int p = PS[i - 1];
      for (int m = 0, _n = (P); m < _n; ++m) {
        int score = (m - p) % P;
        if (score < 0) score += P;
        if (bestDP[m] > -INF) newDP[i] = max(newDP[i], bestDP[m] + score);
      }
      bestDP[p] = max(bestDP[p], DP[i]);
    }
    DP = newDP;
  }
  return DP[1];
}
}  // namespace DP
int main(int argc, char* argv[]) {
  scanf("%d %d %d", &N, &K, &P);
  for (int i = (1), _b = (N); i <= _b; ++i) scanf("%d", A + i);
  long long res = DP::solve();
  printf("%lld\n", res);
  return 0;
}
