https://www.acmicpc.net/problem/14501
[JAVA]
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main_bj_14501_퇴사 {
static int N,ans=0;
static int[] T;//시간
static int[] P;//돈
public static void main(String[] args)throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
N=Integer.parseInt(br.readLine());
T=new int[N+1];
P=new int[N+1];
for(int i=1; i<=N; i++) {
st=new StringTokenizer(br.readLine()," ");
T[i]=Integer.parseInt(st.nextToken());
P[i]=Integer.parseInt(st.nextToken());
}//입력끝
for(int i=1; i<=N; i++) {
dfs(i,P[i],P[i]);
}
System.out.println(ans);
}
private static void dfs(int day, int sum, int preVal) {
if(day+T[day]>N+1) {
if(ans<sum-preVal)ans=sum-preVal;
return;
}
if(day+T[day]==N+1) {
if(ans<sum)ans=sum;
return;
}
int next=day+T[day];
for(int i=next; i<=N; i++) {
dfs(i,sum+P[i],P[i]);
}
}
}
[C++]
#include <iostream>
using namespace std;
int N, ans=0;
int time[16];
int pay[16];
void Dfs(int day, int nowSum, int add) {
if (day == N + 1) {
if (nowSum > ans) ans = nowSum;
return;
}
if (day > N+1) {
nowSum -= add;
if (nowSum > ans) ans = nowSum;
return;
}
for (int i = day + time[day]; i <= N +time[day]; i++) {
Dfs(i, nowSum + pay[day], pay[day]);
}
}
int main() {
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> time[i] >> pay[i];
}
for(int i=1; i<=N; i++)
Dfs(i,0,0);
cout <<ans;
return 0;
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준 2151] 거울 설치(JAVA) (0) | 2020.03.23 |
---|---|
[백준 8979] 올림픽(JAVA) (0) | 2020.03.21 |
[백준 1309] 동물원(JAVA) (0) | 2020.03.18 |
[백준 1543] 문서 검색(JAVA) (0) | 2020.03.17 |
[백준 2186] 문자판(JAVA) (0) | 2020.03.16 |
댓글