#include <bits/stdc++.h>
using namespace std;
template <typename TF>
void debug(ostream &out, TF const &f) {
  out << f << std::endl;
}
template <typename TF, typename... TR>
void debug(ostream &out, TF const &f, TR const &...rest) {
  out << f << " ";
  debug(out, rest...);
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vi = vector<int>;
using vii = vector<pii>;
using vll = vector<ll>;
using vs = vector<string>;
const int DR[] = {-1, 0, 1, 0, -1, 1, 1, -1};
const int DC[] = {0, -1, 0, 1, -1, -1, 1, 1};
const double PI = acos(-1.0);
const double EPS = 1e-9;
const int MOD = 1e9 + 7;
const int INF = 1073741823;
const ll INFLL = 4e18;
const int MAX = 2e4;
const int BMAX = 50;
const int PMAX = 100;
int N, K, P, arr[MAX + 5], sum[MAX + 5], dp[BMAX + 5][MAX + 5];
int stree[4 * PMAX + 5];
void update(int id, int l, int r, int pos, int v) {
  if (pos < l || r < pos) return;
  if (l == r) {
    stree[id] = max(stree[id], v);
    return;
  }
  int chld = 2 * id;
  int m = (l + r) / 2;
  update(chld, l, m, pos, v);
  update(chld + 1, m + 1, r, pos, v);
  stree[id] = max(stree[chld], stree[chld + 1]);
}
int query(int id, int l, int r, int x, int y) {
  if (y < l || r < x) return -INF;
  if (x <= l && r <= y) return stree[id];
  int chld = 2 * id;
  int m = (l + r) / 2;
  int ret = max(query(chld, l, m, x, y), query(chld + 1, m + 1, r, x, y));
  return ret;
}
void update(int pos, int v) { update(1, 0, P - 1, pos, v); }
int query(int x, int y) { return query(1, 0, P - 1, x, y); }
void read() {
  cin >> N >> K >> P;
  for (int i = 0; i < (int)N; i++) {
    cin >> arr[i];
    arr[i] %= P;
  }
}
void solve() {
  sum[0] = arr[0];
  for (int i = (int)1; i <= (int)N - 1; i++) sum[i] = (sum[i - 1] + arr[i]) % P;
  for (int i = 0; i < (int)N; i++) dp[1][i] = sum[i];
  for (int k = (int)2; k <= (int)K; k++) {
    for (int i = 0; i < (int)4 * PMAX + 3; i++) stree[i] = -INF;
    for (int i = 0; i < (int)N; i++) {
      if (i < k - 1)
        dp[k][i] = -INF;
      else {
        int seg = max(query(0, sum[i]), query(sum[i] + 1, P - 1) + P);
        dp[k][i] = sum[i] + seg;
      }
      update(sum[i], dp[k - 1][i] - sum[i]);
    }
  }
  cout << dp[K][N - 1] << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int TC = 1;
  for (int tc = (int)1; tc <= (int)TC; tc++) {
    read();
    solve();
  }
}
