https://www.codechef.com/LICO2020/problems/DCANDY

https://www.codechef.com/LICO2020/problems/DCANDY
my submission link i am getting the wrong answer
https://www.codechef.com/viewsolution/34648917

don’t write to much conditions just use min function to get the minimum value among 3 & first of all you need to find the n%3 if it’s 0 then the loop will continue to 0 otherwise the loop will continue to n%3 & you didn’t sort the. array otherwise you won’t get the maximum output .Just try it

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ll t,n,i,s,p;
cin>>t;
while(t–){
cin>>n;
ll a[n];
s=0;
for(i=0;i<n;i++){
cin>>a[i];
}
if(n>=3){
sort(a,a+n);
p=n%3;
for(i=n-1;i>=p;i=i-3){
s=s+min(min(a[i],a[i-1]),a[i-2]);
}
cout<<s<<endl;
}
else
cout<<“0”<<endl;
}
return 0;
}

1 Like

thanks bro

:+1:t3:

Just sort the array in decreasing order and run a loop for every index with multiple of 3, since 3 candies were to be chosen. So, if your indexing is from 1, you have to sum the elements of index 3,6,9,…till last multiple of 3<=n. Here is my solution:

https://www.codechef.com/viewsolution/34645423

1 Like