Submission #848056

#TimeUsernameProblemLanguageResultExecution timeMemory
848056_hbk06Knapsack (NOI18_knapsack)C++14
100 / 100
100 ms16724 KiB
#include<bits/stdc++.h>

using namespace std;
#define TASK "task"
#define all(x) (x).begin(),(x).end()
#define int long long
const int mxN = 5e5 + 7;

template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
     if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
     if (a < b) {a = b; return true;} return false;
}

int n;
int m;
int dp[mxN];
vector<pair<int,int>>weight[mxN];

signed main() {
	//freopen(TASK".inp","r",stdin);
	//freopen(TASK".out","w",stdout);
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		int v,w,k;
		cin >> v >> w >> k;
		weight[w].emplace_back(make_pair(v,k));
	}
	for (int i = 1; i <= n; i++) sort(all(weight[i]),greater<pair<int,int>>());
	for (int i = 1; i <= n; i++) {
		if((int) weight[i].size() == 0) continue;
		int cur = 0;
		for (int j = 0; j * i < n; j++) {
			if(cur >= (int) weight[i].size()) break;
			for (int k = n; k >= i; k--) maximize(dp[k],dp[k - i] + weight[i][cur].first);
			weight[i][cur].second--;
			if(!weight[i][cur].second) ++cur;
		}
	}
	cout << dp[n] << "\n";
	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...