Submission #652358

#TimeUsernameProblemLanguageResultExecution timeMemory
652358chanhchuong123Knapsack (NOI18_knapsack)C++14
100 / 100
65 ms1812 KiB
#include <bits/stdc++.h>
using namespace std;
#define task "C"
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
  if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
  if (a < b) {a = b; return true;} return false;
}

const int S = 2020;
int s, n;
long long dp[S];
vector<pair<int, int>> a;
vector<pair<int, int>> occ[S];

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

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

  cin >> s >> n;
  for (int i = 1; i <= n; i++) {
    int V, W, K; cin >> V >> W >> K;
    occ[W].push_back({V, K});
  }
  for (int w = 1; w <= s; w++) {
    sort(all(occ[w]));
    int lim = s / w;
    while (lim > 0 && occ[w].size()) {
      if (!occ[w].back().se) occ[w].pop_back();
      else {
        occ[w].back().se--; lim--;
        a.push_back({occ[w].back().fi, w});
      }
    }
  }
  n = a.size();
  for (int i = 0; i < n; i++) {
    for (int j = s; j >= 0; j--) {
      if (j + a[i].se > s) continue;
      maxi(dp[j + a[i].se], dp[j] + a[i].fi);
    }
  }
  cout << dp[s];
}

Compilation message (stderr)

knapsack.cpp:22:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   22 | main() {
      | ^~~~
knapsack.cpp: In function 'int main()':
knapsack.cpp:27:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |     freopen(task".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:28:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |     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...