Buying CandiesProblem code: BUYING |
All submissions for this problem are available.
Emily is coming back from her two-week vacation at the other continent. But when Emily arrived at the airport, she realized that she had got no presents for her K friends! She still had some time before her flight, so she went for a walk around the airport hoping to find something suitable.
Soon Emily found a candy store and decided to buy some of her favorite candies for her friends. The store offers N packs of these candies, where pack i contains Ai candies.
Another reason Emily decided to buy candies is that she is fond of collecting empty candy packs from various parts of the world. That's why she decided to buy exactly M packs and present the friends with the candies and keep the packs for her collection. Emily would also like the total number of candies to be divisible by K so that an equal distribution of candies between friends is possible. Among all possible sets of packs, she would like to buy a set with the smallest possible total number of candies (money is the reason).
Your task is to help Emily, of course.
Input
The first line of the input file contains an integer T -- the number of test cases (no more than 5). T test cases follow, and each test case consists of two lines. The first of them contains three integers N, M and K (1 ? M ? N ? 50000, 1 ? K ? 20). The second of them contains N integers Ai (1 ? Ai ? 109).
Output
For each test case, output just one line containing the smallest possible total number of bought candies, or -1 if it's impossible to buy exactly M packs so that the total number of candies is divisible by K.
Example
Input: 2 5 3 5 1 2 3 4 5 6 3 4 9 5 1 7 3 7 Output: 10 -1 Explanation:
In the first test case, it's impossible to buy one candy per friend as three smallest packs contain 6 candies all together. Two candies per friend is possible though -- for example, if you buy packs with 1, 4 and 5 candies.
In the second test case, buying 3 packs implies buying an odd number of candies, which is impossible to distribute equally among 4 friends.
| Author: | gennady.korotkevich |
| Date Added: | 12-11-2011 |
| Time Limit: | 3 sec |
| Source Limit: | 50000 Bytes |
| Languages: | ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TCL, TEXT, WSPC |
Comments

Fetching successful submissions

can she buy same pack more
@dineshsingh No
Im using Dev C++ 4.9.9.2
In this coing do I have to
@adu101992: What do you mean
@adu101992: What do you mean by option ?
@adu101992. n,m,k are written
I meant should I choose C++
oh k thnx :)
@adu101992. You can choose
Do I have to use the same
Thnx :)
Should I end my program with
No, you shouldn't.
K then Im confused why its