#include <bits/stdc++.h>
using namespace std;
const int N = 2 * 1e4 + 9;
const int mod = 2004;
const int oo = 1e9;
int n, k, md;
long long a[N], dp[N][55], tmp[101][101], suf[N];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k >> md;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    suf[i] = a[i] + suf[i - 1];
  }
  for (int i = 1; i <= n; ++i) dp[i][1] = suf[i] % md;
  for (int i = 2; i <= k; ++i) {
    for (int j = 0; j < md; ++j)
      for (int t = 0; t < md; ++t) tmp[j][t] = -oo;
    for (int j = 1; j <= n; ++j) {
      for (int t = 0; t < md; ++t)
        dp[j][i] = max(dp[j][i], tmp[(suf[j]) % md][t] + t);
      for (int t = 0; t < md; ++t)
        tmp[(suf[j] + t) % md][t] = (tmp[(suf[j] + t) % md][t] > dp[j][i - 1])
                                        ? tmp[(suf[j] + t) % md][t]
                                        : dp[j][i - 1];
    }
  }
  std::cout << dp[n][k];
}
