https://www.acmicpc.net/problem/2012
The question is quite simple though at first attempt, my code was time inefficient. We sort the given list and iterate and compare with the ascending order of ranks (i.e. the index of list from 1 to n), the difference is abs(lst[i]-i)
we can just iterate by the index but i created a tmp list from 1 to n and compared its element with the given lst's element. efficient!!
yes straightforward greedy q
import sys
input = sys.stdin.readline
n = int(input())
lst = [0]
for _ in range(n):
lst.append(int(input()))
lst.sort()
ans = 0
for i in range(1, n + 1):
ans += abs(lst[i] - i)
print(ans)
n time and space