#include <bits/stdc++.h>
using namespace std;
int n, g, mod, x, sum[20005], tmp[20005];
int dp[20005][55];
int main() {
  scanf("%d%d%d", &n, &g, &mod);
  for (register int i = 1; i <= n; i++) {
    dp[i][0] = -0x3f3f3f3f;
  }
  for (register int i = 1; i <= n; i++) {
    scanf("%d", &x);
    sum[i] = sum[i - 1] + x;
    sum[i] %= mod;
  }
  for (register int i = 1; i <= g; i++) {
    memset(tmp, -0x3f, sizeof(tmp));
    if (i == 1) {
      tmp[0] = 0;
    }
    for (register int j = 1; j <= n; j++) {
      dp[j][i] = -0x3f;
      for (register int k = 0; k < mod; k++) {
        dp[j][i] = (dp[j][i] > (sum[j] - k + mod) % mod + tmp[k]
                        ? dp[j][i]
                        : (sum[j] - k + mod) % mod + tmp[k]);
      }
      tmp[sum[j]] = (tmp[sum[j]] > dp[j][i - 1] ? tmp[sum[j]] : dp[j][i - 1]);
    }
  }
  printf("%d\n", dp[n][g]);
  return 0;
}
