설명
최대점수구하기
예시
5 20
10 5
25 12
15 8
6 3
7 4
41
풀이
맨 뒤부터 돌면서 해당숫자-time[i]인 곳에가서 내 score을 더한후 더 큰값을 배열에다가 넣어준다.
for(int i=0; i<n; i++) {
for(int j=m; j>=time[i]; j--) {
dis[j]=Math.max(dis[j], dis[j-time[i]]+score[i]);
answer=Math.max(answer, dis[j]);
}
}
import java.util.*;
class Main {
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
int n= sc.nextInt();
int m= sc.nextInt();
int[] score= new int[n];
int[] time= new int[n];
for(int i=0; i<n ; i++) {
int s= sc.nextInt();
int t= sc.nextInt();
score[i]=s;
time[i]=t;
}
int[] dis = new int[m+1];
Arrays.fill(dis, 0);
int answer=0;
for(int i=0; i<n; i++) {
for(int j=m; j>=time[i]; j--) {
dis[j]=Math.max(dis[j], dis[j-time[i]]+score[i]);
answer=Math.max(answer, dis[j]);
}
}
System.out.println(answer);
}
}
'알고리즘기초 > DP' 카테고리의 다른 글
| 05. 동전교환 (0) | 2022.10.03 |
|---|---|
| 04. 가장 높은 탑 쌓기 (0) | 2022.10.02 |
| 03. 최대 부분 증가수열 (0) | 2022.10.02 |
| 02. 돌다리 건너기 (0) | 2022.10.02 |
| 01. 계단오르기 (0) | 2022.10.02 |