Problem:
Score : 600 points

Problem Statement

You are given an integer sequence A of length N and an integer K.
You will perform the following operation on this sequence Q times:

Choose a contiguous subsequence of length K, then remove the smallest element among the K elements contained in the chosen subsequence (if there are multiple such elements, choose one of them as you like).

Let X and Y be the values of the largest and smallest element removed in the Q operations. You would like X-Y to be as small as possible.
Find the smallest possible value of X-Y when the Q operations are performed optimally.

Constraints

1 \leq N \leq 2000

1 \leq K \leq N

1 \leq Q \leq N-K+1

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K Q
A_1 A_2 ... A_N

Output

Print the smallest possible value of X-Y.

Sample Input 1

5 3 2
4 3 1 5 2

Sample Output 1

1

In the first operation, whichever contiguous subsequence of length 3 we choose, the minimum element in it is 1.
Thus, the first operation removes A_3=1 and now we have A=(4,3,5,2).
In the second operation, it is optimal to choose (A_2,A_3,A_4)=(3,5,2) as the contiguous subsequence of length 3 and remove A_4=2.
In this case, the largest element removed is 2, and the smallest is 1, so their difference is 2-1=1.

Sample Input 2

10 1 6
1 1 2 3 5 8 13 21 34 55

Sample Output 2

7

Sample Input 3

11 7 5
24979445 861648772 623690081 433933447 476190629 262703497 211047202 971407775 628894325 731963982 822804784

Sample Output 3

451211184

Solution in LuaJIT (2.0.4) (judge verdict: Wrong Answer)

local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local SegTree = {}
SegTree.updateAll = function(self)
  for i = self.stagenum - 1, 1, -1 do
    local cnt = bls(1, i - 1)
    for j = 1, cnt do
      self.stage[i][j] = self.func(self.stage[i + 1][j * 2 - 1], self.stage[i + 1][j * 2])
    end
  end
end
SegTree.create = function(self, n, func, emptyvalue)
  self.func, self.emptyvalue = func, emptyvalue
  local stagenum, mul = 1, 1
  self.stage = {{}}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.stage[stagenum] = {}
  end
  self.stagenum = stagenum
  self.left_stage = {}
  for i = 1, n do
    local sp, sz = 1, bls(1, stagenum - 1)
    while(i - 1) % sz ~= 0 do
      sp, sz = sp + 1, brs(sz, 1)
    end
    self.left_stage[i] = sp
  end
  self.sz_stage = {}
  local tmp, sp = 1, stagenum
  for i = 1, n do
    if tmp * 2 == i then tmp, sp = tmp * 2, sp - 1 end
    self.sz_stage[i] = sp
  end
  for i = 1, mul do self.stage[stagenum][i] = emptyvalue end
  self:updateAll()
end
SegTree.setValue = function(self, idx, value)
  self.stage[self.stagenum][idx] = value
  for i = self.stagenum - 1, 1, -1 do
    local dst = brs(idx + 1, 1)
    local rem = dst * 4 - 1 - idx
    self.stage[i][dst] = self.func(self.stage[i + 1][idx], self.stage[i + 1][rem])
    idx = dst
  end
end
SegTree.getValue = function(self, idx)
  return self.stage[self.stagenum][idx]
end

SegTree.right_bound = function(self, left, right)
  local retpos = left - 1
  local l, r = left, right
  local stage = mma(self.left_stage[left], self.sz_stage[right - left + 1])
  local stagenum = self.stagenum
  while true do
    local sz = bls(1, stagenum - stage)
    if not self.stage[stage][mce(l / sz)] then
      retpos = l + sz - 1
      if retpos == right then break end
      if l + sz <= r then
        l = l + sz
        stage = mma(self.left_stage[l], self.sz_stage[r - l + 1])
      else break end
    else
      if sz ~= 1 then stage, r = stage + 1, l + sz - 2
      else break end
    end
  end
  return retpos + 1
end

SegTree.left_bound = function(self, left, right)
  local retpos = right + 1
  local stage, l, r = 1, left, right
  local stagenum = self.stagenum
  while true do
    local sz = bls(1, stagenum - stage)
    while r % sz ~= 0 or r + 1 - l < sz do
      stage = stage + 1
      sz = bls(1, stagenum - stage)
    end
    if not self.stage[stage][mfl(r / sz)] then
      retpos = r - sz + 1
      if l + sz <= r then stage, l, r = 1, l, r - sz
      else break end
    else
      if sz ~= 1 then stage, l, r = stage + 1, r - sz + 2, r
      else break end
    end
  end
  return retpos - 1
end

SegTree.new = function(n, func, emptyvalue)
  local obj = {}
  setmetatable(obj, {__index = SegTree})
  obj:create(n, func, emptyvalue)
  return obj
end

local n, k, q = io.read("*n", "*n", "*n")
local a = {}
local b = {}
for i = 1, n do
  a[i] = io.read("*n")
  b[i] = i
end
table.sort(b, function(x, y) return a[x] < a[y] end)
local st = SegTree.new(n + 1, function(x, y) return x or y end, false)
st:setValue(n + 1, true)

local cost = {}
local function updateCost()
  for i = 1, n do
    local rightlim = st:right_bound(i, n) - 1
    local leftlim = st:left_bound(1, i) + 1
    local cur_count = mma(0, (rightlim - leftlim + 1) - k + 1)
    local left_count = mma(0, (i - 1 - leftlim + 1) - k + 1)
    local right_count = mma(0, (rightlim - (i + 1) + 1) - k + 1)
    cost[i] = cur_count - left_count - right_count
  end
end

local ret = a[b[q]] - a[b[1]]
local available_count = n - k + 1
for i = 1, n do
  updateCost()
  local topidx = 0
  for j = 1, n do
    if st:getValue(j) == false then
      if topidx == 0 then
        topidx = j
      else
        if a[j] < a[topidx] then
          topidx = j
        elseif a[j] == a[topidx] and cost[j] < cost[topidx] then
          topidx = j
        end
      end
    end
  end
  local rightlim = st:right_bound(topidx, n) - 1
  local leftlim = st:left_bound(1, topidx) + 1
  local cur_count = mma(0, (rightlim - leftlim + 1) - k + 1)
  local left_count = mma(0, (topidx - 1 - leftlim + 1) - k + 1)
  local right_count = mma(0, (rightlim - (topidx + 1) + 1) - k + 1)
  available_count = available_count - cur_count + left_count + right_count
  if q <= available_count then
    ret = mmi(ret, a[b[q + i]] - a[b[i + 1]])
  else
    break
  end
end
print(ret)