본문 바로가기
알고리즘 & 자료구조/백준

백준 11052

by 신재권 2022. 6. 29.
package baekjoon.DP;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main11052 {

   static int N;
   static int[] P, D;

   public static void main(String[] args) throws Exception {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      N = Integer.parseInt(br.readLine());
      P = new int[N + 1];
      D = new int[N + 1];
      StringTokenizer st = new StringTokenizer(br.readLine());
      for (int i = 1; i <= N; i++) {
         P[i] = Integer.parseInt(st.nextToken());
      }
      go();
      System.out.println(D[N]);
   }

   //D[N] = 카드 N개를 구매하는 비용의 최대값
   //D[N] = max(D[N-i] + P[i])
   // 1 <=i <=N
   // N x O(N) = O(N^2)

   static void go() {
      for (int i = 1; i <= N; i++) {
         for (int j = 1; j <= i; j++) {
            D[i] = Math.max(D[i], D[i - j] + P[j]);
         }
      }
   }

}

'알고리즘 & 자료구조 > 백준' 카테고리의 다른 글

백준 1715  (0) 2022.07.01
백준 1946  (0) 2022.06.30
백준 2470  (0) 2022.06.29
백준 7795  (0) 2022.06.29
백준 15663  (0) 2022.06.28