Submission #832817

#TimeUsernameProblemLanguageResultExecution timeMemory
832817avnithvKnapsack (NOI18_knapsack)C++14
100 / 100
54 ms5028 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define pb push_back
#define f first
#define s second
#define endl "\n"
#define mp make_pair
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define TC int tc; cin >> tc; while (tc--) solve();
#define FORN(i, a, b) for(int i = a; i < b; i++)
#define REV(i, b, a) for(int i = b - 1; i >= a; i--)
#define EACH(x, a) for(auto &x : a)
#define DBG(z) FORN(i,0,sz(z))cerr<<z[i]<<" \n"[i==sz(z)-1];
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define rall(a) (a).rbegin(), (a).rend()
#define log(x) (31-__builtin_clz(x))
#define logll(x) (63-__builtin_clzll(x))
#define tcT template<class T
#define tcTU tcT, class U
#define lb lower_bound
#define ub upper_bound

mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
#define RAND(a, b) uniform_int_distribution<ll>(a, b)(rng)

// only in emergencies
// #define int ll

using ll = long long;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using ppl = pair<ll, pl>;
tcT> using vt = vector<T>;

const ll RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
tcT> struct chash { 
	const uint64_t C = ll(4e18*acos(0))+71; 
	ll operator()(T x) const { return __builtin_bswap64((((ll)x)^RANDOM)*C); }
};

tcTU> using gp = gp_hash_table<T, U, chash<T>>;
tcT> using pq = priority_queue<T>;
tcT> using qu = queue<T>;
tcT> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
tcT> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
tcTU> using ordered_map = tree<T, U, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

tcT> inline void chmin(T &a, T b) {a = min(a, b);}
tcT> inline void chmax(T &a, T b) {a = max(a, b);}

const ll MOD = 998244353, MXN = 1e5+5, INF = 1e18;

void solve() {
	ll S, n; cin >> S >> n;
	vt<vt<pl>> obs(S+1);
	FORN(i,0,n) {
		ll v, w, k; cin >> v >> w >> k;
		obs[w].pb(mp(v, k));
	}
	vt<pl> items;
	FORN(i,1,S+1) {
		if (!sz(obs[i])) continue;
		sort(rall(obs[i]));
		ll mx = S / i;
		ll j = 0;
		while (mx > 0 && j < sz(obs[i])) {
			items.pb(mp(i, obs[i][j].f)); mx--;
			if (--obs[i][j].s == 0) j++;
		}
	}
	ll m = sz(items);
	vt<ll> dp(S+1, -INF);
	dp[0] = 0;
	FORN(i,0,m) {
		REV(j,S+1,items[i].f) {
			chmax(dp[j], dp[j-items[i].f]+items[i].s);
		}
	}
	ll ans = -INF;
	FORN(i,0,S+1) chmax(ans, dp[i]);
	cout << ans << endl;
}

signed main() {
	FASTIO
	solve();
}
#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...