#include <bits/stdc++.h>
using namespace std;
const int kn = 400005;
const int N = 2 * 1e4 + 1;
const long long inf = 9999999999;
int dp[N][51], pre[N], re[N], k, n, p, a[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> k >> p;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    pre[i] = (pre[i - 1] + a[i]) % p;
    dp[i][1] = pre[i];
  }
  for (int j = 2; j <= k; j++) {
    for (int i = 0; i <= p - 1; i++) re[i] = -inf;
    re[pre[j - 1]] = dp[j - 1][j - 1];
    for (int i = j; i <= n; i++) {
      for (int tmp = 0; tmp <= p - 1; tmp++)
        dp[i][j] = max(dp[i][j], re[tmp] + (pre[i] - tmp + p) % p);
      re[pre[i]] = dp[i][j - 1];
    }
  }
  cout << dp[n][k];
}
