#include <bits/stdc++.h>
using namespace std;
int n, m, mod, atot, btot, ans, t[40], a[400005], b[400005];
void dfs1(int pos, int res) {
  if (pos == m + 1) {
    a[++atot] = res % mod;
    return;
  }
  dfs1(pos + 1, res);
  dfs1(pos + 1, (res + t[pos]) % mod);
}
void dfs2(int pos, int res) {
  if (pos == n + 1) {
    b[++btot] = res % mod;
    return;
  }
  dfs2(pos + 1, res);
  dfs2(pos + 1, (res + t[pos]) % mod);
}
int main() {
  scanf("%d%d", &n, &mod);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &t[i]);
    t[i] %= mod;
  }
  if (n == 1) {
    printf("%d\n", t[1]);
    return 0;
  }
  m = n >> 1;
  dfs1(1, 0);
  dfs2(m + 1, 0);
  sort(a + 1, a + atot + 1);
  sort(b + 1, b + btot + 1);
  for (int i = 1, j = btot; i <= atot; i++) {
    while (j > 0 && a[i] + b[j] >= mod) j--;
    ans = max(ans, a[i] + b[j]);
  }
  printf("%d\n", ans);
  return 0;
}
