-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckout Assistant
More file actions
39 lines (35 loc) · 1.23 KB
/
Checkout Assistant
File metadata and controls
39 lines (35 loc) · 1.23 KB
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
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine().trim());
int[] t = new int[n];
long[] c = new long[n];
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
t[i] = Integer.parseInt(st.nextToken());
c[i] = Long.parseLong(st.nextToken());
}
// weight = t[i] + 1, value = c[i]
// we need total weight >= n, minimize total value
int maxWeight = n + 2000; // enough upper bound
long INF = Long.MAX_VALUE / 2;
long[] dp = new long[maxWeight + 1];
Arrays.fill(dp, INF);
dp[0] = 0;
for (int i = 0; i < n; i++) {
int w = t[i] + 1;
for (int j = maxWeight; j >= w; j--) {
if (dp[j - w] + c[i] < dp[j]) {
dp[j] = dp[j - w] + c[i];
}
}
}
long ans = INF;
for (int j = n; j <= maxWeight; j++) {
ans = Math.min(ans, dp[j]);
}
System.out.println(ans);
}
}