#include <bits/stdc++.h>
using namespace std;
const int inf = 1e7;
int n, K, P, a[500055], S[500055], F[500055], top;
int f[2][105][105], sb[105];
int find(int value) {
  int l = 1, r = top, mid, ans;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (S[mid] > value)
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  return ans;
}
void min(int &a, int b) {
  if (b < a) a = b;
}
int main() {
  scanf("%d%d%d", &n, &K, &P);
  if (K * P >= n) {
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]), a[i] %= P;
    int w = 0;
    memset(f, 51, sizeof(f));
    f[w][1][a[1]] = 0;
    for (int i = 0; i < P; i++)
      for (int j = 0; j < P; j++) sb[i + j] = (i + j) % P;
    for (register int i = 2; i <= n; i++) {
      w ^= 1;
      for (register int j = 1; j <= K; j++)
        for (register int k = 0; k < P; k++) f[w][j][k] = inf;
      for (register int j = 1; j <= K; j++)
        for (register int k = 0; k < P; k++)
          if (f[w ^ 1][j][k] < inf) {
            min(f[w][j][sb[k + a[i]]], f[w ^ 1][j][k]);
            if (j + 1 <= K) min(f[w][j + 1][a[i]], f[w ^ 1][j][k] + k);
          }
    }
    int ans = inf;
    for (int k = 0; k < P; k++)
      if (f[w][K][k] < inf) min(ans, f[w][K][k] + k);
    cout << ans << endl;
    return 0;
  }
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) a[i] = (a[i] + a[i - 1]) % P;
  for (int i = 1; i <= n; i++) {
    if (a[i] >= S[top]) {
      S[++top] = a[i];
      F[i] = top;
    } else {
      int pos = find(a[i]);
      S[pos] = a[i];
      F[i] = pos;
    }
  }
  if (F[n] >= K)
    printf("%d\n", a[n]);
  else
    printf("%d\n", a[n] + P);
  return 0;
}
