Submission #1175076

#TimeUsernameProblemLanguageResultExecution timeMemory
1175076trvhungKnapsack (NOI18_knapsack)C++20
100 / 100
48 ms1936 KiB
#include <bits/stdc++.h>
// #include <ext/rope>
// #include <ext/pb_ds/assoc_container.hpp>

// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
using namespace std;

// #define   ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define            ll long long
#define           ull unsigned long long
#define            ld long double
#define            pb push_back
#define  bit(mask, i) ((mask >> i) & 1)
#define            el '\n'
#define             F first
#define             S second

template <class X, class Y> bool maximize(X &x, const Y &y) { return (x < y ? x = y, 1 : 0); }
template <class X, class Y> bool minimize(X &x, const Y &y) { return (x > y ? x = y, 1 : 0); }

const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7;
const int MULTI = 0;
const ld eps = 1e-9;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; // R D L U
const int ddx[4] = {-1, 1, 1, -1}, ddy[4] = {1, 1, -1, -1}; // UR DR DL UL
const char cx[4] = {'R', 'D', 'L', 'U'};
const ll base = 31;
const int nMOD = 2;
const ll mods[] = {(ll)1e9 + 10777, (ll)1e9 + 19777, (ll)1e9 + 3, (ll)1e9 + 3777};

const int N = 1e5 + 5;
const int S = 2e3 + 5;
int n, s, dp[S];

struct item {
	int v, w, k;
	bool operator < (const item &other) const {
		if (w != other.w) return w < other.w;
		return v > other.v;
	}
} a[N];

vector<pair<int, int>> b;

void solve() {
	cin >> s >> n;
	for (int i = 1; i <= n; ++i)
		cin >> a[i].v >> a[i].w >> a[i].k;

	sort(a + 1, a + 1 + n);

	int it = 1;
	while (it <= n) {
		int j = it;
		while (j < n && a[j + 1].w == a[j].w) j++;

		int lim = s / a[it].w, cur = 0;
		for (int i = it; i <= j; ++i)
			if (cur + a[i].k <= lim) {
				cur += a[i].k;
				for (int t = 1; t <= a[i].k; ++t)
					b.push_back(make_pair(a[i].w, a[i].v));
			} else {
				for (int t = 1; t <= lim - cur; ++t)
					b.push_back(make_pair(a[i].w, a[i].v));
				break;
			}

		it = j + 1;
	}

	memset(dp, -1, sizeof(dp)); dp[0] = 0;

	for (int i = 0; i < (int) b.size(); ++i)
		for (int j = s; j >= 1; --j)
			if (j >= b[i].F && dp[j - b[i].F] >= 0)
				maximize(dp[j], dp[j - b[i].F] + b[i].S);

	cout << *max_element(dp, dp + 1 + s);
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    if (!MULTI) solve();
    else {
        int test; cin >> test;
        while (test--) solve();
    }
    
    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...