#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
int n, k, p, a[20100];
long long sum, dp[3][51][101];
bool u = 1;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k >> p;
  for (int i = 1; i <= n; i++) cin >> a[i], a[i] %= p;
  for (int i = 0; i <= k; i++)
    for (int j = 0; j < p; j++) dp[0][i][j] = -2e9;
  dp[0][0][0] = 0;
  for (int i = 1; i <= n; i++, u = !u) {
    for (int j = 0; j <= k; j++)
      for (int z = 0; z < p; z++) dp[u][j][z] = -2e9;
    dp[u][i][a[i]] = (i <= k ? dp[!u][i - 1][a[i - 1]] : dp[u][i][a[i]]);
    int mn = min(i, k);
    for (int j = 1; j <= mn; j++)
      for (int z = 0; z < p; z++) {
        if (j < i && dp[!u][j - 1][z] != -2e9)
          dp[u][j][a[i]] = max(dp[u][j][a[i]], dp[!u][j - 1][z] + z);
        if (dp[!u][j][z] != -2e9)
          dp[u][j][(a[i] + z) % p] =
              max(dp[!u][j][z], dp[u][j][(a[i] + z) % p]);
      }
  }
  for (int i = 0; i < p; i++)
    if (dp[!u][k][i] != -2e9) {
      sum = max(sum, 0LL + dp[!u][k][i] + i);
    }
  cout << sum;
  return 0;
}
