#include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
int n, n1, n2, m;
int num[maxn];
set<int> st[2];
set<int> ans1, ans2;
set<int>::iterator it;
set<int>::iterator itt;
void get_st(int n) {
  st[0].clear();
  st[1].clear();
  st[0].insert(0);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &num[i]);
    num[i] %= m;
    int now = (i + 1) % 2;
    st[now] = st[!now];
    for (it = st[!now].begin(); it != st[!now].end(); ++it) {
      st[now].insert((num[i] + *it) % m);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  scanf("%d%d", &n, &m);
  n1 = (n + 1) / 2;
  n2 = n - n1;
  get_st(n1);
  ans1 = st[n1 % 2];
  get_st(n2);
  ans2 = st[n2 % 2];
  it = ans1.end();
  --it;
  int ans = *it;
  it = ans2.end();
  --it;
  ans = max(ans, *it);
  for (it = ans1.begin(); it != ans1.end(); ++it) {
    itt = ans2.upper_bound(m - 1 - *it);
    --itt;
    ans = max(ans, *it + *itt);
    itt = ans2.upper_bound(2 * m - 1 - *it);
    --itt;
    ans = max(ans, (*it + *itt) % m);
  }
  printf("%d\n", ans);
  return 0;
}
