UVa11269 - Setting Problems

import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Comparator;
import java.util.Arrays;

public class Main 
{
	public static final boolean DEBUG = false;
	public static Node[] node;
	public static int n;
	
	static class Node
	{
		int s, g;
	}
	
	public static boolean init(BufferedReader cin) throws IOException
	{
		String s = cin.readLine();
		if (s == null) return false;
		
		StringTokenizer st = new StringTokenizer(s);
		n = Integer.parseInt(st.nextToken());
		node = new Node[n];
		for (int i = 0; i < n; i++) {
			node[i] = new Node();
		}
		
		s = cin.readLine();
		st = new StringTokenizer(s);
		int i = 0;
		
		while (i < n && st.hasMoreTokens()) {
			node[i].s = Integer.parseInt(st.nextToken());
			i++;
		}
		
		s = cin.readLine();
		st = new StringTokenizer(s);
		i = 0;
		while (i < n && st.hasMoreTokens()) {
			node[i].g = Integer.parseInt(st.nextToken());
			i++;
		}
		return true;	
	}
	
	public static int solve()
	{
		Comparator<Node> cmp = new Comparator<Node>() {
			public int compare(Node a, Node b)
			{
				return a.s + Math.max(b.s, a.g) + b.g - (b.s + Math.max(a.s, b.g) + a.g);
			}
		};
		
		Arrays.sort(node, cmp);
		
		int s = 0, g = 0;
		for (int i = 0; i < n; i++) {
			s += node[i].s;
			if (s > g) g = s + node[i].g;
			else {
				g += node[i].g;
			}
		}
		
		return g;
	}
	
	public static void main(String[] args) throws IOException
	{
		BufferedReader cin;
		
		if (DEBUG) {
			cin = new BufferedReader(new FileReader("e:\\uva_in.txt"));
		} else {
			cin = new BufferedReader(new InputStreamReader(System.in));
		}
		
		while (init(cin)) {
			System.out.println(solve());
		}
	}
}

你可能感兴趣的:(UVa11269 - Setting Problems)