https://www.acmicpc.net/problem/14501
14501번: 퇴사
첫째 줄에 백준이가 얻을 수 있는 최대 이익을 출력한다.
www.acmicpc.net
package BOJ.simulation;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.function.Function;
public class BOJ_14501 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
Function<String,Integer> stoi = Integer::parseInt;
int n = stoi.apply(st.nextToken());
int[] period = new int[n];
int[] value = new int[n];
for(int i = 0 ; i < n ; i++){
st = new StringTokenizer(br.readLine());
period[i] = stoi.apply(st.nextToken());
value[i] = stoi.apply(st.nextToken());
}
int result = cal(0,period,value,n);
System.out.println(result);
}
private static int cal(int depth, int[] period, int[] value,int n) {
if(depth >= n){
return 0;
}
int result = 0;
if(depth + period[depth] <= n){
result = Math.max(cal(depth+period[depth],period,value,n)+value[depth],result);
}
result = Math.max(cal(depth+1,period,value,n),result);
return result;
}
}
'알고리즘' 카테고리의 다른 글
프로그래머스 택배 배달과 수거하기(JAVA) (0) | 2023.04.01 |
---|---|
프로그래머스 이모티콘 할인행사 (JAVA) (0) | 2023.03.31 |
프로그래머스 개인정보 수집 유효기간 (JAVA) (0) | 2023.03.29 |
프로그래머스 혼자서 하는 틱택토 (JAVA) (0) | 2023.03.28 |
프로그래머스 배열 자르기 (JAVA) (0) | 2023.03.27 |