#include <bits/stdc++.h>
using namespace std;
int n, m;
int arr[50];
vector<int> fhalf;
set<int> shalf;
int brute(int ind, long long int curr) {
  if (ind == n) {
    return curr % m;
  }
  int ans = 0;
  ans = max(brute(ind + 1, curr), ans);
  ans = max(brute(ind + 1, (curr + arr[ind])), ans);
  return ans;
}
void secondhalf(int ind, long long int curr) {
  if (ind == n) {
    shalf.insert(curr % m);
    return;
  }
  secondhalf(ind + 1, curr);
  secondhalf(ind + 1, (curr + arr[ind]));
}
void firsthalf(int ind, long long int curr) {
  if (ind == 18) {
    fhalf.push_back(curr % m);
    return;
  }
  firsthalf(ind + 1, curr);
  firsthalf(ind + 1, (curr + arr[ind]));
}
int solve() {
  int ans = 0;
  for (int el : fhalf) {
    auto it = shalf.lower_bound(m - el);
    if (it == shalf.begin()) {
      it = shalf.end();
      it--;
    } else {
      it--;
    }
    int curr = el + *it;
    if (curr >= m) {
      curr -= m;
    }
    ans = max(ans, curr);
  }
  return ans;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
    arr[i] %= m;
  }
  if (n < 21) {
    cout << brute(0, 0);
  } else {
    firsthalf(0, 0);
    secondhalf(18, 0);
    cout << solve();
  }
  return 0;
}
