#K15486. Maximum Weight Subset

    ID: 24367 Type: Default 1000ms 256MiB

Maximum Weight Subset

Maximum Weight Subset

You are given n components and a maximum weight limit W. Each component has an individual weight. Your task is to select a subset of these components such that the total weight is as large as possible but does not exceed W.

This is a variation of the 0/1 Knapsack problem where the value and weight of each component are the same.

Mathematical formulation:

Find a subset S of {1, 2, ..., n} such that

\[ \sum_{i \in S} w_i \leq W \quad \text{and} \quad \max \left\{ \sum_{i \in S} w_i \right\}, \]

where \(w_i\) is the weight of the i-th component.

inputFormat

The first line of input contains two integers n and W separated by a space, representing the number of components and the maximum weight limit, respectively.

The second line contains n space-separated integers, each representing the weight of a component.

outputFormat

Output a single integer, which is the maximum weight that can be achieved without exceeding W.

## sample
5 10
2 3 8 1 6
10

</p>