実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。
長さ M の A の連続部分列 B=(B_1,B_2,\dots,B_M) に対する、\displaystyle \sum_{i=1}^{M} i \times B_i の最大値を求めてください。
注記
数列の連続部分列とは、数列の先頭から 0 個以上、末尾から 0 個以上の要素を削除して得られる列のことをいいます。
例えば (2, 3) や (1, 2, 3) は (1, 2, 3, 4) の連続部分列ですが、(1, 3) や (3,2,1) は (1, 2, 3, 4) の連続部分列ではありません。
制約
- 1 \le M \le N \le 2 \times 10^5
- - 2 \times 10^5 \le A_i \le 2 \times 10^5
- 入力は全て整数。
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
4 2 5 4 -1 8
出力例 1
15
B=(A_3,A_4) とした場合、\displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times (-1) + 2 \times 8 = 15 となります。16 以上の値を達成することはできないため、解は 15 です。
B=(A_1,A_4) などを選ぶことができないことに注意してください。
入力例 2
10 4 -3 1 -4 1 -5 9 -2 6 -5 3
出力例 2
31
Score : 300 points
Problem Statement
You are given an integer sequence A=(A_1,A_2,\dots,A_N) of length N.
Find the maximum value of \displaystyle \sum_{i=1}^{M} i \times B_i for a contiguous subarray B=(B_1,B_2,\dots,B_M) of A of length M.
Notes
A contiguous subarray of a number sequence is a sequence that is obtained by removing 0 or more initial terms and 0 or more final terms from the original number sequence.
For example, (2, 3) and (1, 2, 3) are contiguous subarrays of (1, 2, 3, 4), but (1, 3) and (3,2,1) are not contiguous subarrays of (1, 2, 3, 4).
Constraints
- 1 \le M \le N \le 2 \times 10^5
- - 2 \times 10^5 \le A_i \le 2 \times 10^5
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N
Output
Print the answer.
Sample Input 1
4 2 5 4 -1 8
Sample Output 1
15
When B=(A_3,A_4), we have \displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times (-1) + 2 \times 8 = 15. Since it is impossible to achieve 16 or a larger value, the solution is 15.
Note that you are not allowed to choose, for instance, B=(A_1,A_4).
Sample Input 2
10 4 -3 1 -4 1 -5 9 -2 6 -5 3
Sample Output 2
31
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
1 から N までの番号がついた N 個の空の箱と、何も書かれていない無数のカードがあります。
Q 個のクエリが与えられるので、順番に処理してください。クエリは次の 3 種類のいずれかです。
1 i j\colon カードを 1 枚選んで数 i を書き込み、そのカードを箱 j に入れる2 i\colon 箱 i に入っているカードに書かれた数を昇順ですべて答える3 i\colon 数 i が書かれたカードが入っている箱の番号を昇順ですべて答える
ただし、以下の点に注意してください。
- 2 番目の形式のクエリにおいては、箱 i の中に同じ数が書かれたカードが複数枚あるときは、入っている枚数と同じ回数だけその数を出力する
- 3 番目の形式のクエリにおいては、数 i が書かれたカードが同じ箱に複数枚入っている場合でも、その箱の番号は 1 度だけ出力する
制約
- 1 \leq N, Q \leq 2 \times 10^5
- 1 番目の形式のクエリについて、
- 1 \leq i \leq 2 \times 10^5
- 1 \leq j \leq N
- 2 番目の形式のクエリについて、
- 1 \leq i \leq N
- このクエリが与えられる時点で箱 i にカードが入っている
- 3 番目の形式のクエリについて、
- 1 \leq i \leq 2 \times 10^5
- このクエリが与えられる時点で数 i が書かれたカードが入っている箱が存在する
- 出力するべき数は合計で 2 \times 10^5 個以下
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
ただし、\mathrm{query}_q は q 個目のクエリを表しており、次のいずれかの形式で与えられる。
1 i j
2 i
3 i
出力
2 番目の形式のクエリと 3 番目の形式のクエリに対し、順番に答えを出力せよ。
各クエリでは出力するべき要素を昇順に空白区切りで出力し、クエリごとに改行せよ。
入力例 1
5 8 1 1 1 1 2 4 1 1 4 2 4 1 1 4 2 4 3 1 3 2
出力例 1
1 2 1 1 2 1 4 4
クエリを順に処理していきます。
- カードに 1 を書き込み、箱 1 に入れます。
- カードに 2 を書き込み、箱 4 に入れます。
- カードに 1 を書き込み、箱 4 に入れます。
- 箱 4 に入っているカードに書かれた数は 1, 2 です。
- 1, 2 の順に出力してください。
- カードに 1 を書き込み、箱 4 に入れます。
- 箱 4 に入っているカードに書かれた数は 1, 1, 2 です。
- 1 を 2 度出力することに注意してください。
- 数 1 が書かれたカードが入っている箱は箱 1, 4 です。
- 箱 4 には数 1 が書かれたカードが 2 枚入っていますが、4 は 1 回しか出力しないことに注意してください。
- 数 2 が書かれたカードが入っている箱は箱 4 です。
入力例 2
1 5 1 1 1 1 2 1 1 200000 1 2 1 3 200000
出力例 2
1 2 200000 1
Score : 300 points
Problem Statement
We have N boxes numbered 1 to N that are initially empty, and an unlimited number of blank cards.
Process Q queries in order. There are three kinds of queries as follows.
1 i j\colon Write the number i on a blank card and put it into box j.2 i\colon Report all numbers written on the cards in box i, in ascending order.3 i\colon Report all box numbers of the boxes that contain a card with the number i, in ascending order.
Here, note the following.
- In a query of the second kind, if box i contains multiple cards with the same number, that number should be printed the number of times equal to the number of those cards.
- In a query of the third kind, even if a box contains multiple cards with the number i, the box number of that box should be printed only once.
Constraints
- 1 \leq N, Q \leq 2 \times 10^5
- For a query of the first kind:
- 1 \leq i \leq 2 \times 10^5
- 1 \leq j \leq N
- For a query of the second kind:
- 1 \leq i \leq N
- Box i contains some cards when this query is given.
- For a query of the third kind:
- 1 \leq i \leq 2 \times 10^5
- Some boxes contain a card with the number i when this query is given.
- At most 2 \times 10^5 numbers are to be reported.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Here, \mathrm{query}_q denotes the q-th query, which is in one of the following formats:
1 i j
2 i
3 i
Output
Respond to the queries of the second and third kinds in order.
For each of those queries, print one line containing the elements to be reported in ascending order, with spaces in between.
Sample Input 1
5 8 1 1 1 1 2 4 1 1 4 2 4 1 1 4 2 4 3 1 3 2
Sample Output 1
1 2 1 1 2 1 4 4
Let us process the queries in order.
- Write 1 on a card and put it into box 1.
- Write 2 on a card and put it into box 4.
- Write 1 on a card and put it into box 4.
- Box 4 contains cards with the numbers 1 and 2.
- Print 1 and 2 in this order.
- Write 1 on a card and put it into box 4.
- Box 4 contains cards with the numbers 1, 1, and 2.
- Note that you should print 1 twice.
- Boxes 1 and 4 contain a card with the number 1.
- Note that you should print 4 only once, even though box 4 contains two cards with the number 1.
- Boxes 4 contains a card with the number 2.
Sample Input 2
1 5 1 1 1 1 2 1 1 200000 1 2 1 3 200000
Sample Output 2
1 2 200000 1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
N 個のステージからなるゲームがあり、i \, (1 \leq i \leq N) 番目のステージは A_i 分間のストーリー映像と B_i 分間のゲームプレイによって構成されます。
初めて i 番目のステージをクリアするためにはストーリー映像の視聴とゲームプレイを両方行う必要がありますが、二回目以降はストーリー映像をスキップすることができるので、ゲームプレイのみでクリアすることができます。
初めから遊べるのは 1 番目のステージのみですが、i \, (1 \leq i \leq N - 1) 番目のステージをクリアすることにより、i+1 番目のステージも遊べるようになります。
合計 X 回ステージをクリアするために必要な時間の最小値を求めてください。ただし、同じステージを複数回クリアしたとしても、全てクリア回数に数えられます。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq 10^9 \, (1 \leq i \leq N)
- 1 \leq X \leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N X A_1 B_1 \vdots A_N B_N
出力
答えを出力せよ。
入力例 1
3 4 3 4 2 3 4 2
出力例 1
18
例えば、次のようにして 18 分で 4 回クリアすることができます。
- ステージ 1 をクリアする。A_1 + B_1 = 7 分かかる。
- ステージ 2 をクリアする。A_2 + B_2 = 5 分かかる。
- ステージ 2 を再びクリアする。B_2 = 3 分かかる。
- ステージ 2 を再びクリアする。B_2 = 3 分かかる。
17 分以内に 4 回クリアすることはできません。
入力例 2
10 1000000000 3 3 1 6 4 7 1 8 5 7 9 9 2 4 6 4 5 1 3 1
出力例 2
1000000076
Score : 400 points
Problem Statement
We have a video game consisting of N stages. The i-th stage (1 \leq i \leq N) is composed of a movie lasting A_i minutes and gameplay lasting B_i minutes.
To clear the i-th stage for the first time, one must watch the movie and do the gameplay for that stage. For the second and subsequent times, one may skip the movie and do just the gameplay.
In the beginning, only the 1-st stage is unlocked, and clearing the i-th stage (1 \leq i \leq N - 1) unlocks the (i+1)-th stage.
Find the shortest time needed to clear a stage X times in total. Here, if the same stage is cleared multiple times, all of them count.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq 10^9 \, (1 \leq i \leq N)
- 1 \leq X \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X A_1 B_1 \vdots A_N B_N
Output
Print the answer.
Sample Input 1
3 4 3 4 2 3 4 2
Sample Output 1
18
Here is one way to clear a stage 4 times in 18 minutes:
- Clear Stage 1. It takes A_1 + B_1 = 7 minutes.
- Clear Stage 2. It takes A_2 + B_2 = 5 minutes.
- Clear Stage 2 again. It takes B_2= 3 minutes.
- Clear Stage 2 again. It takes B_2= 3 minutes.
It is impossible to clear a stage 4 times within 17 minutes.
Sample Input 2
10 1000000000 3 3 1 6 4 7 1 8 5 7 9 9 2 4 6 4 5 1 3 1
Sample Output 2
1000000076
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
以下の条件を満たす正の整数 n を、 等差数 と呼びます。
- (n を先頭に余計な 0 を付けずに 10 進法で表記した際、) n の上から i 桁目を d_i とする。このとき、 n が k 桁の整数であったとすると、 (d_2-d_1)=(d_3-d_2)=\dots=(d_k-d_{k-1}) が成立する。
- この条件は、「 数列 (d_1,d_2,\dots,d_k) が等差数列である」と言い換えることができる。
- 但し、 n が 1 桁の整数である時、 n は等差数であるものとする。
たとえば、 234,369,86420,17,95,8,11,777 は等差数ですが、 751,919,2022,246810,2356 は等差数ではありません。
等差数のうち、 X 以上で最小のものを求めてください。
制約
- X は 1 以上 10^{17} 以下の整数である
入力
入力は以下の形式で標準入力から与えられる。
X
出力
答えを整数として出力せよ。
入力例 1
152
出力例 1
159
152 以上で最小の等差数は 159 です。
入力例 2
88
出力例 2
88
X 自身が等差数である場合もあります。
入力例 3
8989898989
出力例 3
9876543210
Score : 500 points
Problem Statement
Let us call a positive integer n that satisfies the following condition an arithmetic number.
- Let d_i be the i-th digit of n from the top (when n is written in base 10 without unnecessary leading zeros.) Then, (d_2-d_1)=(d_3-d_2)=\dots=(d_k-d_{k-1}) holds, where k is the number of digits in n.
- This condition can be rephrased into the sequence (d_1,d_2,\dots,d_k) being arithmetic.
- If n is a 1-digit integer, it is assumed to be an arithmetic number.
For example, 234,369,86420,17,95,8,11,777 are arithmetic numbers, while 751,919,2022,246810,2356 are not.
Find the smallest arithmetic number not less than X.
Constraints
- X is an integer between 1 and 10^{17} (inclusive).
Input
Input is given from Standard Input in the following format:
X
Output
Print the answer as an integer.
Sample Input 1
152
Sample Output 1
159
The smallest arithmetic number not less than 152 is 159.
Sample Input 2
88
Sample Output 2
88
X itself may be an arithmetic number.
Sample Input 3
8989898989
Sample Output 3
9876543210
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
高橋君は整数 x を持っています。はじめ、x = 0 です。
N 個の操作があります。i \, (1 \leq i \leq N) 個目の操作は整数 t_i, y_i を用いて以下のように表されます。
- t_i = 1 のとき、x を y_i で置き換える。
- t_i = 2 のとき、x を x + y_i で置き換える。
高橋君は 0 個以上 K 個以下の好きな個数の操作を無視することができます。残った操作を一度ずつ順序を変えずに行ったとき、最終的な x の値としてあり得る最大値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq K \leq N
- t_i \in \{1,2\} \, (1 \leq i \leq N)
- |y_i| \leq 10^9 \, (1 \leq i \leq N)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K t_1 y_1 \vdots t_N y_N
出力
答えを出力せよ。
入力例 1
5 1 2 4 2 -3 1 2 2 1 2 -3
出力例 1
3
5 個目の操作を無視すると、x は 0 \rightarrow 4 \rightarrow 1 \rightarrow 2 \rightarrow 3 と変化し、最終的な x の値は 3 となります。これが最大です。
入力例 2
1 0 2 -1000000000
出力例 2
-1000000000
入力例 3
10 3 2 3 2 -1 1 4 2 -1 2 5 2 -9 2 2 1 -6 2 5 2 -3
出力例 3
15
Score : 500 points
Problem Statement
Takahashi has an integer x. Initially, x = 0.
There are N operations. The i-th operation (1 \leq i \leq N) is represented by two integers t_i and y_i as follows:
- If t_i = 1, replace x with y_i.
- If t_i = 2, replace x with x + y_i.
Takahashi may skip any number between 0 and K (inclusive) of the operations. When he performs the remaining operations once each without changing the order, find the maximum possible final value of x.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq K \leq N
- t_i \in \{1,2\} \, (1 \leq i \leq N)
- |y_i| \leq 10^9 \, (1 \leq i \leq N)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N K t_1 y_1 \vdots t_N y_N
Output
Print the answer.
Sample Input 1
5 1 2 4 2 -3 1 2 2 1 2 -3
Sample Output 1
3
If he skips the 5-th operation, x changes as 0 \rightarrow 4 \rightarrow 1 \rightarrow 2 \rightarrow 3, so x results in 3. This is the maximum.
Sample Input 2
1 0 2 -1000000000
Sample Output 2
-1000000000
Sample Input 3
10 3 2 3 2 -1 1 4 2 -1 2 5 2 -9 2 2 1 -6 2 5 2 -3
Sample Output 3
15