#include <bits/stdc++.h>
using namespace std;
int n, m, k, s[20005], a;
int dp[55][105];
int dp1[55][105];
int main() {
  scanf("%d%d%d", &n, &k, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a);
    s[i] = (s[i - 1] + a) % m;
  }
  int sol = INT_MIN;
  for (int i = 0; i < 55; ++i)
    for (int j = 0; j < 105; ++j) dp[i][j] = INT_MIN;
  dp[0][0] = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < 55; ++j)
      for (int k1 = 0; k1 < 105; ++k1) dp1[j][k1] = INT_MIN;
    for (int j = 0; j < k; ++j)
      for (int x = 0; x < m; ++x) {
        dp1[j + 1][s[i]] = max(dp1[j + 1][s[i]], dp[j][x] + (m + s[i] - x) % m);
        if (j == k - 1 && i == n) sol = max(sol, dp1[j + 1][s[i]]);
      }
    for (int j = 0; j < 55; ++j) {
      for (int k1 = 0; k1 < 105; ++k1) {
        dp[j][k1] = max(dp[j][k1], dp1[j][k1]);
      }
    }
  }
  printf("%d\n", sol);
  return 0;
}
