#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
long long max(long long a, long long b) {
  if (a > b) {
    return a;
  } else {
    return b;
  }
}
long long min(long long a, long long b) {
  if (a < b) {
    return a;
  } else {
    return b;
  }
}
long long power(long long b, long long e) {
  if (e == 0) return 1;
  if (e % 2)
    return ((b * power((b) * (b), (e - 1) / 2)));
  else
    return power((b) * (b), e / 2);
}
long long modpower(long long b, long long e, long long q) {
  long long MOD = q;
  if (e == 0) return 1;
  if (e % 2)
    return ((b % MOD) * modpower((b % MOD) * (b % MOD), (e - 1) / 2, q)) % MOD;
  else
    return modpower((b % MOD) * (b % MOD), e / 2, q) % MOD;
}
void dpv(vector<long long> v) {
  for (long long i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << endl;
}
void dpv(vector<pair<long long, long long> > v) {
  for (long long i = 0; i < v.size(); i++) {
    cout << v[i].first << " " << v[i].second << endl;
  }
}
void dpv(set<long long> v) {
  for (auto i : v) {
    cout << i << " ";
  }
  cout << endl;
}
const long long N = 2e4 + 5, M = 105;
long long n, m, mo, a[N], f[N][55], g[55][M];
void oblivious() {
  long long n, k, p;
  cin >> n >> k >> p;
  a[0] = 0;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
    a[i] %= p;
  }
  for (long long i = 0; i <= k; i++) {
    for (long long j = 0; j < p; j++) {
      g[i][j] = -1e9 + 7;
    }
  }
  g[0][0] = 0;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= min(i, k); j++) {
      for (long long rem = 0; rem < p; rem++) {
        f[i][j] = max(f[i][j], (g[j - 1][rem] + (a[i] - rem + p) % p));
      }
      g[j][a[i] % p] = max(g[j][a[i] % p], f[i][j]);
    }
  }
  long long mx = -1;
  for (long long i = 0; i < p; i++) {
    mx = max(mx, g[k][i]);
  }
  cout << g[k][a[n] % p] << endl;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) {
    oblivious();
  }
  return 0;
}
