이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll dp[2005];
array<int,3> Op[100005];
vector<pair<int,int>> vec[2005];
const ll inf = 1e15;
signed main(){
int S, n; scanf("%d %d", &S, &n);
vector<int>C;
for(int i = 0, Vi, Wi, Ki; i<n; ++i){
scanf("%d %d %d", &Vi, &Wi, &Ki);
Op[i] = {Vi, Wi, Ki};
C.push_back(Wi);
}
C.push_back(-1);
sort(C.begin(), C.end());
C.resize(unique(C.begin(), C.end()) - C.begin());
for(int i = 0; i<n; ++i){
int Vi = Op[i][0], Wi = Op[i][1], Ki = Op[i][2];
vec[upper_bound(C.begin(), C.end(), Wi) - C.begin() - 1].emplace_back(Vi, Ki);
}
int m = C.size();
for(int j = 1; j<=S; ++j) dp[j] = -inf;
dp[0] = 0;
for(int i = 1; i<m; ++i){
sort(vec[i].begin(), vec[i].end(), greater<pair<int,int>>());
ll w = C[i];
for(int j = S; j>=0; --j){
bool br = false;
ll curr = 0, profit = 0;
for(pair<int,int> it: vec[i]){
ll val = it.first, no = it.second;
for(int cnt = 1; cnt<=no; ++cnt){
++curr; profit += val;
if(j>=w*curr){
dp[j] = max(dp[j], dp[j-w*curr] + profit);
}else{
br = true; break;
}
}
if(br) break;
}
}
}
ll ans = -inf;
printf("%lld", *max_element(dp, dp+S+1));
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'int main()':
knapsack.cpp:46:5: warning: unused variable 'ans' [-Wunused-variable]
46 | ll ans = -inf;
| ^~~
knapsack.cpp:9:17: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
9 | int S, n; scanf("%d %d", &S, &n);
| ~~~~~^~~~~~~~~~~~~~~~~
knapsack.cpp:12:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | scanf("%d %d %d", &Vi, &Wi, &Ki);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |