#include <bits/stdc++.h>
using namespace std;
const long long o = 5e5 + 1;
long long n, m, p, f[o][51], a[o], g[o], w[o], l[o], r[o];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> p;
  for (long long i = 1; i <= n; i++) cin >> a[i], a[i] = a[i] + a[i - 1];
  memset(g, 0x3f, sizeof(g));
  for (long long i = 1; i <= n; i++) {
    f[i][1] = a[i] % p;
    for (long long j = 1; j <= m; j++) l[j] = g[j], r[j] = w[j];
    if (g[1] > f[i][1]) l[1] = f[i][1], r[1] = i;
    for (long long j = 1; j < min(i, m + 1); j++) {
      f[i][j + 1] = g[j] + (a[i] - a[w[j]]) % p;
      if (f[i][j + 1] < g[j + 1]) l[j + 1] = f[i][j + 1], r[j + 1] = i;
    }
    for (long long j = 1; j <= m; j++) g[j] = l[j], w[j] = r[j];
  }
  cout << f[n][m];
  return 0;
}
