#include <bits/stdc++.h>
using namespace std;
const int N = 500050;
const int M = 105;
const int inf = 1e9 + 7;
int a[N], mn[M], dp[N];
int x[M], y[M], tsz, root;
int p;
void Set(int qi, int val) {
  qi++;
  int i;
  for (i = qi; i <= p; i += i & -i) x[i] = min(x[i], val);
  for (i = qi; i >= 1; i -= i & -i) y[i] = min(y[i], val);
}
int Get(int qi, int t) {
  qi++;
  int ans = inf, i;
  if (t == 1)
    for (i = qi; i >= 1; i -= i & -i) ans = min(ans, x[i]);
  else
    for (i = qi; i <= p; i += i & -i) ans = min(ans, y[i]);
  return ans;
}
int main() {
  x[0] = inf;
  int k, n, i, j;
  scanf("%i %i %i", &n, &k, &p);
  for (i = 1; i <= n; i++) scanf("%i", &a[i]), a[i] += a[i - 1], a[i] %= p;
  dp[0] = 0;
  for (i = 1; i <= n; i++) dp[i] = inf;
  int h = 0;
  while (k--) {
    for (i = 1; i <= p; i++) x[i] = y[i] = inf;
    root = tsz = 0;
    Set(a[h], p - a[h] + dp[h]);
    for (i = h + 1; i <= n; i++) {
      int ans = inf;
      ans = min(Get(a[i], 1) + a[i] - p, Get(a[i] + 1, 0) + a[i]);
      Set(a[i], p - a[i] + dp[i]);
      dp[i] = ans;
    }
    h++;
  }
  printf("%i\n", dp[n]);
  return 0;
}
