forked from improper4/uva
/
UVa12324_PhilipJFryProblem.java
57 lines (48 loc) · 1.3 KB
/
UVa12324_PhilipJFryProblem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package uva;
/* USER: 46724 (sfmunera) */
/* PROBLEM: 3746 (12324 - Philip J. Fry Problem) */
/* SUBMISSION: 11573467 */
/* SUBMISSION TIME: 2013-04-08 01:30:18 */
/* LANGUAGE: 2 */
import java.util.*;
import java.io.*;
public class UVa12324_PhilipJFryProblem {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while (true) {
int N = Integer.parseInt(in.readLine());
if (N == 0) break;
int[] t = new int[N];
int[] b = new int[N];
for (int i = 0; i < N; ++i) {
StringTokenizer stk = new StringTokenizer(in.readLine());
t[i] = Integer.parseInt(stk.nextToken());
b[i] = Integer.parseInt(stk.nextToken());
}
boolean[] taken = new boolean[N];
for (int i = N - 1; i >= 0; --i) {
while (b[i] > 0) {
int max = 0;
int maxAt = -1;
for (int j = i + 1; j < N; ++j) {
if (taken[j]) continue;
if (t[j] > max) {
max = t[j];
maxAt = j;
}
}
if (maxAt >= 0) {
taken[maxAt] = true;
--b[i];
} else break;
}
}
int ans = 0;
for (int i = 0; i < N; ++i)
ans += taken[i] ? t[i] / 2 : t[i];
System.out.println(ans);
}
in.close();
System.exit(0);
}
}