A - Three-Point Shot

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

バスケットボールの試合が行われており、現在の両チームの得点は XY です。ここで X \neq Y であることが保証されます。
現在劣勢であるチームが、 3 ポイントシュートを一本成功させて優勢に立つことはできますか?
つまり、現在得点が低い側のチームが 3 点を得た場合、そのチームの得点が他方のチームの得点より真に高くなるかを判定してください。

制約

  • 0 \le X \le 100
  • 0 \le Y \le 100
  • X \neq Y
  • X, Y は整数である

入力

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

X Y

出力

現在劣勢であるチームが 3 ポイントシュートを一本成功させて優勢に立つことができるなら Yes を、できないなら No を出力せよ。


入力例 1

3 5

出力例 1

Yes

現在 3 点を取っている側が劣勢です。
このチームが 3 ポイントシュートを成功させると、得点は 6 点となり、優勢だったチームの得点である 5 点を上回ります。
よって Yes を出力します。


入力例 2

16 2

出力例 2

No

点差が開きすぎていて、劣勢側が 3 点を取ったとしても優勢側を上回ることはできません。


入力例 3

12 15

出力例 3

No

3 ポイントシュートによって同点にはなりますが、追い抜くことはできません。

Score : 100 points

Problem Statement

A basketball game is being played, and the score is now X-Y. Here, it is guaranteed that X \neq Y.
Can the team which is behind turn the tables with a successful three-point goal?
In other words, if the team which is behind earns three points, will its score become strictly greater than that of the other team?

Constraints

  • 0 \le X \le 100
  • 0 \le Y \le 100
  • X \neq Y
  • X and Y are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

If the team which is behind can turn the tables with a successful three-point goal, print Yes; otherwise, print No.


Sample Input 1

3 5

Sample Output 1

Yes

The team with 3 points is behind.
After a successful 3-point goal, it will have 6 points, which is greater than that of the other team - 5.
Thus, we should print Yes.


Sample Input 2

16 2

Sample Output 2

No

The gap is too much. The team which is behind cannot overtake the other by getting 3 points.


Sample Input 3

12 15

Sample Output 3

No

A 3-point goal will tie the score but not turn the tables.

B - Orthogonality

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

2 つの N 次元ベクトル A = (A_1, A_2, A_3, \dots, A_N), B = (B_1, B_2, B_3, \dots, B_N) が与えられます。
AB の内積が 0 かどうかを判定してください。
すなわち、A_1B_1 + A_2B_2 + A_3B_3 + \dots + A_NB_N = 0 かどうかを判定してください。

制約

  • 1 \le N \le 100000
  • -100 \le A_i \le 100
  • -100 \le B_i \le 100
  • 入力に含まれる値は全て整数である

入力

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

N
A_1 A_2 A_3 \dots A_N
B_1 B_2 B_3 \dots B_N

出力

AB の内積が 0 ならば Yes を、0 でないならば No を出力せよ。


入力例 1

2
-3 6
4 2

出力例 1

Yes

AB の内積は (-3) \times 4 + 6 \times 2 = 0 です。


入力例 2

2
4 5
-1 -3

出力例 2

No

AB の内積は 4 \times (-1) + 5 \times (-3) = -19 です。


入力例 3

3
1 3 5
3 -6 3

出力例 3

Yes

AB の内積は 1 \times 3 + 3 \times (-6) + 5 \times 3 = 0 です。

Score : 200 points

Problem Statement

Given are two N-dimensional vectors A = (A_1, A_2, A_3, \dots, A_N) and B = (B_1, B_2, B_3, \dots, B_N).
Determine whether the inner product of A and B is 0.
In other words, determine whether A_1B_1 + A_2B_2 + A_3B_3 + \dots + A_NB_N = 0.

Constraints

  • 1 \le N \le 100000
  • -100 \le A_i \le 100
  • -100 \le B_i \le 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 A_3 \dots A_N
B_1 B_2 B_3 \dots B_N

Output

If the inner product of A and B is 0, print Yes; otherwise, print No.


Sample Input 1

2
-3 6
4 2

Sample Output 1

Yes

The inner product of A and B is (-3) \times 4 + 6 \times 2 = 0.


Sample Input 2

2
4 5
-1 -3

Sample Output 2

No

The inner product of A and B is 4 \times (-1) + 5 \times (-3) = -19.


Sample Input 3

3
1 3 5
3 -6 3

Sample Output 3

Yes

The inner product of A and B is 1 \times 3 + 3 \times (-6) + 5 \times 3 = 0.

C - ABC Tournament

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

選手 1 から選手 2^N までの 2^N 人の選手がトーナメント形式のプログラミング対決をします。
選手 i のレートは A_i です。どの 2 人の選手のレートも異なり、2 人の選手が対戦すると常にレートが高い方が勝ちます。

トーナメント表は完全二分木の形をしています。
より正確には、このトーナメントは以下の要領で行われます。

  • i = 1, 2, 3, \dots, N について順に、以下のことが行われる。

    • 各整数 j (1 \le j \le 2^{N - i}) について、まだ負けたことのない選手のうち、 2j - 1 番目に番号の小さい選手と 2j 番目に番号の小さい選手が対戦する。

準優勝する、すなわち最後に行われる対戦において負ける選手の番号を求めてください。

制約

  • 1 \le N \le 16
  • 1 \le A_i \le 10^9
  • A_i は相異なる
  • 入力に含まれる値は全て整数である

入力

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

N
A_1 A_2 A_3 \dots A_{2^N}

出力

準優勝する選手の番号を出力せよ。


入力例 1

2
1 4 2 5

出力例 1

2

まず選手 12、選手 34 がそれぞれ対戦し、レートの大小から選手 24 が勝利します。
次に選手 2 と選手 4 が対戦し、選手 4 が勝利してトーナメントが終了します。
最後の対戦で負けるのは選手 2 なので、2 を出力します。


入力例 2

2
3 1 5 4

出力例 2

1

まず選手 12、選手 34 がそれぞれ対戦し、レートの大小から選手 13 が勝利します。
次に選手 1 と選手 3 が対戦し、選手 3 が勝利してトーナメントが終了します。
最後の対戦で負けるのは選手 1 なので、1 を出力します。


入力例 3

4
6 13 12 5 3 7 10 11 16 9 8 15 2 1 14 4

出力例 3

2

Score : 300 points

Problem Statement

2^N players, labeled 1 through 2^N, will compete against each other in a single-elimination programming tournament.
The rating of Player i is A_i. Any two players have different ratings, and a match between two players always results in the victory of the player with the higher rating.

The tournament looks like a perfect binary tree.
Formally, the tournament will proceed as follows:

  • For each integer i = 1, 2, 3, \dots, N in this order, the following happens.
    • For each integer j (1 \le j \le 2^{N - i}), among the players who have never lost, the player with the (2j - 1)-th smallest label and the player with the 2j-th smallest label play a match against each other.

Find the label of the player who will take second place, that is, lose in the final match.

Constraints

  • 1 \le N \le 16
  • 1 \le A_i \le 10^9
  • A_i are pairwise different.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 A_3 \dots A_{2^N}

Output

Print the label of the player who will take second place.


Sample Input 1

2
1 4 2 5

Sample Output 1

2

First, there will be two matches between Players 1 and 2 and between Players 3 and 4. According to the ratings, Players 2 and 4 will win.
Then, there will be a match between Players 2 and 4, and the tournament will end with Player 4 becoming champion.
The player who will lose in the final match is Player 2, so we should print 2.


Sample Input 2

2
3 1 5 4

Sample Output 2

1

First, there will be two matches between Players 1 and 2 and between Players 3 and 4. According to the ratings, Players 1 and 3 will win.
Then, there will be a match between Players 1 and 3, and the tournament will end with Player 3 becoming champion.
The player who will lose in the final match is Player 1, so we should print 1.


Sample Input 3

4
6 13 12 5 3 7 10 11 16 9 8 15 2 1 14 4

Sample Output 3

2
D - Snuke Prime

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

株式会社すぬけは様々なサービスを提供しています。
この会社は、すぬけプライムという支払いプランを用意しています。
すぬけプライムへの加入中は、1 日あたり C 円を支払うことで、提供される全てのサービスを追加料金の支払いなしに利用することができます。
すぬけプライムへの加入および脱退は、それぞれ 1 日の始めおよび終わりに自由に行うことができます。

高橋くんは、この会社のサービスのうち N 個を利用しようとしています。
そのうち i 個目のサービスは、今日を 1 日目として、a_i 日目の始めから b_i 日目の終わりまで利用する予定です。
すぬけプライムに加入していない期間中は、i 個目のサービスを利用する際に 1 日あたり c_i 円を支払う必要があります。

サービスを利用するために高橋くんが支払う必要のある最小の合計金額を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq C \leq 10^9
  • 1 \leq a_i \leq b_i \leq 10^9
  • 1 \leq c_i \leq 10^9
  • 入力に含まれる値は全て整数

入力

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

N C
a_1 b_1 c_1
\vdots
a_N b_N c_N

出力

高橋くんが支払う必要のある最小の合計金額を出力せよ。


入力例 1

2 6
1 2 4
2 2 4

出力例 1

10

1 番目のサービスは 1 日目と 2 日目に、 2 番目のサービスは 2 日目に利用します。
2 日目のみすぬけプライムに加入すると、 1 日目に 4 円、 2 日目に 6 円がかかるため、高橋くんが支払う合計金額は 10 円です。
高橋くんが支払う金額を 10 円より少なくすることはできないため、 10 を出力します。


入力例 2

5 1000000000
583563238 820642330 44577
136809000 653199778 90962
54601291 785892285 50554
5797762 453599267 65697
468677897 916692569 87409

出力例 2

163089627821228

すぬけプライムに全く加入しないのが最適です。


入力例 3

5 100000
583563238 820642330 44577
136809000 653199778 90962
54601291 785892285 50554
5797762 453599267 65697
468677897 916692569 87409

出力例 3

88206004785464

Score : 400 points

Problem Statement

Snuke Inc. offers various kinds of services.
A payment plan called Snuke Prime is available.
In this plan, by paying C yen (the currency of Japan) per day, you can use all services offered by the company without additional fees.
You can start your subscription to this plan at the beginning of any day and cancel your subscription at the end of any day.

Takahashi is going to use N of the services offered by the company.
He will use the i-th of those services from the beginning of the a_i-th day until the end of the b_i-th day, where today is the first day.
Without a subscription to Snuke Prime, he has to pay c_i yen per day to use the i-th service.

Find the minimum total amount of money Takahashi has to pay to use the services.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq C \leq 10^9
  • 1 \leq a_i \leq b_i \leq 10^9
  • 1 \leq c_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 b_1 c_1
\vdots
a_N b_N c_N

Output

Print the minimum total amount of money Takahashi has to pay.


Sample Input 1

2 6
1 2 4
2 2 4

Sample Output 1

10

He will use the 1-st service on the 1-st and 2-nd days, and the 2-nd service on the 2-nd day.
If he subscribes to Snuke Prime only on the 2-nd day, he will pay 4 yen on the 1-st day and 6 yen on the 2-nd day, for a total of 10 yen.
It is impossible to make the total payment less than 10 yen, so we should print 10.


Sample Input 2

5 1000000000
583563238 820642330 44577
136809000 653199778 90962
54601291 785892285 50554
5797762 453599267 65697
468677897 916692569 87409

Sample Output 2

163089627821228

It is optimal to do without Snuke Prime.


Sample Input 3

5 100000
583563238 820642330 44577
136809000 653199778 90962
54601291 785892285 50554
5797762 453599267 65697
468677897 916692569 87409

Sample Output 3

88206004785464
E - Peddler

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

高橋国には、町 1 から町 N までの N 個の町があります。
また、この国には道 1 から道 M までの M 本の道があります。道 i を使うと、町 X_i から町 Y_i へ移動することができます。逆向きへは移動できません。ここで X_i < Y_i であることが保証されます。
この国では金の取引が盛んであり、町 i では、金 1\,\mathrm{kg}A_i 円で売ったり買ったりすることができます。

旅商人である高橋君は、高橋国内のある町で金を 1\,\mathrm{kg} だけ買い、いくつかの道を使った後、買った町とは別の町で金を 1\,\mathrm{kg} だけ売ろうと考えています。
このとき、高橋君の利益 (すなわち (金を売った価格) - (金を買った価格)) として考えられる最大値を求めてください。

制約

  • 2 \le N \le 2 \times 10^5
  • 1 \le M \le 2 \times 10^5
  • 1 \le A_i \le 10^9
  • 1 \le X_i \lt Y_i \le N
  • (X_i, Y_i) \neq (X_j, Y_j) (i \neq j)
  • 入力に含まれる値は全て整数

入力

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

N M
A_1 A_2 A_3 \dots A_N
X_1 Y_1
X_2 Y_2
X_3 Y_3
\hspace{15pt} \vdots
X_M Y_M

出力

答えを出力せよ。


入力例 1

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

出力例 1

3

以下のようにして利益 3 円を達成できます。

  • 12 円で金 1\,\mathrm{kg} を買う
  • 2 を使って町 2 に移動する
  • 1 を使って町 4 に移動する
  • 45 円で金 1\,\mathrm{kg} を売る

入力例 2

5 5
13 8 3 15 18
2 4
1 2
4 5
2 3
1 3

出力例 2

10

以下のようにして利益 10 円を達成できます。

  • 28 円で金 1\,\mathrm{kg} を買う
  • 1 を使って町 4 に移動する
  • 3 を使って町 5 に移動する
  • 518 円で金 1\,\mathrm{kg} を売る

入力例 3

3 1
1 100 1
2 3

出力例 3

-99

金を買った町で売ることはできないため、答えが負になる可能性があることに注意してください。

Score : 500 points

Problem Statement

In Takahashi Kingdom, there are N towns, called Town 1 through Town N.
There are also M roads in the kingdom, called Road 1 through Road M. By traversing Road i, you can travel from Town X_i to Town Y_i, but not vice versa. Here, it is guaranteed that X_i < Y_i.
Gold is actively traded in this kingdom. At Town i, you can buy or sell 1 kilogram of gold for A_i yen (the currency of Japan).

Takahashi, a traveling salesman, plans to buy 1 kilogram of gold at some town, traverse one or more roads, and sell 1 kilogram of gold at another town.
Find the maximum possible profit (that is, the selling price minus the buying price) in this plan.

Constraints

  • 2 \le N \le 2 \times 10^5
  • 1 \le M \le 2 \times 10^5
  • 1 \le A_i \le 10^9
  • 1 \le X_i \lt Y_i \le N
  • (X_i, Y_i) \neq (X_j, Y_j) (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 A_3 \dots A_N
X_1 Y_1
X_2 Y_2
X_3 Y_3
\hspace{15pt} \vdots
X_M Y_M

Output

Print the answer.


Sample Input 1

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

Sample Output 1

3

We can achieve the profit of 3 yen, as follows:

  • At Town 1, buy one kilogram of gold for 2 yen.
  • Traverse Road 2 to get to Town 2.
  • Traverse Road 1 to get to Town 4.
  • At Town 4, sell one kilogram of gold for 5 yen.

Sample Input 2

5 5
13 8 3 15 18
2 4
1 2
4 5
2 3
1 3

Sample Output 2

10

We can achieve the profit of 10 yen, as follows:

  • At Town 2, buy one kilogram of gold for 8 yen.
  • Traverse Road 1 to get to Town 4.
  • Traverse Road 3 to get to Town 5.
  • At Town 5, sell one kilogram of gold for 18 yen.

Sample Input 3

3 1
1 100 1
2 3

Sample Output 3

-99

Note that we cannot sell gold at the town where we bought the gold, which means the answer may be negative.

F - +1-1x2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

高橋君は黒板に整数 X を書きました。
高橋君は以下の 3 種類の操作を好きな順序で何回でも実行することができます。

  • 黒板に書かれている値を 1 増やす
  • 黒板に書かれている値を 1 減らす
  • 黒板に書かれている値を 2 倍する

高橋君が黒板に書かれている値を Y にするために必要な最小の操作回数を求めてください。

制約

  • 1 \le X \le 10^{18}
  • 1 \le Y \le 10^{18}
  • X, Y は整数である

入力

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

X Y

出力

答えを出力せよ。


入力例 1

3 9

出力例 1

3

最初、黒板には 3 が書かれています。以下の 3 回の操作で、これを 9 にすることができます。

  • 黒板に書かれている値を 1 増やす。黒板に書かれている値は 4 になる。
  • 黒板に書かれている値を 2 倍する。黒板に書かれている値は 8 になる。
  • 黒板に書かれている値を 1 増やす。黒板に書かれている値は 9 になる。

入力例 2

7 11

出力例 2

3

以下の手順で黒板に書かれている値を 11 にすることができます。

  • 黒板に書かれている値を 1 減らす。黒板に書かれている値は 6 になる。
  • 黒板に書かれている値を 2 倍する。黒板に書かれている値は 12 になる。
  • 黒板に書かれている値を 1 減らす。黒板に書かれている値は 11 になる。

入力例 3

1000000000000000000 1000000000000000000

出力例 3

0

最初から黒板に書かれている値が Y に等しい場合、0 を出力してください。

Score : 600 points

Problem Statement

Takahashi has written an integer X on a blackboard. He can do the following three kinds of operations any number of times in any order:

  • increase the value written on the blackboard by 1;
  • decrease the value written on the blackboard by 1;
  • multiply the value written on the blackboard by 2.

Find the minimum number of operations required to have Y written on the blackboard.

Constraints

  • 1 \le X \le 10^{18}
  • 1 \le Y \le 10^{18}
  • X and Y are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the answer.


Sample Input 1

3 9

Sample Output 1

3

Initially, 3 is written on the blackboard. The following three operations can make it 9:

  • increase the value by 1, which results in 4;
  • multiply the value by 2, which results in 8;
  • increase the value by 1, which results in 9.

Sample Input 2

7 11

Sample Output 2

3

The following procedure can make the value on the blackboard 11:

  • decrease the value by 1, which results in 6;
  • multiply the value by 2, which results in 12;
  • decrease the value by 1, which results in 11.

Sample Input 3

1000000000000000000 1000000000000000000

Sample Output 3

0

If the value initially written on the blackboard equals Y, print 0.