I am trying to solve Stupid Machine problem [ CodeChef: Practical coding for everyone ] I think my approach is correct but failing hidden test cases.
My approach:
1.Check for 0 in the array if found set L to index-1 else set L to N-1
2.subtract each element of array from 0 to L
3. Repeat the above steps until L >=0
4.Store the L values in another array .
5.Print sum of L array.
Here is the code:
N=int(input())#3
L=N-1#2
arr = list(map(int,input().split()))
def sub(arr,L):
for i in range(L+1):
arr[i]-=1;
list=[]
try:
index = arr.index(0)
L=index
except ValueError:
L=N-1#2
#print(arr)
while(L>=0):
if(L==0):
if(arr[L]>0):
arr[L]-=1
list.append(L+1)
L=-1
else:
try:
index = arr.index(0)
L=index-1
#print(L)
list.append(L+1)
sub(arr,L)
#print(arr)
except ValueError:
#print("L:" + str(L))
sub(arr,L)
list.append(L+1)
#print(arr)
print(sum(list))
Thank You.