Pick 8 of N cards and spend at most M coins on upgrades to maximize the total attack power of the chosen deck.
Hard9Dynamic programmingGreedySortingNo attempts yetTime limit20sMemory limit512 MBClash Royale is a real-time strategy card game. Each card has an attack power and a level. Each player picks 8 cards to form a battle deck, and the total attack power of a deck is the sum of the attack powers of its cards. Players fight each other by placing cards from their battle decks into the arena. The winner of a battle receives coins, which can be used to upgrade cards. Upgrading a card increases its attack power.
After days of arena fights, Little Shawn has collected M coins in total, and he has decided to upgrade some of his cards. Shawn has N cards. The i-th card can have any level from 1 through Ki, and its attack power at level j is Ai,j. A card is upgraded one level at a time, and upgrading the i-th card from level j to level j+1 costs Ci,j coins. Before any upgrade, the i-th card is at level Li.
Shawn wants to spend some or all of his coins on upgrades and then form a deck of exactly 8 cards whose total attack power is as large as possible. He can upgrade the same card more than once as long as he can afford it, and he does not have to upgrade every card. Find the maximum total attack power of a deck Shawn can form.
The first line contains the number of test cases T. T test cases follow.
Each test case starts with a line containing two integers M and N: the number of coins and the number of cards Shawn has. Then N blocks follow. The i-th block has three lines that describe the i-th card.
For each test case, print one line in the form Case #x: y, where x is the test case number (starting from 1) and y is the maximum total attack power of a deck Shawn can form with the coins he has.
In the first sample case, upgrade the first 4 cards to level 3, upgrade the 5th and 6th cards to level 2, and keep the last 2 cards at level 1. This costs (1+2)+(1+3)+(1+4)+(1+5)+1+1=20 coins, and the total attack power is 100+100+100+100+10+10+1+1=422, which is the maximum possible.