#include <bits/stdc++.h>
using namespace std;
int pd[2][55][105], n, k, p, v[20005];
int main() {
  cin >> n >> k >> p;
  for (int i = 1; i <= n; ++i) {
    cin >> v[i];
    v[i] %= p;
  }
  int sum = 0;
  memset(pd, -1, sizeof(pd));
  pd[1][1][v[1]] = v[1];
  sum = v[1];
  for (int i = 2; i <= n; ++i) {
    sum += v[i];
    sum %= p;
    pd[i % 2][1][sum] = sum;
    for (int j = 0; j < p; ++j)
      if (j != sum) pd[i % 2][1][j] = pd[1 - (i % 2)][1][j];
    for (int t = 2; t <= k && t <= i; ++t) {
      for (int j = 0; j < p; ++j)
        if (pd[1 - (i % 2)][t - 1][j] != -1) {
          int add = sum - j;
          if (add < 0) add += p;
          pd[i % 2][t][sum] =
              max(pd[i % 2][t][sum], pd[1 - (i % 2)][t - 1][j] + add);
        }
      for (int j = 0; j < p; ++j)
        if (j != sum) pd[i % 2][t][j] = pd[1 - (i % 2)][t][j];
    }
  }
  cout << pd[n % 2][k][sum];
  return 0;
}
