#include <bits/stdc++.h>
using namespace std;
int seq[1000006], sum[1000006], cnt[1000006];
int n, k, p;
struct FEN {
  int sum[202];
  FEN() { memset(sum, 0x3f, sizeof(sum)); }
  void add(int p, int x) {
    for (int i = p + 1; i < 202; i += (i & -i)) sum[i] = min(sum[i], x);
  }
  int qmin(int p) {
    int x = 0x3f3f3f3f;
    for (int i = p + 1; i; i -= (i & -i)) x = min(x, sum[i]);
    return x;
  }
} L[202], R[202];
bool lis() {
  static int app[1000006];
  memset(app, 0x3f, sizeof(app));
  for (int i = 1; i < n; i++)
    *upper_bound(app + 1, app + n + 1, sum[i]) = sum[i];
  if (app[k - 1] <= sum[n])
    return true;
  else
    return false;
}
int dp() {
  static int f[202][40004];
  memset(f, 0x3f, sizeof(f));
  f[0][0] = 0;
  L[0].add(0, 0);
  R[0].add(p, 0);
  for (int j = 1; j <= n; j++)
    for (int i = k; i >= 1; i--) {
      f[i][j] = min(f[i][j], L[i - 1].qmin(sum[j]) + sum[j]);
      f[i][j] = min(f[i][j], R[i - 1].qmin(p - sum[j]) + sum[j] + p);
      L[i].add(sum[j], f[i][j] - sum[j]);
      R[i].add(p - sum[j], f[i][j] - sum[j]);
    }
  return f[k][n];
}
int main() {
  scanf("%d%d%d", &n, &k, &p);
  for (int i = 1; i <= n; i++) scanf("%d", &seq[i]);
  for (int i = 1; i <= n; i++) sum[i] = (sum[i - 1] + seq[i]) % p;
  for (int i = 1; i <= n; i++) cnt[sum[i]]++;
  int max_cnt = 0;
  for (int i = 0; i < p; i++) max_cnt = max(max_cnt, cnt[i]);
  if (lis())
    printf("%d\n", sum[n]);
  else if (max_cnt >= k)
    printf("%d\n", sum[n] + p);
  else
    printf("%d\n", dp());
  return 0;
}
