Problem:


Score : 300 points

Problem Statement

Snuke has an integer sequence A of length N.

He will freely choose an integer b.
Here, he will get sad if A_i and b+i are far from each other.
More specifically, the sadness of Snuke is calculated as follows:

abs(A_1 - (b+1)) + abs(A_2 - (b+2)) + ... + abs(A_N - (b+N))

Here, abs(x) is a function that returns the absolute value of x.

Find the minimum possible sadness of Snuke.

Constraints

1 \leq N \leq 2 \times 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible sadness of Snuke.

Sample Input 1

5
2 2 3 5 5

Sample Output 1

2

If we choose b=0, the sadness of Snuke would be abs(2-(0+1))+abs(2-(0+2))+abs(3-(0+3))+abs(5-(0+4))+abs(5-(0+5))=2.
Any choice of b does not make the sadness of Snuke less than 2, so the answer is 2.

Sample Input 2

9
1 2 3 4 5 6 7 8 9

Sample Output 2

0

Sample Input 3

6
6 5 4 3 2 1

Sample Output 3

18

Sample Input 4

7
1 1 1 1 2 3 4

Sample Output 4

6



Sample input:
5
2 2 3 5 5


Sample output:
2

Submission status: Runtime Error

Solution code:
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = np.array(lr())
A -= np.arange(1, N+1)
A.sort()

def check(i):
    ret = np.abs(A - A[i]).sum()
    return ret

answer = min(check(0), check(-1))
left = 0; right = N-1
while abs(right-left) > 2:
    l_mid = (left*2 + right) // 3
    r_mid = (left + right*2) // 3
    if check(l_mid) < check(r_mid):
        right = r_mid
    else:
        left = l_mid

cand = min(check(l_mid), check(r_mid))
answer = min(answer, cand)
print(answer)

Submission status: Runtime Error

Solution code:
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = np.array(lr())
A -= np.arange(1, N+1)
answer = 10 ** 15
B = np.abs(np.subtract.outer(A, A))
answer = np.min(B.sum(axis=1))
print(answer)

Submission status: Runtime Error

Solution code:
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
b = [0]*n
for i in range(n):
    sa = a[i] - (i+1)
    b[i] = sa
b.sort()
idx = int(n/2) -  (n%2)^1

p = b[idx]
ans = 0
for i in range(n):
    ans += abs(i+1+p-a[i])
print(ans)

Submission status: Runtime Error

Solution code:
import statistics
def resolve():
	n = int(input())
	a = list(map(int, input().split()))
	seq = []
	for i in range(1, n+1):
		seq.append(abs(i-a[i-1]))
	seq = sorted(seq)
	m = statistics.median(seq)
	ans = 0
	for i in seq:
		ans += abs(i-m)
	print(ans)
resolve()

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
temp=[0]*n
for i in range(0,n):
  temp[i]=a[i]-(i+1)
temp.sort()
ave=temp/n
print(ave)

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
from fractions import gcd
import sys
def input(): return sys.stdin.readline().rstrip()
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = 10**18
# ----------------------------------------------------------- #

n = I()
A = LI()
B = [A[i]-i-1 for i in range(n)]
mid = B[len(B)//2+1]
def sad(b, n):
    ret = 0
    for i in range(n):
        ret += abs(B[i]-b)
    return ret

ans = INF
for i in range(mid-50, mid+50):
    ans = min(ans, sad(i, n))
print(ans)

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/abc102/submissions/7991084
def abc102_c():
    N = int(readline())
    A = np.array(read().split(), dtype=np.int64)

    # b=0として変形する
    B = A - np.arange(1, N+1)
    # 中央値が0になるよう高さ調整する
    B.sort()
    x = B[N//2]
    # 絶対値の合計をとる
    ans = np.abs(B - x).sum()
    print(ans)

abc102_c()

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))

b_low=-1*(10**12)
b_high=10**12
b_low_d=sum([abs(a[i]-(b_low+2+i)) for i in range(n)])-sum([abs(a[i]-(b_low+1+i)) for i in range(n)])
b_high_d=sum([abs(a[i]-(b_high+2+i)) for i in range(n)])-sum([abs(a[i]-(b_high+1+i)) for i in range(n)])

while b_high-b_low>10:
  b_tmp=(b_low+b_high)//2
  b_tmp_d=sum([abs(a[i]-(b_tmp+2+i)) for i in range(n)])-sum([abs(a[i]-(b_tmp+1+i)) for i in range(n)])
  if b_tmp_d>0:
    b_high=b_tmp
  else:
    b_low=b_tamp

ans=10**20
for j in range(b_low,b_high):
  ans=min(ans,sum([abs(a[i]-(j+2+i)) for i in range(n)]))

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(x) for x in input().split()]

a2 = [a[i] - i - 1 for i in range(n)]
mean = sum(a2) / len(a2)
b = np.floor(mean)
a3 = sum([abs(a2[i] - b) for i in range(n)])
b = np.ceil(mean)
a4 = sum([abs(a2[i] - b) for i in range(n)])
print(int(min(a3, a4)))

Submission status: Runtime Error

Solution code:
n = int(input())
x = list(map(int,input().split()))
for i in range(n):
    x[i]-=i+1
x.sort()
l=len(x)
if n%2==0:
    b = int((x[l/2-1]+x[l/2])/2)
else:
    b = x[int((l-1)/2)]
ans=0
for i in range(n):
    ans+=abs(x[i]-b)
print(ans)

Submission status: Runtime Error

Solution code:
import math

N = int(input())
A = list(map(int, input().split()))
# print(N, A)

_A = [A[i] - (i + 1) for i in range(len(A))]
_A.sort()
median = _A[math.ceil(len(A) / 2)]

sadness = 0
for i in range(len(A)):
    sadness += abs(A[i] - (i + 1 + median))

print(sadness)

Submission status: Runtime Error

Solution code:
import math

N = int(input())
A = list(map(int, input().split()))
# print(N, A)

_A = [] * len(A)
for i in range(len(A)):
    _A[i] -= (i + 1)
_A.sort()
median = _A[len(A) / 2]

sadness = 0
for i in range(len(A)):
    sadness += abs(A[i] - (i + 1 + median))

print(sadness)

Submission status: Runtime Error

Solution code:
import statistics

N = int(input())
A = [int(i) for i in input().split()]
B = [A[i] - (i+1) for i in range(N)]
b = statistics.median(B)
sum = 0
for i in range(N):
    sum += abs(B[i] - b)
print(int(sum))

Submission status: Runtime Error

Solution code:
import statistics

N = int(input())
A = [int(i) for i in input().split()]
B = [A[i] - (i+1) for i in range(N)]
B.sort()
b = statistics.median(B)
sum = 0
for i in range(N):
    sum += B[i] - b
print(sum)

Submission status: Runtime Error

Solution code:
n=int(input().split())
lists=list(map(int,input().split())
for i in range(n):
    lists[i]-=(i+1)
mini=10**100
for k in range(-10**9,10**9,1):
    sub=0
    for i in range(n):
        sub+=abs(list[i]-k)
    mini=min(mini,sub)
           
print(mini)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))

def func(b):
    return sum([abs(val - b) for val in I])

#y seppen wo motomeru
import statistics
import math

I=[A[i]-i-1 for i in range(N)]

m=min(I)
M=max(I)

if N % 2 ==1:
    print(func(statistics.median(I)))
else:
    print(func(math.floor(statistics.median(I))))

'''
    for i in range(m, M+1):
        #print(i)
        temp=sum([abs(val-i) for val in I])
        if temp<opp:
            opp=temp
    print(opp)
'''

Submission status: Runtime Error

Solution code:
import math
N=int(input())
A=list(map(int,input().split(' ')))
ans = math.inf
l = []
for n,i in enumerate(A):
    l.append(i-n-1)
l = sorted(l)
for i in l[N//2-1:N//2+2]:
    tmp = 0
    for j in l:
        tmp += abs(j-i)
    ans = min(ans,tmp)
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
a.sort()
b.sort()
c.sort()
n=len(a)
j=0
k=0
for i in range(n):
    while j<n and b[j]<=a[i]:
        j+=1
    while k<n and c[k]<=b[j]:
        k+=1

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
    a[i] -= i + 1
a.sort()
if n %  2 == 0:
    m, l = 0, 0
    for i in a:
        m += abs(i - a[n // 2])
        l += abs(i - a[n // 2 - 1])
    ans = min(l, m)
else:
    k, m, l = 0, 0, 0
    for i in a:
        k += abs(i - a[n // 2 - 1])
        m += abs(i - a[n // 2])
        l += abs(i - a[n // 2 + 1])
        ans = min(k, m, l)
print(ans)

Submission status: Runtime Error

Solution code:
import statistics
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline

def resolve():
    N=int(input())
    A=np.array(list(map(int,input().split())))

    B=np.array([i for i in range(1,N+1)])

    A=A-B
    val=0
    for i in A:
        val += abs(i - statistics.median(A))
    print(int(val))
resolve()

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))

for i in range(n):
    a[i] = a[i]-(i+1)

a.sort()
if i%2==1:
    b = a[i//2]
else:
    b = (a[i//2]+a[i//2+1])//2

ans = 0
for i in range(n):
    ans += abs(a[i]-b)
print(ans)

Submission status: Runtime Error

Solution code:
import statistics, math

num = int(input())

num_list = list(map(int, input().split()))
abs_num = []
for i in range(num):
    b = num_list[i] - i+1
    abs_num.append(b)

median = statistics.median(abs_num)

ans = 0
for b in abs_num:
    ans += abs(b - math.floor(median))
print(ans)

Submission status: Runtime Error

Solution code:
import statistics  import median
N = int(input())
A=list(map(int, input().split()))
B=[]
for number, i in enumerate(A, 1):
    B.append(i-number)
median = int(median(B))
ans2 = 0
for i in B:
    ans2 += abs(i-median)
print(ans2)

Submission status: Runtime Error

Solution code:
from statistics import median

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

b = int(median([v-i for i, v in enumerate(a, 1)]))
res = 0
print(b)
for i, v in enumerate(a, 1):
    res += abs(v-(b+i))

print(res)

Submission status: Runtime Error

Solution code:
import statistics  import median
N = int(input())
A=list(map(int, input().split()))
B=[]
for number, i in enumerate(A, 1):
    B.append(i-number)
median = round(median(B))
ans2 = 0
for i in B:
    ans2 += abs(i-median)
print(ans2)

Submission status: Runtime Error

Solution code:
import statistics
N = int(input())
A=list(map(int, input().split()))
B=[]
for number, i in enumerate(A, 1):
    B.append(i-number)
median = round(statistics.median(B))
ans2 = 0
for i in B:
    ans2 += abs(i-median)
print(ans2)

Submission status: Runtime Error

Solution code:
import math
sousuu = int(input())
listnum = list(map(int, input().split()))
posinum = sousuu // 2 if sousuu % 2 == 0 else sousuu // 2 + 1
posival = sum(listnum) // len(listnum)
dst = math.inf
for p in range(posinum - 3, posinum + 3):
    _dst = 0
    for i, s in enumerate(range(p - sousuu//2, p - sousuu//2 + sousuu)):
        _dst += abs(listnum[i] - s)
    if dst > _dst:
        dst = _dst
print(dst)

Submission status: Runtime Error

Solution code:
import sys
readline=sys.stdin.readline

N=int(readline())
A=list(map(int,readline().split()))
B=[0]*N
sumB=0
for i in range(N):
  B[i]=A[i]-(i+1)
  
B=sorted(B)
candi=[0]*3
candi[0]=B[N//2-1]
candi[1]=B[N//2]
candi[2]=B[N//2+1]

ans=10**10
for c in candi:
  res=0
  for i in range(N):
    res+=abs(B[i]-c)
  if res<ans:
    ans=res
print(ans)

Submission status: Runtime Error

Solution code:
"""
102 C
"""

import math

def read_input():
    n = int(input())
    alist = list(map(int, input().split()))

    return n, alist


def sum_abs(alist, b):
    return sum([abs(a - b) for a in alist])


def submit():
    n, alist = read_input()

    alist = [a - (i + 1) for i, a in enumerate(alist)]
    alist.sort()

    if n % 2:
        b_min, b_max = alist[n // 2], alist[n // 2 + 1]
        print(min(sum_abs(alist, b_min), sum_abs(alist, b_max)))
    else:
        print(sum_abs(alist, alist[n // 2]))


if __name__ == "__main__":
    submit()

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))

#n=5
#stra="2 2 3 5 5"
#ans=2

#n=9
#stra="1 2 3 4 5 6 7 8 9"
#ans=0

#n=6
#stra="6 5 4 3 2 1"
#ans=18

#n=7
#stra="1 1 1 1 2 3 4"
#ans=6

#a=list(map(int,stra.split()))

for i in range(n):
    a[i]=a[i]-i-1
#print(a)
a.sort()

s=[a[0]]
for i in range(1,n):
    s.append(s[i-1]+a[i])
    
bmin=min(a)-n
bmax=max(a)+n

  
asmin=bmax*n
m0=0
for b in range(bmin,bmax+1):
    m=bisect.bisect(a,b)
    if m<=0:
        asf=-n*b+s[n-1]
    elif m<n:
        asf=(2*m-n)*b+s[n-1]-2*s[m-1]
    elif n<=m:
        asf=n*b-s[n-1]
        
#    print("b",b,"m",m,"asf",asf)
    if asf<asmin:
        asmin=asf

print(asmin)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
for i in range(N):
  A[i] = A[i] - i - 1
print( A.sum() // N )

Submission status: Runtime Error

Solution code:
import statistics
input()
a=list(map(int,input().split()))
k=[n-i-1 for i,n in enumerate(a)]
b=int(statistics.median(k))
print(sum([abs(a-b) for a in k]))

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
def quick_sort(a):
    if len(a)<= 1:
        return a
    else:
        b=[]
        c=[]
        for i in range(1, len(a)):
            if a[i]<a[0]:
                b.append(a[i])
            else:
                c.append(a[i])
        return quick_sort(b)+[a[0]]+quick_sort(c)
def abs(x):
    if x>=0:
        return x
    else:
        return x*(-1)
B=[0]*N
for i in range(N):
  B[i]=A[i]-i-1
C=quick_sort(B)
if N%2==0:
  b=(C[N//2]+C[N//2-1])//2
else:
  b=C[N//2+1]
sad=0
for j in range(N):
  sad=sad+abs(C[j]-b)
print(sad)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
def quick_sort(a):
    if len(a) <= 1:
        return a
    else:
        b = []
        c = []
        for i in range(1, len(a)):
            if a[i] < a[0]:
                b.append(a[i])
            else:
                c.append(a[i])
        return quick_sort(b)+[a[0]]+quick_sort(c)
def abs(x):
    if x>=0:
        return x
    else:
        return x*(-1)
B=[0]*N
for j in range(N):
    B[j]=A[j]-j-1
bh=quick_sort(B)[0]
bt=quick_sort(B)[N]
def sad(A,b):
  s=[0]*(len(A))
  for m in range(len(A)):
    s[m]=abs(A[m]-(b+m+1))
  return s
def sum(A):
  sum=0
  for n in range(len(A)):
    sum=sum+A[n]
  return sum
l=[]
for o in range(bh-5,bt+5):
  l.append(sum(sad(A,o)))
print(quick_sort(l)[0])

Submission status: Runtime Error

Solution code:
from math import floor

def solve(n, A):
    A.sort()
    b = A[n // 2 + 1]
    return sum(abs(a - (b + i + 1)) for i, a in enumerate(A))

_n = int(input())
_A = list(map(int, input().split()))
print(solve(_n, _A))

Submission status: Runtime Error

Solution code:
N=int(input())
A=sorted(a-i-1 for i,a in enumerate(map(int,input().split())))
print(sume(abs(a-A[N//2]) for a in A))

Submission status: Runtime Error

Solution code:
9
1 2 3 4 5 6 7 8 9

Submission status: Runtime Error

Solution code:
def calc_sadness(b):
    ans = 0
    for a in A:
        ans += abs(a - b)
    return ans


N = int(input())
A = []
for idx, a in enumerate(map(int, input().split())):
    A.append(a - idx - 1)
A.sort()

if N % 2 == 0:
    b = [[N // 2]]
else:
    b = [A[N // 2 - 1], A[N // 2]]

print(min(map(calc_sadness, b)))

Submission status: Runtime Error

Solution code:
n = int(input().strip())
a = list(map(int, input().split()))
b = [(a[i] - (i + 1)) for i in range(n)]
b.sort()
mid = 0
if n % 2 == 1:
    mid = b[(n - 1) // 2 + 1]
else:
    mid = (b[(n - 1) // 2] + b[(n - 1) // 2]) // 2

ans = 0
for i in range(n):
    ans += abs(b[i] - mid)
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input().strip())
a = list(map(int, input().split()))
b = [a[i] - (i + 1) for i in range(n)]
b.sort()
mid = b[n // 2]
ans = 0
for i in range(n):
    ans += abs(b - mid)
print(ans)

Submission status: Runtime Error

Solution code:
s = 10000000

for i in range(2+max(a)):
  s_tmp = 0
  
  for j in range(n):
    s_tmp = s_tmp + abs(a[l] - i - (j+1))
    
    if s_tmp < s:
      s = s_tmp

Submission status: Runtime Error

Solution code:
n = int(input())

a = list(map(int, input().split()))
s = 10000000

for i in range(2+max(a)):
  s_tmp = 0
  
  for j in range(n):
    s_tmp = s_tmp + abs(a[l] - i - (j+1))
    
    if s_tmp < s:
      s = s_tmp

print(s)

Submission status: Runtime Error

Solution code:
def median(B):
	N = len(B)
	if N%2 == 0:
		return (B[N/2 - 1] + B[N/2])/2
	else:
		return B[N//2]

N = int(input())
arr = list(map(int, input().split()))

s = 0
B = [0]*N

for i in range(N):
	B[i] = arr[i] - i -1
    
B.sort()
b = median(B)

for j in range(N):
	s += abs(B[j] - b)
print(s)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
if n=1:
    print(0)
    exit()
for i in range(n):
    a[i]=a[i]-i-1
a.sort()
b1=a[n//2]
b2=a[n//2+1]
ans1=[]
ans2=[]
for i in range(n):
    ans1.append(abs(a[i]-b1))
for i in range(n):
    ans2.append(abs(a[i]-b2))
print(min(sum(ans1),sum(ans2)))

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
for i in range(n):
    a[i]=a[i]-i-1
a.sort()
b1=a[n//2]
b2=a[n//2+1]
ans1=[]
ans2=[]
for i in range(n):
    ans1.append(abs(a[i]-b1))
for i in range(n):
    ans2.append(abs(a[i]-b2))
print(min(sum(ans1),sum(ans2)))

Submission status: Runtime Error

Solution code:
import statistics

N = int(input())
A = list(map(int,input().split()))
b = 0
n = len(A)
B = [0] * n
for i in range(n):
    B[i] = abs(A[i] - (i+1))
    
median = statistics.median(B)
b = median

for i in range(n):
    sum = sum + abs(A[i] - b - i -1 )

print(sum)

Submission status: Runtime Error

Solution code:
N = int(input())
A = input()
A = A.split()
B =[]
C = []
ans = 0
for i in range(N):
    B.append(int(A[i]))
    C.append(int(A[i]))
C.sort(reverse=True)
"""
Max = C[0]
b = Max - N
"""
b = C[N/2]
for i in range(N):
    ans = ans + abs(B[i]-(i+b+1))

ans = ans
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = input()
A = A.split()
B =[]
C = []
ans1 = 0
ans2 = 0
for i in range(N):
    B.append(int(A[i]))
    C.append(int(A[i]))
C.sort(reverse=True)
Max = C[0]
b = Max - N
for i in range(N):
    ans1 = ans + abs(B[i]-(i+b+1))
for i in range(N):
    ans2 = ans + abs(B[i]-(i+1))
if ans1>ans2:
    ans = ans1
else:
    ans = ans2

print(ans)

Submission status: Runtime Error

Solution code:
from statistics import mode
N = int(input())
arr = list(map(int, input().split()))
for i in range(N):
    arr[i] = arr[i]-(i+1)
if len(arr) == len(set(arr)):
    sad = 0
else:
    sad = mode(arr)
ans = 0
for i in arr:
    ans += abs(i-sad)
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
s=input().split()
a=sorted([int(s[i])-i-1 for i in range(n)])

if n==0:
    print(0)
else:
    #それぞれの間で考える
    mi=[]
    for i in range(n-1):
        c,d=a[i],a[i+1]
        m=0
        for k in range(n):
            if k<=i:
                m-=a[k]
            else:
                m+=a[k]
        l=(i+1)-(n-i-1)
        if l>0:
            m+=(l*a[i+1])
        else:
            m+=(l*a[i])
        mi.append(m)
    print(min(mi))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = []
for i in range(n):
  b.append(a[i]-i-1)
  
b.sort()

center = n//2
c1 = b[center]
c2 = b[center+1]

ans1 = 0
ans2 = 0
for i in range(n):
  ans1 += abs(b[i]-c1)
  ans2 += abs(b[i]-c2)
print(min(ans1,ans2))

Submission status: Runtime Error

Solution code:
N=int(input())
A=[int(X) for X in input().split()]
B=[]
for i in range(N):
  B +=[A[i]-1-i]
B = sorted(B)
p=N//2
answer=float('inf')
for i in range(B[p-1],B[p+2]):
  a=0
  for j in range(N):
    a+=abs(B[j]-i)
  answer=min(answer,a)
print(answer if answer!=float('inf') else 0)

Submission status: Runtime Error

Solution code:
import bisect

N = int(input())
A = list(map(int, input().split()))
A_n = []

for n in range(N):
    A_n.append(A[n]-n)

A_n = sorted(A_n, reverse=True)

can_f = A_n[N//2]
can_s = A_n[N//2 + 1]

ans_f = 0
ans_s = 0

for n in range(N):
    ans_f += abs(A_n[n]-can_f)

for n in range(N):
    ans_s += abs(A_n[n]-can_s)

ans = min(ans_f, ans_s)
print(ans)

Submission status: Runtime Error

Solution code:
import statistics
n = int(input())
al = list(map(int,input().split()))

for i in range(n):
    al[i] -= i + 1

median = statistics.median(al)

ans = 0
for a in al:
    ans += abs(median - a)

print(int(ans))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))

b = [0]*n
for i in range(n):
  b[i] = a[i] - (i+1)  
  
if n%2 ==0:
  median = b[n//2] 
else:
  median = (b[n//2]+b[(n//2)+1])//2
  
ans = 0
for i in range(n):
  ans+= abs(a[i] -(median + i+1))
  
print(ans)

Submission status: Runtime Error

Solution code:
import statistics
import math

N=int(input())
A=[int(x) for x in input().split()]
def ka(b):
  k=0
  for i in range(N):
    k += abs(A[i]-(b+i+1))
  return k

B=[]
for j in range(N):
  B +=[A[j]-j-1]
B=sorted(B)

result=float('inf')
for k in range(B[N//2],B[N//2+2]):
  result =min(result,ka(k))
print(result)

Submission status: Runtime Error

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
s=[0 for x in range(n)]
 
for i in range(n):
  a[i] -= i+1
 
a.sort()
 
if n%2:
　s=0
  for i in range(n):
    s += abs(a[i]-a[n//2])
  print(s)
else:
  m=10**14
  for i in range(a[n//2-1],a[n//2]+1):
    s = 0
    for j in range(n):
      s += abs(a[j]-i)
    m = min(m,s)
  print(s)

Submission status: Runtime Error

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
s=[0 for x in range(n)]
 
for i in range(n):
  a[i] -= i+1
 
a.sort()
 
s=0
if s%2:
  for i in range(n):
    s += abs(a[i]-a[n//2])
  print(s)
else:
  m=10**14
  v=(a[n//2-1]+a[n//2])//2
  for i in range(v,v+2):
    s = 0
    for j in range(n):
      s += abs(a[j]-i)
    m = min(m,s)
  print(s)
n=int(input())
a=[int(x) for x in input().split()]
s=[0 for x in range(n)]

for i in range(n):
  a[i] -= i+1

a.sort()

if n%2:
  s=0
  for i in range(n):
    s += abs(a[i]-a[n//2])
  print(s)
else:
  m=10**14
  v=(a[n//2-1]+a[n//2])//2
  for i in range(a[n//2-1],a[n//2]+1):
    s = 0
    for j in range(n):
      s += abs(a[j]-i)
    m = min(m,s)
  print(s)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))

li = []

for i in range(n):
    memo = a[i]-(i+1)
    li.append(memo)

li.sort()

memo = len(li)
memo2 = li[memo//2]

li2 = [memo2 for i in range(len(li))]
li = li - li2

ttl = 0
for i in range(n):
    ttl += abs(li[i])

print(ttl)

Submission status: Runtime Error

Solution code:
import math
n=int(input())
a=list(map(int,input().split()))

for i in range(n):
    a[i] -= i+1

# ave = sum(a)/n
# ave1= math.ceil(ave)
# ave2= math.floor(ave)


# print(min(sum([abs(a[i]-ave1) for i in range(n)]),sum([abs(a[i]-ave2) for i in range(n)])))

a.sort()
med1=a[n//2-1]
med2=a[n//2-2]
med3=a[n//2]

print(min(sum([abs(a[i]-med1) for i in range(n)]),sum([abs(a[i]-med2) for i in range(n)]),sum([abs(a[i]-med3) for i in range(n)])))

Submission status: Runtime Error

Solution code:
import sys
from math import floor, ceil
inputs = sys.stdin.readlines()

n = int(inputs[0])
A = list(map(int, inputs[1].split()))

B = list(map(lambda x: x[1]-x[0], enumerate(A, 1)))
b = floor(sum(B) / n)
b1 = round(sum(B) / n)
b2 = ceil(sum(B) / n)
C = list(map(lambda x: abs(x-b), B))
C1 = list(map(lambda x: abs(x-b1), B))
C2 = list(map(lambda x: abs(x-b2), B))
print(min(list(map(sum, [C, C1, C2])))

Submission status: Runtime Error

Solution code:
n = int(inputs[0])
A = list(map(int, inputs[1].split()))

B = list(map(lambda x: x[1]-x[0], enumerate(A, 1)))
b = floor(sum(B) / n)
b1 = round(sum(B) / n)
b2 = ceil(sum(B) / n)
C = list(map(lambda x: abs(x-b), B))
C1 = list(map(lambda x: abs(x-b1), B))
C2 = list(map(lambda x: abs(x-b2), B))
print(min(list(map(sum, [C, C1, C2])))

Submission status: Runtime Error

Solution code:
import sys
inputs = sys.stdin.readlines()
n = int(inputs[0])
A = list(map(int, inputs[1].split()))

B = list(map(lambda x: x[1]-x[0], enumerate(A, 1)))
b = floor(sum(B) / n)
C = list(map(lambda x: abs(x-b), B))
print(sum(C))

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
A = np.array([int(i) for i in input().split()], 
             dtype = 'int64')
A = A - np.arange(N) - 1
B = np.abs(A - np.reshape(A, (-1, 1)))
print(np.sum(B, axis=1).min())

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
A = np.array([int(i) for i in input().split()])
A = A - np.arange(N) - 1
B = np.abs(A - np.reshape(A, (-1, 1)))
print(np.sum(B, axis=1).min())

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
A = np.array([int(i) for i in input().split()])
A = A - np.arange(N) - 1
B = A - np.reshape(A, (-1, 1))
print(np.sum(B, axis=1).max())

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!
N=int(input())
A=list(map(int,input().split()))

high=max(A)
low=min(A)

l=list(range(low-N,high+1))

ans=10**9
for item in l:
    temp=0
    for i in range(N):
        temp+=abs(A[i]-i-item)
        
    ans=min(ans,temp)
print(ans)

#遅そう

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=map(int,input().split())
    B=[a-i for i,a in enumerate(A)]
    B.sort()
    f=lambda x:sum(abs(x-b) for b in B)
    ans=min(f(B[n//2]),f(B[n//2+1]))
    print(ans)
resolve()

Submission status: Runtime Error

Solution code:
5
2 2 3 5 5

Submission status: Runtime Error

Solution code:
import bisect
n=int(input())
lists=list(map(int,input().split()))
uselists=[]
for _ in range(n):
    uselists.append(lists[_]-_-1)
uselist=sorted(uselists)
if n%2==0:
    num1=int(n/2-1)
    num2=int(n/2)
    numbers=min(uselist[num1],uselist[num2])
if n%2!=0:
    num1=int((n-1)/2)
    numbers=uselist[num1]  
for x in range(n):
    ans+=abs(uselist[x]-numbers)
print(ans)

Submission status: Runtime Error

Solution code:
import statistics
n=int(input())
box=list(map(int,input().split()))
box_m=sorted(list(y-(x+1) for x,y in enumerate(box)))
median=int(statistics.median(box_m))
count=0
for x,y in enumerate(box):
    count += abs(y-(median+x+1))
print(count)

Submission status: Runtime Error

Solution code:
import statistics
n=int(input())
box=list(map(int,input().split()))
box_m=sorted([y-(x+1) for x,y in enumerate(box)])
median=int(statistics.median(box_m))
count=0
for x,y in enumerate(box):
    count +=abs(y-(median+x+1))
print(count)

Submission status: Runtime Error

Solution code:
import statistics
N = int(input())
A = [int(a)-(i+1) for i, a in enumerate(input().split())]
ans = float('inf')
medi = statistics.median(A)
for b in range(medi-1, medi+2):
  ans = min(ans, sum([abs(A[i]-b) for i in range(N)]))
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
l=list(map(int,input().split()))
for i in range(n):
  l[i]=l[i]-i-1
l=sorted(l)
if n%2==0:
  mid=(l[n/2-1]+l[n/2])
else:
  mid=l[n/2]
sum=0
for i in range(n):
  sum+=abs(l[i]-mid)
print(sum)

Submission status: Runtime Error

Solution code:
N = int(input().strip())
A = list(map(int, input().strip().split(' ')))
B = [a-(i+1) for i,a in enumerate(A)]
avg = int(sum(B)/len(B))
ans = float('inf')
for i in range(-2,3):
  ans = min(ans, sum([abs(b-(avg+i)) for b in B])
print(ans)

Submission status: Runtime Error

Solution code:
b = sorted([a[i] - i - 1 for i in range(n)])
c = [i + (0 - b[0]) for i in b]

d = c[0]
ans = []
count = 0
while c[-1] != 0:
    ans.append(sum([abs(i) for i in c]))
    for e, name in enumerate(c):
       if name >0:
           break
    c = [i - name for i in c]
    if count >0:
        if ans[-1] >= ans[-2]:
            break
    count += 1

ans.append(sum([abs(i) for i in c]))
print(min(ans))

Submission status: Runtime Error

Solution code:
from statistics import median as m
n=int(input())
A=list(map(int,input().split()))
B=sorted([A[i]-i-1 for i in range(n)])
m(B)=mb
print(int(sum([abs(b-mb) for b in B])))

Submission status: Runtime Error

Solution code:

def fact(n):
  if 1<n:
    return n + fact(n-1)
  else:
    return n


N = int(input())
A = list(map(int,input().split()))
b = int(round(float((sum(A)-fact(N)))/N))
if b<0:
    b -= 1

ans = 0

for i in range(N):
    ans += abs(A[i]-(b+i+1))

print(ans)

Submission status: Runtime Error

Solution code:

def fact(n):
  if 1<n:
    return n + fact(n-1)
  else:
    return n


N = int(input())
A = list(map(int,input().split()))
b = round(float((sum(A)-fact(N)))/N)
if b<0:
    b -= 1

ans = 0

for i in range(N):
    ans += abs(A[i]-(b+i+1))

print(ans)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
from collections import  Counter
import statistics
import math
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

n = ni()
a_li = na()
for i in range(n):
    a_li[i] = a_li[i] - (i + 1)
m1 = statistics.median_low(a_li)
#m2 = statistics.median_high(a_li)
ans1 = 0
#ans2 = 0
for i in range(n):
    ans1 += abs(a_li[i] - m1)
    #ans2 += abs(a_li[i] - m2)
print(min(ans1))

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
A=[]
for i in range(n):
  A.append(a[i]-i-1)
A.sort()
b=0
c=0
ans=0
Ans=0
if n%!=0:
  b=n//2
  for i in range(n):
    ans+=abs(A[i]-b)
  print(ans)
else:
  b=n//2
  c=(n//2)-1
  for i in range(n):
    ans+=abs(A[i]-b)
    Ans+=abs(A[i]-c)
  print(min(ans,Ans))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
t = list(sorted(a[i] - (i + 1) for i in range(n)))
if len(t) % 2 == 0:
  b = (t[len(t)] + t[len(t + 1)]) // 2
else:
  b = t[len(t) // 2]
print(sum(abs(a[i] - (b + i + 1)) for i in range(n)))

Submission status: Runtime Error

Solution code:
from math import log2
n = int(input())
a = list(map(int, input().split()))
mean = sum(a)//n
log = int(log2(n))

def sadness(b) :
    return sum(abs(a[i]-b-i-1) for i in range(n))

print(min(sadness(mean-n//2+i) for i in range(-log, log)))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = abs(n- max(a))
ans = 0

for i in range(n):
    ans += abs(a[i]-(b+i+1))

print(sum(ans))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))

aa = [a[i] - i for i in range(n)]
aa.sort()

print(min(aa[n//2]))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
total = 0
for i in range(n):
    a[i] = a[i] - (i + 1)
if n % 2 == 0:
    ave = a[int(n/2)]
    for i in range(n):
        total += abs(a[i] - ave)
    print(total)
else:
    total2 = 0
    ave = a[int(n/2)]
    for i in range(n):
        total += abs(a[i] - ave)
    ave = a[int((n + 1)/2)]
    for i in range(n):
        total2 += abs(a[i] - ave)
    if total2 >= total:
        print(total)
    else:
        print(total2)