Submission #1305201

#TimeUsernameProblemLanguageResultExecution timeMemory
1305201pdnamKnapsack (NOI18_knapsack)C++20
100 / 100
38 ms1764 KiB
#include <bits/stdc++.h>
#define ll long long
#define fast ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define pii pair<int , int>
#define pli pair<ll , int>
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define MASK(i) (1ll << i)
#define bit(mask , i) ((mask >> i) & 1)
#define debug cout << "NOBUG" << endl;
#define fi first
#define se second
#define i128 __int128
#define ldb long double
#define el "\n"
#define pb push_back
#define ALL(vec) vec.begin(),vec.end()
#define SORT_AND_UNIQUE(vec) sort(ALL(vec)); vec.erase(unique(ALL(vec)) , vec.end())
#define lb(vec , x) lower_bound(ALL(vec) , x)

using namespace std;
const ll oo = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7 , MOD = 102e6 + 100829;

void File(string NAME) {
    freopen((NAME + ".inp").c_str() , "r" , stdin);
    freopen((NAME + ".out").c_str() , "w" , stdout);
}
template <class X , class Y>
    bool maximize(X  &x , Y y) {
        if(x < y) {
            x = y;
            return true;
        }
        return false;
    }
template <class X , class Y>
    bool minimize(X  &x , Y y) {
        if(x > y) {
            x = y;
            return true;
        }
        return false;
    }
template <class X , class Y>
    void add_mod(X &x , Y y ) {
        x += y;
        if(x >= mod) x -= mod;
        if(x < 0) x += mod;
    }

const int Nlimit = 1e5+5 , Slimit = 2000+3;
int n , S;
vector <pii> hehe[Slimit];
pii obj[Slimit*11];
int m = 0;
ll f[Slimit];


int main()
{
    fast
    //File("TASK");
    cin >> S >> n;
    for(int i = 1 ; i <= n ; i++) {
        int v , w , k; cin >> v >> w >> k;
        hehe[w].pb({v,k});
    }
    for(int w = 1 ; w <= S ; w++) {
        vector <ll> tmp;
        tmp.pb(0);
        sort(ALL(hehe[w]) , [&](const pii &x , const pii &y) {
            return x.fi > y.fi;
        });

        int it = 0;
        for(int s = 0 ; s <= S ; s += w) {
            if(it >= hehe[w].size()) break;
            while(hehe[w][it].se == 0) {
                it++;
                if(it >= hehe[w].size()) break;
            }
            if(it >= hehe[w].size()) break;

            tmp.pb(hehe[w][it].fi);
            hehe[w][it].se--;
        }

        int sz = tmp.size()-1;
        for(int i = 1 ; i <= sz ; i++) {
            obj[++m] = {tmp[i] , w};
        }
    }

    for(int i = 1 ; i <= m ; i++) {
        //cout << obj[i].fi << ',' << obj[i].se << el;
        for(int s = S ; s >= obj[i].se ; s--) {
            maximize(f[s] , f[s-obj[i].se] + obj[i].fi);
        }
    }
    for(int s = 1 ; s <= S ; s++) maximize(f[s] , f[s-1]);
    cout << f[S];
    return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'void File(std::string)':
knapsack.cpp:26:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |     freopen((NAME + ".inp").c_str() , "r" , stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:27:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |     freopen((NAME + ".out").c_str() , "w" , stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...