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 <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 2010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf 1000000000
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1 << j))
#define onbit(i, j) (i | (1 << j))
#define vi vector<int>
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
struct things
{
int v, w, k;
} a[maxn];
int n, s;
namespace sub1
{
void solve()
{
cout << min(s / a[1].w, a[1].k) * a[1].v;
}
}
namespace sub2
{
int dp[N];
void solve()
{
fo(i, 1, n)
{
fod(j, s, 1) if (j >= a[i].w)
dp[j] = max(dp[j], dp[j - a[i].w] + a[i].v);
}
cout << dp[s];
}
}
namespace sub3
{
int dp[maxn];
vector<things> arr;
void solve()
{
fo(i, 1, n)
{
fo(j, 1, min(s / a[i].w, a[i].k)) arr.push_back({a[i].v, a[i].w, 1});
}
for (auto [v, w, k] : arr)
{
fod(j, s, 1) if (j >= w)
dp[j] = max(dp[j], dp[j - w] + v);
}
cout << dp[s];
}
}
namespace full
{
vii adj[N];
int dp[N][N];
vi v;
void solve()
{
fo(i, 1, n)
{
if (a[i].w <= s and a[i].k > 0)
adj[a[i].w].pb(a[i].v, a[i].k), v.pb(a[i].w);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
fo(i, 0, (int)v.size()) fo(j, 0, s) dp[i][j] = -inf;
dp[0][0] = 0;
for (int i = 0; i < v.size(); i++)
{
int it = v[i];
sort(adj[it].begin(), adj[it].end(), greater<pii>());
// two pointer
fo(sum, 0, s)
{
dp[i + 1][sum] = max(dp[i + 1][sum], dp[i][sum]);
int profit = 0, pos = 0, used = 0, tot = 0;
while (tot + it <= sum and pos < adj[it].size())
{
tot += it;
used++;
profit += adj[it][pos].fi;
dp[i + 1][sum] = max(dp[i + 1][sum], dp[i][sum - tot] + profit);
if (used == adj[it][pos].se)
{
if (pos < adj[it].size())
{
used = 0;
pos++;
}
else break;
}
}
}
}
cout << *max_element(dp[(int)v.size()],dp[(int)v.size()] + s + 1);
}
}
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> s >> n;
fo(i, 1, n) cin >> a[i].v >> a[i].w >> a[i].k;
if (n == 1)
sub1::solve();
else
full::solve();
// else if(n <= 100) sub3::solve();
}
Compilation message (stderr)
knapsack.cpp: In function 'void full::solve()':
knapsack.cpp:99:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for (int i = 0; i < v.size(); i++)
| ~~^~~~~~~~~~
knapsack.cpp:108:48: 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]
108 | while (tot + it <= sum and pos < adj[it].size())
| ~~~~^~~~~~~~~~~~~~~~
knapsack.cpp:116:33: 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]
116 | if (pos < adj[it].size())
| ~~~~^~~~~~~~~~~~~~~~
knapsack.cpp: At global scope:
knapsack.cpp:129:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
129 | main()
| ^~~~
knapsack.cpp: In function 'int main()':
knapsack.cpp:134:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
134 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:135:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |