이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1e18
void solve() {
ll S,N; cin>>S>>N;
map<ll,vector<pair<ll,ll>>> m; // wt: [[val,cnt]]
for (ll i=0; i<N; i++) {
ll V,W,K; cin>>V>>W>>K;
m[W].push_back({V,K});
}
for (auto x: m) {
sort(x.second.rbegin(), x.second.rend());
}
vector<pair<ll,ll>> items;
for (auto x:m) {
ll wt = x.first;
ll cnt = S/wt;
ll done = 0;
for (ll i=0; i<x.second.size(); i++) {
ll val = x.second[i].first;
ll ttl = x.second[i].second;
for (ll j=0; j<ttl; j++) {
items.push_back({val, wt});
cnt--;
if (cnt == 0) {
done = 1; break;
}
}
if (done == 1) break;
}
}
vector<vector<ll>> dp(items.size()+1, vector<ll>(S+1,0));
for (ll i=1; i<=items.size(); i++) {
for (ll j=1; j<=S; j++) {
if (j - items[i-1].second >= 0) {
dp[i][j] = max(dp[i][j], dp[i-1][j-items[i-1].second] + items[i-1].first);
}
dp[i][j] = max(dp[i][j], dp[i-1][j]);
}
}
cout << dp[items.size()][S] << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'void solve()':
knapsack.cpp:21:23: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for (ll i=0; i<x.second.size(); i++) {
| ~^~~~~~~~~~~~~~~~
knapsack.cpp:35:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for (ll i=1; i<=items.size(); i++) {
| ~^~~~~~~~~~~~~~
# | 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... |