#include <bits/stdc++.h>
using namespace std;
int f[2][51][110], n, m, p, a[20010];
int main() {
  cin >> n >> m >> p;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), a[i] %= p;
  for (int i = 0; i <= 1; i++)
    for (int j = 0; j <= m; j++)
      for (int k = 0; k < p; k++) f[i][j][k] = -999999999;
  f[1][1][a[1]] = 0;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= m; j++)
      for (int k = 0; k < p; k++) f[i & 1 ^ 1][j][k] = -999999999;
    for (int j = 0; j <= m; j++)
      for (int k = 0; k < p; k++)
        if (f[i & 1][j][k] >= 0) {
          f[i & 1 ^ 1][j][(k + a[i + 1]) % p] =
              max(f[i & 1 ^ 1][j][(k + a[i + 1]) % p], f[i & 1][j][k]);
          f[i & 1 ^ 1][j + 1][a[i + 1]] =
              max(f[i & 1 ^ 1][j + 1][a[i + 1]], f[i & 1][j][k] + k);
        }
  }
  int ans = 0;
  for (int i = 0; i < p; i++) ans = max(ans, f[n & 1][m][i] + i);
  cout << ans << endl;
  return 0;
}
