본문 바로가기
알고리즘/DP

01knapsack 더블릿 (복습 필요)

by tryotto 2019. 2. 8.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <algorithm>
 
using namespace std;
 
int profit[300= { 0 };
int weight[300= { 0 };
int ans[300][11000= { 0 };
 
int main() {
    int w, n;
    scanf("%d %d"&w, &n);
    getchar();
 
    for (int i = 1; i <= n; i++) {
        scanf("%d %d",&weight[i],&profit[i]);
    }
 
    for (int i = weight[1]; i <= w; i++) {
        ans[1][i] = profit[1];    
    }
    
    for (int i = 2; i <= n; i++) {
        for (int j = 1; j <= w; j++) {
            if (j < weight[i]) {
                ans[i][j] = ans[i-1][j];                
            }
            else {
                ans[i][j] = max(ans[i - 1][j], 
                    profit[i] + ans[i - 1][j - weight[i]]);                
            }                    
        }                
    }
 
    printf("%d", ans[n][w]);
}
cs


'알고리즘 > DP' 카테고리의 다른 글

1463 1로 만들기  (0) 2019.02.08
2600 구슬게임  (0) 2019.02.08
sumset 더블릿  (0) 2019.02.08
분할수 더블릿  (0) 2019.02.08
1932 정수삼각형 (숫자 삼각형 더블릿)  (0) 2019.02.07