1. '''
  2. # Sample code to perform I/O:
  3.  
  4. name = input() # Reading input from STDIN
  5. print('Hi, %s.' % name) # Writing output to STDOUT
  6.  
  7. # Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
  8. '''
  9.  
  10. # Write your code here
  11. def ab(k,arr1):
  12. sum1=0
  13. c=0
  14. m=k
  15. for i in arr1:
  16. if (i>=0):
  17. c+=1
  18. sum1+=i
  19. m=k
  20. else:
  21. if(c==0):
  22. return abs(sum(arr1)+i)
  23. else:
  24. m=m-1
  25. if (m<0):
  26. sum1+=abs(i)
  27. else:
  28. sum1+=i
  29. return abs(sum1)
  30. n,k= map(int,input().split())
  31. arr1=map(int,input().split())
  32. out= ab(k,arr1)
  33. print(out)
Language: Python 3.8