제출 #652346

#제출 시각아이디문제언어결과실행 시간메모리
652346chanhchuong123Knapsack (NOI18_knapsack)C++14
17 / 100
1 ms340 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<long long> v;
vector<long long> w;

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;
    for (int j = 30; j >= 0; j--) if (K >> j & 1) {
      v.push_back((1LL << j) * V);
      w.push_back((1LL << j) * W);
      K -= (1 << j);
    }
    if (K) {
      v.push_back(1LL * K * V);
      w.push_back(1LL * K * W);
    }
  }
  n = v.size();
  for (int i = 0; i < n; i++) {
    for (int j = s; j >= 0; j--) {
      if (j + w[i] > s) continue;
      maxi(dp[j + w[i]], dp[j] + v[i]);
    }
  }
  cout << *max_element(dp + 1, dp + 1 + s);
}

컴파일 시 표준 에러 (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...