#include <bits/stdc++.h>
using namespace std;
void load() {}
const int N = 5e5 + 123;
const int inf = 1e9;
int a[N], v[N];
int dp[200][200];
void trungtt() {
  int n, k, p;
  scanf("%d%d%d", &n, &k, &p);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), a[i] += a[i - 1], a[i] %= p;
  if (k * p > n) {
    for (int i = 0; i <= p; ++i)
      for (int j = 0; j <= k; ++j) dp[i][j] = inf;
    dp[0][0] = 0;
    for (int i = 1; i <= n; ++i)
      for (int j = k; j >= 1; --j)
        for (int t = 0; t < p; ++t)
          dp[a[i]][j] = min(dp[a[i]][j], dp[t][j - 1] + (a[i] - t + p) % p);
    printf("%d", dp[a[n]][k]);
  } else {
    for (int i = 0; i <= n; ++i) v[i] = inf;
    for (int i = 1; i < n; ++i) *upper_bound(v + 1, v + n, a[i]) = a[i];
    if (v[k - 1] <= a[n])
      printf("%d", a[n]);
    else
      printf("%d", a[n] + p);
  }
}
int main() {
  load();
  trungtt();
  return 0;
}
