Submission #999427

#TimeUsernameProblemLanguageResultExecution timeMemory
999427AdiKnapsack (NOI18_knapsack)C++14
100 / 100
298 ms5068 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <class T>
using indexed_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
const long long M = 1000000007;
#define multiply(a, b) ((a % M) * (b % M)) % M
#define all(a) (a).begin(), (a).end()
#define ll long long
#define ld long double
#define max(a, b) (a > b ? a : b)
#define loop(i, a, b) for (long long i = a; i < b; i++)
#define rloop(i, a, b) for (int i = a; i >= b; i--)
#define logarr(arr, a, b) for (int i = a; i < b; i++) cout << arr[i] << " "; cout << endl
#define vi vector<int>
#define pi pair<int, int>
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define p push
#define po pop
#define gcd(a, b) __gcd(a, b)
#define yes "YES\n"
#define no "NO\n"

// find_by_order -> iterator to  value
// order_of_key -> index where value is/will be
long long binary_expo(long long x, long long n) {
    if (n == 0) return 1;
    if (n == 1) return x % M;
    long long temp = binary_expo(x, n / 2);
    temp = multiply(temp, temp);
    if (n % 2) temp = multiply(temp, x);
    return temp;
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(NULL);
    ll n, s;
    cin >> s >> n;
    vector<priority_queue<pair<ll, ll>>> trk(s + 1);
    ll x, y, z;
    loop(i, 0, n) {
        cin >> x >> y >> z;
        trk[y].push(make_pair(x, z));
    }
    vector<ll> prev(s + 1, LLONG_MIN), nxt(s + 1, LLONG_MIN);
    prev[0] = 0; // Initialization for the base case
    ll p = -1;
    vector<pair<ll, ll>> vec;
    for (ll ind = 1; ind <= s; ind++) {
        if (trk[ind].empty()) continue;
        nxt = prev; // Start with the current state of prev
        vec.clear(); // Clear vec for each track
        while (!trk[ind].empty()) {
            vec.push_back(trk[ind].top());
            trk[ind].pop();
        }
        ll sz = vec.size();
        for (ll sum = 0; sum <= s; sum++) {
            ll notake = prev[sum];
            ll take = LLONG_MIN;
            ll sm = 0, prof = 0, i = 0;
            while (i < sz && sum - sm >= 0) {
                ll val = vec[i].first;
                ll time = vec[i].second;
                prof += val;
                sm += ind;
                while (time-- && sum - sm >= 0) {
                    if (prev[sum - sm] != LLONG_MIN) {
                        take = max(take, prof + prev[sum - sm]);
                    }
                    prof += val;
                    sm += ind;
                }
                prof -= val;
                sm -= ind;
                i++;
            }
            nxt[sum] = max(notake, take);
        }
        prev = nxt; // Move to the next state
    }
    ll maxprof = 0;
    for (ll i = 0; i <= s; i++) {
        if (prev[i] != LLONG_MIN) maxprof = max(maxprof, prev[i]);
    }
    std::cout << maxprof << endl;

    return 0;
}

Compilation message (stderr)

knapsack.cpp: In function 'int main()':
knapsack.cpp:23:11: warning: unused variable 'push' [-Wunused-variable]
   23 | #define p push
      |           ^~~~
knapsack.cpp:52:8: note: in expansion of macro 'p'
   52 |     ll p = -1;
      |        ^
#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...