A - コンサートチケットの予約

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

配点 : 266

問題文

高橋君は、人気アーティストのコンサートチケット予約システムを管理しています。

このコンサート会場には N 種類の座席エリアがあり、それぞれ 1 から N までの番号が付けられています。各エリア i (1 \leq i \leq N) には、チケット価格 C_i と、そのエリアの定員(座席数) K_i が設定されています。

M 人のファンが 1 番目から M 番目まで順番に、それぞれ 1 回ずつチケットの予約を試みます。j 番目のファンはエリア P_j のチケットを予約しようとします。その時点でエリア P_j の予約済み人数が定員 K_{P_j} 未満であれば、そのファンはエリア P_j のチケットを予約でき、エリア P_j の予約済み人数が 1 増えます。すでに予約済み人数が定員 K_{P_j} に等しい場合、そのファンは予約できません。予約できなかったファンが他のエリアに振り替えて予約することはありません。

すべてのファンの予約処理が終わった後、実際にチケットの予約が成立したファンのチケット価格の合計を求めてください。エリア i のチケットを予約したファンのチケット価格は C_i です。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq C_i \leq 10^4 (1 \leq i \leq N)
  • 1 \leq K_i \leq 10^5 (1 \leq i \leq N)
  • 1 \leq P_j \leq N (1 \leq j \leq M)
  • 入力はすべて整数である

入力

N M
C_1 K_1
C_2 K_2
\vdots
C_N K_N
P_1
P_2
\vdots
P_M
  • 1 行目には、座席エリアの数 N と、ファンの数 M が、スペース区切りで与えられる。
  • 続く N 行のうち i 行目には、エリア i のチケット価格 C_i と定員 K_i が、スペース区切りで与えられる。
  • 続く M 行のうち j 行目には、j 番目のファンが予約しようとするエリアの番号 P_j が与えられる。

出力

実際にチケットの予約が成立したファンのチケット価格の合計を 1 行で出力せよ。


入力例 1

2 5
500 2
1000 1
1
1
1
2
2

出力例 1

2000

入力例 2

3 8
300 3
700 2
1500 1
1
2
3
1
2
1
3
2

出力例 2

3800

入力例 3

5 12
1000 2
2500 3
800 1
5000 2
1200 4
1
2
3
4
5
1
2
2
3
4
5
5

出力例 3

23900

Score : 266 pts

Problem Statement

Takahashi is managing a reservation system for concert tickets of a popular artist.

The concert venue has N types of seating areas, numbered from 1 to N. Each area i (1 \leq i \leq N) has a ticket price C_i and a capacity (number of seats) K_i.

M fans, from the 1-st to the M-th, attempt to reserve tickets one at a time in order. The j-th fan tries to reserve a ticket for area P_j. If the number of reservations already made for area P_j at that point is less than the capacity K_{P_j}, the fan successfully reserves a ticket for area P_j, and the number of reservations for area P_j increases by 1. If the number of reservations already equals the capacity K_{P_j}, the fan cannot make a reservation. A fan who fails to reserve does not attempt to reserve in any other area.

After all fans have been processed, find the total ticket price of the fans who successfully reserved tickets. The ticket price for a fan who reserved a ticket in area i is C_i.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 10^5
  • 1 \leq C_i \leq 10^4 (1 \leq i \leq N)
  • 1 \leq K_i \leq 10^5 (1 \leq i \leq N)
  • 1 \leq P_j \leq N (1 \leq j \leq M)
  • All input values are integers

Input

N M
C_1 K_1
C_2 K_2
\vdots
C_N K_N
P_1
P_2
\vdots
P_M
  • The first line contains the number of seating areas N and the number of fans M, separated by a space.
  • The following N lines each contain, for the i-th line, the ticket price C_i and capacity K_i of area i, separated by a space.
  • The following M lines each contain, for the j-th line, the area number P_j that the j-th fan tries to reserve.

Output

Print the total ticket price of the fans who successfully reserved tickets, on a single line.


Sample Input 1

2 5
500 2
1000 1
1
1
1
2
2

Sample Output 1

2000

Sample Input 2

3 8
300 3
700 2
1500 1
1
2
3
1
2
1
3
2

Sample Output 2

3800

Sample Input 3

5 12
1000 2
2500 3
800 1
5000 2
1200 4
1
2
3
4
5
1
2
2
3
4
5
5

Sample Output 3

23900
B - ボールの転がり

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

配点 : 333

問題文

高橋君は、一列に並んだ M 個のマスの上でボールを転がす実験をしています。

マスには 1 から M までの番号が付けられており、マス 1 が最も高く、マス M が最も低い位置にあります。隣り合うマスの間には段差があり、マス i1 \leq i \leq M - 1 )とマス i + 1 の間の段差には、ボールが通過できる重さの閾値 W_i が定められています。重さが W_i 以上のボールはこの段差を越えてマス i+1 へ転がることができますが、重さが W_i 未満のボールはこの段差を越えることができず、そこで止まります。

高橋君は N 個のボールを使って実験を行います。ボール j1 \leq j \leq N )の重さは B_j です。各ボールについて、他のボールとは独立に以下の実験を行います。

高橋君がボール j をマス 1 に置くと、そのボールは以下のルールに従って転がります:

  1. ボールの現在のマスを p とする。初期値は p = 1 である。
  2. p < M かつ B_j \geq W_p ならば、ボールはマス p + 1 へ転がる( pp + 1 に更新する)。この手順を条件が満たされる限り繰り返す。
  3. p = M または B_j < W_p ならば、ボールはマス p で停止する。

各ボールについて、最終的に停止するマスの番号を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 2 \leq M \leq 2 \times 10^5
  • 1 \leq W_i \leq 10^91 \leq i \leq M - 1
  • 1 \leq B_j \leq 10^91 \leq j \leq N
  • 入力はすべて整数である

入力

N M
W_1 W_2 \ldots W_{M-1}
B_1 B_2 \ldots B_N
  • 1 行目には、ボールの個数を表す整数 N とマスの個数を表す整数 M が、スペース区切りで与えられる。
  • 2 行目には、各段差の重さの閾値を表す M - 1 個の整数 W_1, W_2, \ldots, W_{M-1} が、スペース区切りで与えられる。
  • 3 行目には、各ボールの重さを表す N 個の整数 B_1, B_2, \ldots, B_N が、スペース区切りで与えられる。

出力

N 行出力せよ。j 行目( 1 \leq j \leq N )には、ボール j が最終的に停止するマスの番号を整数で出力せよ。


入力例 1

3 5
2 5 3 1
4 1 10

出力例 1

2
1
5

入力例 2

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

出力例 2

1
1
6
6
6

入力例 3

7 10
5 12 8 20 3 15 6 10 25
10 20 5 30 1 8 100

出力例 3

2
9
2
10
1
2
10

Score : 333 pts

Problem Statement

Takahashi is conducting an experiment rolling balls on M squares arranged in a row.

The squares are numbered from 1 to M, where square 1 is at the highest position and square M is at the lowest position. There is a step between each pair of adjacent squares, and the step between square i (1 \leq i \leq M - 1) and square i + 1 has a weight threshold W_i that determines whether a ball can pass through. A ball with weight at least W_i can roll over this step to square i+1, but a ball with weight less than W_i cannot pass this step and stops there.

Takahashi conducts the experiment using N balls. Ball j (1 \leq j \leq N) has weight B_j. For each ball, the following experiment is performed independently of the other balls.

When Takahashi places ball j on square 1, the ball rolls according to the following rules:

  1. Let p be the current square of the ball. Initially, p = 1.
  2. If p < M and B_j \geq W_p, the ball rolls to square p + 1 (update p to p + 1). This step is repeated as long as the condition is satisfied.
  3. If p = M or B_j < W_p, the ball stops at square p.

For each ball, determine the number of the square where it finally stops.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 2 \leq M \leq 2 \times 10^5
  • 1 \leq W_i \leq 10^9 (1 \leq i \leq M - 1)
  • 1 \leq B_j \leq 10^9 (1 \leq j \leq N)
  • All input values are integers

Input

N M
W_1 W_2 \ldots W_{M-1}
B_1 B_2 \ldots B_N
  • The first line contains an integer N representing the number of balls and an integer M representing the number of squares, separated by a space.
  • The second line contains M - 1 integers W_1, W_2, \ldots, W_{M-1} representing the weight thresholds of each step, separated by spaces.
  • The third line contains N integers B_1, B_2, \ldots, B_N representing the weights of each ball, separated by spaces.

Output

Print N lines. The j-th line (1 \leq j \leq N) should contain the number of the square where ball j finally stops.


Sample Input 1

3 5
2 5 3 1
4 1 10

Sample Output 1

2
1
5

Sample Input 2

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

Sample Output 2

1
1
6
6
6

Sample Input 3

7 10
5 12 8 20 3 15 6 10 25
10 20 5 30 1 8 100

Sample Output 3

2
9
2
10
1
2
10
C - ペアの合計点

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

配点 : 366

問題文

青木先生のクラスでは N 人の生徒がテストを受けました。生徒にはそれぞれ 1 から N までの出席番号が付けられており、生徒 i の得点は A_i 点です。

青木先生は、異なる 2 人の生徒をペアにしてグループワークを行わせることを考えています。ペアの実力を測る指標として、 2 人の得点の合計を「ペアスコア」と定義します。

青木先生は、ペアスコアがある基準値 K 以上となるペアがいくつあるかを知りたいと思っています。

2 人の異なる生徒の組 (i, j)1 \leq i < j \leq N )のうち、 A_i + A_j \geq K を満たすものの個数を求めてください。

制約

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

入力

N K
A_1 A_2 \ldots A_N
  • 1 行目には、生徒の人数を表す整数 N と、ペアスコアの基準値を表す整数 K が、スペース区切りで与えられる。
  • 2 行目には、各生徒の得点を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。

出力

ペアスコアが K 以上となるペアの個数を 1 行で出力せよ。


入力例 1

4 6
1 3 5 7

出力例 1

5

入力例 2

3 10
1 2 3

出力例 2

0

入力例 3

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

出力例 3

9

入力例 4

15 100
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50

出力例 4

105

入力例 5

2 2000000000
1000000000 1000000000

出力例 5

1

Score : 366 pts

Problem Statement

In Mr. Aoki's class, N students took a test. Each student is assigned a student ID number from 1 to N, and student i scored A_i points.

Mr. Aoki is considering pairing up two different students for group work. As a measure of a pair's ability, he defines the "pair score" as the sum of the two students' scores.

Mr. Aoki wants to know how many pairs have a pair score of at least a certain threshold K.

Among all pairs of distinct students (i, j) (1 \leq i < j \leq N), find the number of pairs satisfying A_i + A_j \geq K.

Constraints

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

Input

N K
A_1 A_2 \ldots A_N
  • The first line contains an integer N representing the number of students and an integer K representing the threshold for the pair score, separated by a space.
  • The second line contains integers A_1, A_2, \ldots, A_N representing each student's score, separated by spaces.

Output

Output the number of pairs whose pair score is at least K, on a single line.


Sample Input 1

4 6
1 3 5 7

Sample Output 1

5

Sample Input 2

3 10
1 2 3

Sample Output 2

0

Sample Input 3

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

Sample Output 3

9

Sample Input 4

15 100
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50

Sample Output 4

105

Sample Input 5

2 2000000000
1000000000 1000000000

Sample Output 5

1
D - 冒険者パーティの編成

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

配点 : 400

問題文

高橋君はギルドマスターとして、ダンジョン攻略に挑む冒険者パーティを編成することになりました。

ギルドには N 人の冒険者が所属しています。各冒険者 i1 \leq i \leq N)には、攻撃力 A_i と防御力 B_i が設定されています。

高橋君は N 人の冒険者の中からちょうど K 人を選んでパーティを編成します。ただし、同じ冒険者を2回以上選ぶことはできません。パーティの戦闘力は以下のルールで計算されます:

  • 選んだ K 人の冒険者の攻撃力の合計を S とする
  • 選んだ K 人の冒険者の防御力の最小値を M とする
  • パーティの戦闘力は S \times M である

パーティ全体の攻撃力の合計がいくら高くても、防御力の最小値が小さいとパーティの戦闘力は伸び悩んでしまうのです。

K 人の選び方を最適に決めたとき、パーティの戦闘力としてありうる最大値を求めてください。

制約

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

入力

N K
A_1 B_1
A_2 B_2
\vdots
A_N B_N

1 行目には、ギルドに所属する冒険者の人数 N と、パーティに選ぶ人数 K が、スペース区切りで与えられます。

続く N 行のうち i 番目の行(1 \leq i \leq N)には、冒険者 i の攻撃力 A_i と防御力 B_i が、スペース区切りで与えられます。

出力

パーティの戦闘力としてありうる最大値を整数として 1 行で出力してください。


入力例 1

4 2
3 5
8 2
6 4
5 3

出力例 1

36

入力例 2

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

出力例 2

40

入力例 3

8 3
5 9
12 4
7 8
10 6
3 11
9 5
6 10
8 7

出力例 3

150

入力例 4

15 7
13 9
21 4
8 15
17 11
5 20
19 7
14 10
6 18
25 3
11 14
9 16
16 8
7 19
18 12
10 13

出力例 4

880

入力例 5

1 1
1000000 1000000

出力例 5

1000000000000

Score : 400 pts

Problem Statement

As a guild master, Takahashi must form a party of adventurers to challenge a dungeon.

The guild has N adventurers. Each adventurer i (1 \leq i \leq N) has an attack power A_i and a defense power B_i.

Takahashi will select exactly K adventurers from the N adventurers to form a party. The same adventurer cannot be selected more than once. The party's combat power is calculated according to the following rules:

  • Let S be the sum of the attack powers of the K selected adventurers.
  • Let M be the minimum defense power among the K selected adventurers.
  • The party's combat power is S \times M.

No matter how high the total attack power of the party is, if the minimum defense power is small, the party's combat power will suffer.

Find the maximum possible combat power of the party when the selection of K adventurers is chosen optimally.

Constraints

  • 1 \leq K \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^6
  • 1 \leq B_i \leq 10^6
  • All inputs are integers.

Input

N K
A_1 B_1
A_2 B_2
\vdots
A_N B_N

The first line contains the number of adventurers in the guild N and the number of adventurers to select for the party K, separated by a space.

The i-th of the following N lines (1 \leq i \leq N) contains the attack power A_i and defense power B_i of adventurer i, separated by a space.

Output

Output the maximum possible combat power of the party as an integer on a single line.


Sample Input 1

4 2
3 5
8 2
6 4
5 3

Sample Output 1

36

Sample Input 2

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

Sample Output 2

40

Sample Input 3

8 3
5 9
12 4
7 8
10 6
3 11
9 5
6 10
8 7

Sample Output 3

150

Sample Input 4

15 7
13 9
21 4
8 15
17 11
5 20
19 7
14 10
6 18
25 3
11 14
9 16
16 8
7 19
18 12
10 13

Sample Output 4

880

Sample Input 5

1 1
1000000 1000000

Sample Output 5

1000000000000
E - ビルの見晴らし

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

配点 : 466

問題文

高橋君は都市計画の調査員です。一直線の通りに沿って N 棟のビルが一列に並んでいます。左から順にビル 1 , ビル 2 , ... , ビル N と番号が付けられており、ビル i の高さは H_i です。すべてのビルの高さは互いに異なります。

各ビル i1 \leq i \leq N )について、「見晴らしスコア」 を以下のように定義します。

まず、ビル i から見て右方向(番号が i より大きい側)にある、ビル i よりも高いビルのうち、最も近いものをビル R_i とします。すなわち、 R_iH_j > H_i を満たす j > i のうち最小の j です。該当するビルが存在しない場合、 R_i は存在しないものとします。

同様に、ビル i から見て左方向(番号が i より小さい側)にある、ビル i よりも高いビルのうち、最も近いものをビル L_i とします。すなわち、 L_iH_j > H_i を満たす j < i のうち最大の j です。該当するビルが存在しない場合、 L_i は存在しないものとします。

直感的には、ビル i の見晴らしスコアは、ビル i を含み、左右それぞれ自分より高いビル(または通りの端)に到達するまでの連続する区間に含まれるビルの棟数です。具体的には、次のように定まります。

  • L_iR_i の両方が存在する場合:スコアは R_i - L_i - 1(ビル L_i とビル R_i の間にあるビルの棟数)
  • R_i のみ存在し L_i が存在しない場合:スコアは R_i - 1(ビル 1 からビル R_i - 1 までのビルの棟数)
  • L_i のみ存在し R_i が存在しない場合:スコアは N - L_i(ビル L_i + 1 からビル N までのビルの棟数)
  • どちらも存在しない場合:スコアは N(すべてのビルの棟数)

高橋君は Q 個の質問に答える必要があります。各質問では整数 X_k が与えられ、見晴らしスコアが X_k 以上であるビルの棟数 を答えてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq H_i \leq 10^91 \leq i \leq N
  • H_i はすべて互いに異なる
  • 1 \leq X_k \leq N1 \leq k \leq Q
  • 入力はすべて整数である

入力

N Q
H_1 H_2 \cdots H_N
X_1
X_2
\vdots
X_Q
  • 1 行目には、ビルの棟数を表す整数 N と質問の数を表す整数 Q が、スペース区切りで与えられる。
  • 2 行目には、各ビルの高さを表す整数 H_1, H_2, \ldots, H_N がスペース区切りで与えられる。すべての H_i は互いに異なる。
  • 3 行目から Q 行にわたって、各質問の値を表す整数 X_k1 行に 1 つずつ与えられる。

出力

Q 行出力せよ。 k 行目( 1 \leq k \leq Q )には、第 k 番目の質問に対する答え、すなわち見晴らしスコアが X_k 以上であるビルの棟数を出力せよ。


入力例 1

5 4
3 1 4 2 5
1
2
3
5

出力例 1

5
3
2
1

入力例 2

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

出力例 2

7
3
2
1

入力例 3

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

出力例 3

10
6
4
2
1

入力例 4

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

出力例 4

15
8
4
1
1
1

入力例 5

1 1
1000000000
1

出力例 5

1

Score : 466 pts

Problem Statement

Takahashi is a surveyor for urban planning. Along a straight street, N buildings stand in a row. They are numbered Building 1, Building 2, ..., Building N from left to right, and the height of Building i is H_i. All building heights are distinct.

For each Building i (1 \leq i \leq N), we define a "view score" as follows.

First, among the buildings to the right of Building i (those with numbers greater than i) that are taller than Building i, let Building R_i be the nearest one. That is, R_i is the smallest j > i such that H_j > H_i. If no such building exists, then R_i does not exist.

Similarly, among the buildings to the left of Building i (those with numbers less than i) that are taller than Building i, let Building L_i be the nearest one. That is, L_i is the largest j < i such that H_j > H_i. If no such building exists, then L_i does not exist.

Intuitively, the view score of Building i is the number of buildings in the contiguous interval containing Building i that extends left and right until reaching a taller building (or the end of the street) in each direction. Specifically, it is determined as follows:

  • If both L_i and R_i exist: the score is R_i - L_i - 1 (the number of buildings between Building L_i and Building R_i)
  • If only R_i exists and L_i does not exist: the score is R_i - 1 (the number of buildings from Building 1 to Building R_i - 1)
  • If only L_i exists and R_i does not exist: the score is N - L_i (the number of buildings from Building L_i + 1 to Building N)
  • If neither exists: the score is N (the total number of buildings)

Takahashi needs to answer Q questions. For each question, an integer X_k is given. Answer the number of buildings whose view score is at least X_k.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq H_i \leq 10^9 (1 \leq i \leq N)
  • All H_i are distinct
  • 1 \leq X_k \leq N (1 \leq k \leq Q)
  • All input values are integers

Input

N Q
H_1 H_2 \cdots H_N
X_1
X_2
\vdots
X_Q
  • The first line contains two integers separated by a space: N, the number of buildings, and Q, the number of questions.
  • The second line contains N integers H_1, H_2, \ldots, H_N separated by spaces, representing the heights of the buildings. All H_i are distinct.
  • The following Q lines each contain a single integer X_k, representing the value for each question.

Output

Output Q lines. On the k-th line (1 \leq k \leq Q), output the answer to the k-th question, that is, the number of buildings whose view score is at least X_k.


Sample Input 1

5 4
3 1 4 2 5
1
2
3
5

Sample Output 1

5
3
2
1

Sample Input 2

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

Sample Output 2

7
3
2
1

Sample Input 3

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

Sample Output 3

10
6
4
2
1

Sample Input 4

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

Sample Output 4

15
8
4
1
1
1

Sample Input 5

1 1
1000000000
1

Sample Output 5

1