Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
N 個の商品があります。高橋君と青木君がどの商品を欲しがっているかを表す長さ N の文字列 T,A が与えられます。T,A の i\ (1\leq i\leq N) 文字目をそれぞれ T_i,A_i とします。
高橋君は T_i が o のとき i 番目の商品を欲しがっており、T_i が x のとき i 番目の商品を欲しがっていません。
同様に、青木君は A_i が o のとき i 番目の商品を欲しがっており、A_i が x のとき i 番目の商品を欲しがっていません。
2 人ともが欲しがっている商品が存在するか判定してください。
制約
- 1\leq N\leq 100
- N は整数
- T,A は
oおよびxからなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N T A
出力
2 人とも欲しがっている商品が存在するならば Yes を、存在しないならば No を出力せよ。
入力例 1
4 oxoo xoox
出力例 1
Yes
3 つ目の商品は 2 人ともが欲しがっているため、Yes を出力します。
入力例 2
5 xxxxx ooooo
出力例 2
No
2 人とも欲しがっている商品は存在しないため、No を出力します。
入力例 3
10 xoooxoxxxo ooxooooxoo
出力例 3
Yes
Score : 100 points
Problem Statement
There are N items. You are given strings T and A of length N that represent which items Takahashi and Aoki want, respectively. Let T_i and A_i be the i-th (1\leq i\leq N) characters of T and A, respectively.
Takahashi wants the i-th item when T_i is o, and does not want the i-th item when T_i is x.
Similarly, Aoki wants the i-th item when A_i is o, and does not want the i-th item when A_i is x.
Determine whether there exists an item that both of them want.
Constraints
- 1\leq N\leq 100
- N is an integer.
- T and A are strings of length N consisting of
oandx.
Input
The input is given from Standard Input in the following format:
N T A
Output
If there exists an item that both of them want, output Yes; otherwise, output No.
Sample Input 1
4 oxoo xoox
Sample Output 1
Yes
The third item is wanted by both of them, so output Yes.
Sample Input 2
5 xxxxx ooooo
Sample Output 2
No
There is no item that both of them want, so output No.
Sample Input 3
10 xoooxoxxxo ooxooooxoo
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
1 から N までの番号が付けられた N 人の人がいます。 それぞれの人にはプログラミング力という整数値が定まっており、人 i のプログラミング力は P_i です。 人 1 が最強になるためには、あといくつプログラミング力を上げる必要がありますか? すなわち、すべての i \neq 1 に対して P_1 + x > P_i を満たすような最小の非負整数 x は何ですか?
制約
- 1\leq N \leq 100
- 1\leq P_i \leq 100
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N P_1 P_2 \dots P_N
出力
答えを整数として出力せよ。
入力例 1
4 5 15 2 10
出力例 1
11
人 1 が最強になるためには、プログラミング力を 16 以上にする必要があります。 よって、答えは 16-5=11 です。
入力例 2
4 15 5 2 10
出力例 2
0
人 1 は既に最強なので、これ以上プログラミング力を上げる必要はありません。
入力例 3
3 100 100 100
出力例 3
1
Score : 100 points
Problem Statement
There are N people numbered 1 through N. Each person has a integer score called programming ability; person i's programming ability is P_i points. How many more points does person 1 need, so that person 1 becomes the strongest? In other words, what is the minimum non-negative integer x such that P_1 + x > P_i for all i \neq 1?
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 the answer as an integer.
Sample Input 1
4 5 15 2 10
Sample Output 1
11
Person 1 becomes the strongest when their programming skill is 16 points or more, so the answer is 16-5=11.
Sample Input 2
4 15 5 2 10
Sample Output 2
0
Person 1 is already the strongest, so no more programming skill is needed.
Sample Input 3
3 100 100 100
Sample Output 3
1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
以下のような N 個の整数列 A_0,\ldots,A_{N-1} を求めてください。
- 各 i (0\leq i \leq N-1) について、A_i の長さは i+1 である。
-
各 i,j (0\leq i \leq N-1, 0 \leq j \leq i) について、A_i の j+1 番目の値 a_{i,j} は次のように定められる。
- j=0 または j=i の時、a_{i,j}=1
- それ以外の時、a_{i,j} = a_{i-1,j-1} + a_{i-1,j}
制約
- 1 \leq N \leq 30
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
N 行出力せよ。 i 行目には A_{i-1} の値を順に空白区切りで出力せよ。
入力例 1
3
出力例 1
1 1 1 1 2 1
入力例 2
10
出力例 2
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1
Score : 200 points
Problem Statement
Find the N integer sequences A_0,\ldots,A_{N-1} defined as follows.
- For each i (0\leq i \leq N-1), the length of A_i is i+1.
- For each i and j (0\leq i \leq N-1, 0 \leq j \leq i), the (j+1)-th term of A_i, denoted by a_{i,j}, is defined as follows.
- a_{i,j}=1, if j=0 or j=i.
- a_{i,j} = a_{i-1,j-1} + a_{i-1,j}, otherwise.
Constraints
- 1 \leq N \leq 30
- N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
Print N lines. The i-th line should contain the terms of A_{i-1} separated by spaces.
Sample Input 1
3
Sample Output 1
1 1 1 1 2 1
Sample Input 2
10
Sample Output 2
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君は長さ N の整数列 A=(A_1,A_2,\ldots,A_N) を持っています。
A は広義単調増加であることが保証されます。
高橋君はこの整数列に対して M 回操作を行います。 i 回目 (1\leq i\leq M) の操作では、次の操作を行います。
数列 A が B_i を要素として持つならば、そのような要素を 1 つ選び、削除する。 そのような要素が存在しないならば何もしない。
A は広義単調増加であるため、操作後の列は要素の選び方によらず一意であり、再び広義単調増加となることに注意せよ。
M 回の操作を行なった後の A を求めてください。
広義単調増加 とは
数列 X=(X_1,X_2,\ldots,X_K) が広義単調増加であるとは、 任意の i (1\leq i\leq K-1) について、 X_i\leq X_{i+1} をみたすことを指します。制約
- 1 \leq N \leq 100
- 1 \leq M \leq 100
- 1 \leq A_i,B_i \leq 10^9
- 整数列 A は広義単調増加である。
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \ldots A_N B_1 B_2 \ldots B_M
出力
操作後の A の各要素を、順に空白区切りで一行に出力せよ。
操作後の A が空ならば、何も出力しないようにせよ。
入力例 1
8 5 1 2 2 3 3 3 5 6 2 2 7 3 2
出力例 1
1 3 3 5 6
最初、A は A=(1,2,2,3,3,3,5,6) です。
操作は次のように行われます。
- A から 2 を 1 つ削除し、操作後の A は A=(1,2,3,3,3,5,6) となります。
- A から 2 を 1 つ削除し、操作後の A は A=(1,3,3,3,5,6) となります。
- A は 7 を要素として持たないため、何も行いません。操作後の A は A=(1,3,3,3,5,6) のままです。
- A から 3 を 1 つ削除し、操作後の A は A=(1,3,3,5,6) となります。
- A は 2 を要素として持たないため、何も行いません。操作後の A は A=(1,3,3,5,6) のままです。
よって、すべての操作の後、A=(1,3,3,5,6) であるため、要素をこの順に空白区切りで出力します。
入力例 2
1 2 1 1 1
出力例 2
操作後の A は空となるため、何も出力しません。
Score : 200 points
Problem Statement
Takahashi has an integer sequence A=(A_1,A_2,\ldots,A_N) of length N.
It is guaranteed that A is non-decreasing.
Takahashi performs M operations on this integer sequence. In the i-th operation (1\leq i\leq M), he performs the following operation:
If the sequence A contains B_i as an element, select one such element and delete it. If no such element exists, do nothing.
Note that since A is non-decreasing, the sequence after the operation is uniquely determined regardless of the choice of element, and remains non-decreasing.
Find A after performing M operations.
What is non-decreasing?
A sequence X=(X_1,X_2,\ldots,X_K) is non-decreasing if X_i\leq X_{i+1} holds for all i (1\leq i\leq K-1).Constraints
- 1 \leq N \leq 100
- 1 \leq M \leq 100
- 1 \leq A_i,B_i \leq 10^9
- The integer sequence A is non-decreasing.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \ldots A_N B_1 B_2 \ldots B_M
Output
Output the elements of A after the operations, in order, separated by spaces on a single line.
If A is empty after the operations, output nothing.
Sample Input 1
8 5 1 2 2 3 3 3 5 6 2 2 7 3 2
Sample Output 1
1 3 3 5 6
Initially, A is A=(1,2,2,3,3,3,5,6).
The operations are performed as follows:
- Delete one 2 from A, and A becomes A=(1,2,3,3,3,5,6) after the operation.
- Delete one 2 from A, and A becomes A=(1,3,3,3,5,6) after the operation.
- Since A does not contain 7 as an element, do nothing. A remains A=(1,3,3,3,5,6) after the operation.
- Delete one 3 from A, and A becomes A=(1,3,3,5,6) after the operation.
- Since A does not contain 2 as an element, do nothing. A remains A=(1,3,3,5,6) after the operation.
Therefore, after all operations, A=(1,3,3,5,6), so output the elements in this order separated by spaces.
Sample Input 2
1 2 1 1 1
Sample Output 2
A becomes empty after the operations, so output nothing.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
長さ N の文字列 S が与えられます。クエリが Q 個与えられるので、順番に処理してください。
i 番目のクエリは以下の通りです。
- 整数 X_i と文字 C_i が与えられるので、 S の X_i 番目の文字を C_i に置き換える。その後、 S に文字列
ABCが部分文字列として何箇所含まれるかを出力する。
ここで、 S の 部分文字列 とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。
例えば、ab は abc の部分文字列ですが、ac は abc の部分文字列ではありません。
制約
- 3\le N\le 2\times 10^5
- 1\le Q\le 2\times 10^5
- S は英大文字からなる長さ N の文字列
- 1\le X_i\le N
- C_i は英大文字
入力
入力は以下の形式で標準入力から与えられる。
N Q S X_1 C_1 X_2 C_2 \vdots X_Q C_Q
出力
Q 行出力せよ。 i 行目 (1\le i\le Q) には i 番目のクエリに対する答えを出力せよ。
入力例 1
7 4 ABCDABC 4 B 3 A 5 C 4 G
出力例 1
2 1 1 0
各クエリを処理した後の S は次のようになります。
- 1 つ目のクエリを処理後: S=
ABCBABCとなる。この中にABCは部分文字列として 2 回登場する。 - 2 つ目のクエリを処理後: S=
ABABABCとなる。この中にABCは部分文字列として 1 回登場する。 - 3 つ目のクエリを処理後: S=
ABABCBCとなる。この中にABCは部分文字列として 1 回登場する。 - 4 つ目のクエリを処理後: S=
ABAGCBCとなる。この中にABCは部分文字列として 0 回登場する。
入力例 2
3 3 ABC 1 A 2 B 3 C
出力例 2
1 1 1
クエリの処理前と処理後で S が変わらない場合もあります。
入力例 3
15 10 BBCCBCACCBACACA 9 C 11 B 5 B 11 B 4 A 8 C 8 B 5 B 7 B 14 B
出力例 3
0 0 0 0 1 1 2 2 1 1
Score : 350 points
Problem Statement
You are given a string S of length N. You are also given Q queries, which you should process in order.
The i-th query is as follows:
- Given an integer X_i and a character C_i, replace the X_i-th character of S with C_i. Then, print the number of times the string
ABCappears as a substring in S.
Here, a substring of S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S.
For example, ab is a substring of abc, but ac is not a substring of abc.
Constraints
- 3 \le N \le 2 \times 10^5
- 1 \le Q \le 2 \times 10^5
- S is a string of length N consisting of uppercase English letters.
- 1 \le X_i \le N
- C_i is an uppercase English letter.
Input
The input is given from Standard Input in the following format:
N Q S X_1 C_1 X_2 C_2 \vdots X_Q C_Q
Output
Print Q lines. The i-th line (1 \le i \le Q) should contain the answer to the i-th query.
Sample Input 1
7 4 ABCDABC 4 B 3 A 5 C 4 G
Sample Output 1
2 1 1 0
After processing each query, S becomes as follows.
- After the first query: S=
ABCBABC. In this string,ABCappears twice as a substring. - After the second query: S=
ABABABC. In this string,ABCappears once as a substring. - After the third query: S=
ABABCBC. In this string,ABCappears once as a substring. - After the fourth query: S=
ABAGCBC. In this string,ABCappears zero times as a substring.
Sample Input 2
3 3 ABC 1 A 2 B 3 C
Sample Output 2
1 1 1
There are cases where S does not change through processing a query.
Sample Input 3
15 10 BBCCBCACCBACACA 9 C 11 B 5 B 11 B 4 A 8 C 8 B 5 B 7 B 14 B
Sample Output 3
0 0 0 0 1 1 2 2 1 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
一台のバスが走っています。バスの乗客の数は常に非負整数です。
このバスにはある時点で 0 人以上の乗客が乗っており、その時点から現在までに N 回停車しました。このうち i 回目の停車では乗客が差し引き A_i 人増えました。A_i は負の値であることもあり、その場合は乗客が差し引き -A_i 人減ったことを意味しています。また、停車時以外には乗客の乗り降りはありませんでした。
与えられた情報に矛盾しない現在のバスの乗客の数として考えられる最小値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- -10^9 \leq A_i \leq 10^9
- 入力される数値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
4 3 -5 7 -4
出力例 1
3
はじめに乗っている乗客の人数が 2 人であるとき、現在の乗客の人数は 2 + 3 + (-5) + 7 + (-4) = 3 人であり、さらにバスの乗客の人数は常に非負整数となります。
入力例 2
5 0 0 0 0 0
出力例 2
0
入力例 3
4 -1 1000000000 1000000000 1000000000
出力例 3
3000000000
Score: 250 points
Problem Statement
A bus is in operation. The number of passengers on the bus is always a non-negative integer.
At some point in time, the bus had zero or more passengers, and it has stopped N times since then. At the i-th stop, the number of passengers increased by A_i. Here, A_i can be negative, meaning the number of passengers decreased by -A_i. Also, no passengers got on or off the bus other than at the stops.
Find the minimum possible current number of passengers on the bus that is consistent with the given information.
Constraints
- 1 \leq N \leq 2 \times 10^5
- -10^9 \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 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
4 3 -5 7 -4
Sample Output 1
3
If the initial number of passengers was 2, the current number of passengers would be 2 + 3 + (-5) + 7 + (-4) = 3, and the number of passengers on the bus would have always been a non-negative integer.
Sample Input 2
5 0 0 0 0 0
Sample Output 2
0
Sample Input 3
4 -1 1000000000 1000000000 1000000000
Sample Output 3
3000000000
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
H \times W のグリッドがあり、各マスには 0 または 1 の整数が書き込まれています。
各マスに書き込まれた整数の情報は H 個の長さ W の文字列 S_1,S_2,\dots,S_H として与えられます。
S_i の j 文字目が 0 である時はグリッドの上から i 行目、左から j 列目に 0 が書き込まれており、 S_i の j 文字目が 1 である時はグリッドの上から i 行目、左から j 列目に 1 が書き込まれています。
書き込まれた整数の総和が K となる長方形領域がいくつあるか求めてください。
厳密には、以下の条件を全て満たす整数の四つ組 (r_1,c_1,r_2,c_2) がいくつあるか求めてください。
- 1 \le r_1 \le r_2 \le H
- 1 \le c_1 \le c_2 \le W
- グリッドの上から i 行目、左から j 列目に書き込まれた整数を、 r_1 \le i \le r_2, c_1 \le j \le c_2 を満たす全ての整数組 (i,j) について足し合わせた値が K である。
制約
- H,W は 1 以上 500 以下の整数
- K は 0 以上 HW 以下の整数
- S_i は
0,1からなる長さ W の文字列
入力
入力は以下の形式で標準入力から与えられる。
H W K S_1 S_2 \vdots S_H
出力
答えを出力せよ。
入力例 1
3 4 3 1001 1101 0110
出力例 1
8
書き込まれた整数の総和が K となる長方形領域は以下の 8 個です。
- 上から 1 行目から 2 行目、左から 1 列目から 2 列目を抜き出した長方形領域
- 上から 1 行目から 2 行目、左から 1 列目から 3 列目を抜き出した長方形領域
- 上から 1 行目から 2 行目、左から 2 列目から 4 列目を抜き出した長方形領域
- 上から 1 行目から 3 行目、左から 2 列目から 3 列目を抜き出した長方形領域
- 上から 1 行目から 3 行目、左から 3 列目から 4 列目を抜き出した長方形領域
- 上から 2 行目から 2 行目、左から 1 列目から 4 列目を抜き出した長方形領域
- 上から 2 行目から 3 行目、左から 1 列目から 2 列目を抜き出した長方形領域
- 上から 2 行目から 3 行目、左から 2 列目から 3 列目を抜き出した長方形領域
入力例 2
5 4 20 0101 1010 0101 1010 0101
出力例 2
0
入力例 3
15 20 17 10111101101100000100 01100000000010000011 01110010111000111000 11001100000111011000 10100001100011100010 01101000101010000101 10110001111110000100 10110011101100101101 01010001110011001001 01010110010001100110 01110100011110011110 01100000100111010010 11001101100111101100 10111100010101111011 00101101011100010000
出力例 3
448
Score : 425 points
Problem Statement
There is an H \times W grid, and each cell contains an integer 0 or 1.
The information of the integer written in each cell is given as H strings S_1, S_2, \dots, S_H of length W.
If the j-th character of S_i is 0, then 0 is written in the cell at the i-th row from the top and the j-th column from the left of the grid; if the j-th character of S_i is 1, then 1 is written in that cell.
Find the number of rectangular regions where the sum of the written integers equals K.
More formally, find the number of quadruples of integers (r_1, c_1, r_2, c_2) satisfying all of the following conditions:
- 1 \le r_1 \le r_2 \le H
- 1 \le c_1 \le c_2 \le W
- The sum of the integers written in the cell at the i-th row from the top and the j-th column from the left, over all pairs of integers (i, j) satisfying r_1 \le i \le r_2, c_1 \le j \le c_2, equals K.
Constraints
- H and W are integers between 1 and 500, inclusive.
- K is an integer between 0 and HW, inclusive.
- S_i is a string of length W consisting of
0and1.
Input
The input is given from Standard Input in the following format:
H W K S_1 S_2 \vdots S_H
Output
Output the answer.
Sample Input 1
3 4 3 1001 1101 0110
Sample Output 1
8
There are eight rectangular regions where the sum of the written integers equals K:
- The rectangular region extracted from row 1 to row 2 from the top and column 1 to column 2 from the left
- The rectangular region extracted from row 1 to row 2 from the top and column 1 to column 3 from the left
- The rectangular region extracted from row 1 to row 2 from the top and column 2 to column 4 from the left
- The rectangular region extracted from row 1 to row 3 from the top and column 2 to column 3 from the left
- The rectangular region extracted from row 1 to row 3 from the top and column 3 to column 4 from the left
- The rectangular region extracted from row 2 to row 2 from the top and column 1 to column 4 from the left
- The rectangular region extracted from row 2 to row 3 from the top and column 1 to column 2 from the left
- The rectangular region extracted from row 2 to row 3 from the top and column 2 to column 3 from the left
Sample Input 2
5 4 20 0101 1010 0101 1010 0101
Sample Output 2
0
Sample Input 3
15 20 17 10111101101100000100 01100000000010000011 01110010111000111000 11001100000111011000 10100001100011100010 01101000101010000101 10110001111110000100 10110011101100101101 01010001110011001001 01010110010001100110 01110100011110011110 01100000100111010010 11001101100111101100 10111100010101111011 00101101011100010000
Sample Output 3
448
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
高橋君が N 体のモンスターと順に戦うゲームで遊ぼうとしています。最初、高橋君の体力は H 、魔力は M です。
i 番目に戦うモンスターに対して、高橋君は以下のどちらかの行動を選べます。
- 魔法を使わずに戦う。体力が A_i 以上のときのみ選ぶことができ、体力が A_i 減少しモンスターを倒す。
- 魔法を使って戦う。魔力が B_i 以上のときのみ選ぶことができ、魔力が B_i 減少しモンスターを倒す。
N 体全てのモンスターを倒すか、高橋君が行動できなくなるとゲーム終了です。ゲーム終了までに最大で何体のモンスターを倒せますか。
制約
- 1 \leq N \leq 3000
- 1 \leq H,M \leq 3000
- 1 \leq A_i,B_i \leq 3000
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N H M A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
答えを出力せよ。
入力例 1
4 10 14 5 8 5 6 7 9 99 99
出力例 1
3
次のように行動することで、ゲーム終了までに 3 体のモンスターを倒すことができます。
- 最初、高橋君の体力は 10、魔力は 14 です。
- 1 体目のモンスターと魔法を使わずに戦う。体力が 5 減少し、体力が 5、魔力が 14 となる。
- 2 体目のモンスターと魔法を使わずに戦う。体力が 5 減少し、体力が 0、魔力が 14 となる。
- 3 体目のモンスターと魔法を使って戦う。魔力が 9 減少し、体力が 0、魔力が 5 となる。
- 4 体目のモンスターとの戦いではどちらの行動も選べないためゲーム終了となる。
入力例 2
3 3000 3000 3 3 3 3 3 3
出力例 2
3
全てのモンスターを倒せることもあります。
入力例 3
10 8 8 2 2 2 3 2 2 1 2 2 3 1 2 3 3 3 2 3 1 3 2
出力例 3
9
Score : 450 points
Problem Statement
Takahashi is about to play a game where he fights N monsters in order. Initially, Takahashi's health is H and his magic power is M.
For the i-th monster he fights, he can choose one of the following actions:
- Fight without using magic. This can only be chosen when his health is at least A_i, and his health decreases by A_i and the monster is defeated.
- Fight using magic. This can only be chosen when his magic power is at least B_i, and his magic power decreases by B_i and the monster is defeated.
The game ends when all N monsters are defeated or when he cannot take any action. What is the maximum number of monsters he can defeat before the game ends?
Constraints
- 1 \leq N \leq 3000
- 1 \leq H,M \leq 3000
- 1 \leq A_i,B_i \leq 3000
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N H M A_1 B_1 A_2 B_2 \vdots A_N B_N
Output
Output the answer.
Sample Input 1
4 10 14 5 8 5 6 7 9 99 99
Sample Output 1
3
By taking the following actions, Takahashi can defeat 3 monsters before the game ends.
- Initially, his health is 10 and his magic power is 14.
- Fight the 1st monster without using magic. His health decreases by 5, becoming health 5 and magic power 14.
- Fight the 2nd monster without using magic. His health decreases by 5, becoming health 0 and magic power 14.
- Fight the 3rd monster using magic. His magic power decreases by 9, becoming health 0 and magic power 5.
- For the 4th monster, he cannot choose either action, so the game ends.
Sample Input 2
3 3000 3000 3 3 3 3 3 3
Sample Output 2
3
He may be able to defeat all monsters.
Sample Input 3
10 8 8 2 2 2 3 2 2 1 2 2 3 1 2 3 3 3 2 3 1 3 2
Sample Output 3
9
Time Limit: 5 sec / Memory Limit: 1024 MiB
配点 : 575 点
問題文
円形のケーキがあり、ケーキは切り目によって N 個のピースに分けられています。各切り目は円の中心と円弧上の点を結ぶ線分です。
ピースおよび切り目には時計回りに 1, 2, \ldots, N の番号が付けられており、ピース i の質量は A_i です。ピース 1 をピース N + 1 とも呼ぶこととします。
切り目 i は ピース i とピース i + 1 の間にあり、ピース 1, 切り目 1, ピース 2, 切り目 2, \ldots, ピース N, 切り目 N の順に時計回りに並んでいます。
このケーキを以下の条件を満たすように K 人に分けようとしています。ただし、i 番目の人が受け取るピースの質量の合計を w_i とします。
- すべての人が 1 つ以上の連続するピースを受け取る
- 誰も受け取らないピースは存在しない
- 上の 2 つの条件を満たすという条件下で \min(w_1, w_2, \ldots, w_K) が最大になるようにする
条件を満たす分け方における \min(w_1, w_2, \ldots, w_K) の値および条件を満たすすべての分け方で切られることのない切り目の個数を求めてください。ただし、切り目 i が切られるとは、ピース i とピース i + 1 が異なる人に分けられることを指します。
制約
- 2 \leq K \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^4
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \ldots A_N
出力
条件を満たす分け方における \min(w_1, w_2, \ldots, w_K) の値を x、 切られることのない切り目の個数を y として、x と y をこの順に空白区切りで出力せよ。
入力例 1
5 2 3 6 8 6 4
出力例 1
13 1
以下の分け方が条件を満たします。
- 一方の人にピース 2, 3 を渡し、もう一方の人にピース 4, 5, 1 を渡す。ピース 2, 3 の質量の合計は 14、ピース 4, 5, 1 の質量の合計は 13 である。
- 一方の人にピース 3, 4 を渡し、もう一方の人にピース 5, 1, 2 を渡す。ピース 3, 4 の質量の合計は 14、ピース 5, 1, 2 の質量の合計は 13 である。
条件を満たす分け方における \min(w_1, w_2) の値は 13 であり、どちらの分け方でも切られない切り目は切り目 5 の 1 つです。
入力例 2
6 3 4 7 11 3 9 2
出力例 2
11 1
入力例 3
10 3 2 9 8 1 7 9 1 3 5 8
出力例 3
17 4
Score : 575 points
Problem Statement
There is a circular cake divided into N pieces by cut lines. Each cut line is a line segment connecting the center of the circle to a point on the arc.
The pieces and cut lines are numbered 1, 2, \ldots, N in clockwise order, and piece i has a mass of A_i. Piece 1 is also called piece N + 1.
Cut line i is between pieces i and i + 1, and they are arranged clockwise in this order: piece 1, cut line 1, piece 2, cut line 2, \ldots, piece N, cut line N.
We want to divide this cake among K people under the following conditions. Let w_i be the sum of the masses of the pieces received by the i-th person.
- Each person receives one or more consecutive pieces.
- There are no pieces that no one receives.
- Under the above two conditions, \min(w_1, w_2, \ldots, w_K) is maximized.
Find the value of \min(w_1, w_2, \ldots, w_K) in a division that satisfies the conditions, and the number of cut lines that are never cut in the divisions that satisfy the conditions. Here, cut line i is considered cut if pieces i and i + 1 are given to different people.
Constraints
- 2 \leq K \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^4
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \ldots A_N
Output
Let x be the value of \min(w_1, w_2, \ldots, w_K) in a division that satisfies the conditions, and y be the number of cut lines that are never cut. Print x and y in this order, separated by a space.
Sample Input 1
5 2 3 6 8 6 4
Sample Output 1
13 1
The following divisions satisfy the conditions:
- Give pieces 2, 3 to one person and pieces 4, 5, 1 to the other. Pieces 2, 3 have a total mass of 14, and pieces 4, 5, 1 have a total mass of 13.
- Give pieces 3, 4 to one person and pieces 5, 1, 2 to the other. Pieces 3, 4 have a total mass of 14, and pieces 5, 1, 2 have a total mass of 13.
The value of \min(w_1, w_2) in divisions satisfying the conditions is 13, and there is one cut line that is not cut in either division: cut line 5.
Sample Input 2
6 3 4 7 11 3 9 2
Sample Output 2
11 1
Sample Input 3
10 3 2 9 8 1 7 9 1 3 5 8
Sample Output 3
17 4