Cod sursa(job #3325705)

Utilizator MegaCoderMinoiu Teodor Mihai MegaCoder Data 26 noiembrie 2025 09:52:37
Problema Zebughil Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.13 kb
#include<fstream>
#include<vector>
#define INF 999999999999
#define ll long long
std::ifstream fin("zebughil.in");
std::ofstream fout("zebughil.out");
const int NMAX=19;
std::vector<std::pair<ll, ll>> dp(1<<NMAX);
void solve()
{

    dp=std::vector<std::pair<ll, ll>>(1<<NMAX, {INF, 0});
    int n, g;
    fin>>n>>g;
    std::vector<int>v(n);
    for(int i=0; i<n; ++i)
        fin>>v[i];

    dp[0]={1, 0};
    for(int msk=0; msk<(1<<n); ++msk)
    {
        for(int bit=0; bit<n; ++bit)
        {
            if(!(msk&(1<<bit)))
            {
                int newMask=msk|(1<<bit);
                ll cntCand=dp[msk].first, newSum=dp[msk].second;
                if(dp[msk].second+v[bit]>g)
                {
                    ++cntCand;
                    newSum=v[bit];
                }
                else
                    newSum+=v[bit];
                if(cntCand<dp[newMask].first || (cntCand==dp[newMask].first && newSum<dp[newMask].second))
                    dp[newMask]={cntCand, newSum};
            }
        }
    }
    fout<<dp[(1<<n)-1].first<<'\n';
}
int main()
{
    int t=3;
    while(t--)
        solve();
    return 0;
}