#include <bits/stdc++.h>
using namespace std;
void JIZZ(string output = "") {
  cout << output;
  exit(0);
}
const long double PI = 3.14159265358979323846264338327950288;
const long double eps = 1e-13;
const long long mod = 1e9 + 7;
struct node {
  node *l, *r;
  int mx;
  node() : l(0), r(0), mx(-(1 << 30)) {}
} * root, pool[100000];
int pptr = -1;
int a[20005], pre[20005], dp[53][20005];
node *nnode() {
  ++pptr;
  pool[pptr].l = pool[pptr].r = 0;
  pool[pptr].mx = -(1 << 30);
  return &pool[pptr];
}
void build(node *now, int l, int r) {
  if (l == r) return;
  build(now->l = nnode(), l, ((l + r) >> 1));
  build(now->r = nnode(), ((l + r) >> 1) + 1, r);
}
void modify(node *now, int l, int r, int x, int v) {
  if (l == r) {
    now->mx = max(now->mx, v);
    return;
  }
  if (x <= ((l + r) >> 1))
    modify(now->l, l, ((l + r) >> 1), x, v);
  else
    modify(now->r, ((l + r) >> 1) + 1, r, x, v);
  now->mx = max(now->l->mx, now->r->mx);
}
int query(node *now, int l, int r, int ql, int qr) {
  ;
  ;
  if (qr < l || r < ql) return -(1 << 30);
  if (ql <= l && r <= qr) return now->mx;
  return max(query(now->l, l, ((l + r) >> 1), ql, qr),
             query(now->r, ((l + r) >> 1) + 1, r, ql, qr));
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k, p;
  cin >> n >> k >> p;
  for (int i = 1; i <= n; ++i) cin >> a[i], a[i] %= p;
  for (int i = 1; i <= n; ++i) pre[i] = (pre[i - 1] + a[i]) % p;
  for (int i = 1; i <= n; ++i) dp[0][i] = -(1 << 30);
  for (int i = 1; i <= k; ++i) {
    pptr = -1;
    build(root = nnode(), 0, p - 1);
    modify(root, 0, p - 1, 0, 0);
    for (int j = 1; j <= n; ++j) {
      dp[i][j] = pre[j] + query(root, 0, p - 1, 0, pre[j]);
      if (pre[j] != p - 1)
        dp[i][j] = max(dp[i][j],
                       pre[j] + query(root, 0, p - 1, pre[j] + 1, p - 1) + p);
      modify(root, 0, p - 1, pre[j], -pre[j] + dp[i - 1][j]);
      ;
      ;
    }
  }
  cout << dp[k][n] << endl;
}
