-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path879.cpp
More file actions
32 lines (26 loc) · 799 Bytes
/
Copy path879.cpp
File metadata and controls
32 lines (26 loc) · 799 Bytes
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
class Solution {
public:
int profitableSchemes(int G, int P, vector<int>& group, vector<int>& profit) {
const int MOD = 1e9 + 7;
vector<vector<int>> dp(P+1, vector<int>(G+1, 0));
for (size_t i = 0; i < group.size(); i++) {
int g = group[i], p = profit[i];
if (g > G) continue;
int gend = G - g;
auto bp = dp;
for (int m = 0; m <= P; m++) {
int pend = m + p;
if (pend > P) pend = P;
for (int n = 1; n <= gend; n++) {
if (dp[m][n]) {
bp[pend][n+g] = (bp[pend][n+g] + dp[m][n]) % MOD;
}
}
}
if (p > P) p = P;
bp[p][g]++;
dp = bp;
}
return accumulate(dp.back().begin(), dp.back().end(), 0ULL) % MOD;
}
};