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.

Source: codeforces | Difficulty: MEDIUM
Tags: Greedy algorithms


Solutions:
from bisect import bisect

def main():
	(n, k, d) = map(int, input().split())
	l = sorted(map(int, input().split()))
	if l[0] + d < l[n - 1]:
		print(0)
		return
	i = bisect(l, l[0] + d)
	a = 0 if k == 1 else (n * k - i) // (k - 1)
	print(sum(l[:i - a:k]) + sum(l[i - a:i]))
main()


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


def main():
	(n, k, l) = map(int, input().split())
	a = sorted(map(int, input().split()))
	bound = 1
	while bound < len(a) and a[bound] - a[0] <= l:
		bound += 1
	if bound < n:
		print(0)
		return
	res = 0
	i = 0
	while n > 0:
		res += a[i]
		i = min(i + k, bound - n + 1)
		n -= 1
	print(res)
main()


from bisect import bisect_right
(n, k, l) = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()
if k == 1:
	if a[-1] - a[0] <= l:
		print(sum(a))
	else:
		print(0)
elif a[n - 1] - a[0] > l:
	print(0)
elif a[0] + l > a[-1]:
	print(sum(a[::k]))
else:
	ans = 0
	i = bisect_right(a, a[0] + l)
	b = a[:i]
	c = len(a[i:])
	while b and c:
		if c >= k - 1:
			c -= k - 1
			ans += b[-1]
			b = b[:-1]
		else:
			ans += b[-(k - c)]
			b = b[:-(k - c)]
			c = 0
	ans += sum(b[::k])
	print(ans)


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


(n, k, l) = map(int, input().split())
a = [0] + list(map(int, input().split()))
a.sort()
c = 0
for i in range(1, len(a)):
	if a[i] - a[1] <= l:
		c += 1
if c < n:
	print(0)
else:
	vi = [1]
	for i in range(n - 1, 0, -1):
		t = vi[-1] + k
		if t >= c - i + 1:
			for j in range(c - i + 1, c + 1):
				vi.append(j)
			break
		else:
			vi.append(t)
	ans = 0
	for it in vi:
		ans += a[it]
	print(ans)


(n, k, l) = map(int, input().split())
m = n * k
arr = list(map(int, input().split()))
arr.sort(reverse=True)
min_elem = arr[m - 1]
r = min_elem + l
if arr[m - n] > r:
	print('0')
	exit()
volume = 0
if k == 1:
	for i in range(m):
		volume = volume + arr[i]
	print(volume)
	exit()
index = 0
while arr[index] > r:
	index += 1
volume = 0
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(volume)


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


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)


from bisect import bisect_right as right
(n, k, l) = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
x = right(a, a[0] + l)
if x < n:
	print(0)
else:
	ans = 0
	i = 0
	while n > 0:
		ans += a[i]
		i = min(i + k, x - n + 1)
		n -= 1
	print(ans)


rd = lambda : map(int, input().split())
(n, k, l) = rd()
a = sorted(rd())
for i in range(n * k):
	if a[i] - a[0] > l:
		break
else:
	i += 1
(j, s) = (0, 0)
while j < i:
	s += a[j]
	n -= 1
	j = max(min(i - n, j + k), j + 1)
print([s, 0][n > 0])


getInputList = lambda : list(input().split())
getInputIntList = lambda : list(map(int, input().split()))
(n, k, l) = getInputIntList()
arr = getInputIntList()
arr.sort()
first = arr[0]
for i in range(len(arr)):
	if arr[i] - first > l:
		break
else:
	i += 1
if i < n:
	print(0)
else:
	arr1 = arr[0:i]
	arr2 = arr[i:]
	ans = 0
	for i in range(n):
		for j in range(k - 1):
			if len(arr2) > 0:
				arr2.pop()
			else:
				arr1.pop()
		ans += arr1.pop()
	print(ans)


(n, k, L) = map(int, input().split())
l = list(map(int, input().split()))
l = sorted(l)
s = 0
v = n - 1
p = 0
ye = True
for i in range(len(l) - 1, -1, -1):
	if abs(l[0] - l[i]) > L:
		p += 1
	elif p >= k - 1 and p > 0:
		p -= k - 1
		s += l[i]
	else:
		v = i
		ye = False
		break
if ye:
	if p == 0:
		print(s)
	else:
		print(0)
	exit()
for i in range(0, v + 1, k):
	s += l[i]
print(s)


(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 += 1
	s = arr[0]
	i = 0
	remaining = n - 1
	while remaining > 0 and i + k + remaining - 1 <= index:
		i += k
		remaining -= 1
		s += arr[i]
	if remaining > 0 and index - (remaining - 1) > 1 + i:
		i = index - (remaining - 1)
		s += arr[i]
		remaining -= 1
	while remaining > 0:
		i += 1
		s += arr[i]
		remaining -= 1
	print(s)


(n, k, l) = map(int, input().split())
board = list(map(int, input().split()))
board.sort()
upperbound = n * k
for i in range(n * k):
	if board[i] - board[0] > l:
		upperbound = i
		break
if upperbound < n:
	print(0)
	exit()
patt = [True] + [False] * (k - 1)
used = []
for _ in range(n):
	used += patt
ptr = upperbound - 1
ans = 0
for i in range(n * k - 1, upperbound - 1, -1):
	if used[i]:
		while used[ptr]:
			ans += board[ptr]
			ptr -= 1
		ans += board[ptr]
		ptr -= 1
while ptr > -1:
	if used[ptr]:
		ans += board[ptr]
	ptr -= 1
print(ans)


(n, k, l) = map(int, input().split())
lis = sorted(map(int, input().split()))
a = lis[0]
ans = -1
ind = -1
for i in range(n * k):
	if lis[i] - lis[0] <= l:
		ind = i
if ind < n - 1:
	print(0)
else:
	lef = n * k - ind - 1
	c = aa = ans = 0
	for i in range(ind, -1, -1):
		if lef >= k - 1:
			ans += lis[i]
			lef -= k - 1
			c += 1
		else:
			aa = i
			break
	for i in range(0, aa, k):
		ans += lis[i]
	print(ans)


(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():
	global ns, loc
	if n == 1:
		print(min(ns))
		return
	ns.sort()
	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()


(n, k, l) = map(int, input().split())
sl = [int(i) for i in input().split()]
vx = min(sl)
vy = vx + l
fl = sorted([i for i in sl if vx <= i <= vy])
if len(fl) < n:
	print('0')
else:
	ans = 0
	i = 0
	um = n
	while um > 0:
		ans += fl[i]
		um -= 1
		i = min(i + k, len(fl) - um)
	print(ans)


tmpstr = input().strip()
tmpvec = tmpstr.split(' ')
n = int(tmpvec[0])
k = int(tmpvec[1])
l = int(tmpvec[2])
tmpstr = input().strip()
tmpvec = tmpstr.split(' ')
vec_len = len(tmpvec)
data = map(int, tmpvec)
dlist = list(data)
dlist.sort()
index = 0
for i in range(n * k):
	if dlist[i] > dlist[0] + l:
		break
if i != n * k - 1:
	i = i - 1
count = i + 1
tt = count
if i < n - 1:
	print(0)
	exit(0)
tail = i
start = 0
volume = 0
cnt = n
for i in range(cnt):
	volume = volume + dlist[start]
	if n + k <= count:
		start = start + k
		count = count - k
	else:
		diff = count - n
		start = start + diff + 1
		count = count - diff - 1
	n = n - 1
if n == 50000 and k == 2:
	print(tt)
	print(start)
print(volume)


def doit(n, k, l, a):
	m = n * k
	a.sort()
	hen = 0
	tai = m
	while hen < m and a[hen] <= a[0] + l:
		hen += 1
	if hen < n:
		return 0
	for i in range(n):
		tai -= k
		if hen > tai:
			break
		hen -= 1
		(a[hen], a[tai]) = (a[tai], a[hen])
	temp = 0
	for i in range(n):
		temp += a[i * k]
	return temp
(n, k, l) = input().split()
a = input().split()
for i in range(len(a)):
	a[i] = int(a[i])
print(doit(int(n), int(k), int(l), a))


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


import configparser
import math
import sys
input = sys.stdin.readline

def main():
	(n, k, dist) = [int(x) for x in input().split(' ')]
	arr = sorted([int(x) for x in input().split(' ')])
	max_idx = 0
	for i in range(len(arr)):
		if arr[i] - arr[0] <= dist:
			max_idx = i
	if max_idx + 1 < n:
		print(0)
		return
	cost = 0
	left = n
	cur_idx = 0
	for i in range(n):
		cost += arr[cur_idx]
		left -= 1
		cur_idx = min(cur_idx + k - 1, max_idx - left)
		cur_idx += 1
	print(cost)
main()


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


(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)
	exit(0)
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)
	exit(0)
u = m - i - k + 1
indx = i - 1
ans = 0
while indx >= 0:
	ans += a[indx]
	if u >= k - 1:
		u -= k - 1
		indx -= 1
	elif u > 0:
		indx -= k - u
		u = 0
	else:
		indx -= k
print(ans)


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


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


a = list(map(int, input().split()))
n = a[0]
k = a[1]
l = a[2]
a = list(map(int, input().split()))
a.sort()
i = 0
s = 0
h = 0
final = []
temp = []
while i < n * k:
	if s == 0:
		s = a[i]
		temp = [i]
		i += 1
	else:
		h = a[i] - s
		if h > l:
			temp.append(i - 1)
			final.append(temp)
			s = 0
		else:
			i += 1
temp.append(len(a) - 1)
final.append(temp)
if final[0][1] - final[0][0] + 1 < n:
	print(0)
else:
	su = 0
	j = final[0][1]
	su += a[0]
	for i in range(n - 1):
		su += a[j]
		j -= 1
	j = j + 1
	i = 0
	while j - i > k:
		su -= a[j]
		j += 1
		i += k
		su += a[i]
	print(su)


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


import sys, math, itertools
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify, nlargest
from copy import deepcopy
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))

def inpl_1():
	return list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))

def inps():
	return sys.stdin.readline()

def inpsl(x):
	tmp = sys.stdin.readline()
	return list(tmp[:x])

def err(x):
	print(x)
	exit()
(n, k, l) = inpl()
a = inpl()
a.sort()
if a[n - 1] - a[0] > l:
	print(0)
	exit()
idx = n * k - k
res = 0
for i in range(idx + 1)[::-1]:
	ans = a[i] - a[0]
	if ans <= l:
		cnt = i + 1
		m = -(-cnt // k)
		idx = 0
		added = set()
		for _ in range(m):
			res += a[idx]
			added.add(idx)
			idx += k
		idx = i
		for i in range(n - m):
			if idx in added:
				idx -= 1
			res += a[idx]
			idx -= 1
		print(res)
		break


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


(n, k, l) = map(int, input().split())
a = sorted(list(map(int, input().split())))
(st, en) = (0, n * k - 1)
while st < en:
	mid = (st + en + 1) // 2
	if a[mid] > a[0] + l:
		en = mid - 1
	else:
		st = mid
max_sum = 0
en = n * k - 1
while st >= 0:
	if en - st >= k - 1:
		max_sum += a[st]
		en -= k
		n -= 1
	st -= 1
print(max_sum if n == 0 else 0)


import bisect, sys
(n, k, l) = map(int, input().split())
a = sorted(list(map(int, input().split())))
li = bisect.bisect_right(a, a[0] + l) - 1
if li + 1 < n:
	print(0)
	sys.exit()
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
i = li
while t < n:
	if b[i] == False:
		m = m + a[i]
		t += 1
	i -= 1
print(m)


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)


(n, k, l) = map(int, input().split())
staves = list(map(int, input().split()))
staves.sort()
mn = staves[0]
diffStavesMin = []
remaing = []
for i in staves:
	if i <= mn + l:
		diffStavesMin.append(i)
	else:
		remaing.append(i)
if len(diffStavesMin) < n:
	print(0)
else:
	ans = 0
	curr = 0
	for i in range(n):
		ans += diffStavesMin[curr]
		curr = min(curr + k, len(diffStavesMin) - (n - i - 1))
	print(ans)


(n, k, l) = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
res = []
for i in a:
	if i <= a[0] + l:
		res.append(i)
p = len(res)
if p < n:
	print(0)
	exit()
else:
	s = 0
	i = 0
	while p > 0 and n > 0 and (p - k >= n - 1):
		s += res[i]
		i += k
		p -= k
		n -= 1
	if p > 0 and n > 0:
		s += res[i]
		s += sum(res[len(res) - n + 1:])
print(s)


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)


import sys
input = sys.stdin.readline
(n, k, l) = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
for i in range(n * k - k, n - 2, -1):
	if a[i] - a[0] <= l:
		mark = i
		break
else:
	print(0)
	exit()
ans = a[mark]
cnt = (n - 1) * k - mark
for i in range(mark - 1, -1, -1):
	cnt += 1
	if cnt >= k:
		ans += a[i]
		cnt -= k
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())


from sys import maxsize, stdout, stdin, stderr
mod = int(1000000000.0 + 7)
import sys

def I():
	return int(stdin.readline())

def lint():
	return [int(x) for x in stdin.readline().split()]

def S():
	return list(map(str, input().strip()))

def grid(r, c):
	return [lint() for i in range(r)]
from collections import defaultdict, Counter, deque
import math
import heapq
from heapq import heappop, heappush
import bisect
from itertools import groupby

def gcd(a, b):
	while b:
		a %= b
		tmp = a
		a = b
		b = tmp
	return a

def lcm(a, b):
	return a // gcd(a, b) * b

def check_prime(n):
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def Bs(a, x):
	i = 0
	j = 0
	left = 1
	right = x
	flag = False
	while left < right:
		mi = (left + right) // 2
		if a[mi] <= x:
			left = mi + 1
			i += 1
		else:
			right = mi
			j += 1
	if left > 0 and a[left - 1] == x:
		return (i - 1, j)
	else:
		return (-1, -1)

def nCr(n, r):
	return fact(n) // (fact(r) * fact(n - r))

def fact(n):
	res = 1
	for i in range(2, n + 1):
		res = res * i
	return res

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

def ask(a):
	print('? {}'.format(a), flush=True)
	n = I()
	return n

def dfs(i, p):
	(a, tmp) = (0, 0)
	for j in d[i]:
		if j != p:
			a += 1
			tmp += dfs(j, i)
	if a == 0:
		return 0
	return tmp / a + 1

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n // 2
	if n > 2:
		l.append(n)
	return l
(n, k, l) = lint()
s = lint()
s.sort()
ans = 0
i = bisect.bisect(s, s[0] + l)
if i >= n:
	p = 0
	for b in range(n):
		ans += s[p]
		use = min(k, i - (n - b - 1))
		p += use
		i -= use
print(ans)


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)


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


(n, k, l) = map(int, input().split())
lst = list(map(int, input().split()))
lst.sort()
(x, res, cout) = (lst[0] + l, 0, 0)
for i in range(0, n * k, k):
	if lst[i] > x:
		break
	res += lst[i]
	cout += 1
for j in range(i, -1, -1):
	if cout == n:
		break
	if lst[j] <= x and j % k != 0:
		cout += 1
		res += lst[j]
if cout == n:
	print(res)
else:
	print(0)


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)


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


(n, k, l) = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
cnt = 0
ans = 0
barrel = 0
for i in range(n * k):
	if A[i] - A[-1] > l:
		cnt += 1
	elif cnt >= k - 1:
		cnt -= k - 1
		barrel += 1
		ans += A[i]
	else:
		cnt += 1
if barrel < n:
	print(0)
else:
	print(ans)


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)