This page is still under construction.

Parts of this page are still being built. What you see may change.

Optimizing Assignment

Time limit2sMemory limit1024 MB

Summary
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 nn employees. The company's new project consists of mm 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 mm parts to the nn 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 3,6,4,8,23, 6, 4, 8, 2, and the company has three employees. Let the assignment be as follows: the first employee has parts 11 and 22 (total time 3+6=93 + 6 = 9), the second employee has part 44 (total time 88), and the third employee has parts 33 and 55 (total time 4+2=64 + 2 = 6). 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 6+2=86 + 2 = 8, and the third employee's becomes 3+4=73 + 4 = 7. Since max⁡(9,6)>max⁡(8,7)\max(9, 6) > \max(8, 7), 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 nn and mm (1≤n,m≤1051 \le n, m \le 10^5): the number of employees in the company and the number of parts in the project. The second line contains mm natural numbers; the ii-th number is the completion time of the ii-th part of the project (parts are numbered starting from 11). Completion times of parts do not exceed 10910^9. The next nn 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.

Examples2

  1. Example 1

    Input
    3 5
    3 6 4 8 2
    2 1 2
    1 4
    2 3 5
    
    Expected output
    2
    
  2. Example 2

    Input
    2 4
    1 2 3 4
    2 1 2
    2 3 4
    
    Expected output
    4