A - A Multiply

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 300

問題文

長さ N の整数列 A=(A_1,A_2,\dots,A_N) と整数 C が与えられます。
以下の操作を 高々 1 行って達成できる A の全要素の総和の最大値を求めてください。

  • 1 \le l \le r \le N を満たす整数 l,r を指定し、 A_l,A_{l+1},\dots,A_r の全ての要素を C 倍する。

制約

  • 入力は全て整数
  • 1 \le N \le 3 \times 10^5
  • -10^6 \le C \le 10^6
  • -10^6 \le A_i \le 10^6

入力

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

N C
A_1 A_2 \dots A_N

出力

答えを整数として出力せよ。


入力例 1

5 2
-10 10 20 30 -20

出力例 1

90

この入力では、 A=(-10,10,20,30,-20), C=2 です。
l=2,r=4 と指定して操作を 1 度行うことで、操作後の A(-10,20,40,60,-20) とすることができます。
このとき A の全要素の総和は 90 となり、これが達成可能な最大値です。


入力例 2

5 1000000
-1 -2 -3 -4 -5

出力例 2

-15

この入力では、 A=(-1,-2,-3,-4,-5), C=1000000 です。
操作を一度も行わないとき A の全要素の総和は -15 となり、これが達成可能な最大値です。


入力例 3

9 -1
-9 9 -8 2 -4 4 -3 5 -3

出力例 3

13

Score: 300 points

Problem Statement

You are given an integer sequence of length N, A=(A_1,A_2,\dots,A_N), and an integer C.
Find the maximum possible sum of the elements in A after performing the following operation at most once:

  • Specify integers l and r such that 1 \le l \le r \le N, and multiply each of A_l,A_{l+1},\dots,A_r by C.

Constraints

  • All input values are integers.
  • 1 \le N \le 3 \times 10^5
  • -10^6 \le C \le 10^6
  • -10^6 \le A_i \le 10^6

Input

The input is given from Standard Input in the following format:

N C
A_1 A_2 \dots A_N

Output

Print the answer as an integer.


Sample Input 1

5 2
-10 10 20 30 -20

Sample Output 1

90

In this input, A=(-10,10,20,30,-20), C=2.
After performing the operation once specifying l=2 and r=4, A will be (-10,20,40,60,-20).
Here, the sum of the elements in A is 90, which is the maximum value achievable.


Sample Input 2

5 1000000
-1 -2 -3 -4 -5

Sample Output 2

-15

In this input, A=(-1,-2,-3,-4,-5), C=1000000.
Without performing the operation, the sum of the elements in A is -15, which is the maximum value achievable.


Sample Input 3

9 -1
-9 9 -8 2 -4 4 -3 5 -3

Sample Output 3

13
B - Bought Review

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 300

問題文

T 個のテストケースについて、次の問題に答えてください。

グルメレビューサイトである EatCocoder では、レストランに 1 以上 5 以下の整数個の星を付けてレビューすることができます。
最初、B料理長の経営するレストランには、星 i のレビューが A_i 件付いています。 (1 \le i \le 5)
B料理長は EatCocoder の運営に P_i 円の賄賂を渡すことで、星 i のレビューを 1 件追加してもらえます。 (1 \le i \le 5)

賄賂によってレビューを全部で k 件追加したとき、最終的なレビューは合計で A_1+A_2+A_3+A_4+A_5+k 件になります。
B料理長はこれらのレビューの平均評価を星 3 以上にしたいと考えています。これを達成するために必要な賄賂の合計金額の最小値を求めてください。

制約

  • 入力は全て整数
  • 1 \le T \le 10^4
  • 0 \le A_i \le 10^8
  • 1 \le A_1+A_2+A_3+A_4+A_5
  • 1 \le P_i \le 10^8

入力

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

T
\rm{Case}_1
\rm{Case}_2
\vdots
\rm{Case}_T

但し、 \rm{Case}_ii 個目のテストケースを意味する。
各テストケースは以下の形式で与えられる。

A_1 A_2 A_3 A_4 A_5
P_1 P_2 P_3 P_4 P_5

出力

全体で T 行出力せよ。
そのうち i 行目には、 i 個目のテストケースに対する答えを整数として出力せよ。


入力例 1

6
1 0 1 0 0
1 2 3 4 5
0 2 2 0 0
1 1 1 1 5
0 1 2 0 0
1 1 1 5 3
1 1 1 0 0
1 1 1 1 1
0 0 0 0 1
1 1 1 1 1
100000000 100000000 100000000 0 0
100000000 100000000 100000000 100000000 100000000

出力例 1

5
2
3
2
0
15000000000000000

この入力には 6 個のテストケースが含まれています。

  • 1 個目のテストケースについて、例えば以下のようにすると 5 円の賄賂で平均評価を星 3 以上にでき、これが達成可能な最小値です。
    • 元々星 1,2,3,4,5 のレビューが 1,0,1,0,0 件付いています。
    • P_5 = 5 円の賄賂を渡し、星 5 のレビューを 1 件追加させます。
    • その結果、星 1,2,3,4,5 のレビューが 1,0,1,0,1 件となり、これらの平均は星 3 です。
  • 2 個目のテストケースについて、例えば以下のようにすると 2 円の賄賂で平均評価を星 3 以上にでき、これが達成可能な最小値です。
    • 元々星 1,2,3,4,5 のレビューが 0,2,2,0,0 件付いています。
    • P_4 \times 2 = 2 円の賄賂を渡し、星 4 のレビューを 2 件追加させます。
    • その結果、星 1,2,3,4,5 のレビューが 0,2,2,2,0 件となり、これらの平均は星 3 です。
  • 3 個目のテストケースについて、例えば以下のようにすると 3 円の賄賂で平均評価を星 3 以上にでき、これが達成可能な最小値です。
    • 元々星 1,2,3,4,5 のレビューが 0,1,2,0,0 件付いています。
    • P_5 = 3 円の賄賂を渡し、星 5 のレビューを 1 件追加させます。
    • その結果、星 1,2,3,4,5 のレビューが 0,1,2,0,1 件となり、これらの平均は星 3.25 です。
  • 4 個目のテストケースについて、例えば以下のようにすると 2 円の賄賂で平均評価を星 3 以上にでき、これが達成可能な最小値です。
    • 元々星 1,2,3,4,5 のレビューが 1,1,1,0,0 件付いています。
    • P_4 = 1 円の賄賂を渡し、星 4 のレビューを 1 件追加させます。
    • P_5 = 1 円の賄賂を渡し、星 5 のレビューを 1 件追加させます。
    • その結果、星 1,2,3,4,5 のレビューが 1,1,1,1,1 件となり、これらの平均は星 3 です。
  • 5 個目のテストケースについて、例えば以下のようにすると 0 円の賄賂で平均評価を星 3 以上にでき、これが達成可能な最小値です。
    • 元々星 1,2,3,4,5 のレビューが 0,0,0,0,1 件付いています。
    • これらの平均は星 5 でありこれは既に 3 以上であるため、賄賂を全く渡しません。
  • 6 個目のテストケースについて、答えが 32bit 符号付き整数に収まらないこともあります。

Score: 300 points

Problem Statement

Solve the following problem for T test cases.

On the gourmet review site EatCocoder, you can review restaurants with an integer number of stars from 1 to 5.
Initially, the restaurant managed by Chef B has A_i reviews with i stars. (1 \le i \le 5)
The chef can pay a bribe of P_i yen to the EatCocoder administration to have one additional i-star review. (1 \le i \le 5)

After adding a total of k reviews by bribery, there will be A_1+A_2+A_3+A_4+A_5+k reviews in total.
Chef B wants the average rating of these reviews to be at least 3 stars. Determine the minimum total amount of bribery required to achieve this.

Constraints

  • All input values are integers.
  • 1 \le T \le 10^4
  • 0 \le A_i \le 10^8
  • 1 \le A_1+A_2+A_3+A_4+A_5
  • 1 \le P_i \le 10^8

Input

The input is given from Standard Input in the following format:

T
\rm{Case}_1
\rm{Case}_2
\vdots
\rm{Case}_T

Here, \rm{Case}_i represents the i-th test case.
Each test case is given in the following format:

A_1 A_2 A_3 A_4 A_5
P_1 P_2 P_3 P_4 P_5

Output

Print T lines in total.
The i-th line should contain the answer to the i-th test case as an integer.


Sample Input 1

6
1 0 1 0 0
1 2 3 4 5
0 2 2 0 0
1 1 1 1 5
0 1 2 0 0
1 1 1 5 3
1 1 1 0 0
1 1 1 1 1
0 0 0 0 1
1 1 1 1 1
100000000 100000000 100000000 0 0
100000000 100000000 100000000 100000000 100000000

Sample Output 1

5
2
3
2
0
15000000000000000

This input contains six test cases.

  • For the first test case, you can, for example, do the following to have an average rating of at least 3 stars with a bribe of 5 yen, which is the minimum possible amount.
    • Initially, there are 1,0,1,0,0 reviews with 1,2,3,4,5 stars, respectively.
    • Pay a bribe of P_5 = 5 yen to add one 5-star review.
    • As a result, there are 1,0,1,0,1 reviews with 1,2,3,4,5 stars, respectively, averaging 3 stars.
  • For the second test case, you can, for example, do the following to have an average rating of at least 3 stars with a bribe of 2 yen, which is the minimum possible amount.
    • Initially, there are 0,2,2,0,0 reviews with 1,2,3,4,5 stars, respectively.
    • Pay a bribe of P_4 \times 2 = 2 yen to add two 4-star reviews.
    • As a result, there are 0,2,2,2,0 reviews of with 1,2,3,4,5 stars, respectively, averaging 3 stars.
  • For the third test case, you can, for example, do the following to have an average rating of at least 3 stars with a bribe of 3 yen, which is the minimum possible amount.
    • Initially, there are 0,1,2,0,0 reviews with 1,2,3,4,5 stars, respectively.
    • Pay a bribe of P_5 = 3 yen to add one 5-star review.
    • As a result, there are 0,1,2,0,1 reviews with 1,2,3,4,5 stars, respectively, averaging 3.25 stars.
  • For the fourth test case, you can, for example, do the following to have an average rating of at least 3 stars with a bribe of 2 yen, which is the minimum possible amount.
    • Initially, there are 1,1,1,0,0 reviews with 1,2,3,4,5 stars, respectively.
    • Pay a bribe of P_4 = 1 yen to add one 4-star review.
    • Pay a bribe of P_5 = 1 yen to add one 5-star review.
    • As a result, there are 1,1,1,1,1 reviews with 1,2,3,4,5 stars, respectively, averaging 3 stars.
  • For the fifth test case, you can, for example, do the following to have an average rating of at least 3 stars with a bribe of 0 yen, which is the minimum possible amount.
    • Initially, there are 0,0,0,0,1 reviews with 1,2,3,4,5 stars, respectively.
    • Since the average is already 5, which is not less than 3, give no bribe.
  • For the sixth test case, note that the answer may not fit into a 32-bit signed integer.
C - Catastrophic Roulette

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 500

問題文

整数 1,2,\dots,N が均等な確率で出るルーレットがあります。
これを使って 2 人で以下のゲームを行います。

  • 先攻と後攻が交互にルーレットを回す。
    • 出た整数が今までに出ていないものであった場合、何も起こらない。
    • そうでない場合、ルーレットを回したプレイヤーが罰金 1 円を支払う。
  • N 個の整数全てが少なくとも 1 度出たとき、直ちにゲームが終了する。

先攻後攻それぞれについて、ゲームが終了するまでに支払う罰金の期待値を \text{mod}\ 998244353 で求めてください。

期待値 \text{mod } 998244353 の定義

この問題で求める期待値は必ず有理数になることが証明できます。 また、この問題の制約下では、求める期待値を既約分数 \frac{y}{x} で表したときに x998244353 で割り切れないことが保証されます。

このとき xz \equiv y \pmod{998244353} を満たすような 0 以上 998244352 以下の整数 z が一意に定まります。この z を答えてください。

制約

  • N1 \le N \le 10^6 を満たす整数

入力

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

N

出力

答えとして 2 つの整数を出力せよ。
そのうち 1 つ目は先攻が支払う罰金の期待値、 2 つ目は後攻が支払う罰金の期待値をそれぞれ \text{mod}\ 998244353 で表現した整数である。


入力例 1

1

出力例 1

0 0

この入力では N=1 です。
先攻がルーレットを回すと必ず 1 が出て、直ちにゲームが終了します。
よって、支払う罰金の期待値は先攻後攻ともに 0 です。


入力例 2

2

出力例 2

332748118 665496236

この入力では N=2 です。ゲームの進行の一例は以下の通りです。

  • 先攻がルーレットを回し、 2 が出た。この時、何も起こらない。
  • 後攻がルーレットを回し、 2 が出た。この時、後攻は罰金 1 円を支払う。
  • 先攻がルーレットを回し、 2 が出た。この時、先攻は罰金 1 円を支払う。
  • 後攻がルーレットを回し、 1 が出た。この時、何も起こらない。
  • この時点で 1,2 が少なくとも 1 度出たので、直ちにゲームが終了する。
  • このようにゲームが進行した場合、先攻が支払う罰金は 1 円、後攻が支払う罰金も 1 円となります。

先攻が支払う罰金の期待値は \frac{1}{3} 円、後攻が支払う罰金の期待値は \frac{2}{3} 円であることが示せます。


入力例 3

3

出力例 3

174692763 324429416

Points: 500 points

Problem Statement

There is a roulette that produces an integer from 1,2,\dots,N with equal probability.
Two players use it to play the following game:

  • The players take turns spinning the roulette.
    • If the produced integer has not appeared before, nothing happens.
    • Otherwise, the player who spun the roulette pays a fine of 1 yen.
  • The game immediately ends when all of the N integers have appeared at least once.

For each of the first and second players, find the expected value of the amount of the fine paid before the game ends, modulo 998244353.

On expected values modulo 998244353

It can be proved that the expected values sought in this problem are always rational numbers. Furthermore, under the constraints of this problem, when the expected values are expressed as irreducible fractions \frac{y}{x}, it is guaranteed that x is not divisible by 998244353.

Now, there is a unique integer z between 0 and 998244352, inclusive, such that xz \equiv y \pmod{998244353}. Provide this z as the answer.

Constraints

  • N is an integer such that 1 \le N \le 10^6.

Input

The input is given from Standard Input in the following format:

N

Output

Print two integers as the answer.
The first is the expected value of the amount of the fine paid by the first player, and the second is the expected value of the amount of the fine paid by the second player, represented as integers modulo 998244353.


Sample Input 1

1

Sample Output 1

0 0

In this input, N=1.
When the first player spins the roulette, it always produces 1, ending the game immediately.
Thus, the expected values of the amounts of the fines paid by the first and second players are both 0.


Sample Input 2

2

Sample Output 2

332748118 665496236

In this input, N=2. Here is a possible progression of the game:

  • The first player spins the roulette, and it produces 2. Nothing happens.
  • The second player spins the roulette, and it produces 2. The second player pays a fine of 1 yen.
  • The first player spins the roulette, and it produces 2. The first player pays a fine of 1 yen.
  • The second player spins the roulette, and it produces 1. Nothing happens.
  • At this point, both 1 and 2 have appeared at least once, so the game immediately ends.
  • In this progression, the amount of the fine paid by the first player is 1 yen, and the amount of the fine paid by the second player is also 1 yen.

It can be shown that the expected value of the amount of the fine paid by the first player is \frac{1}{3} yen, and the expected value of the amount of the fine paid by the second player is \frac{2}{3} yen.


Sample Input 3

3

Sample Output 3

174692763 324429416
D - Digit vs Square Root

実行時間制限: 2 sec / メモリ制限: 1024 MB

配点 : 500

問題文

T 個のテストケースについて、以下の問題に答えてください。

整数 N が与えられるので、以下の条件を全て満たす整数 x の個数を求めてください。

  • 1 \le x \le N
  • y = \lfloor \sqrt{x} \rfloor とする。このとき、 x,y 双方を (先頭に 0 を含まずに) 十進法で書き下した場合、 yx の接頭辞になる。

制約

  • T1 \le T \le 10^5 を満たす整数
  • N1 \le N \le 10^{18} を満たす整数

入力

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

T
N_1
N_2
\vdots
N_T

但し、 N_ii 個目のテストケースにおける整数 N を表す。

出力

全体で T 行出力せよ。
そのうち i 行目には、 i 個目のテストケースに対する答えを整数として出力せよ。


入力例 1

2
1
174

出力例 1

1
22

この入力には、 2 個のテストケースが含まれます。

  • 1 つ目のテストケースについて、 x=1y = \lfloor \sqrt{1} \rfloor = 1 となり問題文中の条件を満たします。
  • 2 つ目のテストケースについて、例えば x=100y = \lfloor \sqrt{100} \rfloor = 10 となり問題文中の条件を満たします。

Score: 500 points

Problem Statement

Solve the following problem for T test cases.

Given an integer N, find the number of integers x that satisfy all of the following conditions:

  • 1 \le x \le N
  • Let y = \lfloor \sqrt{x} \rfloor. When x and y are written in decimal notation (without leading zeros), y is a prefix of x.

Constraints

  • T is an integer such that 1 \le T \le 10^5.
  • N is an integer such that 1 \le N \le 10^{18}.

Input

The input is given from Standard Input in the following format:

T
N_1
N_2
\vdots
N_T

Here, N_i represents the integer N for the i-th test case.

Output

Print T lines in total. The i-th line should contain the answer for the i-th test case as an integer.


Sample Input 1

2
1
174

Sample Output 1

1
22

This input contains two test cases.

  • For the first test case, x=1 satisfies the conditions since y = \lfloor \sqrt{1} \rfloor = 1.
  • For the second test case, for example, x=100 satisfies the conditions since y = \lfloor \sqrt{100} \rfloor = 10.
E - Existence Counting

実行時間制限: 4 sec / メモリ制限: 1024 MB

配点 : 700

問題文

整数 N,K が与えられます。このとき、以下の条件を全て満たす長さ K の数列 a=(a_1,a_2,\dots,a_K) を考えます。

  • a_i1 \le a_i \le N を満たす整数である
  • a の全ての要素は相異なる

a として考えられる数列を辞書順に全て並べた 「数列の列」 を辞書 s とします。

辞書 s 中に存在する数列 P が与えられるので、整数 t=1,2,\dots,N に対して以下の質問に答えてください。

  • 以下の条件を全て満たす数列 b の個数を 998244353 で割った余りを求めよ。
    • 数列 b は辞書 s 中に存在する。
    • 数列 b 中に整数 t が含まれる。
    • 数列 b は辞書順で数列 P 以下である。
数列の辞書順とは? 数列 A = (A_1, \ldots, A_{|A|})B = (B_1, \ldots, B_{|B|}) より辞書順で真に小さいとは、下記の 1. と 2. のどちらかが成り立つことを言います。
  1. |A|<|B| かつ (A_{1},\ldots,A_{|A|}) = (B_1,\ldots,B_{|A|}) である。
  2. ある整数 1\leq i\leq \min\{|A|,|B|\} が存在して、下記の 2 つがともに成り立つ。
    • (A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1})
    • A_i < B_i

制約

  • 入力は全て整数
  • 1 \le K \le N \le 3 \times 10^5
  • P は問題文中の条件を満たす。

入力

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

N K
P_1 P_2 \dots P_K

出力

全体で N 行出力せよ。
このうち i 行目には、 t=i であるときの質問の答えを整数として出力せよ。


入力例 1

4 2
3 2

出力例 1

5
5
4
2

この入力では、 N=4,K=2 です。
このとき、辞書 s=((1,2),(1,3),(1,4),(2,1),(2,3),(2,4),(3,1),(3,2),(3,4),(4,1),(4,2),(4,3)) となります。

辞書 s に含まれ、かつ辞書順で (3,2) 以下である数列のうち、

  • 1 が含まれるものは (1,2),(1,3),(1,4),(2,1),(3,1)5
  • 2 が含まれるものは (1,2),(2,1),(2,3),(2,4),(3,2)5
  • 3 が含まれるものは (1,3),(2,3),(3,1),(3,2)4
  • 4 が含まれるものは (1,4),(2,4)2

です。


入力例 2

18 13
5 13 11 2 18 1 10 15 17 4 12 7 3

出力例 2

925879409
905921009
665544804
665544719
783035803
349952762
349952758
349952757
349952757
349921178
212092637
710350150
378895603
129113201
129111892
129098081
129096772
110181652

Score: 700 points

Problem Statement

You are given integers N and K. Consider a sequence a=(a_1,a_2,\dots,a_K) of length K that satisfies all of the following conditions:

  • a_i is an integer such that 1 \le a_i \le N.
  • All elements in a are different.

Let us arrange all possible sequences a in lexicographical order to form a "sequence of sequences" called the dictionary s.

Given a sequence P that exists in the dictionary s, answer the following question for each integer t=1,2,\dots,N:

  • Find the number, modulo 998244353, of sequences b that satisfy all of the following conditions:
    • The sequence b exists in the dictionary s.
    • The integer t is contained in the sequence b.
    • The sequence b is lexicographically less than or equal to the sequence P.
What is lexicographical order for sequences? A sequence A = (A_1, \ldots, A_{|A|}) is lexicographically strictly less than B = (B_1, \ldots, B_{|B|}) if and only if 1. or 2. below is satisfied:
  1. |A|<|B| and (A_{1},\ldots,A_{|A|}) = (B_1,\ldots,B_{|A|}).
  2. There is an integer 1\leq i\leq \min\{|A|,|B|\} that satisfies both of the following:
    • (A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1})
    • A_i < B_i

Constraints

  • All input values are integers.
  • 1 \le K \le N \le 3 \times 10^5
  • P satisfies the condition in the problem statement.

Input

The input is given from Standard Input in the following format:

N K
P_1 P_2 \dots P_K

Output

Print N lines in total.
The i-th line should contain the answer to the problem for t=i as an integer.


Sample Input 1

4 2
3 2

Sample Output 1

5
5
4
2

In this input, N=4,K=2.
Here, the dictionary s is ((1,2),(1,3),(1,4),(2,1),(2,3),(2,4),(3,1),(3,2),(3,4),(4,1),(4,2),(4,3)).

Among the sequences in the dictionary s that are lexicographically less than or equal to (3,2),

  • five sequences contain 1: (1,2),(1,3),(1,4),(2,1),(3,1),
  • five sequences contain 2: (1,2),(2,1),(2,3),(2,4),(3,2),
  • four sequences contain 3: (1,3),(2,3),(3,1),(3,2),
  • two sequences contain 4: (1,4),(2,4).

Sample Input 2

18 13
5 13 11 2 18 1 10 15 17 4 12 7 3

Sample Output 2

925879409
905921009
665544804
665544719
783035803
349952762
349952758
349952757
349952757
349921178
212092637
710350150
378895603
129113201
129111892
129098081
129096772
110181652
F - Final Stage

実行時間制限: 4 sec / メモリ制限: 1024 MB

配点 : 900

問題文

プレイヤーである Alice と Bob は、長さ N の数列 L,R を用いて以下のゲームを行います。

  • ゲームは N ターンからなる。
  • i が奇数のとき i ターン目は Alice の手番であり、 i が偶数の時 i ターン目は Bob の手番である。
  • 最初、いくつかの石を積んだ山をひとつ用意する。
  • i=1,2,\dots,N の順に以下の操作 ( i ターン目と呼ぶ ) を行う。
    • i ターン目に手番のプレイヤーは、山から L_i 個以上 R_i 個以下の整数個の石を取る。
    • もし上記を満たすように石を取ることができない場合、手番のプレイヤーは敗北し、もう片方のプレイヤーが勝利する。
  • N ターン目を終えた時点でどちらのプレイヤーも敗北していない場合、ゲームは引き分けで終了する。

ゲーム開始前に、両プレイヤーに対して 数列 L,R とゲーム開始時点で山にある石の個数 は知らされています。

このとき、このゲームは以下の 3 通りの 解析結果 のうち丁度ひとつに当てはまることが証明できます。

  • Alice ... Alice に必勝法が存在する。
  • Bob ... Bob に必勝法が存在する。
  • Draw ... どちらのプレイヤーにも必勝法は存在しない。

このゲームについて、 Q 個の質問に答えてください。 i 個目の質問は以下の通りです。

  • ゲーム開始時点で山に C_i 個の石がある場合を考えます。ゲームの解析結果を Alice, Bob, Draw のいずれかで答えてください。

制約

  • N,L_i,R_i,Q,C_i は整数
  • 1 \le N \le 3 \times 10^5
  • 1 \le L_i \le R_i \le 10^9
  • 1 \le Q \le 3 \times 10^5
  • 1 \le C_i \le \sum_{i=1}^{N} R_i

入力

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

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N
Q
C_1
C_2
\vdots
C_Q

出力

全体で Q 行出力せよ。
そのうち i 行目には、 i 個目の質問に対する答えを出力せよ。


入力例 1

4
1 3
1 2
3 4
1 2
11
1
2
3
4
5
6
7
8
9
10
11

出力例 1

Alice
Alice
Alice
Bob
Bob
Alice
Alice
Alice
Draw
Draw
Draw

この入力には 11 個の質問が含まれます。

  • C_i \le 3 のとき、 1 ターン目で Alice が C_i 個の石を取って山に残る石を 0 個にできるので、 Alice に必勝法が存在します。
  • 4 \le C_i \le 5 のとき、 Bob に必勝法が存在します。
  • 6 \le C_i \le 8 のとき、 Alice に必勝法が存在します。
  • 9 \le C_i のとき、どちらにも必勝法は存在しません。
    • C_i=9 である場合のゲームの進行の一例を示します。
      • 1 ターン目で Alice が 3 個の石を取る。山に残る石は 6 個である。
      • 2 ターン目で Bob が 1 個の石を取る。山に残る石は 5 個である。
      • 3 ターン目で Alice が 4 個の石を取る。山に残る石は 1 個である。
      • 4 ターン目で Bob が 1 個の石を取る。山に残る石は 0 個である。
      • 4 ターン目を終えた時点でどちらのプレイヤーも敗北していないので、ゲームは引き分けで終了する。
    • 他にも様々な進行が考えられますが、 C_i=9 のときどちらにも必勝法が無い (両者が勝利に対し最善を尽くした場合、ゲームは引き分けで終了する) ことが示せます。

Points: 900 points

Problem Statement

Players Alice and Bob play a game using sequences L and R of length N, as follows.

  • The game consists of N turns.
  • If i is odd, turn i is played by Alice; if i is even, turn i is played by Bob.
  • Initially, there is a pile with some number of stones.
  • For i=1,2,\dots,N in this order, they perform the following operation (called turn i):
    • The player who plays turn i takes an integer number of stones between L_i and R_i, inclusive, from the pile.
    • If the player cannot take stones satisfying the above, they lose, and the other player wins.
  • If neither player has lost by the end of turn N, the game ends in a draw.

Before the game starts, both players are informed of the sequences L and R and the number of stones in the pile at the start of the game.

It can be proved that the game has exactly one of the following three consequences:

  • Alice ... Alice has a winning strategy.
  • Bob ... Bob has a winning strategy.
  • Draw ... Neither player has a winning strategy.

Answer Q queries about this game. The i-th query is as follows:

  • Assume that the pile contains C_i stones at the start of the game. Report the consequence of the game: Alice, Bob, or Draw.

Constraints

  • N, L_i, R_i, Q, and C_i are integers.
  • 1 \le N \le 3 \times 10^5
  • 1 \le L_i \le R_i \le 10^9
  • 1 \le Q \le 3 \times 10^5
  • 1 \le C_i \le \sum_{i=1}^{N} R_i

Input

The input is given from Standard Input in the following format:

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N
Q
C_1
C_2
\vdots
C_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.


Sample Input 1

4
1 3
1 2
3 4
1 2
11
1
2
3
4
5
6
7
8
9
10
11

Sample Output 1

Alice
Alice
Alice
Bob
Bob
Alice
Alice
Alice
Draw
Draw
Draw

This input contains 11 queries.

  • When C_i \le 3, Alice can take all C_i stones on turn 1, leaving no stones in the pile, so Alice has a winning strategy.
  • When 4 \le C_i \le 5, Bob has a winning strategy.
  • When 6 \le C_i \le 8, Alice has a winning strategy.
  • When C_i \ge 9, neither player has a winning strategy.
    • For example, if C_i=9, the game could proceed as follows:
      • On turn 1, Alice takes 3 stones. 6 stones remain.
      • On turn 2, Bob takes 1 stone. 5 stones remain.
      • On turn 3, Alice takes 4 stones. 1 stone remains.
      • On turn 4, Bob takes 1 stone. No stones remain.
      • Since neither player has lost by the end of turn 4, the game ends in a draw.
    • Various other progressions are possible, but it can be shown that when C_i=9, neither player has a winning strategy (if both players play optimally, the game will end in a draw).