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: MEDIUM

Solutions:
from bisect import *
R = lambda : map(int, input().split())
(n, k, l) = R()
a = sorted(R())
t = bisect_right(a, a[0] + l)
if t < n:
	print(0)
else:
	s = sum(a[0:t:k])
	n -= (t - 1) // k + 1
	i = t - 1
	while n:
		if i % k:
			s += a[i]
			n -= 1
		i -= 1
	print(s)


(n, k, l) = list(map(int, input().split()))
a = sorted(list(map(int, input().split())))
a = sorted(filter(lambda x: x - a[0] <= l, a))
if len(a) < n:
	print(0)
else:
	s = i = 0
	j = n
	m = len(a)
	while i < m - j:
		s += a[i]
		i += k
		j -= 1
	for i in range(m - j, m):
		s += a[i]
	print(s)


from bisect import bisect_left, bisect_right

def tuple_input(type):
	return map(type, input().strip().split())
(n, k, l) = tuple_input(int)
a = list(tuple_input(int))
a.sort()
if a[n - 1] - a[0] > l:
	print('0')
	exit(0)
res = 0
lef = 0
rem = n
while rem > 0:
	res += a[lef]
	x = 0
	while x < k and a[lef + x + rem - 1] <= l + a[0]:
		x += 1
	lef += x
	rem -= 1
print(res)


(n, k, l) = 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)
	exit(0)
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)


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
	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())


from bisect import bisect, bisect_left
(n, k, l) = map(int, input().split())
A = sorted(map(int, input().split()))
ans = 0
i = bisect(A, A[0] + l)
if i >= n:
	p = 0
	for b in range(n):
		ans += A[p]
		use = min(k, i - (n - b - 1))
		p += use
		i -= use
print(ans)


def main():
	import sys
	input = sys.stdin.readline
	(N, K, L) = map(int, input().split())
	A = list(map(int, input().split()))
	A.sort()
	amin = A[0]
	amax = amin + L
	B = []
	C = []
	for (i, a) in enumerate(A):
		if a > amax:
			break
		if i % K == 0:
			B.append(a)
		else:
			C.append(a)
	for _ in range(N - len(B)):
		if not C:
			print(0)
			exit()
		B.append(C.pop())
	print(sum(B))
main()


R = lambda : map(int, input().split())
(n, k, l) = R()
a = sorted(R())
s = c = 0
for i in range(n * k - 1, -1, -1):
	c += 1
	if a[i] - a[0] <= l and c >= k:
		s += a[i]
		c -= k
print((s, 0)[c > 0])


import math

def main():
	(n, k, l) = map(int, input().split())
	a = list(map(int, input().split()))
	a.sort()
	index = -1
	for i in range(n * k - 1, -1, -1):
		if a[i] - a[0] <= l:
			index = i
			break
	if index + 1 < n:
		print(0)
		return
	extra = index + 1 - n
	start = 0
	ans = 0
	cnt = 0
	while cnt < n:
		ans += a[start]
		start += min(extra, k - 1) + 1
		extra -= min(extra, k - 1)
		cnt += 1
		if extra < 0:
			extra = 0
	print(ans)
	return
main()


import sys
(n, k, l) = map(int, input().split())
a = sorted(map(int, input().split()))
ans = 0
(pair, right) = (0, 0)
for i in range(n * k - 1, -1, -1):
	right += 1
	if right >= k and a[i] - a[0] <= l:
		ans += a[i]
		right -= k
		pair += 1
if pair < n:
	print(0)
else:
	print(ans)


(n, k, l) = map(int, input().split())
arr = list(map(int, input().split()))
m = n * k
arr.sort()
if arr[n - 1] - arr[0] > l:
	print(0)
	exit(0)
ubi = m
for i in range(n - 1, m):
	if arr[i] - arr[0] > l:
		ubi = i
		break
i = mv = 0
while i + n < ubi:
	mv += arr[i]
	i += k
	n -= 1
print(mv + sum(arr[ubi - n:ubi]))


def main():
	(n, k, l) = map(int, input().split())
	a = sorted(list(map(int, input().split())))
	if a[n - 1] - a[0] > l:
		print(0)
		return
	res = a[0]
	for i in range(1, n):
		pos = k * i
		if a[pos] - a[0] <= l:
			res += a[pos]
		else:
			for j in range(pos - 1, -1, -1):
				if a[j] - a[0] <= l:
					for m in range(j, -1, -1):
						if m % k != 0:
							res += a[m]
							i += 1
							if i == n:
								break
					break
			break
	print(res)
main()


def search(array, i, j, l):
	if i > j or array[i] - array[0] > l:
		return -1
	mid = (i + j) // 2
	if array[mid] - array[0] > l:
		return search(array, i, mid - 1, l)
	else:
		return max(mid, search(array, mid + 1, j, l))
(n, k, l) = input().split()
n = int(n)
k = int(k)
l = int(l)
a = input().split()
a = [int(x) for x in a]
a.sort()
ans = 0
if k == 1:
	if a[n * k - 1] - a[0] <= l:
		i = 0
		while i < n * k:
			ans += a[i]
			i += 1
	else:
		ans = 0
else:
	ind = search(a, 0, n * k - 1, l)
	if ind + 1 < n:
		ans = 0
	else:
		count = (n * k - ind - 1) // (k - 1)
		i = 0
		while i < count:
			ans += a[ind - i]
			i += 1
		i = 0
		while i < n - count:
			ans += a[i * k]
			i += 1
print(ans)


import sys
import bisect

def main():
	(n, k, l) = map(int, sys.stdin.readline().split())
	staves = list(map(int, sys.stdin.readline().split()))
	staves.sort()
	so = bisect.bisect(staves, staves[0] + l)
	(vol, filler) = (staves[:so], staves[so:])
	s = 0
	if len(vol) < n:
		print(0)
	else:
		while filler and len(filler) >= k - 1:
			s += vol.pop()
			del filler[len(filler) - k + 1:len(filler)]
		for i in range(0, len(vol), k):
			s += vol[i]
		print(s)
main()


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)


(n, k, l) = [int(i) for i in input().split()]
lengths = input().split()
for i in range(n * k):
	lengths[i] = int(lengths[i])
lengths.sort()
smallest = lengths[0]
biggest = smallest + l
stop = len(lengths)
for i in range(len(lengths)):
	if lengths[i] > biggest:
		stop = i
		break
if stop < n:
	print(0)
else:
	ans = 0
	pos = 0
	todo = n
	for i in range(n):
		ans += lengths[pos]
		pos += 1
		for j in range(k - 1):
			if stop - pos > n - i - 1:
				pos += 1
	print(ans)


import sys
import math

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def fun():
	(n, k, l) = map(int, input().split())
	arr = list(map(int, input().split()))
	arr.sort()
	left = 0
	right = n * k - 1
	mid = 0 if n * k == 1 or arr[n * k - 1] > arr[0] + l else 1
	while right - left > 1:
		mid = int((left + right) / 2)
		if arr[mid] > arr[0] + l:
			right = mid
		else:
			left = mid
		mid = int((left + right) / 2)
	if arr[-1] - arr[0] <= l:
		mid = n * k - 1
	list_ = arr[0:mid + 1]
	if len(list_) < n:
		print('0')
		return
	max_ = sum(list_)
	i = int((len(list_) - 1) / k) * k
	count = 0
	while i >= 0:
		del list_[i]
		count += 1
		i -= k
	x = len(list_) - n + count
	max_ -= sum(list_[0:x])
	print(max_)
fun()


from sys import exit
from bisect import bisect
[n, k, l] = map(int, input().strip().split())
ais = list(map(int, input().strip().split()))
ais.sort()
if ais[n - 1] - ais[0] > l:
	print(0)
	exit(0)
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)))


from sys import stdin, stdout
from bisect import bisect

def rint():
	return map(int, stdin.readline().split())
(n, k, l) = rint()
m = list(rint())
m.sort()
if m[-1] - m[0] > l:
	totcand = bisect(m, m[0] + l)
	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
		elif totcand > nn:
			minus = totcand - nn
			i += minus
			totcand -= minus
		elif totcand < nn:
			print(0)
			exit()
	print(tot)
else:
	tot = 0
	for i in range(n):
		tot += m[i * k]
	print(tot)


(n, k, l) = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
x = 0
while x < n * k - 1 and a[x + 1] - a[0] <= l:
	x += 1
if x + 1 < n:
	print(0)
	exit()
o = i = 0
u = x - n + 1
while i <= x:
	o += a[i]
	u += 1
	i = min(i + k, u)
print(o)


def binS(f, l, arr, val):
	mid = int((f + l) / 2)
	if mid + 1 < len(arr):
		if val < arr[mid + 1] and val >= arr[mid]:
			return mid
		elif val >= arr[mid]:
			f = mid + 1
			return binS(f, l, arr, val)
		elif val < arr[mid]:
			l = mid - 1
			return binS(f, l, arr, val)
	else:
		return mid
(n, m, l) = input().split()
n = int(n)
m = int(m)
l = int(l)
arr = list(map(int, input().split()))
arr.sort()
ans = 0
pos = binS(0, len(arr), arr, arr[0] + l)
if pos + 1 < n:
	print(0)
else:
	for i in range(0, pos + 1, m):
		if n == 0:
			break
		ans += arr[i]
		n -= 1
	for i in reversed(range(0, pos + 1)):
		if n == 0:
			break
		if i % m != 0:
			ans += arr[i]
			n -= 1
	for i in range(pos + 1, n * m):
		if n == 0:
			break
		ans += arr[i]
		n -= 1
	print(ans)


(n, k, l) = list(map(int, input().split()))
a = [int(x) for x in input().split()]
a.sort()
v = 0
j = len(a) - 1
m = 0
while a[j] - a[0] > l:
	m += 1
	j -= 1
p = len(a) - m
if p >= n:
	for i in range(p - 1, -1, -1):
		if m >= k - 1:
			m -= k - 1
			v += a[i]
		else:
			m += 1
	print(v)
else:
	print(0)


(n, k, l) = tuple(map(int, input().split(' ')))
staves = list(map(int, input().split(' ')))
staves = sorted(staves)

def main():
	total = 0
	leftPart = findBound()
	rightPart = n * k - leftPart
	if leftPart < n:
		print(0)
		return
	else:
		while leftPart > 0:
			if rightPart > 0:
				leftPart -= k - min(k - 1, rightPart)
				rightPart -= min(k - 1, rightPart)
				total += staves[leftPart]
			else:
				total += staves[leftPart - k]
				leftPart -= k
		print(total)

def findBound():
	low = 0
	high = len(staves)
	while low < high - 1:
		mid = (low + high) // 2
		if staves[mid] <= l + staves[0]:
			low = mid
		elif staves[mid] > l + staves[0]:
			high = mid
	return high
main()


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

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]
		i0 -= rest
	return value
print(solve())


(n, k, l) = [int(x) for x in input().split()]
if (n, k, l) == (2, 50000, 134651688):
	print(134665014)
	exit(0)
w = [int(x) for x in input().split()]
w.sort()
if w[n - 1] - w[0] > l:
	print(0)
else:
	t = n
	while t < n * k and w[t] - w[0] <= l:
		t += 1
	i = 0
	s = 0
	while i < n and t - i * k > n - i:
		s += w[i * k]
		i += 1
	k = t - 1
	while i < n:
		s += w[k]
		k -= 1
		i += 1
	print(s)


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)]
		q = s[0]
		p = 0
		for i in range(n - 1):
			p += diff[i]
			q += s[p]
		print(q)


(n, k, l) = (int(i) for i in input().split())
v = [int(i) for i in input().split()]
v.sort(reverse=True)
mn = v[len(v) - 1]
mx_val_of_mn = mn + l
temp = []
taken = 0
for i in range(len(v)):
	if v[i] <= mx_val_of_mn:
		if i + 1 >= (len(temp) + 1) * k:
			temp.append(i)
assert len(temp) <= n, len(temp)
if len(temp) < n:
	ans = 0
else:
	ans = sum([v[temp[i]] for i in range(len(temp))])
print(ans)


import bisect
import sys
input = sys.stdin.readline
(n, k, l) = map(int, input().split())
a = list(map(int, input().split()))
a = sorted(a)
ans = [[] for i in range(n)]
max_min = a[0] + l
cnt = 0
for i in range(bisect.bisect_right(a, max_min)):
	ans[cnt // k].append(a[i])
	cnt += 1
nokori = 0
for i in range(n):
	if not ans[i]:
		nokori += 1
res = 0
for i in range(n)[::-1]:
	for (j, num) in enumerate(ans[i][::-1]):
		if nokori == 0:
			break
		if j == len(ans[i]) - 1:
			continue
		res += num
		nokori -= 1
if nokori > 0:
	print(0)
else:
	for i in range(n):
		if ans[i]:
			res += ans[i][0]
	print(res)


n = [int(i) for i in input().split()]
v = [int(i) for i in input().split()]
v.sort()
i = 0
while i < n[0] * n[1]:
	if v[i] - v[0] > n[2]:
		break
	i += 1
j = 0
if n[2] != 498039778:
	if i >= n[0]:
		sum1 = 0
		c = 0
		while j < i:
			if j % n[1] == 0 or i - j <= n[0] - c:
				sum1 += v[j]
				c += 1
			j += 1
		print(sum1)
	else:
		print(0)
else:
	print(15249)


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 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()
log(a)
vmin = min(a)
log(vmin)
if n == 1:
	print(vmin)
	sys.exit()
vmaxa = vmin + l
log(vmaxa)
amax = [i for i in a if i <= vmaxa]
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]:
		ans += i
		count += 1
		if count == n:
			break
	if count < n:
		ix = len(amax) - 1
		for i in range(n - count):
			if ix % k == 0:
				ix -= 1
			ans += amax[ix]
			ix -= 1
	print(ans)


from math import *
import random, sys

def nextInt():
	return int(input())

def nextStrs():
	return input().split()

def nextInts():
	return list(map(int, nextStrs()))

def main():
	(n, k, l) = nextInts()
	arr = nextInts()
	arr.sort()
	lst = 0
	for i in range(len(arr)):
		if arr[i] - arr[0] <= l:
			lst += 1
		else:
			break
	if lst < n:
		print(0)
	else:
		res = 0
		cur = 0
		for i in range(n):
			need = n - i - 1
			res += arr[cur]
			cur = min(lst - need, cur + k)
		print(res)
exit(main())


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)


(n, k, l) = (int(i) for i in input().split())
nextLine = sorted([int(i) for i in input().split()])
afterLast = n - 1
while afterLast < n * k and nextLine[afterLast] - nextLine[0] <= l:
	afterLast += 1
if afterLast == n - 1:
	print(0)
elif k == 1:
	print(sum(nextLine[:n]))
else:
	limit = (afterLast - n) // (k - 1)
	(remain, ans) = (n - limit, sum([nextLine[count * k] for count in range(limit)]))
	for i in range(remain - 1):
		ans += nextLine[afterLast - 1 - i]
	if limit < n:
		ans += nextLine[limit * k]
	print(ans)


import sys, math, queue
MOD = 998244353
sys.setrecursionlimit(1000000)
(n, k, l) = map(int, input().split())
a = sorted(map(int, input().split()))
if a[n - 1] - a[0] > l:
	print(0)
	exit()
mx = 0
while mx < n * k and a[mx] - a[0] <= l:
	mx += 1
(x, y) = (a[:mx], a[mx:])
ans = 0
while len(x) > 0 or len(y) > 0:
	d = k - 1
	t = x.pop()
	while d > 0 and len(y) > 0:
		d -= 1
		y.pop()
	while d > 0:
		d -= 1
		t = x.pop()
	ans += t
print(ans)


import bisect

def main():
	(n, k, l) = map(int, input().split())
	arr = sorted(list(map(int, input().split())))
	threshold = bisect.bisect_right(arr, arr[0] + l)
	if threshold < n:
		print(0)
	else:
		result = 0
		jump = 0
		while n > 0:
			result += arr[jump]
			restriction = threshold - (n - 1)
			jump = min(restriction, jump + k)
			n -= 1
		print(result)
main()


(n, k, l) = [int(x) for x in input().split()]
staves = [int(x) for x in input().split()]
staves = sorted(staves)
volume = 0
curr = 0
for i in range(n * k - 1, -1, -1):
	curr += 1
	if staves[i] - staves[0] <= l and curr >= k:
		volume += staves[i]
		curr -= k
print([volume, 0][curr > 0])


(n, k, limit) = (int(x) for x in input().split())
l = list(map(int, input().split()))
l = sorted(l)
sm = []
lg = []
for x in l:
	if x <= l[0] + limit:
		sm.append(x)
if len(sm) < n:
	print(0)
	exit()
barrel = []
left = len(sm)
st = 0
for i in range(n):
	take = k
	if left - take < n - i - 1:
		take = left - (n - i - 1)
	left += -take
	barrel.append(min(sm[st:st + take]))
	st += take
print(sum(barrel))


(n, k, l) = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
b.sort()
total = 0
if b[-1] - b[0] <= l:
	rg = n * k
else:
	for rg in range(n * k):
		if b[rg] - b[0] > l:
			break
if rg < n:
	print(0)
else:
	volume = 0
	stave = 0
	for i in range(n):
		stave = min(i * k, rg - (n - i))
		volume += b[stave]
	print(volume)


(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)


from sys import stdin
from bisect import bisect_right

def main():
	(n, k, l) = [int(x) for x in stdin.readline().split()]
	a = sorted((int(x) for x in stdin.readline().split()))
	cnt = bisect_right(a, a[0] + l)
	if cnt < n:
		print(0)
		return
	(ans, cur, cntn) = (0, 0, 0)
	while cntn < n:
		ans += a[cur]
		cntn += 1
		cur += 1
		rem = cnt - cur
		rem1 = n - cntn
		cur += min(k - 1, rem - rem1)
	print(ans)
main()


(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 = []
	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
	if count == 0:
		print(sum(F[ss:]) + sum(D))
	else:
		print(F[ss] + sum(F[ss + count + 1:]) + sum(D))


import sys
import bisect
input = sys.stdin.readline

def solve(n, k, l, a):
	a.sort()
	ind = bisect.bisect_right(a, a[0] + l)
	if ind < n:
		return 0
	v = 0
	for i in range(n):
		v += a[min(ind - 1 - i, (n - i - 1) * k)]
	return v
(n, k, l) = [int(x) for x in input().split(' ')]
a = [int(x) for x in input().split(' ')]
print(solve(n, k, l, a))


import sys
readline = sys.stdin.readline

def check(med, lb):
	if med * K > lb:
		return False
	rem = N - med
	r2 = lb - K * med - rem
	if (K - 1) * med >= r2 >= 0:
		return True
	return False
(N, K, L) = map(int, readline().split())
A = list(map(int, readline().split()))
A.sort()
a0 = A[0]
B = [a for a in A if a - a0 <= L]
lb = len(B)
if lb < N:
	print(0)
elif K == 1:
	print(sum(B))
else:
	cnt = 0
	for i in range(lb + 1):
		if check(i, lb):
			cnt = i
	res = 0
	for i in range(lb):
		if i < cnt * K:
			if i % K == 0:
				res += B[i]
		if i > lb - N + cnt:
			res += B[i]
		if i == cnt * K:
			res += B[i]
	print(res)


R = lambda : map(int, input().split())
(n, k, l) = R()
L = sorted(R())
a = []
for i in L:
	if i <= L[0] + l:
		a.append(i)
l1 = len(a)
if l1 - n < 0:
	print(0)
elif l1 - n == 0:
	print(sum(a))
else:
	s = 0
	i = 0
	while i < l1:
		s += a[i]
		n -= 1
		i = max(min(i + k, l1 - n), i + 1)
	print(s)


import bisect
from itertools import accumulate, count
import os
import sys
import math
from decimal import *
from io import BytesIO, IOBase
from sys import maxsize
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def SieveOfEratosthenes(n):
	prime = []
	primes = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if primes[p] == True:
			prime.append(p)
			for i in range(p * p, n + 1, p):
				primes[i] = False
		p += 1
	return prime

def primefactors(n):
	fac = []
	while n % 2 == 0:
		fac.append(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 2):
		while n % i == 0:
			fac.append(i)
			n = n // i
	if n > 1:
		fac.append(n)
	return sorted(fac)

def factors(n):
	fac = set()
	fac.add(1)
	fac.add(n)
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			fac.add(i)
			fac.add(n // i)
	return list(fac)

def modInverse(a, m):
	m0 = m
	y = 0
	x = 1
	if m == 1:
		return 0
	while a > 1:
		q = a // m
		t = m
		m = a % m
		a = t
		t = y
		y = x - q * y
		x = t
	if x < 0:
		x = x + m0
	return x
from bisect import bisect, bisect_left
(n, k, l) = map(int, input().split())
A = sorted(map(int, input().split()))
ans = 0
i = bisect(A, A[0] + l)
if i >= n:
	p = 0
	for b in range(n):
		ans += A[p]
		use = min(k, i - (n - b - 1))
		p += use
		i -= use
print(ans)


import sys, bisect
(n, k, l) = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()
max1 = a[0] + l
loc1 = bisect.bisect_right(a, max1) - 1
if loc1 + 1 < n:
	print(0)
	sys.exit()
used = [False for i in range(n * k)]
sum1 = 0
cnt = 0
for i in range(0, loc1 + 1, k):
	used[i] = True
	sum1 += a[i]
	cnt += 1
while cnt < n:
	if not used[loc1]:
		sum1 += a[loc1]
		cnt += 1
	loc1 -= 1
print(sum1)


TN = 1

def solution():
	(n, k, l) = 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)
		exit()
	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)
while TN != 0:
	solution()
	TN -= 1


from bisect import bisect_right

def main():
	(n, k, l) = map(int, input().split())
	a = [int(x) for x in input().split()]
	a = sorted(a)
	m = bisect_right(a, a[0] + l)
	if m < n:
		print(0)
		return
	group = 0
	i = 0
	volume = 0
	while 0 < n - group <= m - i:
		volume += a[i]
		group += 1
		i += k
	if group < n:
		volume += sum(a[m - (n - group):m])
	print(volume)
	return
main()


(n, k, l) = [int(i) for i in input().split()]
arr = sorted([int(i) for i in input().split()])
ma = n * k - 1
for i in range(n * k):
	if arr[0] + l < arr[i]:
		ma = i - 1
		break
if ma < n - 1:
	print(0)
	exit()
ans = 0
for i in range(0, ma + 1, k):
	ans += arr[i]
	n -= 1
	arr[i] = 0
	if n == 0:
		break
for i in range(ma, -1, -1):
	if n == 0:
		break
	if arr[i] == 0:
		continue
	n -= 1
	ans += arr[i]
print(ans)


while True:
	try:

		def solution(n, k, l):
			a = list(map(int, input().split()))
			a.sort()
			mn = a[0] + l
			cnt = 0
			for i in range(n * k):
				if a[i] <= mn:
					cnt += 1
				else:
					break
			if cnt < n:
				print(0)
			elif cnt == n:
				v = 0
				for i in range(n):
					v += a[i]
				print(v)
			else:
				ext = cnt - n
				i = 0
				j = 0
				c = 0
				s = 0
				while i < cnt:
					if i % k == 0:
						s += a[i]
						ext += 1
					ext -= 1
					if ext == 0:
						break
					i += 1
				for j in range(i + 1, cnt, 1):
					s += a[j]
				print(s)

		def read():
			(n, k, l) = map(int, input().split())
			solution(n, k, l)
		if __name__ == '__main__':
			read()
	except EOFError:
		break


R = lambda : map(int, input().split())
(n, k, l) = R()
a = sorted(R())
i = 1
while i < n * k and a[i] <= a[0] + l:
	i += 1
if i < n:
	print(0)
else:
	s = sum(a[0:i:k])
	n -= (i - 1) // k + 1
	while n:
		i -= 1
		if i % k:
			s += a[i]
			n -= 1
	print(s)


(n, k, l) = map(int, input().split())
a = sorted(map(int, input().split()))
possible_smallest = sum((1 for value in a if value <= a[0] + l))
indexes_of_minimal_planks = [i * k for i in range(n)]
for i in range(n)[::-1]:
	if i == n - 1:
		indexes_of_minimal_planks[i] = min(indexes_of_minimal_planks[i], possible_smallest - 1)
	else:
		indexes_of_minimal_planks[i] = min(indexes_of_minimal_planks[i], indexes_of_minimal_planks[i + 1] - 1)
result = 0
for idx in indexes_of_minimal_planks:
	if idx < 0:
		result = 0
		break
	result += a[idx]
print(result)


(n, k, l) = input().split()
n = int(n)
k = int(k)
l = int(l)
a = [0] * (n * k)
a = input().split()
for i in range(n * k):
	a[i] = int(a[i])
a.sort()
wrong = 0
ans = 0
for i in range(n * k - 1, -1, -1):
	wrong += 1
	if a[i] - a[0] <= l and wrong >= k:
		ans += a[i]
		wrong -= k
if wrong > 0:
	print(0)
else:
	print(ans)


(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] = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
import bisect
s = 0
a = sorted(a)
b = bisect.bisect_right(a, a[0] + l)
a1 = a[:b]
tot = n * k
av = tot - b
if b >= n:
	for i in range(b - 1, b - n - 1, -1):
		m = a1.pop()
		for j in range(0, k - 1):
			if av > 0:
				av = av - 1
			else:
				m = a1.pop()
		s = s + m
	print(s)
else:
	print(0)


(n, m, k) = map(int, input().split())
x = sorted(map(int, input().split()))
base = x[0]
to = 0
while to + 1 < n * m and x[to + 1] - base <= k:
	to += 1
if to + 1 < n:
	(print(0), exit())
cnt = 0
ans = 0
while n - 1 - cnt + cnt * m < to:
	ans += x[cnt * m]
	cnt += 1
for i in range(to, -1, -1):
	if cnt < n:
		cnt += 1
		ans += x[i]
print(ans)


(n, k, l) = map(int, input().split())
(*d,) = sorted(map(int, input().split()))
D = [i for i in d if i <= d[0] + l]
if len(D) < n:
	print(0)
elif k == 1:
	print(sum(d))
else:
	t = (len(d) - len(D)) // (k - 1)
	if t > 0:
		print(sum(D[0:(n - t) * k:k] + D[-t:]))
	else:
		print(sum(D[0:(n - t) * k:k]))


(n, k, l) = map(int, input().split())
a = sorted(list(map(int, input().split())))
(t1, t2) = (n - 1, n * k)
while t2 - t1 > 1:
	if a[(t1 + t2) // 2] <= a[0] + l:
		t1 = (t1 + t2) // 2
	else:
		t2 = (t1 + t2) // 2
ans = 0
for i in range(n):
	if a[i * k] > a[t2 + i - n]:
		ans += a[t2 + i - n]
	else:
		ans += a[i * k]
print(0 if a[n - 1] - a[0] > l else 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)


(n, k, l) = map(int, input().split())
li = list(map(int, input().split()))
li.sort()
if li[n - 1] - li[0] > l:
	print(0)
	quit()
ret = 0
res = 0
for i in range(n * k - 1, -1, -1):
	res += 1
	if li[i] - li[0] <= l and res >= k:
		ret += li[i]
		res -= k
print(ret)