#include <bits/stdc++.h>
long long R = 7 + 1e9, NUMTESTCASE;
const int NN = 10 + 1e5;
const double pi = acos(-1.0);
int di[4] = {1, 0, -1, 0}, dj[4] = {0, 1, 0, -1},
    DI[8] = {1, 1, 0, -1, -1, -1, 0, 1}, DJ[8] = {0, 1, 1, 1, 0, -1, -1, -1};
using namespace std;
long long n, m, x, ans;
set<long long, greater<long long> > s2, s1;
void build(vector<long long> v, long long sum, long long it, bool A) {
  if (it == v.size()) {
    ans = max(ans, sum);
    if (A)
      s2.insert(sum);
    else
      s1.insert(sum);
    return;
  }
  build(v, (sum + v[it]) % m, it + 1, A);
  build(v, sum, it + 1, A);
}
int main() {
  cin >> n >> m;
  vector<long long> a, b;
  for (int i = (1); i <= (n); ++i) {
    scanf("%lld", &x);
    if (i <= 18)
      a.push_back(x);
    else
      b.push_back(x);
  }
  build(a, 0, 0, 0);
  build(b, 0, 0, 1);
  for (long long u : s1) {
    auto flag = s2.upper_bound(m - u);
    if (flag == s2.end()) continue;
    ans = max(ans, *flag + u);
  }
  cout << ans;
  return 0;
}
