LOGIN
  • Register
  • Forgot Password?

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
  • COMPETE
    • March Algorithm Challenge
    • February Algorithm Challenge
    • January Algorithm Challenge
    • December Algorithm Challenge
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Ranks
    • Tutorials
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • About Directi
    • Careers
Home » Problems (easy) » Paying up

Paying up

Problem code: MARCHA1

  • Submit
  • All Submissions

A tutorial for this problem is available here

The following problem appeared in the CodeChef March '09 Challenge

In the mysterious country of Byteland, everything is quite different from what you'd normally expect. In most places, if you were approached by two mobsters in a dark alley, they would probably tell you to give them all the money that you have. If you refused, or didn't have any - they might even beat you up.

In Byteland the government decided that even the slightest chance of someone getting injured has to be ruled out. So, they introduced a strict policy. When a mobster approaches you in a dark alley, he asks you for a specific amount of money. You are obliged to show him all the money that you have, but you only need to pay up if he can find a subset of your banknotes whose total value matches his demand. Since banknotes in Byteland can have any positive integer value smaller than one thousand you are quite likely to get off without paying.

Both the citizens and the gangsters of Byteland have very positive feelings about the system. No one ever gets hurt, the gangsters don't lose their jobs, and there are quite a few rules that minimize that probability of getting mugged (the first one is: don't go into dark alleys - and this one is said to work in other places also).

Input

The first line contains integer t, the number of test cases (about 100). Then t test cases follow. Each test case starts with n, the number of banknotes in your wallet, and m, the amount of money the muggers asked of you. Then n numbers follow, representing values of your banknotes. Your wallet does not hold more than 20 banknotes, and the value of a single banknote is never more than 1000.

Output

For each test case output a single line with the word 'Yes' if there is a subset of your banknotes that sums to m, and 'No' otherwise.

Example

Input:
5
3 3
1
1
1
5 11
1
2
4
8
16
5 23
1
2
4
8
16
5 13
1
5
5
10
10
20 132
17
6
4
998
254
137
259
153
154
3
28
19
123
542
857
23
687
35
99
999

Output:
Yes
Yes
Yes
No
Yes

 

Explanation: For example, in the last case you have to pay up, since: 6+3+123=132.


Date: 2009-03-17
Time limit: 7

s
Source limit: 50000
Languages: C C99 strict C++ PAS gpc PAS fpc JAVA NICE JAR C# C#2 NEM ST ASM D FORT ADA BASH PERL PYTH RUBY LUA ICON PIKE PHP SCM guile SCM qobi LISP sbcl LISP clisp HASK CAML CLPS PRLG WSPC BF ICK


  • Submit

Comments

  • Login or Register to post a comment.

Rohan Tahiliani - 31st May,2009 07:17:52.

I dunno wat d problem seems to be im gettin perfect output for the sample input given and iv even tested other cases.. y does it give wrong answer iv implemented all conditions like bank notes must be less than 20 and currency <=1000

Pratik Vakil - 26th Jun,2009 02:56:59.

My program is giving output perfectly fine. Still it is suggesting me as wrong answer. Unable to understand whats wrong.

Aniruddha - 26th Jun,2009 04:53:03.

The test cases are correct. You should spend some more time in going over the problem statement and trying to find corner cases where your code might fail.

Pratik Vakil - 28th Jun,2009 17:14:09.

@aniruddha. I have tried all possible test cases and its working fine from my side. Cant we get a copy of some of the test cases? Not all but some would definitely work out. And since its a practice session, it shouldnt matter much. Thanks

viswateja nelakuditi - 25th Jul,2009 15:49:49.

No matter how many test cases i try, this problem keeps on giving me wrong answer error !

Akash - 31st Jul,2009 00:51:53.

Initially I was getting a time limit exceed error. After some optimization it started giving run-time error while I have checked all the example test cases along with some other. May I know, which test case fails?

Stephen Merriman - 31st Jul,2009 12:44:26.

Nope, figuring that out yourself is the most important part of solving a problem :)

Atul Bansal - 2nd Aug,2009 04:45:52.

what could be the possible corner cases? is m=0 a valid proposition?
though i didnt get right answer even after allowing for m=0.

Atul Bansal - 2nd Aug,2009 04:47:32.

OH MY GOD. I WAS PRINTING YES IN PLACE OF Yes. SILLY

Nishant bulchandani - 17th Aug,2009 05:13:26.

please,provide someone provide details about dynamic programming.
regards

Directi Admin - 17th Aug,2009 05:17:26.

Yes, something to that effect will be put up soon :)

      Hi Guys, Am getting WA.

pravinth - 31st Aug,2009 11:43:46.

 

    Hi Guys, Am getting WA. donno wots the problem in my code. Can any of you help me find it out? Thanks. Here is my code,
      #include&lt;iostream&gt;
      #include &lt;algorithm&gt;
      #include &lt;cmath&gt;
      #include&lt;cstring&gt;
      #include &lt;list&gt;
      #include &lt;iterator&gt;
 
using namespace std;
 
int cal(int* a,int noc,int index)
{
int sum=0;
for(int i=0;i&lt;noc;i++)
{
if(index & (1&lt;&lt;i))
{
sum+=a[i];
}
}
return sum;
}
int main()
{
int t,i,j,noc,mon;
cin&gt;&gt;t;
while(t–)
{
bool found=false;
cin&gt;&gt;noc;
cin&gt;&gt;mon;
int c[noc];
for(i=0;i&lt;noc;i++) {cin&gt;&gt;c[i];}
int start=0,end=(int)pow(2,(float)noc);
while(start&lt;=end)
{
int val=cal(c,noc,start);
if(val==mon) {found=true;break;}
start++;
}
if(found) cout&lt;&lt;”Yes”;
else cout&lt;&lt;”No”;
}
//cin&gt;&gt;t;
return 0;
}
 

  1.        /*sorry guys..

pravinth - 31st Aug,2009 11:45:10.

 

1.        /*sorry guys.. here i replaced the &lt; with < and &gt with > */
#include<iostream>
#include <algorithm>
#include <cmath>
#include<cstring>
#include <list>
#include <iterator>
using namespace std;
int cal(int* a,int noc,int index)
{
int sum=0;
for(int i=0;i<noc;i++)
{
if(index & (1<<i))
{
sum+=a[i];
}
}
return sum;
}
int main()
{
int t,i,j,noc,mon;
cin>>t;
while(t–)
{
bool found=false;
cin>>noc;
cin>>mon;
int c[noc];
for(i=0;i<noc;i++) {cin>>c[i];}
int start=0,end=(int)pow(2,(float)noc);
while(start<=end)
{
int val=cal(c,noc,start);
if(val==mon) {found=true;break;}
start++;
}
if(found) cout<<”Yes”;
else cout<<”No”;
}
//cin>>t;
return 0;
}
 

The pow() function is not

Aniruddha (Codechef) - 31st Aug,2009 13:52:07.

The pow() function is not always very precise. You should probably use end = (1<<noc) instead of the function pow().

  Hi Drupal, Thanks for ur

pravinth - 2nd Sep,2009 11:01:51.

 

Hi Drupal, Thanks for ur reply but "end = (1<<noc)" did not fix it. still am getting WA.

                    Any idea ?

Well, for one, you appear to

Stephen Merriman - 2nd Sep,2009 13:46:46.

Well, for one, you appear to be printing YesYesYesNoYes for the sample input, rather than what you are meant to print.

oh my god... missing endl

pravinth - 2nd Sep,2009 14:59:51.

oh my god... missing endl made it - WA !!!

Thanks Stephen, Thank you very much.

After i put endl in the cout, it got accepted.

Thanks!

There is also a tutorial for

Gaurav Menghani - 2nd Sep,2009 15:39:31.

There is also a tutorial for this problem: blog.codechef.com/2009/07/09/tutorial-for-problem-paying-up/

How to handle the case when

trying2code - 6th Sep,2009 02:45:15.

How to handle the case when number of notes>20 or the following value is >1000 are we supposed to exit

The problem says the number

Stephen Merriman - 6th Sep,2009 03:32:28.

The problem says the number of banknotes will be <= 20  and the value is <= 1000.

It doesn't say 'sometimes that is true, the rest of the time it isn't'..

@tryingtocode Such a case

Aniruddha (Codechef) - 7th Sep,2009 14:10:28.

@tryingtocode Such a case will not be there in the input

don't you think we should get

Mukul Srivastava - 23rd Sep,2009 08:19:27.
don't you think we should get the test case also with our code in the mail we get from codechef for which my solution failed because i am trying to figure out input for which my program failed and i am not able to find the test case . Can you please suggest some border testcases ?

Of course you shouldn't be

Stephen Merriman - 23rd Sep,2009 08:52:30.

Of course you shouldn't be emailed it, a large part of solving problems is figuring out why your code isn't working yourself :)

Also, if we did that, it

Aniruddha (Codechef) - 23rd Sep,2009 14:16:54.

Also, if we did that, it would be possible for people to hard code the test cases and output the answers. :)

What will be the output for

Anitha - 23rd Sep,2009 17:11:38.

What will be the output for the following 3 cases ?

a) 0 0

b) 0 13

c) 2 0

   1

   1

Please read the tutorial for

Aniruddha (Codechef) - 23rd Sep,2009 19:21:04.

Please read the tutorial for this problem

can the value of a banknote

ayan agrawal - 26th Sep,2009 13:32:48.

can the value of a banknote be 0 ?

hello guyz... take into

ayan agrawal - 26th Sep,2009 14:23:41.

hello guyz... take into consideration that the value of m(amount of money the mugger asks for) can also be 0... i got correct answer on including this condition

I've made multiple attempts

Casey - 11th Oct,2009 04:35:09.

I've made multiple attempts at this now, and the results seem very accurate to me. Anitha's advice was helpful. However, even accounting for these situations, it's still not certain what the answer SHOULD be in those cases. My program gives the following results to each condition you proposed:

a. Yes

b. No

c. Yes

I wrote the code for the

bezgin zor - 30th Oct,2009 19:36:10.

I wrote the code for the explained algorithm and i got a correct answer but for an input file where the answer is No for all test cases, my program runs in about 13 secs.

 

Thus the algorithm provided in the tutorial is not efficient enough, and the control mechanism is not strict enough. I shouldn't have gotten a 'correct answer' response by any means.

I am getting runtime error

Deepak Sharma - 25th Nov,2009 15:44:55.

I am getting runtime error everytime i uplaod the code ..

its running perfectly fine on my system

..

can anyone help me why i am getting runtime error ...

...

the code is as below...

-----------------------------------------------------


#include <stdio.h>
#include <stdlib.h>

int notes[100][20] = {0};
int mask[20] = {0};
int isSum(int caseNum,int sumReq,int notesAv);
int main()
{
int numCases;
int reqSum[20],availNotes[20];

int LoopCount,i;
int result;
scanf("%d",&numCases);

for(LoopCount=0; LoopCount<numCases; LoopCount++)
{
scanf("%d",&availNotes[LoopCount]);
scanf("%d",&reqSum[LoopCount]);
for(i=0; i<availNotes[LoopCount]; i++)
{
scanf("%d",&notes[LoopCount][i]);
}
}

mask[0] = 1;
for(i=1;i<20;i++)
{
mask[i] = mask[i-1]<<1;
}

for( LoopCount=0;LoopCount<numCases;LoopCount++)   
{
result = isSum(LoopCount,reqSum[LoopCount],availNotes[LoopCount]);
if(result == 1)
printf("Yesn",result);
else
printf("Non",result);
}
return 0;
}

int isSum(int caseNum,int sumReq,int notesAv)
{
int loopLim;
int i,j,sum;

//loopLim = pow(2,notesAv);
loopLim = 1<<notesAv;

for(i=1;i<loopLim;i++)
{
sum = 0;
for(j=0;j<notesAv;j++)
{
if((mask[j] & i) != 0)
sum = sum + notes[caseNum][j];
}
if(sum == sumReq)
return 1;
}
return 0;
}

It is not working perfectly

Stephen Merriman - 25th Nov,2009 16:04:28.

It is not working perfectly fine on your system. You just aren't testing it properly.

Have you tried any input with 100 test cases like the problem says will happen?

do we need to check the case

Ankit Dwivedi - 8th Dec,2009 10:30:02.

do we need to check the case when any of t,n or m is negative? if not, then can anyone please tell me wt's wrong with this code?

 

  1. #include<stdio.h>
  2. main()
  3. {
  4. int t,n,m,i,j,sum,flag;
  5. int val;
  6. scanf("%d",&t);
  7. if(t<=100)
  8. {
  9.   for(j=0;j<t;j++)
  10.   {
  11.    sum=0;  
  12.    flag=0;
  13.    scanf("%d %d",&n,&m);
  14.    if(n<=20)
  15.    {
  16.     for(i=0;i<n;i++)
  17.     {   
  18.      scanf("%d",&val);
  19.      if(val<=1000)
  20.      sum=sum+val;  
  21.      if(sum==m)
  22.      flag=1;
  23.     }
  24.     if(flag!=1)
  25.     printf("Non");
  26.     else
  27.     printf("Yesn");
  28.    }
  29.   }
  30. }
  31. return 0;
  32. }    

it's printf("No\n") and

Ankit Dwivedi - 8th Dec,2009 10:33:56.

it's printf("Non") and printf("Yesn") in the code that i have submitted...dont know what happened while copying it here...

i may have asked something

Ankit Dwivedi - 11th Dec,2009 19:21:55.

i may have asked something really silly...but someone please help..

Well, your code will

Stephen Merriman - 12th Dec,2009 05:31:53.

Well, your code will obviously not work if there are 101 test cases. It will also fail on most test cases; for example, if the correct sum is made up of the first and last numbers and nothing else.

i know...thank you so

Ankit Dwivedi - 15th Dec,2009 19:39:30.

i know...thank you so much...i misunderstood the problem...should have read the tutorial first...

can smone fnd error in my

Gaurav Gaba - 1st Jan,2010 01:59:40.

can smone fnd error in my code i dnt kno why it's giving wrong answer...............

pls

#include<stdio.h>

int main(void) {

int t,n,m;
int i,j,s,a[20];


scanf("%d",&t);

while(t--) {

scanf("%d %d",&n,&m);

for(i=0;i<n;i++)
scanf("%d",&a[i]);

for(i=1;i<(1<<n);i++) {

s=0;

for(j=0;j<n;j++)
if(i&(1<<j)>0)
s=s+a[j];

if(s==m) {
printf("YESn");
goto end;
}


}


printf("NOn");

end : continue;

}
return 0;
}

will there be test cases only

Dal Chand Choudhary - 15th Jan,2010 23:59:44.

will there be test cases only with n <= 20 or we have to take any 20 banknotes out of n ??

what does the Source limit

Shaurya Gupta - 22nd Jan,2010 01:22:46.

what does the Source limit 50000 means

is it 50000 byte or something else

@ Dal - the problem statement

Stephen Merriman - 22nd Jan,2010 02:12:01.

@ Dal - the problem statement tells you n can be at most 20.

@ Shaurya - yes.

  What's wrong in this

Pankaj - 22nd Jan,2010 09:46:17.

 

What's wrong in this procedure ???

int solve(int note[],int m,int i){
if(i<0) return 0;
if(m<note[i])return solve(note,m,i-1);
else return max(solve(note,m,i-1), note[i]+solve(note,m-note[i],i-1));
}

. . . . . . .

if(m == solve(note,m,n-1)) puts("YES");

else puts("NO");

oh!! sorry i figure out my

Pankaj - 22nd Jan,2010 10:03:33.

oh!! sorry i figure out my problem..

Tell me what's the problem

Dileepkumar - 9th Feb,2010 19:06:27.

Tell me what's the problem with my code,in my computer it is giving f9 output

#include<iostream.h>
int main()
{int ban,mug,test;
int a[20],i=0,p=0,l=0;int m[100];
cin>>test;
while(test)
{cin>>ban>>mug;
while(ban)
{cin>>a[i];
i++;
ban--;}
i--;
while(i+1)
{p=p+a[i];
i--;
}
if(p==mug)
m[l]=1;
else
m[l]=0;
test--;i=0;p=0;l++;}
i=l;
l=0;
while(l!=i)
{if(m[l]==1)
cout<<"Yes";
else
cout<<"No";
cout<<"n";
l++;}
return 0;
}

It is showing that my

Dileepkumar - 9th Feb,2010 19:08:23.

It is showing that my programme compiled successfully,but lacks the desired output

if we have a note that

Abhi K - 14th Feb,2010 17:11:12.

if we have a note that matches the value of m, then will be considered as the subset or is it necessary that the sum of a certain number of notes equals the value of m???

import

Rohit Kumar gupta - 8th Mar,2010 00:39:36.

import java.util.*;

 

public class PayingUp {

public static Scanner s=new Scanner(System.in);

public static void main(String[] args) {

int t;

t=s.nextInt();

ArrayList<String> res=new ArrayList<String>();

for(int i=0;i<t;i++) {

int n=s.nextInt();

int sum=s.nextInt();

int[] elements=new int[n];

for(int j=0;j<n;j++)

elements[j]=s.nextInt();

if(solve(elements,sum))

res.add("Yes");

else

res.add("No");

}

for(int i=0;i<res.size();i++)

System.out.println(res.get(i));

}

public static boolean solve(int[] arr,int sum) {

int n=(int)Math.pow(2,arr.length);

for(int i=1;i<=n;i++) {

int temp=0;

int k=1;

for(int j=arr.length-1;j>=0;j--) {

if((k&i)!=0)

temp+=arr[j];

k=k<<1;

}

if(temp==sum)

return true;

}

return false;

}

}

can anyone check, what's wrong in this code snippet? it's according to that tutorial.

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Fetching successful submissions

HELP

Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

 

  • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.
  • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
  • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.
  • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
  • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.
  • If you are still having problems, see a sample solution here.

  • About CodeChef
  • About Directi
  • CEO's Corner
  • Careers
  • feedback@codechef.com
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
Sponsors
The time now is: