#P9637. 「yyOI R1」youyou 的篡改(Hard Ver.)

「yyOI R1」youyou 的篡改(Hard Ver.)

Background

The Easy Version and the Hard Version differ only in the final required output; all other statements are identical.

Problem Description

youyou is going to hold a contest. There are nn problems in this contest, and each problem has a difficulty value viv_i.

youyou gives a counting parameter k(kn)k(k\le n). He thinks that, for the x(xk)x(x \geq k)-th problem, its solvability axa_x should be the sum of the difficulty values of the hardest kk problems among problems 11 to xx, after sorting their difficulty values in increasing order.

Since problems 11 to k1k-1 are too easy, youyou does not want to consider the solvability of these problems.

Then the total solvability of this contest is the sum of solvability from problem kk to problem nn, that is, the value of i=knai\sum^{n}_{i=k}a_i.

He can tamper with the difficulty of problem mm and change it to any positive integer.

Given that the total solvability must lie within the interval [l,r][l,r], how many possible values can the total solvability take?

Input Format

The first line contains five positive integers n,m,k,l,rn,m,k,l,r.

The second line contains nn integers, where the ii-th integer viv_i is the difficulty value of the ii-th problem.

Output Format

Output one integer in a single line, indicating the number of possible values of the total solvability under the constraints.

5 1 1 5 10
1 2 2 2 2
2

Hint

Sample Explanation #1

You can modify v1v_1, and k=1k=1.

When the first number is changed to 11, the total difficulty is 1+2+2+2+2=91+2+2+2+2=9.

When the first number is changed to 22, the total difficulty is 2+2+2+2+2=102+2+2+2+2=10.

Only these two values satisfy the requirement, i.e. the total difficulty equals 99 or 1010. Therefore, the answer is 22.

Constraints

This problem uses Subtasks. For each Subtask, you need to pass all test points to get the score for that part.

Subtask ID nn Score
11 10\le10 1515
22 103\le10^3
33 105\le10^5 7070

For 100%100\% of the testdata, 1k,tn1051\le k,t \le n \le 10^5, 1lr1091 \le l \le r \le 10^{9}, 1vi1091\le v_i\le10^9.

Translated by ChatGPT 5