#include <bits/stdc++.h>
using namespace std;
int c[2][110][110], dp[110], s[500010], n, k, p;
void update(int f, int t, int x, int w) {
  for (int i = x + 1; i <= p + 1; i += i & -i) c[f][t][i] = min(c[f][t][i], w);
}
int query(int f, int t, int x) {
  int res = 0x3f3f3f3f;
  for (int i = x + 1; i; i -= i & -i) res = min(c[f][t][i], res);
  return res;
}
int main() {
  while (~scanf("%d%d%d", &n, &k, &p)) {
    memset(dp, 0x3f3f3f3f, sizeof(dp));
    for (int i = 1; i <= n; i++) {
      scanf("%d", &s[i]);
      s[i] = (s[i - 1] + s[i]) % p;
    }
    for (int i = 0; i <= k; i++)
      for (int j = 0; j <= p + 1; j++) c[0][i][j] = c[1][i][j] = 0x3f3f3f3f;
    update(0, 0, 0, 0);
    update(1, 0, 0, 0);
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= min(i, k); j++)
        dp[j] = min(query(0, j - 1, s[i]), query(1, j - 1, p - s[i])) + s[i];
      for (int j = 1; j <= min(i, k); j++) {
        if (dp[j] == 0x3f3f3f3f) continue;
        update(0, j, s[i], dp[j] - s[i]);
        update(1, j, p - s[i], dp[j] - s[i] + p);
      }
    }
    printf("%d\n", dp[k]);
  }
  return 0;
}
