Submission #584758

#TimeUsernameProblemLanguageResultExecution timeMemory
584758AkramElOmraniKnapsack (NOI18_knapsack)C++17
12 / 100
33 ms63188 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define ios ios_base::sync_with_stdio(0);cin.tie(0);

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
 
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int gen(int a, int b) {return (ll)rng() % (b - a + 1) + a;}

#define int ll

#define f first
#define s second

signed main()
{
	ios
	int n, s; cin >> s >> n;
	vector<vector<pair<int, int>>> all(s + 1);
	for(int i = 0, a, b, c; i < n; ++i) {
		cin >> a >> b >> c;
		if(b < 0 || b > s) continue;
		all[b].push_back(make_pair(a, c));
	}
	auto max_self = [&](pair<int, int>& a, pair<int, int> b) {
		if(a.f < b.f) a = b;
		if(a.f == b.f && a.s > b.s) {
			a = b;
		}
	};

	vector<vector<int>> a(s + 1);
	for(int i = 1; i <= s; ++i) {
		sort(all[i].rbegin(), all[i].rend());
		int cur = 0, added = 0;
		while(cur < int(all[i].size()) && int(a[i].size()) < s) {
			while(all[i][cur].s > 0 && int(a[i].size()) < s) {
				a[i].push_back(all[i][cur].f);
				--all[i][cur].s;
			}
			++cur;
		}
	}
	vector<vector<pair<int, int>>> dp(s + 1, vector<pair<int, int>>(s + 1));
	// [profit, cur_index]
	int mx = 0;
	for(int i = 1; i <= s; ++i) {
		for(int j = 0; j <= s; ++j) {
			if(dp[i - 1][j].f > dp[i][j].f) {
				dp[i][j] = {dp[i - 1][j].f, 0};
			}
			if(i + j <= s && dp[i][j].s < a[i].size()) {
				max_self(dp[i][j + i], {dp[i][j].f + a[i][dp[i][j].s], dp[i][j].s + 1});
			}
			mx = max(mx, dp[i][j].f);
		}
	}
	cout << mx << '\n';
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:41:16: warning: unused variable 'added' [-Wunused-variable]
   41 |   int cur = 0, added = 0;
      |                ^~~~~
knapsack.cpp:58:32: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |    if(i + j <= s && dp[i][j].s < a[i].size()) {
#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...