You have m = n·k wooden staves. The i-th stave has length a_{i}. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.

Let volume v_{j} of barrel j be equal to the length of the minimal stave in it. [Image] 

You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |v_{x} - v_{y}| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.

Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.


-----Input-----

The first line contains three space-separated integers n, k and l (1 ≤ n, k ≤ 10^5, 1 ≤ n·k ≤ 10^5, 0 ≤ l ≤ 10^9).

The second line contains m = n·k space-separated integers a_1, a_2, ..., a_{m} (1 ≤ a_{i} ≤ 10^9) — lengths of staves.


-----Output-----

Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |v_{x} - v_{y}| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.


-----Examples-----
Input
4 2 1
2 2 1 2 3 2 2 3

Output
7

Input
2 1 0
10 10

Output
20

Input
1 2 1
5 2

Output
2

Input
3 2 1
1 2 3 4 5 6

Output
0



-----Note-----

In the first example you can form the following barrels: [1, 2], [2, 2], [2, 3], [2, 3].

In the second example you can form the following barrels: [10], [10].

In the third example you can form the following barrels: [2, 5].

In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.

Difficulty: interview

Starter code:

Solutions:
def ii():
    return int(input())
def mi():
    return list(map(int, input().split()))
def li():
    return list(mi())

N, K, L = mi()
A = li()
A.sort()
end = 1
while end < N * K and A[end] <= A[0] + L:
    end += 1
B = A[:end]
if len(B) < N:
    ans = 0
else:
    cur = ans = 0
    for i in range(N):
        ans += B[cur]
        cur = min(cur + K, len(B) - (N - i - 1))
print(ans)


import bisect;
def getIntList():
    return list(map(int, input().split()));
def getTransIntList(n):
    first=getIntList();
    m=len(first);
    result=[[0]*n for _ in range(m)];
    for i in range(m):
        result[i][0]=first[i];
    for j in range(1, n):
        curr=getIntList();
        for i in range(m):
            result[i][j]=curr[i];
    return result;
n, k, l=getIntList();
m=n*k;
a=getIntList();
a.sort();
lowLim=a[0];
upLim=lowLim+l;
#print(a);
def solve():
    if a[n-1]>upLim:
        return 0;
    i0=bisect.bisect_right(a, upLim);
    nbBad=m-i0;
    i0-=1;
    value=0;
    for i in range(n-1, -1, -1):
        diff=min(nbBad, k-1);
        rest=k-diff;
        nbBad-=diff;
        value+=a[i0-rest+1];
        #print(i0-rest+1, a[i0-rest+1])
        i0-=rest;
    return value;
print(solve());

T = input().split(' ')
n = int(T[0])
k = int(T[1])
l = int(T[2])
P = input().split(' ')
for i in range(n * k):
    P[i] = int(P[i])
P.sort()
a = 0
while a < n * k and P[a] - P[0] <= l:
    a+=1
if a < n:
    print(0)
else:
    tot = 0
    i = 0
    j = a
    for g in range(n-1, -1, -1):
        tot += P[i]
        i+=1
        j-=1
        for f in range(k-1):
            if j > g:
                i+=1
                j-=1
            else:
                break
    print(tot)


n, k, l = list(map(int, input().split()))
a = sorted(list(map(int, input().split())))

p = 0
while p < len(a) and a[p] - a[0] <= l:
    p += 1

if p < n:
    print(0)
    return

ans = 0
u = 0
for i in range(n):
    ans += a[u]
    if u + k >= p - (n - i - 1):
        u = p - (n - i - 1)
    else:
        u += k
print(ans)


s = [int(x) for x in input().split()]
n, k, ll = s[0], s[1], s[2]
s = [int(x) for x in input().split()]
s.sort()

if s[n - 1] - s[0] > ll:
	print(0)
else:
	x = 0
	while x < n * k and s[x] <= ll + s[0]:
		x += 1
	if x > k * (n - 1):
		q = 0
		for i in range(n):
			q += s[i * k]
		print(q)
	else:
		add = []
		left = x - n
		while left > 0:
			if left >= k - 1:
				add.append(k - 1)
				left -= k - 1
			else:
				add.append(left)
				left = 0
		add += [0 for _ in range(n)]
		diff = [add[i] + 1 for i in range(n - 1)]
		# print(diff)
		q = s[0]
		p = 0
		for i in range(n - 1):
			p += diff[i]
			q += s[p]
		print(q)


n, k, l = map(int,input().split()) 
L = list(map(int,input().split()))
L.sort()
if L[n-1]-L[0]>l:
    print(0)
else:
    F = L[0:n]
    D = []
    # print(F)
    count = 0
    ss = 0
    for i in range(n,n*k):
        if L[i]-L[0]<=l:
            F.append(L[i])
            count+=1
            if count==k-1:
                D.append(L[ss])
                ss+=k
                count = 0
        else:
            break
    # print(F[ss:])
    # print(D)
    if count == 0:
        print(sum(F[ss:])+sum(D))
    else:
        print(F[ss] + sum(F[ss+count+1:]) + sum(D))

n, k, l = list(map(int, input().split()))
arr = list(map(int, input().split()))
arr = sorted(arr)
if arr[n - 1] - arr[0] > l:
    print(0)
else:
    maxVol = arr[0] + l
    index = 0
    while index < n * k - 1 and arr[index + 1] <= maxVol: #index will be at most n * k - 1
        index += 1
    s = arr[0]
    i = 0
    remaining = n - 1
    while remaining > 0 and i + k + remaining - 1 <= index: #jumps of k
        i += k
        remaining -= 1
        s += arr[i]
    if remaining > 0 and index - (remaining - 1) > 1 + i: #can now only make a jump of at most k - 1
        i = (index - (remaining - 1))
        s += arr[i]
        remaining -= 1
    while remaining > 0:
        i += 1
        s += arr[i]
        remaining -= 1
    print(s)

n,k,l=[int(x) for x in input().split()]
ns=[int(x) for x in input().split()]
loc=-1
def judge(start,restb):
    length=loc-start+1
    if length-k>=(restb-1):
        return True
    return False

def func():
    nonlocal ns,loc
    if n==1:
        print(min(ns))
        return
    ns.sort()
    # print(ns)
    for i in range(n*k):
        if ns[i]>ns[0]+l:
            loc=i
            break
    if loc==-1:
        loc=n*k-1
    else:
        loc-=1
    if loc<n-1:
        print(0)
        return
    st=0
    rb=n
    ans=0
    while judge(st,rb):
        ans+=ns[st]
        st+=k
        rb-=1
        if rb==0:
            break
    if rb>0:
        ans+=ns[st]
        rb-=1
        for i in range(rb):
            ans+=ns[loc-i]
    print(ans)
    return
func()



t = input().split(" ")
t = [int(e) for e in t]

(n, k, l) = t


big = input().split(" ")
big = [int(e) for e in big]

assert len(big) == n * k

big.sort()


def getResult(n, k, l, big):
    if big[n-1] - big[0] > l:
        return 0

    right = None
    for (i, e) in enumerate(big):
        if e - big[0] > l:
            right = i
            break
    if right == None:
        right = len(big)

    n_fei = right - n

    result = 0

    i = 0
    for todo in range(n):
        result += big[i]

        if n_fei >= k - 1:
            i += k
            n_fei -= k-1
        else:
            i += n_fei + 1
            n_fei -= n_fei

    return result








result = getResult(n, k, l, big)
print(result)


#!/usr/bin/env python3

from bisect import bisect

[n, k, l] = list(map(int, input().strip().split()))
ais = list(map(int, input().strip().split()))

ais.sort()
if ais[n - 1] - ais[0] > l:
	print(0)
	return

inds = list(range(0, n * k, k))
bnd = bisect(ais, ais[0] + l) - 1
for i in range(1, n):
	inds[i] = min(inds[i], bnd)

for i in range(n - 1, 1, -1):
	if inds[i] <= inds[i - 1]:
		inds[i - 1] = inds[i] - 1

print(sum(ais[i] for i in inds))


def main():
    n, k, l = map(int, input().split())
    m = n * k
    a = list(map(int, input().split()))
    a.sort()
    f = a[0]
    i = 0
    while (i < m and a[i] <= f + l):
        i += 1
    if (i != m):
        i -= 1
    else:
        ans = 0
        ind = 0
        for i in range(n):
            ans += a[ind]
            ind += k
        print(ans)
        return
    last = i
    if (last + 1 < n):
        print(0)
        return
    count = m - last - 1
    ost = last + 1
    ans = 0
    i = last
    while (i >= 0):
        c = min(k - 1, count)
        count -= c
        i -= (k - c)
        ans += a[i + 1]
    print(ans)
main()

#!/usr/bin/env python3
from sys import stdin
import bisect


def solve(tc):
    n, k, l = map(int, stdin.readline().split())
    li = list(map(int, stdin.readline().split()))
    li.sort()

    p = bisect.bisect_right(li, li[0] + l)
    if p < n:
        print(0)
        return

    res = 0
    pp = 0
    rem = p - n
    while pp < p:
        res += li[pp]
        if rem > 0:
            mod = min(k-1, rem)
            pp += mod
            rem -= mod
        pp += 1
    print(res)


tc = 1
solve(tc)

#!/usr/bin/env python3
from sys import stdin, stdout
from bisect import bisect

def rint():
    return map(int, stdin.readline().split())
#lines = stdin.readlines()

n, k, l = rint()

m = list(rint())

m.sort()
#print(m)
if m[-1] - m[0] > l:
    totcand = bisect(m, m[0]+l)
    #print(totcand)
    tot = 0
    i = 0
    nn = n
    while True:
        if nn == 0:
            break
        tot += m[i]
        i += 1
        nn -= 1
        totcand -= 1
        if totcand - (k-1)  >=  nn:
            i += (k-1)
            totcand -= (k-1)
        else:
            if totcand > nn:
                minus = totcand - nn
                i += minus
                totcand -= minus
            elif totcand < nn:
                print(0)
                return
    print(tot)
else:
    tot = 0
    for i in range(n):
        tot += m[i*k]

    print(tot)

n, k, l = list(map(int, input().split()))
a = sorted(list(map(int, input().split(' '))))
# print(n, k, l, a)
index = 0
for i in a:
    if i > a[0] + l:
        break
    index += 1
ok = index - n
if ok < 0:
    print(0)
else:
    ans = 0
    cur = 0
    for i in range(n):
        ans += a[cur]
        use = min(k-1, ok)
        ok -= use
        cur += 1 + use
    print(ans)

n, k, l = map(int, input().split())
barrels = []
for i in range(n):
    barrels.append([])
    for j in range(k):
        barrels[i].append(-1)
boards = sorted(list(map(int, input().split())))
ans = 0
if boards[n - 1] - boards[0] <= l:
    i = n * k - 1
    ba, bo = n - 1, k
    while boards[i] > boards[0] + l:
        bo -= 1
        if bo == 0:
            ba, bo = ba - 1, k - 1
        barrels[ba][bo] = boards[i]
        i -= 1
    ba, bo = 0, -1
    j = 0
    flag = True
    while j <= i:
        bo += 1
        if bo == k or barrels[ba][bo] != -1:
            ba, bo = ba + 1, 0
            flag = True
        barrels[ba][bo] = boards[j]
        if flag:
            ans += barrels[ba][bo]
            flag = False  
        j += 1
print(ans)

n, k, l = list(map(int, input().split()))
a = list(map(int, input().split()))

a.sort()

ub = n*k
for i in range(n*k):
    if a[i] - a[0] > l:
        ub = i
        break

if ub < n:
    print(0)
    return

res = 0
left = n
picked = [0] * (n*k)

for i in range(0, ub, k):
    res += a[i]
    picked[i] = 1
    left -= 1

i = ub - 1
while left > 0 and i >= 0:
    if picked[i] == 0:
        res += a[i]
        left -= 1

    i -= 1

assert(left == 0)
print(res)


import bisect,sys
n,k,l=list(map(int,input().split()))
a=sorted(list(map(int,input().split())))
li=bisect.bisect_right(a,a[0]+l)-1
#print(li)
if li+1<n:
    print(0)
    return
b=[False]*(n*k)
m=0
i=0
t=0
while i<=li:
    m=m+a[i]
    t+=1
    b[i]=True
    i=i+k
    #print(a[i],i)
i=li
while t<n:
    if b[i]==False:
        m=m+a[i]
        t+=1
    i-=1
print(m)
        


n, k, l = list(map(int, input().split()))
a = list(map(int, input().split()))
a0 = min(a)

v = []
for ai in a:
    if ai <= a0 + l:
        v.append(ai)
v = list(sorted(v))
m = len(v)
if m < n:
    ans = 0
else:
    ans = 0
    a = n
    b = m
    i = 0
    j = 0
    for vi in v:
        if i == 0:
            ans += vi
        if i == k - 1 or a == b:
            i = 0
            j += 1
            a -= 1
        else:
            i += 1
        b -= 1

print(ans)


n,k,l=list(map(int,input().split()))
a=sorted(list(map(int,input().split())))
i=1
m=k*n
if(a[n-1]-a[0]>l):
    print(0)
else:
    while(i<m and a[i]<=a[0]+l):
        i+=1
    if(i==m or m-i+1<k):
        ans=0
        for j in range(0,m,k):
            ans+=a[j]
        print(ans)
    else:
        usables=m-i-k+1
        ind=i-1
        ans=0
        while(ind>=0):
            ans+=a[ind]
            if(usables>=k-1):
                usables-=k-1
                ind-=1
            elif(usables>0):
                ind-=(k-usables)
                usables=0
            else:
                ind-=k
        print(ans)

R = lambda: list(map(int, input().split()))

n, k, l = R()
a = list(R())
a.sort()


def func():
    if n == 1: return a[0]

    r = (n-1)*k
    while a[r] - a[0] > l: r -= 1
    # dbvar(r)
    if r < n - 1: return 0

    res = a[0] + a[r]

    for i in range(n - 2, 0, -1):
        if i*k >= r - 1:
            res += a[r-1]
            r -= 1
        else:
            res += a[i*k]

    return res

print(func())


import sys, math, os.path

FILE_INPUT = "C.in"
DEBUG = os.path.isfile(FILE_INPUT)
if DEBUG: 
    sys.stdin = open(FILE_INPUT)    

def ni():
    return list(map(int, input().split(" ")))

def nia(): 
    return list(map(int,input().split()))

def log(x):
    if (DEBUG):
        print(x)

n,k,l = ni()
a = nia()
# print(n,k,l)
log(a)

vmin = min(a)
log(vmin)

if (n==1):
    print(vmin)
    return

vmaxa = vmin + l
log(vmaxa)
amax = [i for i in a if i <= vmaxa]
log(f"{amax} -> {len(amax)}")

if (len(amax) < n):
    print(0)

else:
    amax.sort()
    log(amax)
    ans = 0
    count = 0
    lonhonMax = n*k - len(amax)
    for i  in amax[0::k]:
        log(f"+ {count*k}: {i}")
        ans += i
        count += 1
        if (count == n):
            break

    if (count < n):
        ix = len(amax) - 1
        log(f"add more={n} - {count} = {n - count}")
        for i in range(n - count):
            if (ix % k == 0):
                ix -= 1
            ans += amax[ix]
            ix -= 1
    print(ans)     
        



import sys, math, os.path

FILE_INPUT = "C.in"
DEBUG = os.path.isfile(FILE_INPUT)
if DEBUG: 
    sys.stdin = open(FILE_INPUT)    

def ni():
    return list(map(int, input().split(" ")))

def nia(): 
    return list(map(int,input().split()))

def log(x):
    if (DEBUG):
        print(x)

n,k,l = ni()
a = nia()
# print(n,k,l)
log(a)

vmin = min(a)
log(vmin)

if (n==1):
    print(vmin)
    return

vmaxa = vmin + l
log(vmaxa)
amax = [i for i in a if i <= vmaxa]
# log(f"{amax} -> {len(amax)}")

if (len(amax) < n):
    print(0)

else:
    amax.sort()
    log(amax)
    ans = 0
    count = 0
    lonhonMax = n*k - len(amax)
    for i  in amax[0::k]:
        # log(f"+ {count*k}: {i}")
        ans += i
        count += 1
        if (count == n):
            break

    if (count < n):
        ix = len(amax) - 1
        # log(f"add more={n} - {count} = {n - count}")
        for i in range(n - count):
            if (ix % k == 0):
                ix -= 1
            ans += amax[ix]
            ix -= 1
    print(ans)     
        



n, k, l = list(map(int, input().split()))
m = n * k
arr = list(map(int, input().split()))
arr.sort(reverse=True)
#print(arr)
min_elem = arr[m - 1]
r = min_elem + l
#print("m =", m, "r =", r, arr[m - n])
if arr[m - n] > r:
    #print("печаль")
    print("0")
    return
volume = 0
if k == 1:
    for i in range(m):
        volume = volume + arr[i]
    print(volume)
    return
########

index = 0
while arr[index] > r:
    index += 1
volume = 0
#print(index)
q = index // (k - 1)
w = index % (k - 1)
while q != 0:
    volume = volume + arr[index]
    q -= 1
    index += 1
w = k - 1 - w
index += w
while index < m:
    volume = volume + arr[index]
    index += k

#print(index)
print(volume)


a,b,c = [int(i) for i in input().split()]
length = a*b
numArr = [int(i) for i in input().split()]

numArr.sort()

count = 0
mmin = numArr[0]
mmax = numArr[a-1]

if mmax - mmin > c:
    print(0)
else:
    maxPos = a-1
    while(maxPos+1 < length and numArr[maxPos+1]<=mmin+c):
        maxPos+=1
    dis = maxPos+1 - a
    bCount = 0
    for i in range(maxPos+1):
        if dis != 0:
            bCount += 1
            dis -= 1
            if bCount%b == 1:
                count += numArr[i]
                dis += 1
        else:
            count += numArr[i]
    print(count)


n, k, l = list(map(int, input().split()))
a = sorted(list(map(int, input().split(' '))))
index = 0
for i in a:
    if i > a[0] + l:
        break
    index += 1
ok = index - n
if ok < 0:
    print(0)
else:
    ans = 0
    cur = 0
    for i in range(n):
        ans += a[cur]
        use = min(k-1, ok)
        ok -= use
        cur += 1 + use
    print(ans)