#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream& operator<<(ostream& stream, const vector<T> v) {
  stream << "[ ";
  for (int i = 0; i < (int)v.size(); i++) stream << v[i] << " ";
  stream << "]";
  return stream;
}
long long fpow(long long x, long long p, long long m) {
  long long r = 1;
  for (; p; p >>= 1) {
    if (p & 1) r = r * x % m;
    x = x * x % m;
  }
  return r;
}
long long inv(long long a, long long b) {
  return a < 2 ? a : ((a - inv(b % a, a)) * b + 1) / a % b;
}
int gcd(int a, int b) {
  if (!b) return a;
  return gcd(b, a % b);
}
long long gcd(long long a, long long b) {
  if (!b) return a;
  return gcd(b, a % b);
}
int N, k, p, a[20100], dp[2][52][102];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> k >> p;
  int i, j, m;
  for (i = 1; i <= N; i++) {
    cin >> a[i];
    a[i] %= p;
  }
  for (i = 1; i <= k; i++)
    for (j = 0; j < p; j++) dp[0][i][j] = -(1 << 30);
  bool l = 0;
  for (i = 1; i <= N; i++) {
    l = !l;
    for (j = 1; j <= k; j++)
      for (m = 0; m < p; m++) {
        dp[l][j][m] = dp[!l][j][(m + a[i - 1]) % p];
        if (j > 1 || (i == 1))
          dp[l][j][m] = max(dp[l][j][m], dp[!l][j - 1][a[i - 1]] + m);
      }
  }
  cout << dp[l][k][a[N]] << "\n";
}
