Family Dinner Family Dinner
Undefined
English

Dr. Mickhead's family is visiting him next Sunday to have dinner together. He wishes the party to be really special, so he plans to use his best plates for the table setting. Dr. Mickhead has N stacks of plates. Each stack contains K plates. Each plate has a positive beauty value, describing how beautiful it looks. Dr. Mickhead would like to take exactly P plates to use for dinner. If he would like to take a plate from a stack, he must also take all of the plates above it. Help Dr. Mickhead to calculate the maximum total sum of beauty values that can be achieved by picking P plates.

Note: P is always less than or equal to the total amount of plates in all stacks.

This mission was proposed by Ben Jacobson and Wajeb Saab for Google Kickstart Round A 2020.

Input: Two arguments

  • The first one is a list of lists of equal size. The i-th list contains positive integers, describing the beauty values of i-th stack of plates from top to bottom;
  • The second is a positive integer. The amount of plates Dr. Mickhead is willing to put on the table.

Output: An integer - the maximum total sum of beauty values that can be achieved with respect to the constraints.

Example:

Consider the following input: [[10, 10, 100, 30], [80, 50, 10, 50,]], 5. So the best option would be picking 3 plates from the first stack and 2 plates from the second stack. In total, the sum of beauty values is 250.

	[10, 10, 100, _] -> total: 10 + 10 + 100 = 120
	[80, 50,   _, _] -> total: 80 + 50       = 130
	----------------------------------------------
	                    total: 120 + 130     = 250
	

best_plates([[10, 10, 100, 30], 
             [80, 50, 10, 50,]], 5) == 250
			 
best_plates([[80, 80],
             [15, 50],
             [20, 10]], 3) == 180

How it’s used: This is a variation of a very well-known knapsack problem.

Precondition:
1 ≤ len(stacks) ≤ 100
1 ≤ len(stacks[i]) ≤ 50
1 ≤ stacks[i][j] ≤ 200