This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
بسم الله الرحمن الرحيم
وَاتَّقُوا يَوْمًا تُرْجَعُونَ فِيهِ إِلَى اللَّهِ ۖ ثُمَّ تُوَفَّىٰ كُلُّ نَفْسٍ مَّا كَسَبَتْ وَهُمْ لَا يُظْلَمُونَ
In The name if allah
And fear a Day when you will be returned to Allah
Then every soul will be compensated for what it earned
and they will not be treated unjustly.
*/
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<long long , null_type,less<long long >, rb_tree_tag,tree_order_statistics_node_update>
void init() {
cin.tie(nullptr);
std::istream::sync_with_stdio(false);
cout.tie(nullptr);
std::ostream::sync_with_stdio(false);
}
const long long mod = 1e9 + 7;
vector<pair<int, int>> vec[2001];
long long dp[2001][2001];
long long n, s;
long long solve(int i, int w) {
if (w == 0)
return w;
if (i > w)
return 0;
long long &ret = dp[i][w];
if (~ret)
return ret;
ret = solve(i + 1, w);
bool is = true;
long long ans = 0, tot = 0;
for (int j = 0; j < vec[i].size(); ++j) {
for (int k = 1; k <= vec[i][j].second; ++k) {
ans += vec[i][j].first;
tot += i;
if (w >= tot)
ret = max(ret, solve(i + 1, w - tot) + ans);
else {
is = false;
break;
}
}
if (!is)
break;
}
return ret;
}
int main() {
init();
cin >> s >> n;
int w, v, k;
for (int i = 0; i < n; ++i) {
cin >> v >> w >> k;
vec[w].emplace_back(v, k);
}
for (int i = 1; i <= s; ++i)
sort(vec[i].begin(), vec[i].end(), greater<pair<int, 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]);
long long ans = 0, tot = 0;
for (int l = 0; l < vec[i].size(); ++l) {
for (int k = 0; k < vec[i][l].second; ++k) {
ans += vec[i][l].first;
tot += i;
if (j >= tot)
dp[i][j] = max(dp[i][j], dp[i - 1][j - tot] + ans);
if (tot > j)
break;
}
if (tot > j)
break;
}
}
}
cout << dp[s][s];
}
Compilation message (stderr)
knapsack.cpp: In function 'long long int solve(int, int)':
knapsack.cpp:43: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]
43 | for (int j = 0; j < vec[i].size(); ++j) {
| ~~^~~~~~~~~~~~~~~
knapsack.cpp: In function 'int main()':
knapsack.cpp:76:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for (int l = 0; l < vec[i].size(); ++l) {
| ~~^~~~~~~~~~~~~~~
# | 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... |