Untitled
By: a guest | Mar 22nd, 2010 | Syntax:
None | Size: 3.21 KB | Hits: 75 | Expires: Never
import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
class triple {
int a,b,c;
public triple(int x, int y, int z) {
a = x;
b = y;
c = z;
}
}
public int[] mat;
public int[] cum;
HashMap< triple, Boolean > mapped;
int n, sum, max;
public Main( int len) {
n = len;
mat = new int[ n ];
cum = new int[ n ];
mapped = new HashMap< triple, Boolean >();
}
public void init() {
sum = 0;
for( int i = 0; i < n; i++) {
sum += mat[ i ];
}
max = sum/2;
Arrays.sort( mat);
Arrays.fill( cum, 0);
cum[ 0 ] = mat[ 0 ];
for( int i = 1; i < n; i++) {
cum[ i ] = mat[ i ] + cum[ i - 1 ];
}
}
public boolean exists( int i, int j, int k) {
triple temp = new triple( i, j, k);
if( mapped.containsKey( temp))
return mapped.get( temp);
if( ( j == 0) && ( k == 0)) {
mapped.put( temp, true);
return true;
}
if( i == 0) {
mapped.put( temp, false);
return false;
}
if( cum [ i - 1 ] < k) {
mapped.put( temp, false);
return false;
}
boolean ans;
ans = exists( i - 1, j - 1, k-mat[ i - 1 ]);
if( ans) {
mapped.put( temp, true);
return true;
}
ans = exists( i - 1, j, k);
if( ans) {
mapped.put( temp, true);
return true;
}
mapped.put( temp, false);
return false;
}
public void showdown() {
for( int i = max; i > 0; i--) {
if( exists( n, ( int)Math.ceil( (double)n/2), i)) {
System.out.println( i + " " + ( sum - i));
break;
}
}
}
public static void main( String[] args) throws IOException {
BufferedReader br = new BufferedReader( new InputStreamReader( System.in));
int tcase = Integer.parseInt( br.readLine());
for( int i = 0; i < tcase; i++) {
br.readLine();
int n = Integer.parseInt( br.readLine());
Main temp = new Main( n);
for( int j = 0; j<n; j++)
temp.mat[ j ] = Integer.parseInt( br.readLine());
temp.init();
temp.showdown();
if( i < ( tcase - 1))
System.out.println();
}
}
}