#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
using V = vector<T>;
template <class T, class U>
using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
constexpr ll MOD = 1000000007;
constexpr ll HIGHINF = (ll)1e18;
constexpr int INF = 1e9;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  ll m;
  cin >> n >> m;
  V<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  int h1 = (n + 1) / 2;
  set<ll> modm = {0};
  for (int state = 0; state < (1 << h1); state++) {
    ll aa = 0;
    for (int j = 0; j < h1; j++) {
      if (state >> j & 1) aa += ll(a[j]);
    }
    modm.insert(aa % m);
  }
  ll ans = *modm.rbegin();
  int h2 = n - h1;
  for (int state = 0; state < (1 << h2); state++) {
    ll aa = 0;
    for (int j = 0; j < h2; j++) {
      if (state >> j & 1) aa += ll(a[h1 + j]);
    }
    ll mm = m - 1 - (aa % m);
    auto it = modm.upper_bound(mm);
    chmax(ans, *prev(it) + (aa % m));
  }
  cout << ans << '\n';
  return 0;
}
