最近刷了不少的弱弱的背包题,但是背包九讲仅仅会了五讲,有空应该把剩下的四讲学学,结合那篇国家集训队论文。
vidw code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define maxn 210000000
int n, b, h[50], sum, dp[20000010], i, j;
int mini;
int max(int a, int b)
{
if (a > b) return a;
else return b;
}
int main()
{
while (cin >> n >> b)
{
sum = 0;
mini = maxn;
memset(h, 0, sizeof(h));
for (i = 1; i <= n; i++)
{
cin >> h[i];
sum += h[i];
}
for (i = 1; i <= n; i++)
for (j = sum; j >= h[i]; j--)
{
dp[j] = max(dp[j], dp[j - h[i]] + h[i]);
if (dp[j] >= b && dp[j] < mini)
mini = dp[j];
}
cout << mini - b << endl;
}
return 0;
}