#include <bits/stdc++.h>
using namespace std;
void see(int x) { cout << x; }
void see(long long int x) { cout << x; }
void see(long double x) { cout << x; }
void see(char x) { cout << '\'' << x << '\''; }
void see(string x) { cout << '\"' << x << '\"'; }
template <typename T, typename V>
void see(const pair<T, V> &x) {
  cout << '{';
  see(x.first);
  cout << ',';
  see(x.second);
  cout << '}';
}
template <typename T>
void see(const T &x) {
  int f = 0;
  cout << '{';
  for (auto &i : x) cout << (f++ ? "," : ""), see(i);
  cout << "}";
}
void _print() { cout << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  see(t);
  if (sizeof...(v)) cout << ", ";
  _print(v...);
}
long long int gcd(long long int a, long long int b) {
  return (!b) ? a : gcd(b, a % b);
}
const int M = 2e4 + 5;
int N, K, P;
int a[M], dp[M][55], md[105];
int inf = 1e6;
void solve() {
  cin >> N >> K >> P;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
    dp[i][0] = -inf;
    a[i] = (a[i - 1] + a[i]) % P;
  }
  dp[0][0] = 0;
  for (int k = 1; k <= K; k++) {
    for (int i = 0; i <= N; i++) dp[i][k] = -inf;
    for (int i = 0; i <= P - 1; i++) md[i] = -inf;
    for (int i = 1; i <= N; i++) {
      md[a[i - 1]] = max(md[a[i - 1]], dp[i - 1][k - 1]);
      for (int j = 0; j <= P - 1; j++) {
        if (md[j] != -inf) {
          int val = md[j] + (a[i] - j + P) % P;
          dp[i][k] = max(dp[i][k], val);
        }
      }
    }
  }
  cout << dp[N][K] << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}
