#include <bits/stdc++.h>
using namespace std;
int a[500050], sum[500050], dp[105][105];
int c[205][105];
int lowbit(int x) { return x & (-x); }
void update(int id, int x, int v) {
  while (x <= 100) {
    c[id][x] = min(c[id][x], v);
    x += lowbit(x);
  }
}
int query(int id, int x) {
  int res = 0x3f3f3f3f;
  while (x) {
    res = min(res, c[id][x]);
    x -= lowbit(x);
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  int n, k, p;
  cin >> n >> k >> p;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) sum[i] = (sum[i - 1] + a[i]) % p;
  memset(c, 0x3f, sizeof(c));
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = 0;
  update(0, 1, 0);
  update(k + 1, p, 0);
  for (int i = 1; i <= n; i++)
    for (int j = k; j; j--) {
      dp[sum[i]][j] = min(dp[sum[i]][j], query(j - 1, sum[i] + 1) + sum[i]);
      dp[sum[i]][j] =
          min(dp[sum[i]][j], query(j + k, p - sum[i] - 1) + sum[i] + p);
      update(j, sum[i] + 1, dp[sum[i]][j] - sum[i]);
      update(j + k + 1, p - sum[i], dp[sum[i]][j] - sum[i]);
    }
  cout << dp[sum[n]][k];
  return 0;
}
