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
| #include <algorithm> #include <vector>
class Solution { public: int maxTotalReward(std::vector<int>& rewardValues) { std::sort(rewardValues.begin(), rewardValues.end());
int m = rewardValues.back();
std::vector<int> dp(2 * m, 0); dp[0] = 1;
for (int x : rewardValues) { for (int k = 2 * x - 1; k >= x; k--) { if (dp[k - x] == 1) { dp[k] = 1; } } }
int res = 0; for (int i = dp.size() - 1; i >= 0; i--) { if (dp[i] == 1) { res = i; break; } } return res; } };
|