http://acm.hdu.edu.cn/showproblem.php?pid=1114 Piggy-Bank
#include <cmath>
#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define CLR(c,v) (memset(c,v,sizeof(c)))
template <typename _T>
_T Max(_T a,_T b){
return a<b?b:a;
}
template <typename _T>
_T Max(_T a,_T b,_T c){
return a<Max(b,c)?Max(b,c):a;
}
template <typename _T>
_T Min(_T a,_T b){
return a>b?b:a;
}
template <typename _T>
_T Min(_T a,_T b,_T c){
return a>Min(b,c)?Min(b,c):a;
}
const int inf = -(1<<30);
const int INF = (1<<30);
const int M = 1e6 + 10;
int max_cost;
int dp[M];
int main()
{
//freopen("in.txt","r",stdin);
int Ncase;
cin >> Ncase;
while(Ncase--){
int empty_box, max_weight , n;
cin >> empty_box >> max_weight >> n;
max_cost = max_weight - empty_box;
CLR(dp,-1);
for(int i = 0 ; i <= max_cost ; i++)
dp[i] = INF;
dp[0] = 0;
int ans = INF;
for(int i = 1; i <= n ; i++){
int cost,value;
cin >> value >> cost;
for(int j = cost ; j <= max_cost ; j++){
if(dp[j-cost] != -1 )
dp[j] = Min(dp[j],dp[j-cost]+value);
}
if(dp[max_cost] != -1)
ans = Min(dp[max_cost],ans);
}
if(ans == INF)
printf("This is impossible.\n");
else
printf("The minimum amount of money in the piggy-bank is %d.\n",ans);
}
return 0;
}