2056: Jojer's Story IV
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
3s | 16384K | 611 | 121 | Standard |
Input
Each test case will first give you two integers n(<500) the number of the games and m(<2000000) the money he wants to get exactly and follow by n integers to tell you the money he can win a special game each day.Output
You should just output the minimum days or impossible.Sample Input
2 250 100 50
Sample Output
3
Problem Source: fennec
This problem is used for contest: 11
#include<iostream>
#include<algorithm>
using namespace std;
const int inf=(1<<31)-199;
int n,goal,minc;
bool flag;
int a[600];
bool cmp(int x,int y)
{
return x>y;
}
void dfs(int money,int count ,int mark)
{
if(mark==n) {if(money==0&&count<minc) {minc=count;flag=true;} return ;}
if(count+ money/a[mark] >minc) return ;
if(money<a[mark]) dfs(money,count,mark+1);
int t=money/a[mark];
int i;
for(i=t;i>=0;i--)
{
dfs(money-a[mark]*i,count+i,mark+1);
}
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&goal)==2)
{
for(i=0;i<n;i++) scanf("%d",&a[i]);
sort(a,a+n,cmp);
flag=false;
minc=inf;
dfs(goal,0,0);
if(flag) printf("%d/n",minc);
else printf("impossible/n");
}
return 0;
}