File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ static int c, n;
7+ static int [] dp;
8+
9+ public static void main (String [] args ) throws Exception {
10+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
11+ StringTokenizer st = new StringTokenizer (br. readLine());
12+
13+ c = Integer . parseInt(st. nextToken());
14+ n = Integer . parseInt(st. nextToken());
15+
16+ int max = c + 100 ;
17+ dp = new int [max + 1 ];
18+
19+ Arrays . fill(dp, Integer . MAX_VALUE );
20+ dp[0 ] = 0 ;
21+
22+ for (int i = 0 ; i < n; i++ ) {
23+ st = new StringTokenizer (br. readLine());
24+ int cost = Integer . parseInt(st. nextToken());
25+ int gain = Integer . parseInt(st. nextToken());
26+
27+ for (int j = gain; j <= max; j++ ) {
28+ if (dp[j - gain] == Integer . MAX_VALUE ) {
29+ continue ;
30+ }
31+ dp[j] = Math . min(dp[j], dp[j - gain] + cost);
32+ }
33+ }
34+
35+ int ans = Integer . MAX_VALUE ;
36+ for (int i = c; i <= max; i++ ) {
37+ ans = Math . min(ans, dp[i]);
38+ }
39+
40+ System . out. println(ans);
41+ }
42+ }
43+ ```
You can’t perform that action at this time.
0 commit comments