Moony and ICPC team | CodeChef

I still didnt get the question completely. Can someone explain why is it working?

#Program 1

 def fun(a):
    a.sort()
    return sum(a[-3:])

t=int(input())
for _ in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    ans=0
    a=[a[-1]]+a+[a[0]]
    for i in range(1,n+1):
        ans=max(ans,sum(a[i-1:i+2]))
    print(ans)

#program 2

def fun(a):
      a.sort()
      return sum(a[-3:])

t=int(input())
for _ in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    ans=0
    if(n<=5):
        a.sort()
        ans=sum(a[-3:])
    else:
        a=[a[-1]]+a+[a[0]]
        for i in range(1,n+1):
            ans=max(ans,sum(a[i-1:i+2]))
    print(ans)

Both solution gives AC. :sweat_smile:

Then what will be the ans for
7
5 0 10 0 6 0 12 ?

18

Why can’t it be 28 (also AC code gives ans 18)?

Because the edge people aren’t friends(12 and 10). 17 was a typo.

By edge you mean 1st and last indexed person ?

See edit above.

Was this mentioned in the problem statement?

10 and 12 are not at a distance of 1 or 2, so they aren’t friends.

the answer is 17
it’s just the maximum sum of three consecutive elements in a circular array
submission

it should be 28
Cant you take 10,6,12 as the team?

If n is equals to 3,4 or 5, answer would be sum of largest three values out of them as all elements are friend of each other.
If n >= 6, each time we can only take consecutive three elements.
But if n is 6, we need to consider one special case of (1,3,5) or (2,4,6) as these three are not consecutive elements but are friend of each other.

1 Like

#include
#include <stdio.h>
using namespace std;

int main() {
int t;
cin >> t;
for(int s=0;s<t;s++)
{
int n,temp=0,c=0;
cin >> n;
int a[n];
for(long long int i=0;i<n;i++)
{
cin >> a[i];
}
for(long long int i=0;i<n-2;i++)
{
c=a[i]+a[i+1]+a[i+2];
if(c>temp)
{
temp=c;
}

    }
    c=a[0]+a[n-2]+a[n-1];
    if(c>temp)
    {
        temp=c;
    }
    c=a[0]+a[1]+a[n-1];
    if(c>temp)
    {
        temp=c;
    }
    cout << temp;
    cout << "\n";
}
// your code goes here
return 0;

}

can anyone explain me whats wrong in my code its showing wrong answer.

All the three coders should be friends of each other. The coder having skill 12 isn’t friends with the one having skill 10 as they are not 2 places to the left or right of himself.

1 Like

Please include a link to your submission as it would allow us to debug it much more easily.

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

Hi,
Just changing the datatype from int to long long gives AC.
Here is the link of the modified solution : CodeChef: Practical coding for everyone

Hope you have a great day.

thank you so much.

Its circle right so last indexed and 1st indexed person can also be friends ,
So the answer should be 28 .
But my AC code is giving answer 18

In the problem statement they have not mentioned it should be consecutive .

I am confused :thinking::thinking::thinking::thinking: