Submission #515809

# Submission time Handle Problem Language Result Execution time Memory
515809 2022-01-19T18:46:52 Z LunaMeme Knapsack (NOI18_knapsack) C++14
12 / 100
1 ms 332 KB
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
typedef vector<pair<int, int>> vii;
typedef vector<int> vi;
typedef long long ll;
#define PB push_back
#define MP make_pair
#define FOR(i, x, y) for (int i = x; i < y ; i ++)

int main(){
    int s, n;
    cin >> s >> n;
    vector<pair<ll, ll>> weights[s + 1];
    FOR(i, 0, n){
        ll v, w, k;
        cin >> v >> w >> k;
        weights[w].PB(MP(v, k));
    }
    FOR(i, 0, s){
        sort(weights[i].rbegin(), weights[i].rend());
    }
    ll dp[s + 1] = { 0 };
    ll ans = 0;
        FOR(j, 1, s + 1){
            int indx = 0;
            ll prev = 0;
            FOR(cnt, 1, s/j + 1){
                for(int i = s; i >= 1; i --){
                    //cout << dp[s] << '\n';
                    if (j > i  || indx >= weights[j].size()){
                        break;
                    }
                    if (cnt - prev > weights[j][indx].second){
                        indx ++;
                        prev += weights[j][indx].second;
                    }
                    if (indx >= weights[j].size()){
                        break;
                    }
                    dp[i] = max(dp[i], dp[i - j] + weights[j][indx].first);
            }
        }
    }
    FOR(i, 1, s + 1){
        //cout << i << " : " << dp[i] << '\n';
        ans = max(ans, dp[i]);
    }
    cout << ans << '\n';

}

Compilation message

knapsack.cpp: In function 'int main()':
knapsack.cpp:31:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |                     if (j > i  || indx >= weights[j].size()){
      |                                   ~~~~~^~~~~~~~~~~~~~~~~~~~
knapsack.cpp:38:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |                     if (indx >= weights[j].size()){
      |                         ~~~~~^~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 288 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 296 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 296 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 288 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Incorrect 1 ms 296 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 288 KB Output is correct
2 Correct 1 ms 288 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Incorrect 1 ms 296 KB Output isn't correct
6 Halted 0 ms 0 KB -