#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 20000;
const int MAXPARTS = 50;
const int MAXMOD = 100;
int n, MOD, nparts;
int a[MAXN];
int dp[2][MAXPARTS][MAXMOD];
void upd(int &a, int b) { a = max(a, b); }
void run() {
  scanf("%d%d%d", &n, &nparts, &MOD);
  for (int i = (0); i < (n); ++i) scanf("%d", &a[i]);
  int at = 0;
  memset(dp[at], -1, sizeof(dp[at]));
  dp[at][0][0] = 0;
  for (int i = (0); i < (n); ++i) {
    memset(dp[1 - at], -1, sizeof(dp[1 - at]));
    for (int j = (0); j < (nparts); ++j)
      for (int k = (0); k < (MOD); ++k)
        if (dp[at][j][k] != -1) {
          int nk = (k + a[i]) % MOD;
          upd(dp[1 - at][j][nk], dp[at][j][k]);
          if (j + 1 < nparts) upd(dp[1 - at][j + 1][0], dp[at][j][k] + nk);
        }
    at = 1 - at;
  }
  int ret = 0;
  for (int k = (0); k < (MOD); ++k)
    if (dp[at][nparts - 1][k] != -1) upd(ret, dp[at][nparts - 1][k] + k);
  printf("%d\n", ret);
}
int main() {
  run();
  return 0;
}
