#include <bits/stdc++.h>
using namespace std;
int MOD;
set<int> Solve(const vector<int>& v) {
  const int S = v.size();
  set<int> out;
  for (int msk = 0; msk < (1 << S); msk++) {
    int tot = 0;
    for (int k = 0; k < S; k++) {
      if (msk & (1 << k)) {
        tot += v[k];
        tot %= MOD;
      }
    }
    out.insert(tot);
  }
  return out;
}
int main() {
  int N;
  cin >> N >> MOD;
  vector<int> A(N);
  for (int i = 0; i < N; i++) cin >> A[i];
  int half = N / 2;
  vector<int> lft(A.begin(), A.begin() + half + 1);
  vector<int> rgt(A.begin() + half + 1, A.end());
  set<int> poss_lft = Solve(lft);
  set<int> poss_rgt = Solve(rgt);
  int best = 0;
  for (int k : poss_lft) {
    auto want = poss_rgt.lower_bound(MOD - k);
    if (want == poss_rgt.begin()) {
      best = max(best, *poss_rgt.begin() + k);
    } else {
      best = max(best, *--want + k);
    }
  }
  printf("%d\n", best);
  return 0;
}
