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;
#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define MOD 1000000007
#define LOGN 20
#define MAXN 1000000
#define int long long
int dp[2005][2005];
signed main() {
fast
int S, N;
cin >> S >> N;
map<int, vector<pair<int,int>>> mp;
for (int i = 0; i < N; i++) {
int V, W, K;
cin >> V >> W >> K;
mp[W].push_back({V, K});
}
for (int i = 0; i < 2005; i++)
for (int j = 0; j < 2005; j++)
dp[i][j] = INT_MIN;
dp[0][0] = 0;
int now_considering = 0;
for (auto u : mp) {
vector<pair<int,int>> items = u.s;
int item_w = u.f;
now_considering++;
sort(items.begin(), items.end(), greater<pair<int,int>>());
for (int w = 0; w <= S; w++) {
dp[now_considering][w] = dp[now_considering - 1][w];
int items_taken_all = 0;
int items_taken = 0;
int item_no = 0;
int sum = 0;
while ((items_taken_all + 1) * item_w <= w && item_no < items.size()) {
items_taken_all++;
items_taken++;
sum += items[item_no].f;
if (dp[now_considering-1][w - items_taken_all*item_w] != INT_MIN)
dp[now_considering][w] = max(dp[now_considering][w], dp[now_considering-1][w - items_taken_all*item_w] + sum);
if (items_taken == items[item_no].s) {
items_taken = 0;
item_no++;
}
}
}
}
int ans = 0;
for (auto u : dp[mp.size()])
ans = max(ans, u);
cout << ans << "\n";
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:44:67: 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]
44 | while ((items_taken_all + 1) * item_w <= w && item_no < items.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... |