Submission #976126

#TimeUsernameProblemLanguageResultExecution timeMemory
976126vjudge1Knapsack (NOI18_knapsack)C++17
0 / 100
357 ms262144 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define endl "\n" const int N = 2e3 + 5; vector < pair < int, int > > a(1, {0, 0}), yes[N]; int n , s; signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> s >> n; for ( int i = 1; i <= n; i++){ int harga, berat, stok; cin >> harga >> berat >> stok; yes[berat].push_back({harga, stok}); } for ( int i = 1; i <= 2000; i++) sort ( yes[i].begin(), yes[i].end(), greater<pair < int, int >>()); for ( int i = 1; i <= 2000; i++){ int can = 2000; for ( int j = 0; j < yes[i].size(); j++){ for ( int k = 1; k <= yes[i][j].second; k++){ if ( can <= 0 ) break; a.push_back({i, yes[i][j].first}); // berat, harga } } } // 2e3 . log ( 2e3 ) vector < vector < int > > dp ( a.size() + 5, vector < int > ( s + 5, 0 )); for ( int i = 1; i < a.size(); i++){ for ( int j = 0; j <= s; j++){ dp[i][j] = dp[i-1][j]; if ( i >= a[j].first ){ dp[i][j] = max ( dp[i][j], dp[i - 1][j - a[i].first] + a[i].second); } } } cout << dp[a.size() - 1][s]; }

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:27:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |         for ( int j = 0; j < yes[i].size(); j++){
      |                          ~~^~~~~~~~~~~~~~~
knapsack.cpp:40:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |     for ( int i = 1; i < a.size(); 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...