# Moony and ICPC team | CodeChef

What should be the answer for test case
1
5
1 0 7 0 10

No shouldnt it be 18?
When you consider the coder with rating 7 he is friends with all others as there are 2 on his left side and 2 on his right.
So the team should consist of 1,7 and 10?

It’s 18. The largest 3 consecutive element sum only applies to n>6

But the output of accepted code comes 17

1 Like

Let me check.

@everule1 is right. Sorry for the inconvenience. I left the corner case.

``````for _ in range(int(input())):
n=int(input())
ans=0
seq=map(int,input().split())
seq=list(seq)
if(0):
for j in range(3):
ans+=max(seq)
seq.remove(max(seq))
else:
for i in range(n):
thisans=seq[i]+seq[(i+1)%n]+seq[i-1]
ans=max(ans,thisans)
print(ans)
``````

This passed There are 2 edge cases
N<6 any triplet can be chosen
N=6 remember to check alternate 1 3 5 or 2 4 6.

1 Like

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]
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]
for i in range(1,n+1):
ans=max(ans,sum(a[i-1:i+2]))
print(ans)
``````

Both solution gives AC. 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.

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