Cod sursa(job #3206768)

Utilizator Theo20067Cismaru Theodor-Alexe Theo20067 Data 24 februarie 2024 00:06:52
Problema Zebughil Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin ("zebughil.in");
ofstream fout("zebughil.out");
struct elem
{
    long long c,w;
}D[140001];
long long V[18],n,t,G,i,j,k;
int main()
{
    t=3;
    D[0].c=1;
    D[0].w=0;
    while(t--)
    {
        fin>>n>>G;
        for(i=1;i<=n;i++)
            fin>>V[i];
        for(i=1;i<(1<<n);i++)
        {
            D[i].c=n+1;
            D[i].w=0;
        }
        for(i=1;i<(1<<n);i++)
        {
            for(j=0;j<n;j++)
            {
                if((i>>j)&1)
                {
                    k=i-(1<<j);
                    if(D[k].w+V[j+1]<=G)
                    {
                        if(D[i].c>D[k].c||(D[i].c==D[k].c&&D[i].w>D[k].w+V[j+1]))
                        {
                            D[i].c=D[k].c;
                            D[i].w=D[k].w+V[j+1];
                        }
                    }
                    else
                    {
                        if(D[i].c>D[k].c+1||(D[i].c==D[k].c+1&&D[i].w>V[j+1]))
                        {
                            D[i].c=D[k].c+1;
                            D[i].w=V[j+1];
                        }
                    }
                }
            }
        }
        fout<<D[(1<<n)-1].c<<"\n";
    }
    return 0;
}