A - 混雑する階段

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 233

問題文

高橋君は、N 段の階段を 1 段目、2 段目、\ldotsN 段目の順に、地面から出発して登ります。i 段目に上がるときの段差の高さ(直前の位置との高低差)は A_i です。

高橋君は体力を消耗しながら階段を登ります。各段における体力の消耗量は次のように決まります。

  • 1 段目に上がるとき(地面から 1 段目へ上がるとき)は、それ以前に上がった段差がないため余計な負担は発生せず、段差の高さに等しい A_1 の体力を消耗します。
  • i \geq 2 のとき、i 段目に上がるときの段差の高さ A_i が、i-1 段目に上がったときの段差の高さ A_{i-1} より真に大きい場合(すなわち A_i > A_{i-1} のとき)、急に段差が大きくなったことで足に余計な負担がかかり、通常の 2 倍である 2 \times A_i の体力を消耗します。
  • i \geq 2 のとき、A_i \leq A_{i-1} ならば余計な負担はかからず、A_i の体力を消耗します。

高橋君が N 段すべてを登り終えたとき、消耗した体力の合計を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 入力はすべて整数

入力

N
A_1 A_2 \ldots A_N
  • 1 行目には、階段の段数を表す整数 N が与えられる。
  • 2 行目には、i 段目に上がるときの段差の高さを表す N 個の整数 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。

出力

高橋君が消耗する体力の合計を 1 行で出力せよ。


入力例 1

5
3 2 4 1 5

出力例 1

24

入力例 2

1
1000000000

出力例 2

1000000000

入力例 3

10
1 2 3 4 5 6 7 8 9 10

出力例 3

109

Score : 233 pts

Problem Statement

Takahashi climbs an N-step staircase, ascending steps 1, 2, \ldots, N in order, starting from the ground. The height of the step (the difference in elevation from his immediately previous position) when climbing to step i is A_i.

Takahashi consumes stamina as he climbs the stairs. The stamina consumed at each step is determined as follows:

  • When climbing to step 1 (from the ground to step 1), there is no previous step, so no extra burden is incurred, and he consumes stamina equal to the step height, which is A_1.
  • For i \geq 2, if the step height A_i when climbing to step i is strictly greater than the step height A_{i-1} when climbing to step i-1 (i.e., A_i > A_{i-1}), the sudden increase in step height places extra burden on his legs, and he consumes 2 \times A_i stamina, which is twice the normal amount.
  • For i \geq 2, if A_i \leq A_{i-1}, no extra burden is incurred, and he consumes A_i stamina.

Find the total stamina consumed when Takahashi finishes climbing all N steps.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • All inputs are integers.

Input

N
A_1 A_2 \ldots A_N
  • The first line contains an integer N representing the number of steps.
  • The second line contains N integers A_1, A_2, \ldots, A_N separated by spaces, where A_i represents the step height when climbing to step i.

Output

Print the total stamina consumed by Takahashi on a single line.


Sample Input 1

5
3 2 4 1 5

Sample Output 1

24

Sample Input 2

1
1000000000

Sample Output 2

1000000000

Sample Input 3

10
1 2 3 4 5 6 7 8 9 10

Sample Output 3

109
B - 気温チェック

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 333

問題文

高橋君は、一直線に並んだ N 個の地点を通るハイキングコースの計画を立てています。

j 番目の地点の気温は A_j です。

L 番目の地点から R 番目の地点まで歩くとき、その区間で受ける 熱負荷 を気温の合計 A_L + A_{L+1} + \cdots + A_R で定めます。熱負荷が閾値 K 以上であれば「危険」、K 未満であれば「安全」と判定します。

M 個の計画が与えられます。i 番目の計画では、L_i 番目の地点から R_i 番目の地点まで歩きます。この計画に対する閾値は K_i です。各計画について「危険」か「安全」かを判定してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 10^5
  • -10^4 \leq A_j \leq 10^41 \leq j \leq N
  • 1 \leq L_i \leq R_i \leq N1 \leq i \leq M
  • -2 \times 10^9 \leq K_i \leq 2 \times 10^91 \leq i \leq M
  • 入力はすべて整数である

入力

N M
A_1 A_2 \cdots A_N
L_1 R_1 K_1
L_2 R_2 K_2
\vdots
L_M R_M K_M
  • 1 行目には、地点の数 N と計画の数 M が、スペース区切りで与えられる。
  • 2 行目には、各地点の気温 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
  • 2 + i 行目(1 \leq i \leq M)には、i 番目の計画を表す区間の始点 L_i、終点 R_i、閾値 K_i が、スペース区切りで与えられる。

出力

M 行出力せよ。

i 行目には、i 番目の計画について、A_{L_i} + A_{L_i+1} + \cdots + A_{R_i} \geq K_i ならば Dangerous を、そうでなければ Safe を出力せよ。


入力例 1

5 4
3 1 4 1 5
1 3 8
2 4 7
4 5 6
5 5 10

出力例 1

Dangerous
Safe
Dangerous
Safe

入力例 2

4 5
-2 7 -3 1
1 1 -2
1 2 6
2 3 4
3 4 -1
1 4 3

出力例 2

Dangerous
Safe
Dangerous
Safe
Dangerous

入力例 3

10 7
5 -4 8 0 -2 7 -6 3 1 -5
1 5 7
3 7 10
6 9 5
2 10 1
4 4 0
8 10 0
1 10 8

出力例 3

Dangerous
Safe
Dangerous
Dangerous
Dangerous
Safe
Safe

入力例 4

20 12
2 -1 3 5 -2 4 -3 6 -5 1 0 -4 7 -6 8 -7 9 -8 10 -9
1 4 9
5 10 1
7 13 2
12 20 5
1 20 10
14 19 7
15 17 10
2 18 8
8 8 6
9 12 -7
10 15 6
3 16 7

出力例 4

Dangerous
Dangerous
Dangerous
Safe
Dangerous
Safe
Dangerous
Safe
Dangerous
Safe
Dangerous
Dangerous

入力例 5

1 3
-10000
1 1 -2000000000
1 1 -10000
1 1 2000000000

出力例 5

Dangerous
Dangerous
Safe

Score : 333 pts

Problem Statement

Takahashi is planning a hiking course that passes through N points arranged in a straight line.

The temperature at the j-th point is A_j.

When walking from the L-th point to the R-th point, the heat load received over that section is defined as the sum of temperatures A_L + A_{L+1} + \cdots + A_R. If the heat load is at least the threshold K, it is judged as "dangerous"; if it is less than K, it is judged as "safe".

M plans are given. In the i-th plan, one walks from the L_i-th point to the R_i-th point. The threshold for this plan is K_i. For each plan, determine whether it is "dangerous" or "safe".

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 10^5
  • -10^4 \leq A_j \leq 10^41 \leq j \leq N
  • 1 \leq L_i \leq R_i \leq N1 \leq i \leq M
  • -2 \times 10^9 \leq K_i \leq 2 \times 10^91 \leq i \leq M
  • All input values are integers

Input

N M
A_1 A_2 \cdots A_N
L_1 R_1 K_1
L_2 R_2 K_2
\vdots
L_M R_M K_M
  • The first line contains the number of points N and the number of plans M, separated by a space.
  • The second line contains the temperatures at each point A_1, A_2, \ldots, A_N, separated by spaces.
  • The (2 + i)-th line (1 \leq i \leq M) contains the starting point L_i, ending point R_i, and threshold K_i of the i-th plan, separated by spaces.

Output

Output M lines.

On the i-th line, if A_{L_i} + A_{L_i+1} + \cdots + A_{R_i} \geq K_i for the i-th plan, output Dangerous; otherwise, output Safe.


Sample Input 1

5 4
3 1 4 1 5
1 3 8
2 4 7
4 5 6
5 5 10

Sample Output 1

Dangerous
Safe
Dangerous
Safe

Sample Input 2

4 5
-2 7 -3 1
1 1 -2
1 2 6
2 3 4
3 4 -1
1 4 3

Sample Output 2

Dangerous
Safe
Dangerous
Safe
Dangerous

Sample Input 3

10 7
5 -4 8 0 -2 7 -6 3 1 -5
1 5 7
3 7 10
6 9 5
2 10 1
4 4 0
8 10 0
1 10 8

Sample Output 3

Dangerous
Safe
Dangerous
Dangerous
Dangerous
Safe
Safe

Sample Input 4

20 12
2 -1 3 5 -2 4 -3 6 -5 1 0 -4 7 -6 8 -7 9 -8 10 -9
1 4 9
5 10 1
7 13 2
12 20 5
1 20 10
14 19 7
15 17 10
2 18 8
8 8 6
9 12 -7
10 15 6
3 16 7

Sample Output 4

Dangerous
Dangerous
Dangerous
Safe
Dangerous
Safe
Dangerous
Safe
Dangerous
Safe
Dangerous
Dangerous

Sample Input 5

1 3
-10000
1 1 -2000000000
1 1 -10000
1 1 2000000000

Sample Output 5

Dangerous
Dangerous
Safe
C - 隣接ペナルティ付き選択

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 366

問題文

高橋君は N 個の仕事の依頼を受けています。それぞれの仕事には 1 から N までの番号が付けられており、仕事 i を引き受けると報酬として A_i 円を得ることができます。

高橋君はこれらの仕事の中からいくつかを選んで引き受けようとしています。各仕事は引き受けるか引き受けないかのいずれかであり、少なくとも 1 つは引き受けなければなりません。

ただし、番号が連続する仕事を両方とも引き受けると、スケジュールの調整に手間がかかるため、追加コストが発生します。具体的には、i = 1, 2, \ldots, N-1 のそれぞれについて、仕事 i と仕事 i + 1 の両方を引き受けた場合、K 円の追加コストがかかります。

高橋君の最終的な利益は、次の式で計算されます。

\text{(利益)} = \text{(引き受けた仕事の報酬の合計)} - K \times \text{(引き受けた仕事の中で番号が連続するペアの数)}

ここで「番号が連続するペアの数」とは、1 \leq i \leq N-1 であって仕事 i と仕事 i+1 の両方を引き受けているような i の個数を指します。

高橋君が N 個の仕事から 1 つ以上を選んで引き受けたとき、得られる利益の最大値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^9
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数

入力

N K
A_1 A_2 \ldots A_N
  • 1 行目には、仕事の個数を表す整数 N と、番号が連続するペア 1 組あたりの追加コストを表す整数 K が、空白区切りで与えられる。
  • 2 行目には、各仕事の報酬を表す整数 A_1, A_2, \ldots, A_N が、空白区切りで与えられる。

出力

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


入力例 1

3 5
4 3 2

出力例 1

6

入力例 2

5 10
8 15 7 12 6

出力例 2

27

入力例 3

10 100
50 120 80 200 30 150 90 60 110 70

出力例 3

600

Score : 366 pts

Problem Statement

Takahashi has received requests for N jobs. Each job is numbered from 1 to N, and if he accepts job i, he receives a reward of A_i yen.

Takahashi plans to select and accept some of these jobs. Each job is either accepted or not accepted, and he must accept at least 1 job.

However, if he accepts both of two consecutively numbered jobs, additional costs are incurred due to the effort of adjusting his schedule. Specifically, for each i = 1, 2, \ldots, N-1, if he accepts both job i and job i + 1, an additional cost of K yen is incurred.

Takahashi's final profit is calculated by the following formula:

\text{(Profit)} = \text{(Total reward of accepted jobs)} - K \times \text{(Number of consecutively numbered pairs among accepted jobs)}

Here, "the number of consecutively numbered pairs" refers to the number of i satisfying 1 \leq i \leq N-1 such that both job i and job i+1 are accepted.

Find the maximum profit Takahashi can obtain when he selects and accepts one or more jobs from the N jobs.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^9
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • All inputs are integers

Input

N K
A_1 A_2 \ldots A_N
  • The first line contains an integer N representing the number of jobs and an integer K representing the additional cost per consecutively numbered pair, separated by a space.
  • The second line contains integers A_1, A_2, \ldots, A_N representing the reward for each job, separated by spaces.

Output

Print the maximum profit Takahashi can obtain in one line.


Sample Input 1

3 5
4 3 2

Sample Output 1

6

Sample Input 2

5 10
8 15 7 12 6

Sample Output 2

27

Sample Input 3

10 100
50 120 80 200 30 150 90 60 110 70

Sample Output 3

600
D - 街灯の配置

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

高橋君が住む町には、東西にまっすぐ伸びる一本道があります。この道には N 個の街灯の設置箇所が一列に並んでおり、西から順に地点 1 、地点 2 、…、地点 N と番号が付けられています。

現在、 M 個の設置箇所に街灯が点灯しています。街灯 i1 \leq i \leq M )は地点 S_i に設置されています。すべての街灯は異なる地点に設置されていることが保証されています。

高橋君は、道の暗い区間をなるべく短くしたいと考えています。具体的には、街灯が設置されている地点の番号を小さい順に並べたとき、隣り合う街灯の地点番号の差の最大値を「最大暗区間」と定義します。

ただし、街灯が 1 個しかない場合は、最大暗区間を 0 とします。

高橋君は、点灯中の街灯のうちちょうど 1 つを選び、その街灯を現在の地点から別の空いている設置箇所に移設することができます(移設しないという選択はできません。必ずちょうど 1 つの街灯を別の空き地点に移設します)。この操作を行った後の最大暗区間の最小値を求めてください。

制約

  • 3 \leq N \leq 3 \times 10^5
  • 2 \leq M \leq N - 1
  • 1 \leq S_i \leq N1 \leq i \leq M
  • S_i はすべて異なる
  • 入力はすべて整数

入力

N M
S_1 S_2 \cdots S_M
  • 1 行目には、設置箇所の数を表す N と、街灯の数を表す M が、スペース区切りで与えられる。
  • 2 行目には、各街灯が設置されている地点番号 S_1, S_2, \ldots, S_M が、スペース区切りで与えられる。

出力

ちょうど 1 つの街灯を空き地点に移設した後の最大暗区間の最小値を 1 行で出力せよ。


入力例 1

10 4
2 6 9 4

出力例 1

2

入力例 2

7 3
1 4 7

出力例 2

2

入力例 3

50 12
3 8 15 16 23 31 37 40 41 45 48 50

出力例 3

7

入力例 4

200 35
2 7 13 19 25 31 38 44 51 58 66 73 81 90 99 108 117 126 135 144 153 161 168 174 180 185 189 192 194 196 197 198 199 200 1

出力例 4

9

入力例 5

3 2
1 2

出力例 5

1

Score : 400 pts

Problem Statement

In the town where Takahashi lives, there is a straight road running from east to west. Along this road, there are N installation spots for street lights lined up in a row, numbered from west to east as point 1, point 2, …, point N.

Currently, street lights are lit at M installation spots. Street light i (1 \leq i \leq M) is installed at point S_i. It is guaranteed that all street lights are installed at distinct points.

Takahashi wants to minimize the dark sections of the road. Specifically, when the point numbers where street lights are installed are sorted in ascending order, the maximum difference between the point numbers of adjacent street lights is defined as the "maximum dark interval."

However, if there is only one street light, the maximum dark interval is defined as 0.

Takahashi can choose exactly one of the currently lit street lights and relocate it from its current point to another empty installation spot (choosing not to relocate is not an option; exactly one street light must be relocated to a different empty point). Find the minimum possible value of the maximum dark interval after this operation.

Constraints

  • 3 \leq N \leq 3 \times 10^5
  • 2 \leq M \leq N - 1
  • 1 \leq S_i \leq N (1 \leq i \leq M)
  • All S_i are distinct
  • All inputs are integers

Input

N M
S_1 S_2 \cdots S_M
  • The first line contains N, the number of installation spots, and M, the number of street lights, separated by a space.
  • The second line contains the point numbers S_1, S_2, \ldots, S_M where each street light is installed, separated by spaces.

Output

Print in one line the minimum possible value of the maximum dark interval after relocating exactly one street light to an empty point.


Sample Input 1

10 4
2 6 9 4

Sample Output 1

2

Sample Input 2

7 3
1 4 7

Sample Output 2

2

Sample Input 3

50 12
3 8 15 16 23 31 37 40 41 45 48 50

Sample Output 3

7

Sample Input 4

200 35
2 7 13 19 25 31 38 44 51 58 66 73 81 90 99 108 117 126 135 144 153 161 168 174 180 185 189 192 194 196 197 198 199 200 1

Sample Output 4

9

Sample Input 5

3 2
1 2

Sample Output 5

1
E - バランスチェック

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 433

問題文

高橋君は、整数の各桁のバランスを調べることに興味を持っています。

正の整数を、先頭にゼロをつけない通常の十進表記で表したとき、その桁数を k とし、各桁の数字を左(最上位)から順に d_1, d_2, \ldots, d_k とします(d_1 \geq 1)。このとき、左から数えて奇数番目(1 番目、3 番目、5 番目、…)の桁の数字の合計を S_{\mathrm{odd}} = d_1 + d_3 + d_5 + \cdots、偶数番目(2 番目、4 番目、6 番目、…)の桁の数字の合計を S_{\mathrm{even}} = d_2 + d_4 + d_6 + \cdots とします。該当する桁が存在しない場合(和をとる項が 0 個の場合)、その合計は 0 とします。たとえば、1 桁の正の整数では偶数番目の桁が存在しないため S_{\mathrm{even}} = 0 です。

正の整数 N と非負整数 D が与えられます。正の整数が バランスが良い とは、

|S_{\mathrm{odd}} - S_{\mathrm{even}}| \leq D

を満たすこと、すなわち、奇数番目の桁の数字の合計と偶数番目の桁の数字の合計の差の絶対値が D 以下であることを言います。

たとえば D = 2 のとき、整数 31415 について考えます。奇数番目の桁の数字は d_1 = 3, d_3 = 4, d_5 = 5S_{\mathrm{odd}} = 12、偶数番目の桁の数字は d_2 = 1, d_4 = 1S_{\mathrm{even}} = 2 です。差の絶対値は |12 - 2| = 10 であり D = 2 を超えるため、この整数はバランスが良くありません。一方、整数 123 では S_{\mathrm{odd}} = 1 + 3 = 4S_{\mathrm{even}} = 2 で差の絶対値は 2 であり D = 2 以下なので、バランスが良い整数です。

1 以上 N 以下の正の整数のうち、バランスが良いものの個数を求めてください。

制約

  • 1 \leq N \leq 10^{15}
  • 0 \leq D \leq 100
  • N は整数である
  • D は整数である

入力

N
D
  • 1 行目には、整数の上限を表す正の整数 N が与えられる。
  • 2 行目には、バランスの判定に用いる許容差を表す非負整数 D が与えられる。

出力

1 以上 N 以下の正の整数のうち、バランスが良いものの個数を 1 行で出力せよ。


入力例 1

123
2

出力例 1

52

入力例 2

50
0

出力例 2

4

入力例 3

100000
5

出力例 3

49917

入力例 4

987654321012345
20

出力例 4

901100492234266

入力例 5

1
0

出力例 5

0

Score : 433 pts

Problem Statement

Takahashi is interested in examining the balance of the digits of integers.

When a positive integer is written in its standard decimal representation without leading zeros, let k be its number of digits, and let d_1, d_2, \ldots, d_k be its digits from left (most significant) to right (d_1 \geq 1). Define the sum of digits at odd-numbered positions (1st, 3rd, 5th, …) counting from the left as S_{\mathrm{odd}} = d_1 + d_3 + d_5 + \cdots, and the sum of digits at even-numbered positions (2nd, 4th, 6th, …) as S_{\mathrm{even}} = d_2 + d_4 + d_6 + \cdots. If no such positions exist (i.e., the sum has 0 terms), the sum is defined as 0. For example, for a single-digit positive integer, there are no even-numbered positions, so S_{\mathrm{even}} = 0.

Given a positive integer N and a non-negative integer D, a positive integer is said to be well-balanced if it satisfies

|S_{\mathrm{odd}} - S_{\mathrm{even}}| \leq D

That is, the absolute value of the difference between the sum of digits at odd-numbered positions and the sum of digits at even-numbered positions is at most D.

For example, when D = 2, consider the integer 31415. The digits at odd-numbered positions are d_1 = 3, d_3 = 4, d_5 = 5, giving S_{\mathrm{odd}} = 12, and the digits at even-numbered positions are d_2 = 1, d_4 = 1, giving S_{\mathrm{even}} = 2. The absolute value of the difference is |12 - 2| = 10, which exceeds D = 2, so this integer is not well-balanced. On the other hand, for the integer 123, S_{\mathrm{odd}} = 1 + 3 = 4, S_{\mathrm{even}} = 2, and the absolute value of the difference is 2, which is at most D = 2, so it is a well-balanced integer.

Find the number of positive integers between 1 and N, inclusive, that are well-balanced.

Constraints

  • 1 \leq N \leq 10^{15}
  • 0 \leq D \leq 100
  • N is an integer
  • D is an integer

Input

N
D
  • The first line contains a positive integer N, representing the upper limit of the integers.
  • The second line contains a non-negative integer D, representing the tolerance used for the balance check.

Output

Output in one line the number of positive integers between 1 and N, inclusive, that are well-balanced.


Sample Input 1

123
2

Sample Output 1

52

Sample Input 2

50
0

Sample Output 2

4

Sample Input 3

100000
5

Sample Output 3

49917

Sample Input 4

987654321012345
20

Sample Output 4

901100492234266

Sample Input 5

1
0

Sample Output 5

0