'''
# Sample code to perform I/O:

name = input()                  # Reading input from STDIN
print('Hi, %s.' % name)         # Writing output to STDOUT

# Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
'''

# Write your code here
def ab(k,arr1):
    sum1=0
    c=0
    m=k
    for i in arr1:
        if (i>=0):
            c+=1
            sum1+=i
            m=k
        else:
            if(c==0):
                return abs(sum(arr1)+i)
            else:
                m=m-1
                if (m<0):
                    sum1+=abs(i)
                else:
                    sum1+=i
    return abs(sum1)
n,k= map(int,input().split())
arr1=map(int,input().split())
out= ab(k,arr1)
print(out)
Language: Python 3.8