https://www.acmicpc.net/problem/14501
14501번: 퇴사
첫째 줄에 백준이가 얻을 수 있는 최대 이익을 출력한다.
www.acmicpc.net
동적계획법으로 풀었다.
배낭 알고리즘을 응용해서 풀었다.
#include <stdio.h>
#include <algorithm>
using namespace std;
FILE* in = fopen("input.txt", "r");
#define MAX 15
int n;
int time[MAX];
int pay[MAX];
int cache[MAX+1][MAX * 1000];
int dp(int idx, int cost) {
//메모이제이션
if (cache[idx][cost] != 0)
return cache[idx][cost];
if (idx == n)
return cost;
if (idx + time[idx] <= n)
return cache[idx][cost] = max(dp(idx + time[idx], cost + pay[idx]), dp(idx + 1, cost));
else
return cache[idx][cost] = dp(idx + 1, cost);
}
int main() {
fscanf(in, "%d", &n);
for (int i = 0; i < n; i++)
fscanf(in, "%d %d", &time[i], &pay[i]);
printf("%d", dp(0, 0));
return 0;
}
'알고리즘 > 백준' 카테고리의 다른 글
백준 1633번 최고의 팀 만들기 C/C++ 풀이 (0) | 2020.08.16 |
---|---|
백준 14502번 연구소 C/C++ 풀이 (0) | 2020.08.15 |
백준 13460번 구슬 탈출 2 C/C++ 풀이 (0) | 2020.08.13 |
백준 14888번 연산자 끼워넣기 C/C++ 풀이 (0) | 2020.08.09 |
백준 15686번 치킨 배달 C/C++ 풀이 (0) | 2020.08.08 |