#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
const int N = 500000;
int n, k, p, a[N + 1], ps[N + 1], dp[N + 1], up[200], down[200];
int min(const int& a, const int& b) { return (a < b ? a : b); }
void ckmin(int& a, const int& b) { a = min(a, b); }
void init() {
  memset(up, 0x3F, sizeof(up));
  memset(down, 0x3F, sizeof(down));
}
void u_down(int x, int y) {
  ++x;
  for (; x <= p; x += x & (-x)) ckmin(down[x], y);
}
int q_down(int x) {
  int ret = 1e9;
  ++x;
  for (; x > 0; x -= x & (-x)) ckmin(ret, down[x]);
  return ret;
}
void u_up(int x, int y) {
  x = p - x + 1;
  for (; x <= p; x += x & (-x)) ckmin(up[x], y);
}
int q_up(int x) {
  int ret = 1e9;
  x = p - x + 1;
  for (; x > 0; x -= x & (-x)) ckmin(ret, up[x]);
  return ret;
}
int main() {
  scanf("%d%d%d", &n, &k, &p);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    dp[i] = ps[i] = (ps[i - 1] + a[i]) % p;
  }
  for (int j = 2; j <= k; ++j) {
    init();
    for (int i = 1; i <= n; ++i) {
      int nex = min(q_down(ps[i]) + ps[i], q_up(ps[i] + 1) + ps[i] + p);
      u_down(ps[i], dp[i] - ps[i]);
      u_up(ps[i], dp[i] - ps[i]);
      dp[i] = nex;
    }
  }
  printf("%d\n", dp[n]);
}
