TIOJ-1774

Ch3. Section 9. 妁艷的頭髮

https://tioj.ck.tp.edu.tw/problems/1774

Solution

裸背包,被值域嚇到XD
不過實際上魔力M只會到2000所以沒差的啦

AC code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <iostream>
using namespace std;
long long n,M,dp[2001];
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    cin >> n >> M;
    for(int i = 0,h,c; i < n; i++) {
        cin >> h >> c;
        for(int j = c; j <= M; j++) dp[j] = max(dp[j-c]+h, dp[j]);
    }
    cout << dp[M] << '\n';
}
comments powered by Disqus