Can anybody share the solutions of the problem?

Or help me debug mine?
my code is passing the given test cases but showing WA on submission

(Training)

t = int(input())
for case in range(t):
    [n, p] = list(map(int, input().split()))
    s = list(map(int, input().split()))
    ans = 0
    if n == p:
        m = max(s)
        for i in range(p):
            if s[i] < m:
                ans += (m - s[i])
    else:
        s = sorted(s)
        temp = 0
        for i in range(p-1):
            temp += (s[p-1] - s[i])
        for i in range(1, n-p+1):
            prev = s[i+p-2] - s[i-1]
            next = ((p-1) * (s[i+p-1] - s[i+p-2]))
            if next < prev:
                temp -= prev
                temp += next
        ans = temp
        
    print("Case #{}: {}".format(case+1, ans))

-- 
You received this message because you are subscribed to the Google Groups 
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-code+unsubscr...@googlegroups.com.
To post to this group, send email to google-code@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-code/e9b60037-0e78-4d45-9345-25d6dc6e74ac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to