이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define sqr(x) ((x) * (x))
#define log2i(x) (64 - __builtin_clzll(1ll * (x)) - 1)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define debug(x) { cout << #x << " = "; cout << (x) << endl; }
#define rr(x) sort(all(x)),x.resize((unique(all(x))-x.begin()));
template<typename T> using vt = vector<T>;
using ll = long long;
using ld = long double;
using vi = vt<int>;
using ii = pair<int, int>;
using vii = vt<ii>;
const ll INF=4e18;
const int inf=INT_MAX;
const int MOD=1000000003;
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
template<class U, class V> std::ostream& operator << (std::ostream& out, const std::pair<U, V>& p) {return out << '(' << p.first << ", " << p.second << ')';} //cout pair type
template<typename T> using minpq = priority_queue<T, vt<T>,greater<T>>;
template<typename T> using maxpq = priority_queue<T>;
ll fgcd(ll a, ll b) {while(b) swap(b, a %= b); return a;}
ll fpow(ll a, ll b, const ll c) { ll ans = 1; a %= c; for(; b; b >>= 1, a = a * a % c) if(b & 1) ans = ans * a % c; return ans;}
ll fpow(ll a, ll b) {ll ans = 1; for(; b; b >>= 1, a *= a) if(b & 1) ans *= a; return ans;}
int flog(int x) {return 31 - __builtin_clz(x);}
int flog(ll x) {return 63 - __builtin_clzll(x);}
void setIO(string name) {
cin.tie(0)->sync_with_stdio(0);
if(sz(name)) {
freopen((name+".inp").c_str(), "r", stdin);
freopen((name+".out").c_str(), "w", stdout);
}
}
int n,s;
vt<ll> k,w,v;
ll dp[2001][2001][10] = {0};
void sub3(){
ll ans=0;
for (int i=1;i<=n;i++){
for (int j=0;j<=s;j++){
for (int cnt=1;cnt<=k[i];cnt++){
if (j - cnt*w[i] >= 0) dp[i][j][cnt] = max(dp[i-1][j][k[i-1]] , dp[i-1][j-cnt*w[i]][k[i-1]] + cnt*v[i]);
ans=max(ans,dp[i][j][cnt]);
//cout<<i<<' '<<j<<' '<<cnt<<'\n';
//debug(dp[i][j][cnt]);
}
//cout<<'\n';
}
//cout<<'\n';
}
cout<<ans;
}
int main(){
setIO("");
cin>>s>>n;
k.resize(n+1,0);
w.resize(n+1,0);
v.resize(n+1,0);
for (int i=1;i<=n;i++){
cin>>v[i]>>w[i]>>k[i];
}
if (*max_element(k.begin()+1,k.end()) <= 10) sub3();
}
/* https://oj.uz/problem/view/NOI18_knapsack */
/*g++ -std=c++17 -O2 -Wall "knapsack.cpp" -o main.exe
./main.exe*/
컴파일 시 표준 에러 (stderr) 메시지
knapsack.cpp: In function 'void setIO(std::string)':
knapsack.cpp:42:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | freopen((name+".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:43:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | freopen((name+".out").c_str(), "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... |