Candies
Time limit1sMemory limit128 MB
Given N bags of candies, choose which bag's count to replace with a new positive value so the number of distinct subset sums is maximized, breaking ties by smallest P then smallest Q.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Brute force, Array
- Solved
- No attempts yet
Problem
Kristian runs a shop that sells candy in prepackaged bags. He has bags, and bag holds candies. When a customer asks for exactly candies, Kristian must hand over a set of whole bags whose candy counts add up to exactly ; if no set of bags sums to , he cannot serve that request.
Call a positive integer servable if some non-empty subset of the bags sums to . The number of distinct options Kristian can offer is the number of distinct servable values .
To please more customers, Kristian opens exactly one bag and changes how many candies it holds. If he opens a bag that currently holds candies, he may refill it with any positive integer of candies. He wants to choose the bag (identified by its current count ) and the new amount so that the number of distinct options afterwards is as large as possible.
Input
The first line contains one integer ().
The second line contains integers () separated by single spaces — the number of candies in each bag.
Output
Print two integers and separated by a single space: Kristian should take a bag that currently holds candies and change its contents to candies. must be equal to one of the , and must be a positive integer.
If several choices reach the maximum possible number of distinct options, print the one with the smallest ; if a tie still remains, print the smallest . It is guaranteed that at least one such change strictly increases the number of distinct options.