#include <bits/stdc++.h>
using namespace std;
template <typename T, typename L>
bool smin(T& x, L y) {
  return x > y ? (x = y, true) : false;
}
const int MAXN = 5e5 + 15, MAXK = 128;
struct FN {
  int bit[MAXK];
  void clear() { memset(bit, 63, sizeof(bit)); }
  void upd(int x, int val) {
    for (x++; x < MAXK; x += x & -x) smin(bit[x], val);
  }
  int get(int x) {
    int ret = 1e9;
    for (x++; x; x -= x & -x) smin(ret, bit[x]);
    return ret;
  }
} fen;
int a[MAXN];
int ps[MAXN], dp[MAXN][MAXK];
int n, k, p;
int32_t main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cerr << "HELLO WORLD :)\n";
  cin >> n >> k >> p;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    (ps[i] = a[i]) %= p;
    if (i) (ps[i] += ps[i - 1]) %= p;
  }
  for (int i = 0; i < n; i++) dp[i][1] = ps[i];
  for (int j = 2; j <= k; j++) {
    fen.clear();
    int help = 1e9;
    for (int i = 0; i < n; i++) {
      dp[i][j] = min(help, fen.get(ps[i])) + ps[i];
      smin(help, dp[i][j - 1] - ps[i] + p);
      fen.upd(ps[i], dp[i][j - 1] - ps[i]);
    }
  }
  return cout << dp[n - 1][k] << '\n', false;
}
