[BOJ 15486] ํ‡ด์‚ฌ2

 

1. ๋ฌธ์ œ : Link

์ตœ๋Œ€ ์ˆ˜์ต ๋น„๊ต

 

2. ํ’€์ด

Math.max(max, dp[i]) ํ•ด์ฃผ๋Š” ์ด์œ  -> ์ด์ „๊นŒ์ง€์˜ max๊ฐ’๊ณผ ์ง€๊ธˆ ํ˜„์žฌ dp ๊ฐ’ ์ค‘ ์ตœ๋Œ€๊ฐ’์„ ํ•ด๋‹น ์ง€์ ์— ๋”ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด

(

 

3. ์ฝ”๋“œ

import java.io.IOException;
import java.util.*;

public class Main {
    static int[] t;
    static int[] p;
    static int[] dp;

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        t = new int[n + 2];
        p = new int[n + 2];
        dp = new int[n + 2];

        for (int i = 1; i <= n; i++) {
            t[i] = sc.nextInt();
            p[i] = sc.nextInt();
        }

        int max = Integer.MIN_VALUE;
        for (int i = 1; i <= n + 1; i++) {
            max = Math.max(max, dp[i]);

            if (i + t[i] < n + 2 && (max + p[i] > dp[i + t[i]])) {
                dp[i + t[i]] = max + p[i];
            }
        }
        System.out.println(max);
    }
}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ