#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
const int inf = 1e9;
int n, k, p;
short dp[20001][50][101];
int arr[20001];
inline int read() {
  int x = 0;
  char ch = getchar();
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x;
}
int solve(int it, int cnt, int cur) {
  if (it == n) return cur;
  if (dp[it][cnt][cur] != -1) return dp[it][cnt][cur];
  int best = 0;
  if (it + (k - cnt) < n)
    best = max(best, solve(it + 1, cnt, (cur + arr[it]) % p));
  if (cnt < k) best = max(best, cur + solve(it + 1, cnt + 1, arr[it] % p));
  return dp[it][cnt][cur] = best;
}
int main() {
  n = read(), k = read(), p = read();
  k--;
  for (int i = 0; i < n; i++) arr[i] = read();
  memset(dp, -1, sizeof dp);
  printf("%d\n", solve(0, 0, 0));
  return 0;
}
