#include <bits/stdc++.h>
using namespace std;
const int maxn = 40;
int n, m;
long long ans = 0;
int a[maxn];
vector<long long> v[3];
void dfs(int now, int e, long long sum, int opt) {
  if (now > e) {
    v[opt].push_back(sum % m);
    return;
  }
  dfs(now + 1, e, sum + a[now], opt);
  dfs(now + 1, e, sum, opt);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
  }
  int mid = n / 2;
  dfs(1, mid, 0, 1);
  dfs(mid + 1, n, 0, 2);
  for (int i = 1; i <= 2; i++) sort(v[i].begin(), v[i].end());
  for (int i = 0; i < v[1].size(); i++) {
    int pos =
        lower_bound(v[2].begin(), v[2].end(), m - v[1][i]) - v[2].begin() - 1;
    ans = max(ans, (v[1][i] + v[2][pos]) % m);
  }
  printf("%lld\n", ans);
  return 0;
}
