soj 2111: littleken bg

@(K ACMer)

水得一发的01背包.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <string>
#include <queue>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
const int mod = 1e9 + 7, INF = 0x3fffffff, maxn = 1e5 + 40;
int dp[209], m, n;

int main(void) {
    while (~scanf("%d%d", &m, &n)) {
        fill(dp, dp + m + 1, INF);
        dp[0] = 0;
        for (int i = 0; i < n; i++) {
            int x;
            scanf("%d", &x);
            for (int j = m; j >= x; j--) {
                dp[j] = min(dp[j], dp[j - x] + x);
            }
        }
        for (int i = m; i >= 0; i--) {
            if (dp[i] != INF) {
                printf("%d\n", i);
                break;
            }
        }
    }
    return 0;
}

你可能感兴趣的:(soj 2111: littleken bg)