fork download
  1. def solve():
  2. #General input section
  3. inp = input()
  4. n = int(inp.split(' ')[0])
  5. k = int(inp.split(' ')[1])
  6. x = int(inp.split(' ')[2])
  7. arr = []
  8. inp = input()
  9. rng = 1000000000
  10. mn = rng + 2
  11. for e in inp.split(' '):
  12. arr.append(int(e))
  13. #General input section END
  14. #Step1: Find minimum of the array
  15. mn = min(arr)
  16. #Step2: Find the maximum term for p array in question
  17. max_term = mn + x
  18. if max_term > rng:
  19. max_term = rng
  20. #Step 3:
  21. #I have to explain what I did after this
  22. #Let's say max_term = 50, n = 10, k = 3
  23. #arr = 11,26,58
  24. #then I tried
  25. #to find sum of digits from 44-50
  26. #which can be written as (1+2+3+..+50)-(1+2+3+..+43)
  27.  
  28. max_sum = int(((max_term)*(max_term+1))/2)
  29. min_term = (max_term-(n-k))
  30. min_sum = int(((min_term)*(min_term+1))/2)
  31. sm = max_sum-min_sum
  32. #Setting last included term to 44, as till now it was 43
  33. #as of example i gave
  34. min_term += 1
  35. #Just sorting the array
  36. arr.sort()
  37. i = k-1
  38. while i >= 0:
  39. #If a given number is greater,equal to min_term
  40. #this means, it is already added
  41. #so decrease min_terms to a level it is not included
  42. if arr[i] >= min_term:
  43. min_term -= 1
  44. while min_term in arr:
  45. min_term -= 1
  46. sm += min_term
  47. else:
  48. sm += arr[i]
  49. i -= 1
  50. # print (mn)
  51. print (int(sm))
  52.  
  53. t = int(input())
  54. for i in range(t):
  55. solve()
Runtime error #stdin #stdout #stderr 0.04s 27712KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 53, in <module>
EOFError: EOF when reading a line