CHFICRM getting partial points

Can anyone figure out why this code is getting only partial points. problem : CHFICRM of june long challenge

#include<bits/stdc++.h>

using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];

	int c5=0,c10=0;
	bool flag=true;
	for(int i=0;i<n;i++)
	{
		if(a[i]==5)
		{
			c5++;
		}
		if(a[i]==10)
		{
			if(c5==0)
			{
				flag=false;
				break;
			}
			else
			{
				c10++;
				c5--;
			}
		}
		if(a[i]==15)
		{
			if(c5>=2)
			{
				c5=c5-2;
			}
			else if(c10>0)
			{
				c10--;
			}
			else
			{
				flag=false;
				break;
			}
		}
	}
	if(flag)cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
}

return 0;

}

Your code is right for subtask 1. The answer for subtask 2 is greedy. You have to choose a coin of denomination 10 first if the coin is 15. If you don’t have any coin of denomination 10, you must have at least 2 coins of denomination 5.

I am checking that here.
if(a[i]==15)
{
if(c5>=2)
{
c5=c5-2;
}
else if(c10>0)
{
c10–;
}
else
{
flag=false;
break;
}
}

Read my answer again. I said to choose a 10 coin first because that is optimal. You are checking for 5 coins first.

1 Like

oh right I am checking for 5 rs first… my bad
thanks