-
Notifications
You must be signed in to change notification settings - Fork 0
/
Doom.java
90 lines (82 loc) · 2.34 KB
/
Doom.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
/*
br = new BufferedReader(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
*/
public class Doom {
private static BufferedReader br;
private static StringTokenizer st;
private static PrintWriter pw;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
//int qq = 1;
//int qq = Integer.MAX_VALUE;
int qq = readInt();
for(int casenum = 1; casenum <= qq; casenum++) {
int n = readInt();
long[] dp = new long[101];
Arrays.fill(dp, Integer.MAX_VALUE);
dp[100] = 0;
while(n-- > 0) {
long[] next = new long[101];
Arrays.fill(next, Integer.MAX_VALUE);
int k = readInt();
while(k-- > 0) {
int time = readInt();
int add = -readInt();
for(int i = 0; i < dp.length; i++) {
if(i+add <= 0) continue;
next[Math.min(100, i+add)] = Math.min(next[Math.min(100, i+add)], dp[i] + time);
}
}
dp = next;
}
long ret = Integer.MAX_VALUE;
for(long out: dp) {
ret = Math.min(ret, out);
}
String ans = ret == Integer.MAX_VALUE ? "IMPOSSIBLE" : Long.toString(ret);
pw.println("Case " + casenum + ": " + ans);
}
exitImmediately();
}
private static void exitImmediately() {
pw.close();
System.exit(0);
}
private static long readLong() throws IOException {
return Long.parseLong(nextToken());
}
private static double readDouble() throws IOException {
return Double.parseDouble(nextToken());
}
private static int readInt() throws IOException {
return Integer.parseInt(nextToken());
}
private static String nextLine() throws IOException {
if(!br.ready()) {
exitImmediately();
}
st = null;
return br.readLine();
}
private static String nextToken() throws IOException {
while(st == null || !st.hasMoreTokens()) {
if(!br.ready()) {
exitImmediately();
}
st = new StringTokenizer(br.readLine().trim());
}
return st.nextToken();
}
}