Problem p03309 (python)

Fixed submission:
n = int(input())
a = [int(i) for i in input().split()]

if n == 1:
    print(0)
    exit()

val = []

for i in range(n):
    val.append(a[i]-i)

val.sort()
#print(val)

#idx = [(val[0],0)]
idx = []
cnt = 1
for i in range(n-1):
    if val[i] == val[i+1]:
        #cnt += 1
        if i == n-2:
            idx.append((val[i+1],i+2))
        continue
    else:
        if i == n-2:
            idx.append((val[i],i+1))
            idx.append((val[i+1],i+2))
        else:
           idx.append((val[i],i+1)) 
        #cnt = 1
        
ans_val = 0
ans2_val = 0
ans3_val = 0

flag2 = False
flag3 = False

for i in range(len(idx)):
    if idx[i][1] >= (n+1) // 2:
        ans_val = idx[i][0]
        """
        #print(i-1)
        if i-1 >= 0:
            flag2 = True
            ans2_val = idx[i-1][0]
        
        if i+1 <= len(idx)-1:
            flag3 = True
            ans3_val = idx[i+1][0]
        """
        break

#print(idx,ans_val,ans2_val)
#exit()
     
ans = 0
ans2 = 10**20
ans3 = 10**20
for i in range(n):
    ans += abs(a[i]-ans_val-i)
    if flag2 == True:
        ans2 += abs(a[i]-ans2_val-i)
    if flag3 == True:
        ans3 += abs(a[i]-ans3_val-i)
    


print(ans)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = [int(i) for i in input().split()]

val = []

for i in range(n):
    val.append(a[i]-i)

val.sort()
#print(val)

#idx = [(val[0],0)]
idx = []
cnt = 1
for i in range(n-1):
    if val[i] == val[i+1]:
        #cnt += 1
        if i == n-2:
            idx.append((val[i],i+2))
        continue
    else:
        if i == n-2:
            idx.append((val[i],i+1))
            idx.append((val[i+1],i+2))
        else:
           idx.append((val[i],i+1)) 
        #cnt = 1
        
ans_val = 0
ans2_val = 0
ans3_val = 0

flag2 = False
flag3 = False

for i in range(len(idx)):
    if idx[i][1] >= (n) // 2:
        ans_val = idx[i][0]
        
        #print(i-1)
        if i-1 >= 0:
            flag2 = True
            ans2_val = idx[i-1][0]
        
        if i+1 <= n-1:
            flag3 = True
            ans3_val = idx[i+1][0]
        
        break

#print(idx,ans_val,ans2_val)
#exit()
     
ans = 0
ans2 = 10**20
ans3 = 10**20
for i in range(n):
    ans += abs(a[i]-ans_val-i)
    if flag2 == True:
        ans2 += abs(a[i]-ans2_val-i)
    if flag3 == True:
        ans3 += abs(a[i]-ans3_val-i)
    


print(min(ans,ans2,ans3))

Buggy submission (verdict: Runtime Error):
n = int(input())
a = [int(i) for i in input().split()]

val = []

for i in range(n):
    val.append(a[i]-i)

val.sort()
#print(val)

#idx = [(val[0],0)]
idx = []
cnt = 1
for i in range(n-1):
    if val[i] == val[i+1]:
        #cnt += 1
        if i == n-2:
            idx.append((val[i],i+2))
        continue
    else:
        if i == n-2:
            idx.append((val[i],i+1))
            idx.append((val[i+1],i+2))
        else:
           idx.append((val[i],i+1)) 
        #cnt = 1
        
ans_val = 0
ans2_val = 0
ans3_val = 0

flag2 = False
flag3 = False

for i in range(len(idx)):
    if idx[i][1] >= (n) // 2:
        ans_val = idx[i][0]
        
        #print(i-1)
        if i-1 >= 0:
            flag2 = True
            ans2_val = idx[i-1][0]
        
        if i+1 <= n-1:
            flag3 = True
            ans3_val = idx[i+1][0]
        
        break

#print(idx,ans_val,ans2_val)
#exit()
     
ans = 0
ans2 = 0
ans3 = 0
for i in range(n):
    ans += abs(a[i]-ans_val-i)
    ans2 += abs(a[i]-ans2_val-i)
    ans3 += abs(a[i]-ans3_val-i)
    

print(min(ans,ans2,ans3))