[Offer收割]编程练习赛4 register

Ended

Participants:899

Verdict:Accepted
Score:100 / 100
Submitted:2016-08-07 12:32:50

Lang:G++

Edit
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int main() {
    int n, x;
    cin >> n >> x;
    vector<int> a(n + 1, 0);
    int sum = 0;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        sum += a[i];
    }
    if (sum < x) cout << -1 << endl;
    else {
        for (int upper = x; upper <= sum; ++upper) {
            vector<vector<int>> dp(n + 1, vector<int>(upper + 1, 0));
            for (int i = 1; i <= n; ++i) {
                for (int j = 0; j <= upper; ++j) {
                    dp[i][j] = dp[i - 1][j];
                    if (j >= a[i]) dp[i][j] = max(dp[i][j], dp[i - 1][j - a[i]] + a[i]);
                }
            }
            if (dp[n][upper] >= x) {
                cout << upper << endl;
                break;
            }
        }
    }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX