Cod sursa(job #3163907)

Utilizator andiRTanasescu Andrei-Rares andiR Data 1 noiembrie 2023 17:07:41
Problema Zebughil Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <iostream>
#include <fstream>
#include <bitset>

#define fi first
#define se second
using namespace std;
ifstream fin ("zebughil.in");
ofstream fout ("zebughil.out");
const int Nmax=17, inf=(1<<30);

int n, g, v[Nmax];
pair<int, int> dp[(1<<Nmax)]; 

int main(){
    int t=3;
    while (t--){
        fin>>n>>g;
        for (int i=0; i<n; i++)
            fin>>v[i];

        for (int i=0; i<(1<<n); i++)
            dp[i]={inf, inf};
        dp[0]={0, 0};
        for (int i=0; i<(1<<n); i++){
            for (int j=0; j<n; j++)
                if (i | (1<<j) != i){
                    pair<int, int> crt=dp[i];
                    if (crt.se+v[j]>g){
                        crt.fi++;
                        crt.se=v[j];
                    }
                    else crt.se+=v[j];
                    dp[i | (1<<j)]=min(dp[i | (1<<j)], crt);
                }
        }
        if (dp[(1<<n)-1].se>0)
            dp[(1<<n)-1].fi++;
        fout<<dp[(1<<n)-1].fi<<'\n';
    }
    
}