A - アルバイトの給料計算

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

配点 : 200

問題文

高橋君は今月、 N 種類のアルバイトをしました。

i 番目のアルバイトの時給は A_i 円で、高橋君はそのアルバイトを T_i 時間働きました。

高橋君が今月受け取る給料の合計金額(円)を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq 10^6
  • 1 \leq T_i \leq 10^6
  • 入力はすべて整数である

入力

N
A_1 T_1
A_2 T_2
\vdots
A_N T_N
  • 1 行目には、アルバイトの種類数を表す N が与えられる。
  • 続く N 行では、各アルバイトの情報が与えられる。
  • 1 + i 行目には、 i 番目のアルバイトの時給を表す A_i と、働いた時間を表す T_i が、スペース区切りで与えられる。

出力

高橋君が受け取る給料の合計金額(円)を 1 行で出力してください。


入力例 1

3
1000 5
1200 3
900 4

出力例 1

12200

入力例 2

2
850 1
1500 2

出力例 2

3850

入力例 3

7
980 12
1250 8
1500 6
1100 10
2000 3
950 15
1750 4

出力例 3

69010

入力例 4

15
1000 40
1200 35
950 28
1500 20
1300 22
1600 18
1100 30
1750 16
1400 25
2000 12
1050 27
1800 14
1550 19
900 33
1450 21

出力例 4

459150

入力例 5

1
1000000 1000000

出力例 5

1000000000000

Score : 200 pts

Problem Statement

Takahashi worked N different part-time jobs this month.

The hourly wage of the i-th job is A_i yen, and Takahashi worked T_i hours at that job.

Find the total salary (in yen) that Takahashi will receive this month.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq 10^6
  • 1 \leq T_i \leq 10^6
  • All input values are integers

Input

N
A_1 T_1
A_2 T_2
\vdots
A_N T_N
  • The first line contains N, the number of types of part-time jobs.
  • The following N lines contain the information for each job.
  • The (1 + i)-th line contains A_i, the hourly wage of the i-th job, and T_i, the number of hours worked, separated by a space.

Output

Print the total salary (in yen) that Takahashi will receive, on a single line.


Sample Input 1

3
1000 5
1200 3
900 4

Sample Output 1

12200

Sample Input 2

2
850 1
1500 2

Sample Output 2

3850

Sample Input 3

7
980 12
1250 8
1500 6
1100 10
2000 3
950 15
1750 4

Sample Output 3

69010

Sample Input 4

15
1000 40
1200 35
950 28
1500 20
1300 22
1600 18
1100 30
1750 16
1400 25
2000 12
1050 27
1800 14
1550 19
900 33
1450 21

Sample Output 4

459150

Sample Input 5

1
1000000 1000000

Sample Output 5

1000000000000
B - 料理コンテスト

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

配点 : 233

問題文

高橋君と青木君は、料理コンテストの審査員を務めています。このコンテストでは、 N 人の参加者がそれぞれ 1 つずつ料理を提出し、 2 人の審査員がすべての料理に対してそれぞれ点数をつけます。

参加者には 1 から N までの番号が振られています。高橋君が参加者 i の料理につけた点数を A_i、青木君が参加者 i の料理につけた点数を B_i とします。各点数は 1 以上 100 以下の整数です。

各参加者の最終スコアは、高橋君の点数と青木君の点数の合計 A_i + B_i で決まります。最終スコアが最も高い参加者が優勝です。最終スコアが最も高い参加者はちょうど 1 人であることが保証されます。

優勝者の参加者番号を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 100 (1 \leq i \leq N)
  • 1 \leq B_i \leq 100 (1 \leq i \leq N)
  • 入力はすべて整数である。
  • 最終スコアが最も高い参加者はちょうど 1 人である。

入力

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

1 行目には、参加者の人数を表す整数 N が与えられる。続く N 行のうち i 行目には、参加者 i に対する高橋君の点数 A_i と青木君の点数 B_i が、スペース区切りで与えられる。

出力

最終スコアが最も高い参加者の番号を 1 行で出力せよ。


入力例 1

3
50 60
80 70
40 90

出力例 1

2

入力例 2

5
10 20
30 40
50 50
60 30
20 70

出力例 2

3

入力例 3

10
45 55
60 40
30 70
80 15
25 75
50 50
90 10
35 65
70 30
55 46

出力例 3

10

入力例 4

20
12 34
56 78
90 11
23 45
67 89
1 99
100 50
48 52
73 27
36 64
85 14
29 71
58 42
17 83
94 5
41 59
66 33
8 91
75 25
53 47

出力例 4

5

入力例 5

1
1 1

出力例 5

1

Score : 233 pts

Problem Statement

Takahashi and Aoki are serving as judges for a cooking contest. In this contest, N participants each submit one dish, and the two judges assign a score to every dish.

The participants are numbered from 1 to N. Let A_i be the score that Takahashi gives to participant i's dish, and B_i be the score that Aoki gives to participant i's dish. Each score is an integer between 1 and 100, inclusive.

Each participant's final score is determined by the sum of Takahashi's score and Aoki's score, A_i + B_i. The participant with the highest final score wins. It is guaranteed that there is exactly one participant with the highest final score.

Find the participant number of the winner.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 100 (1 \leq i \leq N)
  • 1 \leq B_i \leq 100 (1 \leq i \leq N)
  • All inputs are integers.
  • There is exactly one participant with the highest final score.

Input

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

The first line contains an integer N, representing the number of participants. Of the following N lines, the i-th line contains Takahashi's score A_i and Aoki's score B_i for participant i, separated by a space.

Output

Print the participant number with the highest final score on a single line.


Sample Input 1

3
50 60
80 70
40 90

Sample Output 1

2

Sample Input 2

5
10 20
30 40
50 50
60 30
20 70

Sample Output 2

3

Sample Input 3

10
45 55
60 40
30 70
80 15
25 75
50 50
90 10
35 65
70 30
55 46

Sample Output 3

10

Sample Input 4

20
12 34
56 78
90 11
23 45
67 89
1 99
100 50
48 52
73 27
36 64
85 14
29 71
58 42
17 83
94 5
41 59
66 33
8 91
75 25
53 47

Sample Output 4

5

Sample Input 5

1
1 1

Sample Output 5

1
C - 権限管理システム

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

配点 : 266

問題文

高橋君はある会社のシステム管理者です。この会社には N 種類のリソースがあり、それぞれのリソースにアクセスするために必要な権限が設定されています。権限は L 個の権限項目からなり、i 番目のリソースに必要な権限は、長さ L のビット列 S_i で表されます。ビット列は 01 からなる文字列であり、左端のビットを最上位ビット(MSB)とします。ビット列の第 p ビット(1 \leq p \leq L、左端を第 1 ビットとする)が 1 であるとき、そのリソースへのアクセスには第 p 権限項目が必要であることを意味します。

社員に付与するアクセス権限もまた長さ L のビット列 K で表されます。ある社員がリソース i にアクセスできる条件は、そのリソースが要求するすべての権限項目を社員が持っていること、すなわち各ビット位置について S_i のビットが 1 ならば K の同じ位置のビットも 1 であること(ビットごとの AND を取ったとき K \mathbin{\&} S_i = S_i が成り立つこと)です。

青木君は会社のセキュリティ担当で、不必要に大きな権限を付与しない運用を推進しています。青木君は高橋君に Q 個の依頼を出しました。j 番目の依頼(1 \leq j \leq Q)では、ある社員がアクセスする必要のある M_j 個のリソースの番号 c_{j,1}, c_{j,2}, \ldots, c_{j,M_j} が指定されます(同一の依頼内でリソース番号が重複することもあります)。指定されたすべてのリソースにアクセスできる権限ビット列 K のうち、ビット列を二進数として解釈したときの値が最小となるものを求めてください。

なお、指定されたすべてのリソースにアクセスできる権限ビット列は必ず存在します(例えば、すべてのビットが 1 であるビット列は条件を満たします)。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq L \leq 64
  • 1 \leq Q \leq 10^5
  • S_i01 からなる長さ L の文字列である(1 \leq i \leq N
  • M_jj 番目の依頼で指定されるリソースの個数であり、1 \leq M_j \leq N1 \leq j \leq Q
  • c_{j,k}j 番目の依頼で指定されるリソースの番号であり、1 \leq c_{j,k} \leq N1 \leq j \leq Q, 1 \leq k \leq M_j
  • 同一の依頼内でリソース番号が重複することもある
  • \sum_{j=1}^{Q} M_j \leq 10^5
  • S_i は文字列として与えられ、それ以外の入力はすべて整数である

入力

N L Q
S_1
S_2
\vdots
S_N
M_1 c_{1,1} c_{1,2} \ldots c_{1,M_1}
M_2 c_{2,1} c_{2,2} \ldots c_{2,M_2}
\vdots
M_Q c_{Q,1} c_{Q,2} \ldots c_{Q,M_Q}
  • 1 行目には、リソースの種類数を表す整数 N、ビット列の長さを表す整数 L、依頼の個数を表す整数 Q が、スペース区切りで与えられる。
  • 2 行目から N+1 行目では、各リソースの必要権限が与えられる。
  • 1 + i 行目(1 \leq i \leq N)では、i 番目のリソースの必要権限 S_i が、01 からなる長さ L の文字列として与えられる。文字列の左端が最上位ビットである。
  • 続く Q 行では、各依頼が与えられる。
  • N + 1 + j 行目(1 \leq j \leq Q)では、j 番目の依頼として、まずアクセスしたいリソースの個数を表す整数 M_j が与えられ、続けてアクセスしたいリソースの番号を表す整数 c_{j,1}, c_{j,2}, \ldots, c_{j,M_j} がスペース区切りで与えられる。リソースの番号は 1 以上 N 以下の整数である。同一の依頼内でリソース番号が重複することもある。

出力

Q 行出力せよ。j 行目(1 \leq j \leq Q)には、j 番目の依頼に対して、指定されたすべてのリソースにアクセスできる権限ビット列のうち、二進数として解釈した値が最小となるものを、左端を最上位ビットとする長さ L01 からなる文字列として出力せよ(長さが L に満たない場合は先頭を 0 で埋め、必ず長さ L の文字列とすること)。


入力例 1

3 4 3
1010
0110
1001
1 1
2 1 2
3 1 2 3

出力例 1

1010
1110
1111

入力例 2

5 8 4
10100000
01010000
00001010
00000101
11000011
1 3
2 1 3
3 2 4 5
4 1 2 3 4

出力例 2

00001010
10101010
11010111
11111111

入力例 3

6 16 5
1000000000000001
0100000000000010
0010000000000100
0001000000001000
0000100000010000
1111100000000000
2 1 2
3 1 3 5
1 6
4 2 3 4 5
6 1 2 3 4 5 6

出力例 3

1100000000000011
1010100000010101
1111100000000000
0111100000011110
1111100000011111

Score : 266 pts

Problem Statement

Takahashi is a system administrator at a company. The company has N types of resources, and each resource has a set of required permissions to access it. Permissions consist of L permission items, and the permissions required for the i-th resource are represented by a bit string S_i of length L. A bit string is a string consisting of 0 and 1, where the leftmost bit is the most significant bit (MSB). When the p-th bit (1 \leq p \leq L, counting the leftmost bit as the 1st bit) of the bit string is 1, it means that the p-th permission item is required to access that resource.

The access permissions granted to an employee are also represented by a bit string K of length L. The condition for an employee to access resource i is that the employee possesses all permission items required by that resource. In other words, for each bit position, if the bit of S_i is 1, then the bit at the same position in K must also be 1 (i.e., the bitwise AND satisfies K \mathbin{\&} S_i = S_i).

Aoki is the company's security officer and promotes a policy of not granting unnecessarily broad permissions. Aoki has given Takahashi Q requests. In the j-th request (1 \leq j \leq Q), the numbers of M_j resources that a certain employee needs to access are specified: c_{j,1}, c_{j,2}, \ldots, c_{j,M_j} (resource numbers may be duplicated within the same request). Among all permission bit strings K that allow access to all specified resources, find the one whose value is minimized when the bit string is interpreted as a binary number.

Note that a permission bit string that allows access to all specified resources always exists (for example, a bit string with all bits set to 1 satisfies the condition).

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq L \leq 64
  • 1 \leq Q \leq 10^5
  • S_i is a string of length L consisting of 0 and 1 (1 \leq i \leq N)
  • M_j is the number of resources specified in the j-th request, and 1 \leq M_j \leq N (1 \leq j \leq Q)
  • c_{j,k} is a resource number specified in the j-th request, and 1 \leq c_{j,k} \leq N (1 \leq j \leq Q, 1 \leq k \leq M_j)
  • Resource numbers may be duplicated within the same request
  • \sum_{j=1}^{Q} M_j \leq 10^5
  • S_i is given as a string; all other inputs are integers

Input

N L Q
S_1
S_2
\vdots
S_N
M_1 c_{1,1} c_{1,2} \ldots c_{1,M_1}
M_2 c_{2,1} c_{2,2} \ldots c_{2,M_2}
\vdots
M_Q c_{Q,1} c_{Q,2} \ldots c_{Q,M_Q}
  • The first line contains three space-separated integers: N representing the number of resource types, L representing the length of the bit strings, and Q representing the number of requests.
  • Lines 2 through N+1 give the required permissions for each resource.
  • Line 1 + i (1 \leq i \leq N) gives the required permissions S_i for the i-th resource as a string of length L consisting of 0 and 1. The leftmost character is the most significant bit.
  • The following Q lines give each request.
  • Line N + 1 + j (1 \leq j \leq Q) gives the j-th request: first an integer M_j representing the number of resources to access, followed by space-separated integers c_{j,1}, c_{j,2}, \ldots, c_{j,M_j} representing the resource numbers to access. Resource numbers are integers between 1 and N inclusive. Resource numbers may be duplicated within the same request.

Output

Output Q lines. On the j-th line (1 \leq j \leq Q), output the permission bit string that allows access to all resources specified in the j-th request and has the minimum value when interpreted as a binary number. Output it as a string of length L consisting of 0 and 1 with the leftmost bit being the most significant bit (if the length is less than L, pad with leading 0s to ensure the string is exactly of length L).


Sample Input 1

3 4 3
1010
0110
1001
1 1
2 1 2
3 1 2 3

Sample Output 1

1010
1110
1111

Sample Input 2

5 8 4
10100000
01010000
00001010
00000101
11000011
1 3
2 1 3
3 2 4 5
4 1 2 3 4

Sample Output 2

00001010
10101010
11010111
11111111

Sample Input 3

6 16 5
1000000000000001
0100000000000010
0010000000000100
0001000000001000
0000100000010000
1111100000000000
2 1 2
3 1 3 5
1 6
4 2 3 4 5
6 1 2 3 4 5 6

Sample Output 3

1100000000000011
1010100000010101
1111100000000000
0111100000011110
1111100000011111
D - チームの分割

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

配点 : 300

問題文

高橋君は N 人のメンバーからなるグループのリーダーです。メンバーには 1 から N までの番号が付けられており、メンバー i (1 \le i \le N) の実力値は A_i です。

高橋君は、このグループを連続する番号の境界で二つのチームに分けようとしています。具体的には、ある整数 k (1 \le k < N) を選び、メンバー 1 からメンバー k までを「チーム A」、メンバー k+1 からメンバー N までを「チーム B」とします。

このとき、チーム A の実力値の総和を S_1 = A_1 + A_2 + \cdots + A_k 、チーム B の実力値の総和を S_2 = A_{k+1} + A_{k+2} + \cdots + A_N とします。

k を最適に選んだときの |S_1 - S_2| の最小値を求めてください。

制約

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

入力

N
A_1 A_2 \cdots A_N
  • 1 行目には、メンバーの人数を表す整数 N が与えられる。
  • 2 行目には、メンバー 1 からメンバー N までの実力値を表す N 個の整数 A_1, A_2, \ldots, A_N が空白区切りで与えられる。

出力

|S_1 - S_2| の最小値を 1 行に出力してください。


入力例 1

4
1 2 3 4

出力例 1

2

入力例 2

5
1 3 2 2 4

出力例 2

0

入力例 3

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

出力例 3

10

入力例 4

40
17 23 5 11 29 31 7 13 19 2 37 41 3 43 47 53 59 61 67 71 73 79 83 89 97 101 107 109 113 127 131 137 139 149 151 157 163 167 173 179

出力例 4

71

入力例 5

2
1000000000 1000000000

出力例 5

0

Score : 300 pts

Problem Statement

Takahashi is the leader of a group consisting of N members. The members are numbered from 1 to N, and the skill value of member i (1 \le i \le N) is A_i.

Takahashi wants to divide this group into two teams at a boundary of consecutive numbers. Specifically, he chooses an integer k (1 \le k < N), and assigns members 1 through k to "Team A" and members k+1 through N to "Team B".

Let the sum of skill values of Team A be S_1 = A_1 + A_2 + \cdots + A_k, and the sum of skill values of Team B be S_2 = A_{k+1} + A_{k+2} + \cdots + A_N.

Find the minimum value of |S_1 - S_2| when k is chosen optimally.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • All inputs are integers

Input

N
A_1 A_2 \cdots A_N
  • The first line contains an integer N representing the number of members.
  • The second line contains N integers A_1, A_2, \ldots, A_N separated by spaces, representing the skill values of members 1 through N.

Output

Print the minimum value of |S_1 - S_2| on a single line.


Sample Input 1

4
1 2 3 4

Sample Output 1

2

Sample Input 2

5
1 3 2 2 4

Sample Output 2

0

Sample Input 3

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

Sample Output 3

10

Sample Input 4

40
17 23 5 11 29 31 7 13 19 2 37 41 3 43 47 53 59 61 67 71 73 79 83 89 97 101 107 109 113 127 131 137 139 149 151 157 163 167 173 179

Sample Output 4

71

Sample Input 5

2
1000000000 1000000000

Sample Output 5

0
E - 山の見晴らし

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

配点 : 333

問題文

高橋君は N 個の山が一列に並んでいる風景を眺めています。それぞれの山には 1 から N までの番号が付けられており、山 i の標高は A_i メートルです。

高橋君は、各山について「その山以外に、標高が厳密に高い山がいくつあるか」を調べたいと思っています。これは、その山の頂上に立ったとき、自分より高い山が全部でいくつ存在するかを把握するための参考になるからです。

N 個の山それぞれについて、その山以外の山のうち標高が厳密に高い山の数を求めてください。

制約

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

入力

N
A_1 A_2 \ldots A_N
  • 1 行目には、山の数を表す整数 N が与えられる。
  • 2 行目には、各山の標高を表す N 個の整数 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。
  • A_i は山 i の標高(メートル)を表す。

出力

B_1 B_2 \ldots B_N
  • N 個の整数をスペース区切りで 1 行に出力せよ。
  • B_i は、山 i 以外の山のうち標高が A_i より厳密に高い山の数を表す。

入力例 1

5
3 1 4 1 5

出力例 1

2 3 1 3 0

入力例 2

8
100 200 150 200 300 100 250 300

出力例 2

6 3 5 3 0 6 2 0

入力例 3

12
1000000000 1 500000000 999999999 1000000000 250000000 750000000 1 999999998 500000000 250000000 750000001

出力例 3

0 10 6 2 0 8 5 10 3 6 8 4

Score : 333 pts

Problem Statement

Takahashi is looking at a landscape where N mountains are lined up in a row. Each mountain is numbered from 1 to N, and mountain i has an elevation of A_i meters.

For each mountain, Takahashi wants to find out "how many other mountains have a strictly higher elevation." This serves as a reference for understanding how many mountains taller than himself exist when standing at the summit of that mountain.

For each of the N mountains, determine the number of other mountains that have a strictly higher elevation.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • All inputs are integers

Input

N
A_1 A_2 \ldots A_N
  • The first line contains an integer N representing the number of mountains.
  • The second line contains N integers A_1, A_2, \ldots, A_N separated by spaces, representing the elevation of each mountain.
  • A_i represents the elevation (in meters) of mountain i.

Output

B_1 B_2 \ldots B_N
  • Output N integers separated by spaces on a single line.
  • B_i represents the number of mountains other than mountain i that have a strictly higher elevation than A_i.

Sample Input 1

5
3 1 4 1 5

Sample Output 1

2 3 1 3 0

Sample Input 2

8
100 200 150 200 300 100 250 300

Sample Output 2

6 3 5 3 0 6 2 0

Sample Input 3

12
1000000000 1 500000000 999999999 1000000000 250000000 750000000 1 999999998 500000000 250000000 750000001

Sample Output 3

0 10 6 2 0 8 5 10 3 6 8 4
F - 連続区間の売上目標

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

配点 : 366

問題文

高橋君は、ある商店街の N 店舗の売上データを分析しています。商店街の店舗には 1 から N までの番号が付けられており、各店舗 i の1日あたりの売上は V_i 円です。

商店街では、連続する店舗をまとめて「販売促進エリア」として指定するキャンペーンを企画しています。販売促進エリアは、整数の組 (l, r)1 \leq l \leq r \leq N )を選び、番号 l から番号 r までの連続する店舗をすべて含む形で設定します。(l, r) が異なれば、異なるエリアとして区別します。

ただし、キャンペーンの効果を十分に得るため、選んだ店舗の売上の合計が K 円以上になるエリアのみが採用されます。すなわち、V_l + V_{l+1} + \cdots + V_r \geq K を満たす必要があります。

高橋君は、採用可能な販売促進エリアが何通りあるかを知りたいと思っています。条件を満たす (l, r) の組の個数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^{14}
  • 1 \leq V_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数である

入力

N K
V_1 V_2 \ldots V_N
  • 1 行目には、店舗の個数を表す整数 N と、必要な売上の合計の下限を表す整数 K が、スペース区切りで与えられる。
  • 2 行目には、各店舗の売上を表す整数 V_1, V_2, \ldots, V_N が、スペース区切りで与えられる。

出力

条件を満たす販売促進エリアの個数、すなわち V_l + V_{l+1} + \cdots + V_r \geq K となる組 (l, r)1 \leq l \leq r \leq N )の個数を 1 行で出力してください。なお、答えは 64 ビット整数の範囲に収まることが保証されます。


入力例 1

5 10
3 1 4 1 5

出力例 1

3

入力例 2

8 15
5 3 8 2 7 4 6 1

出力例 2

18

入力例 3

10 1000000000000
100000000 200000000 300000000 400000000 500000000 600000000 700000000 800000000 900000000 1000000000

出力例 3

0

Score : 366 pts

Problem Statement

Takahashi is analyzing the sales data of N stores in a shopping district. The stores in the shopping district are numbered from 1 to N, and the daily sales of each store i is V_i yen.

The shopping district is planning a campaign to designate contiguous stores as a "sales promotion area." A sales promotion area is defined by choosing a pair of integers (l, r) (1 \leq l \leq r \leq N) and including all consecutive stores from number l to number r. Different pairs (l, r) are considered distinct areas.

However, to ensure sufficient campaign effectiveness, only areas where the total sales of the selected stores is at least K yen will be adopted. That is, the condition V_l + V_{l+1} + \cdots + V_r \geq K must be satisfied.

Takahashi wants to know how many possible sales promotion areas can be adopted. Find the number of pairs (l, r) that satisfy the condition.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^{14}
  • 1 \leq V_i \leq 10^9 (1 \leq i \leq N)
  • All input values are integers

Input

N K
V_1 V_2 \ldots V_N
  • The first line contains an integer N representing the number of stores and an integer K representing the minimum required total sales, separated by a space.
  • The second line contains integers V_1, V_2, \ldots, V_N representing the sales of each store, separated by spaces.

Output

Print in one line the number of sales promotion areas that satisfy the condition, that is, the number of pairs (l, r) (1 \leq l \leq r \leq N) such that V_l + V_{l+1} + \cdots + V_r \geq K. It is guaranteed that the answer fits within a 64-bit integer.


Sample Input 1

5 10
3 1 4 1 5

Sample Output 1

3

Sample Input 2

8 15
5 3 8 2 7 4 6 1

Sample Output 2

18

Sample Input 3

10 1000000000000
100000000 200000000 300000000 400000000 500000000 600000000 700000000 800000000 900000000 1000000000

Sample Output 3

0
G - 友達の輪

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

配点 : 366

問題文

高橋君のクラスには N 人の生徒がいます。生徒には 1 から N までの出席番号が付けられています。

このクラスでは、何人かの生徒同士が友達関係にあります。友達関係は M 組存在し、 j 番目の友達関係は生徒 U_j と生徒 V_j の間にあります。友達関係は双方向であり、また友達関係で直接または間接的に繋がっている生徒同士は同じグループに属しているとみなされます。

ある日、先生はクラスで連絡事項を伝えるために、特定の生徒に連絡をしました。連絡を受けた生徒は、同じグループに属する全員にその連絡を共有します。

先生は Q 回の連絡を行い、 k 回目の連絡では生徒 S_k に連絡をしました。

高橋君は、各連絡において実際に何人の生徒が連絡を受け取ったかを知りたいと思っています。各連絡について、連絡を受け取った生徒の総数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq U_j < V_j \leq N (1 \leq j \leq M)
  • i \neq j ならば (U_i, V_i) \neq (U_j, V_j)
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq S_k \leq N (1 \leq k \leq Q)
  • 入力はすべて整数

入力

N M
U_1 V_1
U_2 V_2
:
U_M V_M
Q
S_1
S_2
:
S_Q
  • 1 行目には、生徒の数を表す N 、友達関係の数を表す M が、スペース区切りで与えられる。
  • 2 行目から M + 1 行目には、友達関係の情報が与えられる。
  • 1 + j 行目では、 j 番目の友達関係で結ばれている生徒の出席番号 U_jV_j がスペース区切りで与えられる。
  • M + 2 行目には、連絡の回数を表す Q が与えられる。
  • M + 3 行目から M + 2 + Q 行目には、各連絡で先生が連絡した生徒の出席番号が与えられる。
  • M + 2 + k 行目では、 k 回目の連絡で先生が連絡した生徒の出席番号 S_k が与えられる。

出力

Q 行出力せよ。 k 行目には、 k 回目の連絡において連絡を受け取った生徒の総数を出力せよ。


入力例 1

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

出力例 1

3
2
3

入力例 2

6 2
1 3
2 4
4
1
5
2
6

出力例 2

2
1
2
1

入力例 3

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

出力例 3

4
3
2
1
3

入力例 4

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

出力例 4

5
3
4
2
1
5
3
4

入力例 5

1 0
1
1

出力例 5

1

Score : 366 pts

Problem Statement

There are N students in Takahashi's class. The students are assigned attendance numbers from 1 to N.

In this class, some pairs of students are friends with each other. There are M friendship relations, and the j-th friendship is between student U_j and student V_j. Friendships are bidirectional, and students who are connected directly or indirectly through friendships are considered to belong to the same group.

One day, the teacher contacted specific students to convey announcements to the class. A student who receives the contact shares the announcement with everyone in the same group.

The teacher made Q contacts, and in the k-th contact, the teacher contacted student S_k.

Takahashi wants to know how many students actually received the announcement for each contact. For each contact, find the total number of students who received the announcement.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq U_j < V_j \leq N (1 \leq j \leq M)
  • If i \neq j, then (U_i, V_i) \neq (U_j, V_j)
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq S_k \leq N (1 \leq k \leq Q)
  • All inputs are integers

Input

N M
U_1 V_1
U_2 V_2
:
U_M V_M
Q
S_1
S_2
:
S_Q
  • The first line contains N, the number of students, and M, the number of friendships, separated by a space.
  • From the 2nd line to the (M + 1)-th line, friendship information is given.
  • The (1 + j)-th line contains the attendance numbers U_j and V_j of the students connected by the j-th friendship, separated by a space.
  • The (M + 2)-th line contains Q, the number of contacts.
  • From the (M + 3)-th line to the (M + 2 + Q)-th line, the attendance number of the student the teacher contacted is given.
  • The (M + 2 + k)-th line contains the attendance number S_k of the student the teacher contacted in the k-th contact.

Output

Output Q lines. On the k-th line, output the total number of students who received the announcement in the k-th contact.


Sample Input 1

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

Sample Output 1

3
2
3

Sample Input 2

6 2
1 3
2 4
4
1
5
2
6

Sample Output 2

2
1
2
1

Sample Input 3

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

Sample Output 3

4
3
2
1
3

Sample Input 4

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

Sample Output 4

5
3
4
2
1
5
3
4

Sample Input 5

1 0
1
1

Sample Output 5

1
H - 都市の巡回調査

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

配点 : 400

問題文

高橋君はある地域の調査員です。この地域には N 個の都市があり、都市には 1 から N までの番号が付けられています。都市同士は一方通行の道路で結ばれており、道路は全部で M 本あります。道路 j は都市 U_j から都市 V_j への一方通行で、この道路を使うと都市 U_j から都市 V_j へ直接移動できます(逆方向には移動できません)。

各都市にはそれぞれ異なる重要度が設定されています。都市 i の重要度は B_i です。B_1, B_2, \ldots, B_N1 から N までの整数を並べ替えたもの(順列)です。

高橋君は以下の手順を繰り返し、すべての都市を調査します。はじめ、すべての都市は未調査です。

  1. 出発都市を決める。 1回目の巡回では都市 1 を出発都市とする。2回目以降の巡回では、未調査の都市のうち重要度が最も大きい都市を出発都市とする。
  2. 出発都市から巡回を行う。 まず出発都市を調査済みにする。その後、以下の操作を繰り返す:
  • 現在いる都市から1本の道路で直接移動できる都市の中に、未調査の都市が1つ以上存在するならば、その中で重要度が最も大きい都市へ移動し、その都市を調査済みにする。
  • 現在いる都市から1本の道路で直接移動できる都市の中に未調査の都市が存在しない場合(移動できる都市自体が存在しない場合を含む)、この巡回を終了する
  1. すべての都市が調査済みになっていれば終了する。そうでなければ手順 1 に戻る。

高橋君が都市を調査済みにした順序を、先頭から順にすべて出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq B_i \leq N
  • B_1, B_2, \ldots, B_N はすべて異なる
  • 1 \leq U_j \leq N
  • 1 \leq V_j \leq N
  • U_j \neq V_j
  • 同じ (U_j, V_j) の組は複数回与えられない
  • 入力はすべて整数である

入力

N M
B_1 B_2 \ldots B_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M
  • 1 行目には、都市の数を表す N と道路の本数を表す M が、スペース区切りで与えられる。
  • 2 行目には、都市 i の重要度を表す B_iN 個、スペース区切りで与えられる。B_1, B_2, \ldots, B_N はすべて異なる。
  • 3 行目から M 行にわたり、道路の情報が与えられる。2 + j 行目では、都市 U_j から都市 V_j への一方通行の道路があることを表す。

出力

高橋君が都市を調査済みにした順序を、スペース区切りで 1 行に出力せよ。出力される都市番号はちょうど N 個であり、1 から N の各番号がちょうど 1 回ずつ現れる。


入力例 1

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

出力例 1

1 2 4 3 5

入力例 2

4 0
2 4 3 1

出力例 2

1 2 3 4

入力例 3

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

出力例 3

1 4 6 7 8 2 3 5

入力例 4

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

出力例 4

1 2 3 4 5 6 7 8 9 10

入力例 5

1 0
1

出力例 5

1

Score : 400 pts

Problem Statement

Takahashi is a surveyor for a certain region. This region contains N cities, numbered from 1 to N. The cities are connected by one-way roads, and there are M roads in total. Road j is a one-way road from city U_j to city V_j, allowing direct travel from city U_j to city V_j (travel in the reverse direction is not possible).

Each city has a distinct importance value assigned to it. The importance of city i is B_i. B_1, B_2, \ldots, B_N is a permutation of the integers from 1 to N.

Takahashi surveys all cities by repeating the following procedure. Initially, all cities are unsurveyed.

  1. Determine the starting city. For the first tour, the starting city is city 1. For each subsequent tour, the starting city is the unsurveyed city with the highest importance.
  2. Conduct a tour from the starting city. First, mark the starting city as surveyed. Then, repeat the following operation:
  • If there exists at least one unsurveyed city among the cities directly reachable by a single road from the current city, move to the one with the highest importance among them, and mark that city as surveyed.
  • If there are no unsurveyed cities among the cities directly reachable by a single road from the current city (including the case where no reachable cities exist at all), end this tour.
  1. If all cities have been surveyed, stop. Otherwise, return to step 1.

Output, in order from first to last, the sequence in which Takahashi marks cities as surveyed.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq B_i \leq N
  • B_1, B_2, \ldots, B_N are all distinct
  • 1 \leq U_j \leq N
  • 1 \leq V_j \leq N
  • U_j \neq V_j
  • The same pair (U_j, V_j) is not given more than once
  • All input values are integers

Input

N M
B_1 B_2 \ldots B_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M
  • The first line contains N, the number of cities, and M, the number of roads, separated by a space.
  • The second line contains N space-separated values B_i, representing the importance of city i. B_1, B_2, \ldots, B_N are all distinct.
  • The following M lines provide road information. The (2 + j)-th line indicates that there is a one-way road from city U_j to city V_j.

Output

Output the order in which Takahashi marks cities as surveyed, on a single line separated by spaces. The output should contain exactly N city numbers, with each number from 1 to N appearing exactly once.


Sample Input 1

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

Sample Output 1

1 2 4 3 5

Sample Input 2

4 0
2 4 3 1

Sample Output 2

1 2 3 4

Sample Input 3

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

Sample Output 3

1 4 6 7 8 2 3 5

Sample Input 4

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

Sample Output 4

1 2 3 4 5 6 7 8 9 10

Sample Input 5

1 0
1

Sample Output 5

1
I - 円陣パスゲーム

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

配点 : 400

問題文

N 人の子供たちが円形に並んでゲームをしています。子供たちには時計回りに 1 から N までの番号が付いており、番号 N の時計回りの隣は番号 1 です。

最初、すべての子供が円陣に参加しており、番号 S の子供がボールを持っています。

これから M 回のパスが行われます。i 回目のパスでは、現在ボールを持っている子供を x として、次の操作を行います。

  1. x の時計回りの隣の位置から時計回りに、円陣に残っている子供(x 自身を除く)だけを順に 1, 2, 3, \ldots と数えていきます。すでに円陣を抜けた子供は飛ばします。残っている子供の人数を超える場合は、円を何周でもして数え続けます。
  2. D_i 番目に数えられた子供にボールを渡します。
  3. その直後、x は円陣から抜けます。以降のパスにおいて x は数えられることも、ボールを受け取ることもありません。

M 回すべてのパスが終わった後、ボールを持っている子供の番号を出力してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq N - 1
  • 1 \leq S \leq N
  • 1 \leq D_i \leq 10^9
  • 入力はすべて整数である

入力

N M S
D_1
D_2
\vdots
D_M
  • 1 行目には、子供の人数 N、パスの回数 M、最初にボールを持っている子供の番号 S がスペース区切りで与えられる。
  • 続く M 行のうち i 行目には、i 回目のパスで数える人数 D_i が与えられる。

出力

M 回すべてのパスが終わった後、ボールを持っている子供の番号を 1 行で出力してください。


入力例 1

5 3 1
1
2
1

出力例 1

5

入力例 2

6 4 3
5
1
7
2

出力例 2

1

入力例 3

20 10 8
3
15
1
22
7
100
4
9
18
2

出力例 3

19

入力例 4

60 30 60
1
59
123456789
17
42
1000000000
8
31
73
2
500
19
64
27
91
6
38
111
25
3
999999937
14
52
7
86
41
12
68
29
5

出力例 4

17

入力例 5

2 1 2
1000000000

出力例 5

1

Score : 400 pts

Problem Statement

N children are standing in a circle playing a game. The children are numbered 1 to N in clockwise order, and the clockwise neighbor of child N is child 1.

Initially, all children are participating in the circle, and child number S is holding the ball.

M passes will be performed. For the i-th pass, let x be the child currently holding the ball, and the following operation is performed:

  1. Starting from the position clockwise next to x, count only the children still remaining in the circle (excluding x itself) in clockwise order as 1, 2, 3, \ldots. Children who have already left the circle are skipped. If the count exceeds the number of remaining children, continue counting by going around the circle as many times as needed.
  2. Pass the ball to the child counted as the D_i-th.
  3. Immediately after, x leaves the circle. In subsequent passes, x will neither be counted nor receive the ball.

After all M passes have been completed, output the number of the child holding the ball.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq N - 1
  • 1 \leq S \leq N
  • 1 \leq D_i \leq 10^9
  • All inputs are integers

Input

N M S
D_1
D_2
\vdots
D_M
  • The first line contains the number of children N, the number of passes M, and the number of the child initially holding the ball S, separated by spaces.
  • The i-th of the following M lines contains D_i, the count number for the i-th pass.

Output

Output in one line the number of the child holding the ball after all M passes have been completed.


Sample Input 1

5 3 1
1
2
1

Sample Output 1

5

Sample Input 2

6 4 3
5
1
7
2

Sample Output 2

1

Sample Input 3

20 10 8
3
15
1
22
7
100
4
9
18
2

Sample Output 3

19

Sample Input 4

60 30 60
1
59
123456789
17
42
1000000000
8
31
73
2
500
19
64
27
91
6
38
111
25
3
999999937
14
52
7
86
41
12
68
29
5

Sample Output 4

17

Sample Input 5

2 1 2
1000000000

Sample Output 5

1
J - 道路ネットワークの整備

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

配点 : 433

問題文

高橋君は、N 個の都市からなる道路ネットワークの管理を任されています。

このネットワークは根付き木の構造をしており、都市 1 が首都(根)です。都市 i2 \leq i \leq N)にはそれぞれ親都市 P_iP_i < i)が定まっており、都市 i と親都市 P_i を直接結ぶ道路がちょうど 1 本存在します。この道路を道路 i と呼ぶことにします。道路 i には、交通負荷への耐性を表す耐久値が設定されており、初期耐久値は W_i です。道路は全部で N - 1 本あり、木構造であるため、任意の 2 都市間を結ぶ経路(同じ都市を 2 度通らない道路の列)はちょうど 1 通りに定まります。

高橋君は、道路の補強工事を Q 回行います。j 回目(1 \leq j \leq Q)の補強工事では、2 つの都市 u_j, v_j を指定し、都市 u_j と都市 v_j を結ぶ経路上にあるすべての道路の耐久値をそれぞれ 1 増加させます。u_j = v_j の場合は経路上に道路が存在しないため、何も変化しません。同じ道路に対して複数回の補強工事が適用されることもあり、そのたびに耐久値が 1 ずつ加算されます。

Q 回の補強工事をすべて行った後、青木君が道路ネットワークの状態について R 個の質問をします。k 番目(1 \leq k \leq R)の質問では、2 つの都市 a_k, b_k を指定し、都市 a_k と都市 b_k を結ぶ経路上にあるすべての道路の耐久値のうち、最小値を求めます。ただし、a_k = b_k の場合は経路上に道路が存在しないため、答えは 0 とします。

すべての補強工事を行った後の道路ネットワークに対して、各質問に答えてください。

制約

  • 1 \leq N \leq 5 \times 10^4
  • 1 \leq P_i \leq i - 12 \leq i \leq N
  • 0 \leq W_i \leq 10^92 \leq i \leq N
  • 1 \leq Q \leq 5 \times 10^4
  • 1 \leq u_j, v_j \leq N1 \leq j \leq Q
  • 1 \leq R \leq 5 \times 10^4
  • 1 \leq a_k, b_k \leq N1 \leq k \leq R
  • 入力はすべて整数である。
  • 各質問の答えは 2^{63} - 1 以下であることが保証される。

入力

N
P_2 W_2
P_3 W_3
\vdots
P_N W_N
Q
u_1 v_1
u_2 v_2
\vdots
u_Q v_Q
R
a_1 b_1
a_2 b_2
\vdots
a_R b_R
  • 1 行目には、都市の数 N が与えられる。
  • 続く N - 1 行には、都市 2, 3, \ldots, N に対応する情報が順に与えられる。このうち都市 i2 \leq i \leq N)に対応する行には、親都市 P_i と、道路 i(都市 i とその親都市 P_i を結ぶ道路)の初期耐久値 W_i が、スペース区切りで与えられる。
  • 次の行には、補強工事の回数 Q が与えられる。
  • 続く Q 行の j 行目(1 \leq j \leq Q)には、j 回目の補強工事で指定される都市の組 u_j, v_j が、スペース区切りで与えられる。
  • 次の行には、質問の数 R が与えられる。
  • 続く R 行の k 行目(1 \leq k \leq R)には、k 番目の質問で指定される都市の組 a_k, b_k が、スペース区切りで与えられる。

出力

R 行出力せよ。k 行目(1 \leq k \leq R)には、k 番目の質問に対する答えを出力せよ。すなわち、a_k \neq b_k の場合は都市 a_k と都市 b_k を結ぶ経路上にあるすべての道路の耐久値の最小値を、a_k = b_k の場合は 0 を出力せよ。


入力例 1

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

出力例 1

4
4
0
4

入力例 2

3
1 1000000000
1 0
2
2 3
1 1
3
2 3
1 2
3 3

出力例 2

1
1000000001
0

入力例 3

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

出力例 3

4
4
1
6
0
4
1

入力例 4

25
1 100
1 50
2 70
2 30
3 80
3 60
4 20
4 90
5 10
5 40
6 55
6 65
7 75
7 85
8 15
8 25
9 35
10 45
11 5
12 95
13 0
14 110
15 120
18 33
22
16 25
20 21
22 24
17 19
1 25
8 15
10 14
23 5
12 12
2 18
3 20
21 24
16 17
25 22
11 13
4 7
19 24
1 1
18 20
6 25
14 22
9 23
18
16 24
20 22
1 25
12 23
17 17
2 15
19 21
25 24
8 18
10 11
3 22
5 23
6 14
7 21
11 25
1 1
4 20
13 24

出力例 4

17
3
37
57
0
61
13
37
23
13
3
38
69
57
37
0
8
69

入力例 5

1
1
1 1
1
1 1

出力例 5

0

Score : 433 pts

Problem Statement

Takahashi is in charge of managing a road network consisting of N cities.

This network has the structure of a rooted tree, with city 1 being the capital (root). Each city i (2 \leq i \leq N) has a designated parent city P_i (P_i < i), and there exists exactly one road directly connecting city i and its parent city P_i. We will call this road road i. Road i has a durability value representing its resistance to traffic load, with an initial durability value of W_i. There are N - 1 roads in total, and since the structure is a tree, the path (a sequence of roads not passing through the same city twice) connecting any two cities is uniquely determined.

Takahashi performs Q reinforcement operations on the roads. In the j-th (1 \leq j \leq Q) reinforcement operation, he specifies two cities u_j, v_j and increases the durability value of every road on the path connecting city u_j and city v_j by 1. If u_j = v_j, there are no roads on the path, so nothing changes. The same road may have multiple reinforcement operations applied to it, and each time its durability value is incremented by 1.

After all Q reinforcement operations are completed, Aoki asks R questions about the state of the road network. In the k-th (1 \leq k \leq R) question, he specifies two cities a_k, b_k and asks for the minimum value among the durability values of all roads on the path connecting city a_k and city b_k. However, if a_k = b_k, there are no roads on the path, so the answer is 0.

Answer each question based on the road network after all reinforcement operations have been performed.

Constraints

  • 1 \leq N \leq 5 \times 10^4
  • 1 \leq P_i \leq i - 1 (2 \leq i \leq N)
  • 0 \leq W_i \leq 10^9 (2 \leq i \leq N)
  • 1 \leq Q \leq 5 \times 10^4
  • 1 \leq u_j, v_j \leq N (1 \leq j \leq Q)
  • 1 \leq R \leq 5 \times 10^4
  • 1 \leq a_k, b_k \leq N (1 \leq k \leq R)
  • All input values are integers.
  • It is guaranteed that the answer to each question is at most 2^{63} - 1.

Input

N
P_2 W_2
P_3 W_3
\vdots
P_N W_N
Q
u_1 v_1
u_2 v_2
\vdots
u_Q v_Q
R
a_1 b_1
a_2 b_2
\vdots
a_R b_R
  • The first line gives the number of cities N.
  • The following N - 1 lines give information corresponding to cities 2, 3, \ldots, N in order. The line corresponding to city i (2 \leq i \leq N) gives the parent city P_i and the initial durability value W_i of road i (the road connecting city i and its parent city P_i), separated by a space.
  • The next line gives the number of reinforcement operations Q.
  • The j-th (1 \leq j \leq Q) of the following Q lines gives the pair of cities u_j, v_j specified in the j-th reinforcement operation, separated by a space.
  • The next line gives the number of questions R.
  • The k-th (1 \leq k \leq R) of the following R lines gives the pair of cities a_k, b_k specified in the k-th question, separated by a space.

Output

Output R lines. The k-th line (1 \leq k \leq R) should contain the answer to the k-th question. That is, if a_k \neq b_k, output the minimum durability value among all roads on the path connecting city a_k and city b_k; if a_k = b_k, output 0.


Sample Input 1

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

Sample Output 1

4
4
0
4

Sample Input 2

3
1 1000000000
1 0
2
2 3
1 1
3
2 3
1 2
3 3

Sample Output 2

1
1000000001
0

Sample Input 3

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

Sample Output 3

4
4
1
6
0
4
1

Sample Input 4

25
1 100
1 50
2 70
2 30
3 80
3 60
4 20
4 90
5 10
5 40
6 55
6 65
7 75
7 85
8 15
8 25
9 35
10 45
11 5
12 95
13 0
14 110
15 120
18 33
22
16 25
20 21
22 24
17 19
1 25
8 15
10 14
23 5
12 12
2 18
3 20
21 24
16 17
25 22
11 13
4 7
19 24
1 1
18 20
6 25
14 22
9 23
18
16 24
20 22
1 25
12 23
17 17
2 15
19 21
25 24
8 18
10 11
3 22
5 23
6 14
7 21
11 25
1 1
4 20
13 24

Sample Output 4

17
3
37
57
0
61
13
37
23
13
3
38
69
57
37
0
8
69

Sample Input 5

1
1
1 1
1
1 1

Sample Output 5

0
K - 商店街の区画選び

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

配点 : 466

問題文

高橋君は、商店街の連続する区画を借りてイベント会場を設営する計画を立てています。

商店街には N 個の区画が左から右に一列に並んでいます。

左から i 番目 (1 \leq i \leq N) の区画の賃料は A_i 円です。

また、左から i 番目の区画について、駐車スペースがあれば C_i = 1、なければ C_i = 0 です。

高橋君は、連続するちょうど K 個の区画を選んで借りることを考えています。

すなわち、ある整数 l (1 \leq l \leq N-K+1) を選び、左から l 番目から l+K-1 番目までの K 個の区画を借ります。

これから Q 回の操作が順番に行われます。j 番目 (1 \leq j \leq Q) の操作は整数 T_j で種類が定まり、次のいずれかです。

  • T_j = 1(賃料の変更):整数 X_j, Y_j が与えられる。左から X_j 番目の区画の賃料を Y_j 円に変更する。この変更は以降の操作すべてに影響する。
  • T_j = 2(質問):整数 X_j が与えられる。連続するちょうど K 個の区画の選び方のうち、選んだ K 個の区画に駐車スペースのある区画(C_i = 1 である区画)が X_j 個以上含まれるものを考える。そのような選び方が存在する場合は、選んだ K 個の区画の賃料の合計としてありうる値の最小値を出力する。存在しない場合は IMPOSSIBLE と出力する。

なお、駐車スペースの有無を表す C_i は操作によって変化しません。

T_j = 2 の操作について、答えを出力してください。

制約

  • 1 \leq K \leq N \leq 10^5
  • 1 \leq Q \leq 5 \times 10^4
  • 1 \leq A_i \leq 10^9
  • C_i \in \{0, 1\}
  • T_j \in \{1, 2\}
  • T_j = 1 のとき:1 \leq X_j \leq N1 \leq Y_j \leq 10^9
  • T_j = 2 のとき:0 \leq X_j \leq KY_j = 0
  • T_j = 2 である操作が少なくとも 1 つ存在する
  • 入力はすべて整数である

入力

N K Q
A_1 A_2 \cdots A_N
C_1 C_2 \cdots C_N
T_1 X_1 Y_1
T_2 X_2 Y_2
\vdots
T_Q X_Q Y_Q

各操作は 3 つの整数の組 (T_j, X_j, Y_j) として与えられます。T_j = 2 のとき Y_j = 0 が与えられますが、これは入力形式を統一するためのもので、解答には使用しません。

出力

T_j = 2 の各操作について、条件を満たす連続する K 個の区画の選び方が存在する場合はその賃料の合計の最小値を、存在しない場合は IMPOSSIBLE を、それぞれ 1 行に出力してください。


入力例 1

5 3 5
10 20 5 7 8
1 0 1 0 1
2 1 0
2 2 0
1 2 1
2 1 0
2 2 0

出力例 1

20
20
13
16

入力例 2

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

出力例 2

IMPOSSIBLE
IMPOSSIBLE

入力例 3

12 4 10
15 3 22 8 7 13 5 19 11 2 17 6
1 0 1 1 0 0 1 0 1 1 0 1
2 2 0
2 3 0
1 5 4
2 2 0
1 10 20
2 1 0
1 1 30
2 4 0
1 8 1
2 3 0

出力例 3

33
36
30
30
IMPOSSIBLE
37

入力例 4

25 7 20
31 12 45 7 23 18 60 5 14 39 27 8 50 16 22 41 9 33 11 28 6 19 35 24 10
1 0 0 1 1 0 1 0 1 0 0 1 1 0 1 0 1 1 0 0 1 0 1 0 1
2 3 0
2 5 0
1 7 4
2 4 0
1 14 3
2 2 0
1 1 100
2 5 0
1 20 2
2 1 0
1 25 70
2 6 0
1 12 1
2 4 0
1 18 55
2 3 0
1 9 40
2 0 0
1 4 9
2 5 0

出力例 4

133
179
110
110
166
107
IMPOSSIBLE
110
108
114
181

入力例 5

1 1 5
1000000000
0
2 0 0
2 1 0
1 1 1
2 0 0
2 1 0

出力例 5

1000000000
IMPOSSIBLE
1
IMPOSSIBLE

Score : 466 pts

Problem Statement

Takahashi is planning to rent consecutive sections of a shopping street to set up an event venue.

The shopping street has N sections lined up in a row from left to right.

The rent for the i-th section from the left (1 \leq i \leq N) is A_i yen.

Also, for the i-th section from the left, C_i = 1 if it has a parking space, and C_i = 0 if it does not.

Takahashi is considering renting exactly K consecutive sections.

That is, he chooses an integer l (1 \leq l \leq N-K+1) and rents the K sections from the l-th to the (l+K-1)-th from the left.

Q operations will be performed in order. The j-th operation (1 \leq j \leq Q) is determined by the integer T_j and is one of the following:

  • T_j = 1 (rent change): Integers X_j, Y_j are given. Change the rent of the X_j-th section from the left to Y_j yen. This change affects all subsequent operations.
  • T_j = 2 (query): An integer X_j is given. Among all ways to choose exactly K consecutive sections, consider those where the chosen K sections contain at least X_j sections with parking spaces (sections where C_i = 1). If such a choice exists, output the minimum possible total rent of the chosen K sections. If no such choice exists, output IMPOSSIBLE.

Note that the values C_i representing the presence or absence of parking spaces do not change through operations.

For each operation with T_j = 2, output the answer.

Constraints

  • 1 \leq K \leq N \leq 10^5
  • 1 \leq Q \leq 5 \times 10^4
  • 1 \leq A_i \leq 10^9
  • C_i \in \{0, 1\}
  • T_j \in \{1, 2\}
  • When T_j = 1: 1 \leq X_j \leq N, 1 \leq Y_j \leq 10^9
  • When T_j = 2: 0 \leq X_j \leq K, Y_j = 0
  • There is at least one operation with T_j = 2
  • All inputs are integers

Input

N K Q
A_1 A_2 \cdots A_N
C_1 C_2 \cdots C_N
T_1 X_1 Y_1
T_2 X_2 Y_2
\vdots
T_Q X_Q Y_Q

Each operation is given as a triple of integers (T_j, X_j, Y_j). When T_j = 2, Y_j = 0 is given, but this is only to unify the input format and is not used in the solution.

Output

For each operation with T_j = 2, output on a single line the minimum total rent if a valid choice of K consecutive sections satisfying the condition exists, or IMPOSSIBLE if no such choice exists.


Sample Input 1

5 3 5
10 20 5 7 8
1 0 1 0 1
2 1 0
2 2 0
1 2 1
2 1 0
2 2 0

Sample Output 1

20
20
13
16

Sample Input 2

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

Sample Output 2

IMPOSSIBLE
IMPOSSIBLE

Sample Input 3

12 4 10
15 3 22 8 7 13 5 19 11 2 17 6
1 0 1 1 0 0 1 0 1 1 0 1
2 2 0
2 3 0
1 5 4
2 2 0
1 10 20
2 1 0
1 1 30
2 4 0
1 8 1
2 3 0

Sample Output 3

33
36
30
30
IMPOSSIBLE
37

Sample Input 4

25 7 20
31 12 45 7 23 18 60 5 14 39 27 8 50 16 22 41 9 33 11 28 6 19 35 24 10
1 0 0 1 1 0 1 0 1 0 0 1 1 0 1 0 1 1 0 0 1 0 1 0 1
2 3 0
2 5 0
1 7 4
2 4 0
1 14 3
2 2 0
1 1 100
2 5 0
1 20 2
2 1 0
1 25 70
2 6 0
1 12 1
2 4 0
1 18 55
2 3 0
1 9 40
2 0 0
1 4 9
2 5 0

Sample Output 4

133
179
110
110
166
107
IMPOSSIBLE
110
108
114
181

Sample Input 5

1 1 5
1000000000
0
2 0 0
2 1 0
1 1 1
2 0 0
2 1 0

Sample Output 5

1000000000
IMPOSSIBLE
1
IMPOSSIBLE
L - スケジュール調整

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

配点 : 500

問題文

高橋君は文化祭のステージ発表のスケジュールを管理しています。ここでは各発表の開始時刻のみを考え、発表の所要時間は考慮しません。

発表者は N 人います。発表者 i (1 \leq i \leq N) の発表開始時刻には 2 つの候補があります。時刻 A_i 分に開始するか、ちょうど D_i 分だけ遅らせて時刻 A_i + D_i 分に開始するかのいずれかです。

高橋君は、各発表者について遅らせるかどうかを決定します。その決定を 01 のみからなる長さ N の文字列 S で表します。

  • S_i = 0 のとき、発表者 i は時刻 A_i 分に発表を開始します。
  • S_i = 1 のとき、発表者 i は時刻 A_i + D_i 分に発表を開始します。

また、観客が共通しているために発表開始時刻をなるべく離したい M 組の発表者の対が与えられます。j 番目の対 (1 \leq j \leq M) は発表者 U_j と発表者 V_j からなります。

文字列 S に対し、f(S) を次のように定めます。

  • 各対 j (1 \leq j \leq M) について「発表者 U_j の実際の発表開始時刻と発表者 V_j の実際の発表開始時刻の差の絶対値」を求め、それらの最小値を f(S) とする。

f(S) を最大化することが高橋君の目標です。f(S) の最大値を K とするとき、以下を求めてください。

  1. K の値
  2. f(S) = K を達成する文字列 S のうち、辞書順最小のもの

ここで、文字列の辞書順比較は 0 < 1 として行います。

制約

  • 1 \leq N \leq 2000
  • 1 \leq M \leq 5000
  • N \times M \leq 10^6
  • 0 \leq A_i \leq 10^9
  • 1 \leq D_i \leq 10^9
  • 1 \leq U_j, V_j \leq N
  • U_j \neq V_j
  • (U_j, V_j) の組は順序を無視してすべて異なる
  • 入力はすべて整数

入力

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

N M
A_1 D_1
A_2 D_2
\vdots
A_N D_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M

N は発表者の人数、M は対の数である。A_i は発表者 i の予定開始時刻(分)、D_i は発表者 i の遅延可能時間(分)である。U_j, V_jj 番目の対を構成する発表者の番号である。

出力

以下の形式で出力してください。

K
S

1 行目に f(S) の最大値 K を、2 行目に f(S) = K を達成する辞書順最小の文字列 S を出力してください。


入力例 1

3 2
10 5
12 10
30 3
1 2
2 3

出力例 1

11
011

入力例 2

4 4
0 10
10 5
18 2
25 10
1 2
2 3
3 4
1 4

出力例 2

10
0011

入力例 3

8 10
5 7
20 4
13 15
40 10
0 30
55 5
25 20
70 8
1 2
1 3
2 4
3 4
3 5
4 6
5 7
6 8
2 7
1 8

出力例 3

12
00100010

入力例 4

15 25
100 30
150 20
80 100
300 40
260 75
400 10
50 200
500 60
620 80
700 25
710 90
900 100
850 30
1000 150
1100 50
1 2
1 3
1 7
2 3
2 4
2 8
3 5
3 7
4 5
4 6
4 9
5 6
5 10
6 8
6 11
7 8
7 12
8 9
8 13
9 10
9 14
10 11
11 15
12 13
14 15

出力例 4

40
110000100010000

入力例 5

2 1
0 1000000000
1000000000 1000000000
1 2

出力例 5

2000000000
01

Score : 500 pts

Problem Statement

Takahashi is managing the schedule of stage presentations for a school festival. Here, we only consider the start time of each presentation, and the duration of the presentations is not considered.

There are N presenters. Presenter i (1 \leq i \leq N) has two candidate start times for their presentation: either starting at time A_i minutes, or delaying it by exactly D_i minutes to start at time A_i + D_i minutes.

Takahashi decides whether to delay the presentation for each presenter. This decision is represented by a string S of length N consisting only of 0 and 1.

  • When S_i = 0, presenter i starts their presentation at time A_i minutes.
  • When S_i = 1, presenter i starts their presentation at time A_i + D_i minutes.

Additionally, we are given M pairs of presenters whose presentation start times should be as far apart as possible because they share some audience members. The j-th pair (1 \leq j \leq M) consists of presenter U_j and presenter V_j.

For a string S, we define f(S) as follows:

  • For each pair j (1 \leq j \leq M), calculate the absolute difference between the actual start time of presenter U_j and the actual start time of presenter V_j. The minimum of these absolute differences is f(S).

Takahashi's goal is to maximize f(S). Let K be the maximum value of f(S). Find the following:

  1. The value of K
  2. The lexicographically smallest string S that achieves f(S) = K.

Here, the lexicographical comparison of strings is performed with 0 < 1.

Constraints

  • 1 \leq N \leq 2000
  • 1 \leq M \leq 5000
  • N \times M \leq 10^6
  • 0 \leq A_i \leq 10^9
  • 1 \leq D_i \leq 10^9
  • 1 \leq U_j, V_j \leq N
  • U_j \neq V_j
  • All pairs (U_j, V_j) are unique, regardless of order.
  • All input values are integers.

Input

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

N M
A_1 D_1
A_2 D_2
\vdots
A_N D_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M

N is the number of presenters, and M is the number of pairs. A_i is the scheduled start time (in minutes) of presenter i, and D_i is the delay duration (in minutes) for presenter i. U_j and V_j are the indices of the presenters forming the j-th pair.

Output

Output in the following format:

K
S

In the first line, output the maximum value K of f(S). In the second line, output the lexicographically smallest string S that achieves f(S) = K.


Sample Input 1

3 2
10 5
12 10
30 3
1 2
2 3

Sample Output 1

11
011

Sample Input 2

4 4
0 10
10 5
18 2
25 10
1 2
2 3
3 4
1 4

Sample Output 2

10
0011

Sample Input 3

8 10
5 7
20 4
13 15
40 10
0 30
55 5
25 20
70 8
1 2
1 3
2 4
3 4
3 5
4 6
5 7
6 8
2 7
1 8

Sample Output 3

12
00100010

Sample Input 4

15 25
100 30
150 20
80 100
300 40
260 75
400 10
50 200
500 60
620 80
700 25
710 90
900 100
850 30
1000 150
1100 50
1 2
1 3
1 7
2 3
2 4
2 8
3 5
3 7
4 5
4 6
4 9
5 6
5 10
6 8
6 11
7 8
7 12
8 9
8 13
9 10
9 14
10 11
11 15
12 13
14 15

Sample Output 4

40
110000100010000

Sample Input 5

2 1
0 1000000000
1000000000 1000000000
1 2

Sample Output 5

2000000000
01
M - 秘密の数列と分岐するノート

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

配点 : 500

問題文

高橋君と青木君は、N 個の隠された整数 A_1, A_2, \ldots, A_N を推理しています。

A_i0 以上 K - 1 以下の整数ですが、具体的な値は二人には分かっていません。

区間 [L, R]スコアを、次の値として定義します。

(A_L + A_{L+1} + \cdots + A_R) \bmod K

二人は、推理の記録をノートの「版」として管理しています。

各操作は既存の版を参照して行われ、その結果として新しい版が作られます。これにより、版は木状に分岐していきます。

最初に、受理された記録が 1 つもない版 0 が存在します。

これから Q 個の操作が順に与えられます。

i 番目(1 \leq i \leq Q)の操作では、すでに存在する版 B0 \leq B \leq i - 1)を参照し、操作の結果として新しいi が作られます。

操作には次の 2 種類があります。

  • 種類 00 B L R X

青木君が、版 B に対して「区間 [L, R] のスコアは X である」と主張します。

B で受理されているすべての記録およびこの主張を同時に満たすような、各要素が 0 以上 K-1 以下の整数である割り当て (A_1, A_2, \ldots, A_N) が存在するなら、この主張を受理して YES を出力してください。このとき、版 i は版 B の受理済み記録にこの主張を加えたものになります。

存在しないなら、この主張を却下して NO を出力してください。このとき、版 i は版 B と同じ内容になります。

  • 種類 11 B L R

高橋君が、版 B で受理されている記録だけから、区間 [L, R] のスコアが一意に決まるかを尋ねます。

B の受理済み記録をすべて満たすような割り当て (A_1, A_2, \ldots, A_N)(各要素は 0 以上 K-1 以下の整数)がどのようなものであっても区間 [L, R] のスコアが同じ値になるなら、その値を出力してください。

一意に決まらないなら UNKNOWN を出力してください。

この操作では新しい記録は追加されず、版 i は版 B と同じ内容になります。

Q 個の操作を順に処理し、各操作への答えを出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 2 \leq K \leq 10^9
  • 1 \leq Q \leq 10^5
  • i 番目の操作(1 \leq i \leq Q)において、0 \leq B \leq i - 1
  • 各操作において、1 \leq L \leq R \leq N
  • 種類 0 の操作において、0 \leq X \leq K - 1
  • 入力はすべて整数である

入力

N K Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

1 行目には、要素数 N、法 K、操作の数 Q がスペース区切りで与えられる。

続く Q 行には、操作が時系列順に与えられる。

種類 0 の操作は次の形式で与えられる。

0 B L R X

これは、版 B に対して「区間 [L, R] のスコアは X である」と主張する操作を表す。

種類 1 の操作は次の形式で与えられる。

1 B L R

これは、版 B において区間 [L, R] のスコアが一意に決まるかを尋ねる操作を表す。

出力

各操作に対する答えを、操作が与えられた順に Q 行出力してください。

  • 種類 0 の操作に対しては、主張を受理するなら YES、却下するなら NO を出力してください。
  • 種類 1 の操作に対しては、値が一意に決まるならその値を 0 以上 K - 1 以下の整数として出力し、一意に決まらないなら UNKNOWN を出力してください。

入力例 1

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

出力例 1

UNKNOWN
YES
3
YES
2
YES
3
4

入力例 2

3 7 7
0 0 1 3 2
0 1 1 1 4
0 2 2 3 5
0 2 2 3 6
1 4 2 3
0 4 1 3 3
0 0 1 3 3

出力例 2

YES
YES
YES
NO
5
NO
YES

入力例 3

8 10 18
0 0 1 4 6
0 1 5 8 3
1 2 1 8
0 2 3 6 7
1 4 1 2
0 4 1 2 9
1 6 3 6
0 6 1 8 0
1 8 1 8
0 4 1 2 1
1 10 5 6
0 10 5 6 3
1 12 7 8
0 0 2 7 4
1 14 1 8
0 14 1 1 2
0 16 8 8 5
1 17 1 8

出力例 3

YES
YES
9
YES
UNKNOWN
YES
7
NO
9
YES
2
NO
1
YES
UNKNOWN
YES
YES
1

入力例 4

20 1000000000 30
0 0 1 10 123456789
0 1 11 20 987654321
1 2 1 20
0 2 5 15 555555555
1 4 1 4
0 4 1 4 222222222
1 6 5 10
1 6 11 15
0 6 16 20 333333333
0 6 16 20 333333334
1 10 16 20
0 4 1 4 111111111
1 12 5 10
0 12 11 15 123456789
0 0 3 18 700000000
1 15 1 20
0 15 1 2 100000000
0 17 19 20 200000000
1 18 1 20
0 18 1 20 0
0 18 1 20 1
1 21 3 18
0 6 7 14 444444444
1 23 1 15
0 23 1 15 777777777
0 23 1 15 777777778
1 26 1 15
0 12 16 20 333333333
0 12 16 20 444444444
1 29 1 20

出力例 4

YES
YES
111111110
YES
UNKNOWN
YES
901234567
654320988
YES
NO
333333333
YES
12345678
NO
YES
UNKNOWN
YES
YES
0
YES
NO
700000000
YES
777777777
YES
NO
777777777
NO
YES
111111110

入力例 5

1 2 8
1 0 1 1
0 0 1 1 0
1 2 1 1
0 2 1 1 1
1 4 1 1
0 0 1 1 1
1 6 1 1
0 6 1 1 1

出力例 5

UNKNOWN
YES
0
NO
0
YES
1
YES

Score : 500 pts

Problem Statement

Takahashi and Aoki are trying to deduce N hidden integers A_1, A_2, \ldots, A_N.

Each A_i is an integer between 0 and K - 1 inclusive, but the specific values are unknown to both of them.

The score of an interval [L, R] is defined as the following value:

(A_L + A_{L+1} + \cdots + A_R) \bmod K

The two manage their deduction records as "versions" of a note.

Each operation references an existing version and produces a new version as its result. This causes versions to branch out in a tree structure.

Initially, there exists version 0, which contains no accepted records.

Q operations are given in order.

In the i-th operation (1 \leq i \leq Q), an already existing version B (0 \leq B \leq i - 1) is referenced, and a new version i is created as the result of the operation.

There are two types of operations:

  • Type 0: 0 B L R X

Aoki claims, with respect to version B, that "the score of interval [L, R] is X."

If there exists an assignment (A_1, A_2, \ldots, A_N) where each element is an integer between 0 and K-1 inclusive that simultaneously satisfies all records accepted in version B and this claim, then accept this claim and output YES. In this case, version i consists of the accepted records of version B plus this claim.

If no such assignment exists, reject this claim and output NO. In this case, version i has the same content as version B.

  • Type 1: 1 B L R

Takahashi asks whether the score of interval [L, R] can be uniquely determined from only the records accepted in version B.

If, for every assignment (A_1, A_2, \ldots, A_N) (where each element is an integer between 0 and K-1 inclusive) that satisfies all accepted records of version B, the score of interval [L, R] is the same value, then output that value.

If it cannot be uniquely determined, output UNKNOWN.

This operation does not add any new records, and version i has the same content as version B.

Process the Q operations in order and output the answer to each operation.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 2 \leq K \leq 10^9
  • 1 \leq Q \leq 10^5
  • In the i-th operation (1 \leq i \leq Q), 0 \leq B \leq i - 1
  • In each operation, 1 \leq L \leq R \leq N
  • In type 0 operations, 0 \leq X \leq K - 1
  • All inputs are integers

Input

N K Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

The first line contains the number of elements N, the modulus K, and the number of operations Q, separated by spaces.

The following Q lines contain the operations in chronological order.

A type 0 operation is given in the following format:

0 B L R X

This represents an operation where "the score of interval [L, R] is X" is claimed with respect to version B.

A type 1 operation is given in the following format:

1 B L R

This represents an operation asking whether the score of interval [L, R] can be uniquely determined in version B.

Output

Output the answer to each operation on Q lines, in the order the operations are given.

  • For type 0 operations, output YES if the claim is accepted, or NO if it is rejected.
  • For type 1 operations, if the value is uniquely determined, output that value as an integer between 0 and K - 1 inclusive; if it is not uniquely determined, output UNKNOWN.

Sample Input 1

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

Sample Output 1

UNKNOWN
YES
3
YES
2
YES
3
4

Sample Input 2

3 7 7
0 0 1 3 2
0 1 1 1 4
0 2 2 3 5
0 2 2 3 6
1 4 2 3
0 4 1 3 3
0 0 1 3 3

Sample Output 2

YES
YES
YES
NO
5
NO
YES

Sample Input 3

8 10 18
0 0 1 4 6
0 1 5 8 3
1 2 1 8
0 2 3 6 7
1 4 1 2
0 4 1 2 9
1 6 3 6
0 6 1 8 0
1 8 1 8
0 4 1 2 1
1 10 5 6
0 10 5 6 3
1 12 7 8
0 0 2 7 4
1 14 1 8
0 14 1 1 2
0 16 8 8 5
1 17 1 8

Sample Output 3

YES
YES
9
YES
UNKNOWN
YES
7
NO
9
YES
2
NO
1
YES
UNKNOWN
YES
YES
1

Sample Input 4

20 1000000000 30
0 0 1 10 123456789
0 1 11 20 987654321
1 2 1 20
0 2 5 15 555555555
1 4 1 4
0 4 1 4 222222222
1 6 5 10
1 6 11 15
0 6 16 20 333333333
0 6 16 20 333333334
1 10 16 20
0 4 1 4 111111111
1 12 5 10
0 12 11 15 123456789
0 0 3 18 700000000
1 15 1 20
0 15 1 2 100000000
0 17 19 20 200000000
1 18 1 20
0 18 1 20 0
0 18 1 20 1
1 21 3 18
0 6 7 14 444444444
1 23 1 15
0 23 1 15 777777777
0 23 1 15 777777778
1 26 1 15
0 12 16 20 333333333
0 12 16 20 444444444
1 29 1 20

Sample Output 4

YES
YES
111111110
YES
UNKNOWN
YES
901234567
654320988
YES
NO
333333333
YES
12345678
NO
YES
UNKNOWN
YES
YES
0
YES
NO
700000000
YES
777777777
YES
NO
777777777
NO
YES
111111110

Sample Input 5

1 2 8
1 0 1 1
0 0 1 1 0
1 2 1 1
0 2 1 1 1
1 4 1 1
0 0 1 1 1
1 6 1 1
0 6 1 1 1

Sample Output 5

UNKNOWN
YES
0
NO
0
YES
1
YES
N - 株価の補正

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

配点 : 533

問題文

高橋君は、ある銘柄の N 日間の株価データを分析しています。 i 日目の株価は H_i 円です。

高橋君は、レポートに掲載するため、この株価データを「毎日厳密に上昇している」理想的なデータに補正したいと考えています。すなわち、補正後の株価 H'_1, H'_2, \ldots, H'_NH'_1 < H'_2 < \cdots < H'_N を満たすようにしたいです。

補正は各日の株価を個別に変更することで行います。 i 日目の株価を H_i から H'_i に変更するとき、そのコストは |H_i - H'_i| です。高橋君は、すべての日の変更コストの合計 \sum_{i=1}^{N} |H_i - H'_i| を最小にしたいと考えています。

ただし、補正後の各日の株価 H'_i は整数でなければなりません。

すべての日の株価が厳密に単調増加となるように補正したとき、コストの合計の最小値を求めてください。

制約

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

入力

N
H_1 H_2 \cdots H_N
  • 1 行目には、日数を表す整数 N が与えられる。
  • 2 行目には、各日の株価を表す N 個の整数 H_1, H_2, \ldots, H_N がスペース区切りで与えられる。

出力

コストの合計の最小値を 1 行で出力せよ。


入力例 1

5
5 3 2 4 5

出力例 1

6

入力例 2

7
3 3 3 3 3 3 3

出力例 2

12

入力例 3

10
10 1 12 3 14 5 16 7 18 9

出力例 3

50

Score : 533 pts

Problem Statement

Takahashi is analyzing stock price data for a certain stock over N days. The stock price on day i is H_i yen.

Takahashi wants to correct this stock price data into an ideal dataset that is "strictly increasing every day" for inclusion in a report. Specifically, he wants the corrected stock prices H'_1, H'_2, \ldots, H'_N to satisfy H'_1 < H'_2 < \cdots < H'_N.

The correction is performed by individually changing the stock price of each day. When changing the stock price on day i from H_i to H'_i, the cost is |H_i - H'_i|. Takahashi wants to minimize the total cost of changes across all days, \sum_{i=1}^{N} |H_i - H'_i|.

However, the corrected stock price H'_i for each day must be an integer.

Find the minimum total cost when correcting the stock prices so that they are strictly monotonically increasing across all days.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq H_i \leq 10^9
  • All inputs are integers.

Input

N
H_1 H_2 \cdots H_N
  • The first line contains an integer N representing the number of days.
  • The second line contains N integers H_1, H_2, \ldots, H_N representing the stock price on each day, separated by spaces.

Output

Print the minimum total cost in a single line.


Sample Input 1

5
5 3 2 4 5

Sample Output 1

6

Sample Input 2

7
3 3 3 3 3 3 3

Sample Output 2

12

Sample Input 3

10
10 1 12 3 14 5 16 7 18 9

Sample Output 3

50
O - 円環石板の結合

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

配点 : 533

問題文

高橋君は、円形に並んだ N 枚の石板を持っている。石板には隣り合う順に 1, 2, \dots, N と番号が付けられており、石板 i の重さは A_i である。石板 N と石板 1 も隣り合っている。

高橋君は、石板が 1 枚になるまで以下の操作を繰り返す。

  • 円環上で隣り合っている 2 枚の石板を選び、 1 枚に結合する。結合によるコストは、選んだ 2 枚の石板の重さの合計に等しい。結合後にできる新しい石板の重さも、選んだ 2 枚の石板の重さの合計に等しい。新しい石板は元の 2 枚の石板を取り除いた位置に入り、元の 2 枚それぞれの反対側で隣り合っていた石板と新たに隣り合う。こうして、残りの石板は引き続き円環構造を保つ。

すべての操作で発生するコストの合計の最小値を求めてください。

制約

  • 2 \leq N \leq 3000
  • 1 \leq A_i \leq 10^9
  • 入力はすべて整数

入力

 N 
 A_1   A_2   \cdots   A_N 
  • 1 行目には、石板の枚数を表す整数 N が与えられる。
  • 2 行目には、各石板の重さを表す整数 A_1, A_2, \dots, A_N がスペース区切りで与えられる。

出力

合計コストの最小値を 1 行で出力せよ。


入力例 1

4
3 1 4 1

出力例 1

18

入力例 2

3
10 20 30

出力例 2

90

入力例 3

8
7 2 9 4 6 3 8 5

出力例 3

132

入力例 4

30
12 45 7 23 89 34 56 78 11 90 5 67 38 42 19 73 61 29 84 16 50 99 3 27 64 8 71 36 14 52

出力例 4

6201

入力例 5

2
1000000000 1000000000

出力例 5

2000000000

Score : 533 pts

Problem Statement

Takahashi has N stone tablets arranged in a circle. The tablets are numbered 1, 2, \dots, N in adjacent order, and the weight of tablet i is A_i. Tablet N and tablet 1 are also adjacent.

Takahashi repeats the following operation until only one tablet remains.

  • Choose two tablets that are adjacent on the circle, and merge them into one. The cost of merging is equal to the sum of the weights of the two chosen tablets. The weight of the new tablet created by the merge is also equal to the sum of the weights of the two chosen tablets. The new tablet is placed at the position where the original two tablets were removed, and becomes adjacent to the tablets that were on the opposite sides of the original two tablets. In this way, the remaining tablets continue to maintain a circular structure.

Find the minimum possible total cost incurred across all operations.

Constraints

  • 2 \leq N \leq 3000
  • 1 \leq A_i \leq 10^9
  • All inputs are integers

Input

N
A_1 A_2 \cdots A_N
  • The first line contains an integer N representing the number of tablets.
  • The second line contains integers A_1, A_2, \dots, A_N representing the weight of each tablet, separated by spaces.

Output

Print the minimum total cost in one line.


Sample Input 1

4
3 1 4 1

Sample Output 1

18

Sample Input 2

3
10 20 30

Sample Output 2

90

Sample Input 3

8
7 2 9 4 6 3 8 5

Sample Output 3

132

Sample Input 4

30
12 45 7 23 89 34 56 78 11 90 5 67 38 42 19 73 61 29 84 16 50 99 3 27 64 8 71 36 14 52

Sample Output 4

6201

Sample Input 5

2
1000000000 1000000000

Sample Output 5

2000000000