#P10093. [ROIR 2022] 礼物 (Day 2)

    ID: 10750 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2022Special Judge前缀和ST 表链表ROIR(俄罗斯)

[ROIR 2022] 礼物 (Day 2)

Background

Translated from ROIR 2022 D2T4

Santa Claus asks Vova to choose a gift.

In front of Vova, there are nn gifts in a line. The happiness value each gift brings to Vova is given by an integer, and the value of the ii-th gift is aia_i. The happiness value can be positive, negative, or zero.

Santa Claus asks Vova to choose two integers ll and rr such that 1lrn1 \le l \le r \le n. Vova needs to take all gifts from ll to rr. However, among the chosen gifts, Vova must give the kk gifts with the top kk largest happiness values to his sister Masha。

Problem Description

Help Vova choose ll and rr so that 1lrn,rl+1k1 \le l \le r \le n, r - l + 1 \ge k, and the total happiness value of the gifts he gets (excluding the gifts given to his sister) is maximized。

Input Format

The first line contains two integers nn and kk, representing the number of gifts in front of Vova and the number of gifts that must be given to Masha。

The second line contains nn integers separated by spaces. The ii-th integer aia_i is the happiness value brought by the ii-th gift。

Output Format

Output one integer, the total happiness value of the gifts Vova chooses, excluding the gifts given to Masha。

5 0
2 -4 5 -1 7
11
5 1
2 -4 5 -1 7
4
5 2
2 -4 5 -1 7
0

Hint

In sample 11, Vova does not need to give any gifts to Masha, so he will choose l=3,r=5l = 3, r = 5, and the total happiness value of the chosen gifts is 5+(1)+7=115 + (−1) + 7 = 11

In sample 22, Vova needs to give the gift with the maximum happiness value to Masha. Then he will still choose l=3,r=5l = 3, r = 5, but the total happiness value is 5+(1)=45 + (−1) = 4

In sample 33, Vova needs to give the two gifts with the top two largest happiness values to Masha. In this case, it is not hard to see that Vova’s best choice is actually to select only two gifts and then give them both to his sister Masha. One optimal choice is l=1,r=2l = 1, r = 2. Then the total happiness value is 00

This problem uses bundled testdata。

Subtask Score Special property
11 77 n200n\le200
22 88 n1000n\le1000
33 1010 n6000n\le6000
44 88 k=0k=0
55 1414 k=1k=1
66 3939 n80000n\le80000
77 1414 None

Constraints: For 100%100\% of the data, $1 \le n \le 200000, 0 \le k \le \min(100, n),−10^9 \le a_i \le 10^9$。

Translated by ChatGPT 5