Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
果物屋さんでりんごが売られています。
あなたは次の操作を好きな順で好きなだけ繰り返すことができます。
- X 円を払ってりんごを 1 個手に入れる。
- Y 円を払ってりんごを 3 個手に入れる。
りんごをちょうど N 個手に入れるには最低何円必要ですか?
制約
- 1 \leq X \leq Y \leq 100
- 1 \leq N \leq 100
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
X Y N
出力
答えを整数として出力せよ。
入力例 1
10 25 10
出力例 1
85
25 円払って 3 個のりんごを手に入れる操作を 3 回繰り返した後、10 円払って 1 個のりんごを手に入れると丁度 10 個のりんごを手に入れられます。このときあなたは 85 円を消費します。
これより少ない金額でちょうど 10 個のりんごを手に入れることはできないので、答えは 85 円になります。
入力例 2
10 40 10
出力例 2
100
10 円払って 1 個のりんごを手に入れる操作を 10 回繰り返すのが最適です。
入力例 3
100 100 2
出力例 3
200
100 円を払って 1 個のりんごを手に入れる操作を 2 回繰り返す以外に ちょうど 2 個のりんごを手に入れる方法はありません。
入力例 4
100 100 100
出力例 4
3400
Score : 100 points
Problem Statement
A fruit store sells apples.
You may perform the following operations as many times as you want in any order:
- Buy one apple for X yen (the currency in Japan).
- Buy three apples for Y yen.
How much yen do you need to pay to obtain exactly N apples?
Constraints
- 1 \leq X \leq Y \leq 100
- 1 \leq N \leq 100
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
X Y N
Output
Print the answer as an integer.
Sample Input 1
10 25 10
Sample Output 1
85
Buy three apples for 25 yen three times and one apple for 10 yen, and you will obtain exactly 10 apples for a total of 85 yen.
You cannot obtain exactly 10 apples for a lower cost, so the answer is 85 yen.
Sample Input 2
10 40 10
Sample Output 2
100
It is optimal to buy an apple for 10 yen 10 times.
Sample Input 3
100 100 2
Sample Output 3
200
The only way to obtain exactly 2 apples is to buy an apple for 100 yen twice.
Sample Input 4
100 100 100
Sample Output 4
3400
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
0 以上 255 以下の整数 A,B が与えられます。 A \text{ xor }C=B となる 0 以上の整数 C を求めてください。
なお、そのような C はただ 1 つ存在し、0 以上 255 以下であることが証明されます。
\text{ xor } とは
整数 a, b のビットごとの排他的論理和 a \text{ xor } b は、以下のように定義されます。
- a \text{ xor } b を二進表記した際の 2^k (k \geq 0) の位の数は、a, b を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
制約
- 0\leq A,B \leq 255
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
A B
出力
答えを出力せよ。
入力例 1
3 6
出力例 1
5
3 は 二進表記で 11、5 は二進表記で 101 なので、これらの \text{xor} は二進表記で 110 であり、十進表記で 6 です。
このように、3 \text{ xor } 5 = 6 となるので、答えは 5 です。
入力例 2
10 12
出力例 2
6

Score : 100 points
Problem Statement
You are given integers A and B between 0 and 255 (inclusive). Find a non-negative integer C such that A \text{ xor }C=B.
It can be proved that there uniquely exists such C, and it will be between 0 and 255 (inclusive).
What is bitwise \mathrm{XOR}?
The bitwise \mathrm{XOR} of integers A and B, A\ \mathrm{XOR}\ B, is defined as follows:
- When A\ \mathrm{XOR}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if exactly one of A and B is 1, and 0 otherwise.
Constraints
- 0\leq A,B \leq 255
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
A B
Output
Print the answer.
Sample Input 1
3 6
Sample Output 1
5
When written in binary, 3 will be 11, and 5 will be 101. Thus, their \text{xor} will be 110 in binary, or 6 in decimal.
In short, 3 \text{ xor } 5 = 6, so the answer is 5.
Sample Input 2
10 12
Sample Output 2
6

Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
文字列 S が与えられます。
S の連続する部分文字列のうち、回文であるものの長さの最大値を求めてください。
ただし、S の連続する部分文字列であって回文であるものは常に存在します。
制約
- S は長さ 2 以上 100 以下の英大文字からなる文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
TOYOTA
出力例 1
5
TOYOTA の連続する部分文字列 TOYOT は長さ 5 の回文です。
TOYOTA の唯一の長さ 6 の連続する部分文字列 TOYOTA は回文でないので、5 を出力します。
入力例 2
ABCDEFG
出力例 2
1
すべての長さ 1 の連続する部分文字列は回文です。
入力例 3
AAAAAAAAAA
出力例 3
10
Score : 200 points
Problem Statement
You are given a string S.
Find the maximum length of a contiguous substring of S that is a palindrome.
Note that there is always a contiguous substring of S that is a palindrome.
Constraints
- S is a string of length between 2 and 100, inclusive, consisting of uppercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
TOYOTA
Sample Output 1
5
TOYOT, a contiguous substring of TOYOTA, is a palindrome of length 5.
TOYOTA, the only length-6 contiguous substring of TOYOTA, is not a palindrome, so print 5.
Sample Input 2
ABCDEFG
Sample Output 2
1
Every contiguous substring of length 1 is a palindrome.
Sample Input 3
AAAAAAAAAA
Sample Output 3
10
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
1 から N までの番号が付けられた N 人の人がとあるコンテストに参加し、人 i\ (1\leq i\leq N) の 得点 は P_i でした。
このコンテストでは、以下の手順によって N 人それぞれの 順位 が定まります。
- 変数 r を用意し、r=1 と初期化する。最初、N 人の順位はすべて未確定である。
- N 人全員の順位が確定するまで以下の操作を繰り返す。
- 順位が未確定である人の中での得点の最大値を x とし、得点が x である人の数を k とおく。得点が x である k 人すべての順位を r 位と確定させたのち、r に k を足す。
N 人それぞれの順位を出力してください。
制約
- 1\leq N \leq 100
- 1\leq P_i\leq 100
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N P_1 P_2 \dots P_N
出力
N 行出力せよ。 i\ (1\leq i \leq N) 行目には、人 i の順位を整数として出力せよ。
入力例 1
4 3 12 9 9
出力例 1
4 1 2 2
以下のようにして N\ (=4) 人それぞれの順位が定まります。
- 変数 r を用意し、r=1 と初期化する。最初、4 人の順位はすべて未確定である。
- 現時点で順位が未確定なのは人 1,2,3,4 であり、その中での得点の最大値は P_2\ (=12) である。よって、人 2 の順位を r\ (=1) 位と確定させたのち、r に 1 を足して r=2 とする。
- 現時点で順位が未確定なのは人 1,3,4 であり、その中での得点の最大値は P_3=P_4\ (=9) である。よって、人 3,4 の順位を r\ (=2) 位と確定させたのち、r に 2 を足して r=4 とする。
- 現時点で順位が未確定なのは人 1 であり、その中での得点の最大値は P_1\ (=3) である。よって、人 1 の順位を r\ (=4) 位と確定させたのち、r に 1 を足して r=5 とする。
- 4 人全員の順位が確定したため、手順を終了する。
入力例 2
3 3 9 6
出力例 2
3 1 2
入力例 3
4 100 100 100 100
出力例 3
1 1 1 1
入力例 4
8 87 87 87 88 41 38 41 38
出力例 4
2 2 2 1 5 7 5 7
Score : 200 points
Problem Statement
N people labeled from 1 to N participated in a certain contest. The score of person i (1 \leq i \leq N) was P_i.
In this contest, the rank of each of the N people is determined by the following procedure:
- Prepare a variable r, and initialize r = 1. Initially, the ranks of the N people are all undetermined.
- Repeat the following operation until the ranks of all N people are determined:
- Let x be the maximum score among the people whose ranks are currently undetermined, and let k be the number of people whose score is x. Determine the rank of those k people with score x to be r, and then add k to r.
Print the rank of each of the N people.
Constraints
- 1\leq N \leq 100
- 1\leq P_i \leq 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N P_1 P_2 \dots P_N
Output
Print N lines. The i-th line (1 \leq i \leq N) should contain the rank of person i as an integer.
Sample Input 1
4 3 12 9 9
Sample Output 1
4 1 2 2
The ranks of the N\ (=4) people are determined as follows:
- Prepare a variable r and initialize r=1. At first, the ranks of all 4 people are undetermined.
- Currently, persons 1, 2, 3, 4 have undetermined ranks. The maximum score among them is P_2\ (=12). Therefore, determine the rank of person 2 to be r\ (=1), and then add 1 to r, making r=2.
- Currently, persons 1, 3, 4 have undetermined ranks. The maximum score among them is P_3=P_4\ (=9). Therefore, determine the ranks of persons 3 and 4 to be r\ (=2), and then add 2 to r, making r=4.
- Currently, person 1 has an undetermined rank. The maximum score among them is P_1\ (=3). Therefore, determine the rank of person 1 to be r\ (=4), and then add 1 to r, making r=5.
- The ranks of all 4 people are now determined, so the process ends.
Sample Input 2
3 3 9 6
Sample Output 2
3 1 2
Sample Input 3
4 100 100 100 100
Sample Output 3
1 1 1 1
Sample Input 4
8 87 87 87 88 41 38 41 38
Sample Output 4
2 2 2 1 5 7 5 7
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
整数 N が与えられます。N の各桁の数字を取り出して並べ(並べる順序は好きに変えてよい)、2 つの正整数に分離することを考えましょう。
例えば、123 という整数に対しては以下の 6 通りの分離の仕方が考えられます。
- 12 と 3
- 21 と 3
- 13 と 2
- 31 と 2
- 23 と 1
- 32 と 1
なお、ここで分離されたあとの 2 整数に leading zero が含まれていてはなりません。例えば、101 という整数を 1 と 01 の 2 つに分離することはできません。また上述の「正整数に分離する」という条件より、101 を 11 と 0 の 2 つに分離することもできません。
適切に N を分離したとき、分離後の 2 数の積の最大値はいくらになりますか?
制約
- N は 1 以上 10^9 以下の整数
- N には 0 でない桁が 2 つ以上含まれる
入力
入力は以下の形式で標準入力から与えられる。
N
出力
分離後の 2 数の積の最大値を出力せよ。
入力例 1
123
出力例 1
63
問題文中にある通り、以下の 6 通りの分離の仕方が考えられます。
- 12 と 3
- 21 と 3
- 13 と 2
- 31 と 2
- 23 と 1
- 32 と 1
積はそれぞれ 36, 63, 26, 62, 23, 32 であり、この中の最大値は 63 です。
入力例 2
1010
出力例 2
100
考えられる分離の仕方は以下の 2 通りです。
- 100 と 1
- 10 と 10
いずれの場合にも積は 100 となります。
入力例 3
998244353
出力例 3
939337176
Score : 300 points
Problem Statement
You are given an integer N. Consider permuting the digits in N and separate them into two positive integers.
For example, for the integer 123, there are six ways to separate it, as follows:
- 12 and 3,
- 21 and 3,
- 13 and 2,
- 31 and 2,
- 23 and 1,
- 32 and 1.
Here, the two integers after separation must not contain leading zeros. For example, it is not allowed to separate the integer 101 into 1 and 01. Additionally, since the resulting integers must be positive, it is not allowed to separate 101 into 11 and 0, either.
What is the maximum possible product of the two resulting integers, obtained by the optimal separation?
Constraints
- N is an integer between 1 and 10^9 (inclusive).
- N contains two or more digits that are not 0.
Input
Input is given from Standard Input in the following format:
N
Output
Print the maximum possible product of the two integers after separation.
Sample Input 1
123
Sample Output 1
63
As described in Problem Statement, there are six ways to separate it:
- 12 and 3,
- 21 and 3,
- 13 and 2,
- 31 and 2,
- 23 and 1,
- 32 and 1.
The products of these pairs, in this order, are 36, 63, 26, 62, 23, 32, with 63 being the maximum.
Sample Input 2
1010
Sample Output 2
100
There are two ways to separate it:
- 100 and 1,
- 10 and 10.
In either case, the product is 100.
Sample Input 3
998244353
Sample Output 3
939337176
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
N 棟のビルが等間隔に一列に並んでいます。手前から i 番目のビルの高さは H_i です。
あなたは次の条件をともに満たすようにいくつかのビルを選んで電飾で飾ろうとしています。
- 選んだビルたちは高さが等しい
- 選んだビルたちは等間隔に並んでいる
最大でいくつのビルを選ぶことができますか? なお、ちょうど 1 つのビルを選んだときは条件を満たすとみなします。
制約
- 1 \leq N \leq 3000
- 1 \leq H_i \leq 3000
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N H_1 \ldots H_N
出力
答えを出力せよ。
入力例 1
8 5 7 5 7 7 5 7 7
出力例 1
3
手前から 2,5,8 番目のビルを選ぶと条件を満たします。
入力例 2
10 100 200 300 400 500 600 700 800 900 1000
出力例 2
1
1つのビルを選んだときは条件を満たすとみなします。
入力例 3
32 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5
出力例 3
3
Score : 350 points
Problem Statement
There are N buildings arranged in a line at equal intervals. The height of the i-th building from the front is H_i.
You want to decorate some of these buildings with illuminations so that both of the following conditions are satisfied:
- The chosen buildings all have the same height.
- The chosen buildings are arranged at equal intervals.
What is the maximum number of buildings you can choose? If you choose exactly one building, it is considered to satisfy the conditions.
Constraints
- 1 \leq N \leq 3000
- 1 \leq H_i \leq 3000
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N H_1 \ldots H_N
Output
Print the answer.
Sample Input 1
8 5 7 5 7 7 5 7 7
Sample Output 1
3
Choosing the 2nd, 5th, and 8th buildings from the front satisfies the conditions.
Sample Input 2
10 100 200 300 400 500 600 700 800 900 1000
Sample Output 2
1
Choosing just one building is considered to satisfy the conditions.
Sample Input 3
32 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5
Sample Output 3
3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
正整数 x,y に対して f(x,y) を以下で定義します。
- 十進表記の x,y をそれぞれ文字列として解釈しこの順に連結して得られる文字列を z とする。z を十進表記の整数として解釈したときの値を f(x,y) とする。
例えば f(3,14)=314, f(100,1)=1001 です。
長さ N の正整数列 A=(A_1,\ldots,A_N) が与えられます。次の式の値を 998244353 で割ったあまりを求めてください。
制約
- 2\leq N\leq 2\times 10^5
- 1\leq A_i \leq 10^9
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 \ldots A_N
出力
答えを出力せよ。
入力例 1
3 3 14 15
出力例 1
2044
- f(A_1,A_2)=314
- f(A_1,A_3)=315
- f(A_2,A_3)=1415
なので、答えは f(A_1,A_2)+f(A_1,A_3)+f(A_2,A_3) = 2044 です。
入力例 2
5 1001 5 1000000 1000000000 100000
出力例 2
625549048
式の値を 998244353 で割ったあまりを求めることに注意してください。
Score: 400 points
Problem Statement
For positive integers x and y, define f(x, y) as follows:
- Interpret the decimal representations of x and y as strings and concatenate them in this order to obtain a string z. The value of f(x, y) is the value of z when interpreted as a decimal integer.
For example, f(3, 14) = 314 and f(100, 1) = 1001.
You are given a sequence of positive integers A = (A_1, \ldots, A_N) of length N. Find the value of the following expression modulo 998244353:
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 \ldots A_N
Output
Print the answer.
Sample Input 1
3 3 14 15
Sample Output 1
2044
- f(A_1, A_2) = 314
- f(A_1, A_3) = 315
- f(A_2, A_3) = 1415
Thus, the answer is f(A_1, A_2) + f(A_1, A_3) + f(A_2, A_3) = 2044.
Sample Input 2
5 1001 5 1000000 1000000000 100000
Sample Output 2
625549048
Be sure to calculate the value modulo 998244353.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
長さ N の整数列 A=(A_1, A_2, \dots, A_N) が与えられます。
Q 個のクエリが与えられるので、順に処理してください。 各クエリは以下のいずれかの形式です。
1 x y: A_x の値を y に変更する。2 l r: \displaystyle \sum_{1\leq i\leq N} \max(l, \min(r, A_i)) の値を求める。
制約
- 1\leq N \leq 5\times 10^5
- 1\leq Q \leq 2\times 10^5
- 0\leq A_i \leq 5\times 10^5
- 1 種類目のクエリについて、
- 1\leq x\leq N
- 0\leq y \leq 5\times 10^5
- 2 種類目のクエリについて、
- 0\leq l,r \leq 5\times 10^5
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q
A_1 A_2 \dots A_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
ここで、\text{query}_i\ (1\leq i\leq Q) は i 番目のクエリを表し、以下のいずれかの形式で与えられる。
1 x y
2 l r
出力
2 種類目のクエリの個数を K として、K 行出力せよ。 i 行目 (1\leq i \leq K) には、2 種類目のクエリのうち i 個目のものに対する答えを出力せよ。
入力例 1
3 4 4 3 2 1 1 7 2 3 5 1 2 0 2 4 2
出力例 1
11 12
はじめ、A=(4,3,2) です。
- 1 番目のクエリ:A_1 の値を 7 に変更します。A=(7,3,2) となります。
- 2 番目のクエリ:\max(3, \min(5, 7))+\max(3, \min(5, 3))+\max(3, \min(5, 2))=5+3+3=11 を出力します。
- 3 番目のクエリ:A_2 の値を 0 に変更します。A=(7,0,2) となります。
- 4 番目のクエリ:\max(4, \min(2, 7))+\max(4, \min(2, 0))+\max(4, \min(2, 2))=4+4+4=12 を出力します。
入力例 2
8 10 320 578 244 604 145 839 156 857 2 400 556 1 5 168 2 254 62 2 145 301 1 1 23 1 3 0 2 413 758 2 297 613 1 8 451 2 598 692
出力例 2
3824 2032 2073 4350 3596 4884
Score : 450 points
Problem Statement
You are given an integer sequence A=(A_1, A_2, \dots, A_N) of length N.
You are given Q queries, which you should process in order. Each query is in one of the following formats:
1 x y: Change the value of A_x to y.2 l r: Find the value of \displaystyle \sum_{1\leq i\leq N} \max(l, \min(r, A_i)).
Constraints
- 1\leq N \leq 5\times 10^5
- 1\leq Q \leq 2\times 10^5
- 0\leq A_i \leq 5\times 10^5
- For queries of the first type,
- 1\leq x\leq N
- 0\leq y \leq 5\times 10^5
- For queries of the second type,
- 0\leq l,r \leq 5\times 10^5
- All inputs are integers.
Input
The input is given from Standard Input in the following format:
N Q
A_1 A_2 \dots A_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
Here, \text{query}_i\ (1\leq i\leq Q) represents the i-th query and is given in one of the following formats:
1 x y
2 l r
Output
Let K be the number of queries of the second type. Output K lines. The i-th line (1\leq i \leq K) should contain the answer to the i-th query of the second type.
Sample Input 1
3 4 4 3 2 1 1 7 2 3 5 1 2 0 2 4 2
Sample Output 1
11 12
Initially, A=(4,3,2).
- First query: Change the value of A_1 to 7. A becomes (7,3,2).
- Second query: Output \max(3, \min(5, 7))+\max(3, \min(5, 3))+\max(3, \min(5, 2))=5+3+3=11.
- Third query: Change the value of A_2 to 0. A becomes (7,0,2).
- Fourth query: Output \max(4, \min(2, 7))+\max(4, \min(2, 0))+\max(4, \min(2, 2))=4+4+4=12.
Sample Input 2
8 10 320 578 244 604 145 839 156 857 2 400 556 1 5 168 2 254 62 2 145 301 1 1 23 1 3 0 2 413 758 2 297 613 1 8 451 2 598 692
Sample Output 2
3824 2032 2073 4350 3596 4884
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.