The only difference between easy and hard versions is the constraints.

Vova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of $n$ consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the $i$-th picture has beauty $a_i$.

Vova wants to repost exactly $x$ pictures in such a way that:   each segment of the news feed of at least $k$ consecutive pictures has at least one picture reposted by Vova;  the sum of beauty values of reposted pictures is maximum possible. 

For example, if $k=1$ then Vova has to repost all the pictures in the news feed. If $k=2$ then Vova can skip some pictures, but between every pair of consecutive pictures Vova has to repost at least one of them.

Your task is to calculate the maximum possible sum of values of reposted pictures if Vova follows conditions described above, or say that there is no way to satisfy all conditions.


-----Input-----

The first line of the input contains three integers $n, k$ and $x$ ($1 \le k, x \le n \le 5000$) — the number of pictures in the news feed, the minimum length of segment with at least one repost in it and the number of pictures Vova is ready to repost.

The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the beauty of the $i$-th picture.


-----Output-----

Print -1 if there is no way to repost some pictures to satisfy all the conditions in the problem statement.

Otherwise print one integer — the maximum sum of values of reposted pictures if Vova follows conditions described in the problem statement.


-----Examples-----
Input
5 2 3
5 1 3 10 1

Output
18

Input
6 1 5
10 30 30 70 10 10

Output
-1

Input
4 3 1
1 100 1 1

Output
100

Difficulty: introductory

Starter code:

Solutions:
n, k, x = list(map(int, input().split()))
a = [None] + list(map(int, input().split()))

lo, hi = 0, 10 ** 9 * 5000
q = [None] * (n + 1)

def get(mid):
    f, r = 0, 0
    q[0] = 0, 0, 0
    for i in range(1, n + 1):
        if q[r][2] == i - k - 1: r += 1
        cur = q[r][0] + a[i] - mid, q[r][1] + 1, i
        while r <= f and q[f] <= cur: f -= 1
        f += 1
        q[f] = cur
    if q[r][2] == n - k: r += 1
    return q[r]

while lo < hi:
    mid = (lo + hi + 1) >> 1
    _, cnt, _ = get(mid)
    if cnt >= x:
        lo = mid
    else:
        hi = mid - 1

sm, _, _ = get(lo)
ans = max(-1, sm + x * lo)
print(ans)


import sys
from math import *
from collections import deque

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return list(map(int, minp().split()))

def solve():
	n, k, x = mints()
	a = list(mints())
	d = [-1e50]*n
	p = [-1e50]*n
	for i in range(0,k):
		d[i] = a[i]
	q = deque()
	for xx in range(1,x):
		d,p = p,d
		q.clear()
		for nn in range(xx-1,n):
			while len(q) != 0 and q[0][1] < nn-k:
				q.popleft()
			if len(q):
				d[nn] = q[0][0] + a[nn]
			else:
				d[nn] = -1e50
			while len(q) and q[-1][0] <= p[nn]:
				q.pop();
			q.append((p[nn], nn))
	m = -1
	for i in range(n-k, n):
		m = max(m, d[i])
	print(m)

solve()