The Java Program: balloons.java
1
2 import java.io.*;
3 import java.util.*;
4
5 public class balloons
6 {
7 public Scanner sc;
8 public PrintStream ps;
9
10
11
12
13 public class Team implements Comparable<Team>
14 {
15 public int number, distances[], prefer, difference;
16
17
18
19
20
21
22
23
24 public Team( int n, int d0, int d1 )
25 {
26 number = n;
27 distances = new int[2];
28 distances[0] = d0;
29 distances[1] = d1;
30
31
32 prefer = d0<d1 ? 0 : 1;
33
34
35 difference = Math.abs( d1-d0 );
36 }
37
38
39
40
41
42
43
44
45 public int compareTo( Team t )
46 {
47 return t.difference - difference;
48 }
49
50
51
52
53
54
55 public String toString()
56 {
57 return "{Team: n=" + number + ", d0=" + distances[0] + ", d1=" + distances[1]
58 + ", prefer=" + prefer + ", difference=" + difference + "}";
59 }
60 }
61
62
63
64
65
66 public void doit() throws Exception
67 {
68 sc = new Scanner( new File( "balloons.judge" ) );
69 ps = new PrintStream( new FileOutputStream( "balloons.out" ) );
70 int s[] = new int[2];
71
72 for(;;)
73 {
74 int n = sc.nextInt();
75 if( n==0 ) break;
76
77
78 s[0] = sc.nextInt();
79 s[1] = sc.nextInt();
80
81 Team teams[] = new Team[n];
82 for( int i=0; i<n; i++ )
83 {
84 int k = sc.nextInt();
85 int d0 = sc.nextInt();
86 int d1 = sc.nextInt();
87 teams[i] = new Team( k, d0, d1 );
88 }
89
90
91
92
93 Arrays.sort( teams );
94
95
96 int total = 0;
97 for( Team team : teams )
98 {
99
100 int p = team.prefer;
101 int q = 1-p;
102 if( s[p] >= team.number )
103 {
104
105 total += team.number * team.distances[p];
106 s[p] -= team.number;
107 }
108 else
109 {
110
111 total += s[p]* team.distances[p];
112 team.number -= s[p];
113 s[p] = 0;
114
115
116 total += team.number * team.distances[q];
117 s[q] -= team.number;
118 }
119 }
120
121 ps.println( total );
122 System.out.println( total );
123 }
124 }
125
126
127
128
129
130
131
132 public static void main( String[] args ) throws Exception
133 {
134 new balloons().doit();
135 }
136
137 }