Cod sursa(job #2165577)

Utilizator RickSanchezRick Sanchez RickSanchez Data 13 martie 2018 12:43:31
Problema Zebughil Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("zebughil.in");
ofstream fout("zebughil.out");
const int nmax = 20;
int N,G;
int a[nmax];
int dp[(1 << 18) + 5][2];

inline void Read()
{
    fin >> N >> G;
    for(int i = 0; i < N; i++) fin >> a[i];
    cout << N << " " << G << "\n";
}

inline void Solve()
{
    int stare,j,M;
    M = (1 << N) - 1;
    for(stare = 1; stare <= M; ++stare)
        dp[stare][0] = N+1;
    dp[0][1] = G; dp[0][0] = 1;
    for(stare = 1; stare <= M; ++stare)
        for(j = 0; j < N; j++)
        {
            if(stare & (1 << j))
            {
                if(dp[stare][0] > dp[stare ^ (1 << j)][0])
                {
                    if (dp[stare ^ (1 << j)][1] - a[j] >= 0)
                    {
                        dp[stare][0] = dp[stare ^ (1 << j)][0];
                        dp[stare][1] = dp[stare ^ (1 << j)][1] - a[j];
                    }
                }
                if(dp[stare][0] > dp[stare ^ (1 << j)][0] + 1)
                {
                    dp[stare][0] = dp[stare ^ (1 << j)][0] + 1;
                    dp[stare][1] = G - a[j];
                }
                else if(dp[stare][0] == dp[stare ^ (1 << j)][0])
                    dp[stare][1] = max(dp[stare][1],dp[stare ^ (1 << j)][1] - a[j]);
            }
        }
    fout << dp[M][0] << "\n";
}



int main()
{
    int T = 3;
    while(T--)
    {
        Read();
        Solve();
    }
    return 0;
}