#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const int N = 2e4 + 1, mod = 1e9 + 7;
int n, k, p, ans = 0, a[N], dp[2][55][101];
void update_max(int& x, int y) {
  if (x < y) {
    x = y;
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n >> k >> p;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    a[i] %= p;
  }
  for (int j = 0; j <= k; ++j) {
    for (int m = 0; m < p; ++m) {
      dp[0][j][m] = -1e9;
    }
  }
  dp[0][0][0] = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j <= k; ++j) {
      for (int m = 0; m < p; ++m) {
        dp[i & 1][j][m] = -1e9;
      }
    }
    bool cur = i & 1;
    bool pre = cur ^ 1;
    for (int j = 1; j <= k; ++j) {
      for (int m = 0; m < p; ++m) {
        int val = (m + a[i]) % p;
        update_max(dp[cur][j][val], dp[pre][j][m] - m + val);
        update_max(dp[cur][j][a[i]], dp[pre][j - 1][m] + a[i]);
      }
    }
  }
  for (int m = 0; m < p; ++m) {
    ans = max(ans, dp[n & 1][k][m]);
  }
  cout << ans;
}
