Submission #580433

#TimeUsernameProblemLanguageResultExecution timeMemory
580433vbeeKnapsack (NOI18_knapsack)C++17
100 / 100
96 ms36388 KiB
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define ii pair<int,int>
#define vii vector<ii>
#define vi vector<int>
#define fi first
#define se second
#define TASK ""
#define ll long long
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpll vector<pll>
#define pb push_back
#define MASK(i) (1 << (i))
#define BIT(x, i) ((x >> (i)) & 1)
 
using namespace std;

const int oo = 1e9 + 7;
const ll loo = (ll)1e18 + 7; 
const int N = 1e5 + 7;
const int NN = 2007;

ll s, n;
ll f[NN][NN];
map<ll, vector<pair<ll, ll> > > M;
void maximize(ll &a, ll b){
	if (a < b) a = b;
}
int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	//freopen(TASK".inp", "r", stdin);
	//freopen(TASK".out", "w", stdout);
	cin >> s >> n;
	for (int i = 1; i <= n; i++){
		ll v, w, k; cin >> v >> w >> k;
		M[w].pb({v, k});
	}
	for (int i = 0; i <= (int)M.size(); i++) for (int j = 0; j <= s; j++) f[i][j] = -1;
	f[0][0] = 0;
	int i = 1;
	for (auto& u : M){
		ll w = u.fi;
		vector<pair<ll, ll> > items = u.se;
		sort(all(items), greater<pair<ll, ll> >());
		for (int j = 0; j <= s; j++){
			f[i][j] = f[i - 1][j];
			int id = 0;
			int numtake = 0;
			int copy = 0;
			ll value = 0;

			while ((copy + 1) * w <= j && id < (int)items.size()){
				copy++;
				value += items[id].fi;
				if (f[i - 1][j - copy * w] != -1){
					maximize(f[i][j], f[i - 1][j - copy * w] + value);
				}

				numtake++;
				if (numtake == items[id].se){
					numtake = 0;
					id++;
				}
			}
		}
		i++;
	}
	ll res = 0;
	for (int i = 0; i <= s; i++) maximize(res, f[M.size()][i]);
	cout << res;
	return 0;
}
#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...