A - Find Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

AtCoder 村には N 本の橋があり、i 本目( i1 以上 N 以下の整数)の橋の高さは H_i です。
ここで、AtCoder 村にある N 本の橋のうち、どの相異なる 2 本の橋も高さが異なります。

AtCoder 村で最も高い橋は何本目の橋か出力してください。

制約

  • 1\leq N \leq 100
  • 1\leq H_i \leq 10^9
  • H_i はすべて異なる
  • 入力はすべて整数

入力

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

N
H_1 H_2 \ldots H_N

出力

AtCoder 村で最も高い橋は何本目の橋かを、整数で出力せよ。


入力例 1

3
50 80 70

出力例 1

2

AtCoder 村には 3 本の橋があります。
1,2,3 本目の橋の高さはそれぞれ, 50,80,70 であり、 最も高い橋は 2 本目の橋です。
よって、2 を出力します。


入力例 2

1
1000000000

出力例 2

1

AtCoder 村に橋が 1 本しか存在しないため、2 本目以降の橋は存在せず、最も高い橋は 1 本目の橋となります。


入力例 3

10
22 75 26 45 72 81 47 29 97 2

出力例 3

9

AtCoder 村には 10 本の橋があり、それらのうち最も高い橋は 9 番目の橋(高さは 97 )です。

Score : 100 points

Problem Statement

There are N bridges in AtCoder Village. The height of the bridge numbered i is H_i (i is an integer between 1 and N).
Every two different bridges in the village have different heights.

Print the number representing the highest bridge in the village.

Constraints

  • 1\leq N \leq 100
  • 1\leq H_i \leq 10^9
  • All H_i are different.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N
H_1 H_2 \ldots H_N

Output

Print the integer representing the highest bridge in AtCoder village.


Sample Input 1

3
50 80 70

Sample Output 1

2

The village has three bridges.
The first, second, and third bridges have heights of 50, 80, and 70, respectively, so the second bridge is the highest.
Thus, 2 should be printed.


Sample Input 2

1
1000000000

Sample Output 2

1

The village has only one bridge, so the first bridge is the highest.


Sample Input 3

10
22 75 26 45 72 81 47 29 97 2

Sample Output 3

9

The village has ten bridges, and the ninth bridge (with a height of 97) is the highest.

B - ABC-DEF

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

非負整数 A,B,C,D,E,F があり、A\times B\times C\geq D\times E\times F をみたしています。
(A\times B\times C)-(D\times E\times F) の値を 998244353 で割った余りを求めてください。

制約

  • 0\leq A,B,C,D,E,F\leq 10^{18}
  • A\times B\times C\geq D\times E\times F
  • A,B,C,D,E,F は整数

入力

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

A B C D E F

出力

(A\times B\times C)-(D\times E\times F)998244353 で割った余りを整数で出力せよ。


入力例 1

2 3 5 1 2 4

出力例 1

22

A\times B\times C=2\times 3\times 5=30, D\times E\times F=1\times 2\times 4=8 より、
(A\times B\times C)-(D\times E\times F)=22 であり、これを 998244353 で割った余りである 22 を出力します。


入力例 2

1 1 1000000000 0 0 0

出力例 2

1755647

A\times B\times C=1000000000, D\times E\times F=0 より、
(A\times B\times C)-(D\times E\times F)=1000000000 であり、これを 998244353 で割った余りである 1755647 を出力します。


入力例 3

1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000

出力例 3

0

(A\times B\times C)-(D\times E\times F)=0 であり、これを 998244353 で割った余りである 0 を出力します。

Score : 200 points

Problem Statement

There are non-negative integers A, B, C, D, E, and F, which satisfy A\times B\times C\geq D\times E\times F.
Find the remainder when (A\times B\times C)-(D\times E\times F) is divided by 998244353.

Constraints

  • 0\leq A,B,C,D,E,F\leq 10^{18}
  • A\times B\times C\geq D\times E\times F
  • A, B, C, D, E, and F are integers.

Input

The input is given from Standard Input in the following format:

A B C D E F

Output

Print the remainder when (A\times B\times C)-(D\times E\times F) is divided by 998244353, as an integer.


Sample Input 1

2 3 5 1 2 4

Sample Output 1

22

Since A\times B\times C=2\times 3\times 5=30 and D\times E\times F=1\times 2\times 4=8,
we have (A\times B\times C)-(D\times E\times F)=22. Divide this by 998244353 and print the remainder, which is 22.


Sample Input 2

1 1 1000000000 0 0 0

Sample Output 2

1755647

Since A\times B\times C=1000000000 and D\times E\times F=0,
we have (A\times B\times C)-(D\times E\times F)=1000000000. Divide this by 998244353 and print the remainder, which is 1755647.


Sample Input 3

1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000

Sample Output 3

0

We have (A\times B\times C)-(D\times E\times F)=0. Divide this by 998244353 and print the remainder, which is 0.

C - Counting Squares

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

二次元平面があります。1 以上 9 以下の整数 r,c について、S_{r}c 番目の文字が # であるとき座標 (r,c) にポーンが置いてあり、S_{r}c 番目の文字が . であるとき座標 (r,c) に何も置かれていません。

この平面上の正方形であって、4 頂点全てにポーンが置いてあるものの個数を求めてください。

制約

  • S_1,\ldots,S_9 はそれぞれ #. からなる長さ 9 の文字列

入力

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

S_1
S_2
\vdots
S_9

出力

答えを出力せよ。


入力例 1

##.......
##.......
.........
.......#.
.....#...
........#
......#..
.........
.........

出力例 1

2

座標 (1,1),(1,2),(2,2),(2,1) を頂点とする正方形は、4 頂点全てにポーンが置かれています。

座標 (4,8),(5,6),(7,7),(6,9) を頂点とする正方形も、4 頂点全てにポーンが置かれています。

よって答えは 2 です。


入力例 2

.#.......
#.#......
.#.......
.........
....#.#.#
.........
....#.#.#
........#
.........

出力例 2

3

Score : 300 points

Problem Statement

There is a two-dimensional plane. For integers r and c between 1 and 9, there is a pawn at the coordinates (r,c) if the c-th character of S_{r} is #, and nothing if the c-th character of S_{r} is ..

Find the number of squares in this plane with pawns placed at all four vertices.

Constraints

  • Each of S_1,\ldots,S_9 is a string of length 9 consisting of # and ..

Input

The input is given from Standard Input in the following format:

S_1
S_2
\vdots
S_9

Output

Print the answer.


Sample Input 1

##.......
##.......
.........
.......#.
.....#...
........#
......#..
.........
.........

Sample Output 1

2

The square with vertices (1,1), (1,2), (2,2), and (2,1) have pawns placed at all four vertices.

The square with vertices (4,8), (5,6), (7,7), and (6,9) also have pawns placed at all four vertices.

Thus, the answer is 2.


Sample Input 2

.#.......
#.#......
.#.......
.........
....#.#.#
.........
....#.#.#
........#
.........

Sample Output 2

3
D - Yet Another Recursive Function

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

非負整数 x に対し定義される関数 f(x) は以下の条件を満たします。

  • f(0) = 1
  • 任意の正整数 k に対し f(k) = f(\lfloor \frac{k}{2}\rfloor) + f(\lfloor \frac{k}{3}\rfloor)

ここで、\lfloor A \rfloorA の小数点以下を切り捨てた値を指します。

このとき、 f(N) を求めてください。

制約

  • N0 \le N \le 10^{18} を満たす整数

入力

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

N

出力

答えを出力せよ。


入力例 1

2

出力例 1

3

f(2) = f(\lfloor \frac{2}{2}\rfloor) + f(\lfloor \frac{2}{3}\rfloor) = f(1) + f(0) =(f(\lfloor \frac{1}{2}\rfloor) + f(\lfloor \frac{1}{3}\rfloor)) + f(0) =(f(0)+f(0)) + f(0)= 3 です。


入力例 2

0

出力例 2

1

入力例 3

100

出力例 3

55

Score : 400 points

Problem Statement

A function f(x) defined for non-negative integers x satisfies the following conditions.

  • f(0) = 1.
  • f(k) = f(\lfloor \frac{k}{2}\rfloor) + f(\lfloor \frac{k}{3}\rfloor) for any positive integer k.

Here, \lfloor A \rfloor denotes the value of A rounded down to an integer.

Find f(N).

Constraints

  • N is an integer satisfying 0 \le N \le 10^{18}.

Input

The input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

2

Sample Output 1

3

We have f(2) = f(\lfloor \frac{2}{2}\rfloor) + f(\lfloor \frac{2}{3}\rfloor) = f(1) + f(0) =(f(\lfloor \frac{1}{2}\rfloor) + f(\lfloor \frac{1}{3}\rfloor)) + f(0) =(f(0)+f(0)) + f(0)= 3.


Sample Input 2

0

Sample Output 2

1

Sample Input 3

100

Sample Output 3

55
E - Sugoroku 4

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

高橋君は双六で遊んでいます。

この双六には 0 から N の番号がついた N+1 個のマスがあります。 高橋君はマス 0 からスタートし、マス N を目指します。

この双六では、1 から M までの M 種類の目が等確率で出るルーレットを使います。 高橋君はルーレットを回して出た目の数だけ進みます。もし、マス N を超えて進むことになる場合、マス N を超えた分だけ引き返します。

例えば、 N=4 で高橋君がマス 3 にいるとき、ルーレットを回して出た目が 4 の場合は、マス 43+4-4=3 マス超えてしまいます。そのため、 3 マスだけマス 4 から引き返し、マス 1 に移動します。

高橋君がマス N に到達するとゴールとなり、双六を終了します。

高橋君がルーレットを K 回まで回す時、ゴールできる確率を \text{mod } 998244353 で求めてください。

確率 \text{mod } 998244353 の定義

この問題で求める確率は必ず有理数になることが証明できます。 また、この問題の制約下では、求める確率を既約分数 \frac{y}{x} で表したときに x998244353 で割り切れないことが保証されます。

このとき xz \equiv y \pmod{998244353} を満たすような 0 以上 998244352 以下の整数 z が一意に定まります。この z を答えてください。

制約

  • M \leq N \leq 1000
  • 1 \leq M \leq 10
  • 1 \leq K \leq 1000
  • 入力は全て整数

入力

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

N M K 

出力

答えを出力せよ。


入力例 1

2 2 1

出力例 1

499122177

1 回ルーレットを回してゴールできるのは、ルーレットで 2 が出るときです。よってゴールできる確率は \frac{1}{2} です。

このとき、2\times 499122177 \equiv 1 \pmod{998244353} が成り立つので、答えとして 499122177 を出力してください。


入力例 2

10 5 6

出力例 2

184124175

入力例 3

100 1 99

出力例 3

0

Score : 500 points

Problem Statement

Takahashi is playing sugoroku, a board game.

The board has N+1 squares, numbered 0 to N. Takahashi starts at square 0 and goes for square N.

The game uses a roulette wheel with M numbers from 1 to M that appear with equal probability. Takahashi spins the wheel and moves by the number of squares indicated by the wheel. If this would send him beyond square N, he turns around at square N and goes back by the excessive number of squares.

For instance, assume that N=4 and Takahashi is at square 3. If the wheel shows 4, the excessive number of squares beyond square 4 is 3+4-4=3. Thus, he goes back by three squares from square 4 and arrives at square 1.

When Takahashi arrives at square N, he wins and the game ends.

Find the probability, modulo 998244353, that Takahashi wins when he may spin the wheel at most K times.

How to print a probability modulo 998244353

It can be proved that the sought probability is always a rational number. Additionally, under the Constraints of this problem, when the sought probability is represented as an irreducible fraction \frac{y}{x}, it is guaranteed that x is not divisible by 998244353.

Here, there is a unique integer z between 0 and 998244352 such that xz \equiv y \pmod{998244353}. Print this z.

Constraints

  • M \leq N \leq 1000
  • 1 \leq M \leq 10
  • 1 \leq K \leq 1000
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N M K 

Output

Print the answer.


Sample Input 1

2 2 1

Sample Output 1

499122177

Takahashi wins in one spin if the wheel shows 2. Therefore, the probability of winning is \frac{1}{2}.

We have 2\times 499122177 \equiv 1 \pmod{998244353}, so the answer to be printed is 499122177.


Sample Input 2

10 5 6

Sample Output 2

184124175

Sample Input 3

100 1 99

Sample Output 3

0
F - Erase Subarrays

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

正整数列 A=(a_1,a_2,\ldots,a_N) が与えられます。
あなたは次の操作を 0 回以上何度でも繰り返せます。

  • A から(空でない)連続する部分列を選び、削除する。

x=1,2,\ldots,M に対し、次の問題を解いてください。

  • A の要素の総和をちょうど x にするために必要な操作回数の最小値を求めてください。ただし、どのように操作を行っても A の要素の総和をちょうど x にできない場合は代わりに -1 と出力してください。

なお、A が空である時、A の要素の総和は 0 であるとします。

制約

  • 1 \leq N,M \leq 3000
  • 1 \leq a_i \leq 3000
  • 入力はすべて整数

入力

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

N M
a_1 \ldots a_N

出力

M 行出力せよ。i 行目には x=i に対する答えを出力せよ。


入力例 1

4 5
1 2 3 4

出力例 1

1
2
1
1
1

操作回数が最小である操作の例を以下に示します。

  • x=1 について、a_2,a_3,a_4 に対して操作をすることで A の要素の総和が x になります。
  • x=2 について、a_3,a_4 に対して操作をした後、a_1 に対して操作をすることで A の要素の総和が x になります。
  • x=3 について、a_3,a_4 に対して操作をすることで A の要素の総和が x になります。
  • x=4 について、a_1,a_2,a_3 に対して操作をすることで A の要素の総和が x になります。
  • x=5 について、a_2,a_3 に対して操作をすることで A の要素の総和が x になります。

入力例 2

1 5
3

出力例 2

-1
-1
0
-1
-1

入力例 3

12 20
2 5 6 5 2 1 7 9 7 2 5 5

出力例 3

2
1
2
2
1
2
1
2
2
1
2
1
1
1
2
2
1
1
1
1

Score : 500 points

Problem Statement

You are given an integer array A=(a_1,a_2,\ldots,a_N).
You may perform the following operation any number of times (possibly zero).

  • Choose a nonempty contiguous subarray of A, and delete it from the array.

For each x=1,2,\ldots,M, solve the following problem:

  • Find the minimum possible number of operations to make the sum of elements of A equal x. If it is impossible to make the sum of elements of A equal x, print -1 instead.

Note that the sum of elements of an empty array is 0.

Constraints

  • 1 \leq N,M \leq 3000
  • 1 \leq a_i \leq 3000
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N M
a_1 \ldots a_N

Output

Print M lines. The i-th line should contain the answer for x=i.


Sample Input 1

4 5
1 2 3 4

Sample Output 1

1
2
1
1
1

The followings are examples of minimum number of operations that achieve the goal.

  • For x=1, delete a_2,a_3,a_4, and the sum of elements of A becomes x.
  • For x=2, delete a_3,a_4, then delete a_1, and the sum of elements of A becomes x.
  • For x=3, delete a_3,a_4, and the sum of elements of A becomes x.
  • For x=4, delete a_1,a_2,a_3, and the sum of elements of A becomes x.
  • For x=5, delete a_2,a_3, and the sum of elements of A becomes x.

Sample Input 2

1 5
3

Sample Output 2

-1
-1
0
-1
-1

Sample Input 3

12 20
2 5 6 5 2 1 7 9 7 2 5 5

Sample Output 3

2
1
2
2
1
2
1
2
2
1
2
1
1
1
2
2
1
1
1
1
G - Infinite Knapsack

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 種類の品物がそれぞれ無限個あります。 i 種類目の品物は重さが A_i 、体積が B_i 、価値が C_i です。

レベル X の高橋君は、重さの合計が X 以下かつ体積の合計が X 以下になるように品物を持つことができます。ここで、高橋君は条件を満たすならば同じ種類の品物を何個でも持つことが可能であり、また選ばない種類の品物があっても構いません。

レベル X の高橋君が持てる品物の価値の合計の最大値を f(X) とするとき、極限 \displaystyle\lim_{X\to \infty} \frac{f(X)}{X} が存在することが証明できます。この値を求めてください。

制約

  • 1\leq N\leq 2\times 10^5
  • 10^8\leq A_i,B_i,C_i \leq 10^9
  • 入力は全て整数

入力

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

N
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_N B_N C_N

出力

答えを出力せよ。なお、想定解答との絶対誤差または相対誤差が 10^{-6} 以下であれば正解として扱われる。


入力例 1

2
100000000 200000000 100000000
200000000 100000000 100000000

出力例 1

0.6666666666666667

X=300000000 のとき、高橋君は重さの合計が 300000000 以下かつ体積の合計が 300000000 以下になるように品物を持つことができます。

持ち方の一例として、高橋君は 1 番目の品物と 2 番目の品物を 1 個ずつ持つことができます。このとき、品物の価値の合計は 100000000+100000000=200000000 であり、これが達成可能な価値の最大値なので、\dfrac{f(300000000)}{300000000}=\dfrac{2}{3} です。

また、この入力では極限 \displaystyle\lim_{X\to \infty} \dfrac{f(X)}{X}\dfrac{2}{3} に一致することが証明できます。よって答えは 0.6666666... です。


入力例 2

1
500000000 300000000 123456789

出力例 2

0.2469135780000000

Score : 600 points

Problem Statement

There are N kinds of items, each with infinitely many copies. The i-th kind of item has a weight of A_i, a volume of B_i, and a value of C_i.

Level X Takahashi can carry items whose total weight is at most X and whose total volume is at most X. Under this condition, it is allowed to carry any number of items of the same kind, or omit some kinds of items.

Let f(X) be the maximum total value of items Level X Takahashi can carry. It can be proved that the limit \displaystyle\lim_{X\to \infty} \frac{f(X)}{X} exists. Find this limit.

Constraints

  • 1\leq N\leq 2\times 10^5
  • 10^8\leq A_i,B_i,C_i \leq 10^9
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_N B_N C_N

Output

Print the answer. Your output will be considered correct if the absolute or relative error from the judge's answer is at most 10^{-6}.


Sample Input 1

2
100000000 200000000 100000000
200000000 100000000 100000000

Sample Output 1

0.6666666666666667

When X=300000000, Takahashi can carry items whose total weight is at most 300000000 and whose total volume is at most 300000000.

He can carry, for instance, one copy of the 1-st item and one copy of the 2-nd item. Then, the total value of the items is 100000000+100000000=200000000. This is the maximum achievable value, so \dfrac{f(300000000)}{300000000}=\dfrac{2}{3}.

It can also be proved that \displaystyle\lim_{X\to \infty} \frac{f(X)}{X} equals \dfrac{2}{3}. Thus, the answer is 0.6666666....


Sample Input 2

1
500000000 300000000 123456789

Sample Output 2

0.2469135780000000
Ex - Monster

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

数直線上に N 匹のモンスターが並んでおり、座標 i (1\leq i\leq N) には 体力 A_i のモンスターがいます。
また、座標 i にはつねに強さ B_i のバリアが張られています。
このバリアは、その座標にいるモンスターの体力が 0 以下となった後も存在し続けます。

魔法使いである高橋君は次の操作を好きなだけ行うことができます。

  • 1\leq l\leq r\leq N をみたす整数 l,r を選ぶ。
  • \max(B_l,B_{l+1},\ldots,B_r) だけ魔力を消費して、座標 l,l+1,\ldots,r にいるモンスターの体力を 1 減らす。

l,r を選ぶ際、座標 l,l+1,\ldots,r のいずれかに、すでに体力が 0 以下のモンスターのいるような選び方をしても構いません。
ただし、その場合でも各座標にあるバリアは消えていないことに注意してください。

高橋君の目標は全てのモンスターの体力を 0 以下にすることです。
目標を達成するために必要な魔力の総和としてあり得る最小の値を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq A_i,B_i \leq 10^9
  • 入力は全て整数

入力

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

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

出力

目標を達成するために必要な魔力の総和の最小値を出力せよ。


入力例 1

5
4 3 5 1 2
10 40 20 60 50

出力例 1

210

高橋君は次のように操作を行うことで、目標を達成することができます。

  • 高橋君は (l,r)=(1,5) を選びます。魔力を \max(10,40,20,60,50)=60だけ消費して、操作後の各モンスターの体力は (3,2,4,0,1) となります。
  • 高橋君は (l,r)=(1,5) を選びます。魔力を \max(10,40,20,60,50)=60だけ消費して、操作後の各モンスターの体力は (2,1,3,-1,0) となります。
  • 高橋君は (l,r)=(1,3) を選びます。魔力を \max(10,40,20)=40だけ消費して、操作後の各モンスターの体力は (1,0,2,-1,0) となります。
  • 高橋君は (l,r)=(1,1) を選びます。魔力を \max(10)=10だけ消費して、操作後の各モンスターの体力は (0,0,2,-1,0) となります。
  • 高橋君は (l,r)=(3,3) を選びます。魔力を \max(20)=20だけ消費して、操作後の各モンスターの体力は (0,0,1,-1,0) となります。
  • 高橋君は (l,r)=(3,3) を選びます。魔力を \max(20)=20だけ消費して、操作後の各モンスターの体力は (0,0,0,-1,0) となります。

このとき、消費した魔力の総和は 60+60+40+10+20+20=210 となり、このときが最小となります。


入力例 2

1
1000000000
1000000000

出力例 2

1000000000000000000

入力例 3

10
522 4575 6426 9445 8772 81 3447 629 3497 7202
7775 4325 3982 4784 8417 2156 1932 5902 5728 8537

出力例 3

77917796

Score : 600 points

Problem Statement

There are N monsters along a number line. At the coordinate i (1\leq i\leq N) is a monster with a stamina of A_i.
Additionally, at the coordinate i, there is a permanent shield of a strength B_i.
This shield persists even when the monster at the same coordinate has a health of 0 or below.

Takahashi, a magician, can perform the following operation any number of times.

  • Choose integers l and r satisfying 1\leq l\leq r\leq N.
  • Then, consume \max(B_l, B_{l+1}, \ldots, B_r) MP (magic points) to decrease by 1 the stamina of each of the monsters at the coordinates l,l+1,\ldots,r.

When choosing l and r, it is fine if some of the monsters at the coordinates l,l+1,\ldots,r already have a stamina of 0 or below.
Note, however, that the shields at all those coordinates still exist.

Takahashi wants to make the stamina of every monster 0 or below.
Find the minimum total MP needed to achieve his objective.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq A_i,B_i \leq 10^9
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

Output

Print the minimum total MP needed to achieve his objective.


Sample Input 1

5
4 3 5 1 2
10 40 20 60 50

Sample Output 1

210

Takahashi can achieve his objective as follows.

  • Choose (l,r)=(1,5). Consume \max(10,40,20,60,50)=60 MP, and the staminas of the monsters are (3,2,4,0,1).
  • Choose (l,r)=(1,5). Consume \max(10,40,20,60,50)=60 MP, and the staminas of the monsters are (2,1,3,-1,0).
  • Choose (l,r)=(1,3). Consume \max(10,40,20)=40 MP, and the staminas of the monsters are (1,0,2,-1,0).
  • Choose (l,r)=(1,1). Consume \max(10)=10 MP, and the staminas of the monsters are (0,0,2,-1,0).
  • Choose (l,r)=(3,3). Consume \max(20)=20 MP, and the staminas of the monsters are (0,0,1,-1,0).
  • Choose (l,r)=(3,3). Consume \max(20)=20 MP, and the staminas of the monsters are (0,0,0,-1,0).

Here, he consumes a total of 60+60+40+10+20+20=210 MP, which is the minimum possible.


Sample Input 2

1
1000000000
1000000000

Sample Output 2

1000000000000000000

Sample Input 3

10
522 4575 6426 9445 8772 81 3447 629 3497 7202
7775 4325 3982 4784 8417 2156 1932 5902 5728 8537

Sample Output 3

77917796