File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed
Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ ```
2+ import java.io.*;
3+ import java.util.*;
4+
5+ public class Main {
6+ private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+ private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+ private static int[] dp;
9+ private static int[][] cites;
10+ private static int N, C;
11+
12+ public static void main(String[] args) throws IOException {
13+ init();
14+
15+ bw.write(dp[N] + "\n");
16+ bw.flush();
17+ bw.close();
18+ br.close();
19+ }
20+
21+ private static void init() throws IOException {
22+ StringTokenizer st = new StringTokenizer(br.readLine());
23+ N = Integer.parseInt(st.nextToken());
24+ C = Integer.parseInt(st.nextToken());
25+
26+ cites = new int[C][2];
27+ dp = new int[N+1];
28+
29+ for (int i = 0; i < C; i++) {
30+ st = new StringTokenizer(br.readLine());
31+ cites[i][0] = Integer.parseInt(st.nextToken());
32+ cites[i][1] = Integer.parseInt(st.nextToken());
33+ }
34+
35+ Arrays.fill(dp, Integer.MAX_VALUE);
36+ dp[0] = 0;
37+
38+ for (int i = 0; i < C; i++) {
39+ for (int j = 1; j < N; j++) {
40+ if (j - cites[i][1] >= 0 && dp[j - cites[i][1]] != Integer.MAX_VALUE) {
41+ dp[j] = Math.min(dp[j], dp[j - cites[i][1]] + cites[i][0]);
42+ }
43+ }
44+
45+ for (int j = N - cites[i][1]; j <= N; j++) {
46+ if (j >= 0 && dp[j] != Integer.MAX_VALUE) dp[N] = Math.min(dp[N], dp[j] + cites[i][0]);
47+ }
48+ }
49+ }
50+ }
51+ ```
You can’t perform that action at this time.
0 commit comments