#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 500000;
const int MAXMOD = 100;
int n, nparts, MOD;
int a[MAXN];
int sum[MAXN + 1];
int dp[2][MAXN + 1];
int bit[MAXMOD + 1];
void bclear() {
  for (int i = (0); i <= (MOD); ++i) bit[i] = -1;
}
void bset(int idx, int val) {
  ++idx;
  while (idx <= MOD) {
    bit[idx] = max(bit[idx], val);
    idx += idx & -idx;
  }
}
int bget(int idx) {
  ++idx;
  int ret = -1;
  while (idx > 0) {
    ret = max(ret, bit[idx]);
    idx -= idx & -idx;
  }
  return ret;
}
void run() {
  scanf("%d%d%d", &n, &nparts, &MOD);
  for (int i = (0); i < (n); ++i) scanf("%d", &a[i]);
  sum[0] = 0;
  for (int i = (0); i < (n); ++i) sum[i + 1] = (sum[i] + a[i]) % MOD;
  int at = 0, ret;
  for (ret = 0; true; ++ret) {
    at = 1 - at;
    memset(dp[at], -1, sizeof(dp[at]));
    int mxcheaper = -1;
    bclear();
    for (int i = (0); i <= (n); ++i) {
      if (i == 0) dp[at][i] = max(dp[at][i], 0);
      if (ret != 0) dp[at][i] = max(dp[at][i], dp[1 - at][i]);
      if (mxcheaper != -1) dp[at][i] = max(dp[at][i], mxcheaper + 1);
      int mxsame = bget(sum[i]);
      if (mxsame != -1) dp[at][i] = max(dp[at][i], mxsame + 1);
      bset(sum[i], dp[at][i]);
      mxcheaper = max(mxcheaper, dp[1 - at][i]);
    }
    if (dp[at][n] >= nparts) break;
  }
  printf("%d\n", sum[n] + ret * MOD);
}
int main() {
  run();
  return 0;
}
