#include <bits/stdc++.h>
using namespace std;
template <class T>
bool umin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool umax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
const int N = 105;
int dp[2][500009], F[2][N], par[500009];
void upd(int t, int x, int val) {
  if (!t) {
    for (; x < N; x += x & (-x)) umin(F[0][x], val);
  } else {
    for (; x; x -= x & (-x)) umin(F[1][x], val);
  }
}
int tap(int t, int x) {
  int res = 1000000007;
  if (!t) {
    for (; x; x -= x & (-x)) umin(res, F[0][x]);
  } else {
    for (; x < N; x += x & (-x)) umin(res, F[1][x]);
  }
  return res;
}
int main() {
  int n, k, p;
  scanf("%d%d%d", &n, &k, &p);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    par[i] = (par[i - 1] + x) % p;
  }
  for (int j = 0; j < 2; j++)
    for (int i = 0; i <= n; i++) dp[j][i] = 1000000007;
  for (int i = 1; i <= n; i++) dp[1][i] = par[i];
  for (int j = 2; j <= k; j++) {
    for (int i = 0; i < N; i++) F[0][i] = F[1][i] = 1000000007;
    for (int i = 1; i <= n; i++) {
      upd(0, par[i - 1] + 1, dp[(j - 1) & 1][i - 1] - par[i - 1]);
      upd(1, par[i - 1] + 1, dp[(j - 1) & 1][i - 1] - par[i - 1] + p);
      dp[j & 1][i] = min(tap(0, par[i] + 1), tap(1, par[i] + 1)) + par[i];
    }
  }
  printf("%d\n", dp[k & 1][n]);
  return 0;
}
