Problem p03147 (python)

Fixed submission:
N = int(input())
listh = list(map(int, input().split()))
listans = [listh[0]]
for n in range(1,N-1):
    if (listh[n+1]-listh[n])*(listh[n]-listh[n-1])<=0:
        listans.append(listh[n])
listans.append(listh[N-1])        
#print(listans)
l =len(listans)
listans2 = []
for n in range(l-1):
    if listans[n] == listans[n+1]:
        pass
    else:
        listans2.append(listans[n])
listans2.append(listans[l-1])        
#print(listans2)    
L = len(listans2)
#print(L)
listans3 = [listans2[0]]
for n in range(1,L-1):
    if (listans2[n+1]-listans2[n])*(listans2[n]-listans2[n-1])<=0:
        listans3.append(listans2[n])
listans3.append(listans2[L-1])        
#print(listans3)
ll =len(listans3)
listodd = [listans3[x] for x in range(ll) if x%2 ==1]
#print(listodd)
listeven = [listans3[x] for x in range(ll) if x%2 ==0]
#print(listeven)
if len(listodd) == 0:
    print(listeven[0])
elif ll%2 == 0:
    if listeven[0]>listodd[0]:
        del listodd[-1]
        ans = sum(listeven)-sum(listodd)
        print(ans)

    else:  
        del listeven[0]
        ans = sum(listodd)-sum(listeven)  
        print(ans)

else:     
    if listeven[0]>listodd[0]:
        ans = sum(listeven)-sum(listodd)
        print(ans)

    else:
        del listeven[-1]
        if len(listeven) >= 1:
            del listeven[0]
        ans = sum(listodd)-sum(listeven)
        print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
listh = list(map(int, input().split()))
listans = [listh[0]]
for n in range(1,N-1):
    if (listh[n+1]-listh[n])*(listh[n]-listh[n-1])<=0:
        listans.append(listh[n])
listans.append(listh[N-1])        
#print(listans)
l =len(listans)
listans2 = []
for n in range(l-1):
    if listans[n] == listans[n+1]:
        pass
    else:
        listans2.append(listans[n])
listans2.append(listans[l-1])        
#print(listans2)    
L = len(listans2)
#print(L)
listodd = [listans2[x] for x in range(L) if x%2 ==1]
#print(listodd)
listeven = [listans2[x] for x in range(L) if x%2 ==0]
#print(listeven)
if L%2 == 0:
    if listeven[0]>listodd[0]:
        del listodd[-1]
        ans = sum(listeven)-sum(listodd)
        print(ans)

    else:  
        del listeven[0]
        ans = sum(listodd)-sum(listeven)  
        print(ans)

else:     
    if listeven[0]>listodd[0]:
        ans = sum(listeven)-sum(listodd)
        print(ans)

    else:
        del listeven[-1]
        if len(listeven) >= 1:
            del listeven[0]
        ans = sum(listodd)-sum(listeven)
        print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
listh = list(map(int, input().split()))
listans = [listh[0]]
for n in range(1,N-1):
    if (listh[n+1]-listh[n])*(listh[n]-listh[n-1])<=0:
        listans.append(listh[n])
listans.append(listh[N-1])        
#print(listans)
l =len(listans)
listans2 = []
for n in range(l-1):
    if listans[n] == listans[n+1]:
        pass
    else:
        listans2.append(listans[n])
listans2.append(listans[l-1])        
#print(listans2)    
L = len(listans2)
#print(L)
listodd = [listans2[x] for x in range(L) if x%2 ==1]
#print(listodd)
listeven = [listans2[x] for x in range(L) if x%2 ==0]
#print(listeven)
if L%2 == 0:
    if listeven[0]>listodd[0]:
        del listodd[-1]
        ans = sum(listeven)-sum(listodd)
        print(ans)

    else:  
        del listeven[0]
        ans = sum(listodd)-sum(listeven)  
        print(ans)

else:     
    if listeven[0]>listodd[0]:
        ans = sum(listeven)-sum(listodd)
        print(ans)

    else:
        del listeven[-1]
        if len(listeven)>0
        del listeven[0]
        ans = sum(listodd)-sum(listeven)
        print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
listh = list(map(int, input().split()))
listans = [listh[0]]
for n in range(1,N-1):
    if (listh[n+1]-listh[n])*(listh[n]-listh[n-1])<=0:
        listans.append(listh[n])
listans.append(listh[N-1])        
#print(listans)
l =len(listans)
listans2 = []
for n in range(l-1):
    if listans[n] == listans[n+1]:
        pass
    else:
        listans2.append(listans[n])
listans2.append(listans[l-1])        
#print(listans2)    
L = len(listans2)
#print(L)
listodd = [listans2[x] for x in range(L) if x%2 ==1]
#print(listodd)
listeven = [listans2[x] for x in range(L) if x%2 ==0]
#print(listeven)
if L%2 == 0:
    if listeven[0]>listodd[0]:
        del listodd[-1]
        ans = sum(listeven)-sum(listodd)
        print(ans)

    else:  
        del listeven[0]
        ans = sum(listodd)-sum(listeven)  
        print(ans)

else:     
    if listeven[0]>listodd[0]:
        ans = sum(listeven)-sum(listodd)
        print(ans)

    else:   
        del listeven[0]
        del listeven[-1] 
        ans = sum(listodd)-sum(listeven)
        print(ans)