Submission #1150830

#TimeUsernameProblemLanguageResultExecution timeMemory
1150830pinbuKnapsack (NOI18_knapsack)C++20
100 / 100
90 ms8680 KiB
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define sz(v) (int)(v).size()
using namespace std;
 
const int MXn = 100005, MXs = 2e3 + 6;
const long long oo = 1e18;
 
int S, N;
vector<int> obj[MXs];
int dp[MXs];
 
signed main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    
    #define TASK "KNAPSACK"
    if (ifstream(TASK".INP")) {
    	freopen(TASK".INP", "r", stdin);
    	freopen(TASK".OUT", "w", stdout);
    }
    
    cin >> S >> N;
    for (int i = 1, v, w, k; i <= N; i++) {
    	cin >> v >> w >> k;
    	k = min(k, S / w);
    	int tmp = 0;
    	for (int t = 0; ; t++) {
    		if (tmp + (1 << t) <= k) {
    			tmp += (1 << t);
    			obj[w << t].emplace_back(v << t);
    		} else break;
    	}
    	if (k > tmp) obj[w * (k - tmp)].emplace_back(v * (k - tmp));
    }
    fill(dp + 1, dp + S + 1, -oo);
    dp[0] = 0;
    for (int w = 1; w <= S; w++) {
    	sort(obj[w].rbegin(), obj[w].rend());
    	for (int t = 0; t < min(S / w, sz(obj[w])); t++) {
    		for (int wei = S; wei >= w; wei--) {
    			dp[wei] = max(dp[wei], dp[wei - w] + obj[w][t]);
    		}
    	}
    }
    cout << *max_element(dp, dp + S + 1);
    
    return 0;
}
// Revive! >:)

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:21:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:22:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 |         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...