/* * Warmup Contest #2, September 4, 2010 Problem D * Solution by Dennis Meng * Originally from http://ncpc.idi.ntnu.no/ncpc2007/ncpc2007problems.pdf (problem H) * ACM NCPC 2007 Problem H * * It is always best to purchase the three most expensive items together * (so the 3rd one is free). Repeat until there is nothing left. * */ import java.util.Arrays; import java.util.Scanner; public class D { public static void main(String[] args) { Scanner myScanner = new Scanner(System.in); int cases = myScanner.nextInt(); for (int i = 0; i < cases; i++) { int arr[] = new int[myScanner.nextInt()]; for (int j = 0; j < arr.length; j++) { arr[j] = myScanner.nextInt(); } Arrays.sort(arr); int discount = 0; for (int k = arr.length - 3; k >= 0; k -= 3) { discount += arr[k]; } System.out.println(discount); } } }