퇴사 2 15486

LJM·2023년 3월 22일
0

백준풀기

목록 보기
152/259

https://www.acmicpc.net/problem/15486

예외처리가 너무 복잡해져서 자꾸 틀렸는데 다른 풀이를 찾아보니
뒤에 하나더 붙여서 처리해야 코드가 간결해진다

이전에 풀었던 문제와 같은 방식으로 풀었다

import java.io.*;

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());//1 ≤ N ≤ 1,500,000

        String[] input;
        int[][] schedule = new int[N+2][2];
        long[] dp = new long[N+2];

        for (int i = 1; i < N+1; i++)
        {
            input = br.readLine().split(" ");

            schedule[i][0] = Integer.parseInt(input[0]);
            schedule[i][1] = Integer.parseInt(input[1]);
        }

        for (int today = N; today >= 1; today--)
        {
            int needD = schedule[today][0];

            if(today+needD-1 > N)
            {
                dp[today] = dp[today+1];
            }
            else
            {
                dp[today] = Math.max(dp[today+1], dp[today+needD]+schedule[today][1]);
            }
        }

        System.out.println(dp[1]);
    }
}
profile
게임개발자 백엔드개발자

0개의 댓글