Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
imput = sys.stdin.readline


diff = ((0, 1), (0, -1), (1, 0), (-1, 0))
n = int(input())
p = list(map(int, input().split()))

cost = [0] * (n * n)
for i in range(n):
    for j in range(n):
        cost[i * n + j] = min(i, j, n - i - 1, n - j - 1)

occ = [True] * (n * n)
ans = 0
for start in p:
    start -= 1
    q = deque([start])
    ans += cost[start]
    occ[start] = False
    while q:
        ij = q.popleft()
        i, j = divmod(ij, n)
        for di, dj in diff:
            nxt_ij = (i + di) * n + (j + dj)
            if not(0 <= i + di < n and 0 <= j + dj < n):
                continue
            if not occ[ij] and cost[nxt_ij] > cost[ij]:
                    cost[nxt_ij] = cost[ij]
                    q.append(nxt_ij)
            elif cost[nxt_ij] > cost[ij] + 1:
                    cost[nxt_ij] = cost[ij] + 1
                    q.append(nxt_ij)

print(ans)

Fixed submission:
from collections import deque
import sys
imput = sys.stdin.readline


n = int(input())
p = list(map(int, input().split()))

cost = [0] * (n * n)
for i in range(n):
    for j in range(n):
        cost[i * n + j] = min(i, j, n - i - 1, n - j - 1)

occ = [True] * (n * n)
ans = 0
for start in p:
    start -= 1
    q = deque([start])
    ans += cost[start]
    occ[start] = False
    while q:
        ij = q.popleft()
        i, j = divmod(ij, n)

        nxt_ij = (i + 1) * n + j
        if not(0 <= i + 1 < n and 0 <= j < n):
             pass
        elif not occ[ij] and cost[nxt_ij] > cost[ij]:
            cost[nxt_ij] = cost[ij]
            q.append(nxt_ij)
        elif cost[nxt_ij] > cost[ij] + 1:
            cost[nxt_ij] = cost[ij] + 1
            q.append(nxt_ij)
              
        nxt_ij = (i - 1) * n + j
        if not(0 <= i - 1 < n and 0 <= j < n):
            pass
        elif not occ[ij] and cost[nxt_ij] > cost[ij]:
            cost[nxt_ij] = cost[ij]
            q.append(nxt_ij)
        elif cost[nxt_ij] > cost[ij] + 1:
            cost[nxt_ij] = cost[ij] + 1
            q.append(nxt_ij)

        nxt_ij = i * n + (j + 1)
        if not(0 <= i + 0 < n and 0 <= j + 1 < n):
            pass
        elif not occ[ij] and cost[nxt_ij] > cost[ij]:
            cost[nxt_ij] = cost[ij]
            q.append(nxt_ij)
        elif cost[nxt_ij] > cost[ij] + 1:
            cost[nxt_ij] = cost[ij] + 1
            q.append(nxt_ij)

        nxt_ij = i * n + (j - 1)
        if not(0 <= i < n and 0 <= j - 1 < n):
            pass
        elif not occ[ij] and cost[nxt_ij] > cost[ij]:
            cost[nxt_ij] = cost[ij]
            q.append(nxt_ij)
        elif cost[nxt_ij] > cost[ij] + 1:
            cost[nxt_ij] = cost[ij] + 1
            q.append(nxt_ij)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
imput = sys.stdin.readline


diff = ((0, 1), (0, -1), (1, 0), (-1, 0))
n = int(input())
p = list(map(int, input().split()))

cost = [0] * (n * n)
for i in range(n):
    for j in range(n):
        cost[i * n + j] = min(i, j, n - i - 1, n - j - 1)

occ = [True] * (n * n)
ans = 0
for start in p:
    start -= 1
    q = deque([start])
    ans += cost[start]
    occ[start] = False
    while q:
        ij = q.popleft()
        i, j = divmod(ij, n)
        for di, dj in diff:
            nxt_i = i + di
            nxt_j = j + dj
            nxt_ij = nxt_i * n + nxt_j
            if not(0 <= nxt_i < n and 0 <= nxt_j < n):
                continue
            if not occ[ij]:
                if cost[nxt_ij] > cost[ij]:
                    cost[nxt_ij] = cost[ij]
                    q.append(nxt_ij)
            else:
                if cost[nxt_ij] > cost[ij] + 1:
                    cost[nxt_ij] = cost[ij] + 1
                    q.append(nxt_ij)

print(ans)

Fixed submission:
from collections import deque
import sys
imput = sys.stdin.readline


n = int(input())
p = list(map(int, input().split()))

cost = [0] * (n * n)
for i in range(n):
    for j in range(n):
        cost[i * n + j] = min(i, j, n - i - 1, n - j - 1)

occ = [True] * (n * n)
ans = 0
for start in p:
    start -= 1
    q = deque([start])
    ans += cost[start]
    occ[start] = False
    while q:
        ij = q.popleft()
        i, j = divmod(ij, n)

        nxt_ij = (i + 1) * n + j
        if not(0 <= i + 1 < n and 0 <= j < n):
             pass
        elif not occ[ij] and cost[nxt_ij] > cost[ij]:
            cost[nxt_ij] = cost[ij]
            q.append(nxt_ij)
        elif cost[nxt_ij] > cost[ij] + 1:
            cost[nxt_ij] = cost[ij] + 1
            q.append(nxt_ij)
              
        nxt_ij = (i - 1) * n + j
        if not(0 <= i - 1 < n and 0 <= j < n):
            pass
        elif not occ[ij] and cost[nxt_ij] > cost[ij]:
            cost[nxt_ij] = cost[ij]
            q.append(nxt_ij)
        elif cost[nxt_ij] > cost[ij] + 1:
            cost[nxt_ij] = cost[ij] + 1
            q.append(nxt_ij)

        nxt_ij = i * n + (j + 1)
        if not(0 <= i + 0 < n and 0 <= j + 1 < n):
            pass
        elif not occ[ij] and cost[nxt_ij] > cost[ij]:
            cost[nxt_ij] = cost[ij]
            q.append(nxt_ij)
        elif cost[nxt_ij] > cost[ij] + 1:
            cost[nxt_ij] = cost[ij] + 1
            q.append(nxt_ij)

        nxt_ij = i * n + (j - 1)
        if not(0 <= i < n and 0 <= j - 1 < n):
            pass
        elif not occ[ij] and cost[nxt_ij] > cost[ij]:
            cost[nxt_ij] = cost[ij]
            q.append(nxt_ij)
        elif cost[nxt_ij] > cost[ij] + 1:
            cost[nxt_ij] = cost[ij] + 1
            q.append(nxt_ij)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import sys
imput = sys.stdin.readline


diff = ((0, 1), (0, -1), (1, 0), (-1, 0))
n = int(input())
p = list(map(int, input().split()))

cost = [0] * (n * n)
for i in range(n):
    for j in range(n):
        cost[i * n + j] = min(i, j, n - i - 1, n - j - 1) + 1

occ = [True] * (n * n)
ans = 0
for start in p:
    start -= 1
    q = deque([start])
    ans += cost[start] - 1
    occ[start] = False

    while q:
        ij = q.popleft()
        i, j = divmod(ij, n)
        for di, dj in diff:
            nxt_i = i + di
            nxt_j = j + dj
            nxt_ij = nxt_i * n + nxt_j
            if not(0 <= nxt_i < n and 0 <= nxt_j < n):
                continue
            elif not occ[ij] and cost[nxt_ij] > cost[ij]:
                cost[nxt_ij] = cost[ij]
                q.append(nxt_ij)
            elif cost[nxt_ij] > cost[ij] + 1:
                cost[nxt_ij] = cost[ij] + 1
                q.append(nxt_ij)
print(ans)

Fixed submission:
from collections import deque
import sys
imput = sys.stdin.readline


n = int(input())
p = list(map(int, input().split()))

cost = [0] * (n * n)
for i in range(n):
    for j in range(n):
        cost[i * n + j] = min(i, j, n - i - 1, n - j - 1)

occ = [True] * (n * n)
ans = 0
for start in p:
    start -= 1
    q = deque([start])
    ans += cost[start]
    occ[start] = False
    while q:
        ij = q.popleft()
        i, j = divmod(ij, n)

        nxt_ij = (i + 1) * n + j
        if not(0 <= i + 1 < n and 0 <= j < n):
             pass
        elif not occ[ij] and cost[nxt_ij] > cost[ij]:
            cost[nxt_ij] = cost[ij]
            q.append(nxt_ij)
        elif cost[nxt_ij] > cost[ij] + 1:
            cost[nxt_ij] = cost[ij] + 1
            q.append(nxt_ij)
              
        nxt_ij = (i - 1) * n + j
        if not(0 <= i - 1 < n and 0 <= j < n):
            pass
        elif not occ[ij] and cost[nxt_ij] > cost[ij]:
            cost[nxt_ij] = cost[ij]
            q.append(nxt_ij)
        elif cost[nxt_ij] > cost[ij] + 1:
            cost[nxt_ij] = cost[ij] + 1
            q.append(nxt_ij)

        nxt_ij = i * n + (j + 1)
        if not(0 <= i + 0 < n and 0 <= j + 1 < n):
            pass
        elif not occ[ij] and cost[nxt_ij] > cost[ij]:
            cost[nxt_ij] = cost[ij]
            q.append(nxt_ij)
        elif cost[nxt_ij] > cost[ij] + 1:
            cost[nxt_ij] = cost[ij] + 1
            q.append(nxt_ij)

        nxt_ij = i * n + (j - 1)
        if not(0 <= i < n and 0 <= j - 1 < n):
            pass
        elif not occ[ij] and cost[nxt_ij] > cost[ij]:
            cost[nxt_ij] = cost[ij]
            q.append(nxt_ij)
        elif cost[nxt_ij] > cost[ij] + 1:
            cost[nxt_ij] = cost[ij] + 1
            q.append(nxt_ij)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque


diff = ((0, 1), (0, -1), (1, 0), (-1, 0))
n = int(input())
p = list(map(int, input().split()))

cost = [[0] * n for i in range(n)]
for i in range(n):
    for j in range(n):
        cost[i][j] = min(i, j, n - i - 1, n - j - 1) + 1

occ = [[True] * n for i in range(n)]
ans = 0
for start in p:
    i, j = divmod(start - 1, n)
    q = deque([(i, j)])
    ans += cost[i][j] - 1
    occ[i][j] = False

    while q:
        i, j = q.popleft()
        for di, dj in diff:
            nxt_i = i + di
            nxt_j = j + dj
            if not(0 <= nxt_i < n and 0 <= nxt_j < n):
                continue
            elif not occ[i][j] and not occ[i][j] and cost[nxt_i][nxt_j] > cost[i][j]:
                cost[nxt_i][nxt_j] = cost[i][j]
                q.append((nxt_i, nxt_j))
            elif cost[nxt_i][nxt_j] > cost[i][j] + 1:
                cost[nxt_i][nxt_j] = cost[i][j] + 1
                q.append((nxt_i, nxt_j))

print(ans)

Fixed submission:
from collections import deque
import sys
imput = sys.stdin.readline


n = int(input())
p = list(map(int, input().split()))

cost = [0] * (n * n)
for i in range(n):
    for j in range(n):
        cost[i * n + j] = min(i, j, n - i - 1, n - j - 1)

occ = [True] * (n * n)
ans = 0
for start in p:
    start -= 1
    q = deque([start])
    ans += cost[start]
    occ[start] = False
    while q:
        ij = q.popleft()
        i, j = divmod(ij, n)

        nxt_ij = (i + 1) * n + j
        if not(0 <= i + 1 < n and 0 <= j < n):
             pass
        elif not occ[ij] and cost[nxt_ij] > cost[ij]:
            cost[nxt_ij] = cost[ij]
            q.append(nxt_ij)
        elif cost[nxt_ij] > cost[ij] + 1:
            cost[nxt_ij] = cost[ij] + 1
            q.append(nxt_ij)
              
        nxt_ij = (i - 1) * n + j
        if not(0 <= i - 1 < n and 0 <= j < n):
            pass
        elif not occ[ij] and cost[nxt_ij] > cost[ij]:
            cost[nxt_ij] = cost[ij]
            q.append(nxt_ij)
        elif cost[nxt_ij] > cost[ij] + 1:
            cost[nxt_ij] = cost[ij] + 1
            q.append(nxt_ij)

        nxt_ij = i * n + (j + 1)
        if not(0 <= i + 0 < n and 0 <= j + 1 < n):
            pass
        elif not occ[ij] and cost[nxt_ij] > cost[ij]:
            cost[nxt_ij] = cost[ij]
            q.append(nxt_ij)
        elif cost[nxt_ij] > cost[ij] + 1:
            cost[nxt_ij] = cost[ij] + 1
            q.append(nxt_ij)

        nxt_ij = i * n + (j - 1)
        if not(0 <= i < n and 0 <= j - 1 < n):
            pass
        elif not occ[ij] and cost[nxt_ij] > cost[ij]:
            cost[nxt_ij] = cost[ij]
            q.append(nxt_ij)
        elif cost[nxt_ij] > cost[ij] + 1:
            cost[nxt_ij] = cost[ij] + 1
            q.append(nxt_ij)
print(ans)