#QHD0000095. Tạ đĩa gần 1000 kg (Walrus Weights)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Tạ đĩa gần 1000 kg (Walrus Weights)
Walrus Weights
Source: Kattis
Version: Phuoc Hung OJ Extended
Problem Statement
Wallace the walrus is training to lift exactly kilograms. He has separate weight plates, indexed from to . Plate has a positive integer weight . He may choose any subset of the plates, and each plate can be chosen at most once. Even if two plates weigh the same, they are distinct physical plates. The empty selection is allowed, with total weight zero.
For a selection with total weight , Wallace wants to minimize . If two different reachable totals have the same minimum distance, he must take the larger total. For example, if totals and are both reachable and no closer total exists, the answer is .
Find the best total weight, not the indices of the selected plates.
Input
- The first line contains one integer , the number of plates.
- Each of the next lines contains one positive integer , the weight of plate .
Output
Output the attainable integer total closest to ; on a tie, output the larger total.
Subtasks
- Subtask 1 (20 points): and for every .
- Subtask 2 (30 points): and for every .
- Subtask 3 (50 points): and for every .
Examples
Example 1
Input
4
900
500
498
4
Output
1002
Explanation: Plates weighing , , and give total , two units above the goal. The pair , gives , two units below it. Since the distances tie, Wallace chooses . There is no attainable total closer than two units.
Example 2
Input
1
1
Output
1
Explanation: The only attainable totals are and ; the latter is closer to .