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
using namespace std;
int dp[2001][2001];
signed main() {
int s, n;
cin >> s >> n;
map<int,vector<pair<int,int>>> gruplar;
for(int i = 0; i < n; i++) {
int vi, wi, ki;
cin >> vi >> wi >> ki;
gruplar[wi].push_back({vi, ki});
}
for(auto x : gruplar) {
sort(x.second.begin(), x.second.end(), greater<pair<int,int>>());
}
// dp[i][j] ilk i grubu kullanarak j ağırlık kullanarak elde edebileceğim en fazla değer
dp[0][0] = 0;
int i = 1;
for(auto x : gruplar) {
int agirlik = x.first; // gruptakilerin ağırlığı
vector<pair<int,int>> grup = x.second; // {değer, kaç tane olduğu}
for(int w = 0; w <= s; w++) {
int j = 0;
int k = -1;
int deger = 0;
int kk = -1;
dp[i][w] = dp[i-1][w];
while(j < grup.size() && (kk+1)*agirlik <= w) {
kk++;
k++;
if(kk != 0) deger += grup[j].first;
if(w - kk * agirlik >= 0) dp[i][w] = max(dp[i][w], dp[i-1][w-kk*agirlik]+deger);
if(k == grup[j].second) {
j++;
k = 0;
}
}
}
i++;
}
int mx = 0;
for(int j = 0; j <= s; j++) {
mx = max(dp[i-1][j], mx);
}
cout << mx << endl;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:32:12: 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]
32 | while(j < grup.size() && (kk+1)*agirlik <= w) {
| ~~^~~~~~~~~~~~~
# | 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... |