Problem: Help a Thief!!!

*Difficulty: Basic

You have to help a thief to steal as many as GoldCoins as possible from a GoldMine. There he saw N Gold Boxes an each Gold Boxes consists of Ai Plates each plates consists of Aj Gold Coins, Each Box consist of plates with exactly same number of GoldCoins per plate. Your task is to print the maximum gold coins theif can steal.

Input:
First line consists of T test cases. Each test case consists of three lines.
First line of each test case consistss of an Integer nT denoting the number of plates theif can steal.
Second line of each test case consists of an Integer N denoting the number of Gold Boxes available in the gold mine.
Third line of each test case consists of 2*N spaced Integers Ai,Aj dentoning number of plates in each box and number of gold coins in each plates respectively.

Output:
Print the respective result in each line.

Constraints:
1<=T<=20
1<=nT,Ai<=20
1<=N<=20
1<=Aj<=100

Example:
Input:

1
3
3
1 3 2 2 3 1
Output:
7

Implementation: Python 2.7

#code
def sortByGoldCoins(goldBoxes):
    return sorted(goldBoxes, key=lambda goldBox: goldBox[1], reverse=True)

def helpThief(goldBoxes, platesCanSteal):
    maxGoldCoins, i = 0, 0
    while platesCanSteal > 0:
        if platesCanSteal >= goldBoxes[i][0]:
            maxGoldCoins += goldBoxes[i][1] * goldBoxes[i][0]
            platesCanSteal -= goldBoxes[i][0]
            i += 1
        else:
            maxGoldCoins += platesCanSteal * goldBoxes[i][0]
            platesCanSteal = 0
    return maxGoldCoins

def main():
    t = input()
    for _ in range(t):
        nT = input()
        N = input()
        goldBoxes = map(int, raw_input().strip().split())
        plates = [goldBoxes[i] for i in range(len(goldBoxes)) if i % 2 == 0]
        goldCoins = [goldBoxes[i] for i in range(len(goldBoxes)) if i % 2 != 0]
        goldBoxes = zip(plates, goldCoins)
        goldBoxes = sortByGoldCoins(goldBoxes)
        #print goldBoxes
        print helpThief(goldBoxes, nT)

if __name__ == '__main__':
    main()

 

Advertisements