#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
long long mpow(long long a, long long b, long long p = MOD) {
  a = a % p;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = (res * a) % p;
    a = (a * a) % p;
    b = b >> 1;
  }
  return res % p;
}
const long long N = 2e4 + 3;
long long n, p, k;
long long a[N], pref[N], dp[N][55];
long long orz[102][55];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> k >> p;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (long long i = 1; i <= n; i++) {
    pref[i] = pref[i - 1] + a[i];
    pref[i] %= p;
  }
  for (long long j = 0; j < p; j++) {
    for (long long l = 0; l <= k; l++) {
      orz[j][l] = -1e12;
    }
  }
  for (long long i = 0; i <= n; i++)
    for (long long j = 0; j <= k; j++) dp[i][j] = -1e12;
  orz[0][0] = 0;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 0; j < p; j++) {
      for (long long l = 1; l <= k; l++) {
        dp[i][l] = max(dp[i][l], (pref[i] - j + p) % p + orz[j][l - 1]);
      }
    }
    for (long long l = 0; l <= k; l++) {
      orz[pref[i]][l] = max(orz[pref[i]][l], dp[i][l]);
    }
  }
  cout << dp[n][k];
}
