#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 2;
const int INF = 0x3f3f3f3f;
int n, k, mod;
int a[maxn], dp[maxn];
void DP() {
  memset(dp, INF, sizeof(dp));
  dp[0] = 0;
  for (int j = 0; j < k; j++) {
    int mn = dp[0], val = 0;
    for (int i = 1; i <= n; i++) {
      int tmp = mn;
      if (val > a[i]) tmp++;
      if (dp[i] < mn || (dp[i] == mn && val > a[i])) {
        mn = dp[i];
        val = a[i];
      }
      dp[i] = tmp;
    }
    dp[0] = INF;
  }
}
int main() {
  scanf("%d%d%d", &n, &k, &mod);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    a[i] = (a[i] + a[i - 1]) % mod;
  }
  DP();
  printf("%d", dp[n] * mod + a[n]);
  return 0;
}
