Going to the Movies
InterviewTime limit1sMemory limit128 MB
Given a truck capacity C and up to 16 cow weights, pick a subset whose total weight is as large as possible without exceeding C.
- Level
Easy3 of 10
- Topics
- Brute force, Backtracking, Array, Recursion
- Solved
- No attempts yet
Problem
Farmer John is taking some of his cows to the movies! His truck can carry at most () kilograms, and he wants to pick the group of cows whose combined weight is as large as possible without exceeding this limit .
Given () cows and their weights , determine the largest total weight of a group of cows that the truck can carry.
Input
The first line contains two space-separated integers, and .
Each of the next lines contains a single integer , the weight of the -th cow.
Output
Print a single integer: the largest total weight of a group of cows that does not exceed . If no cow can be taken, print .