-
Notifications
You must be signed in to change notification settings - Fork 0
/
PIGBANK.cpp
48 lines (47 loc) · 953 Bytes
/
PIGBANK.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<bits/stdc++.h>
using namespace std;
#define INF 6000000000
long long dp[10001];
long long wetval[10001];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
cin>>t;
while(t--)
{
long long e,f,n;
cin>>e>>f;
long long wt=f-e;
cin>>n;
long long i,j;
for(i=0;i<10001;i++)
{
dp[i]=INF;
wetval[i]=INF;
}
for(i=1;i<=n;i++)
{
long long x,y;
cin>>x>>y;
wetval[y]=min(x,wetval[y]);
}
dp[0]=0;
for(i=1;i<=10001;i++)
for(j=1;j<10001;j++)
{
if(wetval[j]!=INF)
{
if(j<=i)
dp[i]=min(dp[i],dp[i-j]+wetval[j]);
else
break;
}
}
if(dp[wt]!=INF)
cout<<"The minimum amount of money in the piggy-bank is "<<dp[wt]<<".\n";
else
cout<<"This is impossible.\n";
}
}