#include <bits/stdc++.h>
using namespace std;
inline int read() {
  char c = 0;
  int x = 0;
  bool f = 0;
  while (!isdigit(c)) f = c == '-', c = getchar();
  while (isdigit(c)) x = x * 10 + c - 48, c = getchar();
  return f ? -x : x;
}
const int N = 5e5 + 100, M = 110;
int dp[N], pre[N], n, k;
int f[M][M], g[M], mod;
int main() {
  n = read();
  k = read();
  mod = read();
  for (int i = 1; i <= n; i++) pre[i] = (pre[i - 1] + read()) % mod;
  if (k * mod > n) {
    memset(f, 127, sizeof(f));
    f[0][0] = 0;
    for (int i = 1; i <= n; i++) {
      memset(g, 127, sizeof(g));
      for (int j = 1; j <= k; j++)
        for (int l = 0; l < mod; l++)
          g[j] = min(g[j], f[l][j - 1] + (pre[i] - l + mod) % mod);
      for (int j = 1; j <= k; j++) f[pre[i]][j] = g[j];
    }
    printf("%d", f[pre[n]][k]);
  } else {
    memset(dp, 127, sizeof(dp));
    for (int i = 1; i < n; i++) {
      *upper_bound(dp + 1, dp + n, pre[i]) = pre[i];
    }
    if (dp[k - 1] <= pre[n])
      printf("%d", pre[n]);
    else
      printf("%d", pre[n] + mod);
  }
  return 0;
}
