A - Thermometer

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

高橋君は自分の体温を計測し、 X {}^\circC であることがわかりました。

体温は次の 3 種類に分類されます。

  • 38.0 {}^\circC 以上のとき「高熱」
  • 37.5 {}^\circC 以上 38.0 {}^\circC 未満のとき「発熱」
  • 37.5 {}^\circC 未満のとき「平熱」

高橋君の体温は何に分類されるでしょうか?「出力」の項にしたがって整数で出力してください。

制約

  • 30 \leq X \leq 50
  • X は小数第一位まで与えられる

入力

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

X

出力

高橋君の体温が何に分類されるかを以下に定められる整数で出力せよ。

  • 高熱の場合: 1
  • 発熱の場合: 2
  • 平熱の場合: 3

入力例 1

40.0

出力例 1

1

高橋君の体温は 40.0 {}^\circC であるため、高熱に分類されます。よって、1 を出力します。


入力例 2

37.7

出力例 2

2

高橋君の体温は 37.7 {}^\circC であるため、発熱に分類されます。よって、2 を出力します。


入力例 3

36.6

出力例 3

3

高橋君の体温は 36.6 {}^\circC であるため、平熱に分類されます。よって、3 を出力します。

Score : 100 points

Problem Statement

Takahashi measured his body temperature and found it to be X {}^\circC.

Body temperature is classified into the following:

  • Higher than or equal to 38.0 {}^\circC: “High fever”
  • Higher than or equal to 37.5 {}^\circC and lower than 38.0 {}^\circC: “Fever”
  • Lower than 37.5 {}^\circC: “Normal”

Which classification does Takahashi's body temperature fall into? Present the answer as an integer according to the Output section.

Constraints

  • 30 \leq X \leq 50
  • X is given to one decimal place.

Input

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

X

Output

Print an integer specified below corresponding to Takahashi's body temperature classification.

  • High fever: 1
  • Fever: 2
  • Normal: 3

Sample Input 1

40.0

Sample Output 1

1

His body temperature is 40.0 {}^\circC, which is classified as a high fever. Thus, print 1.


Sample Input 2

37.7

Sample Output 2

2

His body temperature is 37.7 {}^\circC, which is classified as a fever. Thus, print 2.


Sample Input 3

36.6

Sample Output 3

3

His body temperature is 36.6 {}^\circC, which is classified as a normal temperature. Thus, print 3.

B - Stage Clear

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

高橋くんはゲームで遊んでいます。 このゲームにはワールドが 8 つあり、それぞれのワールドにはステージが 8 つずつあります。 ステージには順番があり、最初のステージは 1 個めのワールドにある 1 個めのステージです。 i 個め (1\le i\le 8) のワールドにある j 個め (1\le j\le 8) のステージの次のステージは、以下のようになっています。

  • j\lt8 のとき、次のステージは i 個めのワールドにある j+1 個めのステージです。
  • i\lt8,j=8 のとき、次のステージは i+1 個めのワールドにある 1 個めのステージです。
  • i=8,j=8 のとき、次のステージはありません。このステージが最後のステージです。

i 個め (1\le i\le 8) のワールドにある j 個め (1\le j\le 8) のステージの名前は i-j です。 たとえば、最初のステージ名は 1-1 で、最後のステージ名は 8-8 です。

高橋くんが現在遊んでいるステージのステージ名 S が与えられるので、次のステージのステージ名を出力してください。

制約

  • S はいずれかのステージのステージ名である。
  • S は 8-8 ではない。

入力

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

S

出力

次のステージのステージ名を出力せよ。


入力例 1

4-2

出力例 1

4-3

4 個めのワールドの 2 個めのステージの次のステージは、同じワールドの 3 個めのステージです。


入力例 2

1-8

出力例 2

2-1

1-8 は 1 個めのワールドの最後のステージなので、次のステージは 2 個めのワールドの 1 個めのステージです。


入力例 3

3-3

出力例 3

3-4

Score : 100 points

Problem Statement

Takahashi is playing a game. This game has eight worlds, and each world has eight stages. The stages have an order, and the first stage is the 1st stage in the 1st world. The next stage after the j-th stage (1\le j\le 8) in the i-th world (1\le i\le 8) is as follows:

  • When j\lt8, the next stage is the (j+1)-th stage in the i-th world.
  • When i\lt8,j=8, the next stage is the 1st stage in the (i+1)-th world.
  • When i=8,j=8, there is no next stage. This stage is the last stage.

The name of the j-th stage (1\le j\le 8) in the i-th world (1\le i\le 8) is i-j. For example, the name of the first stage is 1-1, and the name of the last stage is 8-8.

Given the stage name S of the stage Takahashi is currently playing, output the stage name of the next stage.

Constraints

  • S is the stage name of one of the stages.
  • S is not 8-8.

Input

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

S

Output

Output the stage name of the next stage.


Sample Input 1

4-2

Sample Output 1

4-3

The next stage after the 2nd stage in the 4th world is the 3rd stage in the same world.


Sample Input 2

1-8

Sample Output 2

2-1

1-8 is the last stage in the 1st world, so the next stage is the 1st stage in the 2nd world.


Sample Input 3

3-3

Sample Output 3

3-4
C - Permute to Minimize

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

正整数 X が与えられます。

X を(先頭に 0 を含まない形で)十進表記した際に現れる数字を、先頭に 0 が来ないように 並び替えることで得られる正整数のうち、値が最小のものを求めてください。

制約

  • 1\leq X < 10^5
  • X は整数

入力

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

X

出力

答えを出力せよ。


入力例 1

903

出力例 1

309

X を十進表記した際に現れる数字を先頭に 0 が来ないように並び替えることで得られる正整数は、903, 930, 309, 390 の 4 通りであり、このうち値が最小のものは 309 です。


入力例 2

432

出力例 2

234

入力例 3

100

出力例 3

100

Score : 200 points

Problem Statement

You are given a positive integer X.

Find the minimum value among all positive integers that can be obtained by rearranging the digits appearing in the decimal representation of X (without leading zeros) such that there is no leading zero.

Constraints

  • 1\leq X < 10^5
  • X is an integer.

Input

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

X

Output

Output the answer.


Sample Input 1

903

Sample Output 1

309

There are four positive integers that can be obtained by rearranging the digits appearing in the decimal representation of X such that there is no leading zero: 903, 930, 309, 390; the minimum value among them is 309.


Sample Input 2

432

Sample Output 2

234

Sample Input 3

100

Sample Output 3

100
D - Four Hidden

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250 点

問題文

英小文字と ? からなる文字列 T と、英小文字のみからなる文字列 U が与えられます。

T は、英小文字のみからなる文字列 S のちょうど 4 文字を ? で置き換えることで得られた文字列です。

S が U を連続部分文字列として含んでいた可能性があるかどうか判定してください。

制約

  • T は長さ 4 以上 10 以下の英小文字と ? からなる文字列
  • T にはちょうど 4 つの ? が含まれる
  • U は長さ 1 以上 |T| 以下の英小文字からなる文字列

入力

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

T
U

出力

S が U を部分文字列として含んでいた可能性があるならば Yes を、そうでないならば No を出力せよ。


入力例 1

tak??a?h?
nashi

出力例 1

Yes

例えば、S が takanashi である場合、これは nashi を連続部分文字列として含みます。


入力例 2

??e??e
snuke

出力例 2

No

? がどのような文字であっても、S は snuke を連続部分文字列として含むことがありません。


入力例 3

????
aoki

出力例 3

Yes

Score : 250 points

Problem Statement

You are given a string T consisting of lowercase English letters and ?, and a string U consisting of lowercase English letters.

The string T is obtained by taking some lowercase-only string S and replacing exactly four of its characters with ?.

Determine whether it is possible that the original string S contained U as a contiguous substring.

Constraints

  • T is a string of length between 4 and 10, inclusive, consisting of lowercase letters and ?.
  • T contains exactly four occurrences of ?.
  • U is a string of length between 1 and |T|, inclusive, consisting of lowercase letters.

Input

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

T
U

Output

Print Yes if it is possible that the original string S contained U as a contiguous substring; otherwise, print No.


Sample Input 1

tak??a?h?
nashi

Sample Output 1

Yes

For example, if S is takanashi, it contains nashi as a contiguous substring.


Sample Input 2

??e??e
snuke

Sample Output 2

No

No matter what characters replace the ?s in T, S cannot contain snuke as a contiguous substring.


Sample Input 3

????
aoki

Sample Output 3

Yes
E - Various Kagamimochi

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

N 個の餅が小さいほうから順に並んでいます。 i 番目 (1\leq i\leq N) の餅の大きさは A _ i です。

2 つの餅 A,B の大きさをそれぞれ a,b としたとき、a が b の半分以下であるとき、かつそのときに限り、餅 A を餅 B の上に乗せて鏡餅を 1 つ作ることができます。

N 個の餅から 2 つの餅を選び、一方をもう一方の上に乗せることで鏡餅を 1 つ作ります。

何種類の鏡餅を作ることができるか求めてください。

ただし、鏡餅を構成する餅の大きさが同じでも、少なくとも一方が異なる餅であれば別の種類の鏡餅として数えます。

制約

  • 2\leq N\leq 5\times 10 ^ 5
  • 1\leq A _ i\leq 10 ^ 9\ (1\leq i\leq N)
  • A _ i\leq A _ {i+1}\ (1\leq i\lt N)
  • 入力はすべて整数

入力

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

N
A _ 1 A _ 2 \cdots A _ N

出力

作ることができる鏡餅の種類数を出力せよ。


入力例 1

6
2 3 4 4 7 10

出力例 1

8

与えられた餅の大きさは以下のようになっています。

このとき、以下の 8 種類の鏡餅を作ることができます。

大きさ 4 の餅の上に大きさ 2 の餅を乗せた鏡餅や、大きさ 10 の餅の上に大きさ 4 の餅を乗せた鏡餅は 2 種類あることに注意してください。


入力例 2

3
387 388 389

出力例 2

0

鏡餅を 1 種類も作ることができない場合もあります。


入力例 3

32
1 2 4 5 8 10 12 16 19 25 33 40 50 64 87 101 149 175 202 211 278 314 355 405 412 420 442 481 512 582 600 641

出力例 3

388

Score : 300 points

Problem Statement

There are N mochi (rice cakes) arranged in ascending order of size. The size of the i-th mochi (1 \leq i \leq N) is A_i.

Given two mochi A and B, with sizes a and b respectively, you can make one kagamimochi (a stacked rice cake) by placing mochi A on top of mochi B if and only if a is at most half of b.

You choose two mochi out of the N mochi, and place one on top of the other to form one kagamimochi.

Find how many different kinds of kagamimochi can be made.

Two kagamimochi are distinguished if at least one of the mochi is different, even if the sizes of the mochi are the same.

Constraints

  • 2 \leq N \leq 5 \times 10^5
  • 1 \leq A_i \leq 10^9 \ (1 \leq i \leq N)
  • A_i \leq A_{i+1} \ (1 \leq i < N)
  • All input values are integers.

Input

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

N
A_1 A_2 \cdots A_N

Output

Print the number of different kinds of kagamimochi that can be made.


Sample Input 1

6
2 3 4 4 7 10

Sample Output 1

8

The sizes of the given mochi are as follows:

In this case, you can make the following eight kinds of kagamimochi:

Note that there are two kinds of kagamimochi where a mochi of size 4 is topped by a mochi of size 2, and two kinds where a mochi of size 10 is topped by a mochi of size 4.


Sample Input 2

3
387 388 389

Sample Output 2

0

It is possible that you cannot make any kagamimochi.


Sample Input 3

32
1 2 4 5 8 10 12 16 19 25 33 40 50 64 87 101 149 175 202 211 278 314 355 405 412 420 442 481 512 582 600 641

Sample Output 3

388
F - Max Even

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

A の異なる 2 要素の和として表せる値の中に偶数が存在するか判定し、存在する場合その最大値を求めてください。

制約

  • 2\leq N \leq 2\times 10^5
  • 0\leq A_i\leq 10^9
  • A の要素は相異なる
  • 入力は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

A の異なる 2 要素の和として表せる値の中に偶数が存在しない場合、-1 を出力せよ。

偶数が存在する場合、その最大値を出力せよ。


入力例 1

3
2 3 4

出力例 1

6

A の異なる 2 要素の和として表せる値は 5,6,7 です。この中に偶数は存在し、その最大値は 6 です。


入力例 2

2
1 0

出力例 2

-1

A の異なる 2 要素の和として表せる値は 1 です。この中に偶数は存在しないので、 -1 を出力してください。

Score : 300 points

Problem Statement

You are given a sequence A=(A_1,A_2,\ldots,A_N) of length N consisting of non-negative integers.

Determine if there is an even number represented as the sum of two different elements of A. If it exists, find the maximum such number.

Constraints

  • 2\leq N \leq 2\times 10^5
  • 0\leq A_i\leq 10^9
  • The elements of A are distinct.
  • All values in the input are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Print -1 if there is no even number represented as the sum of two different elements of A.

If such an even number exists, print the maximum such number.


Sample Input 1

3
2 3 4

Sample Output 1

6

The values represented as the sum of two distinct elements of A are 5, 6, and 7. We have an even number here, and the maximum is 6.


Sample Input 2

2
1 0

Sample Output 2

-1

The value represented as the sum of two distinct elements of A is 1. We have no even number here, so -1 should be printed.

G - Reconstruct Chocolate

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425 点

問題文

N 個の板チョコのピースが机の上に置かれています。 i\ (1 \leq i \leq N) 番目のピースは縦 h_i ブロック、横 w_i ブロックの長方形状です。

これらのピースは以下の方法で得られたことがわかっています。

  • はじめに縦 H ブロック、横 W ブロックの板チョコを手に持つ。机には何も置かれていない。
  • 以下の操作を N-1 回繰り返す。
    • 現在手に持っているピースを 2 つに分割する。分割した後のピースはどちらもブロックの境界に沿った長方形でなくてはならない。
    • 一方のピースを机に置き、もう一方のピースは手に持ったままにする。
  • 最後に手に持っているピースを机に置く。

以下の条件を満たすように N 個のピースを縦 H ブロック、横 W ブロックの長方形状に並べる方法を一つ求めてください。

  • 異なるピース同士は重ならない。
  • ピースは回転してはいけない。すなわち h\neq w に対して縦 h ブロック、横 w ブロックのピースを縦 w ブロック、横 h ブロックのピースとして配置することはできない。

制約

  • 1 \leq H \leq 10^9
  • 1 \leq W \leq 10^9
  • 2 \leq N \leq 2\times 10^5
  • 1 \leq h_i \leq H
  • 1 \leq w_i \leq W
  • 入力は問題文中の手順で得られたことが保証される
  • 入力はすべて整数

入力

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

H W N
h_1 w_1
h_2 w_2
\vdots
h_N w_N

出力

条件を満たす並べ方において i\ (1 \leq i \leq N) 番目のピースの一番左上のブロックが全体で上から x_i 行目、左から y_i 列目のブロックに位置するとき、以下の形式で出力せよ。

x_1 y_1
x_2 y_2
\vdots
x_N y_N

条件を満たす並べ方が複数存在する場合、どれを出力しても正解とみなされる。


入力例 1

4 6 6
2 2
1 4
3 1
1 2
3 1
4 2

出力例 1

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

出力例の並べ方を図示すると以下のようになります。


入力例 2

100 100 20
72 7
4 74
17 67
20 67
96 3
1 95
5 81
87 3
3 87
16 67
95 8
100 2
92 1
3 98
92 5
87 3
1 98
19 67
11 74
87 1

出力例 2

7 89
90 22
62 22
7 22
4 96
99 1
94 15
7 19
4 9
27 22
4 1
1 99
7 14
1 1
7 9
7 16
100 1
43 22
79 22
7 15

Score : 425 points

Problem Statement

There are N pieces of a chocolate bar placed on a table. Piece i\ (1 \leq i \leq N) is a rectangle of h_i blocks tall and w_i blocks wide.

It is known that these pieces were obtained by the following procedure.

  • Start by holding a chocolate bar of H blocks tall and W blocks wide. Nothing is placed on the table.
  • Repeat the following operation N-1 times.
    • Split the piece currently in hand into two pieces. Both pieces after splitting must be rectangles along the block boundaries.
    • Place one of the pieces on the table, and keep the other piece in hand.
  • Finally, place the piece in hand on the table.

Find one way to arrange the N pieces into a rectangle of H blocks tall and W blocks wide satisfying the following conditions.

  • Different pieces do not overlap.
  • Pieces must not be rotated. That is, for h \neq w, a piece of h blocks tall and w blocks wide cannot be placed as a piece of w blocks tall and h blocks wide.

Constraints

  • 1 \leq H \leq 10^9
  • 1 \leq W \leq 10^9
  • 2 \leq N \leq 2\times 10^5
  • 1 \leq h_i \leq H
  • 1 \leq w_i \leq W
  • It is guaranteed that the input was obtained by the procedure described in the problem statement.
  • All input values are integers.

Input

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

H W N
h_1 w_1
h_2 w_2
\vdots
h_N w_N

Output

In a valid arrangement, let the top-left block of piece i\ (1 \leq i \leq N) be located at the x_i-th row from the top and the y_i-th column from the left of the whole rectangle. Output in the following format:

x_1 y_1
x_2 y_2
\vdots
x_N y_N

If there are multiple valid arrangements, any of them will be accepted.


Sample Input 1

4 6 6
2 2
1 4
3 1
1 2
3 1
4 2

Sample Output 1

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

The arrangement in the sample output is illustrated below.


Sample Input 2

100 100 20
72 7
4 74
17 67
20 67
96 3
1 95
5 81
87 3
3 87
16 67
95 8
100 2
92 1
3 98
92 5
87 3
1 98
19 67
11 74
87 1

Sample Output 2

7 89
90 22
62 22
7 22
4 96
99 1
94 15
7 19
4 9
27 22
4 1
1 99
7 14
1 1
7 9
7 16
100 1
43 22
79 22
7 15
H - Max/Min

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475 点

問題文

長さ N の数列 A=(A_1,\ldots,A_N) が与えられます。

\displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^{N}\left\lfloor\frac{\max(A_i,A_j)}{\min(A_i,A_j)}\right\rfloor を求めてください。

ただし、\lfloor x \rfloor は x 以下の最大の整数を表します。例えば、\lfloor 3.14 \rfloor=3、\lfloor 2 \rfloor=2 です。

制約

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq 10^6
  • 入力は全て整数である

入力

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

N
A_1 \ldots A_N

出力

答えを出力せよ。


入力例 1

3
3 1 4

出力例 1

8

求める値は

\left\lfloor\frac{\max(3,1)}{\min(3,1)}\right\rfloor + \left\lfloor\frac{\max(3,4)}{\min(3,4)}\right\rfloor + \left\lfloor\frac{\max(1,4)}{\min(1,4)}\right\rfloor\\ =\left\lfloor\frac{3}{1}\right\rfloor + \left\lfloor\frac{4}{3}\right\rfloor + \left\lfloor\frac{4}{1}\right\rfloor\\ =3+1+4\\ =8

となります。


入力例 2

6
2 7 1 8 2 8

出力例 2

53

入力例 3

12
3 31 314 3141 31415 314159 2 27 271 2718 27182 271828

出力例 3

592622

Score : 475 points

Problem Statement

You are given a sequence A=(A_1,\ldots,A_N) of length N.

Find \displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^{N}\left\lfloor\frac{\max(A_i,A_j)}{\min(A_i,A_j)}\right\rfloor.

Here, \lfloor x \rfloor represents the greatest integer not greater than x. For example, \lfloor 3.14 \rfloor=3 and \lfloor 2 \rfloor=2.

Constraints

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

Input

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

N
A_1 \ldots A_N

Output

Print the answer.


Sample Input 1

3
3 1 4

Sample Output 1

8

The sought value is

\left\lfloor\frac{\max(3,1)}{\min(3,1)}\right\rfloor + \left\lfloor\frac{\max(3,4)}{\min(3,4)}\right\rfloor + \left\lfloor\frac{\max(1,4)}{\min(1,4)}\right\rfloor\\ =\left\lfloor\frac{3}{1}\right\rfloor + \left\lfloor\frac{4}{3}\right\rfloor + \left\lfloor\frac{4}{1}\right\rfloor\\ =3+1+4\\ =8.


Sample Input 2

6
2 7 1 8 2 8

Sample Output 2

53

Sample Input 3

12
3 31 314 3141 31415 314159 2 27 271 2718 27182 271828

Sample Output 3

592622
I - Takahashi in Narrow Road

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 550 点

問題文

東西に続く道があり、道の上には N 人の高橋くんがいます。 道は原点と呼ばれる点から東西に十分長く続いています。

i 番目 (1\leq i\leq N) の高橋くんは、はじめ原点から東に X _ i メートル進んだところにいます。

高橋くんたちは道の上を東西に動くことができます。 具体的には、次の移動を好きなだけ行うことができます。

  • 高橋くんを一人選ぶ。移動する先に他の高橋くんがいない場合、選んだ高橋くんを 1 メートル東に、もしくは西に移動させる。

高橋くんたちには合計 Q 個の用事があり、i 個目 (1\leq i\leq Q) の用事は次の形式で表されます。

  • T _ i 番目の高橋くんが座標 G _ i に到着する。

Q 個の用事を先頭から順にすべて完了するために必要な移動回数の最小値を求めてください。

制約

  • 1\leq N\leq2\times10 ^ 5
  • 0\leq X _ 1\lt X _ 2\lt\dotsb\lt X _ N\leq10 ^ 8
  • 1\leq Q\leq2\times10 ^ 5
  • 1\leq T _ i\leq N\ (1\leq i\leq Q)
  • 0\leq G _ i\leq10 ^ 8\ (1\leq i\leq Q)
  • 入力はすべて整数

入力

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

N
X _ 1 X _ 2 \ldots X _ N
Q
T _ 1 G _ 1
T _ 2 G _ 2
\vdots
T _ Q G _ Q

出力

答えを出力せよ。


入力例 1

5
10 20 30 40 50
4
3 45
4 20
1 35
2 60

出力例 1

239

高橋くんたちの最適な行動は以下のようになります(それぞれの高橋くんの座標は正確に描かれているとは限りません)。

それぞれの用事では、高橋くんたちは次のように移動しています。

  • 4 番目の高橋くんが 6 回東に進み、3 番目の高橋くんが 15 回東に進む。
  • 2 番目の高橋くんが 2 回西に進み、3 番目の高橋くんが 26 回西に進み、4 番目の高橋くんが 26 回西に進む。
  • 4 番目の高橋くんが 18 回東に進み、3 番目の高橋くんが 18 回東に進み、2 番目の高橋くんが 18 回東に進み、1 番目の高橋くんが 25 回東に進む。
  • 5 番目の高橋くんが 13 回東に進み、4 番目の高橋くんが 24 回東に進み、3 番目の高橋くんが 24 回東に進み、2 番目の高橋くんが 24 回東に進む。

高橋くんたちの移動回数の合計は 21+54+79+85=239 回となります。

移動回数の合計を 238 回以下としてすべての用事を完了することはできないため、239 を出力してください。


入力例 2

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

出力例 2

4294967297

途中で一部の高橋くんが原点より西側や、原点より 10 ^ 8+1 メートル以上東に進んだところに移動する必要がある場合があることに注意してください。

また、答えが 2 ^ {32} を超える場合があることに注意してください。


入力例 3

12
1558 3536 3755 3881 4042 4657 5062 7558 7721 8330 8542 9845
8
9 1694
7 3296
12 5299
5 5195
5 5871
1 2491
8 1149
8 2996

出力例 3

89644

Score : 550 points

Problem Statement

There is a road extending east and west, and N persons are on the road. The road extends infinitely long to the east and west from a point called the origin.

The i-th person (1\leq i\leq N) is initially at a position X_i meters east from the origin.

The persons can move along the road to the east or west. Specifically, they can perform the following movement any number of times.

  • Choose one person. If there is no other person at the destination, move the chosen person 1 meter east or west.

They have Q tasks in total, and the i-th task (1\leq i\leq Q) is as follows.

  • The T_i-th person arrives at coordinate G_i.

Find the minimum total number of movements required to complete all Q tasks in order.

Constraints

  • 1\leq N\leq2\times10^5
  • 0\leq X_1 < X_2 < \dotsb < X_N \leq10^8
  • 1\leq Q\leq2\times10^5
  • 1\leq T_i\leq N\ (1\leq i\leq Q)
  • 0\leq G_i\leq10^8\ (1\leq i\leq Q)
  • All input values are integers.

Input

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

N
X_1 X_2 \ldots X_N
Q
T_1 G_1
T_2 G_2
\vdots
T_Q G_Q

Output

Print the answer.


Sample Input 1

5
10 20 30 40 50
4
3 45
4 20
1 35
2 60

Sample Output 1

239

An optimal sequence of movements for the persons is as follows (the positions of the persons are not necessarily drawn to scale):

For each task, the persons move as follows.

  • The 4th person moves 6 steps east, and the 3rd person moves 15 steps east.
  • The 2nd person moves 2 steps west, the 3rd person moves 26 steps west, and the 4th person moves 26 steps west.
  • The 4th person moves 18 steps east, the 3rd person moves 18 steps east, the 2nd person moves 18 steps east, and the 1st person moves 25 steps east.
  • The 5th person moves 13 steps east, the 4th person moves 24 steps east, the 3rd person moves 24 steps east, and the 2nd person moves 24 steps east.

The total number of movements is 21+54+79+85=239.

You cannot complete all tasks with a total movement count of 238 or less, so print 239.


Sample Input 2

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

Sample Output 2

4294967297

Note that some persons may need to move to the west of the origin or more than 10^8 meters to the east of it.

Also, note that the answer may exceed 2^{32}.


Sample Input 3

12
1558 3536 3755 3881 4042 4657 5062 7558 7721 8330 8542 9845
8
9 1694
7 3296
12 5299
5 5195
5 5871
1 2491
8 1149
8 2996

Sample Output 3

89644