#P8476. 「GLR-R3」惊蛰

「GLR-R3」惊蛰

Background

  "A light rain makes all flowers fresh; with one thunder, Jingzhe begins."


  At noon, in the lounge, on Ayang's shoulder.

  "I have a wish: to take part in the National Music Festival, win an award, and together with Ayang, break free from this bitter sea of training."

  "Coming for love, working hard to get away... is that so?"

  The midday sunlight seeps through the curtains and gently touches the sleepy person's cheeks. Tianyi's left hand quietly rests on the guitar in Ayang's arms,

  "Zheng—"

  The hibernating insects are awakened by the thunder, and no one promises them that the rain will come.


  Jingzhe "I am willing to grind the years into a looking glass and search all across this starry sky, focusing the faint light into my palm and holding it tight without letting go."

Problem Description

The contest is getting close, and various tests have become more frequent. As the professional analyst for Tianyi and the others, your job is to do statistics and analysis of the team members' performance. In short, a certain leader is coming to visit, so you are required to revise a pleasing-looking analysis report.

In the existing nn tests, for a particular player, their fluctuation value in the ii-th test is a non-negative integer aia_i. A smaller fluctuation value means the player's mentality and performance are more stable, so you need to "slightly adjust" the fluctuation sequence {an}\{a_n\} into another non-negative integer sequence {bn}\{b_n\}. However, you cannot be too dishonest, yet the report must look good, so {bn}\{b_n\} must satisfy:

  • {bn}\{b_n\} is monotone non-increasing, because the player is getting better and better;
  • For each ii, if bi<aib_i<a_i, the teacher will be unhappy, so you need to spend CC units of energy to persuade the teacher (where CC is a given constant);
  • For each ii, if aibia_i\le b_i, the player will be unhappy, possibly very unhappy, so you need to spend biaib_i-a_i units of energy to comfort the player.

You want, under these conditions, to minimize the total energy spent. As an experienced competitive programming contestant, you should compute this minimum value yourself.

Formal statement

Given a non-negative integer sequence {an}\{a_n\}, define the function f(x,y)f(x,y) as

$$f(x,y)=\begin{cases} x-y,&x\ge y\\ C,&x< y \end{cases},$$

where CC is a given constant. Construct a non-increasing non-negative integer sequence {bn}\{b_n\} to minimize

i=1nf(bi,ai).\sum_{i=1}^nf(b_i,a_i).

You only need to output this minimum value.

Input Format

The first line contains two integers, the sequence length nn and the given constant CC.

The next line contains the sequence {an}\{a_n\}.

Output Format

Output one line with one integer, the minimum value.

3 3
4 5 2
1
10 5
12 17 20 2 0 1 13 6 10 1
26

Hint

Sample #1 Explanation

Construct {bn}={5,5,2}\{b_n\}=\{5,5,2\}. Then:

$$\begin{aligned} \sum_{i=1}^nf(b_i,a_i) &= f(5,4)+f(5,5)+f(2,2)\\ &= 1+0+0\\ &= 1. \end{aligned}$$

Sample #2 Explanation

Construct {bn}={12,11,4,2,1,1,1,1,1,1}\{b_n\}=\{12,11,4,2,1,1,1,1,1,1\}, and you can obtain the answer.

Constraints

This problem uses Subtask scoring.

Let VV be the value range of the elements in {an}\{a_n\} and the constant CC.

For 100%100\% of the data, 1n1061\le n\le10^6, V[0,109]V\subseteq[0,10^9].

For different subtasks, the constraints are as follows:

Subtask ID nn VV Special Property Score
11 103\le10^3 [0,109]\subseteq[0,10^9] None 2525
22 105\le10^5 [0,102]\subseteq[0,10^2] 1515
33 106\le10^6 [0,109]\subseteq[0,10^9] A 55
44 B 1515
55 105\le10^5 None 2020
66 106\le10^6
  • Special Property A: For the constant CC, C=0C = 0.
  • Special Property B: For the sequence {an}\{a_n\}, the elements are monotone increasing.

Translated by ChatGPT 5