#include <bits/stdc++.h>
using namespace std;
const int N = 20004;
const int K = 54;
const int P = 104;
int dp[2][K][P];
int v[N];
int n, k, p, visid;
int f(int x, int y) { return (x + y) % p; }
int main() {
  visid++;
  scanf("%d %d %d", &n, &k, &p);
  for (int i = 0; i < int(n); i++) scanf("%d", &v[i]);
  for (int i = 0; i < int(n); i++) v[i] %= p;
  for (int parts = int(k); parts >= 0; parts--)
    for (int sum = 0; sum < int(p); sum++) dp[n & 1][parts][sum] = -6000;
  dp[n & 1][k][0] = 0;
  for (int idx = int(n - 1); idx >= 0; idx--) {
    for (int parts = int(k); parts >= 0; parts--) {
      for (int sum = 0; sum < int(p); sum++) {
        dp[idx & 1][parts][sum] = max(
            dp[(idx + 1) & 1][parts][f(sum, v[idx])],
            (parts + 1 <= k) ? dp[(idx + 1) & 1][parts + 1][0] + f(sum, v[idx])
                             : -6000);
      }
    }
  }
  printf("%d\n", dp[0][0][0]);
  return 0;
}
