#include <bits/stdc++.h>
using namespace std;
const int MaxN = 100 + 5;
int n, P;
int a[MaxN];
int dp[400000 + 5], t[400000 + 5];
vector<int> v1, v2;
void DP() {
  dp[0] = t[0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = P - 1; j >= 0; j--) {
      if (!dp[j]) continue;
      t[(j + a[i]) % P] = true;
    }
    memcpy(dp, t, sizeof(t));
  }
  for (int i = P - 1; i >= 0; i--) {
    if (dp[i]) {
      cout << i << endl;
      return;
    }
  }
}
int BinarySearch(int val) {
  int l = 0, r = v2.size() - 1, ans;
  if (val + v2[l] >= P) return 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (val + v2[mid] < P)
      ans = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  return val + v2[ans];
}
int main() {
  cin >> n >> P;
  for (int i = 1; i <= n; i++) cin >> a[i];
  if (P <= 200000) {
    DP();
    return 0;
  }
  int l = n / 2;
  int r = n - l;
  for (int state = 0; state < (1 << l); state++) {
    int expect = 0;
    for (int i = 1; i <= l; i++) {
      if (state & (1 << (i - 1))) expect = (expect + a[i]) % P;
    }
    v1.push_back(expect);
  }
  for (int state = 0; state < (1 << r); state++) {
    int expect = 0;
    for (int i = l + 1; i <= n; i++) {
      if (state & (1 << (i - l - 1))) expect = (expect + a[i]) % P;
    }
    v2.push_back(expect);
  }
  sort(v1.begin(), v1.end());
  sort(v2.begin(), v2.end());
  int ans = 0;
  for (int i = 0; i < v1.size(); i++) {
    int _ = v1[i];
    int sup = BinarySearch(_);
    ans = max(ans, sup);
  }
  cout << ans << endl;
  return 0;
}
