제출 #1170286

#제출 시각아이디문제언어결과실행 시간메모리
1170286yamiza_zinnoKnapsack (NOI18_knapsack)C++20
73 / 100
1097 ms444 KiB
/*
     /\_/\
    (= ._.)
    / >0  \>1

*/
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define el cout<<"\n"
#define rtn return
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#define f0(i,n) for(int i=0;i<n;i++)
#define f1(i,n) for(int i=1;i<=n;i++)
#define fz(i,a,n,z) for(int i=a;i<n;i+=z)
#define rep(i,a,n,z) for(int i=a;i>n;i-=z)
#define onii_chan ios_base::sync_with_stdio(false);
#define baka cin.tie(0);
#define hentai cout.tie(0);
#define file(name) if(fopen(name".inp","r")){freopen(name".inp","r",stdin);freopen(name".out","w",stdout);}
template<typename... T>
void in(T&... args) { ((cin >> args), ...); }
template<typename... T>
void print(T&&... args) { ((cout << args << " "), ...); } 
template<typename... T>
void println(T&&... args) { ((cout << args << " "), ...); cout << '\n'; }
template<typename... T>
void printl(T&&... args) { ((cout << args << "\n"), ...); } 
template <typename T> 
bool minimize(T& a, const T& b) {
    return b < a ? (a = b, true) : false; 
}
template <typename T> 
bool maximize(T& a, const T& b) {
    return a < b ? (a = b, true) : false; 
} 

const int MAXN = 1e6+5;
const int mod =1e9+7;
inline void add(int &a,int b){
    a+=b;
    if(a >= mod) a-=mod;
    if(a < 0) a+=mod;
}
///---------------------------------------------------------------

/// End the template, take a sip of Coke before reading the code ඞඞඞඞඞ

int a[MAXN];
long long dp[MAXN];
signed main(){
    onii_chan;
    baka;hentai;
    file("TEST");
    int s,n;in(s,n);
    f1(i,n){
        int v,w,k;
        in(v,w,k);
        vector<int> block;
        for(int j=1;k > 0;j*=2){
            int x=min(j,k);
            block.push_back(x);
            k-=x;
        }
        for(int x : block){
            for(int j=s;j>=1ll*x*w;j--) maximize(dp[j],dp[j-x*w]+x*v);
        }
    }
    print(dp[s]);
}
// ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢟⣵⣿⣿⣿⣷⣦⣝⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⢿⠩⠑⣺⣿⣿⣦⣝⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⠁⣁⠟⠛⠛⠻⠿⣿⣿⣦⡈⠩⣤⣤⣽⣏⣋⡉⠉⣠⣥⣴⣾⠿⠙⠋⠻⠿⣷⣧⡙⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠱⠋⠉⠁⠀⠀⠀⠀⠀⣀⣈⣿⣿⣿⣿⣿⣿⣿⣿⣿⣾⣿⣏⣀⡀⠀⠀⠀⠀⠁⠘⣿⣿⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢋⠆⣐⣀⠔⠀⠀⢀⣀⣴⣾⣿⣿⣿⠟⠋⣈⣽⣿⣿⣿⣿⣿⣿⣿⣿⢿⣦⠀⠀⠀⠀⠀⢹⣿⣿⣿⣿⣬⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⣣⣿⣿⣿⡟⠁⢀⣴⣿⡟⢹⣿⣿⣉⠄⣠⣼⡟⡟⠛⣶⡄⢻⣿⣿⣿⣿⣧⠩⣿⣦⡀⠀⠀⠈⣿⣿⣿⣿⣿⣯⡻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣟⣼⢯⣿⣿⡏⢀⣴⣿⣿⡟⢀⣾⢻⡿⠋⣼⣿⣟⢰⡇⢰⣿⣷⠈⢿⣿⢻⣿⣿⣆⠸⣿⣷⡀⠀⠀⢿⣿⣿⣿⣿⣟⣿⣜⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⣼⣻⣿⣿⠋⢰⣾⣿⢿⡿⢁⡾⠁⠞⠀⡸⣿⣿⡇⣹⣿⣿⣿⣿⣆⠸⣿⠄⢻⣿⣿⣆⠹⢿⣿⡄⠀⢸⠿⣿⣿⣿⣿⣞⣿⡍⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠯⡾⠽⣽⣿⠇⢠⣿⠿⢁⣾⠗⡼⠃⠈⠀⢰⠣⣿⣿⠈⣿⣿⡿⣿⣿⣿⠀⠘⣶⠈⣿⣿⣿⣆⢸⣿⣷⠀⠈⠀⣿⣿⣿⣿⣿⣿⣿⡜⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣿⣿⣿⡿⡵⣽⢫⣻⣿⡏⢀⣾⠋⢂⣾⠏⢀⡏⠀⠀⠀⡏⢰⣿⡿⢠⣿⣿⡇⢙⣿⣿⠀⠀⢻⡇⢸⣿⣿⣿⡟⣿⡿⡆⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⡜⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣿⡿⢋⣸⣹⢃⡾⣽⡿⠁⣾⡏⡂⣾⠏⠀⢸⠀⠀⠀⢠⠃⢸⣿⡇⢸⣿⣿⡇⠀⣿⣿⠀⠀⠀⢇⠈⣿⣿⣿⡇⣿⡇⢹⡀⠀⢹⣿⣿⣿⢿⣿⣿⣿⣿⠸⣿⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣫⣥⣶⣿⠷⢎⢿⢇⣿⠁⢸⡟⠈⣼⠟⠀⠀⠈⠀⠀⠀⢸⠀⣿⣿⡇⢸⣿⡟⡇⠈⣿⣿⠀⠀⠀⠘⠀⢹⣿⣿⡇⢻⡇⣸⡅⠀⢸⣿⣿⣿⡞⣿⣿⣿⣿⡇⢻⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣿⣿⣿⠂⡈⠠⣸⡏⠀⣿⠁⢤⡏⠀⠀⠀⠇⠀⠀⠀⢸⠀⣿⣿⣧⣼⣿⡿⢷⠀⣿⡟⠀⠀⠀⠀⠀⠸⣿⣿⡇⢸⣿⣿⡇⠀⢸⣿⣿⣿⣷⢻⣿⣿⣿⣷⠘⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣿⣿⡏⠀⠀⡆⣿⠁⢠⡏⠀⡜⠀⠀⢀⡄⠀⠀⢠⡆⢸⠀⢸⣿⡏⢸⣿⡇⢸⡆⣿⠃⠀⠀⠀⠀⠀⠀⣿⣿⡇⢸⣿⢸⣿⠀⢸⣿⣿⣿⣿⢸⣿⣿⣿⢿⡀⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣿⣿⡇⠀⢀⢱⡟⠀⣸⠁⢠⠃⠀⣰⣿⣇⠀⢠⣿⣇⢸⠀⠀⣿⡃⢸⣿⣧⠘⠷⠉⠀⠀⠀⡀⠀⠀⠀⢷⣿⡇⢸⣿⠠⣿⠀⢸⣿⣿⣿⣿⢸⣿⣿⣇⢸⠀⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣿⣿⠇⠀⠸⢸⡇⠀⣿⠀⠀⠀⣰⣿⣿⣿⠀⣾⣿⣿⡘⠀⠀⣿⡇⢸⣿⣿⠀⢧⠀⠀⢀⣾⣿⡀⠀⠀⠀⢻⡇⢸⣿⠀⣿⡀⢸⣿⣿⣿⣿⠘⣿⣿⣿⢻⠃⢿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣿⣿⠀⠀⢠⢸⡇⠀⣿⠀⠇⠀⠿⠿⢿⣿⡀⣿⣿⣿⣄⠇⠀⢸⡇⠈⣿⣿⡆⠀⠀⠀⡾⠿⢿⣗⡀⠀⠀⠀⠃⣼⣿⠀⣿⡇⠈⣿⣿⣿⣿⠀⣿⣿⡏⠘⣆⢸⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣿⡟⡀⠀⠀⢻⣇⠀⡏⠀⠀⣾⠟⠛⠓⠒⠦⠘⠭⣭⣇⣠⠀⡄⠇⠀⣿⠚⣧⠀⠠⠤⠐⠛⠻⠿⠿⠄⠀⠀⠀⣿⡿⢀⣿⡇⠀⣿⣿⣿⣇⠀⣿⣿⡇⢸⣿⢸⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣿⢇⡇⠀⠀⠸⣿⠀⠃⠀⠀⠁⠀⢀⠀⠀⠀⠀⠀⣾⣿⡄⠀⢀⠘⠀⢸⡄⠘⡄⠠⠂⠀⠀⠀⠀⠀⠀⠀⠀⠰⣿⡇⢸⣿⣷⠀⣿⡟⢻⡇⠀⢹⣿⡇⢸⣿⡾⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⡿⠴⠃⠀⠀⠀⣿⠀⢰⡄⠀⠀⣾⡏⠀⠀⠀⠀⠀⢺⣿⣧⠀⢸⡄⠀⢸⣧⠀⠱⠀⠆⠀⠀⠀⠀⠀⢰⣤⠀⠀⠀⡇⢸⣿⡏⠀⣿⣇⢸⡇⠀⢸⣿⡇⢸⣿⡇⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣧⠀⠀⠀⠀⢸⣿⡀⠘⠀⠀⣾⣿⡷⠀⠀⠀⢠⡄⢸⣿⣿⣆⠀⣷⠀⠸⠿⡆⠀⠄⣠⡤⠀⠀⣤⠀⢠⣿⡇⠀⠈⣧⣼⣿⡇⢠⣿⡿⣿⡇⠀⢸⣿⡇⢸⠛⡇⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⡀⡀⠀⠀⠀⢸⢻⡇⠀⡆⠀⣿⣿⣷⡀⢤⣴⡆⣠⣿⣿⣿⣿⡆⢻⣧⣾⣷⣶⣶⣶⣦⡀⠀⠠⠄⢀⣾⣿⠃⡎⢺⠀⣿⣿⡇⢸⣿⠀⣿⠇⠀⣾⣿⡇⠘⠀⢁⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣀⠀⠀⠀⠀⠘⠸⣿⡀⢹⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⣹⣿⣦⣿⣿⣿⣿⣿⣿⣿⣿⣷⣿⣷⣿⡿⣿⠀⡇⠈⢰⠇⣿⠀⣾⣿⠀⣿⠀⠀⣿⣿⠇⠀⢀⣾⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⡄⠀⢿⣷⠈⢧⠈⠻⣿⣿⠿⠿⠿⠿⣿⣿⣷⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠐⣼⠀⣿⠀⣿⣿⠀⣿⠀⠀⣿⣿⠀⠀⣾⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⡇⠀⢠⠀⠀⠀⠀⠘⠿⠷⠄⠀⠈⣉⡁⠀⠐⠒⠒⠛⠛⠠⠤⠤⠤⠤⠐⠛⠋⠉⢉⣉⡉⢽⡟⠛⠛⠿⢟⠀⠀⠀⢿⠀⡇⠀⢿⡟⠀⣿⠀⠀⣿⣿⢀⣲⣿⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⡇⣷⣾⠀⠀⠀⠀⠀⠀⠀⠀⢰⣿⣿⣿⣿⢿⣿⣿⣿⣷⣶⣶⣶⣶⣶⣶⣶⣦⣤⣤⣤⣤⣤⣤⣤⣤⣤⡄⠀⠀⢰⡏⠀⡇⠀⢸⡇⠀⣿⠀⢀⡿⢿⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣷⣿⣿⡀⠀⠀⠀⠀⠀⠀⠀⠈⡿⣟⣿⣿⣾⣿⣿⣿⣏⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡟⠀⠀⢸⡇⠀⡇⠀⢸⡇⠀⢻⠀⢸⡇⢸⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⡟⣿⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⣿⣿⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠃⠀⢀⣼⠃⠀⡇⠀⢸⣇⡆⠸⠀⢸⣇⣸⡟⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣇⢿⣿⣿⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⠁⠀⠀⠀⢹⠀⠀⡇⠀⣼⢸⡇⣠⡀⠈⣿⠙⣇⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣇⠘⣿⣿⠃⠀⠀⠀⠀⠀⠀⠀⢠⠀⠀⠀⠈⠙⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠛⠉⠁⠀⠀⠀⣀⣄⡀⣺⠀⠀⡇⠀⠏⣾⣟⠀⠀⠀⣿⠀⡏⢹⣿⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣿⣄⠈⠋⠀⠀⠀⠀⠀⠀⠀⠀⢸⣷⣦⣀⠀⠀⠀⠀⠉⠻⣿⣿⣿⣿⠿⠟⠋⠉⠀⠀⠀⠀⠀⠀⠀⠈⠙⠃⡇⣿⠀⠀⡇⠀⢰⣿⣿⡤⠀⠀⢿⡶⣷⠺⣿⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣿⣿⡷⠀⡄⠀⠀⠀⠀⠆⠀⠀⢸⣿⣿⣿⣿⣷⣶⣶⣾⠗⡠⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⡇⠀⠀⠇⠀⢾⣿⣿⣿⣅⠀⢸⣿⣿⣀⣿⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⣿⢟⣼⢰⠃⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣿⣿⣿⣿⢰⡿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡀⠀⢠⢰⠀⡇⠀⢸⠀⠀⣾⣿⣿⣿⣿⠀⢸⣿⣸⡏⢻⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⠏⣾⢇⠇⢀⠀⢠⡀⠀⠀⣾⠀⠈⣿⣿⣿⣿⣿⣿⠇⣿⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣴⡟⠃⠀⣀⣴⣿⣞⢧⡇⠀⠸⠀⡴⣿⣿⣿⣿⣿⡇⢸⡿⡟⡇⣸⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣟⢹⠏⡘⢀⣾⢀⣾⡇⠀⠄⣿⣧⠀⣿⣿⡿⢟⣛⣍⣾⣿⣿⡆⠀⠀⢀⣀⣠⣤⣾⡿⠛⠉⣀⣤⣾⣿⣿⣿⣿⣷⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⡇⢸⡇⡇⡇⢹⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⣿⡚⢀⣇⣾⡏⣸⣿⣧⠀⢧⣿⣿⡆⠛⣋⢸⣿⣯⣿⣿⣿⣿⣿⣆⠀⠀⣿⠟⠉⢁⣠⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⡀⠀⠀⠀⢸⢹⣿⣿⣿⣿⠇⡸⣹⡇⣇⣼⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⣿⡿⠀⢰⣿⣏⠁⣉⣥⡄⢸⠚⣿⡏⣇⣸⣿⡞⣿⣿⣿⣿⣿⣿⣿⡟⢀⠀⠀⠀⠀⠘⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠈⡆⣌⣉⣛⠻⠠⢡⣿⡇⣈⣿⣿⣿⣿⣿⣿⣿⣿⣿
// ⣿⣿⣿⡟⡀⠀⠈⣿⣿⡀⢻⢿⡇⢸⠀⣿⢇⢸⠉⣿⣷⡸⣿⣿⣿⣿⣿⠟⣰⣿⡷⣄⡀⢠⣤⣤⠹⣿⣿⣿⣿⣿⣿⣿⣿⣿⡷⡀⠀⡇⠀⠘⡘⣿⣿⠃⢀⣶⣦⠁⣈⣛⣻⠟⠛⠻⠻⠿⣿⣿
// ⣿⣿⡿⣱⣿⡆⣶⡹⠇⣷⡐⣦⣌⠘⠀⡏⢸⡎⠀⣿⣿⣷⠙⣿⣿⣿⡏⣸⣿⣿⣗⣼⠗⣾⣿⣿⡇⢽⣿⣿⣿⣿⣿⣿⠟⢋⣴⣷⣄⠁⣀⠀⠣⠙⢁⣴⣿⣿⡟⣰⣿⣿⣿⣿⣿⣿⣷⣦⡈⢻
// ⣿⡿⢁⣿⣿⣆⢹⣿⣬⣿⣷⡹⣿⡄⠘⢀⣶⣭⡀⣿⣿⣿⣧⣈⠿⡿⠀⠈⢿⣿⣿⣿⢧⣿⣿⠟⠁⡜⣿⣿⡿⡟⣋⣔⣴⣿⣿⣿⣏⢷⣄⣿⣶⣌⡹⣿⣿⣿⣱⣿⣿⣿⣿⣿⣿⣿⠿⣿⣿⡅
// ⡿⠁⣸⣿⣿⣿⣇⢿⣿⣿⣿⣿⣿⣷⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣷⣶⣾⣷⠘⣿⣿⣿⣸⣯⣥⣴⣾⣿⣿⣽⣶⣿⣿⣿⣿⣿⣿⣿⣿⣾⣿⣿⣿⣿⣿⣿⠹⣿⣿⣿⣿⣿⣿⣿⠟⣡⣾⣿⣿⣷

컴파일 시 표준 에러 (stderr) 메시지

knapsack.cpp: In function 'int main()':
knapsack.cpp:22:53: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 | #define file(name) if(fopen(name".inp","r")){freopen(name".inp","r",stdin);freopen(name".out","w",stdout);}
      |                                              ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:56:5: note: in expansion of macro 'file'
   56 |     file("TEST");
      |     ^~~~
knapsack.cpp:22:83: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   22 | #define file(name) if(fopen(name".inp","r")){freopen(name".inp","r",stdin);freopen(name".out","w",stdout);}
      |                                                                            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:56:5: note: in expansion of macro 'file'
   56 |     file("TEST");
      |     ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...