#include <bits/stdc++.h>
using namespace std;
int n, k, p;
int a[20009];
int dp[2][60][109];
int main() {
  while (~scanf("%d%d%d", &n, &k, &p)) {
    for (int i = 0; i <= k; i++)
      for (int j = 0; j <= p; j++) dp[0][i][j] = dp[1][i][j] = -1000000000;
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    dp[0][0][0] = 0;
    dp[0][0][p] = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = k; j > 0; j--)
        for (int m = p - 1; m >= 0; m--) {
          if (j > i) continue;
          dp[1][j][m] = dp[0][j][(m - a[i] + 1000000 * p) % p] -
                        (m - a[i] + 1000000 * p) % p + m;
          if (m == a[i] % p)
            dp[1][j][m] = max(dp[1][j][m], dp[0][j - 1][p] + m);
          dp[1][j][p] = max(dp[1][j][p], dp[1][j][m]);
        }
      for (int j = 0; j <= k; j++) {
        for (int m = 0; m <= p; m++) {
          dp[0][j][m] = dp[1][j][m];
          dp[1][j][m] = -1000000000;
        }
      }
    }
    printf("%d\n", dp[0][k][p]);
  }
}
