#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 2e4 + 24;
long long a[N], dp[N][55], sm[N];
int main() {
  memset(dp, -0x3f, sizeof(dp));
  dp[0][0] = 0;
  int n, k, p;
  scanf("%d%d%d", &n, &k, &p);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    sm[i] = sm[i - 1] + a[i];
    sm[i] %= p;
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= k; j++)
      for (int g = 0; g < p; g++)
        dp[sm[i]][j] = max(dp[sm[i]][j], dp[g][j - 1] + (sm[i] - g + p) % p);
  printf("%lld\n", dp[sm[n]][k]);
  return 0;
}
