Cod sursa(job #3332385)

Utilizator robertcosacCosac Robert-Mihai robertcosac Data 6 ianuarie 2026 14:43:09
Problema Zebughil Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <bits/stdc++.h>
#define int long long
using namespace std;
ifstream f("zebughil.in");
ofstream g("zebughil.out");
struct elem
{
    int camioane, greutate;
};
elem dp[300009];
int v[30];
int p[22];
const int maxlim=1e9;
signed main ()
{
    p[0]=1;
    for (int i=1; i<=19; i++)
        p[i]=2*p[i-1];
    for (int nrtest=1; nrtest<=3; nrtest++)
    {
        int n, gmax;
        f >> n >> gmax;
        for (int i=1; i<=n; i++)
            f >> v[i];
        for (int i=0; i<=(1<<n); i++)
            dp[i]= {2*maxlim, 2*maxlim};
        dp[1]={1, 0};
        for (int stare=1; stare<p[n]; stare++)
        {
            for (int j=1; j<=n; j++)
            {
                if ((stare&p[j])==0)
                {
                    if (dp[stare].greutate+v[j]>gmax)
                    {
                        dp[stare|p[j]].camioane=dp[stare].camioane+1;
                        dp[stare|p[j]].greutate=v[j];
                    }
                    else
                    {
                        dp[stare|p[j]].camioane=dp[stare].camioane;
                        dp[stare|p[j]].greutate=dp[stare].greutate+v[j];
                    }
                }
            }
        }
        g << dp[p[n]-1].camioane << '\n';
    }
}