#include <bits/stdc++.h>
using namespace std;
const int INF = 1e8;
const int N = 20002;
const int P = 111;
const int K = 55;
int n, p, k, a[N], dp[3][P][N], mx[P];
void dfs(int, int, int);
int main() {
  cin >> n >> k >> p;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i] %= p;
  }
  for (int j = 0; j < p; j++)
    for (int i = 0; i < n; i++) dp[1][j][i] = -INF;
  int G = 0;
  for (int i = 0; i < n; i++) {
    G = (G + a[i]) % p;
    dp[1][G][i] = G;
  }
  for (int ks = 2; ks <= k; ks++) {
    for (int j = 0; j < p; j++)
      for (int i = 0; i < n; i++) dp[ks & 1][j][i] = -INF;
    for (int i = 0; i < p; i++) mx[i] = -INF;
    int G = 0;
    for (int i = 0; i < n; i++) {
      G = (G + a[i]) % p;
      for (int j = 0; j < p; j++) {
        dp[ks & 1][j][i] = mx[((G - j + p) % p)] + j;
      }
      for (int j = 0; j < p; j++) mx[G] = max(mx[G], dp[1 - (ks & 1)][j][i]);
    }
  }
  int ans = -INF;
  for (int i = 0; i < p; i++) ans = max(ans, dp[k & 1][i][n - 1]);
  cout << ans << endl;
}
