#include <bits/stdc++.h>
using namespace std;
const int maxn = 20000;
const int inf = 100000000;
int n, k, p;
int a[maxn + 5];
int dp[2][maxn + 5];
int sum[maxn + 5], pre[maxn + 5];
int main() {
  scanf("%d%d%d", &n, &k, &p);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  int now = 0;
  for (int i = 0; i <= n; ++i) dp[1][i] = -inf;
  dp[1][0] = 0;
  for (int turn = 1; turn <= k; ++turn) {
    for (int i = 0; i < p; ++i) pre[i] = -inf;
    for (int i = 0; i <= n; ++i) dp[now][i] = dp[now ^ 1][i];
    for (int i = 1; i <= n; ++i) {
      for (int j = 0; j < p; ++j) sum[j] = -inf;
      for (int j = 0; j < p; ++j) sum[(j + a[i]) % p] = pre[j];
      sum[a[i] % p] = max(sum[a[i] % p], dp[now ^ 1][i - 1]);
      for (int j = 0; j < p; ++j) dp[now][i] = max(dp[now][i], sum[j] + j);
      for (int j = 0; j < p; ++j) pre[j] = sum[j];
    }
    now ^= 1;
  }
  printf("%d\n", dp[now ^ 1][n]);
  return 0;
}
