#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 500000 + 10;
const long long mod = 1e9 + 9;
int dp[110][110];
int _dp[maxn];
int a[maxn];
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] = min(dp[a[i]][j], dp[l][j - 1] + (a[i] - l + 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;
}
