Pagini recente » Cod sursa (job #495914) | Cod sursa (job #1618111) | Cod sursa (job #1731533) | Cod sursa (job #842690) | Cod sursa (job #2488774)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int Nmax = 20;
int v[Nmax];
int n, G;
int dp[1 << Nmax];
int solve(){
int sum,temp;
bool ok;
memset(dp,0,sizeof(dp));
for(int i, substep, step = 0; step < (1 << n); ++step){
sum = 0;
ok = true;
for(i = 1; i <= n and ok; ++i)
if(step & (1 << (i - 1))){
if(1LL* sum + 1LL* v[i] <= 1LL*G)
sum += v[i];
else
ok = false;
}
if(ok)
dp[step] = 1;
else{
dp[step] = INT_MAX;
for(substep = (step - 1) & step; substep; substep = ((substep - 1) & step)){
temp = dp[substep] + dp[step - substep];
if(temp < dp[step])
dp[step] = temp;
}
}
}
return dp[(1 << n) - 1];
}
int main(){
ifstream cin("zebughil.in");
ofstream cout("zebughil.out");
for(int t = 1; t <= 3; ++t){
cin >> n >> G;
for(int i = 1; i <= n; ++i)
cin >> v[i];
cout << solve() << '\n';
}
return 0;
}