Submission #947130

#TimeUsernameProblemLanguageResultExecution timeMemory
947130sandrofeiqrishviliKnapsack (NOI18_knapsack)C++14
73 / 100
115 ms71744 KiB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define int long long
const int N = 2005, inf=1e18;
vector <pair <int, int> > vec[N], g;
int dp[N][N];
int ans=-1e8;
signed main(){
	int s, n, v, w, r;
	cin >> s >> n;
	for(int i=1; i<=n; i++){
		cin >> v >> w >> r;
		vec[w].pb({v,r});
	}
	for (int i=1; i<=s; i++) {
		sort(vec[i].begin(),vec[i].end());
		reverse(vec[i].begin(), vec[i].end());
		int tot = s/i;
		for(int j=0; j<vec[i].size(); j++){
			int val = vec[i][j].ff;
			int raod = vec[i][j].ss;
			
			for (int x=1; x<=min(raod, tot); x++) {
				g.pb({i,val});
			}

			if(tot>=raod){
				tot-=raod;
			} else {
				tot=0;
				break;
			}
		}
	}
	n=g.size();
	for(int i=1; i<=n; i++){
		int w=g[i-1].ff;
		int val=g[i-1].ss;
		for(int j=0; j<=s; j++){
			dp[i][j]=dp[i-1][j];
			if (j>=w) dp[i][j]=max(dp[i-1][j], dp[i-1][j-w]+val);
			ans=max(ans, dp[i][j]);
		}
	}
	cout << ans;
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:22:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |   for(int j=0; j<vec[i].size(); j++){
      |                ~^~~~~~~~~~~~~~
#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...