Market Shopping
InterviewTime limit10sMemory limit256 MB
You choose exactly k prices per query to maximize the odd total, printing -1 when no odd sum exists.
- Level
Medium5 of 10
- Topics
- Greedy, Sorting, Prefix sum
- Solved
- No attempts yet
Problem
Every morning the grandmother goes to the market and buys a few products. Her grandson noticed that the amount she pays is always an odd number, and that every grandmother in the region shops the same way.
The market sells products, and the grandmother buys at most one copy of each. She does not want to carry more money than she needs. One day she asked her grandson how much money to take if she plans to buy exactly products that day. The grandson does not know which products she will pick, so the amount has to be enough for any choice of products whose prices add up to an odd number.
In other words, choose exactly of the products so that the sum of their prices is odd, and report the largest such sum. The same question comes up on several days, so the grandson writes a program that reads all prices once and answers each question.
Input
The first line contains the number of products on the market, (). The second line contains the prices separated by spaces; each price is an integer between and . The third line contains the number of days () that the grandson still spends at his grandmother's house. Each of the next lines contains one integer (), the number of products the grandmother plans to buy that day.
Output
Print lines. On line , print the largest odd total price over all ways to choose exactly products. If no choice of products has an odd total price, print -1 on that line.