Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
AtCoder Beginner Contest は、今回で 214 回目の開催となりました。
今までの AtCoder Beginner Contest において、出題される問題数は次のように変化しました。
- 1 回目から 125 回目までは 4 問
- 126 回目から 211 回目までは 6 問
- 212 回目から 214 回目までは 8 問
N 回目の AtCoder Beginner Contest において出題された問題数を求めてください。
制約
- 1 \leq N \leq 214
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
214
出力例 1
8
入力例 2
1
出力例 2
4
入力例 3
126
出力例 3
6
Score : 100 points
Problem Statement
This is the 214-th AtCoder Beginner Contest (ABC).
The ABCs so far have had the following number of problems.
- The 1-st through 125-th ABCs had 4 problems each.
- The 126-th through 211-th ABCs had 6 problems each.
- The 212-th through 214-th ABCs have 8 problems each.
Find the number of problems in the N-th ABC.
Constraints
- 1 \leq N \leq 214
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
214
Sample Output 1
8
Sample Input 2
1
Sample Output 2
4
Sample Input 3
126
Sample Output 3
6
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
N 個の整数 A_1,A_2,\dots,A_N が与えられます。
N 個の整数を合計した値を求めてください。
制約
- 1 \le N \le 100
- 1 \le A_i \le 100
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
3 2 7 2
出力例 1
11
3 個の整数 2,7,2 が与えられます。
答えは 2 + 7 + 2 = 11 です。
入力例 2
1 3
出力例 2
3
Score : 100 points
Problem Statement
You are given N integers A_1,A_2,\dots, and A_N.
Find the sum of the N integers.
Constraints
- 1 \le N \le 100
- 1 \le A_i \le 100
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print the answer.
Sample Input 1
3 2 7 2
Sample Output 1
11
You are given three integers: 2, 7, and 2.
The answer is 2 + 7 + 2 = 11.
Sample Input 2
1 3
Sample Output 2
3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君には N 人の友達がいます。N 人の友達はそれぞれ、友達 1 、友達 2 、\ldots 、友達 N というあだ名で呼ばれています。
ある日、高橋君はある恥ずかしい秘密を、友達の一人である友達 X に知られてしまいました。
i = 1, 2, \ldots, N について、友達 i が高橋君の秘密を知ったとき、友達 A_i がまだ高橋君の秘密を知らなければ、友達 i は高橋君の秘密を友達 A_i にも教えてしまいます。
高橋君の秘密は最終的に何人の友達に知られることになるでしょうか?
制約
- 2 \leq N \leq 10^5
- 1 \leq X \leq N
- 1 \leq A_i \leq N
- A_i \neq i
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X A_1 A_2 \cdots A_N
出力
答えを出力せよ。
入力例 1
4 2 3 1 1 2
出力例 1
3
高橋君の秘密は以下の流れで友達 1 、友達 2 、友達 3 の 3 人に知れ渡ります。
- ある日、高橋君は秘密を友達 2 に知られてしまいました。
- 秘密を知った友達 2 は、その秘密を友達 1 に教えます。
- 秘密を知った友達 1 は、その秘密を友達 3 に教えます。
高橋君の秘密は最終的に 3 人の友達に知られることになるため、3 を出力します。
入力例 2
20 12 7 11 10 1 7 20 14 2 17 3 2 5 19 20 8 14 18 2 10 10
出力例 2
7
Score : 200 points
Problem Statement
Takahashi has N friends. They have nicknames: Friend 1, Friend 2, \ldots, Friend N.
One day, Takahashi accidentally let one of his friends, Friend X, learn his shameful secret.
For each i = 1, 2, \ldots, N, when Friend i learns the secret, he/she will share it with Friend A_i, if Friend A_i has not already learned it.
How many of Takahashi's friends will learn the secret in the end?
Constraints
- 2 \leq N \leq 10^5
- 1 \leq X \leq N
- 1 \leq A_i \leq N
- A_i \neq i
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X A_1 A_2 \cdots A_N
Output
Print the answer.
Sample Input 1
4 2 3 1 1 2
Sample Output 1
3
Takahashi's secret will be learned by Friend 1, Friend 2, and Friend 3, as follows.
- One day, Takahashi let Friend 2 learn the secret.
- Friend 2 shares it with Friend 1.
- Friend 1 shares it with Friend 3.
In the end, three of his friends learn the secret, so we print 3.
Sample Input 2
20 12 7 11 10 1 7 20 14 2 17 3 2 5 19 20 8 14 18 2 10 10
Sample Output 2
7
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
-10^{18} 以上 10^{18} 以下の整数 X が与えられるので、\left\lceil \dfrac{X}{10} \right\rceil を出力してください。
ここで、\left\lceil a \right\rceil は a 以上の整数のうち最小のものを意味します。
制約
- -10^{18} \leq X \leq 10^{18}
- X は整数
入力
入力は以下の形式で標準入力から与えられる。
X
出力
\left\lceil \dfrac{X}{10} \right\rceil を整数として出力せよ。
入力例 1
27
出力例 1
3
\frac{27}{10} = 2.7 以上の整数は 3, 4, 5, \dots です。この中で一番小さい整数は 3 なので、\left \lceil \frac{27}{10} \right \rceil = 3 となります。
入力例 2
-13
出力例 2
-1
\frac{-13}{10} = -1.3 以上の整数は、全ての正整数および 0, -1 です。この中で一番小さい整数は -1 なので、\left \lceil \frac{-13}{10} \right \rceil = -1 となります。
入力例 3
40
出力例 3
4
\frac{40}{10} = 4 以上の整数で一番小さい整数は 4 自身です。
入力例 4
-20
出力例 4
-2
入力例 5
123456789123456789
出力例 5
12345678912345679
Score: 200 points
Problem Statement
Given an integer X between -10^{18} and 10^{18}, inclusive, print \left\lceil \dfrac{X}{10} \right\rceil.
Here, \left\lceil a \right\rceil denotes the smallest integer not less than a.
Constraints
- -10^{18} \leq X \leq 10^{18}
- X is an integer.
Input
The input is given from Standard Input in the following format:
X
Output
Print \left\lceil \dfrac{X}{10} \right\rceil as an integer.
Sample Input 1
27
Sample Output 1
3
The integers not less than \frac{27}{10} = 2.7 are 3, 4, 5, \dots. Among these, the smallest is 3, so \left \lceil \frac{27}{10} \right \rceil = 3.
Sample Input 2
-13
Sample Output 2
-1
The integers not less than \frac{-13}{10} = -1.3 are all positive integers, 0, and -1. Among these, the smallest is -1, so \left \lceil \frac{-13}{10} \right \rceil = -1.
Sample Input 3
40
Sample Output 3
4
The smallest integer not less than \frac{40}{10} = 4 is 4 itself.
Sample Input 4
-20
Sample Output 4
-2
Sample Input 5
123456789123456789
Sample Output 5
12345678912345679
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
正整数 N,Q と、長さ N の英小文字からなる文字列 S が与えられます。
以下で説明されるクエリを Q 個処理してください。クエリは次の 2 種類のいずれかです。
1 x
: 「S の末尾の文字を削除し、先頭に挿入する」という操作を x 回連続で行う。2 x
: S の x 番目の文字を出力する。
制約
- 2 \le N \le 5 \times 10^5
- 1 \le Q \le 5 \times 10^5
- 1 \le x \le N
- |S|=N
- S は英小文字からなる。
2 x
の形式のクエリが 1 個以上与えられる。- N,Q,x はすべて整数。
入力
入力は以下の形式で標準入力から与えられる。
N Q S \mathrm{query}_1 \mathrm{query}_2 \vdots \mathrm{query}_Q
それぞれのクエリは以下の形式で与えられる。ここで、t は 1 または 2 である。
t x
出力
2 x
の形式の各クエリについて、答えを一行に出力せよ。
入力例 1
3 3 abc 2 2 1 1 2 2
出力例 1
b a
1 個目のクエリのとき、S は abc
なので 2 文字目の b
を出力します。
2 個目のクエリのとき、S は abc
から cab
に変わります。
3 個目のクエリのとき、S は cab
なので 2 文字目の a
を出力します。
入力例 2
10 8 dsuccxulnl 2 4 2 7 1 2 2 7 1 1 1 2 1 3 2 5
出力例 2
c u c u
Score : 300 points
Problem Statement
You are given positive integers N and Q, and a string S of length N consisting of lowercase English letters.
Process Q queries. Each query is of one of the following two types.
1 x
: Perform the following x times in a row: delete the last character of S and append it to the beginning.2 x
: Print the x-th character of S.
Constraints
- 2 \le N \le 5 \times 10^5
- 1 \le Q \le 5 \times 10^5
- 1 \le x \le N
- |S|=N
- S consists of lowercase English letters.
- At least one query in the format
2 x
. - N, Q, x are all integers.
Input
Input is given from Standard Input in the following format:
N Q S \mathrm{query}_1 \mathrm{query}_2 \vdots \mathrm{query}_Q
Each query is in the following format, where t is 1 or 2:
t x
Output
For each query in the format 2 x
, print the answer in a single line.
Sample Input 1
3 3 abc 2 2 1 1 2 2
Sample Output 1
b a
In the 1-st query, S is abc
, so the 2-nd character b
should be printed.
In the 2-nd query, S is changed from abc
to cab
.
In the 3-rd query, S is cab
, so the 2-nd character a
should be printed.
Sample Input 2
10 8 dsuccxulnl 2 4 2 7 1 2 2 7 1 1 1 2 1 3 2 5
Sample Output 2
c u c u
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
縦 H マス、横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i,j) と表します。
(i,j) には文字 G_{i,j} が書きこまれています。ここで G_{i,j} は U
, D
, L
, R
のいずれかです。
あなたは (1,1) にいます。あなたは移動することができなくなるまで次の操作を繰り返します。
あなたは現在 (i,j) にいるとする。
G_{i,j} がU
であり、かつ i \neq 1 ならば (i-1,j) へ移動する。
G_{i,j} がD
であり、かつ i \neq H ならば (i+1,j) へ移動する。
G_{i,j} がL
であり、かつ j \neq 1 ならば (i,j-1) へ移動する。
G_{i,j} がR
であり、かつ j \neq W ならば (i,j+1) へ移動する。
それ以外の場合、あなたは移動することができない。
操作を終了した時点であなたがいるマスを出力してください。
ただし、あなたが操作を終了することなく無限に移動し続ける場合は -1
を出力してください。
制約
- 1 \leq H, W \leq 500
- G_{i,j} は
U
,D
,L
,R
のいずれかである。 - H, W は整数である。
入力
入力は以下の形式で標準入力から与えられる。
H W G_{1,1}G_{1,2}\dots G_{1,W} G_{2,1}G_{2,2}\dots G_{2,W} \vdots G_{H,1}G_{H,2}\dots G_{H,W}
出力
操作を終了した時点であなたが (i,j) にいる場合は次の形式で出力せよ。
i j
また、無限に動き続ける場合は -1
を出力せよ。
入力例 1
2 3 RDU LRU
出力例 1
1 3
あなたは (1, 1) \to (1, 2) \to (2, 2) \to (2, 3) \to (1, 3) の順に動いたあとに操作を終了します。よって答えは (1, 3) です。
入力例 2
2 3 RRD ULL
出力例 2
-1
あなたは (1, 1) \to (1, 2) \to (1, 3) \to (2, 3) \to (2, 2) \to (2, 1) \to (1, 1) \to (1, 2) \to \dots というように無限に動き続けます。この場合は -1
を出力します。
入力例 3
9 44 RRDDDDRRRDDDRRRRRRDDDRDDDDRDDRDDDDDDRRDRRRRR RRRDLRDRDLLLLRDRRLLLDDRDLLLRDDDLLLDRRLLLLLDD DRDLRLDRDLRDRLDRLRDDLDDLRDRLDRLDDRLRRLRRRDRR DDLRRDLDDLDDRLDDLDRDDRDDDDRLRRLRDDRRRLDRDRDD RDLRRDLRDLLLLRRDLRDRRDRRRDLRDDLLLLDDDLLLLRDR RDLLLLLRDLRDRLDDLDDRDRRDRLDRRRLDDDLDDDRDDLDR RDLRRDLDDLRDRLRDLDDDLDDRLDRDRDLDRDLDDLRRDLRR RDLDRRLDRLLLLDRDRLLLRDDLLLLLRDRLLLRRRRLLLDDR RRRRDRDDRRRDDRDDDRRRDRDRDRDRRRRRRDDDRDDDDRRR
出力例 3
9 5
Score : 300 points
Problem Statement
We have a grid with H horizontal rows and W vertical columns. (i, j) denotes the square at the i-th row from the top and j-th column from the left.
(i,j) has a character G_{i,j} written on it. G_{i,j} is U
, D
, L
, or R
.
You are initially at (1,1). You repeat the following operation until you cannot make a move.
Let (i,j) be the square you are currently at.
If G_{i,j} isU
and i \neq 1, move to (i-1,j).
If G_{i,j} isD
and i \neq H, move to (i+1,j).
If G_{i,j} isL
and j \neq 1, move to (i,j-1).
If G_{i,j} isR
and j \neq W, move to (i,j+1).
Otherwise, you cannot make a move.
Print the square you end up at when you cannot make a move.
If you indefinitely repeat moving, print -1
instead.
Constraints
- 1 \leq H, W \leq 500
- G_{i,j} is
U
,D
,L
, orR
. - H and W are integers.
Input
Input is given from Standard Input in the following format:
H W G_{1,1}G_{1,2}\dots G_{1,W} G_{2,1}G_{2,2}\dots G_{2,W} \vdots G_{H,1}G_{H,2}\dots G_{H,W}
Output
If you end up at (i, j), print it in the following format:
i j
If you indefinitely repeat moving, print -1
.
Sample Input 1
2 3 RDU LRU
Sample Output 1
1 3
You will move as (1, 1) \to (1, 2) \to (2, 2) \to (2, 3) \to (1, 3), ending up here, so the answer is (1, 3).
Sample Input 2
2 3 RRD ULL
Sample Output 2
-1
You will indefinitely repeat moving as (1, 1) \to (1, 2) \to (1, 3) \to (2, 3) \to (2, 2) \to (2, 1) \to (1, 1) \to (1, 2) \to \dots, so -1
should be printed in this case.
Sample Input 3
9 44 RRDDDDRRRDDDRRRRRRDDDRDDDDRDDRDDDDDDRRDRRRRR RRRDLRDRDLLLLRDRRLLLDDRDLLLRDDDLLLDRRLLLLLDD DRDLRLDRDLRDRLDRLRDDLDDLRDRLDRLDDRLRRLRRRDRR DDLRRDLDDLDDRLDDLDRDDRDDDDRLRRLRDDRRRLDRDRDD RDLRRDLRDLLLLRRDLRDRRDRRRDLRDDLLLLDDDLLLLRDR RDLLLLLRDLRDRLDDLDDRDRRDRLDRRRLDDDLDDDRDDLDR RDLRRDLDDLRDRLRDLDDDLDDRLDRDRDLDRDLDDLRRDLRR RDLDRRLDRLLLLDRDRLLLRDDLLLLLRDRLLLRRRRLLLDDR RRRRDRDDRRRDDRDDDRRRDRDRDRDRRRRRRDDDRDDDDRRR
Sample Output 3
9 5
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
0 から N-1 までの番号がつけられた N 個のマスが並んでいます。 今から、すぬけくんが以下の手順に従って全てのマスに印をつけていきます。
- マス 0 に印をつける。
-
次の i - iii の手順を N−1 回繰り返す。
- 最後に印をつけたマスの番号を A としたとき、変数 x を (A+D) \bmod N で初期化する。
- マス x に印が付いている限り、 x を (x+1) \bmod N に更新することを繰り返す。
- マス x に印をつける。
すぬけくんが K 番目に印をつけるマスの番号を求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\leq T \leq 10^5
- 1\leq K\leq N \leq 10^9
- 1\leq D \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。ここで、\mathrm{test}_i は i 番目のテストケースを意味する。
T \mathrm{test}_1 \mathrm{test}_2 \vdots \mathrm{test}_T
各テストケースは以下の形式で与えられる。
N D K
出力
T 行出力せよ。
i\ (1\leq i \leq T) 行目には、i 番目のテストケースに対する答えを出力せよ。
入力例 1
9 4 2 1 4 2 2 4 2 3 4 2 4 5 8 1 5 8 2 5 8 3 5 8 4 5 8 5
出力例 1
0 2 1 3 0 3 1 4 2
N=4,D=2 のとき、すぬけくんは以下のように印をつけていきます。
- マス 0 に印をつける。
- (1回目) x=(0+2)\bmod 4=2 と初期化する。マス 2 は印がついていないので、印をつける。
(2回目) x=(2+2)\bmod 4=0 と初期化する。マス 0 は印がついているので、x=(0+1)\bmod 4=1 と更新する。マス 1 は印がついていないので、印をつける。
(3回目) x=(1+2)\bmod 4=3 と初期化する。マス 3 は印がついていないので、印をつける。
Score : 400 points
Problem Statement
There are N squares indexed 0 through (N-1) arranged in a line. Snuke is going to mark every square by the following procedure.
- Mark square 0.
-
Repeat the following steps i - iii (N-1) times.
- Initialize a variable x with (A+D) \bmod N, where A is the index of the square marked last time.
- While square x is marked, repeat replacing x with (x+1) \bmod N.
- Mark square x.
Find the index of the square that Snuke marks for the K-th time.
Given T test cases, find the answer for each of them.
Constraints
- 1\leq T \leq 10^5
- 1\leq K\leq N \leq 10^9
- 1\leq D \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format, where \mathrm{test}_i denotes the i-th test case:
T \mathrm{test}_1 \mathrm{test}_2 \vdots \mathrm{test}_T
Each test case is given in the following format:
N D K
Output
Print T lines.
The i-th (1\leq i \leq T) line should contain the answer to the i-th test case.
Sample Input 1
9 4 2 1 4 2 2 4 2 3 4 2 4 5 8 1 5 8 2 5 8 3 5 8 4 5 8 5
Sample Output 1
0 2 1 3 0 3 1 4 2
If N=4 and D=2, Snuke marks the squares as follows.
- Mark square 0.
- (1-st iteration) Let x=(0+2)\bmod 4=2. Since square 2 is not marked, mark it.
(2-nd iteration) Let x=(2+2)\bmod 4=0. Since square 0 is marked, let x=(0+1)\bmod 4=1. Since square 1 is not marked, mark it.
(3-rd iteration) Let x=(1+2)\bmod 4=3. Since square 3 is not marked, mark it.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
頂点に 1 から N の番号がついた N 頂点 N 辺の有向グラフがあります。
全ての頂点の出次数は 1 で、頂点 i から出ている辺は頂点 a_i へ伸びています。
頂点の組 (u, v) であって、頂点 u から頂点 v へ到達可能であるものの個数を求めてください。
ここで、頂点 u から頂点 v へ到達可能であるとは、ある長さ K+1 の頂点の列 w_0, w_1, \dots, w_K であって次の条件を全て満たすものが存在することをいいます。特に、u = v の時は常に到達可能です。
- w_0 = u
- w_K = v
- 0 \leq i \lt K を満たす全ての i について、頂点 w_i から頂点 w_{i+1} へ伸びる辺が存在する。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq a_i \leq N
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N a_1 a_2 \dots a_N
出力
頂点の組 (u, v) であって、頂点 u から頂点 v へ到達可能であるものの個数を出力せよ。
入力例 1
4 2 1 1 4
出力例 1
8
頂点 1 から到達可能である頂点は頂点 1, 2 です。
頂点 2 から到達可能である頂点は頂点 1, 2 です。
頂点 3 から到達可能である頂点は頂点 1, 2, 3 です。
頂点 4 から到達可能である頂点は頂点 4 のみです。
よって、頂点の組 (u, v) であって頂点 u から頂点 v へ到達可能であるものの個数は 8 個です。
頂点 4 から出ている辺は自己ループ、すなわち頂点 4 自身へ伸びている辺である点に注意してください。
入力例 2
5 2 4 3 1 2
出力例 2
14
入力例 3
10 6 10 4 1 5 9 8 6 5 1
出力例 3
41
Score : 450 points
Problem Statement
There is a directed graph with N vertices numbered 1 to N and N edges.
The out-degree of every vertex is 1, and the edge from vertex i points to vertex a_i.
Count the number of pairs of vertices (u, v) such that vertex v is reachable from vertex u.
Here, vertex v is reachable from vertex u if there exists a sequence of vertices w_0, w_1, \dots, w_K of length K+1 that satisfies the following conditions. In particular, if u = v, it is always reachable.
- w_0 = u.
- w_K = v.
- For every 0 \leq i \lt K, there is an edge from vertex w_i to vertex w_{i+1}.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq a_i \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N a_1 a_2 \dots a_N
Output
Print the number of pairs of vertices (u, v) such that vertex v is reachable from vertex u.
Sample Input 1
4 2 1 1 4
Sample Output 1
8
The vertices reachable from vertex 1 are vertices 1, 2.
The vertices reachable from vertex 2 are vertices 1, 2.
The vertices reachable from vertex 3 are vertices 1, 2, 3.
The vertex reachable from vertex 4 is vertex 4.
Therefore, the number of pairs of vertices (u, v) such that vertex v is reachable from vertex u is 8.
Note that the edge from vertex 4 is a self-loop, that is, it points to vertex 4 itself.
Sample Input 2
5 2 4 3 1 2
Sample Output 2
14
Sample Input 3
10 6 10 4 1 5 9 8 6 5 1
Sample Output 3
41
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
長さ L のパンが 1 つあり、これを N 人の子供たちに切り分けます。 i 番目 (1\leq i\leq N) の子供は長さ A_i のパンを欲しがっています。
そこで、高橋君は次の操作を繰り返して、長さ A_1,A_2,\ldots,A_N のパンを切り出して配ることにしました。
長さ k のパン 1 つと 1 以上 k-1 以下の整数 x を選ぶ。選んだパンを長さ x のパンと 長さ k-x のパンに切り分ける。
このとき、x の値によらずコストが k かかる。
それぞれの子供に配るパンは長さがちょうど A_i のパン 1 つである必要がありますが、誰にも配られずに余るパンがいくつあっても構いません。
このとき、全ての子供たちにパンを配るために必要な最小のコストを求めてください。
制約
- 2 \leq N \leq 2\times 10^5
- 1\leq A_i\leq 10^9
- A_1+A_2+\cdots+A_N\leq L\leq 10^{15}
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N L A_1 A_2 \ldots A_N
出力
全ての子供たちにパンを配るために必要な最小のコストを出力せよ。
入力例 1
5 7 1 2 1 2 1
出力例 1
16
高橋君は次のようにしてパンを切り分けて配ることができます。
- 長さ 7 のパンと整数 x=3 を選ぶ。パンは長さ 3 のパンと長さ 4 のパンに切り分けられる。 (コスト 7 )
- 長さ 3 のパンと整数 x=1 を選ぶ。パンは長さ 1 のパンと長さ 2 のパンに切り分けられる。前者を 1 番目の子供に配る。 (コスト 3 )
- 長さ 2 のパンと整数 x=1 を選ぶ。パンは長さ 1 のパン 2 つに切り分けられる。これを 3,5 番目の子供に配る。 (コスト 2 )
- 長さ 4 のパンと整数 x=2 を選ぶ。パンは長さ 2 のパン 2 つに切り分けられる。これを 2,4 番目の子供に配る。 (コスト 4 )
このとき、コストは 7+3+2+4=16 かかり、これが最小です。 また、余るパンはありません。
入力例 2
3 1000000000000000 1000000000 1000000000 1000000000
出力例 2
1000005000000000
それぞれの子供に配るパンの長さはちょうど A_i でなければならない事に注意してください。
Score : 500 points
Problem Statement
We have a loaf of bread of length L, which we will cut and distribute to N children.
The i-th child (1\leq i\leq N) wants a loaf of length A_i.
Now, Takahashi will repeat the operation below to cut the loaf into lengths A_1, A_2, \ldots, A_N for the children.
Choose a loaf of length k and an integer x between 1 and k-1 (inclusive). Cut the loaf into two loaves of length x and k-x.
This operation incurs a cost of k regardless of the value of x.
Each child i must receive a loaf of length exactly A_i, but it is allowed to leave some loaves undistributed.
Find the minimum cost needed to distribute loaves to all children.
Constraints
- 2 \leq N \leq 2\times 10^5
- 1\leq A_i\leq 10^9
- A_1+A_2+\cdots+A_N\leq L\leq 10^{15}
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N L A_1 A_2 \ldots A_N
Output
Print the minimum cost needed to distribute loaves to all children.
Sample Input 1
5 7 1 2 1 2 1
Sample Output 1
16
Takahashi can cut the loaf for the children as follows.
- Choose the loaf of length 7 and x=3, cutting it into loaves of length 3 and 4 for a cost of 7.
- Choose the loaf of length 3 and x=1, cutting it into loaves of length 1 and 2 for a cost of 3. Give the former to the 1-st child.
- Choose the loaf of length 2 and x=1, cutting it into two loaves of length 1 for a cost of 2. Give them to the 3-rd and 5-th children.
- Choose the loaf of length 4 and x=2, cutting it into two loaves of length 2 for a cost of 4. Give them to the 2-nd and 4-th children.
This incurs a cost of 7+3+2+4=16, which is the minimum possible. There will be no leftover loaves.
Sample Input 2
3 1000000000000000 1000000000 1000000000 1000000000
Sample Output 2
1000005000000000
Note that each child i must receive a loaf of length exactly A_i.