Submission #869895

#TimeUsernameProblemLanguageResultExecution timeMemory
869895Karnis_052Knapsack (NOI18_knapsack)C++17
100 / 100
45 ms4948 KiB
// Bismillahir Rahmanir Rahim

/// It is a oj.uz oj's problem
// problem link https://oj.uz/problem/view/NOI18_knapsack
#include<bits/stdc++.h>
using namespace std;
typedef long long int  ll;
typedef pair<int, int>PI;
typedef pair<ll, ll > PL;
typedef vector<int>VI;
typedef vector<ll>VL;
#define FF first
#define SS second
#define sz size()
const int mod = 1e9 + 7;
const int INF = 1e9;
const int N = 2e3 + 5;
int main()
{
	ios::sync_with_stdio(false);   cin.tie(0); cout.tie(0);
	ll bag, n, value, weight, k;
	cin >> bag >> n;
	vector<PL>items[N];
	for (int i = 0; i < n; i++)
	{
		cin >> value >> weight >> k;
		items[weight].push_back({value, k});
	}
	vector<ll>dp(N);
	dp[0] = 0;
	for (ll i = 1; i < N; i++)
	{
		if (items[i].size() == 0)continue;
		sort(items[i].rbegin(), items[i].rend());


		ll coun = 0, it = 0;
		while (coun * i <= bag and it < items[i].size())
		{
			int picked = 0;
			while (coun * i <= bag and picked < items[i][it].SS)
			{
				for (ll j = bag; j >= i; j--)
					dp[j] = max(dp[j], dp[j - i] + items[i][it].FF);
				coun++;
				picked++;
			}
			it++;
		}

	}
	ll ans = 0;
	for (int i = 0; i < N; i++)
		ans = max(ans, dp[i]);
	cout << ans << '\n';


	cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
	return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:38:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |   while (coun * i <= bag and it < items[i].size())
      |                              ~~~^~~~~~~~~~~~~~~~~
#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...