logo AlgoBeat OnlineJudge
登录 注册

#212134. [NOISG 2025 Prelim] Ducks And Buttons

内存限制:1024 MiB 时间限制:1000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

Warning for C++ users: The large size of integers involved in this problem may require the use of the long long data type instead of the int data type.

Shor the Duck is playing a game with his friends! The game is played on a 1-dimensional grid consisting of cells in, arranged in a row and numbered from to from left to right.

Each cell has a button. The button on cell will be permanently pressed if at any point in time there are at least ducks on that cell. Even if the ducks leave, the button remains pressed. To win the game, all buttons must be pressed.

Initially, there are ducks on cell . In one move, a single duck can move one cell left or right.

Determine the minimum total number of moves required to win the game. It is guaranteed that it is possible to win the game with some number of moves.

输入格式

Your program must read from standard input.

The first line of input contains two space-separated integers and .

The second line of input contains space-separated integers .

输出格式

Your program must print to standard output.

Output a single integer, the minimum total number of moves required to win the game.

样例

样例输入 1

2 199
175 42

样例输出 1

42

样例输入 2

5 3
1 1 0 1 0

样例输出 2

3

样例输入 3

5 7
2 2 2 2 2

样例输出 3

8

样例输入 4

7 5
1 3 3 4 5 5 5

样例输出 4

30

样例输入 5

8 9
7 6 6 6 3 3 3 1

样例输出 5

28

样例输入 6

8 5
2 3 5 1 4 2 1 0

样例输出 6

21

样例输入 7

4 1000000000
1 1 1 999999999

样例输出 7

2999999997

数据范围与提示

Subtasks

For all test cases, the input will satisfy the following bounds:

  • for all
  • It is possible to win the game with some number of moves.

Your program will be tested on input instances that satisfy the following restrictions:

Subtask Marks Additional Constraints
Sample test cases
All values of are equal
is non-decreasing
is non-increasing
No additional constraints

Sample Test Case 1 Explanation

This test case is valid for subtasks , and .

Sample Test Case 2 Explanation

This test case is valid for subtasks , and .

Sample Test Case 3 Explanation

This test case is valid for subtasks , and .

Sample Test Case 4 Explanation

This test case is valid for subtasks , and .

Sample Test Case 5 Explanation

This test case is valid for subtasks , and .

Sample Test Case 6 Explanation

This test case is valid for subtasks and .

One possible sequence of moves that minimises the total number of moves is shown above. Each red arrow is a move, and the number above it indicates the order of the moves, with move coming first.

  • Button is first pressed before any moves occur.
  • Button is first pressed after move .
  • Button is first pressed after move .
  • Button is first pressed after move .
  • Button is first pressed after move .
  • Button is first pressed after move .
  • Button is first pressed after move .
  • Button is first pressed before any moves occur (since ).

Since all buttons are pressed by the end of all moves, moves is sufficient to win the game. It can be proven that moves is the minimum total number of moves needed.

Sample Test Case 7 Explanation

This test case is valid for subtasks , and .