Cod sursa(job #2669470)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 7 noiembrie 2020 06:53:10
Problema Zebughil Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("zebughil.in");
ofstream fout("zebughil.out");

const int inf = 1e9;
int n, g, v[20], dp[(1 << 17) + 4][505];

int solve(int mask, int c){
    if (dp[mask][c]) return dp[mask][c];
    if (mask == 0) return 0;
    int a = inf, b = inf;
    if (c != 0) a = 1 + solve(mask, 0);
    for (int j = 0; j < n; ++j){
        if (mask & (1 << j) && c + v[j + 1] <= g){
            b = min(b, solve(mask - (1 << j), c + v[j + 1]));
        }
    }
    return dp[mask][c] = min(a, b);
}

int main(){
    for (int t = 1; t <= 3; ++t){
        fin >> n >> g;
        for (int i = 1; i <= n; ++i){
            fin >> v[i];
        }
        fout << solve((1 << n) - 1, 0) + 1 << "\n";
        memset(dp, 0, sizeof dp);
    }
    fout.close();
    fout.close();
    return 0;
}