#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const int N = 1e6 + 100, OO = 1e9 + 7, T = 22, M = 1e9 + 7, P = 6151, SQ = 1300,
          lg = 22;
int a[N];
int32_t main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n, k, p;
  cin >> n >> k >> p;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] %= p;
    a[i] += a[i - 1];
    a[i] %= p;
  }
  vector<int> dp(n + 1, 10000);
  dp[0] = 0;
  for (int ph = 1; ph <= k; ph++) {
    vector<int> rmq(p, 10000);
    vector<int> ndp(n + 1, 10000);
    int mn = 10000;
    for (int i = 0; i <= n; i++) {
      ndp[i] = mn + 1;
      for (int j = a[i]; j >= 0; j -= 10) ndp[i] = min(ndp[i], rmq[j]);
      for (int j = a[i]; j < min(p, a[i] + 10); j++)
        rmq[j] = min(rmq[j], dp[i]);
      mn = min(mn, dp[i]);
    }
    dp = ndp;
  }
  cout << (a[n] + dp[n] * p) << endl;
  return 0;
}
