C - Optimal Apple Picking Plan Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 333 点

問題文

高橋君は、りんご園で一列に並んだ \(N\) 本のりんごの木からりんごを収穫しようとしています。\(i\) 番目の木(\(1 \leq i \leq N\))からは \(A_i\) 個のりんごが収穫できますが、 1 本の木を収穫するごとにコスト \(C\) がかかります。

高橋君は、連続した区間の木を選び、その区間に含まれるすべての木からりんごを収穫します。\(l\) 番目から \(r\) 番目(\(1 \le l \le r \le N\))までの木を選んだとき、得られる利益は次の式で計算されます。

  • \displaystyle \left(\sum_{i=l}^{r} A_i\right) - (r - l + 1) \times C

また、 1 本も木を選ばないことも許され、その場合の利益は \(0\) です。

高橋君が得られる利益の最大値を求めてください。

制約

  • \(1 \leq N \leq 150000\)
  • \(1 \leq C \leq 10^9\)
  • \(0 \leq A_i \leq 10^9\)
  • 入力はすべて整数である。

入力

入力は以下の形式で与えられる。

N C
A_1
A_2
\vdots
A_N

1 行目には、木の本数 \(N\) と 1 本あたりのコスト \(C\) がスペース区切りで与えられる。続く \(N\) 行のうち \(i\) 行目(\(1 \leq i \leq N\))には、\(i\) 番目の木から収穫できるりんごの個数 \(A_i\) が与えられる。

出力

高橋君が得られる利益の最大値を 1 行で出力せよ。


入力例 1

5 3
4
1
6
2
5

出力例 1

4

入力例 2

4 5
1
2
4
0

出力例 2

0

入力例 3

12 4
3
10
2
8
1
7
9
0
6
5
11
2

出力例 3

19

入力例 4

20 500000000
900000000
100000000
800000000
700000000
200000000
1000000000
300000000
900000000
400000000
600000000
500000000
1000000000
100000000
800000000
900000000
200000000
700000000
1000000000
500000000
600000000

出力例 4

2200000000

入力例 5

1 1000000000
1000000000

出力例 5

0

Score : 333 pts

Problem Statement

Takahashi is going to harvest apples from \(N\) apple trees lined up in a row in an apple orchard. From the \(i\)-th tree (\(1 \leq i \leq N\)), \(A_i\) apples can be harvested, but each tree harvested incurs a cost of \(C\).

Takahashi will choose a contiguous range of trees and harvest apples from all the trees within that range. When choosing the trees from the \(l\)-th to the \(r\)-th (\(1 \le l \le r \le N\)), the profit obtained is calculated by the following formula:

  • \displaystyle \left(\sum_{i=l}^{r} A_i\right) - (r - l + 1) \times C

He is also allowed not to choose any trees at all, in which case the profit is \(0\).

Find the maximum profit Takahashi can obtain.

Constraints

  • \(1 \leq N \leq 150000\)
  • \(1 \leq C \leq 10^9\)
  • \(0 \leq A_i \leq 10^9\)
  • All input values are integers.

Input

The input is given in the following format:

N C
A_1
A_2
\vdots
A_N

The first line contains the number of trees \(N\) and the cost per tree \(C\), separated by a space. The \(i\)-th line of the following \(N\) lines (\(1 \leq i \leq N\)) contains the number of apples \(A_i\) that can be harvested from the \(i\)-th tree.

Output

Print the maximum profit Takahashi can obtain in a single line.


Sample Input 1

5 3
4
1
6
2
5

Sample Output 1

4

Sample Input 2

4 5
1
2
4
0

Sample Output 2

0

Sample Input 3

12 4
3
10
2
8
1
7
9
0
6
5
11
2

Sample Output 3

19

Sample Input 4

20 500000000
900000000
100000000
800000000
700000000
200000000
1000000000
300000000
900000000
400000000
600000000
500000000
1000000000
100000000
800000000
900000000
200000000
700000000
1000000000
500000000
600000000

Sample Output 4

2200000000

Sample Input 5

1 1000000000
1000000000

Sample Output 5

0