#include <bits/stdc++.h>
using namespace std;
const int N = 2e4 + 10;
const int INF = 0x3f3f3f3f;
const int M = 998244353;
const int maxn = 1e6 + 10;
long long sum[N], dp[55][N];
pair<int, int> w[55][N];
int main() {
  int n, m, p;
  scanf("%d %d %d", &n, &m, &p);
  sum[0] = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &sum[i]);
    sum[i] += sum[i - 1];
  }
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      dp[i][j] = max(dp[i][j], w[i - 1][j - 1].first +
                                   (sum[j] - sum[w[i - 1][j - 1].second]) % p);
      if (dp[i][j] > w[i][j - 1].first)
        w[i][j] = make_pair(dp[i][j], j);
      else
        w[i][j] = w[i][j - 1];
    }
  }
  printf("%lld\n", dp[m][n]);
  return 0;
}
