#!/usr/bin/env python3 def SubsetSums(X, t, B): # O(|X|*B), based on "Linear Time Algorithms for Knapsack Problems # with Bounded Weights" by David Pisinger # (This implementation is the result of 5 minutes of "optimizing" the basic version.) n = len(X) MaxPref = [-1]*(2*B+1) Inc = [None]*(2*B+1) s = 0 #X.sort() for brk in range(n): if s + X[brk] > t: MaxPref[s-(t-B)] = brk break s += X[brk] for i in range(brk, n): nInc = 0 for d in range(B, -1, -1): ov = MaxPref[d+X[i]] nv = MaxPref[d] if nv > ov: MaxPref[d+X[i]] = nv Inc[nInc] = (d+X[i], nv-ov) nInc += 1 for ii in range(nInc): (d, inc) = Inc[ii] for j in range(MaxPref[d]-inc, MaxPref[d]): MaxPref[d-X[j]] = max(MaxPref[d-X[j]], j) return [t-B+i for i in range(2*B+1) if MaxPref[i] >= 0] (n, x) = map(int, input().split()) T = list(sorted(map(int, input().split()))) max_T = T[-1] while T and T[-1] > x: T.pop() if len(T) > 4: Jobs = [x+1-t for t in T[4:]] tot = sum(Jobs) ans = x+1 + min(max(s, tot-s) for s in SubsetSums(Jobs, tot // 2, max(Jobs))) elif len(T) > 2: ans = x+1 else: ans = max_T print(max(ans, max_T))