Sicily Online Judge 1438. Shopaholic (Quick row, three summation)

Source: Internet
Author: User
Tags creative commons attribution

1438. Shopaholic Constraints

Time limit:1 secs, Memory limit:32 MB

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 selective when it comes to which items you get for FR Ee 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, Max and GIVing Tsun 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
16400 100 200 350 300 250
Sample Output
400
Problem Solving Ideas:

Sorting requires a fast row to not time out, and then the sum of three numbers, is basically a water problem. Take this opportunity to re-implement a quick row.

Source:
//problem#: 1438//submission#: 3295603//The source code is licensed under Creative Commons attribution-noncommercial-sharealike 3.0 Unported License//URI:http://creativecommons.org/licenses/by-nc-sa/3.0///All Copyright reserved by informatic Lab of Sun Yat-sen University#include <cstdio>#include<algorithm>voidQuickSort (int* p,intSinte) {intKey = p[(S + E)/2]; intx =s; inty =e; if(S > E | | s <0|| E <0)return;  while(X <y) { while(P[x] < key) x + +;  while(P[y] > key) y--; if(x <=y) {intTMP =P[x]; P[X]=P[y]; P[y]=tmp; X++; y--; }  }  if(S <y) QuickSort (p, S, y); if(X <e) QuickSort (p, X, E);}intGetsum (int*p,intN) {intsum =0;  for(inti = n-3; I >=0; I-=3) {sum+=P[i]; }  returnsum;}intMain () {intT, N; intp[20020]; scanf ("%d", &t);  while(t--) {scanf ("%d", &N);  for(inti =0; I < n; i++) {scanf ("%d", &P[i]); } quickSort (P,0N1); printf ("%d\n", Getsum (P, N)); }  return 0;} 

Sicily Online Judge 1438. Shopaholic (Quick row, three summation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.