inf = open('sochi.in', 'r')
ouf = open('sochi.out', 'w')

n, d = map(int, inf.readline().split())
a = [int(x) for x in inf.readline().split()]

big = [x for x in a if x >= 3 * d]
mid = [x for x in a if x >= 2 * d and x < 3 * d]
mid.sort()

def add(x, y):
    if x == 0:
        return y
    else:
        return x + y - 2 * d

res = max(a)

con = 0
for x in big:
    con = add(con, x)
if len(mid) == 1:
    con = add(con, mid[0])
if len(mid) > 1:
    for x in mid[-2:]:
        con = add(con, x)

print(max(con, res), file = ouf)

    
