#include <bits/stdc++.h>
using namespace std;
using namespace std;
long long dp[20005][55];
long long sum[20005];
int main() {
  int n, k, p;
  int tmp;
  cin >> n >> k >> p;
  sum[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> tmp;
    sum[i] = (sum[i - 1] + tmp) % p;
  }
  memset(dp, -0x3f, sizeof(dp));
  dp[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= k; j++) {
      for (int o = 0; o < p; o++) {
        dp[sum[i]][j] =
            max(dp[sum[i]][j], dp[o][j - 1] + ((sum[i] - o + p) % p));
      }
    }
  }
  cout << dp[sum[n]][k] << endl;
  return 0;
}
