This page is still under construction.

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

Fixed Partition Contest Management

Time limit1sMemory limit128 MB

Summary
Assign up to 10 problems to at most 3 members and order each member's load to minimize the sum of completion times, where a problem's duration depends on the solver's brightness.
Level

Medium6 of 10

Topics
Brute force, Dynamic programming, Greedy, Sorting
Solved
No attempts yet

Problem

A team's total intellectual capacity is split among several members. Each member has a fixed amount of intelligence (its “brightness”), and different members may have different amounts; the sum of all members' brightness is the team's total intellectual capacity.

You are given a set of problems, and the team must assign each problem to a member so that they can be solved concurrently. No member may work on two problems at the same time, and a problem may only be given to a member whose brightness is at least the problem's minimum requirement. The time needed to solve a problem depends on the brightness of the member who solves it: giving it to a brighter member may make the solution time shorter, or longer.

Every problem is submitted at the start of the contest, time 00. The solution time of a problem is the moment at which it is solved (equal to its completion time, since it is submitted at time 00).

Choose which member solves each problem, and the order in which each member solves its assigned problems (so that no member solves two problems at once), to minimize the sum of the solution times of all problems.

Input

The input contains several test cases. Each test case begins with a line containing two integers mm and nn, where mm is the number of team members (1≤m≤31 \le m \le 3) and nn is the number of problems to solve (1≤n≤101 \le n \le 10).

The next line contains mm positive integers giving the brightness of each member, in order.

The following nn lines describe the time–brightness tradeoff of each problem. Each line starts with a positive integer kk (k≤10k \le 10), followed by kk pairs of positive integers s1 t1 s2 t2 … sk tks_1\ t_1\ s_2\ t_2\ \dots\ s_k\ t_k satisfying si<si+1s_i < s_{i+1} for 1≤i<k1 \le i < k. The problem's minimum brightness requirement is s1s_1; a member with less brightness cannot solve it. If a member with brightness ss solves the problem and si≤s<si+1s_i \le s < s_{i+1} for some ii, the solution time is tit_i; if the member's brightness is at least sks_k, the solution time is tkt_k.

The line 0 00\ 0 follows the last test case and marks the end of the input.

Each problem takes exactly the time specified for the given brightness, no matter how many other problems other members are solving at the same time. No problem has a brightness requirement larger than the brightest member's brightness.

Output

For each test case, print one line in the form Case X: T, where XX is the test case number (starting at 11 and increasing by 11) and TT is the minimum achievable sum of the solution times of all problems — that is, over all valid assignments and orderings, the smallest possible total of the nn problems' solution times, printed as an integer.

Examples1

  1. Example 1

    Input
    2 4
    40 60
    1 35 4
    1 20 3
    1 40 10
    1 60 7
    3 5
    10 20 30
    2 10 50 12 30
    2 10 100 20 25
    1 25 19
    1 19 41
    2 10 18 30 42
    0 0
    
    Expected output
    Case 1: 31
    Case 2: 177