#include <bits/stdc++.h>
using namespace std;
int dp[110][60];
int tmp[60];
int s[20007];
const int INF = 0x3f3f3f3f;
int main() {
  int n, m, mo;
  scanf("%d %d %d", &n, &m, &mo);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    s[i] = (s[i - 1] + x) % mo;
  }
  for (int i = 0; i < mo; i++) {
    for (int j = 0; j <= m; j++) {
      dp[i][j] = -INF;
    }
  }
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) tmp[j] = -INF;
    for (int j = 1; j <= m; j++) {
      for (int k = 0; k < mo; k++) {
        tmp[j] = max(tmp[j], dp[k][j - 1] + (s[i] - k + mo) % mo);
      }
    }
    for (int j = 1; j <= m; j++) dp[s[i]][j] = max(dp[s[i]][j], tmp[j]);
  }
  printf("%d\n", dp[s[n]][m]);
  return 0;
}
