Submission #502046

#TimeUsernameProblemLanguageResultExecution timeMemory
502046pawnedKnapsack (NOI18_knapsack)C++17
49 / 100
102 ms262148 KiB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;

struct itemtype {
	ll weight, cost, number;
};

bool cmp(const itemtype& x, const itemtype& y) {
	if (x.weight != y.weight)
		return x.weight < y.weight;
	if (x.cost != y.cost)
		return x.cost > y.cost;
	if (x.number != y.number)
		return x.number < y.number;
	return true;
}

int main() {
	ll S, N;	// maxweight, itemcount
	cin>>S>>N;
	itemtype types[N];	// weight, cost, number
	for (int i = 0; i < N; i++) {
		cin>>types[i].cost>>types[i].weight>>types[i].number;
	}
	sort(types, types + N, cmp);
	vector<pair<ll, ll>> items;	// {weight, cost}
	items.pb({-1, -1});
	ll currweight = -1;
	ll currneeded = -1;
	for (int i = 0; i < N; i++) {
		if (types[i].weight != currweight || currneeded <= 0) {
			currweight = types[i].weight;
			currneeded = S / currweight;
		}
		while (types[i].number > 0 && currneeded > 0) {
			items.pb({types[i].weight, types[i].cost});
			types[i].number--;
			currneeded--;
		}	}
/*
	for (int i = 0; i < items.size(); i++) {
		cout<<"item "<<i<<": "<<items[i].first<<", "<<items[i].second<<endl;
	}
*/
	ll dp[(int)(items.size()) + 5][S + 5];
	for (int i = 0; i < S + 5; i++) {
		dp[0][i] = 0;
	}
	for (int i = 1; i < (int)(items.size()) + 5; i++) {
		dp[i][0] = 0;
		for (int j = 1; j < S + 5; j++) {
			dp[i][j] = -1e18;
		}
	}
	ll maximum = 0;
	for (int i = 1; i < (int)(items.size()); i++) {
		for (ll j = 1; j <= S; j++) {
			dp[i][j] = dp[i - 1][j];
			if (j >= items[i].first)
				dp[i][j] = max(dp[i][j], dp[i - 1][j - items[i].first] + items[i].second);
//			cout<<i<<" "<<j<<": "<<dp[i][j]<<endl;
			maximum = max(maximum, dp[i][j]);
		}
	}
	cout<<maximum<<endl;
}
#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...