Why get WA for COG2002

Problem link: CodeChef: Practical coding for everyone
My submission link: CodeChef: Practical coding for everyone
I think I didn’t understand the problem in right way. Please explain this to me.
When the editorial will come?

Given a circular array, you were just supposed to return the maximum possible sum for any three of its contiguous elements

3 Likes

@cubefreak777 Okay. In my solution, first I make another list with with three maximum numbers.
Like if I have a list like this: s = [10, 40, 30, 30, 20]
My program first will create a list from this with 3 maximum number like: s1 = [40, 30, 30]
Then my program print the sum of this like: print(sum(s1))
So, I did return the maximum possible sum. So, where is the wrong? Please clarify to me.

is it necessary to be contiguous elements?

@cubefreak777 My program also works for the example testcases.

you have to calculate the adjacent 3 elements sum…the sum 1st element with last two elements and similarly sum last element with 1st two elements…and every time you have to check the maximum sum of these three adjacent elements…

2 Likes
for _ in range(int(input())):
    n=int(input())
    ans=0
    seq=map(int,input().split())
    seq=list(seq)
    if(n<6):
        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)

I think this is correct, but I don’t know python properly, just try putting each person in the middle, and take the max on both sides.

1 Like

@gourab_santu Can you please give me some testcases for which my answer goes wrong?

A[10,40,30,30,20] for this

1-> max((10+40+30),(10+20+30))–80
2-> max((40+30+30),(40+20+10))–100
3-> max((30+30+20),(30+40+10))–80
4-> max((30+30+40),(30+20+10))–100
5-> max((20+30+30),(20+10+40))–80

At last max(80,100,80,100,80)----100

1 Like

So, there should be a sequence like this?:

thisans=seq[i]+max(seq[i+1],seq[i+2])+max(seq[i-1],seq[i-2])

I think in the question i didn’t get this. Thank you so much!

No I’m wrong

@gourab_santu Thank you so much for the help. Now, I understand it.

Corrected my code got AC

1 Like

output is large so use long long int in C

Ans will be = seq[i] + max((seq[i+1] + seq[i+2]) ,(seq[i-1] + seq[i-2]))
and also for the 1st and last element cause the standing was circular

1 Like

wlcm :blush:

1 Like

You can only take adjacent because the people on the edges also need to be friends, That wasn’t very clear in the question.

1 Like

Yes…but they also mentioned that the array was circular

Yes, adjacent in the circle, read my new corrected code.

I think its Ok…cause i’m not used to with python…don’t know soo much… :grin: :blush: