제출 #847775

#제출 시각아이디문제언어결과실행 시간메모리
847775vjudge1Knapsack (NOI18_knapsack)C++17
73 / 100
1061 ms18408 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
#define FOR(i, a) for (int i=0; i<(a); i++)
#define all(x) x.begin(), x.end()
#define gcd __gcd
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fi first
#define se second
//const int dr[4] = {-1, 0, 1, 0}, dc[4] = {0, 1, 0, -1};

const int N = 2001;
ll dp[N];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int s, n; cin >> s >> n;
    vector<pll> vec;
    while(n--){
        ll v, w; int a; cin >> v >> w >> a;
        for(int i = 0; i <= 30; ++i){
            if(a >= (1 << i)){
                a -= (1 << i);
                if(w * (1 << i) <= s) 
                    vec.emplace_back(w * (1 << i), v * (1 << i));
            }
            else{
                if(a > 0 && w * a <= s) vec.emplace_back(w * a, v * a);
                break;
            }
        }
    }
    memset(dp, -1, sizeof dp);
    dp[0] = 0;
    for(pll &it : vec)
        for(int j = s; j >= it.fi; --j)
            if(dp[j - it.fi] != -1)
                dp[j] = max(dp[j], dp[j - it.fi] + it.se);
    cout << *max_element(dp, dp + s + 1);
    return 0;
}
#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...