Optimizing Assignment
Time limit2sMemory limit1024 MB
Given parts with times and an assignment of parts to employees, count swaps of one part between two employees that reduce the maximum of their two totals.
- Level
Medium7 of 10
- Topics
- Sorting, Two pointers, Binary search, Implementation
- Solved
- No attempts yet
Problem
The company <<QQQ>> has employees. The company's new project consists of independent parts. The manager estimated the time needed to complete each part of the project (this time does not depend on who performs the part). He then assigned all parts to the employees in some way. As a result, some employees have to spend more time on their work than others (because they received a larger workload).
So the manager decided to improve the assignment as follows: choose two distinct employees and choose one part of the project assigned to the first employee and one part assigned to the second. Then assign the part assigned to the first employee to the second, and the part assigned to the second to the first. If this operation decreases the maximum of the work times of the first and second employees, the operation is called an optimizing operation.
For example, suppose the project consists of five parts with completion times , and the company has three employees. Let the assignment be as follows: the first employee has parts and (total time ), the second employee has part (total time ), and the third employee has parts and (total time ). Then if the first task (assigned to the first employee) is assigned to the third, and the fifth task (assigned to the third) is assigned to the first, the first employee's total time becomes , and the third employee's becomes . Since , this operation is optimizing.
You are given the number of employees in the company, the number of parts in the project, the time needed to complete each part, and the assignment of parts to employees. You need to count the number of distinct possible optimizing operations in the given assignment.
Input
The first line contains two natural numbers and (): the number of employees in the company and the number of parts in the project. The second line contains natural numbers; the -th number is the completion time of the -th part of the project (parts are numbered starting from ). Completion times of parts do not exceed . The next lines describe the assignment of parts to employees. Each line contains the description of the parts received by the corresponding employee: the number of parts assigned to the employee and their indices.
Output
Output the number of optimizing operations.
Hint
In the second example, every operation is optimizing.