File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed
Expand file tree Collapse file tree 1 file changed +54
-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+
7+ static int cnt,ans;
8+ static int[] cost;
9+ static int[] money;
10+ static int[] dp;
11+
12+ public static void main(String[] args) throws Exception {
13+ init();
14+ process();
15+ print();
16+
17+ }
18+
19+ public static void init() throws IOException {
20+ BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
21+ cnt = Integer.parseInt(br.readLine());
22+ cost = new int[cnt+1];
23+ money = new int[cnt+1];
24+ dp = new int[cnt+51];
25+ ans = 0;
26+ for (int i = 1; i <= cnt; i++) {
27+ StringTokenizer st = new StringTokenizer(br.readLine());
28+ cost[i] = Integer.parseInt(st.nextToken());
29+ money[i] = Integer.parseInt(st.nextToken());
30+ }
31+ }
32+
33+ public static void process(){
34+ for (int i = 1; i <= cnt; i++) {
35+ dp[i] = Math.max(dp[i],dp[i-1]);
36+ if (i + cost[i]-1 > cnt) {
37+ continue;
38+ }
39+
40+ dp[i + cost[i]-1] = Math.max (dp[i+cost[i]-1] , dp[i-1] + money[i]);
41+ }
42+
43+ for (int i = 1; i<= cnt; i++) {
44+ System.out.println(dp[i]);
45+ ans = Math.max(ans, dp[i]);
46+ }
47+ }
48+
49+ public static void print(){
50+ System.out.println(ans);
51+ }
52+
53+ ```
54+ }
You can’t perform that action at this time.
0 commit comments