Cod sursa(job #2978438)

Utilizator robertanechita1Roberta Nechita robertanechita1 Data 13 februarie 2023 19:07:35
Problema Zebughil Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.02 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nMax = 20;
int n, G, a[nMax];
pair<int, int>dp[(1 << 18)];
///dp[stare] = {nrMin de camioane, gMax ramasa in ultimul camion}
/// 010101110


int main()
{
    for(int t = 1; t <= 3; t++){
        fin >> n >> G;
        for(int i = 0; i < n; i++)
            fin >> a[i];
        memset(dp, 0, sizeof(dp));
        dp[0] = {1, G};
        dp[1] = {1, G - a[1]};
        for(int stare = 0; stare <= ((1 << n) - 1); stare++){
            for(int j = 0; j < n; j++){
                if(!(stare & (1 << j))){
                    if(a[j] <= dp[stare].second)
                          dp[(stare | (1 << j))] = {dp[stare].first, dp[stare].second - a[j]};
                    else
                          dp[(stare | (1 << j))] = {dp[stare].first + 1, G - (a[j] - dp[stare].second)};
                }
            }

        }
        fout << dp[((1 << n) - 1)].first << "\n";
    }
    return 0;
}