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

https://www.codechef.com/LICO2020/problems/DCANDY
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

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