Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
AtCoder で定期的に開催されている、国際的な権威があるコンテストである AtCoder Grand Contest (以下、AGC) は今までに 54 回開催されてきました。
みなさんがちょうど参加している 230 回目の ABC である ABC230 と同様に、 当初は N 回目の AGC のコンテスト名には N を 3 桁になるようにゼロ埋めした数が割り振られていました。( 1 回目の AGC は AGC001, 2 回目の AGC は AGC002, ...)
ところが、最新の 54 回目の AGC のコンテスト名は AGC055 で、回数より 1 大きい数が割り振られています。これは、AGC042 が社会情勢の影響で中止されて欠番となったため、42 回目以降に開催されたコンテストでは開催された回数より 1 大きい数が割り振られているからです。(入出力例にある説明も参考にしてください。)
さて、ここで問題です。整数 N が与えられるので、N 回目に開催された AGC のコンテスト名を AGCXXX の形式で出力してください。ここで、XXX にはゼロ埋めがなされた 3 桁の数が入ります。
制約
- 1 \leq N \leq 54
- N は整数である。
入力
入力は以下の形式で標準入力から与えられる。
N
出力
N 回目に開催された AGC のコンテスト名を所定の形式で出力せよ。
入力例 1
42
出力例 1
AGC043
問題文にある通り、 42 回目以降の AGC には回数より 1 大きい数が割り振られています。
よって 42 回目の AGC のコンテスト名は AGC043 になります。
入力例 2
19
出力例 2
AGC019
41 回目以前の AGC は回数と同じ数が割り振られています。
よって AGC019 が答えとなります。
入力例 3
1
出力例 3
AGC001
問題文にある通り、 1 回目の AGC のコンテスト名は AGC001 です。
数が 3 桁になるようにゼロ埋めを行う必要があるのに注意してください。
入力例 4
50
出力例 4
AGC051
Score : 100 points
Problem Statement
AtCoder Grand Contest (AGC), a regularly held contest with a world authority, has been held 54 times.
Just like the 230-th ABC ― the one you are in now ― is called ABC230, the N-th AGC is initially named with a zero-padded 3-digit number N. (The 1-st AGC is AGC001, the 2-nd AGC is AGC002, ...)
However, the latest 54-th AGC is called AGC055, where the number is one greater than 54. Because AGC042 is canceled and missing due to the social situation, the 42-th and subsequent contests are assigned numbers that are one greater than the numbers of contests held. (See also the explanations at Sample Inputs and Outputs.)
Here is the problem: given an integer N, print the name of the N-th AGC in the format AGCXXX, where XXX is the zero-padded 3-digit number.
Constraints
- 1 \leq N \leq 54
- N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
Print the name of the N-th AGC in the specified format.
Sample Input 1
42
Sample Output 1
AGC043
As explained in Problem Statement, the 42-th and subsequent AGCs are assigned numbers that are one greater than the numbers of contests.
Thus, the 42-th AGC is named AGC043.
Sample Input 2
19
Sample Output 2
AGC019
The 41-th and preceding AGCs are assigned numbers that are equal to the numbers of contests.
Thus, the answer is AGC019.
Sample Input 3
1
Sample Output 3
AGC001
As mentioned in Problem Statement, the 1-st AGC is named AGC001.
Be sure to pad the number with zeros into a 3-digit number.
Sample Input 4
50
Sample Output 4
AGC051
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
数字のみからなる、長さがちょうど 9 の文字列 S が与えられます。
S には 0 から 9 までのうち、ちょうど 1 つの数字を除いた 9 種類の数字が一度ずつ登場します。
S に登場しない唯一の数字を出力してください。
制約
- S は数字のみからなる長さ 9 の文字列である。
- S の文字はすべて相異なる。
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S に登場しない唯一の数字を出力せよ。
入力例 1
023456789
出力例 1
1
文字列 023456789 には 1 のみが登場していません。
よって、1 を出力します。
入力例 2
459230781
出力例 2
6
文字列 459230781 には 6 のみが登場していません。
よって、6 を出力します。
文字列に数字が現れる順番は昇順とは限らないので注意してください。
Score : 100 points
Problem Statement
You are given a string S of length exactly 9 consisting of digits.
One but all digits from 0 to 9 appear exactly once in S.
Print the only digit missing in S.
Constraints
- S is a string of length 9 consisting of digits.
- All characters in S are distinct.
Input
Input is given from Standard Input in the following format:
S
Output
Print the only digit missing in S.
Sample Input 1
023456789
Sample Output 1
1
The string 023456789 only lacks 1.
Thus, 1 should be printed.
Sample Input 2
459230781
Sample Output 2
6
The string 459230781 only lacks 6.
Thus, 6 should be printed.
Note that the digits in the string may not appear in increasing order.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
英小文字からなる文字列 S, T が与えられます。S の長さは N、T の長さは M です。(N \leq M が制約で保証されています)
S が T の 接頭辞 であるとは、T のはじめ N 文字からなる文字列が S と一致することを言います。
S が T の 接尾辞 であるとは、T の後ろ N 文字からなる文字列が S と一致することを言います。
S が T の接頭辞であり、かつ接尾辞でもある場合は 0 を、
S が T の接頭辞であるが、接尾辞でない場合は 1 を、
S が T の接尾辞であるが、接頭辞でない場合は 2 を、
S が T の接頭辞でも接尾辞でもない場合は 3 を出力してください。
制約
- 1 \leq N \leq M \leq 100
- S は英小文字からなる長さ N の文字列
- T は英小文字からなる長さ M の文字列
入力
入力は以下の形式で標準入力から与えられる。
N M S T
出力
問題文の指示に従って答えを出力せよ。
入力例 1
3 7 abc abcdefg
出力例 1
1
S は T の接頭辞ですが接尾辞ではありません。よって 1 を出力します。
入力例 2
3 4 abc aabc
出力例 2
2
S は T の接尾辞ですが接頭辞ではありません。
入力例 3
3 3 abc xyz
出力例 3
3
S は T の接頭辞でも接尾辞でもありません。
入力例 4
3 3 aaa aaa
出力例 4
0
S と T が完全に一致する場合もあります。この場合、S は T の接頭辞であり、かつ接尾辞でもあります。
Score : 200 points
Problem Statement
You are given two strings S and T consisting of lowercase English letters. The lengths of S and T are N and M, respectively. (The constraints guarantee that N \leq M.)
S is said to be a prefix of T when the first N characters of T coincide S.
S is said to be a suffix of T when the last N characters of T coincide S.
If S is both a prefix and a suffix of T, print 0;
If S is a prefix of T but not a suffix, print 1;
If S is a suffix of T but not a prefix, print 2;
If S is neither a prefix nor a suffix of T, print 3.
Constraints
- 1 \leq N \leq M \leq 100
- S is a string of length N consisting of lowercase English letters.
- T is a string of length M consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N M S T
Output
Print the answer according to the instructions in the problem statement.
Sample Input 1
3 7 abc abcdefg
Sample Output 1
1
S is a prefix of T but not a suffix, so you should print 1.
Sample Input 2
3 4 abc aabc
Sample Output 2
2
S is a suffix of T but not a prefix.
Sample Input 3
3 3 abc xyz
Sample Output 3
3
S is neither a prefix nor a suffix of T.
Sample Input 4
3 3 aaa aaa
Sample Output 4
0
S and T may coincide, in which case S is both a prefix and a suffix of T.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
正整数 N, M が与えられます。
X = \displaystyle\sum_{i = 0}^{M} N^i とします。X \leq 10^9 のときは X の値を、X > 10^9 のときは文字列 inf を出力してください。
制約
- 1 \leq N \leq 10^9
- 1 \leq M \leq 100
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M
出力
問題文の指示に従って X の値あるいは inf を出力せよ。
入力例 1
7 3
出力例 1
400
X = 1 + 7 + 49 + 343 = 400 です。400 \leq 10^9 であるため 400 を出力します。
入力例 2
1000000 2
出力例 2
inf
X = 1000001000001 > 10^9 であるため、inf を出力します。
入力例 3
999999999 1
出力例 3
1000000000
入力例 4
998244353 99
出力例 4
inf
Score : 200 points
Problem Statement
You are given two positive integers N and M.
Let X = \displaystyle\sum_{i = 0}^{M} N^i. If X \leq 10^9, print the value of X. If X > 10^9, print inf.
Constraints
- 1 \leq N \leq 10^9
- 1 \leq M \leq 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M
Output
Print the value of X or inf as specified by the problem statement.
Sample Input 1
7 3
Sample Output 1
400
X = 1 + 7 + 49 + 343 = 400. Since 400 \leq 10^9, print 400.
Sample Input 2
1000000 2
Sample Output 2
inf
X = 1000001000001 > 10^9, so print inf.
Sample Input 3
999999999 1
Sample Output 3
1000000000
Sample Input 4
998244353 99
Sample Output 4
inf
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
あなたは N 本の鍵 1,2,\dots,N を持っています。
このうち何本かの鍵は正しい鍵で、それ以外はダミーの鍵です。
また、鍵を何本でも挿し込める ドアX があり、この ドアX は正しい鍵を K 本以上挿し込んだ時、またその時に限って開きます。
あなたはこれらの鍵に対して M 回のテストを行いました。このうち i 回目のテストの内容は次の通りです。
- C_i 本の鍵 A_{i,1},A_{i,2},\dots,A_{i,C_i} を ドアX に挿し込む。
- テスト結果はひとつの英文字 R_i で表現される。
- R_i =
oのとき i 回目のテストでドアが開いたことを表す。 - R_i =
xのとき i 回目のテストでドアが開かなかったことを表す。
- R_i =
各鍵が正しいかダミーかの組み合わせは 2^N 通り考えられますが、このうちどのテスト結果にも矛盾しない組み合わせの個数を求めてください。
ただし、与えられるテスト結果が誤っており上記の条件を満たす組み合わせが存在しない場合もあります。その場合は 0 通りと解答してください。
制約
- N,M,K,C_i,A_{i,j} は整数
- 1 \le K \le N \le 15
- 1 \le M \le 100
- 1 \le C_i \le N
- 1 \le A_{i,j} \le N
- j \neq k ならば A_{i,j} \neq A_{i,k}
- R_i は
oまたはx
入力
入力は以下の形式で標準入力から与えられる。
N M K
C_1 A_{1,1} A_{1,2} \dots A_{1,C_1} R_1
C_2 A_{2,1} A_{2,2} \dots A_{2,C_2} R_2
\vdots
C_M A_{M,1} A_{M,2} \dots A_{M,C_M} R_M
出力
答えを整数として出力せよ。
入力例 1
3 2 2 3 1 2 3 o 2 2 3 x
出力例 1
2
この入力では鍵が 3 本あり、テストは 2 回行われました。
また、 ドアX を開くのに必要な正しい鍵の本数は 2 本です。
- 1 回目のテストでは鍵 1,2,3 を使い、その結果 ドアX は開きました。
- 2 回目のテストでは鍵 2,3 を使い、その結果 ドアX は開きませんした。
各鍵が正しいかダミーかの組み合わせであって、どのテスト結果にも矛盾しないものは以下の 2 通りです。
- 鍵 1 は本物、鍵 2 はダミー、鍵 3 は本物である。
- 鍵 1 は本物、鍵 2 は本物、鍵 3 はダミーである。
入力例 2
4 5 3 3 1 2 3 o 3 2 3 4 o 3 3 4 1 o 3 4 1 2 o 4 1 2 3 4 x
出力例 2
0
問題文中でも述べた通り、答えが 0 通りである場合もあります。
入力例 3
11 4 9 10 1 2 3 4 5 6 7 8 9 10 o 11 1 2 3 4 5 6 7 8 9 10 11 o 10 11 10 9 8 7 6 5 4 3 2 x 10 11 9 1 4 3 7 5 6 2 10 x
出力例 3
8
Score : 300 points
Problem Statement
You have N keys numbered 1, 2, \dots, N.
Some of these are real keys, while the others are dummies.
There is a door, Door X, into which you can insert any number of keys. Door X will open if and only if at least K real keys are inserted.
You have conducted M tests on these keys. The i-th test went as follows:
- You inserted C_i keys A_{i,1}, A_{i,2}, \dots, A_{i,C_i} into Door X.
- The test result is represented by a single English letter R_i.
- R_i =
omeans that Door X opened in the i-th test. - R_i =
xmeans that Door X did not open in the i-th test.
- R_i =
There are 2^N possible combinations of which keys are real and which are dummies. Among these, find the number of combinations that do not contradict any of the test results.
It is possible that the given test results are incorrect and no combination satisfies the conditions. In such a case, report 0.
Constraints
- N, M, K, C_i, and A_{i,j} are integers.
- 1 \le K \le N \le 15
- 1 \le M \le 100
- 1 \le C_i \le N
- 1 \le A_{i,j} \le N
- A_{i,j} \neq A_{i,k} if j \neq k.
- R_i is
oorx.
Input
The input is given from Standard Input in the following format:
N M K
C_1 A_{1,1} A_{1,2} \dots A_{1,C_1} R_1
C_2 A_{2,1} A_{2,2} \dots A_{2,C_2} R_2
\vdots
C_M A_{M,1} A_{M,2} \dots A_{M,C_M} R_M
Output
Print the answer as an integer.
Sample Input 1
3 2 2 3 1 2 3 o 2 2 3 x
Sample Output 1
2
In this input, there are three keys and two tests were conducted.
Two correct keys are required to open Door X.
- In the first test, keys 1, 2, 3 were used, and Door X opened.
- In the second test, keys 2, 3 were used, and Door X did not open.
There are two combinations of which keys are real and which are dummies that do not contradict any of the test results:
- Key 1 is real, key 2 is a dummy, and key 3 is real.
- Key 1 is real, key 2 is real, and key 3 is a dummy.
Sample Input 2
4 5 3 3 1 2 3 o 3 2 3 4 o 3 3 4 1 o 3 4 1 2 o 4 1 2 3 4 x
Sample Output 2
0
As mentioned in the problem statement, the answer may be 0.
Sample Input 3
11 4 9 10 1 2 3 4 5 6 7 8 9 10 o 11 1 2 3 4 5 6 7 8 9 10 11 o 10 11 10 9 8 7 6 5 4 3 2 x 10 11 9 1 4 3 7 5 6 2 10 x
Sample Output 3
8
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
英小文字からなる長さ N の文字列 S が与えられます。
文字列 S に対して操作を Q 回行います。 i 回目 (1\leq i\leq Q) の操作は文字の組 (c _ i,d _ i) で表され、次のような操作に対応します。
- S に含まれる文字 c _ i をすべて文字 d _ i で置き換える。
すべての操作が終わったあとの文字列 S を出力してください。
制約
- 1\leq N\leq2\times10^5
- S は英小文字からなる長さ N の文字列
- 1\leq Q\leq2\times10^5
- c _ i,d _ i は英小文字 (1\leq i\leq Q)
- N,Q は整数
入力
入力は以下の形式で標準入力から与えられる。
N S Q c _ 1 d _ 1 c _ 2 d _ 2 \vdots c _ Q d _ Q
出力
すべての操作が終わったあとの S を出力せよ。
入力例 1
7 atcoder 4 r a t e d v a r
出力例 1
recover
S は atcoder → atcodea → aecodea → aecovea → recover と変化します。
たとえば、4 番目の操作では S={}aecovea に含まれる a (1 文字目と 7 文字目)をすべて r に置き換えるので S={}recover となります。
すべての操作が終わったときには S={}recover となっているため、recover を出力してください。
入力例 2
3 abc 4 a a s k n n z b
出力例 2
abc
c _ i=d _ i であるような操作や S に c _ i が含まれないような操作もあります。
入力例 3
34 supercalifragilisticexpialidocious 20 g c l g g m c m r o s e a a o f f s e t t l d v p k v h x i h n n j i r s i u a
出力例 3
laklimamriiamrmrllrmlrkramrjimrial
Score: 350 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters.
You will perform an operation Q times on the string S. The i-th operation (1\leq i\leq Q) is represented by a pair of characters (c _ i,d _ i), which corresponds to the following operation:
- Replace all occurrences of the character c _ i in S with the character d _ i.
Print the string S after all operations are completed.
Constraints
- 1\leq N\leq2\times10^5
- S is a string of length N consisting of lowercase English letters.
- 1\leq Q\leq2\times10^5
- c _ i and d _ i are lowercase English letters (1\leq i\leq Q).
- N and Q are integers.
Input
The input is given from Standard Input in the following format:
N S Q c _ 1 d _ 1 c _ 2 d _ 2 \vdots c _ Q d _ Q
Output
Print the string S after all operations are completed.
Sample Input 1
7 atcoder 4 r a t e d v a r
Sample Output 1
recover
S changes as follows: atcoder → atcodea → aecodea → aecovea → recover.
For example, in the fourth operation, all occurrences of a in S={}aecovea (the first and seventh characters) are replaced with r, resulting in S={}recover.
After all operations are completed, S={}recover, so print recover.
Sample Input 2
3 abc 4 a a s k n n z b
Sample Output 2
abc
There may be operations where c _ i=d _ i or S does not contain c _ i.
Sample Input 3
34 supercalifragilisticexpialidocious 20 g c l g g m c m r o s e a a o f f s e t t l d v p k v h x i h n n j i r s i u a
Sample Output 3
laklimamriiamrmrllrmlrkramrjimrial
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
AtCoder Land のお土産屋に N 個の箱が売られています。
箱には 1, 2, \ldots, N の番号が付いており、箱 i の価格は A_i 円であり、A_i 個のお菓子が入っています。
高橋君は N 個の箱のうち M 個の箱を選んで買って帰り、1, 2, \ldots, M の名前が付いた M 人の人に 1 つずつ箱を渡そうとしています。
ただし、高橋君は以下の条件を満たすことができるように箱を買いたいです。
- 各 i = 1, 2, \ldots, M について、人 i には B_i 個以上のお菓子が入った箱を渡す
1 人に 2 個以上の箱を渡すことや同じ箱を複数人に渡すことはできないことに注意してください。
適切に箱を M 個買うことで条件を満たすことができるか判定し、条件を満たすことができる場合は高橋君が支払う金額の合計の最小値を求めてください。
制約
- 1 \leq M \leq N \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq 10^9
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \ldots A_N B_1 B_2 \ldots B_M
出力
適切に箱を M 個買うことで条件を満たすことができる場合は高橋君が支払う金額の合計の最小値を出力せよ。そうでない場合は -1 を出力せよ。
入力例 1
4 2 3 4 5 4 1 4
出力例 1
7
高橋君は箱 1, 4 を買い、箱 1 を人 1、箱 4 を人 2 に渡すことで条件を満たすことができます。
このとき高橋君が支払う金額の合計は 7 円であり、支払う金額が 7 円未満のときは条件を満たすことはできないため、7 を出力します。
入力例 2
3 3 1 1 1 1000000000 1000000000 1000000000
出力例 2
-1
入力例 3
7 3 2 6 8 9 5 1 11 3 5 7
出力例 3
19
Score : 350 points
Problem Statement
A souvenir shop at AtCoder Land sells N boxes.
The boxes are numbered 1 to N, and box i has a price of A_i yen and contains A_i pieces of candy.
Takahashi wants to buy M out of the N boxes and give one box each to M people named 1, 2, \ldots, M.
Here, he wants to buy boxes that can satisfy the following condition:
- For each i = 1, 2, \ldots, M, person i is given a box containing at least B_i pieces of candy.
Note that it is not allowed to give more than one box to a single person or to give the same box to multiple people.
Determine whether it is possible to buy M boxes that can satisfy the condition, and if it is possible, find the minimum total amount of money Takahashi needs to pay.
Constraints
- 1 \leq M \leq N \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 A_2 \ldots A_N B_1 B_2 \ldots B_M
Output
If it is possible to buy M boxes that can satisfy the condition, print the minimum total amount of money Takahashi needs to pay. Otherwise, print -1.
Sample Input 1
4 2 3 4 5 4 1 4
Sample Output 1
7
Takahashi can buy boxes 1 and 4, and give box 1 to person 1 and box 4 to person 2 to satisfy the condition.
In this case, he needs to pay 7 yen in total, and it is impossible to satisfy the condition by paying less than 7 yen, so print 7.
Sample Input 2
3 3 1 1 1 1000000000 1000000000 1000000000
Sample Output 2
-1
Sample Input 3
7 3 2 6 8 9 5 1 11 3 5 7
Sample Output 3
19
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
整数列 A とノートがあります。ノートには 10^9 枚のページがあります。
Q 個のクエリが与えられます。各クエリは下記の 4 種類のいずれかです。
ADD x : 整数 x を A の末尾に追加する。
DELETE : A の末尾の要素を削除する。ただし、A が空である場合は何もしない。
SAVE y : ノートの y ページ目に書かれている数列を消し、代わりに現在の A を y ページ目に書き込む。
LOAD z : A をノートの z ページ目に書かれている数列で置き換える。
はじめ、A は空列であり、ノートのすべてのページには空列の情報が書かれています。 その初期状態から、Q 個のクエリを与えられる順に実行し、各クエリの実行直後における A の末尾の要素を出力してください。
なお、入出力の量が多くなる場合があるので、高速な方法で入出力を行うことを推奨します。
制約
- 1 \leq Q \leq 5 \times 10^5
- 1 \leq x, y, z \leq 10^9
- Q, x, y, z は整数
- 与えられるクエリは問題文中の 4 種類のいずれか
入力
入力は以下の形式で標準入力から与えられる。
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
出力
下記の形式にしたがい、i = 1, 2, \ldots, Q について、i 番目までのクエリを実行した直後の A の末尾の要素 X_i を( A が空の場合は X_i := -1 とする)出力せよ。
X_1 X_2 \ldots X_Q
入力例 1
11 ADD 3 SAVE 1 ADD 4 SAVE 2 LOAD 1 DELETE DELETE LOAD 2 SAVE 1 LOAD 3 LOAD 1
出力例 1
3 3 4 4 3 -1 -1 4 4 -1 4
はじめ、A は空列、すなわち A = () であり、ノートのすべてのページには空列の情報が書かれています。
- 1 番目のクエリによって、 A の末尾に 3 が追加され、A = (3) となります。
- 2 番目のクエリによって、ノートの 1 ページ目に書かれた数列が (3) になります。A は変わらず A = (3) です。
- 3 番目のクエリによって、 A の末尾に 4 が追加され、A = (3, 4) となります。
- 4 番目のクエリによって、ノートの 2 ページ目に書かれた数列が (3, 4) になります。A は変わらず A = (3, 4) です。
- 5 番目のクエリによって、 A がノートの 1 ページ目に書かれた数列 (3) で置き換えられ、A = (3) となります。
- 6 番目のクエリによって、 A の末尾の要素が削除され、A = () となります。
- 7 番目のクエリでは、A がすでに空であるので何もしません。A は変わらず A = () です。
- 8 番目のクエリによって、 A がノートの 2 ページ目に書かれた数列 (3, 4) で置き換えられ、A = (3, 4) となります。
- 9 番目のクエリによって、ノートの 1 ページ目に書かれた数列が (3, 4) になります。A は変わらず A = (3, 4) です。
- 10 番目のクエリによって、 A がノートの 3 ページ目に書かれた数列 () で置き換えられ、A = () となります。
- 11 番目のクエリによって、 A がノートの 1 ページ目に書かれた数列 (3, 4) で置き換えられ、A = (3, 4) となります。
入力例 2
21 ADD 4 ADD 3 DELETE ADD 10 LOAD 7 SAVE 5 SAVE 5 ADD 4 ADD 4 ADD 5 SAVE 5 ADD 2 DELETE ADD 1 SAVE 5 ADD 7 ADD 8 DELETE ADD 4 DELETE LOAD 5
出力例 2
4 3 4 10 -1 -1 -1 4 4 5 5 2 5 1 1 7 8 7 4 7 1
Score : 500 points
Problem Statement
We have an integer sequence A and a notebook. The notebook has 10^9 pages.
You are given Q queries. Each query is of one of the following four kinds:
ADD x: append an integer x to the tail of A.
DELETE: remove the last term of A if A is not empty; do nothing otherwise.
SAVE y: erase the sequence recorded on the y-th page of the notebook, and record the current A onto the y-th page.
LOAD z: replace A with the sequence recorded on the z-th page of the notebook.
Initially, A is an empty sequence, and an empty sequence is recorded on each page of the notebook. Process Q queries successively in the given order and print the last term of A after processing each query.
The use of fast input and output methods is recommended because of potentially large input and output.
Constraints
- 1 \leq Q \leq 5 \times 10^5
- 1 \leq x, y, z \leq 10^9
- Q, x, y, and z are integers.
- Each of the given queries is of one of the four kinds in the Problem Statement.
Input
The input is given from Standard Input in the following format:
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Output
For each i = 1, 2, \ldots, Q, let X_i be the last element of A after processing up to the i-th query, or let X_i := -1 if A is empty, and print them in the following format:
X_1 X_2 \ldots X_Q
Sample Input 1
11 ADD 3 SAVE 1 ADD 4 SAVE 2 LOAD 1 DELETE DELETE LOAD 2 SAVE 1 LOAD 3 LOAD 1
Sample Output 1
3 3 4 4 3 -1 -1 4 4 -1 4
Initially, A is an empty sequence, so A = (), and an empty sequence is recorded on each page of the notebook.
- By the 1-st query, 3 is appended to the tail of A, resulting in A = (3).
- By the 2-nd query, the sequence recorded on the 1-st page of the notebook becomes (3). It remains that A = (3).
- By the 3-rd query, 4 is appended to the tail of A, resulting in A = (3, 4).
- By the 4-th query, the sequence recorded on the 2-nd page of the notebook becomes (3, 4). It remains that A = (3, 4).
- By the 5-th query, A is replaced by (3), which is recorded on the 1-st page of the notebook, resulting in A = (3).
- By the 6-th query, the last term of A is removed, resulting in A = ().
- By the 7-th query, nothing happens because A is already empty. It remains that A = ().
- By the 8-th query, A is replaced by (3,4), which is recorded on the 2-nd page of the notebook, resulting in A = (3, 4).
- By the 9-th query, the sequence recorded on the 1-st page of the notebook becomes (3, 4). It remains that A = (3, 4).
- By the 10-th query, A is replaced by (), which is recorded on the 3-rd page of the notebook, resulting in A = ().
- By the 11-th query, A is replaced by (3, 4), which is recorded on the 1-st page of the notebook, resulting in A = (3, 4).
Sample Input 2
21 ADD 4 ADD 3 DELETE ADD 10 LOAD 7 SAVE 5 SAVE 5 ADD 4 ADD 4 ADD 5 SAVE 5 ADD 2 DELETE ADD 1 SAVE 5 ADD 7 ADD 8 DELETE ADD 4 DELETE LOAD 5
Sample Output 2
4 3 4 10 -1 -1 -1 4 4 5 5 2 5 1 1 7 8 7 4 7 1
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 頂点の木が与えられます。 i = 1, 2, \ldots, N-1 について、i 番目の辺は頂点 u_i と頂点 v_i を結ぶ重み w_i の辺です。
N-1 本の辺のうちのいくつか( 0 本または N-1 本すべてでも良い)を選ぶことを考えます。 ただし、i = 1, 2, \ldots, N について、頂点 i に接続する辺は d_i 本までしか選べません。 選ぶ辺の重みの総和としてあり得る最大値を求めてください。
制約
- 2 \leq N \leq 3 \times 10^5
- 1 \leq u_i, v_i \leq N
- -10^9 \leq w_i \leq 10^9
- d_i は頂点 i の次数以下の非負整数
- 与えられるグラフは木である
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N
d_1 d_2 \ldots d_N
u_1 v_1 w_1
u_2 v_2 w_2
\vdots
u_{N-1} v_{N-1} w_{N-1}
出力
答えを出力せよ。
入力例 1
7 1 2 1 0 2 1 1 1 2 8 2 3 9 2 4 10 2 5 -3 5 6 8 5 7 3
出力例 1
28
1, 2, 5, 6 番目の辺を選ぶと、選ぶ辺の重みは 8 + 9 + 8 + 3 = 28 となります。これがあり得る最大値です。
入力例 2
20 0 2 0 1 2 1 0 0 3 0 1 1 1 1 0 0 3 0 1 2 4 9 583 4 6 -431 5 9 325 17 6 131 17 2 -520 2 16 696 5 7 662 17 15 845 7 8 307 13 7 849 9 19 242 20 6 909 7 11 -775 17 18 557 14 20 95 18 10 646 4 3 -168 1 3 -917 11 12 30
出力例 2
2184
Score : 500 points
Problem Statement
You are given a tree with N vertices. For each i = 1, 2, \ldots, N-1, the i-th edge connects Vertex u_i and Vertex v_i and has a weight w_i.
Consider choosing some of the N-1 edges (possibly none or all). Here, for each i = 1, 2, \ldots, N, one may choose at most d_i edges incident to Vertex i. Find the maximum possible total weight of the chosen edges.
Constraints
- 2 \leq N \leq 3 \times 10^5
- 1 \leq u_i, v_i \leq N
- -10^9 \leq w_i \leq 10^9
- d_i is a non-negative integer not exceeding the degree of Vertex i.
- The given graph is a tree.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
d_1 d_2 \ldots d_N
u_1 v_1 w_1
u_2 v_2 w_2
\vdots
u_{N-1} v_{N-1} w_{N-1}
Output
Print the answer.
Sample Input 1
7 1 2 1 0 2 1 1 1 2 8 2 3 9 2 4 10 2 5 -3 5 6 8 5 7 3
Sample Output 1
28
If you choose the 1-st, 2-nd, 5-th, and 6-th edges, the total weight of those edges is 8 + 9 + 8 + 3 = 28. This is the maximum possible.
Sample Input 2
20 0 2 0 1 2 1 0 0 3 0 1 1 1 1 0 0 3 0 1 2 4 9 583 4 6 -431 5 9 325 17 6 131 17 2 -520 2 16 696 5 7 662 17 15 845 7 8 307 13 7 849 9 19 242 20 6 909 7 11 -775 17 18 557 14 20 95 18 10 646 4 3 -168 1 3 -917 11 12 30
Sample Output 2
2184