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 int long long int
#define mod 1000000007
using namespace std;
signed main()
{
// ios_base::sync_with_stdio(NULL);
// cin.tie(NULL);
// freopen("feast.in", "r", stdin);
// freopen("feast.out", "w", stdout);
int s, n;
cin >> s >> n;
int v[n], w[n], k[n];
vector<vector<pair<int, int>>> vs(s + 1);
for (int i = 0; i < n; i++)
{
cin >> v[i] >> w[i] >> k[i];
vs[w[i]].push_back({v[i], k[i]});
}
vector<int> val, wt;
for (int i = 1; i <= s; i++)
{
sort(vs[i].begin(), vs[i].end());
int x = s / i, y = (int)vs[i].size() - 1;
while (x && y >= 0)
{
if (vs[i][y].second > x)
vs[i][y].second = x;
x -= vs[i][y].second;
while (vs[i][y].second--)
{
val.push_back(vs[i][y].first);
wt.push_back(i);
}
y--;
}
}
n = (int)val.size();
int dp[s + 1] = {};
for (int i = 0; i < n; i++)
{
for (int j = s; j >= wt[i]; j--)
{
dp[j] = max(dp[j], val[i] + dp[j - wt[i]]);
}
}
cout << dp[s] << "\n";
}
# | 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... |