#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 99, K = 103;
int n, k, p, dp[K][K], Fen[K][K], a[N];
void update(int x, int val, int s) {
  dp[s][x] = min(dp[s][x], val);
  x++;
  while (x <= K) {
    Fen[s][x] = min(Fen[s][x], val);
    x += (x & -x);
  }
}
int query(int x, int s) {
  int res = N;
  x++;
  while (x > 0) {
    res = min(res, Fen[s][x]);
    x -= (x & -x);
  }
  return res;
}
int main() {
  for (int i = 0; i < K; i++)
    for (int j = 0; j < K; j++) Fen[i][j] = N, dp[i][j] = N;
  update(0, 0, 0);
  cin >> n >> k >> p;
  for (int io = 1; io < n + 1; io++) {
    int x;
    scanf("%d", &a[io]);
    ;
    a[io] = (a[io] + a[io - 1]) % p;
    x = a[io];
    for (int i = k; i >= 1; i--) {
      update(x, query(x, i - 1), i);
      update(x, query(p - 1, i - 1) + 1, i);
    }
  }
  cout << dp[k][a[n]] * p + a[n];
}
