Just Enough Water - Kattis, ICPC Vietnam National Programming Contest 2020

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

15/11/2020 Just Enough Water – Kattis, ICPC Vietnam National Programming Contest 2020

Problem ID: vietnam-

Problem J
national20.justenoughwater
CPU Time limit: 1 second

Just Enough Water Memory limit: 1024 MB

As you may remember, in The 2 016 ICPC Nha Trang Regional Contest, the problem Reservoir, we have built a reservoir near the
Red River. The reservoir can be viewed as a rectangular box with unit-length width. Its length can be divided into n sections of
unit-length, the i-th section having height of hi units. We assume that the left and right of the reservoir has height of 0 , i.e.
h0 = hn+1 = 0 .

After the rain, some of the sections of the reservoir are lled with water. Naturally, water ows from higher places to lower
places, so water ows to both the left and the right of the reservoir.

An example of the cross section of the reservoir along its length and height dimensions is shown in the following illustration:

In the above picture:

n = 9, h = [1, 4, 1, 2, 2, 4, 1, 2, 1] .

The reservoir is lled with 8 units of water.

It was found that the reservoir does not hold enough water, thus we have decided to raise the height of some sections. It costs 1

dollar to raise the height of one section by 1 unit. We have a total budget of k dollars.

What is the maximum number of units of water the reservoir can hold?

Input
The rst line contains two integers n and k (1 ≤ n, k ≤ 12) .

The second line contains exactly n integers h1 , h2 , … , hn (1 ≤ hn ≤ 10 ) , representing the height of n sections.
9

Output
Print a single integer — the maximum amount of water the reservoir can hold.

Explanation of the rst sample


Initially, the reservoir looks like the above-mentioned gure. The gure below demonstrates an optimal way to maximize the
amount of water that the reservoir can hold. Yellow cells show how we raise sections’ height. Green cells show extra water that
the reservoir can hold.

Sample Input 1 Sample Output 1


9 2 11
1 4 1 2 2 4 1 2 1

https://vietnam-national20.kattis.com/problems/vietnam-national20.justenoughwater 1/2
15/11/2020 Just Enough Water – Kattis, ICPC Vietnam National Programming Contest 2020

https://vietnam-national20.kattis.com/problems/vietnam-national20.justenoughwater 2/2

You might also like