제출 #1262338

#제출 시각아이디문제언어결과실행 시간메모리
1262338hiepsimauhongKnapsack (NOI18_knapsack)C++20
73 / 100
1093 ms2800 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 mod = 1e9;

int n, s;
int v[N], w[N], a[N];
int dp[2005];

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

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

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

        FOR(i, 1, n){
                int bit = 0;
                FOR(j, 0, 30){
                        if(a[i] >> j & 1) bit = j;
                }

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

                int x = a[i] - (1 << bit) + 1;
                add(i, x);
        }

        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...