#include <bits/stdc++.h>
using namespace std;
const int MAX = 500004, inf = 0x3f3f3f3f;
int a[MAX];
int no[102];
int nex[MAX][102];
int f[102][102 * 102];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k, p;
  cin >> n >> k >> p;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
    a[i] %= p;
  }
  for (int i = 0; i < p; i++) no[i] = n + 1;
  for (int i = n; i >= 0; i--) {
    for (int j = 0; j < p; j++) {
      nex[i][j] = no[j];
    }
    no[a[i]] = i;
  }
  int M = (p - 1) * k;
  memset(f, 0x3f, sizeof f);
  f[0][0] = 0;
  for (int i = 1; i <= k; i++) {
    for (int j = 0; j <= M; j++) {
      for (int u = min(p - 1, j); u >= 0; u--) {
        if (f[i - 1][j - u] <= n)
          f[i][j] = min(nex[f[i - 1][j - u]][j % p], f[i][j]);
      }
    }
  }
  for (int j = a[n]; j <= M; j += p)
    if (f[k][j] <= n) {
      cout << j;
      return 0;
    }
}
