TWINGFT - Editorial

sir ,please check my this code …i use priority_queue to solve this question in which i alternately let the chef and his twins to take their gifts . i Also passed sample case very clearly. but on submission showing WA, :frowning: please sir have a look.

can anyone Please point out what is wrong in my logic.
#include
#include
using namespace std;

int main() {
int t;
cin>>t;
while(t–)
{
int n,k;
cin>>n>>k;
int prices[n];
for(int i=0;i<n;i++)
{
cin>>prices[i];
}
int i = 1;
sort(prices,prices+n,greater());
int idx =0;
int chef1 =0;
int chef2 =0;
while(i<=2*k+1)
{

        if(i%2!=0 and i!=2*k+1){
        chef1 += prices[idx];
        }
        if(i%2==0 or i==2*k+1)
        {
            chef2 += prices[idx];
        }
        i++;
        idx++;
    }
    cout<<max(chef1,chef2)<<"\n";
}
return 0;

}

Don’t run the loop till 2*k + 1 rather run the loop till 2k and add the last gift to the second guy because it is said that last guy will recieve two gifts.Just try to take the maximum out of two gifts that will be the one chef chooses.

But I am running the loop from i = 1 not i =0 that’s why I have run it until 2k+1

and for the last gift I had put a or condition if (it’s a last gift then add it to the chef2).

But I am running the loop from i = 1 not i =0 that’s why I have run it until 2k+1

here is the code without using Array. Runs perfectly but shows NZEC error on submitting help at int t =sc.nextInt();.

`
class Codechef{

public static void main (String[] args) throws java.lang.Exception
{   
    	Scanner sc= new Scanner(System.in);
        int t =sc.nextInt();
    
    while(t>0){
    	int n,turns,opt1,opt2,cost;
    	n=sc.nextInt();
    	turns=sc.nextInt();
    	opt1=0;
    	opt2=0;
    	while(turns>0){
    		cost=sc.nextInt();
    		opt1+=cost;
    		cost=sc.nextInt();
    		opt2+=cost;
    		--turns;
    	}
    	if(n%2!=0){
    		cost=sc.nextInt();
    		opt2+=cost;
    	}
    	if(opt1>opt2)
    	    print(opt1);
    	else
    	    print(opt2);
     --t;
    }
	// your code goes here
}
static void print(int x){
   System.out.println(x);
}

}
`

may lunchtime - question - birthday gift | TWINGFT

My codes run perfectly, with all test cases right, and with the time constraints and i even checked the various different test cases with codechef official editorial code. and the results are matching perfectly… can anyone help me why is it still showing the Wrong Answer… please …

here is the code : -

t =int(input())
while(t>0):
t -= 1
n,k = map(int,input().split())
a = [n]
a = list(map(int, input().split()))
a.sort()
a.reverse()
chef = 0
p = 0
for i in range(0,k2):
if(i == 0 and a[i]==a[i+1]):
p = -1
continue
elif(2
k-i==1 and p == -1):
chef += a[i] + a[i+1]
elif(i%2==1 ):
if(p == 0 or p == -1):
chef += a[i]
else:
p = -1
continue
elif(chef==0 or i%2==0):
chef += a[i]
p = 1
print(chef)

Hi!
I’m unable to understand what’s wrong with my code, getting WA.
Any help would be appreciated.
thanks,
https://www.codechef.com/viewsolution/47309925

Can you please tell what I did wrong, I have tested many cases but it said wrong as I submitted. Also, I have used max heap in this. (the code is in java)
https://www.codechef.com/viewsolution/47312260

This is where you got wrong we are not bothered about the order which is already given in the array.In the question it meant that in the last chance i.e in the 2k th chance the guy who gets that chance would choose 2 gifts.
Note:It is given in the question that both players play optimally so obviously the first guy would take the gift with more price that is the 0th gift.So take from i = 0 to 2k and give 2k + 1 th gift to second guy and then take the maximum among two and print the price
Try to read the question more carefully.

int main() {
int t;
cin>>t;
for(int i=1;i<=t;i++)
{
int n,x,k,count_1=0,count_2=0,sum_1=0,sum_2=0,temp;
cin>>n;
cin>>k;

  int a[n],b[n],c[n];
  for(int j=0;j<n;j++)
  {cin>>a[j];
  }
  for(int i=0;i<n;++i)
  {for(int j=i+1;j<n;j++)
     {if (a[i]<a[j])
       {
          temp=a[i];
          a[i]=a[j];
          a[j]=temp;
        }
     }
  }
 
     for(int i=0;i<2*k+1;i++)
  {
    if((i%2==0)&& i<2*k)
     { count_1++;
       b[count_1]=a[i];
      
       sum_1=sum_1+b[count_1];
     }
     else 
     {count_2++;
     c[count_2]=a[i];
     
     sum_2=sum_2+c[count_2];
     }
  }
  if(sum_1>sum_2)
  {cout<<sum_1<<endl;
  }
  else
  {
      cout<<sum_2<<endl;
  }  

}
return 0;
}
can I know what is wrong in this code?
The output matched when I tested with inputs given.

try long long int

ur welcome

u are supposed to give ur submission link :sweat_smile: , u gave wrong link , so i am unable to access

use long long instead of using int

#include
#include
using namespace std;
#define ll long long int
#define f(a,b) for(int i=a;i<b;++i)

int main() {
int t;
cin>>t;
while(t–)
{
int n,k;
cin>>n>>k;
ll a[n];
f(0,n) cin>>a[i];
sort(a,a+n,greater());
ll fsum=0,ssum=0;
f(0,2k-4){
fsum+=(a[2
i]);
ssum+=a[2i+1];
}
fsum+=(a[2
k-4]+a[2k-1]);
ssum+=(a[2
k-3]+a[2*k-2]);
cout<<max(fsum,ssum)<<endl;

}
return 0;
}
Can anyone tell me why i am getting garbage values for this code???

#include <stdio.h>

int main(void) {
// your code goes here
int t;
scanf("%d",&t);
while(t–)
{
int n,k,i,j,m;
scanf("%d %d",&n,&k);
int arr[n];
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}

    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(arr[i]<arr[j])
            {
                m=arr[i];
                arr[i]=arr[j];
                arr[j]=m;
            }
        }
    }
    
    long long int s1=0,s2=0;
    for(i=0;i<2*k;i++)
    {
        if(i%2==0)
          s1=s1+arr[i];
        else
          s2=s2+arr[i];
    }
    s2=s2+arr[i];
    
    if(s1>=s2)
    printf("%d\n",s1);
    else
    printf("%d\n",s2);
}
return 0;

}

why i am getting wrong answer on this code??
plzz help me to identify.