A - check digit

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 9

注意

この問題に対する言及は、2021/7/17 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

高橋君は、15 桁の数字で作られたコード S を発行しました。 S0, \ldots , 9 のみからなる、長さ 15 の文字列です。

この 15 桁のコードの、一番右の桁は、チェックディジットとなっており、残りの 14 桁から計算することが可能です。

まず、左の 14 桁のうち、左から奇数番目の数 (一番左の数を 1 番目と数えます) をすべて足し、その値を 3 倍します。 次に、こうして得られた値に、左から偶数番目の数をすべて足します。その値を 10 で割った余りが 15 桁目の数と一致するのが正しいコードで、一致しないのは正しくないコードです。

高橋君の代わりに、コード S が正しいかどうか判定するプログラムを作成してください。

制約

  • S の長さは 15
  • S0, \ldots , 9 のみからなる

入力

入力は以下の形式で標準入力から与えられる。

S

出力

コード S が正しい場合は Yes を、正しくない場合は No を出力せよ。


入力例 1

123451234512345

出力例 1

No

左の 14 桁のうち、奇数番目の数を足すと、1+3+5+2+4+1+3=19 です。これを 3 倍すると 57 です。

57 に偶数番目の数を足すと、57+2+4+1+3+5+2+4=78 となります。

7810 で割った余りは 8 であり、15 桁目である 5 と一致しません。


入力例 2

123451234512348

出力例 2

Yes

Score : 9 points

Warning

Do not make any mention of this problem until July 17, 2021, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Takahashi has issued a 15-digit code S, which is a string of length 15 consisting of 0, \ldots, 9.

The leftmost digit of this 15-digit code is a check digit, which can be computed from the other 14 digits.

To do this, we first find the sum of all odd-positioned digits in the first 14 digits, that is, the 1-st (leftmost), 3-rd, 5-th, \ldots, digits from the left, and multiply this sum by 3. To this multiplied value, we then add all even-positioned digits from the left in the code. Finally, we divide the result of these additions by 10, and if the remainder matches the 15-th digit, the code is valid; otherwise, the code is invalid.

Take Takahashi's place to write a program that determines whether the code S is valid.

Constraints

  • S has a length of 15.
  • S consists of 0, \ldots , 9.

Input

Input is given from Standard Input in the following format:

S

Output

If the code S is valid, print Yes; otherwise, print No.


Sample Input 1

123451234512345

Sample Output 1

No

The sum of all odd-positioned digits in the first 14 digits is 1+3+5+2+4+1+3=19, which will be 57 after multiplying by 3.

Adding all even-positioned digits to 57 results in 57+2+4+1+3+5+2+4=78.

The remainder when dividing 78 by 10 is 8, which does not match the 15-th digit: 5.


Sample Input 2

123451234512348

Sample Output 2

Yes
B - Vapor Pressure

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 8

注意

この問題に対する言及は、2021/7/17 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

A 個のボールと B 個の風船があります。 高橋君はボールの数が風船の数の C 倍以下になるまで 1 つずつボールを取り除いていきます。 最終的に残ったボールの数を風船の数で割った値を求めてください。

制約

  • 1 \leq A,B,C \leq 10^4
  • A,B,C は整数

入力

入力は以下の形式で標準入力から与えられる。

A B C

出力

答えを出力せよ。
なお、想定解答との絶対誤差または相対誤差が 10^{-6} 以下であれば正解として扱われる。


入力例 1

8 3 2

出力例 1

2.000000000000000

最初ボールは 8 個あります。
高橋君はボールが 3\times 2=6 個以下になるまでボールを取り除き、最終的にボールは 6 個残ります。
よって、答えは \frac{6}{3}=2 となります。


入力例 2

8 5 2

出力例 2

1.600000000000000

最初の時点で 8\leq 5\times 2 であるので高橋君はボールを 1 つも取り除きません。
よって、答えは \frac{8}{5}=1.6 となります。

Score : 8 points

Warning

Do not make any mention of this problem until July 17, 2021, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

We have A balls and B balloons. Takahashi will repeatedly remove one ball until the number of balls is at most C times the number of balloons. Find the number of balls that will eventually remain, divided by the number of balloons.

Constraints

  • 1 \leq A,B,C \leq 10^4
  • A, B, and C are integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the answer.
It will be considered correct when its absolute or relative error from our answer is at most 10^{-6}.


Sample Input 1

8 3 2

Sample Output 1

2.000000000000000

Initially, we have 8 balls.
Takahashi will repeatedly remove a ball until we have not more than 6 balls, after which we will have 6 balls.
Thus, the answer is \frac{6}{3}=2.


Sample Input 2

8 5 2

Sample Output 2

1.600000000000000

We already have 8\leq 5\times 2 in the beginning, so Takahashi will not remove any ball.
Thus, the answer is \frac{8}{5}=1.6.

C - Validation

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 8

注意

この問題に対する言及は、2021/7/17 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

0 から 9 の数字からなる文字列 S が与えられます。
S を整数の十進数表示として見たとき、以下の 2 つの条件をともに満たすかどうか判定してください。

  • 先頭に不要な 0 がない。
  • L 以上 R 以下である。

制約

  • S0 から 9 の数字からなる文字列
  • 1 \leq |S| \leq 100
  • 0 \leq L \leq R \leq 10^9
  • LR は整数

入力

入力は以下の形式で標準入力から与えられる。

S
L R

出力

S が問題文に示す 2 つの条件をともに満たすならば Yes と出力し、 そうでなければ No と出力せよ。


入力例 1

13579
10000 20000

出力例 1

Yes

13579 は先頭に不要な 0 を持たず、 10000 以上 20000 以下であるため、条件を満たします。 よって、Yes と出力します。


入力例 2

12345678901234567890
0 1000000000

出力例 2

No

S が表す整数は、64 ビット整数型で表現できる最大値よりも大きいことがあります。


入力例 3

05
5 5

出力例 3

No

05 は先頭に不要な 0 を持つので、条件を満たしません。 よって、No と出力します。


入力例 4

0
0 1

出力例 4

Yes

0 の先頭の 0 は、不要な 0 ではないことに注意してください。

Score : 8 points

Warning

Do not make any mention of this problem until July 17, 2021, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Given is a string S consisting of digits between 0 and 9 (inclusive).
Determine whether S satisfies both of the conditions below as the decimal representation of an integer.

  • It has no unnecessary leading zeroes.
  • It is between L and R.

Constraints

  • S is a string consisiting of digits between 0 and 9 (inclusive).
  • 1 \leq |S| \leq 100
  • 0 \leq L \leq R \leq 10^9
  • L and R are integers.

Input

Input is given from Standard Input in the following format:

S
L R

Output

If S satisfies both of the conditions in the Problem Statement, print Yes; otherwise, print No.


Sample Input 1

13579
10000 20000

Sample Output 1

Yes

13579 has no unnecessary leading 0 and is between 10000 and 20000, satisfying the condition. Thus, we should print Yes.


Sample Input 2

12345678901234567890
0 1000000000

Sample Output 2

No

S may represent an integer greater than the maximum value that a 64-bit integer type can represent.


Sample Input 3

05
5 5

Sample Output 3

No

05 has an unnecessary leading zero, violating the condition. Thus, we should print No.


Sample Input 4

0
0 1

Sample Output 4

Yes

Note that the 0 at the beginning of 0 is not an unnecessary leading zero.

D - Replace

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 7

注意

この問題に対する言及は、2021/7/17 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

長さ N の文字列 S があります。

あなたは、以下の操作を好きな回数だけ行うことができます。

  • S の(連続する)部分文字列で、axa , ixi , uxu , exe , oxo のいずれかと一致する部分を ... に書き換える。

あなたは、操作を可能な限り多く行いたいと思っています。

操作回数が最大となるように操作を行った後の S1 つ出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • S は 英小文字からなる長さ N の文字列。

入力

入力は以下の形式で標準入力から与えられる。

N
S

出力

操作後の S を出力せよ。 答えが複数通りある場合はどれを出力しても良い。


入力例 1

9
ixixixixi

出力例 1

...x...xi

他に ...xix... なども正解になります。


入力例 2

6
auxuxa

出力例 2

a...xa

a...xa に対してこれ以上操作を行うことはできません。


入力例 3

15
gxgaxixuxexoxxx

出力例 3

gxgaxixuxexoxxx

一度も操作を行えないこともあります。

Score : 7 points

Warning

Do not make any mention of this problem until July 17, 2021, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

We have a string S of length N.

You can do the following operation any number of times:

  • Replace a (contiguous) substring of S that matches axa, ixi, uxu, exe, or oxo with ....

You want to do this operation as many times as possible.

Print a string S that results from doing the maximum number of operations possible.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • S is a string of length N consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print a resulting string S. If there are multiple possible results, you may print any of them.


Sample Input 1

9
ixixixixi

Sample Output 1

...x...xi

The other correct answers include ...xix....


Sample Input 2

6
auxuxa

Sample Output 2

a...xa

We cannot do any more operations on a...xa.


Sample Input 3

15
gxgaxixuxexoxxx

Sample Output 3

gxgaxixuxexoxxx

We may be unable to do a single operation.

E - Aoki's Trick

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 7

注意

この問題に対する言及は、2021/7/17 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

初期値が 1 の変数 X があります。

高橋君は、変数 X に対して、「 X3 倍する」という操作を 30 回行いました。 しかし、操作の結果 X3^{30} とはならず、N という値になってしまいました。

いたずら好きな青木君が、以下の操作をしたことを白状しました。

  • 1 \leq k \leq 30 であるような整数 k1 つだけ選び、高橋君の k 回目の操作の直後に変数 X1 を加算した。

N が与えられるので、k を求めてください。 ただし、どのような k に対しても操作の結果として XN とはならない場合、-1 と出力してください。

制約

  • 1 \leq N \leq 10^{15}
  • N \neq 3^{30}
  • 入力は全て整数である。

入力

入力は以下の形式で標準入力から与えられる。

N

出力

答えを出力せよ。 ただし、どのような k に対しても操作の結果 XN とはならない場合、-1 と出力せよ。


入力例 1

205894618879050

出力例 1

10

k=10 の場合の挙動を示します。

1103 倍すると 59049 になります。 青木君のいたずらにより 1 を加算して 59050 になります。

さらに、203 倍すると 205894618879050 になります。


入力例 2

314159265358979

出力例 2

-1

Score : 7 points

Warning

Do not make any mention of this problem until July 17, 2021, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

We have a variable X whose initial value was 1.

Takahashi did 30 operations on this variable, where each operation multiplied X by 3. However, it did not result in X being 3^{30}, but it became N, a different value.

A mischievous boy Aoki has confessed that he did the following:

  • He chose a integer k such that 1 \leq k \leq 30 and added 1 to X just after the k-th operation by Takahashi.

Given N, find k. Here, if no choice of k would result in X being N after the operations, print -1.

Constraints

  • 1 \leq N \leq 10^{15}
  • N \neq 3^{30}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer. If no choice of k would result in X being N after the operations, print -1.


Sample Input 1

205894618879050

Sample Output 1

10

Let us simulate the process for the case k=10.

Multiplying 1 by 3 ten times results in 59049. Then, Aoki mischievously adds 1 to it, resulting in 59050.

Multiplying that value by 3 twenty more times results in 205894618879050.


Sample Input 2

314159265358979

Sample Output 2

-1
F - Double Booking

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 7

注意

この問題に対する言及は、2021/7/17 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

あなたは N 件のオンラインミーティングに出席する予定があります。i 件目のミーティングは、今日から D_i 日後の S_i 時から T_i 時まで開催される予定です。
ミーティングの予定に重複があるかどうか判定してください。ただし、片方のミーティングの終了時刻ともう片方のミーティングの開始時刻が同じである場合は、重複と見なさないこととします。

制約

  • 入力は全て整数
  • 1 ≤ N ≤ 10^5
  • 1 ≤ D_i ≤ 10^5
  • 0 ≤ S_i < T_i ≤ 24

入力

入力は以下の形式で標準入力から与えられる。

N
D_1 S_1 T_1
D_2 S_2 T_2
\vdots
D_N S_N T_N

出力

ミーティングの予定に重複がある場合は Yes を、ない場合は No を出力せよ。


入力例 1

3
1 10 15
1 11 12
1 14 16

出力例 1

Yes

1 件目と 2 件目のミーティング、1 件目と 3 件目のミーティングが重複しています。


入力例 2

3
1 20 22
1 22 24
2 0 2

出力例 2

No

1 件目のミーティングの直後に 2 件目のミーティング、2 件目のミーティングの直後に 3 件目のミーティングがありますが、重複ではありません。


入力例 3

5
2 0 24
1 0 10
3 0 1
1 12 23
3 22 24

出力例 3

No

Score : 7 points

Warning

Do not make any mention of this problem until July 17, 2021, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

You have plans to attend N online meetings. The i-th meeting is scheduled to take place from S_i o'clock to T_i o'clock on the 24-hour clock D_i days later.
Determine whether some of these schedules are overlapping. Here, if a meeting starts exactly when another meeting ends, we do not consider them to be overlapping.

Constraints

  • All values in input are integers.
  • 1 ≤ N ≤ 10^5
  • 1 ≤ D_i ≤ 10^5
  • 0 ≤ S_i < T_i ≤ 24

Input

Input is given from Standard Input in the following format:

N
D_1 S_1 T_1
D_2 S_2 T_2
\vdots
D_N S_N T_N

Output

If some of these schedules are overlapping, print Yes; otherwise, print No.


Sample Input 1

3
1 10 15
1 11 12
1 14 16

Sample Output 1

Yes

The first and second meetings are overlapping, and so are the first and third meetings.


Sample Input 2

3
1 20 22
1 22 24
2 0 2

Sample Output 2

No

The first meeting is immediately followed by the second meeting, which is immediately followed by the third meeting, but we do not consider them to be overlapping.


Sample Input 3

5
2 0 24
1 0 10
3 0 1
1 12 23
3 22 24

Sample Output 3

No
G - Power Expression

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

注意

この問題に対する言及は、2021/7/17 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

正整数 N を、相異なる 3 べきの和差で表してください。

形式的には、以下の条件をすべて満たす数列 A を構築してください。

  • A の長さを M として、1 \leq M \leq 100
  • \sum_{i=1}^{M}A_i = N
  • |A_i| \leq 10^{18}
  • i\ (1 \leq i \leq M) について、|A_i|3 べき。即ち、ある非負整数 x を用いて |A_i|=3^x と表すことができる。
  • |A_i| \neq |A_j|\ (i \neq j)

この問題の制約下で、このような数列 A の存在は保証されます。

制約

  • 1 \leq N \leq 10^{15}
  • N は整数

入力

入力は以下の形式で標準入力から与えられる。

N

出力

以下の形式で問題文中の条件を満たす数列 A を出力せよ。問題文同様、MA の長さを表す。

M
A_1 A_2 \ldots A_M

条件を満たす A が複数存在する場合、そのいずれを出力してもよい。


入力例 1

6

出力例 1

2
9 -3 

93 は共に 3 べきであり、また 9+(-3)=6 であるため、この出力は問題文中の条件を満たします。


入力例 2

9193

出力例 2

9
2187 27 1 -243 3 9 -81 6561 729 

入力例 3

10120190919012

出力例 3

16
-1594323 9 -177147 -531441 1162261467 -4782969 387420489 -6561 -2187 2541865828329 -27 7625597484987 3486784401 10460353203 -94143178827 31381059609 

入力が 32 bit 整数型に収まりきらない場合があります。

Score : 6 points

Warning

Do not make any mention of this problem until July 17, 2021, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Represent a positive integer N by additions and subtractions of distinct powers of 3.

Formally, construct a sequence A that satisfies all of the following conditions.

  • 1 \leq M \leq 100, where M is the length of A.
  • \sum_{i=1}^{M}A_i = N.
  • |A_i| \leq 10^{18}.
  • For each i\ (1 \leq i \leq M), |A_i| is a power of 3. That is, there is a non-negative integer x such that |A_i|=3^x.
  • |A_i| \neq |A_j|\ (i \neq j).

It is guaranteed that such a sequence A exists under the Constraints of this problem.

Constraints

  • 1 \leq N \leq 10^{15}
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print a sequence A that satisfies the conditions in the Problem Statement in the format below, where M again denotes the length of A.

M
A_1 A_2 \ldots A_M

If there are multiple sequences A satisfying the conditions, you may print any of them.


Sample Input 1

6

Sample Output 1

2
9 -3 

Both 9 and 3 are powers of 3, and we have 9+(-3)=6, so this output satisfies the conditions.


Sample Input 2

9193

Sample Output 2

9
2187 27 1 -243 3 9 -81 6561 729 

Sample Input 3

10120190919012

Sample Output 3

16
-1594323 9 -177147 -531441 1162261467 -4782969 387420489 -6561 -2187 2541865828329 -27 7625597484987 3486784401 10460353203 -94143178827 31381059609 

Input may not fit into a 32-bit integer type.

H - Line Chart

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

注意

この問題に対する言及は、2021/7/17 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

高橋君は、折れ線グラフを描く次のような宿題を先生から与えられました。

  1. N 個の非負整数 A_1, A_2, \ldots, A_N が与えられる。ただし、A_1 = A_N = 0 が成り立つ。
  2. i = 1, 2, \ldots, N について、二次元平面上の座標 (i, A_i) に点 P_i を打つ。
  3. i = 1, 2, \ldots, N-1 について、点 P_i と点 P_{i+1} を結ぶ線分を描く。

高橋君は宿題を早く済ませたいので、A_1, A_2, \ldots, A_N の値をこっそり変更して、 描く線分の長さの総和を小さくしようと思います。

しかし、こっそり値を変更したことを先生に気づかれないためには、 A_1, A_2, \ldots, A_N の変更後の値をそれぞれ B_1, B_2, \ldots, B_N とするとき、 以下の条件をすべて満たす必要があります。

  • B_1, B_2, \ldots, B_N はすべて非負整数である。
  • A_1A_N の値は変更してはならない。すなわち、B_1 = B_N = 0 が成り立つ。
  • \sum_{i=1}^{N} A_i = \sum_{i=1}^{N} B_i

高橋君が先生に気づかれないように A_1, A_2, \ldots, A_N の値を適切に変更するとき、 高橋君が描く線分の長さの総和の最小値を求めてください。

制約

  • 2 \leq N \leq 100
  • 0 \leq A_i \leq 100
  • A_1 = A_N = 0
  • \sum_{i=1}^N A_i \leq 100
  • 入力はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。
想定解答との絶対誤差または相対誤差が 10^{-5} 以下であれば正解として扱われる。


入力例 1

4
0 3 0 0

出力例 1

5.0644951022459797

高橋君は、A_21 に、A_32 に変更することで、線分の長さの総和を最小にできます。


入力例 2

7
0 1 2 3 4 5 0

出力例 2

10.3245553203367599

Score : 6 points

Warning

Do not make any mention of this problem until July 17, 2021, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Takahashi's teacher has given Takahashi homework that required him to draw a polyline as follows.

  1. Given are N positive integers A_1, A_2, \ldots, A_N, where A_1 = A_N = 0.
  2. For each i = 1, 2, \ldots, N, plot a point P_i at the coordinates (i, A_i) in a two-dimensional plane.
  3. For each i = 1, 2, \ldots, N-1, draw a segment connecting P_i and P_{i+1}.

To get it done early, Takahashi is planning to secretly modify the values of A_1, A_2, \ldots, A_N to shorten the total length of segments to draw.

However, in order to keep this secret modification from being noticed by his teacher, all of the following conditions must hold, where B_1, B_2, \ldots, B_N are the new values for A_1, A_2, \ldots, A_N after modification, respectively.

  • B_1, B_2, \ldots, B_N are all non-negative integers.
  • The values of A_1 and A_N cannot be modified, that is, B_1 = B_N = 0.
  • \sum_{i=1}^{N} A_i = \sum_{i=1}^{N} B_i.

Find the minimum possible total length of segments to draw when Takahashi modifies the values of A_1, A_2, \ldots, A_N so that his teacher will not notice it.

Constraints

  • 2 \leq N \leq 100
  • 0 \leq A_i \leq 100
  • A_1 = A_N = 0
  • \sum_{i=1}^N A_i \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N

Output

Print the answer.
It will be considered as correct when its absolute or relative error from our answer is at most 10^{-5}.


Sample Input 1

4
0 3 0 0

Sample Output 1

5.0644951022459797

Takahashi can minimize the total length of segments to draw by changing A_2 to 1 and A_3 to 2.


Sample Input 2

7
0 1 2 3 4 5 0

Sample Output 2

10.3245553203367599
I - Nevus

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

注意

この問題に対する言及は、2021/7/17 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

2 次元平面上に、高橋君の似顔絵がありました。 似顔絵には右目と左目と N 個のホクロが描かれており、

  • E \gt 0 として、右目の座標は (-E, 0)、左目の座標は (E, 0)
  • i 個目のホクロの座標は (A_i, B_i)

でした。

いたずら好きな青木君が、高橋君の似顔絵を(裏返すことなく)移動させてしまいました。 より正確には、似顔絵に平行移動と回転移動を施しました。

その結果、

  • 右目の座標は (x_1, y_1)、左目の座標は (x_2, y_2)
  • i 個目のホクロの座標は (a_i, b_i)

となりました。

移動後の両目とホクロの座標が与えられるので、移動前のホクロの座標を求めてください。 すなわち、(A_1, B_1), \dots , (A_N,B_N) を求めてください。

なお、移動前の両目の座標、すなわち E は与えられないことに注意してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • -10^5 \leq x_i, y_i, a_i, b_i \leq 10^5
  • E \gt 0
  • (x_1, y_1), (x_2, y_2), (a_1, b_1), \dots , (a_N,b_N) は相異なる。
  • (-E, 0) \neq (x_1, y_1) または (E, 0) \neq (x_2, y_2)
  • 入力に含まれる値は全て整数である。

入力

入力は以下の形式で標準入力から与えられる。

N
x_1 y_1
x_2 y_2
a_1 b_1
:
a_N b_N

出力

出力は N 行からなる。 i 行目には、i 個目のホクロの移動前の座標を以下の形式で出力せよ。 出力されたそれぞれの値について、想定解答との絶対誤差または相対誤差が 10^{-5} 以下であれば正解と判定される。

A_1 B_1
:
A_N B_N

入力例 1

2
5 0
-5 0
-1 3
2 4

出力例 1

1 -3
-2 -4

入力では、移動後の両目とホクロの座標が与えられます。


入力例 2

4
4 4
12 10
12 4
8 7
-4 -2
100 10

出力例 2

1.4 -4.8
0 0
-15 0
75.4 -52.8

入力される値は全て整数ですが、出力する値や E は整数とは限らないことに注意してください。


入力例 3

10
-40336 -25353
25518 98473
-66200 57666
23235 -64774
56870 -67151
-99509 73639
39965 -61027
-54385 -34598
-57063 14129
63186 -88708
88770 85106
-92520 69200

出力例 3

-8970.87249328212 61817.21737274555
-75079.28924877638 -74637.35403870217
-61384.55754506934 -105449.9760881721
-10508.55928227892 98726.04733711783
-63915.43362406853 -87648.93490309674
-84883.41976667292 8062.914405771756
-43119.58914921946 33307.21406245974
-77451.63868397648 -121148.543178062
88022.56926540422 -62121.98851386775
-11146.07084342446 90471.08392051774

Score : 6 points

Warning

Do not make any mention of this problem until July 17, 2021, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

There was a portrait of Takahashi on a two-dimensional plane. It consisted of the right eye, the left eye, and N moles, at the following positions.

  • The right eye was at the coordinates (-E, 0), and the left eye was at the coordinates (E, 0), where E \gt 0.
  • The i-th mole was at the coordinate (A_i, B_i).

However, a mischievous boy Aoki moved this portrait (without flipping it). More formally, he translated and rotated the portrait.

As a result, the parts moved to the following positions:

  • The right eye moved to the coordinates (x_1, y_1), and the left eye moved to the coordinates (x_2, y_2).
  • The i-th mole moved to the coordinate (a_i, b_i).

Given the coordinates of the eyes and the moles after the move, find the coordinates of the moles before the move, that is, (A_1, B_1), \dots, (A_N, B_N).

Note that the given values do not contain the coordinates of the eyes before the move, that is, E.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • -10^5 \leq x_i, y_i, a_i, b_i \leq 10^5
  • E \gt 0
  • (x_1, y_1), (x_2, y_2), (a_1, b_1), \dots, (a_N, b_N) are distinct.
  • (-E, 0) \neq (x_1, y_1) or (E, 0) \neq (x_2, y_2).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
a_1 b_1
:
a_N b_N

Output

Print N lines. The i-th line should contain the coordinates of the moles before the move in the format shown below. Your output will be considered correct when, for each value printed, the absolute or relative error from our answer is at most 10^{-5}.

A_1 B_1
:
A_N B_N

Sample Input 1

2
5 0
-5 0
-1 3
2 4

Sample Output 1

1 -3
-2 -4

In the figure below, "移動前" and "移動後" mean before and after the move, "右目" and "左目" mean right and left eyes, and "ホクロ1個目" and "ホクロ2個目" means the first and second moles.

Given in input are the coordinates of both eyes and the moles after the move.


Sample Input 2

4
4 4
12 10
12 4
8 7
-4 -2
100 10

Sample Output 2

1.4 -4.8
0 0
-15 0
75.4 -52.8

All values in input are integers, but note that the values to be printed and E may not be integers.


Sample Input 3

10
-40336 -25353
25518 98473
-66200 57666
23235 -64774
56870 -67151
-99509 73639
39965 -61027
-54385 -34598
-57063 14129
63186 -88708
88770 85106
-92520 69200

Sample Output 3

-8970.87249328212 61817.21737274555
-75079.28924877638 -74637.35403870217
-61384.55754506934 -105449.9760881721
-10508.55928227892 98726.04733711783
-63915.43362406853 -87648.93490309674
-84883.41976667292 8062.914405771756
-43119.58914921946 33307.21406245974
-77451.63868397648 -121148.543178062
88022.56926540422 -62121.98851386775
-11146.07084342446 90471.08392051774
J - Never Ending Journey

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

注意

この問題に対する言及は、2021/7/17 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

高橋王国には N 個の都市があり、それぞれ都市 1、都市 2\ldots、都市 N と呼ばれています。
また、高橋王国には M 本の一方通行の道路があり、 i 番目の道路を通って都市 u_i から都市 v_i に移動することができます。
(道路は一方通行であるため、都市 v_i から都市 u_i には移動できないことに注意してください。)

高橋王国に観光旅行にやってきた青木王国の青木君は、N 個の都市のいずれかから出発し、 青木王国に帰るまでの間、以下の手順を繰り返して旅行をします。

  • 青木君が現在いる都市から道路をちょうど 1 本通って移動可能な都市が存在するならば、移動可能な都市の中から 1 つを選び、そこに移動する。
  • 青木君が現在いる都市から道路をちょうど 1 本通って移動可能な都市が存在しなければ、青木君は青木王国に帰る。

青木君が、出発する都市を適切に選択し、旅行中の移動先の都市を適切に選択しつづけることで、 青木王国に帰ることなくいくらでも旅行を続けることができるかどうか判定してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq u_i, v_i \leq N
  • u_i \neq v_i
  • i \neq j \Rightarrow (u_i, v_i) \neq (u_j, v_j)
  • 入力はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

出力

青木君がいくらでも旅行を続けることができるなら Yes と出力し、 そうでなければ No と出力してください。


入力例 1

3 3
1 2
2 1
2 3

出力例 1

Yes

青木君は、都市 1 から出発し、都市 1 \rightarrow 都市 2 \rightarrow 都市 1 \rightarrow 都市 2 \rightarrow 都市 1 \rightarrow \cdots と移動を繰り返すことで、旅行をいくらでも続けることができます。


入力例 2

8 7
2 4
2 8
7 3
6 1
8 4
8 3
5 3

出力例 2

No

Score : 6 points

Warning

Do not make any mention of this problem until July 17, 2021, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

There are N cities in the Kingdom of Takahashi, called City 1, City 2, \ldots, City N.
There are also M one-way roads in this kingdom, and you can use the i-th road to get from City u_i to City v_i.
(Note that the roads are one-way, that is, the i-th road does not allow you to get from City v_i to City u_i.)

Aoki, a resident of the Kingdom of Aoki, has come to the Kingdom of Takahashi for sightseeing. During his stay, he will travel by repeating the following:

  • If there are one or more cities that he can reach by using just one road from the city he is currently in, he will choose one of those reachable cities and get there.
  • If there is no city that he can reach by using just one road from the city he is currently in, he will go back to the Kingdom of Aoki.

Determine whether it is possible for Aoki to continue traveling indefinitely without returning to his country by choosing the appropriate city from which he starts his travel and keep making appropriate choices for where to go during his travel.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq u_i, v_i \leq N
  • u_i \neq v_i
  • i \neq j \Rightarrow (u_i, v_i) \neq (u_j, v_j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

Output

If it is possible for Aoki to continue traveling indefinitely, print Yes; otherwise, print No.


Sample Input 1

3 3
1 2
2 1
2 3

Sample Output 1

Yes

By starting in City 1 and keep traveling in the manner 1 \rightarrow 2 \rightarrow 1 \rightarrow 2 \rightarrow 1 \rightarrow \cdots, he can continue his travel indefinitely.


Sample Input 2

8 7
2 4
2 8
7 3
6 1
8 4
8 3
5 3

Sample Output 2

No
K - Flying Trip

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

注意

この問題に対する言及は、2021/7/17 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

AtCoder 王国には都市 1 , 都市 2 , \ldots , 都市 NN 個の都市と、 これらの都市を双方向に結ぶ道 1 , 道 2 , \ldots , 道 MM 本の道があり、 どの 2 つの都市の間もいくつかの道を経由して移動することができます。 具体的には道 i は都市 U_i と 都市 V_i を結び、 一方の都市からもう一方の都市まで T_i 時間で移動できます。
さらに、都市にはそれぞれ景観と呼ばれる数値が定まっており、都市 i の景観は A_i です。 ある都市から別の都市へ 1 つ以上の道を経由して移動したとき、 その移動にかかる時間は使用したそれぞれの道の所要時間の総和であり、 その移動の満足度は始点と終点を含め、その移動の過程で通った都市の景観の総和で表されます。 ただし、満足度について、同じ都市を 2 回以上経由してもその都市の景観は 1 回しかカウントしません。

高橋君は今都市 1 におり、都市 N まで移動したいと考えています。高橋君は急いでいるので、移動にかかる時間を最短にした上で、その移動の満足度をなるべく大きくしたいと考えています。このとき高橋君の移動の満足度を求めてください。

制約

  • 2 \leq N \leq 10^5
  • N-1 \leq M \leq 2\times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq U_i,V_i \leq N
  • U_i \neq V_i
  • 1 \leq T_i \leq 10^9
  • 入力は全て整数
  • どの 2 つの都市の間もいくつかの道を経由して移動することができる

入力

入力は以下の形式で標準入力から与えられる。

N M
A_1 A_2 \ldots A_N
U_1 V_1 T_1
U_2 V_2 T_2
:
U_M V_M T_M

出力

答えを出力せよ。


入力例 1

4 5
2 5 7 3
1 2 1
1 3 2
2 4 4
3 4 3
2 3 2

出力例 1

12

1\to 2\to 4 と移動するとき移動時間は 5 で最短で、このとき満足度は 10 です。
1\to 3\to 4 と移動するとき移動時間は 5 で最短で、このとき満足度は 12 です。
他に移動時間が 5 となるような移動の方法は存在しません。 例えば、1\to 2\to 3\to 4 と移動すると満足度は 17 ですが、移動時間が 6 となるため、最短ではありません。 また、移動時間が 4 以下となるような都市 1 から都市 4 への移動方法も存在しません。
よって、答えは 12 となります。


入力例 2

6 5
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
1 2 1000000000
2 3 1000000000
3 4 1000000000
4 5 1000000000
5 6 1000000000

出力例 2

6000000000

答えが 32 bit 整数に収まらない可能性がある事に注意してください。

Score : 6 points

Warning

Do not make any mention of this problem until July 17, 2021, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

There are N cities in the Kingdom of AtCoder called City 1, City 2, \ldots, City N, and M roads called Road 1, Road 2, \ldots, Road M that connect the cities bidirectionally, so that you can travel between any two cities using some number of roads. Specifically, Road i connects City U_i and City V_i, and it enables you to get from either city to the other city in T_i hours.
Additionally, each city has a fixed value called scenery; the scenery of City i is A_i. When you travel from a city to another city by using one or more roads, the time needed for that travel will be the sum of the times it takes to get through the individual roads, and the satisfaction of that travel will be the sum of the sceneries of the cities visited during the travel, including the starting point and the destination. Here, the scenery of a city contributes to the satisfaction only once, even if that city is visited more than once.

Takahashi is now in City 1 and wants to get to City N. Since he is in a hurry, he wants to minimize the time it takes to travel, and maximize the satisfaction of the travel on the condition that the time needed is minimized. Find the satisfaction of the travel that meets these criteria.

Constraints

  • 2 \leq N \leq 10^5
  • N-1 \leq M \leq 2\times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq U_i,V_i \leq N
  • U_i \neq V_i
  • 1 \leq T_i \leq 10^9
  • All values in input are integers.
  • It is possible to travel between any two cities using some number of roads.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 \ldots A_N
U_1 V_1 T_1
U_2 V_2 T_2
:
U_M V_M T_M

Output

Print the answer.


Sample Input 1

4 5
2 5 7 3
1 2 1
1 3 2
2 4 4
3 4 3
2 3 2

Sample Output 1

12

If we follow the path 1\to 2\to 4, the time needed will be 5, which is the minimum possible, and the satisfaction will be 10.
If we follow the path 1\to 3\to 4, the time needed will be 5, which is the minimum possible, and the satisfaction will be 12.
There is no other path that minimizes the time needed.
For example, the path 1\to 2\to 3\to 4 will have the satisfaction of 17, but the time needed will be 6, which is not the minimum possible. Additionally, there is no path from City 1 to City 4 such that the time needed is 4 or less.
Thus, the answer is 12.


Sample Input 2

6 5
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
1 2 1000000000
2 3 1000000000
3 4 1000000000
4 5 1000000000
5 6 1000000000

Sample Output 2

6000000000

Note that the answer may not fit into a 32-bit integer.

L - Multiple Min Query

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

注意

この問題に対する言及は、2021/7/17 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

長さ N の整数列 A=(A_1, A_2, \dots ,A_N) があります。
あなたは今からこの数列について Q 個のクエリを処理します。i 番目のクエリでは、 T_i, X_i, Y_i が与えられるので、以下の処理をしてください。

  • T_i = 1 のとき
    A_{X_i}Y_i で置き換える。
  • T_i = 2 のとき
    A_{X_i}, \ldots ,A_{Y_i}の中での最小値を p とする。X_i \leq j \leq Y_i かつ A_j = p を満たす j を全て列挙する。

制約

  • 1 \leq N, Q \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • T_i1 または 2
  • T_i=1 ならば、1 \leq X_i \leq N かつ 0 \leq Y_i \leq 10^9
  • T_i=2 ならば、1 \leq X_i \leq Y_i \leq N
  • 入力は全て整数
  • 列挙すべき j の個数の総和は 2 \times 10^5 を超えない

入力

入力は以下の形式で標準入力から与えられる。

N Q
A_1 A_2 \dots A_N
T_1 X_1 Y_1
T_2 X_2 Y_2
:
T_Q X_Q Y_Q

出力

ある T_i = 2 のタイプのクエリにおいて、列挙すべき値が j_1, j_2, \dots , j_M ( j_1 \lt j_2 \lt \dots \lt j_M ) であるとする。 このとき以下の形式で 1 行に出力せよ。

M j_1 j_2 \dots j_M

T_i = 2 のタイプのクエリの数を Q_2 個とする。Q_2 行出力せよ。


入力例 1

6 7
3 20 3 10 15 10
2 1 6
2 4 5
1 3 10
1 1 1000000000
2 1 6
1 5 0
2 1 6

出力例 1

2 1 3 
1 4 
3 3 4 6 
1 5 

Score : 6 points

Warning

Do not make any mention of this problem until July 17, 2021, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

We have a sequence of N integers A=(A_1, A_2, \dots ,A_N).
You are to process Q queries on this sequence. In the i-th query, given T_i, X_i, and Y_i, do as follows.

  • If T_i = 1:
    Replace A_{X_i} with Y_i.
  • If T_i = 2:
    Let p be the smallest value among A_{X_i}, \ldots ,A_{Y_i}. List all indices j such that X_i \leq j \leq Y_i and A_j = p.

Constraints

  • 1 \leq N, Q \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • T_i is 1 or 2.
  • 1 \leq X_i \leq N and 0 \leq Y_i \leq 10^9, if T_i=1.
  • 1 \leq X_i \leq Y_i \leq N if T_i=2.
  • All values in input are integers.
  • The total number of indices j to be listed does not exceed 2 \times 10^5.

Input

Input is given from Standard Input in the following format:

N Q
A_1 A_2 \dots A_N
T_1 X_1 Y_1
T_2 X_2 Y_2
:
T_Q X_Q Y_Q

Output

Let j_1, j_2, \dots , j_M ( j_1 \lt j_2 \lt \dots \lt j_M ) be the indices to be listed for a query with T_i = 2. Then, print a line in the following format:

M j_1 j_2 \dots j_M

You should print Q_2 such lines, where Q_2 is the number of queries with T_i = 2.


Sample Input 1

6 7
3 20 3 10 15 10
2 1 6
2 4 5
1 3 10
1 1 1000000000
2 1 6
1 5 0
2 1 6

Sample Output 1

2 1 3 
1 4 
3 3 4 6 
1 5 
M - Divide

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

注意

この問題に対する言及は、2021/7/17 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

長さ N の整数列 A=(A_1, A_2, \dots ,A_N) と整数 C があります。
長さ K\ (K \geq 1) の整数列 B=(B_1, B_2, \dots ,B_K)スコア を、以下の式で定義します。

\displaystyle C + \sum_{i=1}^{K-1} |B_{i+1} - B_i|

整数列 A を順序を保ったまま、いくつかの(連続するとは限らない)部分列に分解します。

このとき整数列 A合計スコア を、得られた整数列の スコア の総和として定義します。

整数列 A合計スコア の最小値を求めてください。

制約

  • 1 \leq N \leq 200
  • 1 \leq C \leq 10^9
  • 1 \leq A_i \leq 10^9
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N C
A_1 A_2 \dots A_N

出力

合計スコア の最小値を出力せよ。


入力例 1

6 10
4 25 1000000000 9 19 22

出力例 1

44

(4, 9), (25, 19, 22), (1000000000) と分解します。それぞれの スコア15, 19, 10 です。


入力例 2

5 100
3 1 4 1 5

出力例 2

112

(3, 1, 4, 1, 5)スコア112 です。


入力例 3

10 5301
6708 1391 3108 7953 7797 2370 7699 1098 2362 2359

出力例 3

17095

Score : 6 points

Warning

Do not make any mention of this problem until July 17, 2021, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

We have a sequence of N integers A=(A_1, A_2, \dots ,A_N) and an integer C.
Let us define the score of the sequence of K\ (K \geq 1) integers B=(B_1, B_2, \dots ,B_K) as the following:

\displaystyle C + \sum_{i=1}^{K-1} |B_{i+1} - B_i|.

We will decompose the sequence A into some (not necessarily contiguous) subsequences while keeping the relative order of the elements.

Here, let us define the total score as the sum of the scores of the subsequences obtained.

Find the minimum possible total score.

Constraints

  • 1 \leq N \leq 200
  • 1 \leq C \leq 10^9
  • 1 \leq A_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N C
A_1 A_2 \dots A_N

Output

Print the minimum possible value of the total score.


Sample Input 1

6 10
4 25 1000000000 9 19 22

Sample Output 1

44

We should divide it into (4, 9), (25, 19, 22), (1000000000). Their scores are 15, 19, 10, respectively.


Sample Input 2

5 100
3 1 4 1 5

Sample Output 2

112

The score of (3, 1, 4, 1, 5) is 112.


Sample Input 3

10 5301
6708 1391 3108 7953 7797 2370 7699 1098 2362 2359

Sample Output 3

17095
N - Monochrome Design

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

注意

この問題に対する言及は、2021/7/17 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

上下左右に無限に広がる座標平面を考えます。 最初、座標平面全体は白色に塗られています。
この上で、次の形のクエリを Q 個行う事を考えます。 i 個目のクエリは次の形で与えられます。

(A_i,B_i) , (A_i,D_i) , (C_i,B_i) , (C_i,D_i) を頂点とする長方形領域を選び、その領域内の色を反転させる。すなわち、白色だった場所は黒色で塗り、黒色だった場所は白色で塗る。

すべてのクエリが終わった後、黒色で塗られている範囲の面積を求めてください。

制約

  • 1 \leq Q \leq 10^5
  • -10^9 \leq A_i < C_i \leq 10^9
  • -10^9 \leq B_i < D_i \leq 10^9
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

Q
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
:
A_Q B_Q C_Q D_Q

出力

すべてのクエリが終わった後、黒色で塗られている範囲の面積を出力せよ。


入力例 1

2
0 0 2 2
1 1 3 3

出力例 1

6

1 回目の操作の後では 2 \times 2 の正方形領域が黒く塗られています。
2 回目の操作の後で、 (1,1) , (1,2) , (2,1) , (2,2) で囲まれた正方形領域は白色に戻り、最終的に黒く塗られた領域の面積は 6 となります。


入力例 2

1
-1000000000 -1000000000 1000000000 1000000000

出力例 2

4000000000000000000

答えが 32 bit 整数に収まらない可能性があることに注意してください。

Score : 6 points

Warning

Do not make any mention of this problem until July 17, 2021, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Consider an infinite coordinate plane. Initially, the plane is entirely painted white.
Let us process the following Q queries on this plane. The i-th query is in the following form:

Invert the rectangular region whose corners are (A_i,B_i), (A_i,D_i), (C_i,B_i), and (C_i,D_i). That is, the parts within the region that are painted white before this query will be painted black, and vice versa.

Find the area of the whole region painted black after all the queries.

Constraints

  • 1 \leq Q \leq 10^5
  • -10^9 \leq A_i < C_i \leq 10^9
  • -10^9 \leq B_i < D_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
:
A_Q B_Q C_Q D_Q

Output

Print the area of the whole region painted black after all the queries.


Sample Input 1

2
0 0 2 2
1 1 3 3

Sample Output 1

6

After the first query, a 2 \times 2 square region is painted black.
After the second query, a square region whose corners are (1,1), (1,2), (2,1), (2,2) is white again, so the final area of the whole region painted black is 6.


Sample Input 2

1
-1000000000 -1000000000 1000000000 1000000000

Sample Output 2

4000000000000000000

Note that the answer may not fit into a 32-bit integer type.

O - Computer

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 6

注意

この問題に対する言及は、2021/7/17 18:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。 試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

整数 N と、長さ N の整数列 A, B が与えられます。
高橋君にはこれからの N 日間で、以下のことが起こります。

  • i 日目の朝 : 所持金が A_i 円増えます。
  • i 日目の昼 : 自分の所持金が負とならないという条件の下、好きな金額を支払ってコンピュータを購入することが出来ます。 コンピュータを購入しないという選択をすることも可能です。既に別のコンピュータを所持している状態で新たなコンピュータを購入した場合、元のコンピュータは破棄されます。
  • i 日目の夜 : 仕事をします。この仕事をこなすには、高橋君が価格が B_i 円以上のコンピュータを所持している必要があります。

はじめ、高橋君の所持金は 0 円であり、高橋君はコンピュータを所持していません。

高橋君が全ての仕事をこなすことが可能かどうかを判定してください。
可能な場合、 高橋君が全ての仕事をこなしたという条件の下での、N 日目終了時点での高橋君の所持金の最大値を答えてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • 入力はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

高橋君が全ての仕事をこなすことが可能な場合、高橋君が全ての仕事をこなしたという条件の下での、N 日目終了時点での高橋君の所持金の最大値を答えてください。
高橋君が全ての仕事をこなすことが不可能な場合、-1 を出力してください。


入力例 1

3
5 1
2 4
4 6

出力例 1

4

高橋君は、1 日目の昼に 1 円のコンピュータを購入、2 日目の昼に 6 円のコンピュータを購入し、 3 日目の昼にはコンピュータを購入しないという選択をすることで、 全ての仕事をこなした上で、 3 日目終了時点での所持金を 4 円にすることができます。


入力例 2

3
3 2
1 2
3 6

出力例 2

-1

高橋君は、どのような選択を行っても、全ての仕事をこなすことはできません。 よって、-1 を出力します。

Score : 6 points

Warning

Do not make any mention of this problem until July 17, 2021, 6:00 p.m. JST. In case of violation, compensation may be demanded. After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Given are an integer N and sequences A and B of N integers each.
For the next N days, the following will happen to Takahashi.

  • On the morning of Day i: He gains A_i yen (Japanese currency).
  • On the afternoon of Day i: He is given an opportunity to buy a computer by paying any amount of money he likes as long as it will not result in a negative cash balance. He is allowed to choose not to buy a computer. If a new computer is bought when he already has one, the old one will be thrown away.
  • On the night of Day i: He does his job. To do it, he has to own a computer worth B_i yen or more.

Initially, Takahashi has zero yen and no computer.

Determine whether it is possible for Takahashi to do all the jobs.
If it is possible, find the maximum possible amount of money he has at the end of Day N on the condition that all jobs are done.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

If it is possible for Takahashi to do all the jobs, print the maximum possible amount of money he has at the end of Day N on the condition that all jobs are done.
If it is impossible for Takahashi to do all the jobs, print -1.


Sample Input 1

3
5 1
2 4
4 6

Sample Output 1

4

Takahashi can buy a computer worth 1 yen on the afternoon of Day 1, another worth 6 yen on Day 2, and nothing on Day 3 to do all the jobs and have 4 yen at the end of Day 3.


Sample Input 2

3
3 2
1 2
3 6

Sample Output 2

-1

Regardless of what choices Takahashi makes, he cannot do all the jobs. Thus, we should print -1.