E - Project Assignment Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 333 点

問題文

高橋君はある会社の部長です。部署には N 人の社員が所属しており、i 番目 (1 \leq i \leq N) の社員にはスキルポイント S_i が定められています。

ある日、M 件のプロジェクトが同時に立ち上がりました。j 番目 (1 \leq j \leq M) のプロジェクトには、優先度 P_j と、遂行に必要な社員の人数 R_j が設定されています。高橋君は、すべてのプロジェクトに対して必要な人数ちょうどの社員を割り当てたいと考えています。ただし、各社員は高々 1 件のプロジェクトにしか割り当てることができず、どのプロジェクトにも割り当てられない社員がいてもかまいません。

社内の方針として、割り当ては以下のルールに従って順番に行います:

  1. まず、M 件のプロジェクトを優先度の値が大きい順に並べる。優先度の値が同じプロジェクトが複数ある場合は、番号(j の値)が小さい方を先にする。
  2. この順番に従い、先頭のプロジェクトから 1 件ずつ社員の割り当てを行う。
  3. あるプロジェクトに社員を割り当てる際、まだどのプロジェクトにも割り当てられていない社員の人数がそのプロジェクトの必要人数 R_j に満たない場合、割り当て全体が失敗となり、処理を中断する(既に割り当て済みのプロジェクトの結果も含め、すべて無効となる)。
  4. 人数が足りている場合は、まだどのプロジェクトにも割り当てられていない社員の中から、スキルポイントが高い順に R_j 人を選んで割り当てる。スキルポイントが等しい社員が複数いる場合は、そのうち誰を選んでもよい。

注記: スキルポイントが等しい社員の選び方によらず、すべてのプロジェクトに割り当てられるスキルポイントの合計値はそれぞれ一意に定まります。これは、同じスキルポイントを持つ社員のうち誰を現在のプロジェクトに選んでも、残りの社員の集合(をスキルポイントの多重集合として見たもの)が変わらないためです。

上記のルールに従って割り当てを行ったとき、すべてのプロジェクトに必要な人数の社員を割り当てることが可能かどうかを判定してください。可能な場合は、各プロジェクトに割り当てられた社員のスキルポイントの合計値をそれぞれ求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq S_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq P_j \leq 10^9 (1 \leq j \leq M)
  • 1 \leq R_j \leq N (1 \leq j \leq M)
  • 入力はすべて整数である。

注意: R_1 + R_2 + \cdots + R_M \leq N は保証されません。必要人数の合計が社員数を超える場合、割り当ては途中で失敗する可能性があります。


入力

N M
S_1 S_2 \ldots S_N
P_1 R_1
P_2 R_2
\vdots
P_M R_M
  • 1 行目には、社員の人数 N と、プロジェクトの件数 M が、スペース区切りで与えられる。
  • 2 行目には、各社員のスキルポイント S_1, S_2, \ldots, S_N が、スペース区切りで与えられる。S_i は i 番目の社員のスキルポイントである。
  • 続く M 行にわたって、各プロジェクトの情報が与えられる。
  • そのうち j 行目 (1 \leq j \leq M)、すなわち入力全体の第 (2 + j) 行目では、j 番目のプロジェクトの優先度 P_j と、遂行に必要な社員の人数 R_j が、スペース区切りで与えられる。

出力

上記のルールに従って割り当てを行った結果、割り当てが失敗する場合は、-1 を 1 行で出力せよ。

すべてのプロジェクトへの割り当てが成功する場合は、M 行出力せよ。j 行目 (1 \leq j \leq M) には、j 番目のプロジェクトに割り当てられた社員のスキルポイントの合計値を出力せよ。


入力例 1

5 2
10 20 30 40 50
5 2
10 2

出力例 1

50
90

入力例 2

3 2
1 2 3
1 2
2 2

出力例 2

-1

入力例 3

10 3
5 15 25 35 45 55 65 75 85 95
3 3
7 2
7 4

出力例 3

75
180
240

入力例 4

15 4
100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500
1 2
5 3
5 4
10 3

出力例 4

900
3300
3000
4200

入力例 5

1 1
1000000000
1 1

出力例 5

1000000000

Score : 333 pts

Problem Statement

Takahashi is a department manager at a company. There are N employees in his department, and the i-th employee (1 \leq i \leq N) has a skill point value of S_i.

One day, M projects are launched simultaneously. The j-th project (1 \leq j \leq M) has a priority P_j and requires exactly R_j employees to carry it out. Takahashi wants to assign exactly the required number of employees to every project. However, each employee can be assigned to at most one project, and it is acceptable for some employees to not be assigned to any project.

According to company policy, assignments are made sequentially following these rules:

  1. First, sort the M projects in decreasing order of priority value. If multiple projects have the same priority value, the one with the smaller index (j value) comes first.
  2. Following this order, assign employees to one project at a time, starting from the first project.
  3. When assigning employees to a project, if the number of employees not yet assigned to any project is less than the required number R_j for that project, the entire assignment fails and processing is terminated (the results of all previously assigned projects are also invalidated).
  4. If there are enough employees, select the R_j employees with the highest skill points from those not yet assigned to any project. If multiple employees have equal skill points, any of them may be chosen.

Note: Regardless of how ties among employees with equal skill points are broken, the total skill points assigned to each project is uniquely determined. This is because no matter which employees with the same skill points are chosen for the current project, the remaining set of employees (viewed as a multiset of skill points) remains unchanged.

Following the rules above, determine whether it is possible to assign the required number of employees to all projects. If it is possible, find the total skill points of the employees assigned to each project.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq S_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq P_j \leq 10^9 (1 \leq j \leq M)
  • 1 \leq R_j \leq N (1 \leq j \leq M)
  • All input values are integers.

Note: It is NOT guaranteed that R_1 + R_2 + \cdots + R_M \leq N. If the total number of required employees exceeds the number of employees, the assignment may fail partway through.


Input

N M
S_1 S_2 \ldots S_N
P_1 R_1
P_2 R_2
\vdots
P_M R_M
  • The first line contains the number of employees N and the number of projects M, separated by a space.
  • The second line contains the skill points S_1, S_2, \ldots, S_N of each employee, separated by spaces. S_i is the skill point of the i-th employee.
  • The following M lines contain information about each project.
  • The j-th of these lines (1 \leq j \leq M), which is the (2 + j)-th line of the entire input, contains the priority P_j and the required number of employees R_j for the j-th project, separated by a space.

Output

If the assignment fails following the rules above, output -1 on a single line.

If the assignment to all projects succeeds, output M lines. The j-th line (1 \leq j \leq M) should contain the total skill points of the employees assigned to the j-th project.


Sample Input 1

5 2
10 20 30 40 50
5 2
10 2

Sample Output 1

50
90

Sample Input 2

3 2
1 2 3
1 2
2 2

Sample Output 2

-1

Sample Input 3

10 3
5 15 25 35 45 55 65 75 85 95
3 3
7 2
7 4

Sample Output 3

75
180
240

Sample Input 4

15 4
100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500
1 2
5 3
5 4
10 3

Sample Output 4

900
3300
3000
4200

Sample Input 5

1 1
1000000000
1 1

Sample Output 5

1000000000