#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e4 + 10, maxk = 60, maxp = 1e2 + 10;
int n, k, p, a[maxn], par[maxn], dp[maxn][maxk], mx[maxk][maxp];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> k >> p;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    par[i + 1] = (par[i] + a[i]) % p;
  }
  memset(dp, -63, sizeof(dp));
  memset(mx, -63, sizeof(mx));
  dp[0][0] = 0;
  mx[0][0] = 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= k; ++j) {
      for (int t = 0; t < p; ++t)
        dp[i][j] = max(dp[i][j], mx[j - 1][t] + (par[i] - t + p) % p);
      mx[j][par[i]] = max(mx[j][par[i]], dp[i][j]);
    }
  cout << dp[n][k] << '\n';
  return 0;
}
