intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> n >> p; m = n / 2; for (int i = 1; i <= m; ++i) cin >> a[i]; for (int i = m + 1; i <= n; ++i) cin >> b[i - m]; n = n - m; for (int i = 0; i < (1 << m); ++i) { int sum = 0; for (int j = 1; j <= m; ++j) if (i >> j - 1 & 1) sum = (sum + a[j]) % p; u[++c1] = sum; } for (int i = 0; i < (1 << n); ++i) { int sum = 0; for (int j = 1; j <= n; ++j) if (i >> j - 1 & 1) sum = (sum + b[j]) % p; v[++c2] = sum; } sort(u + 1, u + c1 + 1); sort(v + 1, v + c2 + 1); int j = c2, ans = (u[c1] + v[c2]) % p; for (int i = 1; i <= c1; ++i) { while (j && u[i] + v[j] >= p) --j; ans = max(ans, u[i] + v[j]); } cout << ans << "\n"; return0; }