#include <bits/stdc++.h>
using namespace std;
const int maxn = 500010;
const int maxk = 110;
const int oo = 100000007;
int n, k, base, a[maxn], t1[4 * maxk], t2[4 * maxk], f[3][maxn];
void update1(int i, int tt) {
  while (i <= maxk) {
    t1[i] = min(t1[i], tt);
    i += i & -i;
  }
}
int get1(int i) {
  int res = oo;
  while (i > 0) {
    res = min(res, t1[i]);
    i -= i & -i;
  }
  return res;
}
void update2(int i, int tt) {
  while (i > 0) {
    t2[i] = min(t2[i], tt);
    i -= i & -i;
  }
}
int get2(int i) {
  int res = oo;
  while (i <= maxk) {
    res = min(res, t2[i]);
    i += i & -i;
  }
  return res;
}
int main() {
  cin >> n >> k >> base;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] = (a[i - 1] + a[i]) % base;
  }
  int t = 0;
  memset(f[1 - t], oo, sizeof(f[1 - t]));
  f[1 - t][0] = 0;
  for (int ti = 1; ti <= k; ti++) {
    memset(t1, oo, sizeof(t1));
    memset(t2, oo, sizeof(t2));
    memset(f[t], oo, sizeof(f[t]));
    for (int i = 1; i <= n; i++) {
      update1(a[i - 1] + 1, f[1 - t][i - 1] - a[i - 1]);
      update2(a[i - 1] + 1, f[1 - t][i - 1] - a[i - 1]);
      f[t][i] = min(f[t][i], get1(a[i] + 1) + a[i]);
      f[t][i] = min(f[t][i], get2(a[i] + 1 + 1) + a[i] + base);
    }
    t = 1 - t;
  }
  cout << f[1 - t][n] << endl;
}
