Submission #886373

#TimeUsernameProblemLanguageResultExecution timeMemory
886373Zero_OPKnapsack (NOI18_knapsack)C++14
73 / 100
1029 ms19140 KiB
#include<bits/stdc++.h>

using namespace std;

#define range(v) begin(v), end(v)
#define compact(v) v.erase(unique(range(v)), end(v))

template<class T> bool minimize(T& a, T b){
  if(a > b) return a = b, true;
  return false;
}

template<class T> bool maximize(T& a, T b){
  if(a < b) return a = b, true;
  return false;
}

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

int S, n;

void Zero_OP(){
  cin >> S >> n;

  vector<pair<ll, int>> items;
  while(n--){
    int v, w, k;
    cin >> v >> w >> k;

    int pw2 = 1;
    while(k >= pw2){
      k -= pw2;
      if(1LL * w * pw2 <= 1LL * S){
        items.push_back({1LL * v * pw2, w * pw2});
      }
      pw2 <<= 1;
    }
    if(k > 0 and 1LL * w * k <= S){
      items.push_back({1LL * v * k, w * k});
    }
  }

  vector<ll> dp(S + 1, -1e18);
  dp[0] = 0;
  for(auto [earn, weight] : items){
    for(int i = S; i >= weight; --i){
      maximize(dp[i], dp[i - weight] + earn);
    }
  }

  cout << *max_element(range(dp));
}

int main(){
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  #define task "antuvu"
  if(fopen(task".inp", "r")){
    freopen(task".inp", "r", stdin);
    freopen(task".out", "w", stdout);
  }

  Zero_OP();

  return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'void Zero_OP()':
knapsack.cpp:47:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   47 |   for(auto [earn, weight] : items){
      |            ^
knapsack.cpp: In function 'int main()':
knapsack.cpp:62:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |     freopen(task".inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:63:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |     freopen(task".out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...