#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 2, K = 102, P = 102;
int dp[3][K][P], a[N], mn[N][K], pre[N];
int main() {
  ios::sync_with_stdio(), cin.tie(0), cout.tie(0);
  int n, k, p;
  cin >> n >> k >> p;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i] %= p;
  }
  if (10000 >= n) {
    for (int i = 0; i <= 100; i++)
      for (int j = 0; j <= 100; j++) dp[0][i][j] = 1e8;
    for (int i = 0; i <= 10000; i++)
      for (int j = 0; j <= 100; j++) mn[i][j] = 1e8;
    dp[0][0][0] = 0;
    mn[0][0] = 0;
    for (int i = 1; i <= n; i++) {
      int nw = (i & 1);
      int pas = (nw ^ 1);
      for (int ii = 0; ii <= 100; ii++)
        for (int j = 0; j <= 100; j++) dp[nw][ii][j] = 1e8;
      for (int j = 1; j <= k; j++) {
        for (int las = 0; las < p; las++) {
          if (las == a[i - 1]) {
            dp[nw][j][las] = mn[i - 1][j - 1] + a[i - 1];
          }
          dp[nw][j][las] =
              min(dp[nw][j][las], dp[pas][j][((las - a[i - 1] + p) % p)] -
                                      ((las - a[i - 1] + p) % p) + las);
          mn[i][j] = min(mn[i][j], dp[nw][j][las]);
        }
      }
    }
    int ans = 1e8;
    for (int i = 0; i < p; i++) {
      ans = min(ans, dp[n & 1][k][i]);
    }
    cout << ans;
  } else {
    for (int i = 1; i <= n; i++) {
      pre[i] = pre[i - 1] + a[i - 1];
      pre[i] %= p;
    }
    vector<int> lis;
    lis.clear();
    for (int i = 1; i < n; i++) {
      auto it = upper_bound(lis.begin(), lis.end(), pre[i]);
      if (it == lis.end()) {
        lis.emplace_back(pre[i]);
      } else {
        lis[it - lis.begin()] = pre[i];
      }
    }
    auto it = upper_bound(lis.begin(), lis.end(), pre[n]);
    if (it - lis.begin() + 1 >= k)
      cout << pre[n];
    else
      cout << pre[n] + p;
  }
}
