solution.javascript
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
import java.io.*;
import java.util.*;

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());
        int[][] consults = new int[N + 1][2];
        for (int i = 1; i <= N; i++) {
        	StringTokenizer st = new StringTokenizer(br.readLine());
        	consults[i][0] = Integer.parseInt(st.nextToken());
        	consults[i][1] = Integer.parseInt(st.nextToken());
        }
        
        int[] dp = new int[N + 2];
        dp[N] = consults[N][1];
        
        for (int i = N; i >= 1; i--) {
        	if (i + consults[i][0] <= N + 1) {
            	int yesPrev = dp[i + consults[i][0]];
            	int cur = consults[i][1];
            	
            	int noPrev = dp[i + 1];
            	dp[i] = Math.max(noPrev, yesPrev + cur);
        	} else {
        		dp[i] = dp[i + 1];
        	}
        }
        System.out.println(dp[1]);
    }
}