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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<functional>
#include<cstdbool>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define INF 987654321
#define LINF 321321321321
#define MAX 101
 
int n, m;
int inv[21][301];
int cache[21][301];
int invested[21][301];
int dfs(int index, ll curMoney) {
 
    if (index > m)
        return 0;
 
    int &ret = cache[index][curMoney];
 
    if (ret != -1)
        return ret;
 
    ret = 0;
    for (int i = 0; i <= curMoney; i++) {
        int iM = dfs(index + 1, curMoney - i) + inv[index][i];
        if (ret < iM) {
            ret = iM;
            invested[index][curMoney] = i;
        }
    }
 
    return ret;
}
 
void howmuch() {
    int curMoney = n;
    for (int i = 1; i <= m; i++) {
        cout << invested[i][curMoney] << " ";
        curMoney -= invested[i][curMoney];
    }
    cout << endl;
}
 
 
 
int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
 
    for (int i = 0; i < n; i++) {
        int money, val;
        cin >> money;
        for (int j = 1; j <= m; j++) {
            cin >> val;
            inv[j][money] = val;
        }
    }
 
    memset(cache, -1sizeof(cache));
 
    cout << dfs(1, n) << "\n";
    howmuch();
    return 0;
}
cs

출처:https://www.acmicpc.net/problem/2662

+ Recent posts