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: Accepted

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

ans = sum(A)

imin = min(A)-N
imax = max(A)+N

def calcAns(b):
        global A
        return sum([abs(A[i]-(b+i+1)) for i in range(N)])

while True:
        if imin == imax:
                break
        else:
                imid = imin + (imax - imin)//2
                tmp = calcAns(imid)
                if calcAns(imid+1)<tmp:
                        imin = imid+1
                elif calcAns(imid-1)<tmp:
                        imax = imid-1
                else:
                        imin = imid
                        imax = imid

print(calcAns(imin))

Submission status: Accepted

Solution code:
import numpy as np
import math
n=int(input())
a=list(map(int, input().split(" ")))
b=[i-(num+1) for num,i in enumerate(a)]
#print(np.mean(b))
#print(b)

b.sort()
#print(b)
if n%2==1:
    c1=[abs(i-(num+b[int((n-1)/2)]+1)) for num,i in enumerate(a)]
    print(np.sum(c1))
else:
    c1=[abs(i-(num+b[int(n/2)]+1)) for num,i in enumerate(a)]
    c2=[abs(i-(num+b[int(n/2+1)]+1)) for num,i in enumerate(a)]
    print(min(np.sum(c1),np.sum(c2)))

Submission status: Accepted

Solution code:
import statistics
N = int(input())
L = list(map(int,input().split( )))
f1 = [li-j for li,j in zip(L,range(1+N+1))]
b = statistics.median(f1)
if N%2 != 0:
	ans = sum([abs(li-j-b) for li,j in zip(L,range(1+N+1))])
else:
	b1 = int(b+0.5)
	b2 = int(b-0.5)
	ans1 = sum([abs(li-j-b1) for li,j in zip(L,range(1+N+1))])
	ans2 = sum([abs(li-j-b2) for li,j in zip(L,range(1+N+1))])
	ans = min(ans1,ans2)
print(ans)

Submission status: Accepted

Solution code:
import math, string, fractions, heapq, re, array, bisect, sys, random, time, copy
import itertools as it
import collections as cl
import functools as ft
import numpy as np


def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def calc_lament(a, b):
    return sum([abs(ai - b) for ai in a])


n = int(input())
a = LI()

a = [ai - i for ai, i in zip(a, range(1, n+1))]

b = int(np.median(a))
print(calc_lament(a, b))

Submission status: Accepted

Solution code:
N = int(input())
nums = input().split(" ")
nums2 = [int(x)-i-1 for i,x in enumerate(nums)]
nums2.sort()
length = len(nums2)
numpre = 0
same = 1
if length == 1:
  value = nums2[0]
 
for n,m in zip(nums2[:-1],nums2[1:]):
  if n == m:
    same += 1
  else:
    impact = same + numpre
    if impact > length-impact:
      value = n
      break
    numpre += same
    same = 1
  value = m
 
answer = 0
for i in nums2:
  answer += abs(i-value)
print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

AA=[]
for i,a in enumerate(A):
    AA.append(a-i+1)

b=np.median(AA)
ans=0

for a in AA:
    ans+=abs(a-b)
print(int(ans))

Submission status: Accepted

Solution code:

str_n = input()
n = int(str_n)
str_a = input()
arr_a = str_a.split(' ')
# newn
arr_ia = []
for i, item in enumerate(arr_a):
    arr_ia.append(int(item)-(i+1))

arr_ia.sort()
med = arr_ia[int(len(arr_ia)/2)]

score = 0
for n in arr_ia:
    score += abs(n-med)
    
print(score)

Submission status: Accepted

Solution code:
def run(n, a):
    a = [a[i]-i-1 for i in range(len(a))]
    a.sort()
    m = a[int((n-1)/2)]
    ret = 0
    for ai in a:
        ret += abs(ai - m)
    return ret


def read_line():
    n = int(input())
    a = list(map(int, input().split()))
    return (n, a)


def main():
    n, a = read_line()
    print(run(n, a))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

a = []
for i in range(n):
    a.append(aa[i]-(i+1))
b = list(sorted(a))

ans = 0

for i in range(n//2):
    ans += b[-i-1] - b[i]
print(ans)

Submission status: Accepted

Solution code:
n = int(raw_input())
a = map(int, raw_input().split())
for i in range(len(a)):
  a[i] -= (i+1)


b = sorted(a)
i = n // 2
what = b[i]
print(sum([abs(t - what) for t in b]))

Submission status: Accepted

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

b = [a[i]-i-1 for i in range(n)]
b.sort()
m = int(n//2)
c=[abs(b[i]-b[m]) for i in range(n)]
print(sum(c))

Submission status: Accepted

Solution code:
import statistics
N = int(input())
A = [int(x) for x in input().split(' ')]
def score(C, b):
    s = 0
    for i, Ci in enumerate(C):
        s += abs(Ci - b)
    return int(s)

C = [Ai - (i+1) for i, Ai in enumerate(A)]
b = statistics.median(C)
b1 = int(b)
b2 = int(b) + 1
s1 = score(C, b1)
s2 = score(C, b2)
print(min(s1, s2))

Submission status: Accepted

Solution code:
def getMedian(data):
    N = len(data)
    data.sort()
    if N % 2 == 0:
        median1 = N / 2
        median2 = N / 2 + 1
        median1 = int(median1) - 1
        median2 = int(median2) - 1
        return int((data[median1] + data[median2]) / 2)
    else:
        median = (N + 1) / 2
        median = int(median) - 1
        return int(data[median])


int(input())
A = list(map(int, input().split()))
B = []
for i in range(len(A)):
    B.append(A[i] - i - 1)

median = getMedian(B)

ans = 0

for b in B:
    ans += abs(b - median)

print(ans)

Submission status: Accepted

Solution code:
import math, statistics
n=int(input())
l=list(map(int, input().split()))
for i in range(n):
    l[i]-=(i+1)
b=statistics.median(l)
sad=0
for i in l:
    sad+=abs(i-b)
print(int(sad))

Submission status: Accepted

Solution code:
def calc(b,A,N):
    sadness = 0
    for i in range(N):
        sadness += abs(A[i] - (b + i))
    return sadness

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

k = []
for i in range(N):
    k.append(A[i] - i)

k = sorted(k)

sadness = 0
if N % 2 == 1:
    sadness = calc(k[N//2],A,N)
else:
    sadness = min(calc(k[N//2],A,N),calc(k[(N//2)+1],A,N))

print(sadness)

Submission status: Accepted

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

a_max = max(a)
a_min = min(a) - n

t = (a_min + a_max) // 2

while (a_min <= a_max):
    plus_minus = 0
    for i,data in enumerate(a):
        if data < t + i + 1:
            plus_minus -= 1
        elif data > t + i + 1:
            plus_minus += 1

    if (plus_minus == 0):
        break
    elif (plus_minus > 0):
        a_min = t + 1
    elif (plus_minus < 0):
        a_max = t - 1
    t = (a_min + a_max) // 2

kanasisa = 0
for i,data in enumerate(a):
    kanasisa += abs(data - (t + i + 1))

print(kanasisa)

Submission status: Accepted

Solution code:
import math
def inpl(): return list(map(int, input().split()))
N = int(input())
A = inpl()

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

if N%2 == 1:
    medR = A[N//2]
    medL = A[N//2]
else:
    medR = math.ceil((A[N//2-1]+A[N//2])/2)
    medL = (A[N//2-1]+A[N//2])//2
    
ansR, ansL = 0, 0
for i in A:
    ansR += abs(i-medR)
    ansL += abs(i-medL)

print(min(ansR,ansL))

Submission status: Accepted

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

tmp = []

for idx, e in enumerate(A):
    tmp.append(e - (idx + 1))

tmp.sort()

b = tmp[len(tmp) // 2]

res = 0
for idx, e in enumerate(A):
    res += abs(e - (idx + 1) - b)

print(res)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
a = np.array(list(map(int, input().split())))
a = a - np.arange(n)
a = np.sort(a)
a = a - a[int(n/2)]
print(sum(abs(a)))

Submission status: Accepted

Solution code:
N = int(input())
A_list = [int(elem) for elem in input().split(' ')]
assert len(A_list) == N

modified_A_list = [A - i for i, A in enumerate(A_list, start=1)]

minus_A_list = sorted([A for A in modified_A_list if A < 0])[::-1]
zero_A_list = [A for A in modified_A_list if A == 0]
plus_A_list = sorted([A for A in modified_A_list if A > 0])
#print(minus_A_list, zero_A_list, plus_A_list)

len_minus = len(minus_A_list)
len_zero = len(zero_A_list)
len_plus = len(plus_A_list)
assert len_minus + len_zero + len_plus == N
current_sum = abs(sum(minus_A_list)) + sum(plus_A_list)
#print(current_sum)

if len_minus > len_plus + len_zero:
    i = 0
    old_num = 0
    while i < len(minus_A_list):
        current_num = minus_A_list[i]
        current_sum -= abs(current_num - old_num) * (len_minus - len_plus - len_zero)
        # 数の更新
        num_current_val = 0
        while i < len(minus_A_list) and (minus_A_list[i] == current_num):
            i += 1
            num_current_val += 1
        len_minus -= num_current_val
        len_zero_old = len_zero
        len_zero += num_current_val - len_zero_old
        len_plus += len_zero_old
        old_num = current_num
        if len_minus <= len_plus + len_zero:
            break

elif len_plus > len_minus + len_zero:
    i = 0
    old_num = 0
    while i < len(plus_A_list):
        current_num = plus_A_list[i]
        current_sum -= (current_num - old_num) * (len_plus - len_minus - len_zero)
        # 数の更新
        num_current_val = 0
        while i < len(plus_A_list) and plus_A_list[i] == current_num:
            i += 1
            num_current_val += 1
        len_plus -= num_current_val
        len_zero_old = len_zero
        len_zero += num_current_val - len_zero_old
        len_minus += len_zero_old
        old_num = current_num
        if len_plus <= len_minus + len_zero:
            break

print(current_sum)

Submission status: Accepted

Solution code:
from statistics import *



n = int(input())
list = input().split()

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

b = int(median(list))

for i in range(n):
    list[i] = abs(list[i] - b)

print(sum(list))

Submission status: Accepted

Solution code:
from statistics import median
import math
N = int(input())
*A, = map(int, input().split())
B = [A[i]-i for i in range(N)]
b = median(B)
bm = math.floor(b)
K = [x - bm for x in B]
KZ = [abs(x) for x in K]
Z = sum(KZ)
for i in range(20):
    C = [abs(x-i+10) for x in K]
    Z = min([sum(C),Z])
print(int(Z))

Submission status: Accepted

Solution code:
from decimal import *
N = int(input())
a = list(map(int, input().split()))
c = a.copy()
for i in range(len(a)):
    c[i] = a[i] - i - 1

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

greef1 = 0
for i in range(len(a)):
    greef1 += abs(a[i] - i - 1 - b)
print(int(greef1))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = []
    data = input().split()
    for i in range(N):
        A.append(int(data[i]))
    C = [A[i] - i - 1 for i in range(N)]
    C.sort()
    m = N // 2
    x = C[m]
    left = sum([x - C[i] for i in range(m)])
    right = sum([C[i] - x for i in range(m + 1, N)])
    print(left + right)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math
import statistics

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

b = [a[i]-(i+1) for i in range(n)]
median = statistics.median(b)

med1 = math.floor(median)
med2 = med1+1

t = sum([abs(i-med1) for i in b])
u = sum([abs(i-med2) for i in b])

print(min(t,u))

Submission status: Accepted

Solution code:
def main():
	N = int(input())
	seq = list(map(int, input().split()))
	seq = [seq[i] - i - 1 for i in range(N)]
	se = sorted(list(set(seq)))
	def helper(b):
		return sum([abs(seq[i] - b) for i in range(N)])
	L = 0
	R = len(se)
	while True:
		ind = (L + R) // 2
		temp = helper(se[ind])
		if helper(se[ind - 1]) > temp and temp > helper(se[ind + 1]):
			L = ind
		elif helper(se[ind - 1]) < temp:
			R = ind
		else:
			print(temp)
			return 0
main()

Submission status: Accepted

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

for i in range(n):
    b.append(a[i]-i-1)
    
c = sorted(b)

if n%2 ==0:
    kas = c[int(n/2)]
else:
    kas = c[int((n+1)/2)-1]



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

Submission status: Accepted

Solution code:
import numpy as np

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

if N % 2 == 0:
    C = int(N / 2)
else:
    C = int((N+1)/2)


for j in range(0,N):
    A[j] = A[j]-j-1

A1 = np.array(A)

B = np.sort(A1)

if B[C-1] > 0:
    d = B[C-1]
    B2 = B - d
    print(sum(np.abs(B2)))
else:
    d = -B[C-1]
    B2 = B + d
    print(sum(np.abs(B2)))

Submission status: Accepted

Solution code:
N = int(raw_input())
A = map(int, raw_input().split())
d = []
for i in range(N):
  d.append(A[i]-(i+1))
if N%2==0:
  B = sorted(d)[N//2-1:N//2+1]
else:
  B = [sorted(d)[N//2]]
r = []
for b in B:
  s = 0
  for i in range(N):
    s += abs(A[i]-(b+i+1))
  r.append(s)
print min(r)

Submission status: Accepted

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

a.sort()
b = a[n//2]
a = list(map(lambda x: abs(x-b), a))
print(sum(a))

Submission status: Accepted

Solution code:
import sys

N = int(raw_input())
A = [int(x)-(i+1) for i,x in enumerate(raw_input().strip().split())]
A.sort()
medians = []
if len(A)%2==1:
    medians.append(A[len(A)/2])
else:
    medians.append(A[len(A)/2])
    medians.append(A[len(A)/2 -1])

result = sys.maxint
for m in medians:
    temp = sum(abs(a-m) for a in A)
    result = min(result, temp)

print result

Submission status: Accepted

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

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

A.sort()
# print(A)
# print(N // 2)
b = A[N // 2]

sum = 0
for i in range(N):
    sum = sum + max(A[i] - b, -A[i] + b)

print(sum)

Submission status: Accepted

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

A.sort()


x = A[N // 2]
S = 0
for i in range(N):
	S += abs(A[i] - x)

print(S)

Submission status: Accepted

Solution code:
if __name__=="__main__":
  N = int(input())
  A = list(map(int, input().split()))
  B = [A[i]-(i+1) for i in range(N)] 
  sortedB = sorted(B)
  optB = sortedB[int(N/2)]
  print(sum([abs(value-optB) for value in sortedB], 0))

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
c=0
d=[]
for i in range(n):
    d.append(a[i]-i-1)
d.sort()
#print(d)
if n%2==1:
    b=d[int((n+1)/2)-1]
else:
    b=int(((d[int(n/2-1)]+d[int(n/2)]))/2)
for i in range(n):
    c=c+abs(a[i]-b-i-1)
#print(b)
print(c)

Submission status: Accepted

Solution code:
from inspect import currentframe

def chkprint(*args):
    names = {id(v):k for k,v in currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))

from functools import reduce
from math import floor, ceil

N = int(input())
A = list(map(lambda s: int(s[1])-s[0]-1, enumerate(input().split())))

A.sort()

if N%2 == 0:
  m0 = N//2
  m1 = m0 - 1
  a0 = floor((A[m0] + A[m1])/2)
  a1 = ceil((A[m0] + A[m1])/2)
  # chkprint(m0)
  # chkprint(m1)

  print(min(reduce(lambda s, x: s + abs(x - a0), A, 0), reduce(lambda s, x: s + abs(x - a1), A, 0)))
else:
  m = N//2
  a = A[m]
  # chkprint(a)
  print(reduce(lambda s, x: s + abs(x - a), A, 0))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a = [x-i-1 for i,x in enumerate(a)]

a = sorted(a)

med1 = a[len(a)//2]
med2 = a[len(a)//2 - 1]



sum1 = sum([abs(x - med1) for x in a])
sum2 = sum([abs(x - med2) for x in a])
print(min(sum1, sum2))

Submission status: Accepted

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


c = n // 2
b = a[c]

sum = 0

for i in range(n):
	if i <= c:
		sum += b - a[i]
	else:
		sum += a[i] - b

print(sum)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    ai = [int(i) for i in input().split()]

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

    ai.sort()

    b = ai[n//2]
    m = sum([abs(i - b) for i in ai])
    print(m)
        
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())

    a = [int(i) for i in input().split()]
    for i in range(n):
        a[i] -= i;

    a.sort()

    m = n // 2

    ans = 0
    for i in a:
        ans += abs(i - a[m])

    print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(x) for x in input().split()]
for i in range(1,N+1):
  A[i-1]-=i
A.sort()
cnt=A[N//2]
ans=0
for i in range(N):
  ans+=abs(A[i]-cnt)
print(ans)

Submission status: Accepted

Solution code:
from statistics import median

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

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

median = round(median(A))

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

print(ans)

Submission status: Accepted

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

c = [a[i] - (i+1) for i in range(0, n)]
c.sort()
b = c[int(n/2)]
print(sum((abs(c[i] - b) for i in range(0, n))))

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = list(map(int,input().split()))
r = np.arange(n) + 1
d = a - r
b = int(np.ceil(np.median(d)))
print(np.sum(np.abs(d - b)))

Submission status: Accepted

Solution code:
n=int(input())
a0=input().split()
a=[]
for i in range(n):
	a.append(int(a0[i])-i)
a.sort()
j=0
for i in range(n):
	j+=abs(a[i]-a[n//2])
print(j)

Submission status: Accepted

Solution code:
import numpy as np
import math

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

B = A - np.arange(1, N + 1)
m = np.median(B)
print(int(np.abs(B - m).sum()))

Submission status: Accepted

Solution code:
from statistics import median

n = int(input())
xs = [int(x) for x in input().split()]

ys = [x-i-1 for i, x in enumerate(xs)]
m = int(median(ys))

print(sum(abs(y-m) for y in ys))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]

# from itertools import accumulate

xs = sorted(a - i - 1 for i, a in enumerate(A))

c = xs[len(xs)//2]

r = 0
for x in xs:
    r += abs(x - c)

print(r)

Submission status: Runtime Error

Solution code:
n=int(input())
alist=list(map(int, input().split()))
blist=[]
for i in range(n):
  blist.append(alist[i]-i-1)
blist.sort()
if n%2==1:
  cnt=0
  for i in range(n):
    cnt+=abs(blist[i]-blist[(i+1)//2])
  print(cnt)
else:
  cnt=0
  num=(blist[i//2]+blist[i//2+1])//2
  for i in range(n):
    cnt+=abs(blist[i]-num
  print(cnt,cnt2)

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()
if N%2==0:
  a=N/2
else:
  a=(N+1)/2
b=B[a-1]
for i in range(N):
  B[N-1]=abs(B[N-1]-b)
print(B)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=A.copy()
B.sort()
A_max=B[N-1]
sum_A=[0]*(A_max+1)
for i in range(A_max+1):
  for num in range(N):
    sum_A[i]+=abs(A[num]-i-num)
sum_A.sort()
print(sum_A[0])

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=A.copy()
B.sort()
A_max=B[N]+N
sum_A=[0]*(A_max+1)
for i in range(A_max+1):
  for num in range(N):
    sum_A[i]+=abs(A[num]-i)
sum_A.sort()
print(sum_A[0])

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=A.copy()
B.sort()
A_max=B[N-1]+N
sum_A=[0]*(A_max+1)
for i in range(A_max+1):
  for num in range(N):
    sum_A[i]+=abs(A[num]-i)
sum_A.sort()
print(sum_A[0])

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
B=A.copy()
B.sort()
A_max=B[N-1]+N
sum_A=[0]*N
for i in range(A_max+1):
  for num in range(N):
    sum_A[i]+=abs(A[num]-i)
sum_A.sort()
print(sum_A[0])

Submission status: Runtime Error

Solution code:
あ

Submission status: Runtime Error

Solution code:
import math
n = int(input())
ls = list(map(int,input().split()))
bs = [ls[i]-i-1 for i in range(n)]
bs.sort()

if n %2 ==0:
    a = int(n/2)
    can = [bs[a],bs[a-1],bs[a+1]]
else:
    a = math.floor(n/2)
    can = [bs[a], bs[a+1],bs[a-1]]

#print(can)
result = 10**10
for k in can:
    ans =0
    for kn in bs:
        ans += abs(kn-k)
    
    result = min(result, ans)
print(result)

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
print(sorted(l))
if n % 2 == 1:
    print(sum(list(map(lambda x: x-l[((n//2)+1)],l))))
else:
    print(max(sum(list(map(lambda x: x-l[(n//2)],l))),sum(list(map(lambda x: x-l[((n//2)+1)],l)))))

Submission status: Runtime Error

Solution code:
from statistics import median

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

for i in range(N):
    an[i] = an[i] - (i+1)
    
b = int(median(bn))

k = []
for i in range(N):
    y = abs(an[i] - b)
    k.append(y)
ans = sum(k)

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
li = sorted([a[i]-(i+1) for i in range(n)])
if n%2==0:
    ave = (li[n//2]+li[n//2-1])//2
else:
    ave = li[(n-1)//2]

import math
ave1 = math.floor(ave)
ave2 = math.ceil(ave)

ll = []
c = 0
for i in range(n):
    c += abs(a[i]-(ave1+i+1))
else:
    ll += [c]

c =0
for i in range(n):
    c += abs(a[i]-(ave2+i+1))
else:
    ll += [c]

print(min(ll))

Submission status: Runtime Error

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

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

b.sort()

m1 = b[n//2]
s1 = sum([abs(b[i] - m1) for i in range(n)])

m2 = b[n//2 + 1]
s2 = sum([abs(b[i] - (m2)) for i in range(n)]) 

print(min(s1, s2))

Submission status: Runtime Error

Solution code:
n=int(input())
A=list(map(int,input().split()))
A=[A[i]-i-1 for i in range(n)]
A.sort()
if n%2==1:
    d=A[n//2+1]
    print(sum([abs(A[i]-d) for i in range(n)]))
    exit()
d=(A[n//2-1]+A[n//2])//2
print(sum([abs(A[i]-d) for i in range(n)]))

Submission status: Runtime Error

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

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

arr.sort()

ans = []

s = 0
b = arr[n//2]
for i in range(n):
    s += abs(a[i] - (b + (i+1)))
ans.append(s)

s = 0
b = arr[n//2 + 1]
for i in range(n):
    s += abs(a[i] - (b + (i+1)))
ans.append(s)

print(min(ans))

Submission status: Runtime Error

Solution code:
from math import ceil
n = int(input())
arr = list(map(int, input().split()))
B = []
for i in range(n):
    B.append(arr[i] - (i+1))
B.sort()
b1 = B[n//2]
b2 = B[int(ceil(n/2))]
ans1, ans2 = 0, 0
for i in range(n):
    ans1 += abs(arr[i]-(b1+i+1))
    ans2 += abs(arr[i]-(b2+i+1))
print (min(ans1, ans2))

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
long long mod = 1e9+7;
const int maxn=2e5+7;

long int sad(vector<int> &arr,int b){
    long int sad_value = 0;
    for(int i=0;i<arr.size();i++){
        sad_value+=abs(arr[i] - (b+i));
    }
    return sad_value;
}

int main(){
    long lo=-1e9,hi=1e9;
    int n;
    cin>>n;
    vector<int> arr(n);
    for(int i=0;i<n;i++) cin>>arr[i];
    long int psad,sadv,nsad;
    while(lo<=hi){
        int b = lo + (hi-lo)/2;
        if(b==0 || b==1e9){
            sadv = sad(arr,b);
        }
        psad = sad(arr,b-1);
        sadv = sad(arr,b);
        nsad = sad(arr,b+1);
        // cout<<psad<<' '<<sadv<<' '<<nsad<<' '<<b<<endl;
        if(psad>=sadv && sadv<=nsad){
            break;
        }
        else if(psad>sadv && sadv >nsad){
            lo = b+1;
        }
        else{
            hi = b-1;
        }
    }
    cout<<sadv;
}

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
import math
import copy
input = sys.stdin.readline
 
N = int(input())
A = np.array(list(map(int,input().split())))
r = np.array(list(range(1,N+1)))
B = np.array(sorted(A-r))
ans = float('inf')
for i in range(max(N//2-1,0),max(N//2+2,N)):
    c = list(map(abs,(B - B[i])))
    ans = min(ans,sum(c))
print(ans)

Submission status: Runtime Error

Solution code:
mport sys
import numpy as np
import math
import copy
input = sys.stdin.readline
 
N = int(input())
A = np.array(list(map(int,input().split())))
r = np.array(list(range(1,N+1)))
B = np.array(sorted(A-r))
ans = float('inf')
for i in range(max(N//2-1,0),max(N//2+2,N)):
    c = list(map(abs,(B - B[i])))
    ans = min(ans,sum(c))
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
import math
input = sys.stdin.readline
 
N = int(input())
A = np.array(list(map(int,input().split())))
r = np.array(list(range(1,N+1)))
B = A-r
C = list(map(abs,B-(math.ceil(sum(B)/len(B)))))
D = list(map(abs,B-(math.floor(sum(B)/len(B)))))
E = list(map(abs,B-(B[N//2])))
F = list(map(abs,B-(B[N//2+1])))

ans = min(sum(C),sum(D))
ans = min(ans,sum(E))
ans = min(ans,sum(F))

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a_list = list(map(int,input().split()))
ans_list = []
for i in range(len(a_list)):
    ans_list.append(a_list[i] - i - 1)
import numpy as np
if len(ans_list)% 2 == 0:
    cary1 = np.delete(ans_list, min(ans_list))
    cary2 = np.delete(ans_list, max(ans_list))
    number1 = int(np.median(cary1))
    number2 = int(np.median(cary2))
    
    ans1 = 0
    ans2 = 0
    for i in ans_list:
        ans1 += abs(i - number1)
        ans2 += abs(i - number2)
    print(min(ans1 , ans2))
else:
    cary = ans_list
    number = int(np.median(cary))
    ans = 0
    for i in ans_list:
        ans += abs(i - number)
    print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a_list = list(map(int,input().split()))
ans_list = []
for i in range(len(a_list)):
    ans_list.append(a_list[i] - i - 1)
import numpy as np
if len(ans_list)% 2 == 0:
    cary = np.delete(ans_list, min(ans_list))
    
else:
    cary = ans_list
number = int(np.median(cary))
ans = 0
for i in ans_list:
    ans += abs(i - number)
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
A = np.array(list(map(int, input().split(' '))))
B = np.arange(1, N + 1)

C = A - B
mean = int(np.mean(C))

print(min(np.sum(np.abs(C - mean))), np.sum(np.abs(C + mean)))

Submission status: Runtime Error

Solution code:
n = int(input())
a_list = list(map(int,input().split()))
ans_list = []
for i in range(len(a_list)):
    ans_list.append(a_list[i] - i - 1)
import numpy as np
if len(ans_list)% 2 == 0:
    cary1 = np.delete(ans_list, max(ans_list))
    cary2 = np.delete(ans_list, min(ans_list))
    number1 = int(np.median(cary1))
    number2 = int(np.median(cary2))
    ans1 = 0
    for i in ans_list:
        ans1 += abs(i - number1)
    ans2 = 0
    for i in ans_list:
        ans2 += abs(i - number2)
    print(min(ans1 , ans2))
    
else:
    cary = ans_list
    number = int(np.median(cary))
    ans = 0
    for i in ans_list:
        ans += abs(i - number)
    print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a_list = list(map(int,input().split()))
ans_list = []
for i in range(len(a_list)):
    ans_list.append(a_list[i] - i - 1)
import numpy as np
if len(ans_list)% 2 == 0:
    cary1 = np.delete(ans_list, max(ans_list))
    cary2 = np.delete(ans_list, min(ans_list))
    number1 = int(np.median(cary))
    number2 = int(np.median(cary))
    ans1 = 0
    for i in ans_list:
        ans1 += abs(i - number1)
    ans2 = 0
    for i in ans_list:
        ans2 += abs(i - number2)
    print(min(ans1 , ans2))
    
else:
    cary = ans_list
    number = int(np.median(cary))
    ans = 0
    for i in ans_list:
        ans += abs(i - number)
    print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a_list = list(map(int,input().split()))
ans_list = []
for i in range(len(a_list)):
    ans_list.append(a_list[i] - i - 1)
import numpy as np
if len(ans_list)% 2 == 0:
    cary = np.delete(ans_list, max(ans_list))
    
else:
    cary = ans_list
number = int(np.median(cary))
ans = 0
for i in ans_list:
    ans += abs(i - number)
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a_list = list(map(int,input().split()))
import numpy as np
print(int(np.median(a_list)) - (a_list.index(int(np.median(a_list))) + 1))

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(a)-i-1 for i,a in enumerate(input().split(),1)]
A.sort()
b = int(sum(a)/len(a))
c = 10**9
for j in range(b-10, b+11):
    k = sum(map(lambda x:abs(x-j), A))
    c = min(c,k)
print(c)

Submission status: Runtime Error

Solution code:
n=int(input())
l=list(map(int,input().split()))
m=sorted([a-i for i,a in enumerate(l, start=1)])
b=min(m[len(m)//2],m[len(m)//2+1])
print(sum([abs(a-b) for a in m]))

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  
  vector<int> A(N);
  for(int i=0; i<N; i++){
    cin >> A[i];
  }
  
  vector<int> D(N);
  for(int i=0; i<N; i++){
    D[i]=(A[i]-(i+1));
  }
  
  //bはDの中央値
  std::sort(std::begin(D), std::end(D));
  size_t median_index=D.size()/2;
  auto median = (D[median_index]);
    
  
  
  int answer=0;
  for(int i=0; i<N; i++){
    answer+= abs(D[i]-median);
  }
  cout << answer << endl;
    
}

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int , input().split()))
b = [a[i] - (i+1) for i in range(n)]
#sa = sum(a)
b.sort()
#print(b)
ans = 10 ** 10
for j in range(3):
    bi = b[n//2+j]
    s = 0
    for i in range(n):
        s += abs(b[i] - bi)
    #print(s)
    ans = min(ans, s)
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int , input().split()))
b = [a[i] - (i+1) for i in range(n)]
#sa = sum(a)
b.sort()
#print(b)
ans = 10 ** 10
for j in range(3):
    bi = b[(n + j)//2]
    s = 0
    for i in range(n):
        s += abs(b[i] - bi)
    #print(s)
    ans = min(ans, s)
print(ans)