Submission details
Task:Ruudukko
Sender:removed82788
Submission time:2022-11-08 21:45:05 +0200
Language:Python3 (CPython3)
Status:BUSTED

Code

n = int(input())
rows = []
for i in range(n):
    rows.append([int(x) for x in input().split(" ")])

unique_numbers = []
for i in range(n):
    for j in range(n):
        if(rows[i][j] not in unique_numbers):
            unique_numbers.append(rows[i][j])
unique_numbers = sorted(unique_numbers)

def count_global(lambda_function):
    c = 0
    for i in range(n):
        for j in range(n):
            if lambda_function(rows[i][j]):
                c += 1
    return c

def count_elements_on_same_row_and_column(x, y, lambda_function):
    c = 0
    for i in range(n):
        if lambda_function(rows[y][i]):
            c += 1
        if lambda_function(rows[i][x]):
            c += 1
    return c

cache = {}

for num in unique_numbers:
    c = 0
    for x in range(n):
        for y in range(n):
            if(rows[y][x] == num):
                c += count_elements_on_same_row_and_column(x, y, lambda x: x < num)
    cache[num] = c + count_global(lambda x: x == num)

answer = 0
for i in range(len(unique_numbers) - 1):
    for j in range(i + 1):
        answer += cache[unique_numbers[j]]

print(answer)