#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
const int INF = 0x3f3f3f3f;
int a[N];
int dp[105][105];
int _dp[N];
int main() {
  int n, k, p;
  scanf("%d%d%d", &n, &k, &p);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), a[i] += a[i - 1], a[i] %= p;
  if (k * p > n) {
    memset(dp, INF, sizeof(dp));
    dp[0][0] = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = k; j >= 1; j--) {
        for (int l = 0; l < p; l++) {
          dp[a[i]][j] = (dp[a[i]][j] < dp[l][j - 1] + ((a[i] - l) % p + p) % p
                             ? dp[a[i]][j]
                             : dp[l][j - 1] + ((a[i] - l) % p + p) % p);
        }
      }
    }
    printf("%d\n", dp[a[n]][k]);
  } else {
    memset(_dp, INF, sizeof(_dp));
    for (int i = 1; i <= n - 1; i++)
      *upper_bound(_dp + 1, _dp + n, a[i]) = a[i];
    if (_dp[k - 1] <= a[n])
      printf("%d\n", a[n]);
    else
      printf("%d\n", a[n] + p);
  }
  return 0;
}
