Submission #779544

#TimeUsernameProblemLanguageResultExecution timeMemory
779544mydeKnapsack (NOI18_knapsack)C++17
100 / 100
60 ms5068 KiB
#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)); } } 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...