이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define N 100000
#define S 2000
#define pb push_back
#define int long long
#define mp make_pair
#define fi first
#define se second
#define rep(i, l, r) for(int i = l; i <= r; i++)
using namespace std;
typedef pair<int,int> ii;
int n, s;
int dp[S+5];
vector<int> val[S+5];
vector<ii> ares;
struct mondo {
int v, w, k;
} a[N+5];
bool cmp(mondo x, mondo y){
if (x.w == y.w) return x.v > y.v;
return x.w < y.w;
}
void solve(){
cin >> s >> n;
rep(i, 1, n) {
cin >> a[i].v >> a[i].w >> a[i].k;
}
sort(a+1, a+1+n, cmp);
rep(i, 1, n) {
// cout << a[i].v << " " << a[i].w << " " << a[i].k << endl;
}
rep(i, 1, n){
int maxsize = s / a[i].w;
if (a[i].w > s) continue;
while (a[i].k && val[a[i].w].size() < maxsize){
a[i].k--;
val[a[i].w].pb(a[i].v);
}
}
rep(i, 1, s){
for(auto x: val[i]){
ares.pb(mp(i, x));
}
}
int res = 0;
for(auto x: ares) {
int w = x.fi, v = x.se;
for(int i = s; i >= w; i--){
dp[i] = max(dp[i], dp[i-w] + v);
}
}
rep(i, 0, s) res = max(res, dp[i]);
cout << res;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("bt.inp", "r", stdin);
freopen("bt.out", "w", stdout);
#endif
solve();
}
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'void solve()':
knapsack.cpp:40:45: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
40 | while (a[i].k && val[a[i].w].size() < maxsize){
| ~~~~~~~~~~~~~~~~~~~^~~~~~~~~
knapsack.cpp: In function 'int main()':
knapsack.cpp:65:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | freopen("bt.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:66:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | freopen("bt.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |