Submission #1108027

#TimeUsernameProblemLanguageResultExecution timeMemory
1108027vjudge1Knapsack (NOI18_knapsack)C++17
100 / 100
43 ms3920 KiB
#include<bits/stdc++.h>
using namespace std;

const bool multiTest = false;
#define task "C"
#define fi first
#define se second
#define MASK(i) (1LL << (i))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define BIT(mask, i) ((mask) >> (i) & 1)

template<typename T1, typename T2> bool minimize(T1 &a, T2 b) {
	if (a > b) a = b; else return 0; return 1;
}
template<typename T1, typename T2> bool maximize(T1 &a, T2 b) {
	if (a < b) a = b; else return 0; return 1;
}

const int MAX = 2020;
int S, N;
long long dp[MAX];
vector<pair<int, int>> A;
vector<pair<int, int>> B[MAX];

void process(void) {
	cin >> S >> N;
	for (int i = 1; i <= N; ++i) {
		int v, w, k; 
		cin >> v >> w >> k;
		B[w].emplace_back(v, k);
	}
	for (int i = 1; i <= S; ++i) if (B[i].size()) {
		sort(all(B[i]));
		int maxSize = S / i;
		while (maxSize > 0 && B[i].size()) {
			A.emplace_back(i, B[i].back().fi); maxSize -= 1;
			if (--B[i].back().se == 0) B[i].pop_back(); 
		}
	}
	memset(dp, -0x3f, sizeof dp);
	dp[0] = 0;
	for (pair<int, int> &p: A) {
		int w = p.fi, v = p.se;
		for (int j = S; j >= w; --j) {
			maximize(dp[j], dp[j - w] + v);
		}
	}
	cout << *max_element(dp, dp + 1 + S);
}

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	if (fopen(task".inp", "r")) {
		freopen(task".inp", "r",  stdin);
		freopen(task".out", "w", stdout);
	}

	int nTest = 1; if (multiTest) cin >> nTest;
	while (nTest--) {
		process();
	}

	return 0;
}

Compilation message (stderr)

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