This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int inf = INT_MAX, mod = 1e9 + 7, MAX_N = (1e5), MAX_S = 2000;
int dp[MAX_N + 1][MAX_S + 1];
void solve()
{
memset(dp, -1, sizeof(dp));
int S, N;
cin >> S >> N;
vector<int> V(N + 1), W(N + 1), K(N + 1);
map<int, vector<pair<int, int>>> mp;
for (int i = 1; i <= N; i++)
{
cin >> V[i] >> W[i] >> K[i];
mp[W[i]].push_back({V[i], K[i]});
}
int i = 1;
for (int i = 0; i <= S; i++)
dp[0][i] = 0;
for (auto &[wt, items] : mp)
{
sort(items.rbegin(), items.rend());
for (int j = 0; j <= S; j++)
{
dp[i][j] = dp[i - 1][j];
int curr_item = 0, cnt = 0, curr_item_cnt = 0, profit = 0;
while (curr_item < items.size() && j - ((cnt + 1) * wt) >= 0 && dp[i - 1][j - ((cnt + 1) * wt)] != -1)
{
cnt++;
profit += items[curr_item].first;
dp[i][j] = max(dp[i][j], dp[i - 1][j - (cnt * wt)] + profit);
if (curr_item_cnt + 1 == items[curr_item].second)
curr_item++, curr_item_cnt = 0;
else
curr_item_cnt++;
}
}
i++;
}
cout << *max_element(dp[i - 1], dp[i - 1] + S + 1) << "\n";
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << fixed;
cout << setprecision(9);
int tt = 1;
// cin >> tt;
for (int i = 1; i <= tt; i++)
{
// cout << "Case #" << i << ": ";
solve();
}
cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
}
Compilation message (stderr)
knapsack.cpp: In function 'void solve()':
knapsack.cpp:30:30: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | while (curr_item < items.size() && j - ((cnt + 1) * wt) >= 0 && dp[i - 1][j - ((cnt + 1) * wt)] != -1)
| ~~~~~~~~~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |