이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define nl '\n'
template <typename T>
inline bool ckmax(T& x, const T& y) {
  return x < y ? x = y, 1 : 0;
}
signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  
  int S, N;
  std::cin >> S >> N;
  
  std::vector<std::map<int, int>> cnt(S + 1);
  for (int i = 0; i < N; i++) {
    int v, w, k;
    std::cin >> v >> w >> k;
    k = std::min(k, S / w);
    if (w <= S) {
      cnt[w][v] += k;
    }
  }
  std::vector<int> dp(S + 1);
  
  for (int i = 1; i <= S; i++) {
    int take_max = S / i;
    int sum = 0;
    for (auto& [v, k] : cnt[i]) {
      if (k >= 3) {
        int q = k - 1;
        k = 1 + (q & 1);
        if (2 * i <= S) {
          cnt[2 * i][2 * v] += q >> 1;
        }
      }
      sum += k;
    }
    for (auto [v, k] : cnt[i]) {
      if (sum <= take_max) {
        while (k--) {
          for (int j = S; j >= i; j--) {
            ckmax(dp[j], dp[j - i] + v);
          }
        }
      }
      sum -= k;
    }
  }
  std::cout << dp[S] << nl;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |