二维背包
f[i][j]:i忍耐度,j个怪物可以得到的最大经验值。
f[i][j] = f[i-c[i]][j-1] + d[i];
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <deque>
#include <cstring>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cstdlib>
#include <iomanip>
using namespace std;
#define rep(i, a, b) for( i = (a); i <= (b); i++)
#define reps(i, a, b) for( i = (a); i < (b); i++)
#define pb push_back
#define ps push
#define mp make_pair
#define CLR(x,t) memset(x,t,sizeof x)
#define LEN(X) strlen(X)
#define F first
#define S second
#define Debug(x) cout<<#x<<"="<<x<<endl;
const double euler_r = 0.57721566490153286060651209;
const double pi = 3.141592653589793238462643383279;
const double E = 2.7182818284590452353602874713526;
const int inf=~0U>>1;
const int MOD = int(1e9) + 7;
const double EPS=1e-6;
typedef long long LL;
/*
f[i][j]:i忍耐度,j个怪物可以得到的最大经验值。
f[i][j] = f[i-c[i]][j-1] + d[i];
*/
int ans, n, m, k, s, f[105][105], i, j, h, c[105], d[105];
int main()
{
while(cin >> n >> m >> k >> s)
{
ans = -1;
rep(i, 1, k) cin >> c[i] >> d[i];
CLR(f, 0);
rep(i, 1, m)
{
rep(j, 1, s)
{
rep(h, 1, k)
{
if(i >= d[h]) f[i][j] = max(f[i][j], f[i-d[h]][j-1] + c[h]);
if(f[i][j] >= n) {ans = m - i; goto Z;}
}
}
}
Z:cout << ans << endl;
}
return 0;
}