#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int pwr(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = (ans * 1LL * a) % mod;
    a = (a * 1LL * a) % mod;
    b >>= 1;
  }
  return ans;
}
const int N = 500005;
const int P = 105;
int a[N];
int pre[N];
int dp[2][N];
int bit1[P], bit2[P];
void upd(int idx, int val, int bit[]) {
  idx++;
  while (idx < P) {
    bit[idx] = min(bit[idx], val);
    idx += idx & (-idx);
  }
}
int query(int idx, int bit[]) {
  idx++;
  int ans = 1e9;
  while (idx > 0) {
    ans = min(ans, bit[idx]);
    idx -= idx & (-idx);
  }
  return ans;
}
int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k, p;
  cin >> n >> k >> p;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    pre[i] = (pre[i - 1] + a[i]) % p;
    a[i] %= p;
  }
  int s = 0;
  for (int i = 1; i <= n; i++) {
    s += a[i];
    if (s >= p) s -= p;
    dp[1][i] = s;
  }
  for (int kk = 2; kk <= k; kk++) {
    int kk1 = kk & 1;
    int kk2 = kk1 ^ 1;
    for (int i = 0; i < P; i++) {
      bit1[i] = bit2[i] = 1e9;
    }
    for (int i = 0; i < kk; i++) {
      dp[kk1][i] = 1e9;
    }
    for (int i = kk; i <= n; i++) {
      upd(pre[i - 1], dp[kk2][i - 1] - pre[i - 1], bit1);
      upd(p - pre[i - 1], dp[kk2][i - 1] + p - pre[i - 1], bit2);
      dp[kk1][i] =
          pre[i] + min(query(pre[i], bit1), query(p - pre[i] - 1, bit2));
    }
  }
  cout << dp[k & 1][n];
  return 0;
}
