제출 #1262348

#제출 시각아이디문제언어결과실행 시간메모리
1262348hiepsimauhongKnapsack (NOI18_knapsack)C++20
100 / 100
29 ms5192 KiB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
const int64_t oo = 1e9;
#define int long long
//#define float long double

#define quickly ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define FOR(I, L, R) for(int I(L) ; I <= (int)R ; ++I)
#define FOD(I, R, L) for(int I(R) ; I >= (int)L ; --I)
#define FOA(I, A) for(auto I : A)
#define print(A,L,R) FOR(OK, L, R){if(A[OK]<=-oo||A[OK]>=oo)cout<<"- ";else cout<<A[OK]<<' ';}cout<<'\n';
#define prints(A) FOA(OK : A){cout<<OK<<' ';}cout << '\n';
#define printz(A,L,R) FOR(OK, 0, L){FOR(KO, 0, R){if(A[OK][KO]>-oo&&A[OK][KO]<oo)cout<<A[OK][KO]<<' ';else cout << "- ";} cout << '\n';}cout << '\n';
#define fs first
#define sd second
#define th second.second
#define ii pair<int,int>
#define iii pair<int, ii>
#define all(A) A.begin(), A.end()

const int N = 1e5 + 5;
const int M = 2005;
const int mod = 1e9;

int n, s;
int v[N], w[N], a[N];
int dp[2005];
vector<ii> st[2005];

void add(int vi, int wi, int sl){
        FOD(j, s, wi * sl){
                dp[j] = max(dp[j], dp[j - wi * sl] + vi * sl);
        }
}

void Binary_Grouping(int vi, int wi, int sl){
        int bit = 0;
        FOR(i, 0, 30){
                if(sl >> i & 1) bit = i;
        }

        FOR(i, 0, bit - 1){
                add(vi, wi, (1 << i));
        }

        int x = sl - (1 << bit) + 1;
        add(vi, wi, x);
}

signed main(){ quickly
        cin >> s >> n;

        FOR(i, 1, n){
                cin >> v[i] >> w[i] >> a[i];
                st[w[i]].push_back({v[i], a[i]});
        }
        memset(dp, -0x3f, sizeof dp);
        dp[0] = 0;

        FOR(x, 1, s){
                int cur = 0;
                sort(all(st[x]), greater<ii>());
                FOA(i, st[x]){
                        cur += i.sd;
                        Binary_Grouping(i.fs, x, i.sd);
                        if(cur > s / x) break;
                }
        }

        cout << *max_element(dp, dp + 1 + s);
}
#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...