Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
高橋君はある会社の採用面接を受けました。
面接官の人数 N と、各面接官の高橋君への評価を表す長さ N の文字列 S が与えられます。
i=1,2,\ldots,N に対し S の i 文字目が i 番目の面接官の評価に対応し、o は「良」、- は「可」、x は 「不可」を表します。
高橋君は以下の 2 つの条件を両方満たすならば合格、そうでなければ不合格です。
- 「良」と評価した面接官が少なくとも 1 人いる
- 「不可」と評価した面接官がいない
高橋君が合格かどうかを判定してください。
制約
- 1 \leq N \leq 100
- S は
o,-,xのみからなる長さが N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
高橋君が合格ならば Yes と、そうでなければ No と出力せよ。
入力例 1
4 oo--
出力例 1
Yes
1, 2 番目の面接官が「良」と評価していて、さらに「不可」と評価した面接官がいないため合格です。
入力例 2
3 ---
出力例 2
No
「良」と評価した面接官が 1 人もいないため不合格です。
入力例 3
1 o
出力例 3
Yes
入力例 4
100 ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooox
出力例 4
No
100 番目の面接官が「不可」と評価しているため不合格です。
Score : 100 points
Problem Statement
Takahashi had a job interview.
You are given the number of interviewers, N, and a string S of length N representing the interviewers' evaluations of him.
For each i=1,2,\ldots,N, the i-th character of S corresponds to the i-th interviewer's evaluation; o means Good, - means Fair, and x means Poor.
Takahashi will pass if both of the following conditions are satisfied, and fail otherwise.
- At least one interviewer's evaluation is Good.
- No interviewer's evaluation is Poor.
Determine whether Takahashi passes.
Constraints
- 1 \leq N \leq 100
- S is a string of length N consisting of
o,-, andx.
Input
The input is given from Standard Input in the following format:
N S
Output
If Takahashi passes, print Yes; otherwise, print No.
Sample Input 1
4 oo--
Sample Output 1
Yes
The first and second interviewers' evaluations are Good, and no interviewer's evaluation is Poor, so he passes.
Sample Input 2
3 ---
Sample Output 2
No
No interviewer's evaluation is Good, so he fails.
Sample Input 3
1 o
Sample Output 3
Yes
Sample Input 4
100 ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooox
Sample Output 4
No
The 100-th interviewer's evaluation is Poor, so he fails.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
正の整数 X について、レベル X の 龍文字列 とは、1 個の L, X 個の o, 1 個の n, 1 個の g をこの順に並べた長さ (X+3) の文字列です。
正の整数 N が与えられるので、レベル N の龍文字列を出力してください。
大文字と小文字は区別されることに注意してください。
制約
- 1\leq N\leq 2024
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
レベル N の龍文字列を出力せよ。
入力例 1
3
出力例 1
Looong
1 個の L, 3 個の o, 1 個の n, 1 個の g をこの順に並べた文字列は Looong です。
入力例 2
1
出力例 2
Long
Score: 100 points
Problem Statement
For a positive integer X, the Dragon String of level X is a string of length (X+3) formed by one L, X occurrences of o, one n, and one g arranged in this order.
You are given a positive integer N. Print the Dragon String of level N.
Note that uppercase and lowercase letters are distinguished.
Constraints
- 1 \leq N \leq 2024
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print the Dragon String of level N.
Sample Input 1
3
Sample Output 1
Looong
Arranging one L, three os, one n, and one g in this order yields Looong.
Sample Input 2
1
Sample Output 2
Long
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
正の整数 N が与えられます。
N=2^x3^y を満たす整数 x,y が存在するなら Yes 、そうでなければ No と出力してください。
制約
- 1\leq N\leq10^{18}
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
条件を満たす整数 x,y が存在するなら Yes 、そうでなければ No と 1 行に出力せよ。
入力例 1
324
出力例 1
Yes
x=2,y=4 とすると、2^x3^y=2^23^4=4\times81=324 となるため条件を満たします。
よって、Yes と出力してください。
入力例 2
5
出力例 2
No
どのような整数 x,y をとっても 2^x3^y=5 とすることはできません。
よって、No と出力してください。
入力例 3
32
出力例 3
Yes
x=5,y=0 とすると、2^x3^y=32\times1=32 となるため、Yes と出力してください。
入力例 4
37748736
出力例 4
Yes
Score : 200 points
Problem Statement
You are given a positive integer N.
If there are integers x and y such that N=2^x3^y, print Yes; otherwise, print No.
Constraints
- 1\leq N\leq10^{18}
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print a single line containing Yes if there are integers x and y that satisfy the condition, and No otherwise.
Sample Input 1
324
Sample Output 1
Yes
For x=2,y=4, we have 2^x3^y=2^23^4=4\times81=324, so the condition is satisfied.
Thus, you should print Yes.
Sample Input 2
5
Sample Output 2
No
There are no integers x and y such that 2^x3^y=5.
Thus, you should print No.
Sample Input 3
32
Sample Output 3
Yes
For x=5,y=0, we have 2^x3^y=32\times1=32, so you should print Yes.
Sample Input 4
37748736
Sample Output 4
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
整数 L,R と、英小文字のみからなる文字列 S が与えられます。
S の L 文字目から R 文字目までの部分を反転した(すなわち、 L 文字目から R 文字目までの文字の並びを逆にした)文字列を出力してください。
制約
- S は英小文字のみからなる。
- 1 \le |S| \le 10^5 (|S| は S の長さ)
- L,R は整数
- 1 \le L \le R \le |S|
入力
入力は以下の形式で標準入力から与えられる。
L R S
出力
問題文の指示通り出力せよ。
入力例 1
3 7 abcdefgh
出力例 1
abgfedch
abcdefgh の 3 文字目から 7 文字目までの部分を反転すると、 abgfedch となります。
入力例 2
1 7 reviver
出力例 2
reviver
操作を行った結果が元の文字列と同一であることもあります。
入力例 3
4 13 merrychristmas
出力例 3
meramtsirhcyrs
Score : 200 points
Problem Statement
You are given integers L, R, and a string S consisting of lowercase English letters.
Print this string after reversing (the order of) the L-th through R-th characters.
Constraints
- S consists of lowercase English letters.
- 1 \le |S| \le 10^5 (|S| is the length of S.)
- L and R are integers.
- 1 \le L \le R \le |S|
Input
Input is given from Standard Input in the following format:
L R S
Output
Print the specified string.
Sample Input 1
3 7 abcdefgh
Sample Output 1
abgfedch
After reversing the 3-rd through 7-th characters of abcdefgh, we have abgfedch.
Sample Input 2
1 7 reviver
Sample Output 2
reviver
The operation may result in the same string as the original.
Sample Input 3
4 13 merrychristmas
Sample Output 3
meramtsirhcyrs
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
AtCoder社ではカードを使った 1 人ゲームが流行っています。
ゲームで使う各カードには、英小文字 1 文字または @ の文字が書かれており、いずれのカードも十分多く存在します。
ゲームは以下の手順で行います。
- カードを同じ枚数ずつ 2 列に並べる。
@のカードを、それぞれa,t,c,o,d,e,rのいずれかのカードと置き換える。- 2 つの列が一致していれば勝ち。そうでなければ負け。
このゲームに勝ちたいあなたは、次のようなイカサマをすることにしました。
- 手順 1 以降の好きなタイミングで、列内のカードを自由に並び替えてよい。
手順 1 で並べられた 2 つの列を表す 2 つの文字列 S,T が与えられるので、イカサマをしてもよいときゲームに勝てるか判定してください。
制約
- S,T は英小文字と
@からなる - S,T の長さは等しく 1 以上 2\times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
イカサマをしてもよいとき、ゲームに勝てるなら Yes、勝てないなら No と出力せよ。
入力例 1
ch@ku@ai choku@@i
出力例 1
Yes
@ をうまく置き換えることによって、両方とも chokudai と一致させることが可能です。
入力例 2
ch@kud@i akidu@ho
出力例 2
Yes
イカサマをし、@ をうまく置き換えることによって、両方とも chokudai と一致させることが可能です。
入力例 3
aoki @ok@
出力例 3
No
イカサマをしても勝つことはできません。
入力例 4
aa bb
出力例 4
No
Score : 300 points
Problem Statement
A single-player card game is popular in AtCoder Inc.
Each card in the game has a lowercase English letter or the symbol @ written on it. There is plenty number of cards for each kind.
The game goes as follows.
- Arrange the same number of cards in two rows.
- Replace each card with
@with one of the following cards:a,t,c,o,d,e,r. - If the two rows of cards coincide, you win. Otherwise, you lose.
To win this game, you will do the following cheat.
- Freely rearrange the cards within a row whenever you want after step 1.
You are given two strings S and T, representing the two rows you have after step 1. Determine whether it is possible to win with cheating allowed.
Constraints
- S and T consist of lowercase English letters and
@. - The lengths of S and T are equal and between 1 and 2\times 10^5, inclusive.
Input
The input is given from Standard Input in the following format:
S T
Output
If it is possible to win with cheating allowed, print Yes; otherwise, print No.
Sample Input 1
ch@ku@ai choku@@i
Sample Output 1
Yes
You can replace the @s so that both rows become chokudai.
Sample Input 2
ch@kud@i akidu@ho
Sample Output 2
Yes
You can cheat and replace the @s so that both rows become chokudai.
Sample Input 3
aoki @ok@
Sample Output 3
No
You cannot win even with cheating.
Sample Input 4
aa bb
Sample Output 4
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
N 頂点 N 辺の有向グラフがあります。
i 番目の辺は頂点 i から 頂点 A_i に伸びます。 ( i \neq A_i であることは制約より保証されます )
同一頂点を複数回含まない有向閉路をひとつ求めてください。
なお、この問題の制約下で答えが存在することが示せます。
注釈
この問題では、有向閉路とは以下の条件を全て満たす頂点の列 B=(B_1,B_2,\dots,B_M) であるとします。
- M \ge 2
- B_i から B_{i+1} に辺が伸びている。 ( 1 \le i \le M-1 )
- B_M から B_1 に辺が伸びている。
- i \neq j ならば B_i \neq B_j
制約
- 入力は全て整数
- 2 \le N \le 2 \times 10^5
- 1 \le A_i \le N
- A_i \neq i
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
以下の形式で出力せよ。
M B_1 B_2 \dots B_M
M は出力する有向閉路の頂点数であり、 B_i は有向閉路の i 番目の頂点である。
出力は以下の条件を満たす必要がある。
- 2 \le M
- B_{i+1} = A_{B_i} ( 1 \le i \le M-1 )
- B_{1} = A_{B_M}
- B_i \neq B_j ( i \neq j )
答えとして考えられるものが複数ある場合、どれを出力しても正解となる。
入力例 1
7 6 7 2 1 3 4 5
出力例 1
4 7 5 3 2
7 \rightarrow 5 \rightarrow 3 \rightarrow 2 \rightarrow 7 は確かに有向閉路になっています。
この入力に対応するグラフは以下の通りです。

他の正解となる出力の例は以下の通りです。
4 2 7 5 3
3 4 1 6
グラフが連結であるとは限らないことに注意してください。
入力例 2
2 2 1
出力例 2
2 1 2
1 \rightarrow 2 と 2 \rightarrow 1 の辺が双方存在するケースです。
この場合、 1 \rightarrow 2 \rightarrow 1 は確かに有向閉路になっています。
この入力に対応するグラフは以下の通りです。
図中 1 \leftrightarrow 2 で 1 \rightarrow 2 と 2 \rightarrow 1 の辺が双方あることを表現しています。

入力例 3
8 3 7 4 7 3 3 8 2
出力例 3
3 2 7 8
この入力に対応するグラフは以下の通りです。

Score : 350 points
Problem Statement
There is a directed graph with N vertices and N edges.
The i-th edge goes from vertex i to vertex A_i. (The constraints guarantee that i \neq A_i.)
Find a directed cycle without the same vertex appearing multiple times.
It can be shown that a solution exists under the constraints of this problem.
Notes
The sequence of vertices B = (B_1, B_2, \dots, B_M) is called a directed cycle when all of the following conditions are satisfied:
- M \geq 2
- The edge from vertex B_i to vertex B_{i+1} exists. (1 \leq i \leq M-1)
- The edge from vertex B_M to vertex B_1 exists.
- If i \neq j, then B_i \neq B_j.
Constraints
- All input values are integers.
- 2 \le N \le 2 \times 10^5
- 1 \le A_i \le N
- A_i \neq i
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print a solution in the following format:
M B_1 B_2 \dots B_M
M is the number of vertices, and B_i is the i-th vertex in the directed cycle.
The following conditions must be satisfied:
- 2 \le M
- B_{i+1} = A_{B_i} ( 1 \le i \le M-1 )
- B_{1} = A_{B_M}
- B_i \neq B_j ( i \neq j )
If multiple solutions exist, any of them will be accepted.
Sample Input 1
7 6 7 2 1 3 4 5
Sample Output 1
4 7 5 3 2
7 \rightarrow 5 \rightarrow 3 \rightarrow 2 \rightarrow 7 is indeed a directed cycle.
Here is the graph corresponding to this input:

Here are other acceptable outputs:
4 2 7 5 3
3 4 1 6
Note that the graph may not be connected.
Sample Input 2
2 2 1
Sample Output 2
2 1 2
This case contains both of the edges 1 \rightarrow 2 and 2 \rightarrow 1.
In this case, 1 \rightarrow 2 \rightarrow 1 is indeed a directed cycle.
Here is the graph corresponding to this input, where 1 \leftrightarrow 2 represents the existence of both 1 \rightarrow 2 and 2 \rightarrow 1:

Sample Input 3
8 3 7 4 7 3 3 8 2
Sample Output 3
3 2 7 8
Here is the graph corresponding to this input:

Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
空の筒があります。Q 個のクエリが与えられるので順に処理してください。
クエリは次の 2 種類のいずれかです。
1 x c:数 x が書かれたボールを筒の右側から c 個入れる2 c:筒の左側からボールを c 個取り出し、取り出したボールに書かれている数の合計を出力する
なお、筒の中でボールの順序が入れ替わることはないものとします。
制約
- 1 \leq Q \leq 2\times 10^5
- 0 \leq x \leq 10^9
- 1 \leq c \leq 10^9
2 cのクエリが与えられるとき、筒の中には c 個以上のボールがある- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
Q
{\rm query}_1
\vdots
{\rm query}_Q
i 番目のクエリを表す {\rm query}_i は以下の 2 種類のいずれかである。
1 x c
2 c
出力
2 c のクエリに対する答えを順に改行区切りで出力せよ。
入力例 1
4 1 2 3 2 2 1 3 4 2 3
出力例 1
4 8
- 1 番目のクエリでは、2 が書かれたボールを筒の右側から 3 個入れます。
筒の中のボールに書かれた数は左から順に (2,2,2) となります。 - 2 番目のクエリでは、筒の左側からボールを 2 個取り出します。
取り出されたボールに書かれている数はそれぞれ 2,2 であり、合計は 4 であるため、これを出力します。 筒の中のボールに書かれた数は左から順に (2) となります。 - 3 番目のクエリでは、3 が書かれたボールを筒の右側から 4 個入れます。
筒の中のボールに書かれた数は左から順に (2,3,3,3,3) となります。 - 4 番目のクエリでは、筒の左側からボールを 3 個取り出します。
取り出されたボールに書かれている数はそれぞれ 2,3,3 であり、合計は 8 であるため、これを出力します。 筒の中のボールに書かれた数は左から順に (3,3) となります。
入力例 2
2 1 1000000000 1000000000 2 1000000000
出力例 2
1000000000000000000
入力例 3
5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
出力例 3
出力するものがないこともあります。
Score : 400 points
Problem Statement
We have a horizontal cylinder. Given Q queries, process them in the given order.
Each query is of one of the following two types.
1 x c: Insert c balls, with a number x written on each of them, to the right end of the cylinder.2 c: Take out the c leftmost balls contained in the cylinder and print the sum of the numbers written on the balls that have been taken out.
We assume that the balls do never change their order in the cylinder.
Constraints
- 1 \leq Q \leq 2\times 10^5
- 0 \leq x \leq 10^9
- 1 \leq c \leq 10^9
- Whenever a query of type
2 cis given, there are c or more balls in the cylinder. - All values in input are integers.
Input
Input is given from Standard Input in the following format:
Q
{\rm query}_1
\vdots
{\rm query}_Q
The i-th query {\rm query}_i is in one of the following two formats.
1 x c
2 c
Output
Print the response to the queries of type 2 c in the given order, with newlines in between.
Sample Input 1
4 1 2 3 2 2 1 3 4 2 3
Sample Output 1
4 8
- For the 1-st query, insert 3 balls, with a number 2 written on each of them, to the right end of the cylinder.
The cylinder has now balls with numbers (2,2,2) written on them, from left to right. - For the 2-nd query, take out the 2 leftmost balls contained in the cylinder.
The numbers written on the balls taken out are 2,2, for a sum of 4, which should be printed. The cylinder has now a ball with a number (2) written on it, from left to right. - For the 3-rd query, insert 4 balls, with a number 3 written on each of them, to the right end of the cylinder.
The cylinder has now balls with numbers (2,3,3,3,3) written on them, from left to right. - For the 4-th query, take out the 3 leftmost balls contained in the cylinder.
The numbers written on the balls taken out are 2,3,3, for a sum of 8, which should be printed. The cylinder has now balls with numbers (3,3) written on them, from left to right.
Sample Input 2
2 1 1000000000 1000000000 2 1000000000
Sample Output 2
1000000000000000000
Sample Input 3
5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Sample Output 3
There may be nothing you should print.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
0 と 1 からなる長さ N の文字列 S が与えられます。
S は長さ N の数列 A=(A _ 1,A _ 2,\ldots,A _ N) の情報を表しており、S の i 文字目 (1\leq i\leq N) が 0 のとき A _ i=0 、1 のとき A _ i=1です。
\[\sum _ {1\leq i\leq j\leq N}(\cdots((A _ i\barwedge A _ {i+1})\barwedge A _ {i+2})\barwedge\cdots\barwedge A _ j)\]
を求めてください。
より厳密には、次のように定められる f(i,j)\ (1\leq i\leq j\leq N) に対して \displaystyle\sum _ {i=1} ^ {N}\sum _ {j=i} ^ Nf(i,j) を求めてください。
\[f(i,j)=\left\{\begin{matrix} A _ i&(i=j)\\ f(i,j-1)\barwedge A _ j\quad&(i\lt j) \end{matrix}\right.\]
ただし、否定論理積 \barwedge は次を満たす二項演算子です。
\[0\barwedge0=1,0\barwedge1=1,1\barwedge0=1,1\barwedge1=0\]
制約
- 1\leq N\leq10^6
- S は
0と1からなる長さ N の文字列 - 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを 1 行で出力せよ。
入力例 1
5 00110
出力例 1
9
1\leq i\leq j\leq N を満たすすべての組 (i,j) に対して、f(i,j) の値は以下のようになります。
- f(1,1)=0=0
- f(1,2)=0\barwedge0=1
- f(1,3)=(0\barwedge0)\barwedge1=0
- f(1,4)=((0\barwedge0)\barwedge1)\barwedge1=1
- f(1,5)=(((0\barwedge0)\barwedge1)\barwedge1)\barwedge0=1
- f(2,2)=0=0
- f(2,3)=0\barwedge1=1
- f(2,4)=(0\barwedge1)\barwedge1=0
- f(2,5)=((0\barwedge1)\barwedge1)\barwedge0=1
- f(3,3)=1=1
- f(3,4)=1\barwedge1=0
- f(3,5)=(1\barwedge1)\barwedge0=1
- f(4,4)=1=1
- f(4,5)=1\barwedge0=1
- f(5,5)=0=0
これらの総和は 0+1+0+1+1+0+1+0+1+1+0+1+1+1+0=9 なので、9 を出力してください。
\barwedge は結合法則を満たさないことに注意してください。 例えば、(1\barwedge1)\barwedge0=0\barwedge0=1\neq0=1\barwedge1=1\barwedge(1\barwedge0) です。
入力例 2
30 101010000100101011010011000010
出力例 2
326
Score : 450 points
Problem Statement
You are given a string S of length N consisting of 0 and 1.
It describes a length-N sequence A=(A _ 1,A _ 2,\ldots,A _ N). If the i-th character of S (1\leq i\leq N) is 0, then A _ i=0; if it is 1, then A _ i=1.
Find the following:
\[\sum _ {1\leq i\leq j\leq N}(\cdots((A _ i\barwedge A _ {i+1})\barwedge A _ {i+2})\barwedge\cdots\barwedge A _ j)\]
More formally, find \displaystyle\sum _ {i=1} ^ {N}\sum _ {j=i} ^ Nf(i,j) for f(i,j)\ (1\leq i\leq j\leq N) defined as follows:
\[f(i,j)=\left\{\begin{matrix} A _ i&(i=j)\\ f(i,j-1)\barwedge A _ j\quad&(i\lt j) \end{matrix}\right.\]
Here, \barwedge, NAND, is a binary operator satisfying the following:
\[0\barwedge0=1,0\barwedge1=1,1\barwedge0=1,1\barwedge1=0.\]
Constraints
- 1\leq N\leq10^6
- S is a string of length N consisting of
0and1. - All input values are integers.
Input
The input is given from Standard Input in the following format:
N S
Output
Print the answer in a single line.
Sample Input 1
5 00110
Sample Output 1
9
Here are the values of f(i,j) for the pairs (i,j) such that 1\leq i\leq j\leq N:
- f(1,1)=0=0
- f(1,2)=0\barwedge0=1
- f(1,3)=(0\barwedge0)\barwedge1=0
- f(1,4)=((0\barwedge0)\barwedge1)\barwedge1=1
- f(1,5)=(((0\barwedge0)\barwedge1)\barwedge1)\barwedge0=1
- f(2,2)=0=0
- f(2,3)=0\barwedge1=1
- f(2,4)=(0\barwedge1)\barwedge1=0
- f(2,5)=((0\barwedge1)\barwedge1)\barwedge0=1
- f(3,3)=1=1
- f(3,4)=1\barwedge1=0
- f(3,5)=(1\barwedge1)\barwedge0=1
- f(4,4)=1=1
- f(4,5)=1\barwedge0=1
- f(5,5)=0=0
Their sum is 0+1+0+1+1+0+1+0+1+1+0+1+1+1+0=9, so print 9.
Note that \barwedge does not satisfy the associative property. For instance, (1\barwedge1)\barwedge0=0\barwedge0=1\neq0=1\barwedge1=1\barwedge(1\barwedge0).
Sample Input 2
30 101010000100101011010011000010
Sample Output 2
326
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.