#include <bits/stdc++.h>
using namespace std;
int n, k, p;
int dp[2][52][102], a[20004];
int main() {
  cin >> n >> k >> p;
  for (int i = 0; i < n; i++) scanf("%d", a + i);
  long long summ[20004] = {0};
  for (int i = n - 1; i > -1; i--) {
    if (i == n - 1)
      summ[i] = a[i];
    else
      summ[i] = summ[i + 1] + a[i];
    summ[i] %= p;
  }
  for (int i = n - 1; i >= 0; i--) {
    for (int j = k - 1; j >= 0; j--) {
      for (int sum = 0; sum < p; ++sum) {
        if (j == k - 1) {
          dp[i % 2][j][sum] = summ[i];
        } else {
          dp[i % 2][j][sum] = dp[(i + 1) % 2][j + 1][0] + (a[i] + sum) % p;
          if ((n - i - 1) > (k - j - 1))
            dp[i % 2][j][sum] =
                max(dp[i % 2][j][sum], dp[(i + 1) % 2][j][(sum + a[i]) % p]);
        }
      }
    }
  }
  cout << dp[0][0][0] << endl;
}
