Submission #1086758

#TimeUsernameProblemLanguageResultExecution timeMemory
1086758quangminh412Knapsack (NOI18_knapsack)C++14
100 / 100
71 ms36432 KiB
#include <bits/stdc++.h>
using namespace std;

/*
  John Watson
  https://codeforces.com/profile/quangminh98

  Mua Code nhu mua Florentino !!
*/

#define faster() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long

const int maxs = 2009;

int s, n;
vector<pair<ll, int>> bag[maxs];
ll dp[maxs][maxs];

signed main()
{
	if (fopen("test.inp", "r"))
	{
		freopen("test.inp", "r", stdin);
		freopen("test.out", "w", stdout);
	}
	faster();

	cin >> s >> n;
	for (int i = 1; i <= n; i++)
	{
		int v, w, k; cin >> v >> w >> k;
		bag[w].emplace_back(v, k);
	}
	
	for (int i = 1; i <= s; i++) sort(bag[i].begin(), bag[i].end(), greater<pair<ll, int>>());
	
	for (int i = 1; i <= s; i++)
		for (int j = 1; j <= s; j++)
		{
			dp[i][j] = max(dp[i][j], dp[i - 1][j]);
			
			int id = 0, cnt = 0, cur = 0;
			ll val = 0;
			for (int t = i; t <= j; t += i)
			{
				while (id < bag[i].size() && cur < t)
				{
					if (cnt == bag[i][id].second)
					{
						cnt = 0;
						id++;
					}
					cur += i; 
					cnt++;
					val += bag[i][id].first;
				}
				if (id == bag[i].size()) break;
				dp[i][j] = max(dp[i][j], dp[i - 1][j - t] + val);
			}
		}
		
	ll ans = 0;
	for (int i = 1; i <= s; i++) ans = max(ans, dp[s][i]);
	
	cout << ans << '\n';

	return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:47:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |     while (id < bag[i].size() && cur < t)
      |            ~~~^~~~~~~~~~~~~~~
knapsack.cpp:58:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |     if (id == bag[i].size()) break;
      |         ~~~^~~~~~~~~~~~~~~~
knapsack.cpp:24:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   24 |   freopen("test.inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:25:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |   freopen("test.out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...