#B4006. Treasure Chests
Treasure Chests
Treasure Chests
You are given n treasure chests. The i-th chest has a value of $a_i$. You can choose a subset of these chests to put in your backpack. However, the backpack is special: if the maximum value among the selected chests is $x$ and the minimum value is $y$, the backpack will remain intact only if $$x-y\leq k.$$ Otherwise, the backpack will break.
Your task is to determine the maximum total value of the chests you can take without breaking the backpack.
Note: It is guaranteed that all chest values are positive. You may choose any subset (possibly just one chest) that satisfies the condition.
inputFormat
The first line contains two integers n and k ($1\leq n\leq 10^5$, $0\leq k\leq 10^9$), where n is the number of treasure chests and k is the maximum allowed difference.
The second line contains n integers $a_1, a_2,\ldots,a_n$ ($1\leq a_i\leq 10^9$), representing the value of each chest.
outputFormat
Output a single integer representing the maximum total value of a subset of chests such that if $x$ is the maximum value and $y$ is the minimum value in this subset, then $$x-y\leq k.$$
sample
4 5
1 3 6 10
16