#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e4;
const int MAXK = 50;
const int INF = 0x3F3F3F3F;
pair<int, int> tree[4 * MAXN + 1][MAXK + 1];
int pre[MAXN + 1];
int a[MAXN + 1];
void fast_io() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
void update(int v, int tl, int tr, int pos, int val, int i) {
  if (tl == tr) {
    tree[v][i] = make_pair(val, pos);
  } else {
    int mid = (tl + tr) >> 1;
    int L = (v << 1) | 1, R = (L + 1);
    if (pos <= mid)
      update(L, tl, mid, pos, val, i);
    else
      update(R, mid + 1, tr, pos, val, i);
    tree[v][i] = max(tree[L][i], tree[R][i]);
  }
}
pair<int, int> query(int v, int tl, int tr, int l, int r, int i) {
  if (l > tr or r < tl) return make_pair(-1, -1);
  if (l <= tl and r >= tr) return tree[v][i];
  int mid = (tl + tr) >> 1;
  int L = (v << 1) | 1, R = (L + 1);
  return max(query(L, tl, mid, l, r, i), query(R, mid + 1, tr, l, r, i));
}
int main() {
  fast_io();
  int n, k, mod;
  cin >> n >> k >> mod;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    pre[i] = (a[i] + pre[i - 1]) % mod;
  }
  for (int i = 0; i <= MAXN; i++) {
    for (int j = 0; j <= MAXK; j++) tree[i][j] = make_pair(-1, -1);
  }
  update(0, 0, n, 0, 0, 0);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= k; j++) {
      pair<int, int> res = query(0, 0, n, 0, i - 1, j - 1);
      int curr = res.first + (pre[i] - pre[res.second] + mod) % mod;
      update(0, 0, n, i, curr, j);
    }
  }
  cout << query(0, 0, n, n, n, k).first << '\n';
  return 0;
}
