Cod sursa(job #3172837)

Utilizator MrPuzzleDespa Fabian Stefan MrPuzzle Data 21 noiembrie 2023 12:53:25
Problema Zebughil Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <bits/stdc++.h>

#define INF 9

using namespace std;

ifstream f("zebughil.in");
ofstream g("zebughil.out");

int n,s;
int v[20];

struct{
    int s = 0;
    int k = INF;
}dp[1<<18];

void solve(){

    f>>n>>s;
    for(int i=0;i<n;i++){
        f>>v[i];
    }

    for(int l = 0;l<(1<<n);l++){
        dp[l] = {0,INF};
    }

    dp[0] = {0,1};
    for(int l=1;l<(1<<n);l++){
        for(int j=0;j<n;j++){
            if(l & (1<<j)){
                int l2 = l - (1<<j);

                if(dp[l2].s + v[j] <= s){
                    if( (dp[l2].k < dp[l].k) ||
                        (dp[l2].k == dp[l].k && dp[l2].s + v[j] < dp[l].s)){

                        dp[l].k = dp[l2].k;
                        dp[l].s = dp[l2].s + v[j];
                    }
                }else{
                    if( (dp[l2].k + 1 < dp[l].k) ||
                        (dp[l2].k + 1 == dp[l].k && v[j] < dp[l].s)){

                        dp[l].k = dp[l2].k + 1;
                        dp[l].s = v[j];
                    }
                }
            }
        }
    }
    g<<dp[(1<<n)-1].k<<'\n';
}

int main()
{
    int t = 3;
    while(t--){
        solve();
    }
    return 0;
}