Cod sursa(job #3332386)

Utilizator robertcosacCosac Robert-Mihai robertcosac Data 6 ianuarie 2026 14:49:15
Problema Zebughil Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.45 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;
elem minim (elem x, elem y)
{
    if (x.camioane<y.camioane) return x;
    if (x.camioane>y.camioane) return y;
    if (x.greutate<y.greutate) return x;
    return y;
}
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=0; i<n; i++)
            f >> v[i];
        for (int i=0; i<=(1<<n); i++)
            dp[i]= {2*maxlim, 2*maxlim};
        dp[0]={1, 0};
        for (int stare=0; stare<p[n]; stare++)
        {
            for (int j=0; j<n; j++)
            {
                if ((stare&p[j])==0)
                {
                    if (dp[stare].greutate+v[j]>gmax)
                    {
                        elem nou={dp[stare].camioane+1, v[j]};
                        dp[stare|p[j]]=minim(dp[stare|p[j]], nou);
                    }
                    else
                    {
                        elem nou={dp[stare].camioane, dp[stare].greutate+v[j]};
                        dp[stare|p[j]]=minim(dp[stare|p[j]], nou);
                    }
                }
            }
        }
        g << dp[p[n]-1].camioane << '\n';
    }
}