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>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long s, n;
cin >> s >> n;
vector<vector<pair<long long, long long>>> v(s + 1);
vector<pair<long long, long long>> items;
long long nilai, berat, banyak;
for (int i = 0; i < n; i++)
{
cin >> nilai >> berat >> banyak;
v[berat].push_back({nilai, banyak});
}
for (int i = 1; i <= s; i++)
{
sort(v[i].begin(), v[i].end(), [](pair<long long, long long>& a, pair<long long, long long>& b)
{
return (a.first == b.first ? (a.second > b.second) : a.first > b.first);
});
int ambil = 0, pos = 0;
while (pos < v[i].size() and ambil < s / i)
{
for (int j = 0; j < min(v[i][pos].second, (s / i - ambil)); j++)
items.push_back({v[i][pos].first, i});
ambil += min(v[i][pos].second, (s / i - ambil));
pos++;
}
}
vector<long long> dp(s + 1, 0);
for (auto i : items)
{
vector<long long> temp = dp;
for (int j = i.second; j <= s; j++)
temp[j] = max(temp[j], dp[j - i.second] + i.first);
dp = temp;
}
cout << dp[s] << '\n';
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:29:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | while (pos < v[i].size() and ambil < s / i)
| ~~~~^~~~~~~~~~~~~
# | 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... |