This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <array>
#include <set>
#include <queue>
#include <map>
#include <iomanip>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unordered_map>
#include <string>
#include <fstream>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Mat = vector<vector<ll>>;
const ll MOD = 1e9+7;
const ll FACTOR1 = 2019201913LL;
const ll FACTOR2 = 2019201949LL;
const int MAXN = 2e3 + 11;
int s, n;
vector<pair<int,int>> cnt[MAXN];
vector<pair<int, int>> coins;
ll dp[MAXN];
bool cmp(pair<int, int> a, pair<int, int> b)
{
return a.first > b.first;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
//freopen("feast.in", "r", stdin);
//freopen("feast.out", "w", stdout);
cin >> s >> n;
for (int i = 0; i < n; i++)
{
int v, w, k;
cin >> v >> w >> k;
cnt[w].push_back({ v,k });
//cout << w << " " << v << " " << k << '\n';
}
for (int i = 1; i <= s; i++)
sort(cnt[i].begin(), cnt[i].end(), cmp);
for (int i = 1; i <= s; i++)
{
if (cnt[i].empty())
continue;
int total = max(1,(s+i-1)/i);
int curr = 0;
for (int j = 0; j < total; j++)
{
//cout << i << " TOADD\n";
if (cnt[i][curr].second == 0)
curr++;
if (curr == cnt[i].size())
break;
coins.push_back({i,cnt[i][curr].first});
cnt[i][curr].second--;
}
}
dp[0] = 0;
for (int i = 0; i < coins.size(); i++)
{
//cout << coins[i].first << " :: " << coins[i].second << '\n';
for (int x = s; x >= 0; x--)
{
if (x == 0 || dp[x])
{
if (x + coins[i].first <= s)
{
dp[x + coins[i].first] = max(dp[x + coins[i].first],dp[x] +
coins[i].second);
}
}
}
}
ll mx = 0;
for (int i = 1; i <= s; i++)
mx = max(mx, dp[i]);
//cout << i << " " << dp[i] << '\n';
cout << mx << '\n';
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:82:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | if (curr == cnt[i].size())
| ~~~~~^~~~~~~~~~~~~~~~
knapsack.cpp:94:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for (int i = 0; i < coins.size(); i++)
| ~~^~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |