実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
りんご市場に N 人の売り手と M 人の買い手がいます。
i 番目の売り手は、A_i 円以上でならりんごを売ってもよいと考えています。
i 番目の買い手は、B_i 円以下でならりんごを買ってもよいと考えています。
次の条件を満たすような最小の整数 X を求めてください。
条件:りんごを X 円で売ってもよいと考える売り手の人数が、りんごを X 円で買ってもよいと考える買い手の人数以上である。
制約
- 1 \leq N,M \leq 2\times 10^5
- 1\leq A_i,B_i \leq 10^9
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 \ldots A_N B_1 \ldots B_M
出力
答えを出力せよ。
入力例 1
3 4 110 90 120 100 80 120 10000
出力例 1
110
りんごを 110 円で売ってもよいと考える売り手は 1,2 番目の 2 人であり、りんごを 110 円で買ってもよいと考える買い手は 3,4 番目の 2 人であるため、110 は条件を満たします。
110 未満の整数が条件を満たすことはないため、これが答えです。
入力例 2
5 2 100000 100000 100000 100000 100000 100 200
出力例 2
201
入力例 3
3 2 100 100 100 80 120
出力例 3
100
Score : 300 points
Problem Statement
There are N sellers and M buyers in an apple market.
The i-th seller may sell an apple for A_i yen or more (yen is the currency in Japan).
The i-th buyer may buy an apple for B_i yen or less.
Find the minimum integer X that satisfies the following condition.
Condition: The number of people who may sell an apple for X yen is greater than or equal to the number of people who may buy an apple for X yen.
Constraints
- 1 \leq N,M \leq 2\times 10^5
- 1\leq A_i,B_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 \ldots A_N B_1 \ldots B_M
Output
Print the answer.
Sample Input 1
3 4 110 90 120 100 80 120 10000
Sample Output 1
110
Two sellers, the 1-st and 2-nd, may sell an apple for 110 yen; two buyers, the 3-rd and 4-th, may buy an apple for 110 yen. Thus, 110 satisfies the condition.
Since an integer less than 110 does not satisfy the condition, this is the answer.
Sample Input 2
5 2 100000 100000 100000 100000 100000 100 200
Sample Output 2
201
Sample Input 3
3 2 100 100 100 80 120
Sample Output 3
100
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
英小文字、,、" からなる長さ N の文字列 S が与えられます。S に含まれる " の個数は偶数であることが保証されています。
S に含まれる " の個数を 2K 個とすると、各 i=1,2,\ldots,K について 2i-1 番目の " から 2i 番目の " までの文字のことを 括られた文字 と呼びます。
あなたの仕事は、 S に含まれる , のうち、括られた文字 でないもの を . で置き換えて得られる文字列を答えることです。
制約
- N は 1 以上 2\times 10^5 以下の整数
- S は英小文字、
,、"からなる長さ N の文字列 - S に含まれる
"の個数は偶数
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを出力せよ。
入力例 1
8 "a,b"c,d
出力例 1
"a,b"c.d
S のうち "a,b" が括られた文字であり、c,d は括られた文字ではありません。
S に含まれる , のうち、括られた文字でないのは S の左から 7 番目の文字なので、7 番目の文字を . で置き換えたものが答えとなります。
入力例 2
5 ,,,,,
出力例 2
.....
入力例 3
20 a,"t,"c,"o,"d,"e,"r,
出力例 3
a."t,"c."o,"d."e,"r.
Score : 300 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters, ,, and ". It is guaranteed that S contains an even number of ".
Let 2K be the number of " in S. For each i=1,2,\ldots,K, the characters from the (2i-1)-th " through the (2i)-th " are said to be enclosed.
Your task is to replace each , in S that is not an enclosed character with . and print the resulting string.
Constraints
- N is an integer between 1 and 2\times 10^5, inclusive.
- S is a string of length N consisting of lowercase English letters,
,, and". - S contains an even number of
".
Input
The input is given from Standard Input in the following format:
N S
Output
Print the answer.
Sample Input 1
8 "a,b"c,d
Sample Output 1
"a,b"c.d
In S, "a,b" are enclosed characters, and c,d are not.
The , in S that is not an enclosed character is the seventh character from the left in S, so replace that character with . to get the answer.
Sample Input 2
5 ,,,,,
Sample Output 2
.....
Sample Input 3
20 a,"t,"c,"o,"d,"e,"r,
Sample Output 3
a."t,"c."o,"d."e,"r.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
正整数 N,M が与えられます。
次の条件をともにみたす最小の正整数 X を求めてください。
ただし、そのようなものが存在しない場合は -1 を出力してください。
- X は 1 以上 N 以下の整数 a,b の積として表される。ここで、a と b は同じ整数でも良い。
- X は M 以上である。
制約
- 1\leq N\leq 10^{12}
- 1\leq M\leq 10^{12}
- N,M は整数
入力
入力は以下の形式で標準入力から与えられる。
N M
出力
問題文の条件をともにみたす最小の正整数 X を出力せよ。そのようなものが存在しない場合は -1 を出力せよ。
入力例 1
5 7
出力例 1
8
まず、7 を 1 以上 5 以下の整数 2 つの積として表すことはできません。
次に、8 は 8=2\times 4 などとして、1 以上 5 以下の整数 2 つの積として表すことができます。
よって、8 を出力します。
入力例 2
2 5
出力例 2
-1
1\times 1=1、1\times 2=2、2\times 1=2、2\times 2=4 より、
1 以上 2 以下の整数 2 つの積として表す事ができるのは 1,2,4 のみであるため、
5 以上の数はそのような整数 2 つの積として表すことができません。
よって、-1 を出力します。
入力例 3
100000 10000000000
出力例 3
10000000000
a=b=100000 (=10^5)とした時、a,b の積は 10000000000 (=10^{10}) となり、これが答えとなります。
答えが 32 bit 整数型に収まらない場合があることに注意してください。
Score : 400 points
Problem Statement
You are given positive integers N and M.
Find the smallest positive integer X that satisfies both of the conditions below, or print -1 if there is no such integer.
- X can be represented as the product of two integers a and b between 1 and N, inclusive. Here, a and b may be the same.
- X is at least M.
Constraints
- 1\leq N\leq 10^{12}
- 1\leq M\leq 10^{12}
- N and M are integers.
Input
The input is given from Standard Input in the following format:
N M
Output
Print the smallest positive integer X that satisfies both of the conditions, or -1 if there is no such integer.
Sample Input 1
5 7
Sample Output 1
8
First, 7 cannot be represented as the product of two integers between 1 and 5.
Second, 8 can be represented as the product of two integers between 1 and 5, such as 8=2\times 4.
Thus, you should print 8.
Sample Input 2
2 5
Sample Output 2
-1
Since 1\times 1=1, 1\times 2=2, 2\times 1=2, and 2\times 2=4, only 1, 2, and 4 can be represented as the product of two integers between 1 and 2,
so no number greater than or equal to 5 can be represented as the product of two such integers.
Thus, you should print -1.
Sample Input 3
100000 10000000000
Sample Output 3
10000000000
For a=b=100000 (=10^5), the product of a and b is 10000000000 (=10^{10}), which is the answer.
Note that the answer may not fit into a 32-bit integer type.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
高橋君が住む世界の一週間は N 日からなります。
一週間は曜日 1,2,\dots,N と進んでいき、曜日 N が終わると次の週の曜日 1 が始まります。
ABC 国の国王である高橋君は、各曜日に「平日」「休日」のどちらかを割り当てます。この割り当ては毎週同じでなければなりません。また、少なくとも 1 つの曜日を「休日」に割り当てなければなりません。
この条件の下で、曜日 i の生産量は長さ N の数列 A を用いて以下のように定義されます。
- 曜日 i が「休日」である場合は 0
- 曜日 i が「平日」のとき、直前の休日が x 日前、直後の休日が y 日後である場合は A_{\min(x,y)}
- 割り当ては毎週繰り返されるため、 直前 / 直後 の「休日」が当日とは別の週に属する可能性があることに注意してください。詳しくはサンプルを参照してください。
上手く割り当てを決めたときの一週間当たりの生産量の最大値を答えてください。
但し、一週間当たりの生産量とは曜日 1,2,\dots,N の生産量の総和を指します。
制約
- 入力はすべて整数
- 1 \le N \le 5000
- 1 \le A_i \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを整数として出力せよ。
入力例 1
7 10 10 1 1 1 1 1
出力例 1
50
例えば曜日 2,4 を「休日」、残りを「平日」に割り当てることで、以下のように一週間当たりの生産量 50 を達成できます。
- 曜日 1 ... x=4,y=1 なので、この曜日の生産量は A_1 = 10 である。
- 曜日 2 ... 「休日」であるので、この曜日の生産量は 0 である。
- 曜日 3 ... x=1,y=1 なので、この曜日の生産量は A_1 = 10 である。
- 曜日 4 ... 「休日」であるので、この曜日の生産量は 0 である。
- 曜日 5 ... x=1,y=4 なので、この曜日の生産量は A_1 = 10 である。
- 曜日 6 ... x=2,y=3 なので、この曜日の生産量は A_2 = 10 である。
- 曜日 7 ... x=3,y=2 なので、この曜日の生産量は A_2 = 10 である。
一週間当たりの生産量を 51 以上にすることはできません。
入力例 2
10 200000000 500000000 1000000000 800000000 100000000 80000000 600000 900000000 1 20
出力例 2
5100000000
入力例 3
20 38 7719 21238 2437 8855 11797 8365 32285 10450 30612 5853 28100 1142 281 20537 15921 8945 26285 2997 14680
出力例 3
236980
Score : 500 points
Problem Statement
In the world where Takahashi lives, a week has N days.
Takahashi, the king of the Kingdom of AtCoder, assigns "weekday" or "holiday" to each day of week. The assignments should be the same for all weeks. At least one day of week should be assigned "holiday".
Under such conditions, the productivity of the i-th day of week is defined by a sequence A of length N as follows:
- if the i-th day of week is "holiday", its productivity is 0;
- if the i-th day of week is "weekday", its productivity is A_{\min(x,y)}, if the last holiday is x days before and the next one is y days after.
- Note that the last/next holiday may belong to a different week due to the periodic assignments. For details, see the Samples.
Find the maximum productivity per week when the assignments are chosen optimally.
Here, the productivity per week refers to the sum of the productivities of the 1-st, 2-nd, \dots, and N-th day of week.
Constraints
- All values in the input are integers.
- 1 \le N \le 5000
- 1 \le A_i \le 10^9
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N
Output
Print the answer as an integer.
Sample Input 1
7 10 10 1 1 1 1 1
Sample Output 1
50
For example, we can assign "holiday" to the 2-nd and 4-th day of week and "weekday" to the rest to achieve a productivity of 50 per week:
- the 1-st day of week ... x=4 and y=1, so its productivity is A_1 = 10.
- the 2-nd day of week ... it is holiday, so its productivity is 0.
- the 3-st day of week ... x=1 and y=1, so its productivity is A_1 = 10.
- the 4-th day of week ... it is holiday, so its productivity is 0.
- the 5-th day of week ... x=1 and y=4, so its productivity is A_1 = 10.
- the 6-th day of week ... x=2 and y=3, so its productivity is A_2 = 10.
- the 7-th day of week ... x=3 and y=2, so its productivity is A_2 = 10.
It is impossible to make the productivity per week 51 or greater.
Sample Input 2
10 200000000 500000000 1000000000 800000000 100000000 80000000 600000 900000000 1 20
Sample Output 2
5100000000
Sample Input 3
20 38 7719 21238 2437 8855 11797 8365 32285 10450 30612 5853 28100 1142 281 20537 15921 8945 26285 2997 14680
Sample Output 3
236980
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
長さ N の 2 つの整数列 A = (A_1, A_2, \ldots, A_N) および B = (B_1, B_2 \ldots, B_N) が与えられます。
整数列 A に対して、「下記の 2 つの操作のうちどちらかを行う」ということを好きな回数( 0 回でもよい)繰り返すことができます。
- 1 \leq i \leq N を満たす整数 i を選び、A_i の値を 1 増やすか 1 減らす。この操作には X 円の費用がかかる。
- 1 \leq i \leq N-1 を満たす整数 i を選び、A_i の値と A_{i+1} の値を入れ替える。この操作には Y 円の費用がかかる。
上記の繰り返しによって整数列 A を整数列 B に一致させるためにかかる合計費用としてあり得る最小値を出力してください。
制約
- 2 \leq N \leq 18
- 1 \leq X \leq 10^8
- 1 \leq Y \leq 10^{16}
- 1 \leq A_i, B_i \leq 10^8
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X Y A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
出力
A を B に一致させるためにかかる合計費用としてあり得る最小値を求めよ。
入力例 1
4 3 5 4 2 5 2 6 4 2 1
出力例 1
16
はじめ、A = (4, 2, 5, 2) です。
下記の通りに操作を行うと、A を B に一致させることができます。
- X = 3 円払い、A_3 の値を 1 増やす。その結果 A = (4, 2, 6, 2) となる。
- Y = 5 円払い、A_2 の値と A_3 の値を入れ替える。その結果 A = (4, 6, 2, 2) となる。
- Y = 5 円払い、A_1 の値と A_2 の値を入れ替える。その結果 A = (6, 4, 2, 2) となる。
- X = 3 円払い、A_4 の値を 1 減らす。その結果 A = (6, 4, 2, 1) = B となる。
上記の操作にかかる費用の合計は 3+5+5+3 = 16 円であり、これが最小となります。
入力例 2
5 12345 6789 1 2 3 4 5 1 2 3 4 5
出力例 2
0
A と B は初めから一致しているため、一度も操作を行う必要がありません。
入力例 3
18 20719114 5117250357733867 10511029 36397527 63027379 44706927 47672230 79861204 57882493 42931589 51053644 52300688 43971370 26515475 62139996 41282303 34022578 12523039 6696497 64922712 14720753 4621362 25269832 91410838 86751784 32741849 6602693 60719353 28911226 88280613 18745325 80675202 34289776 37849132 99280042 73760634 43897718 40659077
出力例 3
13104119429316474
入力や出力が 32 bit 整数型に収まらないことがあることに注意してください。
Score : 500 points
Problem Statement
Given are two sequences of N integers each: A = (A_1, A_2, \ldots, A_N) and B = (B_1, B_2 \ldots, B_N).
On the sequence A, you can do the two operations below any number of times (possibly zero) in any order.
- Choose an integer i satisfying 1 \leq i \leq N, and increase or decrease A_i by 1, for the cost of X yen (Japanese currency).
- Choose an integer i satisfying 1 \leq i \leq N-1, and swap the values of A_i and A_{i+1}, for the cost of Y yen.
Print the minimum total cost needed to make the sequence A equal the sequence B by repeating the above.
Constraints
- 2 \leq N \leq 18
- 1 \leq X \leq 10^8
- 1 \leq Y \leq 10^{16}
- 1 \leq A_i, B_i \leq 10^8
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X Y A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
Output
Print the minimum total cost needed to make A equal B.
Sample Input 1
4 3 5 4 2 5 2 6 4 2 1
Sample Output 1
16
Initialy, we have A = (4, 2, 5, 2).
The following sequence of operations makes A equal B.
- Pay X = 3 yen to increase the value of A_3 by 1, making A = (4, 2, 6, 2).
- Pay Y = 5 yen to swap the values of A_2 and A_3, making A = (4, 6, 2, 2).
- Pay Y = 5 yen to swap the values of A_1 and A_2, making A = (6, 4, 2, 2).
- Pay X = 3 yen to decrease the value of A_4 by 1, making A = (6, 4, 2, 1).
The total cost of these operations is 3+5+5+3 = 16 yen, which is the minimum possible.
Sample Input 2
5 12345 6789 1 2 3 4 5 1 2 3 4 5
Sample Output 2
0
A and B are equal from the beginning, so no operation is needed.
Sample Input 3
18 20719114 5117250357733867 10511029 36397527 63027379 44706927 47672230 79861204 57882493 42931589 51053644 52300688 43971370 26515475 62139996 41282303 34022578 12523039 6696497 64922712 14720753 4621362 25269832 91410838 86751784 32741849 6602693 60719353 28911226 88280613 18745325 80675202 34289776 37849132 99280042 73760634 43897718 40659077
Sample Output 3
13104119429316474
Note that values in input or output may not fit into 32-bit integer types.