Three problems

4.4

7 votes
Algorithms, Basics of Greedy Algorithms, Greedy Algorithms
Problem

You are given n problems. The problems are of three types, 'Type1', 'Type2', and 'Type3'. There are t1 'Type1' problems, t2 'Type2' problems, and t3 'Type3' problems. You can solve each problem using any of the three methods, 'A', 'B', and 'C'. You can use a particular method only a limited number of times that is, method 'A' for a times, method 'B' for b times, and method 'C' for c times. 

You are given a 3×3 matrix A where A[i][j] represents the effort to solve a Type i problem using Method j. You are required to find the minimum effort required to solve all the problems.

Input format

  • The first line contains an integer T denoting the number of test cases.
  • The first line of each test case contains an integer n denoting the number of problems.
  • The second line of each test case contains three space-separated integers denoting the values of t1, t2, and t3 respectively.
  • The third line of each test case contains three space-separated integers denoting the values of a, b, and c respectively.
  • Next three lines of each test case contain three space-separated integers of matrix A.

Output format

For each test case, print the minimum effort required to solve all the problems in a new line.

Constraints

1T51n1e9

0t1, t2, t31e9 (sum of t1, t2, and t3 is equal to n)

0a, b, c1e9 (sum of a, b, and c is equal to n)

1A[i][j]1e9 (1i, j3)

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

For the only testcase, the optimal solution is -

  • solve five 'Type1' problems using method 'B', costing 58=40
  • solve one 'Type2' problem using method 'B', costing 12=2.
  • solve two 'Type3' problems using method 'A' and one 'Type3' problem using method 'B', costing 22+110=14.

Thus, total effort is 40+2+14=56

 

Editor Image

?