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
配点 : 100 点
問題文
高橋君は N 歳の誕生日を迎えました。この時の彼の身長は T cmです。
また、以下のことが分かっています。
- 高橋君は 0 歳の誕生日(生まれた当日)から X 歳の誕生日までの間、毎年身長が D cmずつ伸びた。より厳密に書くと、i=1,2,\ldots,X それぞれに対し、i-1 歳の誕生日から i 歳の誕生日までの間に身長が D cm伸びた。
- 高橋君は X 歳の誕生日から N 歳の誕生日までの間、身長が変化していない。
高橋君の M 歳の誕生日の時の身長が何cmだったかを求めてください。
制約
- 0 \leq M \lt N \leq 100
- 1 \leq X \leq N
- 1 \leq T \leq 200
- 1 \leq D \leq 100
- 高橋君の 0 歳の誕生日の時の身長は 1 cm以上である
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M X T D
出力
答えを整数として出力せよ。
入力例 1
38 20 17 168 3
出力例 1
168
この例では、高橋君の 38 歳の誕生日の時の身長が 168 cmです。また、17 歳の誕生日から 38 歳の誕生日までの間、身長が変化していません。
このことから、20 歳の誕生日の時の身長は 168 cmだったと言え、これが答えになります。
入力例 2
1 0 1 3 2
出力例 2
1
この例において、高橋君は 0(=M) 歳の誕生日の時の身長が 1 cmで、1(=N) 歳の誕生日の時の身長が 3(=T) cmです。
入力例 3
100 10 100 180 1
出力例 3
90
Score : 100 points
Problem Statement
Takahashi had his N-th birthday, when he was T centimeters tall.
Additionally, we know the following facts:
- In each year between Takahashi's birth (0-th birthday) and his X-th birthday, his height increased by D centimeters. More formally, for each i = 1, 2, \ldots, X, his height increased by D centimeters between his (i-1)-th birthday and his i-th birthday.
- Between Takahashi's X-th birthday and his N-th birthday, his height did not change.
Find Takahashi's height on his M-th birthday, in centimeters.
Constraints
- 0 \leq M \lt N \leq 100
- 1 \leq X \leq N
- 1 \leq T \leq 200
- 1 \leq D \leq 100
- Takahashi was at least 1 centimeter tall at his birth.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M X T D
Output
Print the answer as an integer.
Sample Input 1
38 20 17 168 3
Sample Output 1
168
In this sample, Takahashi was 168 centimeters tall on his 38-th birthday. Also, his height did not change between his 17-th birthday and 38-th birthday.
From these facts, we find that he was 168 centimeters tall on his 20-th birthday, so the answer is 168.
Sample Input 2
1 0 1 3 2
Sample Output 2
1
In this sample, Takahashi was 1 centimeter tall on his 0(=M)-th birthday and 3(=T) centimeters tall on his 1(=N)-st birthday.
Sample Input 3
100 10 100 180 1
Sample Output 3
90
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
縦 H 行、横 W 列のマス目があり、各マスには 1 つの整数が書かれています。 上から i 行目、左から j 列目のマスに書かれている整数は A_{i, j} です。
マス目が下記の条件を満たすかどうかを判定してください。
1 \leq i_1 < i_2 \leq H および 1 \leq j_1 < j_2 \leq W を満たすすべての整数の組 (i_1, i_2, j_1, j_2) について、A_{i_1, j_1} + A_{i_2, j_2} \leq A_{i_2, j_1} + A_{i_1, j_2} が成り立つ。
制約
- 2 \leq H, W \leq 50
- 1 \leq A_{i, j} \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W
A_{1, 1} A_{1, 2} \cdots A_{1, W}
A_{2, 1} A_{2, 2} \cdots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \cdots A_{H, W}
出力
マス目が問題文中の条件を満たす場合は Yes と出力し、条件を満たさない場合は No と出力せよ。
入力例 1
3 3 2 1 4 3 1 3 6 4 1
出力例 1
Yes
1 \leq i_1 < i_2 \leq H および 1 \leq j_1 < j_2 \leq W を満たす整数の組 (i_1, i_2, j_1, j_2) は 9 個存在し、それらすべてについて A_{i_1, j_1} + A_{i_2, j_2} \leq A_{i_2, j_1} + A_{i_1, j_2} が成り立ちます。例えば、
- (i_1, i_2, j_1, j_2) = (1, 2, 1, 2) について、A_{i_1, j_1} + A_{i_2, j_2} = 2 + 1 \leq 3 + 1 = A_{i_2, j_1} + A_{i_1, j_2}
- (i_1, i_2, j_1, j_2) = (1, 2, 1, 3) について、A_{i_1, j_1} + A_{i_2, j_2} = 2 + 3 \leq 3 + 4 = A_{i_2, j_1} + A_{i_1, j_2}
- (i_1, i_2, j_1, j_2) = (1, 2, 2, 3) について、A_{i_1, j_1} + A_{i_2, j_2} = 1 + 3 \leq 1 + 4 = A_{i_2, j_1} + A_{i_1, j_2}
- (i_1, i_2, j_1, j_2) = (1, 3, 1, 2) について、A_{i_1, j_1} + A_{i_2, j_2} = 2 + 4 \leq 6 + 1 = A_{i_2, j_1} + A_{i_1, j_2}
- (i_1, i_2, j_1, j_2) = (1, 3, 1, 3) について、A_{i_1, j_1} + A_{i_2, j_2} = 2 + 1 \leq 6 + 4 = A_{i_2, j_1} + A_{i_1, j_2}
が成り立ちます。残りの (i_1, i_2, j_1, j_2) = (1, 3, 2, 3), (2, 3, 1, 2), (2, 3, 1, 3), (2, 3, 2, 3) についても同様に確認できます。
よって、Yes を出力します。
入力例 2
2 4 4 3 2 1 5 6 7 8
出力例 2
No
問題文中の条件を満たさないので、No を出力します。
例えば、(i_1, i_2, j_1, j_2) = (1, 2, 1, 4) について、A_{i_1, j_1} + A_{i_2, j_2} = 4 + 8 > 5 + 1 = A_{i_2, j_1} + A_{i_1, j_2} です。
Score : 200 points
Problem Statement
We have a grid with H horizontal rows and W vertical columns, where each square contains an integer. The integer written on the square at the i-th row from the top and j-th column from the left is A_{i, j}.
Determine whether the grid satisfies the condition below.
A_{i_1, j_1} + A_{i_2, j_2} \leq A_{i_2, j_1} + A_{i_1, j_2} holds for every quadruple of integers (i_1, i_2, j_1, j_2) such that 1 \leq i_1 < i_2 \leq H and 1 \leq j_1 < j_2 \leq W.
Constraints
- 2 \leq H, W \leq 50
- 1 \leq A_{i, j} \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
H W
A_{1, 1} A_{1, 2} \cdots A_{1, W}
A_{2, 1} A_{2, 2} \cdots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \cdots A_{H, W}
Output
If the grid satisfies the condition in the Problem Statement, print Yes; otherwise, print No.
Sample Input 1
3 3 2 1 4 3 1 3 6 4 1
Sample Output 1
Yes
There are nine quadruples of integers (i_1, i_2, j_1, j_2) such that 1 \leq i_1 < i_2 \leq H and 1 \leq j_1 < j_2 \leq W. For all of them, A_{i_1, j_1} + A_{i_2, j_2} \leq A_{i_2, j_1} + A_{i_1, j_2} holds. Some examples follow.
- For (i_1, i_2, j_1, j_2) = (1, 2, 1, 2), we have A_{i_1, j_1} + A_{i_2, j_2} = 2 + 1 \leq 3 + 1 = A_{i_2, j_1} + A_{i_1, j_2}.
- For (i_1, i_2, j_1, j_2) = (1, 2, 1, 3), we have A_{i_1, j_1} + A_{i_2, j_2} = 2 + 3 \leq 3 + 4 = A_{i_2, j_1} + A_{i_1, j_2}.
- For (i_1, i_2, j_1, j_2) = (1, 2, 2, 3), we have A_{i_1, j_1} + A_{i_2, j_2} = 1 + 3 \leq 1 + 4 = A_{i_2, j_1} + A_{i_1, j_2}.
- For (i_1, i_2, j_1, j_2) = (1, 3, 1, 2), we have A_{i_1, j_1} + A_{i_2, j_2} = 2 + 4 \leq 6 + 1 = A_{i_2, j_1} + A_{i_1, j_2}.
- For (i_1, i_2, j_1, j_2) = (1, 3, 1, 3), we have A_{i_1, j_1} + A_{i_2, j_2} = 2 + 1 \leq 6 + 4 = A_{i_2, j_1} + A_{i_1, j_2}.
We can also see that the property holds for the other quadruples: (i_1, i_2, j_1, j_2) = (1, 3, 2, 3), (2, 3, 1, 2), (2, 3, 1, 3), (2, 3, 2, 3).
Thus, we should print Yes.
Sample Input 2
2 4 4 3 2 1 5 6 7 8
Sample Output 2
No
We should print No because the condition is not satisfied.
This is because, for example, we have A_{i_1, j_1} + A_{i_2, j_2} = 4 + 8 > 5 + 1 = A_{i_2, j_1} + A_{i_1, j_2} for (i_1, i_2, j_1, j_2) = (1, 2, 1, 4).
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
1 から N の番号がついた N 人の人がいます。
人 i はキーエンス本社ビルの建築面積を S_i 平方メートルであると予想しました。
キーエンス本社ビルは下図のような形をしています。ただし、a,b はある 正の整数 です。
つまり、キーエンス本社ビルの建築面積は 4ab+3a+3b 平方メートルと表されます。
N 人のうち、この情報のみによって、予想した面積が確実に誤りであるとわかる人数を求めてください。

制約
- 1 \leq N \leq 20
- 1 \leq S_i \leq 1000
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N S_1 \ldots S_N
出力
答えを出力せよ。
入力例 1
3 10 20 39
出力例 1
1
a=1,b=1 のとき面積は 10 平方メートル、a=2,b=3 のとき面積は 39 平方メートルとなります。
しかし a,b がどのような正の整数であったとしても面積が 20 平方メートルになることはありません。
よって、人 2 の予想だけは確実に誤りであることがわかります。
入力例 2
5 666 777 888 777 666
出力例 2
3
Score : 200 points
Problem Statement
There are N people numbered 1 to N.
Person i guessed the building area of KEYENCE headquarters building to be S_i square meters.
The shape of KEYENCE headquarters building is shown below, where a and b are some positive integers.
That is, the building area of the building can be represented as 4ab+3a+3b.
Based on just this information, how many of the N people are guaranteed to be wrong in their guesses?

Constraints
- 1 \leq N \leq 20
- 1 \leq S_i \leq 1000
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N S_1 \ldots S_N
Output
Print the answer.
Sample Input 1
3 10 20 39
Sample Output 1
1
The area would be 10 square meters if a=1,b=1, and 39 square meters if a=2,b=3.
However, no pair of positive integers a and b would make the area 20 square meters.
Thus, we can only be sure that Person 2 guessed wrong.
Sample Input 2
5 666 777 888 777 666
Sample Output 2
3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
2 つの文字列 A,B に対して、A の末尾に B を連結した文字列を A+B と表します。
N 個の文字列 S_1,\ldots,S_N が与えられます。i=1,\ldots,N の順に、次の指示に従って加工して出力してください。
- S_1,\ldots,S_{i-1} の中に S_i と同じ文字列が存在しないならば、S_i を出力する。
- S_1,\ldots,S_{i-1} の中に S_i と同じ文字列が X 個 (X>0) 存在するならば、X を文字列として扱って S_i+
(+X+)を出力する。
制約
- 1 \leq N \leq 2\times 10^5
- S_i は英小文字のみからなる長さ 1 以上 10 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
問題文中の指示にしたがって、N 行出力せよ。
入力例 1
5 newfile newfile newfolder newfile newfolder
出力例 1
newfile newfile(1) newfolder newfile(2) newfolder(1)
入力例 2
11 a a a a a a a a a a a
出力例 2
a a(1) a(2) a(3) a(4) a(5) a(6) a(7) a(8) a(9) a(10)
Score : 300 points
Problem Statement
For two strings A and B, let A+B denote the concatenation of A and B in this order.
You are given N strings S_1,\ldots,S_N. Modify and print them as follows, in the order i=1, \ldots, N:
- if none of S_1,\ldots,S_{i-1} is equal to S_i, print S_i;
- if X (X>0) of S_1,\ldots,S_{i-1} are equal to S_i, print S_i+
(+X+), treating X as a string.
Constraints
- 1 \leq N \leq 2\times 10^5
- S_i is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print N lines as specified in the Problem Statement.
Sample Input 1
5 newfile newfile newfolder newfile newfolder
Sample Output 1
newfile newfile(1) newfolder newfile(2) newfolder(1)
Sample Input 2
11 a a a a a a a a a a a
Sample Output 2
a a(1) a(2) a(3) a(4) a(5) a(6) a(7) a(8) a(9) a(10)
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の非負整数列 A が与えられます。
A から K 要素を選んで順序を保ったまま抜き出した列を B としたとき、 MEX(B) としてありえる最大値を求めてください。
但し、数列 X に対して MEX(X) は以下の条件を満たす唯一の非負整数 m として定義されます。
- 0 \le i < m を満たす全ての整数 i が X に含まれる。
- m が X に含まれない。
制約
- 入力は全て整数
- 1 \le K \le N \le 3 \times 10^5
- 0 \le A_i \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
7 3 2 0 2 3 2 1 9
出力例 1
3
この入力では A=(2,0,2,3,2,1,9) であり、ここから K=3 要素を選んで抜き出して数列 B を得ます。例えば、
- 1,2,3 要素目を抜き出した時、 MEX(B)=MEX(2,0,2)=1
- 3,4,6 要素目を抜き出した時、 MEX(B)=MEX(2,3,1)=0
- 2,6,7 要素目を抜き出した時、 MEX(B)=MEX(0,1,9)=2
- 2,3,6 要素目を抜き出した時、 MEX(B)=MEX(0,2,1)=3
のようになります。
達成可能な MEX の最大値は 3 です。
Score : 300 points
Problem Statement
You are given a length-N sequence of non-negative integers.
When B is a sequence obtained by choosing K elements from A and concatenating them without changing the order, find the maximum possible value of MEX(B).
Here, for a sequence X, we define MEX(X) as the unique non-negative integer m that satisfies the following conditions:
- Every integer i such that 0 \le i < m is contained in X.
- m is not contained in X.
Constraints
- All values in the input are integers.
- 1 \le K \le N \le 3 \times 10^5
- 0 \le A_i \le 10^9
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \dots A_N
Output
Print the answer.
Sample Input 1
7 3 2 0 2 3 2 1 9
Sample Output 1
3
In this input, A=(2,0,2,3,2,1,9), and you obtain the sequence B by picking K=3 elements. For example,
- If the 1-st, 2-nd, and 3-rd elements are chosen, MEX(B)=MEX(2,0,2)=1.
- If the 3-rd, 4-th, and 6-th elements are chosen, MEX(B)=MEX(2,3,1)=0.
- If the 2-nd, 6-th, and 7-th elements are chosen, MEX(B)=MEX(0,1,9)=2.
- If the 2-nd, 3-rd, and 6-th elements are chosen, MEX(B)=MEX(0,2,1)=3.
The maximum possible MEX is 3.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
H 行 W 列のグリッドがあります。上から i 行目、左から j 列目のマス目を (i,j) と表します。各マスの状態は文字 A_{i,j} で表され、意味は以下の通りです。
.:空きマス。#:障害物マス。S:スタートマス。G:ゴールマス。o:開いたドアのマス。x:閉じたドアのマス。?:スイッチマス。
高橋君は、 1 回の操作で今いるマスから上下左右に隣り合う、障害物マスでも閉じたドアでもないマスへ移動することができます。
また、スイッチマスに移動する度に全ての開いたドアのマスは閉じたドアのマスに、閉じたドアのマスは開いたドアのマスに変わります。
高橋君がはじめスタートマスにいる状態からゴールマスにいる状態にするよう操作できるか判定し、可能な場合は必要な操作回数の最小値を求めてください。
制約
- 1\le H,W \le 500
- H,W は整数
- A_{i,j} は
.,#,S,G,o,x,?のいずれか SとGは A_{i,j} にそれぞれちょうど 1 つずつ存在する
入力
入力は以下の形式で標準入力から与えられる。
H W
A_{1,1}A_{1,2}\cdots A_{1,W}
A_{2,1}A_{2,2}\cdots A_{2,W}
\vdots
A_{H,1}A_{H,2}\cdots A_{H,W}
出力
高橋君がはじめスタートマスにいる状態からゴールマスにいる状態するよう操作できる場合は必要な操作回数の最小値を、できない場合は -1 を出力せよ。
入力例 1
2 4 S.xG #?o.
出力例 1
5
(1,1) から順に (1,2),(2,2),(1,2),(1,3),(1,4) と移動するよう操作することで 5 回の操作でゴールマスにいる状態にすることができます。
入力例 2
1 5 So?oG
出力例 2
-1
どのように操作してもゴールマスにいる状態にすることはできません。したがって、 -1 を出力してください。
入力例 3
5 5 Sx?x? o#o#x ?o?o? x#x#o ?x?oG
出力例 3
10
Score : 400 points
Problem Statement
There is a grid with H rows and W columns. Let (i,j) denote the cell at the i-th row from the top and j-th column from the left. The state of each cell is represented by a character A_{i,j}, with the following meanings:
.: Empty cell.#: Obstacle cell.S: Start cell.G: Goal cell.o: Open door cell.x: Closed door cell.?: Switch cell.
Takahashi can move from his current cell to an adjacent cell (up, down, left, right) that is neither an obstacle cell nor a closed door cell in one operation.
Also, every time he moves to a switch cell, all open door cells become closed door cells, and all closed door cells become open door cells.
Determine whether he can move from the initial state of being at the start cell to being at the goal cell, and if possible, find the minimum number of operations required.
Constraints
- 1\le H,W \le 500
- H and W are integers.
- Each A_{i,j} is one of
.,#,S,G,o,x,?. SandGeach appear exactly once in A_{i,j}.
Input
The input is given from Standard Input in the following format:
H W
A_{1,1}A_{1,2}\cdots A_{1,W}
A_{2,1}A_{2,2}\cdots A_{2,W}
\vdots
A_{H,1}A_{H,2}\cdots A_{H,W}
Output
If Takahashi can move from the initial state of being at the start cell to being at the goal cell, output the minimum number of operations required; otherwise, output -1.
Sample Input 1
2 4 S.xG #?o.
Sample Output 1
5
By moving from (1,1) to (1,2),(2,2),(1,2),(1,3),(1,4) in that order, he can reach the goal cell in five operations.
Sample Input 2
1 5 So?oG
Sample Output 2
-1
No matter how he operates, he cannot reach the goal cell. Therefore, output -1.
Sample Input 3
5 5 Sx?x? o#o#x ?o?o? x#x#o ?x?oG
Sample Output 3
10
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
1, \dots, N と番号づけられた N 個の都市と、1, \dots, M と番号づけられた M 本の道があります。
全ての道は一方通行であり、道 i \, (1 \leq i \leq M) を通ると、都市 A_i から都市 B_i へ移動することができます。また、道 i の長さは C_i です。
1 以上 M 以下の整数からなる長さ K の数列 E = (E_1, \dots, E_K) が与えられます。都市 1 から都市 N までいくつかの道を使って移動する方法であって、以下の条件を満たすものを良い経路と呼びます。
- 通る道の番号を通った順番に並べた列は、E の部分列である。
なお、部分列とは、数列から 0 個以上の要素を削除し、残った要素を元の順序で並べて得られる数列のことを指します。
全ての良い経路における、通る道の長さの合計の最小値を求めてください。
ただし、良い経路が存在しない場合は、そのことを報告してください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M, K \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq N, A_i \neq B_i \, (1 \leq i \leq M)
- 1 \leq C_i \leq 10^9 \, (1 \leq i \leq M)
- 1 \leq E_i \leq M \, (1 \leq i \leq K)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M K A_1 B_1 C_1 \vdots A_M B_M C_M E_1 \ldots E_K
出力
全ての良い経路における、通る道の長さの合計の最小値を出力せよ。ただし、良い経路が存在しないならば、-1 を出力せよ。
入力例 1
3 4 4 1 2 2 2 3 2 1 3 3 1 3 5 4 2 1 2
出力例 1
4
良い経路として考えられるのは次の二つです。
- 道 4 を使う。通る道の長さの合計は 5 である。
- 道 1, 2 をこの順で使う。通る道の長さの合計は 2 + 2 = 4 である。
よって、求める最小値は 4 です。
入力例 2
3 2 3 1 2 1 2 3 1 2 1 1
出力例 2
-1
良い経路は存在しません。
入力例 3
4 4 5 3 2 2 1 3 5 2 4 7 3 4 10 2 4 1 4 3
出力例 3
14
Score : 500 points
Problem Statement
There are N towns numbered 1, \dots, N, and M roads numbered 1, \dots, M.
Every road is directed; road i (1 \leq i \leq M) leads you from Town A_i to Town B_i. The length of road i is C_i.
You are given a sequence E = (E_1, \dots, E_K) of length K consisting of integers between 1 and M. A way of traveling from town 1 to town N using roads is called a good path if:
- the sequence of the roads' numbers arranged in the order used in the path is a subsequence of E.
Note that a subsequence of a sequence is a sequence obtained by removing 0 or more elements from the original sequence and concatenating the remaining elements without changing the order.
Find the minimum sum of the lengths of the roads used in a good path.
If there is no good path, report that fact.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq M, K \leq 2 \times 10^5
- 1 \leq A_i, B_i \leq N, A_i \neq B_i \, (1 \leq i \leq M)
- 1 \leq C_i \leq 10^9 \, (1 \leq i \leq M)
- 1 \leq E_i \leq M \, (1 \leq i \leq K)
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M K A_1 B_1 C_1 \vdots A_M B_M C_M E_1 \ldots E_K
Output
Find the minimum sum of the lengths of the roads used in a good path.
If there is no good path, print -1.
Sample Input 1
3 4 4 1 2 2 2 3 2 1 3 3 1 3 5 4 2 1 2
Sample Output 1
4
There are two possible good paths as follows:
- Using road 4. In this case, the sum of the length of the used road is 5.
- Using road 1 and 2 in this order. In this case, the sum of the lengths of the used roads is 2 + 2 = 4.
Therefore, the desired minimum value is 4.
Sample Input 2
3 2 3 1 2 1 2 3 1 2 1 1
Sample Output 2
-1
There is no good path.
Sample Input 3
4 4 5 3 2 2 1 3 5 2 4 7 3 4 10 2 4 1 4 3
Sample Output 3
14
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
1 以上 N 以下の正整数 x であって、ある正整数 a と 2 以上の 正整数 b を用いて x=a^b と表現できるものはいくつありますか?
制約
- 入力は全て整数
- 1 \le N \le 10^{18}
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを整数として出力せよ。
入力例 1
99
出力例 1
12
問題文中の条件を満たす整数は 1,4,8,9,16,25,27,32,36,49,64,81 の 12 個です。
入力例 2
1000000000000000000
出力例 2
1001003332
Score : 500 points
Problem Statement
How many integers x between 1 and N, inclusive, can be expressed as x = a^b using some positive integer a and a positive integer b not less than 2?
Constraints
- All input values are integers.
- 1 \le N \le 10^{18}
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer as an integer.
Sample Input 1
99
Sample Output 1
12
The integers that satisfy the conditions in the problem statement are 1, 4, 8, 9, 16, 25, 27, 32, 36, 49, 64, 81: there are 12.
Sample Input 2
1000000000000000000
Sample Output 2
1001003332