Submission #1086673

#TimeUsernameProblemLanguageResultExecution timeMemory
1086673lemon4lifeKnapsack (NOI18_knapsack)C++17
100 / 100
56 ms36432 KiB
#include <bits/stdc++.h>
using namespace std; 

typedef long long ll;
typedef pair <ll, ll> pii;
#define fi first
#define se second

bool TURN(bool &X){ if (X) return 0; X = 1; return 1;}

template <class X, class Y> bool mini(X &x, const Y &y){ if (x > y){ x = y; return true;} return false;}
template <class X, class Y> bool maxi(X &x, const Y &y){ if (x < y){ x = y; return true;} return false;}

const int S = 2e3 + 5;

ll dp[S][S];

vector <pii> g[S];

void solve(){
    int s, n; cin >> s >> n;

    for (ll i = 1, v, w, k; i <= n; ++i){
        cin >> v >> w >> k;

        g[w].push_back({-v, k});
    }

    memset(dp, -0x3f, sizeof dp);

    dp[0][0] = 0;

    ll ans = 0;

    for (int i = 1; i <= s; ++i){
        sort(g[i].begin(), g[i].end());

        for (int j = s; j >= 0; --j){
            dp[i][j] = dp[i - 1][j];

            if (g[i].size() == 0) continue;

            ll y = 0, cur = g[i][0].second, sum = 0;

            for (int x = 1; j - x * i >= 0; ++x){
                sum -= g[i][y].first;

                maxi(dp[i][j], dp[i - 1][j - x * i] + sum);

                cur--;
                
                if (cur == 0){
                    if (y == g[i].size() - 1) break;

                    y++;
                    cur = g[i][y].second;
                }
            }

            maxi(ans, dp[i][j]);    
        }
    }

    cout << ans;
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen("BALO.INP", "r")){
        freopen("BALO.INP", "r", stdin);
        freopen("BALO.OUT", "w", stdout);
    }

    solve();
    
    return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'void solve()':
knapsack.cpp:53:27: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |                     if (y == g[i].size() - 1) break;
      |                         ~~^~~~~~~~~~~~~~~~~~
knapsack.cpp: In function 'int main()':
knapsack.cpp:70:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |         freopen("BALO.INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:71:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |         freopen("BALO.OUT", "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...