from functools import reduce
inp = open('sochi.in')
n, d = map(int, inp.readline().split())
a = list(map(int, inp.readline().split()))
greater = [x for x in a if x >= 3 * d]
mid = [x for x in a if 2 * d < x < 3 * d]
base = reduce(lambda x, y : max(max(x, y), x + y - 2 * d), 
        greater + sorted(mid)[-2:], 0) or max(a)
print (max(a), file=open('sochi.out', 'w'))
