Need help with GUZAC problem

Link to the Question

I want to ask why am I getting a wrong answer. Here is my code, I have explained it as well:

IDEONE LINK FOR BETTER READABILITY OF CODE

def solve():
    #General input section
    inp = input()
    n = int(inp.split(' ')[0])
    k = int(inp.split(' ')[1])
    x = int(inp.split(' ')[2])
    arr = []
    inp = input()
    rng = 1000000000
    mn = rng + 2
    for e in inp.split(' '):
        arr.append(int(e))
    #General input section END
    #Step1: Find minimum of the array
    mn = min(arr)
    #Step2: Find the maximum term for p array in question
    max_term = mn + x
    if max_term > rng:
        max_term = rng
    #Step 3:
    #I have to explain what I did after this
    #Let's say max_term = 50, n = 10, k = 3
    #arr = 11,26,58
    #then I tried 
    #to find sum of digits from 44-50
    #which can be written as (1+2+3+..+50)-(1+2+3+..+43)
    
    max_sum = int(((max_term)*(max_term+1))/2)
    min_term = (max_term-(n-k))
    min_sum = int(((min_term)*(min_term+1))/2)
    sm = max_sum-min_sum
    #Setting last included term to 44, as till now it was 43
    #as of example i gave
    min_term += 1
    #Just sorting the array
    arr.sort()
    i = k-1
    while i >= 0:
        #If a given number is greater,equal to min_term
        #this means, it is already added
        #so decrease min_terms to a level it is not included
        if arr[i] >= min_term:
            min_term -= 1
            while min_term in arr:
                min_term -= 1
            sm += min_term
        else:
            sm += arr[i]
        i -= 1
    # print (mn)
    print (int(sm))

t = int(input())
for i in range(t):
    solve()
1 Like

You should only decrease the lower limit of the fill block sum by one for each included given array item. Because you’ll take the other steps downwards as you pass the following array items, including the ones that have now been included.


Python style; you can input n, k and x using

n,k,x = map(int,inp.split(' ')

and arr similarly using

arr = list(map(int, inp.split(' ')))

Explanation: map applies a function (here the int function) to some generated set of values, to return a sequence of values.

You find the minimum of arr explicitly but later you sort arr; why not just sort first and read off the first element?

Can you help a little? I don’t understand what you are saying

This code:

        min_term -= 1  
        while min_term in arr:  
            min_term -= 1  
        sm += min_term  

should not have that while loop. It should just be

        min_term -= 1
        sm += min_term

That gives WA as well

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

Try getting rid of:

if max_term > rng:
    max_term = rng