A - ABC -> AC

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

配点 : 100

問題文

英大文字からなる文字列 S が与えられます。ここで、S の長さは奇数です。

S の中央の文字を削除して得られる文字列を出力してください。ただし、S の中央の文字とは S の長さを L として S\frac{L+1}{2} 文字目の文字を指します。

制約

  • S は英大文字からなる文字列
  • S の長さは 3 以上 9 以下の奇数

入力

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

S

出力

問題文中の指示に従い、文字列を出力せよ。


入力例 1

ABCDE

出力例 1

ABDE

ABCDE の中央の文字は 3 文字目の C であるため、ABDE を出力します。


入力例 2

OOO

出力例 2

OO

入力例 3

ATCODER

出力例 3

ATCDER

Score : 100 points

Problem Statement

You are given a string S consisting of uppercase English letters. Here, the length of S is odd.

Print the string obtained by deleting the middle character of S. The middle character of S is the \frac{L+1}{2}-th character of S, where L is the length of S.

Constraints

  • S is a string consisting of uppercase English letters.
  • The length of S is an odd number between 3 and 9, inclusive.

Input

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

S

Output

Print the string as instructed in the problem statement.


Sample Input 1

ABCDE

Sample Output 1

ABDE

The middle character of ABCDE is the 3-rd character C, so print ABDE.


Sample Input 2

OOO

Sample Output 2

OO

Sample Input 3

ATCODER

Sample Output 3

ATCDER
B - Sum of Digits Sequence

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

配点 : 200

問題文

正整数 x に対して、f(x)x の十進表記における各桁の和として定義します。例えば、f(123) = 1 + 2 + 3 = 6 です。

無限数列 A = (A_0, A_1, A_2, \ldots) を以下の式により定義します。

  • A_0 = 1
  • i \geq 1 のとき A_i = \displaystyle\sum_{j = 0}^{i - 1} f(A_j)

正整数 N が与えられます。A_N の値を求めてください。

制約

  • N1 以上 100 以下の整数

入力

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

N

出力

答えを出力せよ。


入力例 1

6

出力例 1

23
  • A_0 = 1
  • A_1 = f(A_0) = 1
  • A_2 = f(A_0) + f(A_1) = 2
  • A_3 = f(A_0) + f(A_1) + f(A_2) = 4
  • A_4 = f(A_0) + f(A_1) + f(A_2) + f(A_3) = 8
  • A_5 = f(A_0) + f(A_1) + f(A_2) + f(A_3) + f(A_4) = 16
  • A_6 = f(A_0) + f(A_1) + f(A_2) + f(A_3) + f(A_4) + f(A_5) = 23

であるため、A_6 = 23 です。


入力例 2

45

出力例 2

427

Score : 200 points

Problem Statement

For a positive integer x, define f(x) as the sum of the digits in the decimal representation of x. For example, f(123) = 1 + 2 + 3 = 6.

Define an infinite sequence A = (A_0, A_1, A_2, \ldots) by the following formula:

  • A_0 = 1
  • For i \geq 1, A_i = \displaystyle\sum_{j = 0}^{i - 1} f(A_j)

You are given a positive integer N. Find the value of A_N.

Constraints

  • N is an integer between 1 and 100, inclusive.

Input

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

N

Output

Print the answer.


Sample Input 1

6

Sample Output 1

23
  • A_0 = 1
  • A_1 = f(A_0) = 1
  • A_2 = f(A_0) + f(A_1) = 2
  • A_3 = f(A_0) + f(A_1) + f(A_2) = 4
  • A_4 = f(A_0) + f(A_1) + f(A_2) + f(A_3) = 8
  • A_5 = f(A_0) + f(A_1) + f(A_2) + f(A_3) + f(A_4) = 16
  • A_6 = f(A_0) + f(A_1) + f(A_2) + f(A_3) + f(A_4) + f(A_5) = 23

Thus, A_6 = 23.


Sample Input 2

45

Sample Output 2

427
C - Bipartize

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



配点 : 350

問題文

N 頂点 M 辺の単純な無向グラフがあります。 グラフは頂点 1, 頂点 2,\ldots, 頂点 N からなり、i 番目 (1\le i\le M) の辺は頂点 u _ i と頂点 v _ i を結んでいます。

あなたは、次の操作を 0 回以上行います。

  • まだ削除されていない辺をひとつ選び、削除する。

あなたの目的はグラフを二部グラフにすることです。 操作を行ったあとのグラフを二部グラフにするために、最低でも何回操作を行う必要があるか求めてください。

グラフが単純であるとは

グラフが単純であるとは、自己ループ(u _ i=v _ i となる辺)や多重辺(u _ i=u _ j かつ v _ i=v _ j となる辺のペア)が存在しないことをいいます。

二部グラフとは

二部グラフとは、頂点をそれぞれ黒か白のどちらか一色で塗り、次の条件を満たすことができるグラフのことをいいます。

  • どの辺についても、その辺が結んでいるふたつの頂点に塗られた色は異なる。

制約

  • 2\le N\le10
  • 1\le M\le\dfrac{N(N-1)}2
  • 1\le u _ i\lt v _ i\le N\ (1\le i\le M)
  • 与えられるグラフは単純
  • 入力はすべて整数

入力

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

N M
u _ 1 v _ 1
u _ 2 v _ 2
\vdots
u _ M v _ M

出力

操作を行ったあとのグラフを二部グラフにするために、行う必要がある操作の回数を出力せよ。


入力例 1

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

出力例 1

2

例えば、頂点 1 と頂点 3 を結ぶ辺、頂点 3 と頂点 5 を結ぶ辺の 2 つを削除することでグラフを二部グラフにすることができます。

操作を 1 回以下行うことでグラフを二部グラフにすることはできないため、2 を出力してください。


入力例 2

2 1
1 2

出力例 2

0

グラフははじめから二部グラフです。 よって、行う必要がある操作の回数は 0 回です。


入力例 3

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

出力例 3

5

Score : 350 points

Problem Statement

There is a simple undirected graph with N vertices and M edges. The graph consists of vertex 1, vertex 2,\ldots, vertex N, and the i-th edge (1\le i\le M) connects vertices u _ i and v _ i.

You will perform the following operation zero or more times:

  • Choose one edge that has not been deleted yet, and delete it.

Your goal is to make the graph bipartite. Find the minimum number of operations needed to make the graph after the operations bipartite.

What it means for a graph to be simple?

A graph is simple if and only if it has no self-loops (edges where u _ i=v _ i) or multi-edges (pairs of edges where u _ i=u _ j and v _ i=v _ j).

What is a bipartite graph?

A bipartite graph is a graph where it is possible to color each vertex black or white satisfying the following condition:

  • For every edge, the two vertices connected by that edge have different colors.

Constraints

  • 2\le N\le10
  • 1\le M\le\dfrac{N(N-1)}2
  • 1\le u _ i\lt v _ i\le N\ (1\le i\le M)
  • The given graph is simple.
  • All input values are integers.

Input

The 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

Print the number of operations that need to be performed to make the graph bipartite.


Sample Input 1

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

Sample Output 1

2

You can make the graph bipartite by deleting two edges: for example, the edge connecting vertices 1 and 3, and the edge connecting vertices 3 and 5.

It is impossible to make the graph bipartite by performing one or less operations, so print 2.


Sample Input 2

2 1
1 2

Sample Output 2

0

The graph is bipartite from the beginning. Thus, the number of operations that need to be performed is 0.


Sample Input 3

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

Sample Output 3

5
D - The Simple Game

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

配点 : 425

問題文

N 頂点 M 辺の有向グラフがあります。頂点には 1 から N までの番号が付けられており、i 番目の辺は頂点 U_i から頂点 V_i へ向かう有向辺です。ここで、各頂点の出次数は 1 以上です。

また、各頂点には文字が書かれており、頂点 i に書かれている文字は S_i です。ただし、S_i とは Si 文字目を指します。

Alice と Bob はこのグラフ上で 1 つの駒を用いて以下のゲームを行います。

  • はじめ、駒は頂点 1 に置かれており、Alice が先手、Bob が後手となって以下の操作を交互に K 回ずつ行う。

    • 現在駒が置かれている頂点を u とする。頂点 u から頂点 v に向かう辺が存在するような頂点 v を選び、駒を頂点 v に移動させる。
  • 最終的に駒が置かれている頂点を v として、S_v = A のとき Alice の勝ち、S_v = B のとき Bob の勝ちである。

両者が最適に行動したときのゲームの勝者を求めてください。

1 つの入力において T 個のテストケースが与えられます。それぞれについて答えてください。

制約

  • 1 \leq T
  • 2 \leq N, M \leq 2 \times 10^5
  • 1 \leq K \leq 10
  • SA, B からなる長さ N の文字列
  • 1 \leq U_i, V_i \leq N
  • i \neq j のとき (U_i, V_i) \neq (U_j, V_j)
  • 各頂点の出次数は 1 以上
  • 1 つの入力に含まれるテストケースについて、N の総和は 2 \times 10^5 以下
  • 1 つの入力に含まれるテストケースについて、M の総和は 2 \times 10^5 以下

入力

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

T
\text{case}_1
\text{case}_2
\ldots
\text{case}_T

ここで、\text{case}_ii 番目のテストケースを表し、各テストケースは以下の形式で与えられる。

N M K
S
U_1 V_1
U_2 V_2
\vdots
U_M V_M

出力

T 行出力せよ。i 行目には、i 番目のテストケースにおいて両者が最適に行動したときに Alice が勝つ場合 Alice を、Bob が勝つ場合 Bob を出力せよ。


入力例 1

3
4 6 2
AABB
1 2
2 3
3 1
3 3
3 4
4 2
4 6 2
ABAB
1 2
2 3
3 1
3 3
3 4
4 2
5 8 3
ABABB
1 2
2 2
2 3
3 1
3 4
4 4
4 5
5 3

出力例 1

Alice
Bob
Bob

1 番目のテストケースについてゲームの進行の一例を説明します。ただし、この進行において両者は最適に行動しているとは限りません。

  • はじめ、駒は頂点 1 に置かれている。
  • Alice が頂点 2 に駒を動かす。
  • Bob が頂点 3 に駒を動かす。
  • Alice が頂点 3 に駒を動かす。
  • Bob が頂点 1 に駒を動かす。

このとき、S_1 = A であるため Alice の勝ちとなります。

Score : 425 points

Problem Statement

There is a directed graph with N vertices and M edges. The vertices are numbered from 1 to N, and the i-th edge is a directed edge from vertex U_i to vertex V_i. Here, the out-degree of each vertex is at least 1.

Also, each vertex has a character written on it, and the character written on vertex i is S_i. Here, S_i refers to the i-th character of S.

Alice and Bob play the following game on this graph using one piece:

  • Initially, the piece is placed on vertex 1, and they alternately perform the following operation K times each, with Alice going first and Bob going second.

    • Let u be the vertex where the piece is currently placed. Choose a vertex v such that there is an edge from vertex u to vertex v, and move the piece to vertex v.
  • Let v be the vertex where the piece is finally placed. If S_v = A, Alice wins; if S_v = B, Bob wins.

Find the winner of the game when both players play optimally.

In each input, you are given T test cases. Solve each of them.

Constraints

  • 1 \leq T
  • 2 \leq N, M \leq 2 \times 10^5
  • 1 \leq K \leq 10
  • S is a string of length N consisting of A and B.
  • 1 \leq U_i, V_i \leq N
  • (U_i, V_i) \neq (U_j, V_j) if i \neq j .
  • The out-degree of each vertex is at least 1.
  • The sum of N over all test cases in a single input is at most 2 \times 10^5.
  • The sum of M over all test cases in a single input is at most 2 \times 10^5.

Input

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

T
\text{case}_1
\text{case}_2
\ldots
\text{case}_T

Here, \text{case}_i represents the i-th test case, and each test case is given in the following format:

N M K
S
U_1 V_1
U_2 V_2
\vdots
U_M V_M

Output

Print T lines. The i-th line should contain Alice if Alice wins when both players play optimally in the i-th test case, and Bob if Bob wins.


Sample Input 1

3
4 6 2
AABB
1 2
2 3
3 1
3 3
3 4
4 2
4 6 2
ABAB
1 2
2 3
3 1
3 3
3 4
4 2
5 8 3
ABABB
1 2
2 2
2 3
3 1
3 4
4 4
4 5
5 3

Sample Output 1

Alice
Bob
Bob

We will explain an example of how the game proceeds in the first test case. Here, both players do not necessarily play optimally.

  • Initially, the piece is placed on vertex 1.
  • Alice moves the piece to vertex 2.
  • Bob moves the piece to vertex 3.
  • Alice moves the piece to vertex 3.
  • Bob moves the piece to vertex 1.

At this point, S_1 = A, so Alice wins.

E - Wind Cleaning

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

配点 : 500

問題文

HW 列のグリッドがあります。このグリッドの上から i 行目、左から j 列目のマスを (i, j) と表記します。

高橋君はこのグリッドの 1 つのマスの上におり、グリッド上のいくつかのマスにはゴミが落ちています。

各マスの状態は H 個の長さ W の文字列 S_1, S_2, \ldots, S_H によって与えられ、S_ij 文字目を S_{i, j} として、以下のような状態を表します。

  • S_{i, j} = T のときマス (i, j) は高橋君がいるマスである。
  • S_{i, j} = # のときマス (i, j) はゴミが落ちているマスである。
  • S_{i, j} = . のときマス (i, j) はゴミが落ちていない空きマスである。

また、高橋君がいるマスにはゴミは落ちていません。

高橋君は、以下の操作を繰り返し行うことができます。

  • 上下左右のいずれかの方向を 1 つ選び、すべてのゴミを同時にその方向へ 1 マス移動させる。ただし、ゴミがグリッドの外に出た場合にはゴミは消滅し、高橋君のいるマスにゴミが移動した場合は高橋君は汚れる。

高橋君が汚れることなくすべてのゴミを消滅させることができるか判定し、できる場合は操作を行う回数の最小値を求めてください。

制約

  • 2 \leq H, W \leq 12
  • S_i は長さ WT, #, . からなる文字列
  • S_{i, j} = T なる (i, j) がちょうど 1 つ存在する
  • S_{i, j} = # なる (i, j)1 つ以上存在する

入力

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

H W
S_1
S_2
\vdots
S_H

出力

高橋君が汚れることなくすべてのゴミを消滅させることができる場合、操作を行う回数として考えられる最小値を出力せよ。そうでないとき、-1 と出力せよ。


入力例 1

3 4
###.
#.T.
...#

出力例 1

3

高橋君は以下のように操作を行うことで 3 回の操作ですべてのゴミを消滅させることができます。

  • 上を選ぶ。この操作後、ゴミはマス (1, 1) とマス (2, 4) にある。

  • 右を選ぶ。この操作後、ゴミはマス (1, 2) にある。

  • 上を選ぶ。この操作後、すべてのゴミは消滅している。


入力例 2

3 3
###
#T#
###

出力例 2

-1

入力例 3

5 5
###..
#T...
..##.
##..#
#..#.

出力例 3

5

Score : 500 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.

Takahashi is on one of the cells in this grid, and there is trash on some cells of the grid.

The states of the cells are given by H strings S_1, S_2, \ldots, S_H of length W, where S_{i, j} denotes the j-th character of S_i and represents the following state:

  • If S_{i, j} = T, cell (i, j) is the cell where Takahashi is.
  • If S_{i, j} = #, cell (i, j) is a cell with trash.
  • If S_{i, j} = ., cell (i, j) is an empty cell without trash.

There is no trash on the cell where Takahashi is.

He can repeatedly perform the following operation:

  • Choose one of the four directions (up, down, left, or right), and move all trash simultaneously one cell in that direction. Here, if trash goes outside the grid, the trash disappears, and if trash moves to the cell where he is, he gets dirty.

Determine whether it is possible for him to make all trash disappear without getting dirty, and if possible, find the minimum possible number of operations.

Constraints

  • 2 \leq H, W \leq 12
  • S_i is a string of length W consisting of T, #, and ..
  • There is exactly one (i, j) such that S_{i, j} = T.
  • There is at least one (i, j) such that S_{i, j} = #.

Input

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

H W
S_1
S_2
\vdots
S_H

Output

If it is possible for Takahashi to make all trash disappear without getting dirty, print the minimum possible number of operations. Otherwise, print -1.


Sample Input 1

3 4
###.
#.T.
...#

Sample Output 1

3

Takahashi can make all trash disappear in three operations as follows:

  • Choose up. After this operation, trash is on cells (1, 1) and (2, 4).

  • Choose right. After this operation, trash is on cell (1, 2).

  • Choose up. After this operation, all trash has disappeared.


Sample Input 2

3 3
###
#T#
###

Sample Output 2

-1

Sample Input 3

5 5
###..
#T...
..##.
##..#
#..#.

Sample Output 3

5
F - Not Adjacent

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



配点 : 525

問題文

長さ N の整数列 A=(A _ 1,A _ 2,\ldots,A _ N) が与えられます。

A の(連続するとは限らない)部分列は 2 ^ N 通りあります。 部分列 (A _ {i _ 1},A _ {i _ 2},\ldots,A _ {i _ k})\ (1\le i _ 1\lt i _ 2\lt\cdots\lt i _ k\le N) であって、次の 2 つの条件の両方を満たすものがいくつあるか求めてください。

  • 選ばれた要素は A において隣接しない。つまり、すべての 1\le j\lt k に対して 1+i _ j\ne i _ {j+1} が成り立つ。
  • 総和が M の倍数である。つまり、\displaystyle\sum _ {j=1} ^ kA _ {i _ j}\equiv0\pmod M

2 つの部分列が整数列として等しくても、取り出す位置が異なるならそれら 2 つの部分列を区別して数えることに注意してください。

制約

  • 1\le N\le60
  • 1\le M\le10 ^ 9
  • 0\le A _ i\lt M
  • 入力はすべて整数

入力

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

N M
A _ 1 A _ 2 \ldots A _ N

出力

答えを出力せよ。


入力例 1

7 6
3 1 4 1 5 3 2

出力例 1

6

以下の 6 つの部分列が条件を満たします。

  • ()=()
  • (A _ 1,A _ 3,A _ 5)=(3,4,5)
  • (A _ 1,A _ 4,A _ 7)=(3,1,2)
  • (A _ 1,A _ 6)=(3,3)
  • (A _ 2,A _ 5)=(1,5)
  • (A _ 3,A _ 7)=(4,2)

なので、6 を出力してください。


入力例 2

15 10
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5

出力例 2

798

入力例 3

20 998244353
778718481 719092922 676292280 713825156 0 434453766 620370916 67922064 0 577696866 21516423 955580612 955580612 0 332156721 0 0 0 632133714 500614291

出力例 3

40

Score : 525 points

Problem Statement

You are given a length-N integer sequence A=(A _ 1,A _ 2,\ldots,A _ N).

There are 2 ^ N (not necessarily contiguous) subsequences of A. Find how many subsequences (A _ {i _ 1},A _ {i _ 2},\ldots,A _ {i _ k})\ (1\le i _ 1\lt i _ 2\lt\cdots\lt i _ k\le N) satisfy both of the following two conditions:

  • The selected elements are not adjacent in A. That is, 1+i _ j\ne i _ {j+1} holds for all 1\le j\lt k.
  • The sum is a multiple of M. That is, \displaystyle\sum _ {j=1} ^ kA _ {i _ j}\equiv0\pmod M.

Even if two subsequences are equal as integer sequences, they are counted separately if the positions from which they are taken are different.

Constraints

  • 1\le N\le60
  • 1\le M\le10 ^ 9
  • 0\le A _ i\lt M
  • All input values are integers.

Input

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

N M
A _ 1 A _ 2 \ldots A _ N

Output

Print the answer.


Sample Input 1

7 6
3 1 4 1 5 3 2

Sample Output 1

6

The following six subsequences satisfy the conditions:

  • ()=()
  • (A _ 1,A _ 3,A _ 5)=(3,4,5)
  • (A _ 1,A _ 4,A _ 7)=(3,1,2)
  • (A _ 1,A _ 6)=(3,3)
  • (A _ 2,A _ 5)=(1,5)
  • (A _ 3,A _ 7)=(4,2)

Thus, print 6.


Sample Input 2

15 10
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5

Sample Output 2

798

Sample Input 3

20 998244353
778718481 719092922 676292280 713825156 0 434453766 620370916 67922064 0 577696866 21516423 955580612 955580612 0 332156721 0 0 0 632133714 500614291

Sample Output 3

40
G - Takahashi's Expectation 2

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



配点 : 625

問題文

高橋くんは、これからいくつかのプレゼントをもらいます。

高橋くんにはテンションという整数のパラメータがあり、テンションはプレゼントをもらうごとに変動します。 それぞれのプレゼントは価値 P という整数のパラメータをもち、このパラメータによって高橋くんのテンションは次のように変動します。

  • もらったプレゼントの価値 P がテンションの値以上であるとき、高橋くんはプレゼントに喜び、テンションが A だけ増加する。
  • もらったプレゼントの価値 P がテンションの値より小さいとき、高橋くんはプレゼントにがっかりし、テンションが B だけ減少する。

はじめ、高橋くんが受け取る予定のプレゼントは N 個あり、i 番目 (1\le i\le N) に受け取るプレゼントの価値は P _ i です。

追加クエリ、質問クエリの 2 種類からなる Q 個のクエリが与えられるので、その全てを順に処理し、すべての質問クエリに答えてください。

i 番目のクエリは 2 つの整数 T _ i,X _ i によって表され、T _ i=1 のとき追加クエリ、T _ i=2 のとき質問クエリです。

追加クエリでは、受け取る予定のプレゼントの末尾に、価値が X _ i であるプレゼントを新たに追加します。

質問クエリでは、次の質問に答えてください。

高橋くんのテンションがはじめ X _ i だったときの、受け取る予定のプレゼントをすべて受け取ったあとの高橋くんのテンションを求めよ。

制約

  • 1\le N\le2\times10 ^ 5
  • 1\le A\le10 ^ 9
  • 1\le B\le10 ^ 9
  • -10 ^ 9\le P _ i\le10 ^ 9\ (1\le i\le N)
  • 1\le Q\le2\times10 ^ 5
  • T _ i=1 もしくは T _ i=2\ (1\le i\le Q)
  • T _ i=2 となるような整数 i\ (1\le i\le Q) が存在する
  • T _ i=1 ならば -10 ^ 9\le X _ i\le10 ^ 9\ (1\le i\le Q)
  • T _ i=2 ならば -10 ^ {12}\le X _ i\le10 ^ {12}\ (1\le i\le Q)
  • 入力はすべて整数

入力

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

N A B
P _ 1 P _ 2 \ldots P _ N
Q
T _ 1 X _ 1
T _ 2 X _ 2
\vdots
T _ Q X _ Q

出力

質問クエリの個数を q 個として、q 行にわたって出力せよ。 i 行目 (1\le i\le q) には、i 個めの質問クエリに対する答えを出力せよ。


入力例 1

4 31 41
59 -26 53 58
5
2 9
1 79
2 32
1 38
2 462

出力例 1

61
43
216

はじめ、高橋くんが受け取る予定のプレゼントの価値は、受け取る順にそれぞれ 59,-26,53,58 です。

5 回のクエリは、それぞれ次のようになっています。

  • 高橋くんのテンションがはじめ 9 であったとき、プレゼントを受け取るたびに高橋くんのテンションは 9\rightarrow40\rightarrow-1\rightarrow30\rightarrow61 と変動するため、最終的なテンションである 61 を出力します。
  • 高橋くんが受け取る予定のプレゼントに価値が 79 のプレゼントが追加され、高橋くんが受け取る予定のプレゼントの価値が 59,-26,53,58,79 となります。
  • 高橋くんのテンションがはじめ 32 であったとき、プレゼントを受け取るたびに高橋くんのテンションは 32\rightarrow63\rightarrow22\rightarrow53\rightarrow84\rightarrow43 と変動するため、最終的なテンションである 43 を出力します。
  • 高橋くんが受け取る予定のプレゼントに価値が 38 のプレゼントが追加され、高橋くんが受け取る予定のプレゼントの価値が 59,-26,53,58,79,38 となります。
  • 高橋くんのテンションがはじめ 462 であったとき、プレゼントを受け取るたびに高橋くんのテンションは 462\rightarrow421\rightarrow380\rightarrow339\rightarrow298\rightarrow257\rightarrow216 と変動するため、最終的なテンションである 216 を出力します。

入力例 2

3 1000000000 1000000000
1000000000 0 -1000000000
3
2 -1000000000000
2 0
2 1000000000000

出力例 2

-997000000000
-1000000000
997000000000

入力や出力すべき値の絶対値が 2 ^ {32} 以上になる場合があることに注意してください。


入力例 3

20 8489 1428
6312 -9511 1048 -5301 -2588 -7097 -3342 5209 7493 3378 -5300 6592 7862 -8118 8109 1116 5549 3953 9244 7773
10
1 1694
2 -9723
2 -5195
2 -1384
1 1149
2 9845
2 -7720
2 8329
2 -4652
2 -5672

出力例 3

9874
14402
8296
8180
10449
6664
3600
12497

Score : 625 points

Problem Statement

Takahashi will receive some presents from now on.

He has an integer parameter called mood, and the mood changes each time he receives a present. Each present has an integer parameter called value P, and Takahashi's mood changes according to this parameter as follows:

  • If the value P of the received present is greater than or equal to the mood value, he is delighted with the present, and the mood increases by A.
  • If the value P of the received present is less than the mood value, he is disappointed with the present, and the mood decreases by B.

Initially, there are N presents that Takahashi is scheduled to receive, and the value of the i-th present he will receive (1\le i\le N) is P _ i.

You are given Q queries consisting of two types: addition queries and question queries. Process all of them in order, and answer all question queries.

The i-th query is represented by two integers T _ i,X _ i, and it is an addition query if T _ i=1, and a question query if T _ i=2.

In an addition query, add a new present with value X _ i to the end of the presents to be received.

In a question query, answer the following question:

Find Takahashi's mood after receiving all the presents he is scheduled to receive when his mood is initially X _ i.

Constraints

  • 1\le N\le2\times10 ^ 5
  • 1\le A\le10 ^ 9
  • 1\le B\le10 ^ 9
  • -10 ^ 9\le P _ i\le10 ^ 9\ (1\le i\le N)
  • 1\le Q\le2\times10 ^ 5
  • T _ i=1 or T _ i=2\ (1\le i\le Q)
  • There exists an integer i\ (1\le i\le Q) such that T _ i=2.
  • If T _ i=1, then -10 ^ 9\le X _ i\le10 ^ 9. (1\le i\le Q)
  • If T _ i=2, then -10 ^ {12}\le X _ i\le10 ^ {12}. (1\le i\le Q)
  • All input values are integers.

Input

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

N A B
P _ 1 P _ 2 \ldots P _ N
Q
T _ 1 X _ 1
T _ 2 X _ 2
\vdots
T _ Q X _ Q

Output

Let q be the number of question queries, and print q lines. The i-th line (1\le i\le q) should contain the answer to the i-th question query.


Sample Input 1

4 31 41
59 -26 53 58
5
2 9
1 79
2 32
1 38
2 462

Sample Output 1

61
43
216

Initially, the values of the presents Takahashi is scheduled to receive are 59,-26,53,58, in the order he will receive them.

The five queries are as follows:

  • When his mood is initially 9, his mood changes as 9\rightarrow40\rightarrow-1\rightarrow30\rightarrow61 each time he receives a present, so print the final mood 61.
  • A present with value 79 is added to the presents he is scheduled to receive, and the values of the presents he is scheduled to receive become 59,-26,53,58,79.
  • When his mood is initially 32, his mood changes as 32\rightarrow63\rightarrow22\rightarrow53\rightarrow84\rightarrow43 each time he receives a present, so print the final mood 43.
  • A present with value 38 is added to the presents he is scheduled to receive, and the values of the presents he is scheduled to receive become 59,-26,53,58,79,38.
  • When his mood is initially 462, his mood changes as 462\rightarrow421\rightarrow380\rightarrow339\rightarrow298\rightarrow257\rightarrow216 each time he receives a present, so print the final mood 216.

Sample Input 2

3 1000000000 1000000000
1000000000 0 -1000000000
3
2 -1000000000000
2 0
2 1000000000000

Sample Output 2

-997000000000
-1000000000
997000000000

Note that the absolute value of the input and the values to be output may be 2 ^ {32} or greater.


Sample Input 3

20 8489 1428
6312 -9511 1048 -5301 -2588 -7097 -3342 5209 7493 3378 -5300 6592 7862 -8118 8109 1116 5549 3953 9244 7773
10
1 1694
2 -9723
2 -5195
2 -1384
1 1149
2 9845
2 -7720
2 8329
2 -4652
2 -5672

Sample Output 3

9874
14402
8296
8180
10449
6664
3600
12497