完全背包问题,先记下代码
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 110
int main ()
{
int dp[N][N], n, m, k, s;
while (cin >> n >> m >> k >> s)
{
int ans = m;
memset (dp, 0, sizeof(dp));
while (k--)
{
int a, b;
cin >> a >> b;
for (int i=1; i<=s; i++)
for (int j=b; j<=m; j++)
{
dp[i][j] = max (dp[i][j], dp[i-1][j-b] + a);
if (dp[i][j] >= n) ans = min (ans, j);
}
}
if (dp[s][m] >= n) cout << m - ans << endl;
else cout << -1 << endl;
}
return 0;
}