Submission #1253366

#TimeUsernameProblemLanguageResultExecution timeMemory
1253366thanhcodedaoKnapsack (NOI18_knapsack)C++17
73 / 100
1087 ms2868 KiB
/****ThanhCodeDao*****/
/*******Azazel*******/
#include<bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define pb push_back
#define checkbit(mask,i) ((mask>>i)&1)
#define bit(i) (1<<i)
#define MLog 21
typedef long long ll;
const ll maxN = 1e5+3, modd = 1e9+7;
struct Point{
    ll x,y;
};
ll cross(Point p,Point q,Point r){ // vi tri r voi duong pq >0 la ben trai, =0 la thang hang
    ll val = (q.x-p.x)*(r.y-q.y) - (q.y-p.y)*(r.x-q.x);
    if(val < 0) return -1;
    if(val > 0) return 1;
    return 0;
}
ll dot(Point vecA,Point vecB){ // tra ve >0 la nhon, <0 la tu, =0 la vuong, 2 vecto phai chung goc
    ll val = vecA.x*vecB.x + vecA.y*vecB.y;
    if(val < 0) return -1;
    if(val > 0) return 1;
    return 0;
}
double triarea(Point p,Point q,Point r){ // cross(pq * pr) / 2
    double cross = (q.x-p.x)*(r.y-q.y) - (q.y-p.y)*(r.x-q.x);
    return fabs(cross)/2.0;
}
ll f[2003];
int s,n;
ll v[maxN], w[maxN],k[maxN];
// knapsack vo han su dung nhi phan hoa de toi uu, dpt n*log2(k)*s (co the giam nhieu vi s >= k*w)
void solve() {
    cin >> s >> n;
    for(int i = 1;i<=n;i++){
        cin >> v[i] >> w[i] >> k[i];
    }
    for(int i = 1;i<=n;i++){
        for(int j = 1;k[i] > 0; j <<= 1){
            ll get = min(k[i],1ll*j); // vd 4 thi sau khi lay 2^0 va 2^1 van con 2^0 co the lay
            k[i] -= get;
            ll val = get * v[i], cost = get * w[i];
            for(int x = s;x>=cost;x--){
                f[x] = max(f[x],f[x-cost] + val);
            }
        }
    }
    ll ans = 0;
    for(int i = 1;i<=s;i++) ans = max(ans,f[i]);
    cout << ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    //freopen("inp.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    solve();
    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...