Cod sursa(job #3175621)

Utilizator Vlad_NistorNIstor Vlad Vlad_Nistor Data 26 noiembrie 2023 09:35:00
Problema Zebughil Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <bits/stdc++.h>
using namespace std;

bool getBit(int i, int j){
    return (i >> j) & 1;
}

pair<int,int> dp[(1<<20)];
int a[30];

int main(void){
    ofstream cout("zebunghil.out");
    ifstream cin("zebunghil.in");
    int T =3;
    while(T--){
        int n,g;
        cin >> n >> g;
        for(int i= 1;i<=n;i++){
            cin >> a[i];
        }
        for(int i = 0;i< (1<< n);i++){
            dp[i].first = n + 1;
            dp[i].second = 0;
        }
        dp[0] = {1,0};
        for(int i = 0;i<(1 << n);i++){
            for(int j = 0;j< n; j++){
                if(getBit(i,j) == 0){
                    pair<int,int> aux = dp[i];
                    if(aux.second + a[j + 1] > g){
                        aux.second = a[j + 1];
                        aux.first++;
                    }else{
                        aux.second += a[j + 1];
                    }
                    dp[i | (1<<j) ] = min(dp[i | (1<<j) ], aux);
                }
            }
        }
        cout << dp[(1<<n) - 1].first << '\n';
    }
}