#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const int N = 5e5 + 1, mod = 1e9 + 7;
int n, k, p, ans = 1e9, a[N], dp[2][101][101], f[N], lis[101];
void update_min(int& x, int y) {
  if (x > y) {
    x = y;
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n >> k >> p;
  if (n <= k * p) {
    for (int i = 1; i <= n; ++i) {
      cin >> a[i];
      a[i] %= p;
    }
    for (int j = 0; j <= k; ++j) {
      for (int m = 0; m < p; ++m) {
        dp[0][j][m] = 1e9;
      }
    }
    dp[0][0][0] = 0;
    for (int i = 1; i <= n; ++i) {
      for (int j = 0; j <= k; ++j) {
        for (int m = 0; m < p; ++m) {
          dp[i & 1][j][m] = 1e9;
        }
      }
      bool cur = i & 1;
      bool pre = cur ^ 1;
      for (int j = 1; j <= k; ++j) {
        for (int m = 0; m < p; ++m) {
          int val = (m + a[i]) % p;
          update_min(dp[cur][j][val], dp[pre][j][m] - m + val);
          update_min(dp[cur][j][a[i]], dp[pre][j - 1][m] + a[i]);
        }
      }
    }
    for (int m = 0; m < p; ++m) {
      ans = min(ans, dp[n & 1][k][m]);
    }
    cout << ans;
  } else {
    for (int i = 1; i <= n; ++i) {
      cin >> a[i];
      a[i] = (a[i] + a[i - 1]) % p;
    }
    for (int i = 1; i <= n; ++i) {
      for (int j = 0; j <= a[i]; ++j) {
        f[i] = max(f[i], lis[j] + 1);
      }
      lis[a[i]] = max(lis[a[i]], f[i]);
    }
    if (f[n] >= k) {
      cout << a[n];
    } else {
      cout << a[n] + p;
    }
  }
}
