Cod sursa(job #1686638)

Utilizator AlexNiuclaeNiculae Alexandru Vlad AlexNiuclae Data 12 aprilie 2016 12:45:24
Problema Zebughil Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <bits/stdc++.h>

using namespace std;

const int Nmax = 20;
const int Vmax = (1 << 17);
const int inf = 0x3f3f3f3f;

int n , gmax , i , j , conf;
int a[Nmax];
int dp[Vmax] , g[Vmax];

int main()
{
    freopen("zebughil.in","r",stdin);
    freopen("zebughil.out","w",stdout);

    for (int tests = 1; tests <= 3; ++tests)
    {
        scanf("%d %d", &n, &gmax);
        for (i = 0; i < n; ++i)
            scanf("%d", a + i);

        memset(dp , inf , sizeof(dp));
        memset(g , inf , sizeof(g));

        dp[0] = 1; g[0] = 0;
        for (conf = 1; conf < (1 << n); ++conf)
            for (j = 0; j < n; ++j)
                if (conf & (1 << j))
                {
                    int y = conf ^ (1 << j);

                    int actdp = dp[y] + (1LL * a[j] + 1LL * g[y] > 1LL * gmax);
                    int actg = (1LL * a[j] + 1LL * g[y] > 1LL * gmax) ? a[j] : g[y] + a[j];

                    if (actdp < dp[conf] || (actdp == dp[conf] && actg < g[conf]))
                        dp[conf] = actdp, g[conf] = actg;

                }

        printf("%d\n", dp[(1<<n)-1]);
    }

    return 0;
}