Problem Description
Lindsay is a shopaholic. Whenever there is a discount of the kind where can buy three items and only pay for both, she goes completely mad and F Eels a need to buy all items in the store. You had given up on curing she disease, but the try to limit it effect on her wallet.
You have realized, the stores coming with these offers is quite elective when it comes to which items I get for fre E It's always the cheapest ones. As an example, when your friend comes to the counter with seven items, costing,,,,,,, and Dolla RS, she'll has to pay dollars. In this case she got a discount of dollars. You realize so if she goes to the counter three times, she might get a bigger discount. e.g. if she goes with the items costs, she'll get a discount of the first round. The next round she brings the item that costs giving no extra discount, but the third round she takes the last items T Hat costs, giving a discount of an additional in dollars, adding up to a total discount of 350.
Your job is to find the maximum discount Lindsay can get.
Input
The first line of input gives the number of test scenarios, 1 <= t <= 20. Each scenario consists of lines of input. The first gives the number of items Lindsay is buying, 1 <= n <= 20000. The next line gives the prices of these items, 1 <= pi <= 20000.
Output
For each scenario, output one line giving the maximum discount Lindsay can get by selectively choosing which items she BRI NGS to the counter at the same time.
Sample Input
1
6
400 100 200 350 300 250
Sample Output
400
The topic is very simple, just need to read the topic first.
In fact, it is said, a person to buy things, as long as buy 3 things, the cheapest thing can not pay, ask, how much money can save? 、、
We order the goods by price, and then every time we take the price of the third item from big to small, we can do it. Accumulate again.
Note that the product may not be a multiple of 3 oh ...
Import Java.util.arrays;import Java.util.Scanner; Public classmain{ Public Static void Main(string[] args) {Scanner SC =NewScanner (System.inch);intT =sc.nextint (); while(t-->0){intn = sc.nextint ();int[] A =New int[n]; for(intI=0; i<a.length;i++) {a[i]=sc.nextint (); } arrays.sort (a);intnum=0; for(inti=a.length-1-2; i>=n%3; i=i-3) {Num=num+a[i]; } System. out. println (num); } }}
Hdoj (HDU) 1678 Shopaholic