#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
const long long INFL = 1e18 + 123;
const double PI = atan2(0, -1);
mt19937 tw(960172);
long long rnd(long long x, long long y) {
  static uniform_int_distribution<long long> d;
  return d(tw) % (y - x + 1) + x;
}
void solve() {
  int n, k, p;
  cin >> n >> k >> p;
  vector<int> inp;
  for (int i = 0; i < n; ++i) {
    int num;
    cin >> num;
    inp.push_back(num);
  }
  vector<vector<int>> d(k, vector<int>(p, -INF));
  d[0][0] = 0;
  for (int i = 0; i < n - 1; ++i) {
    vector<vector<int>> next(k, vector<int>(p, -INF));
    for (int j = 0; j < k; ++j) {
      for (int j2 = 0; j2 < p; ++j2) {
        next[j][(j2 + inp[i]) % p] = max(next[j][(j2 + inp[i]) % p], d[j][j2]);
        if (j < k - 1) {
          next[j + 1][0] = max(next[j + 1][0], d[j][j2] + (j2 + inp[i]) % p);
        }
      }
    }
    swap(d, next);
  }
  int ans = 0;
  for (int j = 0; j < p; ++j) {
    ans = max(ans, d[k - 1][j] + (j + inp[n - 1]) % p);
  }
  cout << ans << "\n";
}
int main() {
  cerr << fixed << setprecision(15);
  cout << fixed << setprecision(15);
  ios::sync_with_stdio(false);
  int tests = 1;
  for (int it = 1; it <= tests; ++it) {
    solve();
  }
  return 0;
}
