Submission #823571

#TimeUsernameProblemLanguageResultExecution timeMemory
823571k5k5k5kKnapsack (NOI18_knapsack)C++14
100 / 100
652 ms18660 KiB
#include <bits/stdc++.h>
#define ll long long int
using namespace std;

/*mistake: changing values that would be accessed multiple times in their further computation*/

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0); 
  int S, N;
  cin >> S >> N;
  map<int, vector<pair<int, int>>> weights;
  for (int i = 0; i < N; i++) {
    int v, w, k;
    cin >> v >> w >> k;
    weights[w].push_back({v, k});
  }
  
  for (int i = 0; i <= S; i++) {
    sort(weights[i].begin(), weights[i].end(), greater<>());
  }
  /*
  15 5 
  4 12 1
  2 1 1 
  10 4 1 
  1 1 1 
  2 2 1
  */
  // instead of chcking for the first i indicies, we check for the first i
  // weights;
  int ans =0 ; 
  vector<vector<int>> dp(S+2, vector<int>(S+2, 0)); 
  for (int current_weight = 1; current_weight <= S; current_weight++) {
    for (int tot_w = 1; tot_w <= S; tot_w++) {
      dp[current_weight][tot_w] =
          max(dp[current_weight][tot_w - 1], dp[current_weight - 1][tot_w]);
      int weight = 0;
      int money = 0;
      int inside_map_index = 0;
      int used = 0; 
      while (weight + current_weight <= tot_w &&
             inside_map_index < weights[current_weight].size() && weights[current_weight][inside_map_index].second > used) {
        
        weight += current_weight;
        money += weights[current_weight][inside_map_index].first;
        dp[current_weight][tot_w] = max(dp[current_weight][tot_w],
                                        dp[current_weight - 1][tot_w - weight]+money);
        used++; 
        if (weights[current_weight][inside_map_index].second == used) {
          inside_map_index++;
          used =0; 
        }
      }
      ans = max(ans, dp[current_weight][tot_w]);
    }
  }
  cout << ans << endl; 
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:43:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |              inside_map_index < weights[current_weight].size() && weights[current_weight][inside_map_index].second > used) {
      |              ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...