Cod sursa(job #2935918)

Utilizator BlueLuca888Girbovan Robert Luca BlueLuca888 Data 7 noiembrie 2022 18:05:42
Problema Energii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast,unroll-loops")
#pragma GCC target ("avx2,popcnt")

using namespace std;

ifstream fin  ("energii.in");
ofstream fout ("energii.out");

const int MAX_N = 1005;
const int MAX_G = 5005;
int n, g, w, c, minn[MAX_G];

int main (){
    ios_base::sync_with_stdio(false);
    fin.tie(nullptr), fout.tie(nullptr);

    fin>>n>>g;

    for(int i=1; i<=g; i++)
        minn[i] = -1;

    for(int i=1; i<=n; i++){
        fin>>w>>c;
        for(int j=g-1, jj; j>=0; j--)
            if(minn[j] != -1){
                jj = min(g, j + w);
                minn[jj] = (minn[jj] == -1 ? minn[j] + c : min(minn[jj], minn[j] + c));
            }
    }

    fout<<minn[g];
    return 0;
}