Under construction

Still being built. Things may change

Food Festival

Time limit1sMemory limit512 MB

Summary
Given each dish's student count and each chef's cooking time per dish, assign dishes to chefs and set their order to minimize the total waiting time of all students.
Level

Medium7 of 10

Topics
Graph, Greedy
Solved
No attempts yet

Problem

City CZ is hosting a large food festival to welcome students from across the nation. Little M loves trying new flavors and does not want to miss any dish. He wants no student to wait for a dish that is already on someone else's table, so he studies how to order the preparation of dishes to minimize the students' waiting time.

The festival has nn kinds of dishes. Each student picks one of these kinds when ordering. There are mm chefs. After all students have ordered, each chef is assigned a list of dishes to prepare. Then all chefs start cooking at the same time. Each chef cooks in the assigned order and can make only one portion at a time.

Chefs need different amounts of time for the same dish. Let ti,jt_{i,j} be the time chef jj needs to prepare dish ii. Dishes are numbered 1 to nn, and chefs are numbered 1 to mm.

A student's waiting time is the time from when all chefs start cooking until the student's dish is finished. If the student's dish is the kk-th dish that some chef prepares, the waiting time is the sum of the preparation times of that chef's first kk dishes. The total waiting time is the sum of the waiting times of all students.

exactly pip_i students ordered dish ii. Find the minimum possible total waiting time.

Input

The first line contains two positive integers nn and mm.

The second line contains nn positive integers p1,p2,…,pnp_1, p_2, \ldots, p_n, where pip_i is the number of students who ordered dish ii.

Each of the next nn lines contains mm nonnegative integers. The jj-th integer on the ii-th line is ti,jt_{i,j}. Adjacent numbers on a line are separated by a single space, and no line has trailing spaces.

Output

Print one line containing the minimum possible total waiting time.

Constraints

n≤40n \le 40, m≤100m \le 100, p≤800p \le 800, and ti,j≤1000t_{i,j} \le 1000, where p=∑pip = \sum p_i is the total number of students who ordered.

Examples1

  1. Example 1

    Input
    3 2
    3 1 1
    5 7
    3 6
    8 9
    
    Expected output
    47