이번에 풀어본 문제는
백준 14501번 퇴사 입니다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
class Counsel
{
int time,cost;
public Counsel(int time, int cost)
{
this.time = time;
this.cost = cost;
}
}
public class Main {
static Counsel [] counsel;
static int [] dp;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
counsel = new Counsel[N];
dp = new int[N+1];
StringTokenizer st;
for(int i = 0; i < N; ++i)
{
st = new StringTokenizer(br.readLine());
int time = Integer.parseInt(st.nextToken());
int cost = Integer.parseInt(st.nextToken());
counsel[i] = new Counsel(time,cost);
}
for(int i = 0; i < N; ++i)
{
Counsel cur = counsel[i];
int next = cur.time + i;
if(next <= N)
{
dp[next] = Math.max((dp[i] + cur.cost),dp[next]);
}
dp[i+1] = Math.max(dp[i+1],dp[i]);
}
Arrays.sort(dp);
System.out.print(dp[dp.length-1]);
}
}
dp문제입니다.
해당 일자 기준으로 소요되는 일자를 더해, 퇴사 이전에 해결이 가능하다면 dp에 값을 누적시켜줍니다. 하지만 이 경우만 생각했을 때, 하루를 지나치고 다음 선택을 하는 경우가 제외되기 때문에 dp배열의 다음 인덱스에 현재 값을 저장해두고, 다음 반복을 진행하도록 한줄을 추가 해주었습니다.
난이도는 낮았지만 지나치는 경우를 고려하지 못하고 문제를 풀어버려서 막판에 조금 헷갈렸던 문제입니다.