#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v.push_back(x);
  }
  vector<int> a;
  for (int mask = 0; mask < (1 << ((n + 1) / 2)); mask++) {
    int cur = 0;
    for (int i = 0; i < (n + 1) / 2; i++) {
      if (mask & (1 << i)) {
        cur += v[i];
        cur %= m;
      }
    }
    a.push_back(cur);
  }
  vector<int> b;
  for (int mask = 0; mask < (1 << ((n + 1) / 2)); mask++) {
    int cur = 0;
    for (int i = (n + 1) / 2; i < n; i++) {
      int kek = i - (n + 1) / 2;
      if (mask & (1 << kek)) {
        cur += v[i];
        cur %= m;
      }
    }
    b.push_back(cur);
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  int gr = b.size() - 1;
  int ans = 0;
  for (int i = 0; i < a.size(); i++) {
    while (b[gr] + a[i] >= m) gr--;
    ans = max(ans, max(b[gr] + a[i], (a[i] + b.back()) % m));
  }
  cout << ans;
  return 0;
}
