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: Wrong Answer

Solution code:
def match_num(a, match):
    match_lst = []
    match = [j-len(match) for j in match]
    for k in range(len(match)*2):
        match_b = 0
        for i in range(len(match)):
            if a[i] == match[i]:
                match_b += 1
        match_lst.append(match_b)
        #print(match) ##
        match = [j+1 for j in match]
    #print(match_lst) ##
    return match_lst

def absnuke(a, b, n):
    sum = 0
    for i in range(n):
        sum += abs(a[i]-(b+i+1))
    return sum

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


match = [x for x in range(1,n+1)]

ans = match_num(a, match)
print(absnuke(a, ans.index(max(ans))-len(match), n))

Submission status: Wrong Answer

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

A = sum(a)
B = n * (n+1) // 2
b = (A - B) // n

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

Submission status: Wrong Answer

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

a = []

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

god = c / n
#print(god)

import math as m

kouho1 = m.ceil(god)
kouho2 = m.floor(god)
    

ans1 = 0
ans2 = 0
for i in a:
    ans1 += abs(i  - kouho1)
    ans2 += abs(i  - kouho2)
    
print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
from sys import stderr
from functools import reduce
from operator import add
def f(): return [int(i) for i in input().split()]
def debug(*x, sep=" ", end="\n"):
    for item in x:
        stderr.write(repr(item))
        stderr.write(sep)
    stderr.write(end)

n = int(input())
a = f()

b1 = (sum(a) - sum(range(1,n+1))) // n
b2 = b1 + 1
ans1 = 0
ans2 = 0
for i,v in enumerate(a):
    ans1 += abs(v - (b1 + i + 1))
    ans2 += abs(v - (b2 + i + 1))
print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input().strip())
    A = [int(x) for x in input().strip().split()]

    b = 0
    print(calc(b, A))


def calc(b, A):
    return sum([abs(x - (b+n+1)) for n, x in enumerate(A)])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

N = int(input())
A = list(map(int, input().split(" ")))
A_i = [0]*N
K = [0] * 101

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

b_0 = sum(A_i) / len(A_i)
b = [0] * 101
b[0] = int(b_0)
for i in range(1,101):
    b[i] = b[0] + int(i - 50.5)
print(b)

for i in range(N):
    for j in range(11):
        K[j] = K[j] + abs(A_i[i] - b[j])

print(min(K))

Submission status: Wrong Answer

Solution code:
from math import *
n=int(input())
arr=list(map(int,input().split()))
for i in range(n):
    arr[i]-=(i+1)
k=sum(arr)
l=floor(k/n)
if(l==0):
    ans1=0
    ans2=0
    ans3=0
    for i in range(n):
        ans1+=abs(arr[i])
        ans2+=abs(arr[i]-1)
        ans3+=abs(arr[i]+1)
    print(min(ans1,ans2,ans3))
else:
    ans1=0
    ans2=0
    for i in range(n):
        ans1+=abs(arr[i]-l)
        ans2+=abs(arr[i]-l-1)
    print(min(ans1,ans2))

Submission status: Wrong Answer

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(sad)

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
a = [int(x) for x in input().split()]
a2 = [0] * n

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

#print(a2)
res = [0] * n
res2 = [0] * n
mean =  sum(a2)//n
#print(mean)
for i in range(n):
    res[i] = min(abs(a2[i] - mean),abs(a2[i] + mean))

print(sum(res))

Submission status: Wrong Answer

Solution code:
from decimal import Decimal,ROUND_HALF_UP

N=int(input())
nums=list(map(int,input().split()))
ave=[]
for i,num in enumerate(nums):
  ave.append(num-(i+1))
x=int(Decimal(str(sum(ave)/N)).quantize(Decimal("0"), rounding=ROUND_HALF_UP))

ans=9999999999
for k in range(-1,2):
  preans=0
  for j in ave:
    preans=preans+abs(j-(x+k))
  if preans<ans:
    ans=preans
print(ans)

Submission status: Wrong Answer

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

b = 0
min_ans = 100000000
while True:
    ans = 0
    for i in range(len(A)):
        #print(A[i], b, i, abs(A[i] - (b + i + 1)))
        ans += abs(A[i] - (b + i + 1))
    #print("ans", b, ans)
    if min_ans >= ans: min_ans = ans
    else: break
    b += 1

b = -1
while True:
    ans = 0
    for i in range(len(A)):
        #print(A[i], b, i, abs(A[i] - (b + i + 1)))
        ans += abs(A[i] - (b + i + 1))
    #print("ans", b, ans)
    if min_ans >= ans: min_ans = ans
    else: break
    b -= 1

print(min_ans)

Submission status: Wrong Answer

Solution code:
import math

def read_line(*types): return [f(a) for a, f in zip(input().split(), types)]

n, = read_line(int)

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

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

min_sadness = float('inf')
avg = sum(AA) / len(AA)

b1 = math.ceil(avg)
b2 = math.floor(avg)

sadness1 = 0
for a in AA:
    sadness1 += abs(a - b1)

sadness2 = 0
for a in AA:
    sadness2 += abs(a - b2)

print(min(sadness1, sadness2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

res = 0
for i in range(N):
    res += A[i] - (i+1)
b = res // N

ans = 99999999
for bj in range(b-5, b+5):
    res = 0
    for i in range(N):
        res += abs(A[i] - (bj+(i+1)))
    ans = min(ans, res)
print(ans)

Submission status: Wrong Answer

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

A = sum(a)
B = n * (n+1) // 2
b = (A - B) // n
print(b)

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

Submission status: Wrong Answer

Solution code:
import numpy as np

n = int(input())
arr = np.array(list(map(int,input().split())))
per = np.array([i+1 for i in range(n)])

arr = arr-per


ave = np.sum(arr)//n
ave1 = ave+1
ave2 = ave-1
ave3 = ave+2
ave4 = ave-2

m = [np.sum(np.abs(arr-ave)),np.sum(np.abs(arr-ave1)),np.sum(np.abs(arr-ave2)),np.sum(np.abs(arr-ave3)),np.sum(np.abs(arr-ave4))]


print(int(min(m)))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
i = 1
#for j in range(len(A)):
#    A[j] = A[j] - i
#    i += 1

#print(A)

B = sum(A) - N * (N + 1) // 2
#print(B)

if B == 0:
    ans = 0
    for i in range(len(A)):
        ans += max(A[i] - i, -(A[i] - i))
    print(ans)
elif B > 0:
    b = 0
    C = B - N*b
    while abs(C) > abs(C - N * (b + 1)):
        b += 1
        C = C - N * b
    print(C)
elif B < 0:
    b = 0
    C = B - N * b
    while abs(C) > abs(C - N * (b - 1)):
        b -= 1
        C = C - N * b
    print(C)

Submission status: Wrong Answer

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

a = [a[i]-i for i in range(n)]
b = int(sum(a)/n)
a = [i-b for i in a]
ans = 0
ans1 = 0
for i in a:
    ans += abs(i)
for i in a:
    ans1 += abs(i+1)
print(min(ans,ans1))

Submission status: Wrong Answer

Solution code:
# Your code here!

N = int(input())
A = list(map(int, input().split(" ")))
A_i = [0]*N
K = [0] * 101

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

b_0 = sum(A_i) / len(A_i)
b = [0] * 101
b[0] = int(b_0)
for i in range(1,101):
    b[i] = b[i] + int(i - 50.5)


for i in range(N):
    for j in range(11):
        K[j] = K[j] + abs(A_i[i] - b[j])

print(min(K))

Submission status: Wrong Answer

Solution code:
from math import floor

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

A = [A[i] - (i+1) for i in range(N)]
b = floor(sum(A)/N)
ans = sum([abs(a-b) for a in A])
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int,input().split())))
X = np.array(range(1,N+1))
M = np.array([X,np.ones(len(X))])
M = M.T
a,b = np.linalg.lstsq(M,A)[0]
m0 = np.sum(np.absolute(A-X-int(b)+2))
m1 = np.sum(np.absolute(A-X-int(b)+1))
m2 = np.sum(np.absolute(A-X-int(b)))
m3 = np.sum(np.absolute(A-X-int(b)-1))
m4 = np.sum(np.absolute(A-X-int(b)-2))
print(min([m0,m1,m2,m3,m4]))

Submission status: Wrong Answer

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

tmp = []

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

b = sum(tmp) // N

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

print(res)

Submission status: Wrong Answer

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


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(sum(a) / n)
lament = []
for b in [b_-1, b_, b_+1]:
    lament.append(calc_lament(a, b))

print(min(lament))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

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

ans = float("inf")
for b in range(100):

    tmp = 0
    for i in range(N):
        tmp += abs(AN[i] - (b + i + 1))

    ans = min(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
NP = 0
NM = 0
for i in range(N):
	NM = NM + A[i]
	NP = NP + i+1

b = round((NM-NP)/N) - 2
sad = 0
out = NM**2
for j in range(5):
	b = b + j
	sad = 0
	for i in range(N):
		sad = sad + abs(A[i]-b-i-1)
	if sad<out:
		out = sad
print(out)

Submission status: Wrong Answer

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
    
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: Wrong Answer

Solution code:
import math
n = int(input())
a = [int(x) for x in input().split()]
a2 = [0] * n

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

#print(a2)
res = [0] * n
res2 = [0] * n
mean =  sum(a2)//n
#print(mean)
for i in range(n):
    res[i] = abs(a2[i] - mean)
    res2[i] = abs(a2[i] + mean)

print(min(sum(res), sum(res2)))

Submission status: Wrong Answer

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

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

center = int(sum(A) / len(A))
ans = 100000000000000
for i in range(51):
    ans = min(ans, FindAns(A, center + i - 25))
print(ans)

Submission status: Wrong Answer

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

max = max(a)
ans = 10000000
prev = 10000000
# list = []
for b in range(max*-1,max):
  sum = 0
  for i, j in enumerate(a):
    i = i+1
    sum += abs(j - (b+i))
    # list.append(sum)
  if ans > sum:
    ans = sum
  if prev < sum:
    break
  prev = sum

# print(list)
print(ans)

Submission status: Wrong Answer

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

for i in range(n):
    c.append(a[i] - (i + 1))
ave = sum(c)//n

ans = 10**8*n

for b in range(ave, ave+2):
    t = 0
    for i in range(n):
        t += abs(c[i]-b)
    ans = min(ans, t)

print(ans)

Submission status: Wrong Answer

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

ans=9999999999
n=min(min(A),max(A)-N)
for b in range(n,max(A)):
    num=0
    for i,a in enumerate(A):
        num += abs(a-(b+i+1))
    if num<ans: 
        ans=num
        print(b)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
result = 2 ** 32
for b in range(2 * N):
    tmp = 0
    for i in range(N):
        tmp += abs(A[i] - (b + i))
        if(tmp > result):
            break
    if(result > tmp):
        result = tmp
print(result)

Submission status: Wrong Answer

Solution code:
b = int(input())  
s = list(map(int,input().split( )))
for i in range(0,b):
	s[i]+=200001-i

c=int(sum(s)/b)
d=0
f=0
for j in range(0,b):
    d+=abs(c-s[j])
    f+=abs(c+1-s[j])
print(min(f,d))

Submission status: Wrong Answer

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

Ai_i = []

for i in range(len(A)):
    Ai_i.append(A[i] - (i+1))

#max_abs = 0
#for i in range(len(Ai_i)):
#    if abs(Ai_i[i]) > max_abs:
#        max_abs = abs(Ai_i[i])

min_ = min(Ai_i)
max_ = max(Ai_i)

if min_ < 0 and max_ < 0:
    max_ = max([abs(min_), abs(max_)])

if min_ > 0 and max_ < 0:
    min_ = min([-abs(min_), -abs(max_)])    

min_abs = float("inf")
min_index = 0

for b in range(min_, max_+1):
    abss = []
    for i in range(len(Ai_i)):
        abss.append(abs(Ai_i[i] + b))
    abs_sum = sum(abss)
    if abs_sum < min_abs:
        min_abs = abs_sum

print(min_abs)

Submission status: Wrong Answer

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

for k in range(N):
    B.append(A[k]-(k+1))

ans1 = 0
a = sum(B)//N
for k in B:
    ans1 += abs(k-a)

ans2 = 0
b = 1+a
for k in B:
    ans2 += abs(k-b)

ans3 = 0
c = a-1
for k in B:
    ans3 += abs(k-c)

print(min(ans1,ans2,ans3))

Submission status: Wrong Answer

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

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

center = int(sum(A) / len(A))
ans = 100000000000000
for i in range(21):
    ans = min(ans, FindAns(A, center + i - 10))
print(ans)

Submission status: Wrong Answer

Solution code:
#!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3

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

ad = []
j = 1
for i in a:
    ad.append(i - j)
    j += 1

s = sum(ad)
avg = s / len(ad)
g1 = int(avg * -1)

if avg >= 0:
    j = 1
    d1 = 0
    for i in a:
        d1 += abs(i - j)
        j += 1
    print(d1)
    exit()
    

j = 0 - g1
j2 = 0 - g1 - 1
d1 = 0
d2 = 0
for i in a:
    d1 += abs(i - j)
    d2 += abs(i - j2)
    j += 1
    j2 += 1

print(min(d1, d2))

Submission status: Wrong Answer

Solution code:
N = int(input())
l = list(map(int, input().split()))
sum=0
absum=0
for i in range(N):
    sum+=l[i]-(i+1)
if abs(sum)<N/2:
    for j in range(N):
        absum+=abs(l[j]-(j+1))
    print(absum)
else:
    b=int(abs(sum)/N)+1
    if sum<0:
        for j in range(N):
            absum+=abs(l[j]-(j+1)+b)
    else:
        for j in range(N):
            absum+=abs(l[j]-(j+1)-b)
    print(absum)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
ck = 10000000000
for b in range(0,N):
    k = 0
    for i in range(0,N):
        k = k + abs(A[i] - (b+i+1))
    if k < ck:
        ck = k
    elif k >= ck:
        print(ck)
        break

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

N = int(input())
A = list(map(int, input().split(" ")))
A_i = [0]*N
K = [0] * 11

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

b_0 = sum(A_i) / len(A_i)
b = [0] * 11
b[0] = int(b_0)
b[1] = b[0] - 1
b[2] = b[0] + 1
b[3] = b[0] - 2
b[4] = b[0] + 2
b[5] = b[0] - 3
b[6] = b[0] + 3
b[7] = b[0] - 4
b[8] = b[0] + 4
b[9] = b[0] - 5
b[10] = b[0] + 5

for i in range(N):
    for j in range(11):
        K[j] = K[j] + abs(A_i[i] - b[j])

print(min(K))

Submission status: Wrong Answer

Solution code:
import math

N = int(input())
A = [int(i) for i in input().split()]

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

b1 = math.floor(su / N)
b2 = b1 + 1

result1 = 0
for i in range(len(A)):
    result1 += abs(A[i] - b1)
result2 = 0
for i in range(len(A)):
    result2 += abs(A[i] - b2)

print(min(result1, result2))

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*
N = int(input())
nums = list(map(int, input().split()))
ave = sum(nums)/len(nums)
if ave in nums:
    ave_i = nums.index(ave)
else:
    ave_i = 0
    diff = max(nums)
    for i in range(N):
        if abs(nums[i]-ave) <= diff:
            diff = abs(nums[i]-ave)
            ave_i = i

b = nums[ave_i] - (ave_i+1)
result = 0
for i in range(1,N+1):
    result += abs(nums[i-1]-(b+i))
print(result)

Submission status: Wrong Answer

Solution code:
N=int(input())
line=input()
line=line.rstrip().split(" ")
list1=[]
for i in range(N):
    list1.append(int(line[i]))
#print(list1)
list2=[]
list3=[]
for k in range(2):
    for j in range(N):
        list2.append(abs(list1[j]-j-1+k))
    list3.append(sum(list2))
#print(list3)
if list3[0]>=list3[1]:
    count=10000000000000
    
    for l in range(1000000):
        list4=[]
        for m in range(N):
            list4.append(abs(list1[m]-m-1-l))
        if sum(list4)<=count:
            count=sum(list4)
        else:
            print(count)
                
            break
            
        
else:
    count=10000000000000
    for l in range(-1,1000000):
        list4=[]
        for m in range(N):
            list4.append(abs(list1[m]-m-1+l))
            
        if sum(list4)<=count:
            count=sum(list4)
        else:
            print(count)
            #print(list4)
            break

Submission status: Wrong Answer

Solution code:
N = int(input())
AS = input().split()
for i in AS:
    i = int(i)
abs_list = []
for i in range (-N, N):
    counter = 0
    for j in range (1,N+1):
        counter += abs(int(AS[j-1]) - i - j)
    abs_list.append(counter)
print(min(abs_list))

Submission status: Wrong Answer

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

Ai_i = []

for i in range(len(A)):
    Ai_i.append(A[i] - (i+1))

#max_abs = 0
#for i in range(len(Ai_i)):
#    if abs(Ai_i[i]) > max_abs:
#        max_abs = abs(Ai_i[i])

min_ = min(Ai_i)
max_ = max(Ai_i)

if min_ < 0 and max_ < 0:
    max_ = max([abs(min_), abs(max_)])

min_abs = float("inf")
min_index = 0

for b in range(min_, max_+1):
    abss = []
    for i in range(len(Ai_i)):
        abss.append(abs(Ai_i[i] + b))
    abs_sum = sum(abss)
    if abs_sum < min_abs:
        min_abs = abs_sum

print(min_abs)

Submission status: Wrong Answer

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

b1 = int(sum(an)/len(an))
b2 = b1 - 2
b3 = b1 - 1
b4 = b1 + 1
b5 = b1 + 2
bs = [sum([abs(an[i] - b1) for i in range(N)]), sum([abs(an[i] - b2) for i in range(N)]), sum([abs(an[i] - b3) for i in range(N)]), sum([abs(an[i] - b4) for i in range(N)]), sum([abs(an[i] - b5) for i in range(N)])]
print(min(bs))

Submission status: Wrong Answer

Solution code:
#! /usr/bin/env python3

import math

n = int(input())
a = [int(i) for i in input().split()]
b = [0 for i in range(n)]
b_sum = 0

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

b_avg = math.floor(b_sum / n)

candidates = [b_avg - 3, b_avg - 2, b_avg - 1, b_avg, b_avg + 1, b_avg + 2, b_avg +3]

res = 10**16

for c in candidates:
    v = 0
    for i in range(n):
        v += abs(a[i] - c - (i + 1))
    if v < res:
        res = v

print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
if max(A) == N:
  b = 0
else :
  b = -max(A) + 1
result = 0
for i in range(N):
  result += abs(A[i]-(b+i+1))
print(result)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = input().split()
s1 = [int(s) for s in s]

s2 = [s1[i]-i-1 for i in range(N)]


ave = round(sum(s2) / N)
ave2 = ave -1
ave3 = ave +1


x = [abs(s1[j] - (ave + j+1)) for j in range(N)]
y = [abs(s1[j] - (ave2 + j+1)) for j in range(N)]
z = [abs(s1[j] - (ave3 + j+1)) for j in range(N)]

print(min(sum(x),sum(y),sum(z)))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

N = int(input())
A = list(map(int, input().split(" ")))
A_i = [0]*N
K = [0] * 5

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

b_0 = sum(A_i) / len(A_i)
b = [0] * 5
b[0] = int(b_0)
b[1] = b[0] - 1
b[2] = b[0] + 1
b[3] = b[0] - 2
b[4] = b[0] + 3

for i in range(N):
    for j in range(5):
        K[j] = K[j] + abs(A_i[i] - b[j])

print(min(K))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

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

_count = {}
for a in A:
    if a not in _count:
        _count[a] = 1
    else:
        _count[a] += 1
_count = sorted(_count.items(), key=lambda x: x[1], reverse=True)

if len(_count) == 1:
    ans = 0
    for a in A:
        ans += abs(a-_count[0][0])
else:
    if _count[0][1] > _count[1][1]:
        ans = 0
        for a in A:
            ans += abs(a-_count[0][0])
    else:
        new_count = []
        for a in _count:
            if a[1] == _count[0][1]:
                new_count.append(a[0])
        _min = min([abs(a) for a in new_count])
        ans = 0
        for a in A:
            ans += abs(a-_min)

print(ans)

Submission status: Wrong Answer

Solution code:
from statistics import median


def run(n, a):
    a = [a[i]-i-1 for i in range(len(a))]
    m = median(a)
    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: Wrong Answer

Solution code:
import math
n = int(input())
a = [int(x) for x in input().split()]
a2 = [0] * n

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

#print(a2)
res = [0] * n
res2 = [0] * n
mean =  math.floor(sum(a2)/n)
mean2 = math.ceil(sum(a2)/n)
#print(mean)
for i in range(n):
    res[i] = abs(a2[i] - mean)
    res2[i] = abs(a2[i] - mean2) 

print(min(sum(res), sum(res2)))

Submission status: Wrong Answer

Solution code:
N = int(input())

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

m = sum(A) // N
sum = 0
for i in range(N):
    sum += abs(A[i] - m)

print(sum)

Submission status: Wrong Answer

Solution code:
import math

n=int(input())
a=list(map(int,input().split()))
for i in range(n):
    a[i]-=(i+1)
mean=sum(a)/n
ans1=math.ceil(mean)
ans2=math.floor(mean)
sum1,sum2=0,0

for i in range(n):
    sum1+=abs(a[i]-ans1)
    sum2+=abs(a[i]-ans2)

if sum1<sum2:
    print(sum1)
else:
    print(sum2)

Submission status: Wrong Answer

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

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

b = su // N

result1 = 0
for i in range(len(A)):
    result1 += abs(A[i] - b)
result2 = 0
for i in range(len(A)):
    result2 += abs(A[i] - b - 1)

print(min(result1, result2))

Submission status: Wrong Answer

Solution code:
import numpy as np

n = int(input())
arr = np.array(list(map(int,input().split())))
per = np.array([i+1 for i in range(n)])

arr = arr-per


ave = np.sum(arr)//n
ave1 = ave+1
ave2 = ave-1

m = [np.sum(np.abs(arr-ave)),np.sum(np.abs(arr-ave1)),np.sum(np.abs(arr-ave2))]

print(int(min(m)))

Submission status: Wrong Answer

Solution code:
from scipy import optimize
N = int(input())
A = list(map(lambda x:int(x),input().split(" ")))
def f(x):
    tmp = 0
    for i in range(0,len(A)):
        tmp += abs(A[i]-(x+i+1))
    return tmp
print(int(round(optimize.brent(f,full_output=True)[1])))

Submission status: Wrong Answer

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

b = 0
min_ans = 100000000
while True:
    ans = 0
    for i in range(len(A)):
        #print(A[i], b, i, abs(A[i] - (b + i + 1)))
        ans += abs(A[i] - (b + i + 1))
    #print("ans", b, ans)
    if min_ans >= ans: min_ans = ans
    else: break
    b += 1

print(min_ans)

Submission status: Wrong Answer

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

min_sum = -1
i_atmin = -1

# for i in range(0, 2*(10**5) + 2) :
for i in range(0, 2*(10**5) + 2) :
    sum_over = 0
    sum_over_minus = 0
    
    for j in range(N) :
        sum_over += abs(a[j] - (i + j))
        
    for j in range(N) :
        sum_over_minus += abs(a[j] - (j - i))
        
    # if min_sum != -1 and min(sum_over, sum_over_minus) > min_sum :
    #     break
        
    if min_sum == -1 or min(sum_over, sum_over_minus) < min_sum :
        min_sum = min(sum_over, sum_over_minus)
        
print(min_sum)

Submission status: Wrong Answer

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))
num1=math.ceil(np.mean(b))
num2=math.floor(np.mean(b))
c1=[abs(i-(num+num1+1)) for num,i in enumerate(a)]
c2=[abs(i-(num+num2+1)) for num,i in enumerate(a)]
#print(c1,c2)
#print(np.sum(c1),np.sum(c2))
print(min(np.sum(c1),np.sum(c2)))

Submission status: Wrong Answer

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

ans=9999999999
n=max(0,min(min(A),max(A)-N))
for b in range(n,max(A)):
    num=0
    for i,a in enumerate(A):
        num += abs(a-(b+i+1))
    if num<ans: ans=num
print(ans)

Submission status: Wrong Answer

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

ans = 9999999
res = 0
for i in range(N):
    res += A[i] - (i+1)

b = int(res / N)
res = 0
for i in range(N):
    res += abs(A[i] - (b+(i+1)))
print(res)

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int,input().split())))
X = np.array(range(1,N+1))
M = np.array([X,np.ones(len(X))])
M = M.T
a,b = np.linalg.lstsq(M,A)[0]
m1 = np.sum(np.absolute(A-X-int(b)))
m2 = np.sum(np.absolute(A-X-int(b)-1))
print(min([m1,m2]))

Submission status: Wrong Answer

Solution code:
def main():
    """
    1 <=  N <= 2 * 10^5
    1 <= ai <= 10^9
    """
    N = int(input())
    *A, = map(int, input().split())

    ans = f(N, A)
    print(ans)


def f(N, A):
    """
    一番多い数を0に近づける
    """
    ans = 0
    C = [a - i for i, a in enumerate(A, 1)]

    from collections import Counter
    co = Counter(C)
    b = co.most_common()[0][0]

    ans = sum(abs(c - b) for c in C)
    return ans


def WA(N, A):
    """
    直感: Aの平均値 => はずれ
    A-iの平均値
    """
    C = [a - i for i, a in enumerate(A, 1)]
    b = sum(C) // N
    # print(b)
    # print(C)
    ans = sum(abs(c - b) for c in C)

    return ans


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
result = 2 ** 32
for b in range(2 * N):
    tmp = 0
    for i in range(N):
        tmp += abs(A[i] - (b + i))
    if(result > tmp):
        result = tmp
print(result)

Submission status: Wrong Answer

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

ans=10**15

ave=0
for i in range(N):
    ave+=S[i]
ave=ave//N

for i in range(ave-N//2-10,ave-N//2+10):
    goukei=0
    for j in range(N):
        goukei+=abs(S[j]-(i+j+1))
        
    if ans>goukei:
        ans=goukei
        
print(ans)

Submission status: Wrong Answer

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

for b in range(N + 1):
    for i in range(N):
        X += abs(A[i] - (b + i+1))
    ans.append(X)
    X = 0
    #
    for i in range(N):
        X += abs(A[i] - (i+1 -b))
    ans.append(X)
    X = 0

print(min(ans))

Submission status: Wrong Answer

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

for i in range(2):
    ans1 = 0
    a = [k+1 for k in a]
    for j in a:
        ans1 += abs(j)
    ans = min(ans,ans1)
print(ans)

Submission status: Wrong Answer

Solution code:
import collections

N = int(input())
A = [int(i) for i in input().split()]

sub = [A[i]-(i+1) for i in range(N)]
m = collections.Counter(sub).most_common()
res = 0

if m[0][1] == 1:
    for i in range(N):
        res += abs(A[i]-(i+1))
else:
    for i in range(N):
        res += abs(A[i]-(m[0][0]+i+1))
print(res)

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
a_tmp = input().split()
a = []
for i in range(n):
    a.append(int(a_tmp[i]))
a1 = []
for i in range(n):
    a1.append(a[i]-1-i)
b_max = max(a1)
b_min = min(a1)
sadness = []
for b in range(b_max+1):
    abs = [math.fabs(a1[i]-b) for i in range(n)]
    sadness.append(int(sum(abs)))
print(min(sadness))

Submission status: Wrong Answer

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

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

center = int(sum(A) / len(A))
print(min(FindAns(A, center - 2), FindAns(A, center - 1), FindAns(A, center), FindAns(A, center + 1), FindAns(A, center + 2)))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
b = A[N // 2] - (N // 2 + 1)
ans = 0

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

print(ans)

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
a = [int(x) for x in input().split()]
a2 = [0] * n

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

#print(a2)
res = [0] * n
mean =  math.floor(sum(a2)/n)
#print(mean)
for i in range(n):
    res[i] = abs(a2[i] - mean)

print(sum(res))

Submission status: Wrong Answer

Solution code:
import math

N = int(input())
A = [int(i) for i in input().split()]

A_ = [A[i] - i for i in range(N)]
s = sum(A_)/N
b1 = math.floor(s)
b2 = math.ceil(s)

A_b1 = sum([abs(a - b1) for a in A_])
A_b2 = sum([abs(a - b2) for a in A_])

print(min((A_b1, A_b2)))

Submission status: Wrong Answer

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

ans=10**12

ave=0
for i in range(N):
    ave+=S[i]
ave=ave//N

for i in range(ave-N//2-10,ave-N//2+10):
    goukei=0
    for j in range(N):
        goukei+=abs(S[j]-(i+j+1))
        
    if ans>goukei:
        ans=goukei
        
print(ans)