#include <bits/stdc++.h>
using namespace std;
const int N = 2e4 + 7;
const int lim = 4e4;
const int INF = 0x3f3f3f;
const int MOD = 10086001;
const double eps = 1e-7;
int sum[N], a;
int f[105][55];
int n, k, p, m;
int main() {
  scanf("%d%d%d", &n, &m, &p);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a);
    sum[i] = (sum[i - 1] + a) % p;
  }
  for (int i = 0; i <= p; i++)
    for (int j = 0; j <= m; j++) f[i][j] = -INF;
  f[0][0] = 0;
  for (int i = 1; i <= n; i++)
    for (int j = m; j >= 1; j--)
      for (int k = 0; k < p; k++)
        f[sum[i]][j] = max(f[k][j - 1] + (sum[i] - k + p) % p, f[sum[i]][j]);
  printf("%d\n", f[sum[n]][m]);
  return 0;
}
