# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
945864 | hihihihaw | Knapsack (NOI18_knapsack) | C++17 | 116 ms | 7628 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define pii pair<int,int>
#define sz(v) (int)v.size()
#define fi first
#define se second
#define INF 1223372036854775807
#define MOD 1000000007
#define cint(x) int x;cin>>x;
#define cinarr(a,n) int a[n];for (int i=0;i<n;i++) cin>>a[i];
#define coutarr(a) for (auto d:a) cout<<d<<" "; cout<<endl;
#define coutarrD(a) for (auto d:a) cout<<d.fi<<","<<d.se<<" "; cout<<endl;
#define AYBERK_SARICA ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define endl '\n'
#define ld long double
#define mid (start+end)/2
int t=1;
int testCase=1;
void solve(){
int s,n;
cin>>s>>n;
int v[n],w[n],k[n];
for (int i=0;i<n;i++) cin>>v[i]>>w[i]>>k[i];
vector<pii> z[s+1];
for (int i=0;i<n;i++) z[w[i]].pb({v[i],k[i]});
for (int i=0;i<=s;i++){
sort(z[i].begin(),z[i].end());
reverse(z[i].begin(),z[i].end());
}
vector<int> vRiyal;
vector<int> wRiyal;
for (int i=1;i<=s;i++){
int a=0;
int p=0;
while (a<=s/i+3){
if (p>=sz(z[i]))break;
if (z[i][p].se==0) p++;
else{
vRiyal.pb(z[i][p].fi);
wRiyal.pb(i);
z[i][p].se--;
a++;
}
}
}
int nn=sz(vRiyal);
int dpOld[s+1]={};
int dpNew[s+1]={};
for (int j=1;j<=s;j++){
if (j>=wRiyal[0]) dpOld[j]=vRiyal[0];
}
for (int i=1;i<nn;i++){
for (int j=1;j<=s;j++){
if (j>=wRiyal[i]) dpNew[j]=max(dpOld[j-wRiyal[i]]+vRiyal[i],dpOld[j]);
else dpNew[j]=dpOld[j];
}
for (int j=1;j<=s;j++){
dpOld[j]=dpNew[j];
}
}
cout<<dpOld[s];
}
int32_t main(){
//AYBERK_SARICA;
if (0){
freopen("in.txt", "r", stdin);
//freopen("wormsort.out", "w", stdout);
}
if (t==1) solve();
else{
cin>>t;
while (t--) solve();
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | 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... |