제출 #735909

#제출 시각아이디문제언어결과실행 시간메모리
735909asdfgraceKnapsack (NOI18_knapsack)C++17
100 / 100
81 ms4940 KiB
#include <bits/stdc++.h>
using namespace std;
#define DEBUG(x) //x
#define A(x) DEBUG(assert(x))
#define PRINT(x) DEBUG(cerr << x)
#define PV(x) DEBUG(cerr << #x << " = " << x << '\n')
#define PV2(x) DEBUG(cerr << #x << " = " << x.first << ',' << x.second << '\n')
#define PAR(x) DEBUG(PRINT(#x << " = { "); for (auto y : x) PRINT(y << ' '); PRINT("}\n");)
#define PAR2(x) DEBUG(PRINT(#x << " = { "); for (auto [y, z] : x) PRINT(y << ',' << z << "  "); PRINT("}\n");)
typedef int64_t i64;
const int INF = 1000000007; //998244353;


struct S {
  int s, n;
  vector<vector<pair<i64, i64>>> a;

  void run() {
    cin >> s >> n;
    a.resize(s + 1);
    for (int i = 0; i < n; ++i) {
      i64 v, w, k;
      cin >> v >> w >> k;
      a[w].push_back({v, k});
    }
    
    vector<i64> cur(s + 1, -1), prev(s + 1, -1);
    cur[0] = 0; prev[0] = 0;
    for (int i = 0; i <= s; ++i) {
      if (a[i].empty()) continue;
      sort(a[i].begin(), a[i].end());
      reverse(a[i].begin(), a[i].end());
      for (int j = 0; j <= s; ++j) {
        PV(j);
        cur[j] = prev[j];
        i64 used = 0LL, elem = 0LL, last = 0LL, val = 0LL;
        while (elem < a[i].size() && (used + 1) * i <= j) {
          ++used;
          val += a[i][elem].first;
          if (prev[j - i * used] != -1 && i * used <= j) {
            cur[j] = max(cur[j], prev[j - i * used] + val);
          }
          ++last;
          if (last == a[i][elem].second) {
            ++elem;
            last = 0;
          }
        }
      }
      PAR(cur);
      prev = cur;
    }
    PAR(cur);
    
    
    i64 ans = 0;
    for (int i = 0; i <= s; ++i) {
      ans = max(ans, cur[i]);
    }
    cout << ans << '\n';
  }
};

int main() {
  ios::sync_with_stdio(0); cin.tie(0);
  auto sol = make_unique<S>();
  sol->run();
}


컴파일 시 표준 에러 (stderr) 메시지

knapsack.cpp: In member function 'void S::run()':
knapsack.cpp:37:21: warning: comparison of integer expressions of different signedness: 'i64' {aka 'long int'} and 'std::vector<std::pair<long int, long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |         while (elem < a[i].size() && (used + 1) * i <= j) {
      |                ~~~~~^~~~~~~~~~~~~
#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...