DCANDY - Editorial

Problem Link : CodeChef: Practical coding for everyone

Distribution is such that from the three picked candies, GOVI gets the candy with the minimum sweetness. Now in order to get the maximum sweetness, the picking of candies should be done in such a way that she picks candies with maximum available sweetness.

Here is the code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int

bool cmp(int a, int b)
{
    return a > b;
}
int main(void)
{
    ll test;
    cin >> test;
    while (test--)
    {
        ll n;
        cin >> n;
        ll arr[n];
        for (int i = 0; i < n; i++)
        {
            cin >> arr[i];
        }
        sort(arr, arr + n, cmp);
        ll sweetness = 0;
        for (int i = 0; i < n; i++)
        {
            if ((i + 1) % 3 == 0)
            {
                sweetness += arr[i];
            }
        }
        cout << sweetness << endl;
    }
    return 0;
}
1 Like

CHOOSE EVERY THIRD ELEMENT FROM THE LAST AND ADD UP WE WILL GET THE DESIRED RESULT

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