#include <bits/stdc++.h>
using namespace std;
const int N = (int)5e5 + 4;
int main() {
  long long int n, m;
  cin >> n >> m;
  long long int ar[n];
  for (int i = 0; i <= n - 1; i++) {
    cin >> ar[i];
  }
  vector<long long int> v1, v2;
  long long int k = n / 2;
  long long int limit = 1 << k;
  for (long long int i = 0; i <= limit - 1; ++i) {
    long long int curr = 0;
    for (int j = 0; j <= k - 1; j++) {
      if (i & (1 << j)) {
        curr = (curr + ar[j]) % m;
      }
    }
    v1.push_back(curr);
  }
  long long int p = k;
  k = n - k;
  limit = 1 << k;
  for (long long int i = 0; i <= limit - 1; ++i) {
    long long int curr = 0;
    for (int j = 0; j <= k - 1; j++) {
      if (i & (1 << j)) {
        curr = (curr + ar[j + p]) % m;
      }
    }
    v2.push_back(curr);
  }
  sort(v2.begin(), v2.end());
  long long int ans = 0;
  for (__typeof(v1.begin()) it = v1.begin(); it != v1.end(); it++) {
    long long int ser = *it;
    ser = m - ser;
    vector<long long int>::iterator itr =
        lower_bound(v2.begin(), v2.end(), ser);
    if (itr != v2.begin()) itr--;
    ans = max(ans, (*it + *itr) % m);
  }
  cout << ans;
  return 0;
}
