Submission #1145379

#TimeUsernameProblemLanguageResultExecution timeMemory
1145379bluevioletKnapsack (NOI18_knapsack)C++20
100 / 100
45 ms1608 KiB
#include             <bits/stdc++.h>
#define             ll   long long
#define            pll   pair<ll, ll>
#define            pii   pair<int, int>
#define          io(x)   if (fopen(x".inp","r")) {freopen(x".inp","r",stdin),freopen(x".out","w",stdout);}
#define      mem(c, x)   memset(c, x, sizeof(c))
#define         all(c)   c.begin(), c.end()
#define       bit(i,j)   ((i >> j) & 1)
#define             pb   push_back
#define             se   second     
#define             fi   first
#define             el   '\n'
using namespace std;  

template<class T> bool   maximize(T &a, const T &b) { return (a < b ? a = b, 1 : 0); }
template<class T> bool   minimize(T &a, const T &b) { return (a > b ? a = b, 1 : 0); }

int dx[8] = {0, 1, 0,-1, 1, 1,-1,-1};
int dy[8] = {1, 0,-1, 0, 1,-1,-1, 1};
const int  maxn  = 2e3 + 1;
const int  Inf   = 2e9 + 7;
const ll   Infll = 1e18 + 9;
const ll   Mod   = 1e9 + 7;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
ll dp[2][maxn];    
int n, s;
vector<pii> WeightGroup[maxn];

void solve() {
    cin >> s >> n;
    for (int i=1; i<=n; i++) {
        int v, w, sl; cin >> v >> w >> sl;
        if (w <= s && sl) WeightGroup[w].pb({v, sl});    
    }

    int step = 1;
    for (int w=1; w<=s; w++) {
        sort(all(WeightGroup[w]), greater<pii>());
        int num = 0;
        ll sum = 0;
        for (auto item : WeightGroup[w]) {
            for (int i=1; i<=item.se; i++) {
                if (1ll * (num+1) * w > s) break;
                num++;
                sum += item.fi;
                for (int j=s; j>=num * w; j--) {
                    maximize(dp[step & 1][j], dp[1 - (step & 1)][j]);
                    maximize(dp[step & 1][j], dp[1 - (step & 1)][j - num * w] + sum); 
                }
            }
        }
        if ((int)WeightGroup[w].size() >= 1) step++;
    }


   
    cout << dp[1 - (step & 1)][s];

}

signed main() {
    
    ios_base::sync_with_stdio(false);  cin.tie(0); cout.tie(0); 
    io("task"); 

    solve();
    
    return (0 ^ 0);
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:5:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define          io(x)   if (fopen(x".inp","r")) {freopen(x".inp","r",stdin),freopen(x".out","w",stdout);}
      |                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~
knapsack.cpp:64:5: note: in expansion of macro 'io'
   64 |     io("task");
      |     ^~
knapsack.cpp:5:85: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define          io(x)   if (fopen(x".inp","r")) {freopen(x".inp","r",stdin),freopen(x".out","w",stdout);}
      |                                                                              ~~~~~~~^~~~~~~~~~~~~~~~~~~~
knapsack.cpp:64:5: note: in expansion of macro 'io'
   64 |     io("task");
      |     ^~
#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...