#K65487. Truck Loading Problem

    ID: 32208 Type: Default 1000ms 256MiB

Truck Loading Problem

Truck Loading Problem

You are given a truck with a weight limit (W) and (n) parcels with specified weights. Your task is to determine the maximum weight that can be loaded into the truck without exceeding (W). Each parcel can be loaded at most once. This is a classic 0/1 knapsack problem where the value and weight of each item are equal to the weight of the parcel.

For example, if (W = 50) and the parcels have weights [10, 20, 30, 40, 50], the optimal solution is to load the parcel weighing 50, giving a total load of 50. If (W = 20) and the parcels are [10, 15, 5], an optimal selection would result in a total load of 20 (by choosing 15 and 5).

inputFormat

Input is read from standard input (stdin). The first line contains two integers (W) (the truck's weight limit) and (n) (the number of parcels). The second line contains (n) space-separated integers representing the weights of the parcels. If (n = 0), the second line may be empty.

outputFormat

Output a single integer to standard output (stdout) representing the maximum total weight of parcels that can be loaded into the truck without exceeding the limit (W).## sample

50 5
10 20 30 40 50
50