https://www.acmicpc.net/problem/15486
dp에 대한 연습이 많이 필요하겠다...
일단 범위를 봐서는 완탐으로 풀면 시간초과가 날 것
매일의 이익을 dp 배열로 두고 풀어야 한다 !!
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[] T = new int[N+2];
int[] P = new int[N+2];
int[] dp = new int[N+2];
int max = Integer.MIN_VALUE;
StringTokenizer st = null;
for(int i=1;i<=N;i++){
st = new StringTokenizer(br.readLine());
T[i] = Integer.parseInt(st.nextToken());
P[i] = Integer.parseInt(st.nextToken());
}
for(int i=1;i<=N+1;i++){
max = Math.max(max, dp[i]);
int next = i+T[i];
if(next<N+2){
dp[next] = Math.max(max+P[i], dp[next]);
}
}
System.out.println(max);
}
}
'스터디 > Algorithm' 카테고리의 다른 글
[백준] 수들의 합2 2003 java - 투포인터 (1) | 2024.09.10 |
---|---|
[백준] ABCDE 13923 java - dfs (1) | 2024.08.21 |
[백준] 말이 되고픈 원숭이 1600 java - bfs (1) | 2024.06.20 |
[백준] 빙산 2573 java - bfs, dfs (2) | 2024.06.18 |
[백준] 직사각형 탈출 16973 java - bfs (2) | 2024.06.15 |