#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, mod;
  cin >> n >> mod;
  vector<long long> v(n);
  for (long long i = 0; i < n; ++i) {
    cin >> v[i];
  }
  long long sz = n / 2;
  vector<long long> first((1 << sz));
  for (long long mask = 0; mask < (1 << sz); ++mask) {
    long long sum = 0;
    for (long long j = 0; j < sz; ++j) {
      if ((mask >> j) & 1) {
        sum += v[j];
        sum %= mod;
      }
    }
    first.push_back(-sum);
  }
  sort(first.begin(), first.end());
  long long ans = -1e9;
  for (long long mask = 0; mask < (1 << (n - sz)); ++mask) {
    long long sum = 0;
    for (long long j = 0; j < (n - sz); ++j) {
      if ((mask >> j) & 1) {
        sum += v[sz + j];
        sum %= mod;
      }
    }
    auto it = upper_bound(first.begin(), first.end(), -(mod - sum));
    if (it == first.end()) {
      continue;
    }
    ans = max(ans, -(*it) + sum);
  }
  cout << ans << "\n";
  return 0;
}
