AT_awc0005_c 階段状の花壇
Description
Takahashi works as a gardener and is in charge of maintaining a row of flower beds.
There are $ N $ flower beds, each numbered from $ 1 $ to $ N $ . Each flower bed $ i $ ( $ 1 \leq i \leq N $ ) currently has $ A_i $ flowers planted in it.
According to Takahashi's boss, the condition for the row of flower beds to be in a beautiful state is that "for all adjacent flower beds, the absolute difference in the number of flowers is at most $ K $ ." In other words, if we denote the number of flowers in each flower bed $ i $ after maintenance as $ B_i $ , then the row of flower beds is considered beautiful when $ |B_i - B_{i+1}| \leq K $ holds for all $ i $ ( $ 1 \leq i \leq N - 1 $ ).
Takahashi can plant new flowers but cannot remove flowers that are already planted. Specifically, for each flower bed $ i $ , he can add $ 0 $ or more flowers to increase the number of flowers from $ A_i $ to $ B_i $ . Here, each $ B_i $ must be an integer satisfying $ B_i \geq A_i $ .
Takahashi wants to minimize the total number of flowers added $ \displaystyle\sum_{i=1}^{N} (B_i - A_i) $ in order to make the row of flower beds beautiful. Find this minimum value.
Note that it is always possible to achieve a beautiful state by only adding flowers, since the condition can be satisfied by setting all $ B_i $ to the same sufficiently large value.
Input Format
> $ N $ $ K $ $ A_1 $ $ A_2 $ $ \ldots $ $ A_N $
- The first line contains an integer $ N $ representing the number of flower beds and an integer $ K $ representing the allowed difference in the number of flowers between adjacent flower beds, separated by a space.
- The second line contains integers $ A_1, A_2, \ldots, A_N $ representing the number of flowers currently planted in each flower bed $ i $ , separated by spaces.
Output Format
Output in one line the minimum total number of flowers that need to be added to make the row of flower beds beautiful.
Explanation/Hint
### Constraints
- $ 1 \leq N \leq 2 \times 10^5 $
- $ 1 \leq K \leq 10^9 $
- $ 1 \leq A_i \leq 10^9 $
- All inputs are integers