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>
using namespace std;
struct chiso{
int val, weight, quantity;
};
chiso a[100005];
vector <int> D[2005];
long long dp[2005][2005];
bool cmp(int x, int y){
return a[x].val > a[y].val;
}
int main(){
//freopen("a.inp", "r", stdin); freopen("a.out", "w", stdout);
int s, n; cin >> s >> n;
for (int i = 1; i <= n; i++){
cin >> a[i].val >> a[i].weight >> a[i].quantity;
D[a[i].weight].push_back(i);
}
for (int i = 1; i <= s; i++){
if (!D[i].empty()) sort(D[i].begin(), D[i].end(), cmp);
}
for (int i = 1; i <= s; i++)
{
for (int j = 1; j <= s; j++)
{
if (!D[i].empty())
{
dp[i][j] = dp[i - 1][j];
int k = 1, dem = 1, ii = 0;
long long sum = 0;
while (j - k * i >= 0 && ii < D[i].size())
{
sum += a[D[i][ii]].val;
dp[i][j] = max(dp[i][j], dp[i - 1][j - k * i] + sum);
dem++; k++;
if (dem > a[D[i][ii]].quantity) dem = 1, ii++;
//cout << i << " " << j << " " << ii << " " << dp[i][j] << endl;
}
}
else dp[i][j] = dp[i - 1][j];
//cout << i << " " << j << " " << dp[i][j] << endl;
}
}
cout << dp[s][s];
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:35:45: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | while (j - k * i >= 0 && ii < D[i].size())
| ~~~^~~~~~~~~~~~~
# | 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... |