Algorithm/백준
[백준] 16194번 카드 구매하기 2
sirius
2021. 3. 5. 09:35
https://www.acmicpc.net/problem/16194
16194번: 카드 구매하기 2
첫째 줄에 민규가 구매하려고 하는 카드의 개수 N이 주어진다. (1 ≤ N ≤ 1,000) 둘째 줄에는 Pi가 P1부터 PN까지 순서대로 주어진다. (1 ≤ Pi ≤ 10,000)
www.acmicpc.net
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[] arr = new int[N + 1];
int[] dp = new int[N + 1];
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i = 1; i <= N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
dp[i] = Integer.MAX_VALUE;
}
for(int i = 1; i <= N ; i++) {
for(int j = 1; j <= i; j++) {
dp[i] = Math.min(dp[i], dp[i-j] + arr[j]);
}
}
System.out.println(dp[N]);
}
}