FYI

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])
        ans = temp
        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]))
            temp -= prev
            temp += next
            ans = min(ans, temp)

    print("Case #{}: {}".format(case+1, ans))

> 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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-code/4fc38d8b-9cbd-4501-b354-f53212eeaa58%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to