#include <bits/stdc++.h>
using namespace std;
long long t, n, i, j, k, x, y, z, l, r, mid, ans, mod = 1e9 + 7, g, m;
long long a[20005], b[20005], c[105], d[20001][52], e[20005][100];
char ch;
string s, st;
vector<long long> v[300004], u;
int dp(int i, int k) {
  if (i > n && k) return -1e7;
  if (k == 1) return (b[n] - b[i - 1]) % m;
  long long &ret = d[i][k];
  if (ret + 1) return ret;
  ret = -1e7;
  for (int j = 0; j < m; j++)
    if (e[i][j]) ret = max(ret, 0LL + j + dp(e[i][j] + 1, k - 1));
  return ret;
}
int main() {
  ios::sync_with_stdio(NULL);
  cin.tie(0);
  cout.tie(0);
  memset(d, -1, sizeof d);
  cin >> n >> k >> m;
  for (i = 1; i <= n; i++) cin >> a[i], b[i] = a[i] + b[i - 1];
  for (i = n; i > 0; i--) {
    c[b[i] % m] = i;
    for (j = 0; j < m; j++) e[i][j] = c[(b[i - 1] + j) % m];
  }
  ans = dp(1, k);
  cout << ans;
}
