Submission #923825

#TimeUsernameProblemLanguageResultExecution timeMemory
923825KarootKnapsack (NOI18_knapsack)C++17
73 / 100
1058 ms28276 KiB
#include <iostream>
#include <cmath>
#include <unordered_map>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <iomanip>
#include <algorithm>

#define all(x)  (x).begin(), (x).end()
#define rall(x)  (x).rbegin(), (x).rend()

using namespace std;

typedef long long ll;

ll linf = 1e15+1;

inline void scoobydoobydoo(){
    ios::sync_with_stdio(false);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
}

ll s, n;
const int MAXN = 2e3+1;
vector<pair<ll, ll> > items[MAXN];
pair<ll, ll> elems[MAXN*MAXN]; // value, weight
int counter = 0;
ll mem[MAXN*30][MAXN];

ll dp(int index, int weight){
    if (index == counter || weight == 0)return 0;
    if (mem[index][weight])return mem[index][weight];
    return mem[index][weight] = max((elems[index].second <= weight ? dp(index+1, weight-elems[index].second)+elems[index].first : 0), dp(index+1, weight));
}

int main(){
    scoobydoobydoo();
    cin >> s >> n;
    for (int i = 0; i < n; i++){
        int v, w, k; cin >> v >> w >> k;
        if (w > s)continue;
        items[w].push_back({v, k});
    }

    for (int i = 1; i < MAXN; i++)sort(rall(items[i]));

    for (int i = 1; i < MAXN; i++){
        int how = 0;
        for (int j = 0; j < items[i].size() && how < s/i; j++){
            int val = items[i][j].first;
            int k = items[i][j].second;
            for (int l = 0; l < k && how+l < s/i; l++){
                elems[counter++] = {val, i};
            }
            how += k;
        }
    }

    cout << dp(0, s) << endl;


    return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:53:27: 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]
   53 |         for (int j = 0; j < items[i].size() && how < s/i; j++){
      |                         ~~^~~~~~~~~~~~~~~~~
#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...