Submission #1182428

#TimeUsernameProblemLanguageResultExecution timeMemory
1182428M0stafaKnapsack (NOI18_knapsack)C++20
12 / 100
0 ms328 KiB
#pragma GCC optimize("Ofast")
#pragma GCC optimize("avx2")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define int long long
using namespace std;
#define all(x) x.begin(), x.end()
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
template<typename T> bool smin(T& a, const T& b) { if (b < a) { a = b; return true; } return false;}
template<typename T> bool smax(T& a, const T& b) { if (a < b) { a = b; return true; } return false;}
template <class A, class B> ostream &operator<<(ostream &out, const pair<A, B> &a){return out << a.first << " " << a.second;}
template<typename T> istream &operator>>(istream &in, vector<T> &v) { for (auto &x: v) in >> x; return in; }
template<typename T>ostream &operator<<(ostream &out, const vector<T> &v) { for (const T &x: v) out << x << ' ';return out;}
template <class K, class V>
using ht = gp_hash_table<K, V, hash<K>, equal_to<K>, direct_mask_range_hashing<>, linear_probe_fn<>, hash_standard_resize_policy<hash_exponential_size_policy<>, hash_load_check_resize_trigger<>, true>>;
#ifndef ONLINE_JUDGE
#define dout(...) cerr << "Line:" << __LINE__ << " [" << #__VA_ARGS__ << "] = ["; _print(__VA_ARGS__)
#else
#define dout(...)
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int gen_random(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}
void pbit(int a) {
    cout << a << " : " << bitset<40>(a) << endl;
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int S, n;
    cin >> S >> n;

    map<int, vector<pair<int, int>>> a;
    for (int i = 0; i < n; i++) {
        int v, w, k;
        cin >> v >> w >> k;
        if (w <= S and k) a[w].push_back({v, min(S / w, k)});
    }

    int dp[2][S + 1]{}, idx = 1;
    for (auto [w, itms] : a) {
        sort(all(a[idx]), greater<>());
        for (int W = 0; W <= S; W++) {
            dp[idx & 1][W] = dp[1 - (idx & 1)][W];
            int copies = 0, types_at = 0, profit = 0;
            int cur_used = 0;
            while ((copies + 1) * w <= W and types_at < itms.size()) {
                profit += itms[types_at].first;
                copies++;
                dp[idx & 1][W] = max(dp[idx & 1][W], profit + dp[1 - (idx & 1)][W - (copies * w)]);
                cur_used++;
                if (cur_used == itms[types_at].second) {
                    types_at++;
                    cur_used = 0;
                }
            }
        }
        idx++;
    }
    int ans = 0;
    for (int W = 0; W <= S; W++) {
        ans = max({ans, dp[0][W], dp[1][W]});
    }
    cout << ans << endl;
    return 0;
}

Compilation message (stderr)

knapsack.cpp:2:28: warning: bad option '-favx2' to pragma 'optimize' [-Wpragmas]
    2 | #pragma GCC optimize("avx2")
      |                            ^
knapsack.cpp:12:48: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
   12 | template<typename T> bool smin(T& a, const T& b) { if (b < a) { a = b; return true; } return false;}
      |                                                ^
knapsack.cpp:13:48: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
   13 | template<typename T> bool smax(T& a, const T& b) { if (a < b) { a = b; return true; } return false;}
      |                                                ^
knapsack.cpp:14:82: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
   14 | template <class A, class B> ostream &operator<<(ostream &out, const pair<A, B> &a){return out << a.first << " " << a.second;}
      |                                                                                  ^
knapsack.cpp:15:67: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
   15 | template<typename T> istream &operator>>(istream &in, vector<T> &v) { for (auto &x: v) in >> x; return in; }
      |                                                                   ^
knapsack.cpp:16:73: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
   16 | template<typename T>ostream &operator<<(ostream &out, const vector<T> &v) { for (const T &x: v) out << x << ' ';return out;}
      |                                                                         ^
knapsack.cpp:25:35: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
   25 | inline int gen_random(int l, int r) {
      |                                   ^
knapsack.cpp:28:16: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
   28 | void pbit(int a) {
      |                ^
knapsack.cpp:31:13: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
   31 | signed main() {
      |             ^
#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...