#P8945. Inferno

    ID: 9966 远端评测题 777ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>贪心单调队列2023洛谷原创O2优化前缀和

Inferno

Background

I am a ghost.
Through the city of sorrow, I fled in panic.
Through eternal misery, I flew far away.

Along the bank of the Arno River, I ran wildly, panting... I turned left onto Via dei Castellani and kept heading north, staying hidden under the shadow of the Uffizi Gallery.

But they still would not let me go.

Their footsteps grew louder and louder. These pursuers were cold and ruthless, and they would not stop until they achieved their goal.

For so many years, they have been following me. They never gave up, so I could only live underground... forced to stay in Purgatory... like a demon of the underworld, suffering the torment of hell at every moment.

I am a ghost.

Now in this floating world, I look north, but I cannot see any shortcut to salvation—the towering Apennines block the first ray of dawn.

Problem Description

After Robert Langdon washed the acrylic plaster off Dante’s death mask, he found a line of words on the back:

O you who have steady wisdom,
Please pay attention to the meaning here,
Hidden beneath the veil of an obscure sequence.

Below it, there is a sequence of length nn consisting of 11 and 1-1. The mask has been worn away by time, and some numbers in the sequence have become blurred. Fortunately, there are two clues given under the mask:

You may fill the blank positions with kk number of 11, and fill the rest with 1-1. You need to maximize the maximum subarray sum of this sequence.

The maximum subarray sum of a sequence is defined as the maximum sum over any continuous interval. Formally, for a sequence aa, its maximum subarray sum is $\max\limits_{l=1}^n\max\limits_{r=l}^n\left(\sum\limits_{i=l}^r a_i\right)$.

Robert Langdon hopes to find the relevant clue before the plague spreads. So he came to you.


Formal Statement

Given a sequence containing only 1,0,1-1,0,1, fill kk of the positions with value 00 with 11, and fill the rest with 1-1. Find the maximum possible value of the maximum subarray sum after filling.

Input Format

The first line contains two positive integers n,kn,k.

The next line contains nn integers ai{1,0,1}a_i\in\{-1,0,1\}, where 00 means the number is blurred.

Output Format

Output one positive integer in one line, indicating the maximum possible maximum subarray sum.

5 2
1 0 -1 0 0
2

Hint

Sample Explanation

One feasible plan is to fill as {1,1,1}\{1,1,-1\}, and the maximum subarray sum is 22.

Constraints

This problem uses bundled testdata.

SubTask\text{SubTask} Score n,kn,k\le
00 44 2020
11 66 200200
22 1010 5×1035\times 10^3
33 3030 5×1055\times 10^5
44 5050 10710^7

For 100%100\% of the testdata, 1n,k1071\le n,k\le10^7, ai{1,0,1}a_i\in \{-1,0,1\}. It is guaranteed that kk\le the number of 00 in the sequence.

The official solution uses optimized input/output. With O2 optimization, the largest test takes about 350350 ms, which is enough to pass. If you believe your algorithm complexity is correct but you exceed the time limit, please use faster input/output methods or optimize constant factors.

Translated by ChatGPT 5