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:
N = int(input())
ary = list(map(int,input().split()))
plus = []
minus = []
zero = []
total = 0
for i in range(N):
    if ary[i] - (i+1) > 0:
        plus += [ary[i] - (i+1)]
    elif ary[i] -(i+1) == 0:
        zero += [ary[i] - (i+1)]
    else:
        minus += [ary[i] - (i+1)]
    total += abs(ary[i] - (i+1))

if len(plus) == len(minus):print(total);exit()
elif len(plus) > len(minus) + len(zero):
    a = abs(sorted(plus)[len(plus)-len(minus)-len(zero)-1])
    total = (len(zero) * a) + (abs(sum(minus)) + len(minus) * a)
    for i in range(len(plus)):
        total += abs(abs(plus[i]) - a)
elif len(minus) > len(plus) + len(zero):
    a = abs(sorted(minus,reverse=True)[len(minus)-len(plus)-len(zero)-1])
    total = (len(zero) * a) + (sum(plus) + len(plus) * a)
    for i in range(len(minus)):
        total += abs(abs(minus[i]) - a)
print(total)

Submission status: Wrong Answer

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



ans = []
for b in range(-1000, 1000):
    r = 0
    for i, ai in enumerate(A):
        r += abs(ai - (b + i + 1))
    ans.append(r)

print(min(ans))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
ans=0
for i in range(n):
    ans+=(a[i]-(i+1))
check=ans//n
ans=0
for i in range(n):
    ans+=abs(a[i]-(i+1+check))
print(ans)

Submission status: Wrong Answer

Solution code:
Data = []

def diff(b):
    sum1 = 0
    for d in Data:
        sum1 += abs(b-d)
    return sum1

N = int(input())
Data = list(map(int,input().split()))
for i in range(N):
    Data[i] -= (i+1) 
b = sum(Data)//N
ans = min(min(diff(b-1),diff(b)),diff(b+1))
print(ans)

Submission status: Wrong Answer

Solution code:
from decimal import Decimal, ROUND_HALF_UP
n=int(input())
a=list(map(int,input().split()))
sum_b=sum(a)-sum(list(range(n+1)))
b=Decimal(sum_b/n).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
ans=0
for i in range(n):
    ans+=abs(a[i] - (b+(i+1)))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from statistics import median
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)))
C = list(map(lambda x: abs(x-median(B)), B))
print(sum(C))

Submission status: Wrong Answer

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

Solution code:
import sys
from math import floor
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: Wrong Answer

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

if N % 2 != 0:
    b = A[N//2] - N//2
    print(sum([abs(A[i]-(b+i)) for i in range(N)]))

else:
    b = A[N//2] - N//2
    res1 = sum([abs(A[i]-(b+i)) for i in range(N)])

    b = A[N//2-1] - N//2-1
    res2 = sum([abs(A[i]-(b+i)) for i in range(N)])

    print(min(res1, res2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input().strip())
A = list(map(int, input().strip().split()))
bias = sorted([a-i for i, a in enumerate(A, 1)])
if N % 2 == 0:
    b = bias[N // 2]
else:
    b = bias[N // 2 - 1]
print(sum([abs(a - i - b) for i, a in enumerate(A, 1)]))

Submission status: Wrong Answer

Solution code:
import math
N = input()
A = list(map(int, input().strip().split()))
bias = [a-i for i, a in enumerate(A, 1)]
diff = min(bias) + max(bias)
if diff % 2 == 0:
    b = diff / 2
else:
    b = min(math.ceil(diff / 2), math.floor(diff / 2))
print(sum([abs(a - i - b) for i, a in enumerate(A, 1)]))

Submission status: Wrong Answer

Solution code:
#coding: utf-8
N = int(input())
A = input().split()

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

#print(A)

result = []

compari = []

for i in range(-max(A), max(A)+1):
    sum = 0
    for j in range(len(A)):
        sum += abs(A[j] - i)
    #print(sum)
    result.append(sum)

print(min(result))

Submission status: Wrong Answer

Solution code:
N = input()
A = list(map(int, input().strip().split()))
tmp = sum([abs(a - (-len(A) + i)) for i, a in enumerate(A, 1)])
for b in range(-len(A), len(A)):
    ans = sum([abs(a - (b + i)) for i, a in enumerate(A, 1)])
    if tmp < ans:
        print(tmp)
        break
    else:
        tmp = ans

Submission status: Wrong Answer

Solution code:
#coding: utf-8
N = int(input())
A = input().split()

for i in range(len(A)):
    A[i] = int(A[i])

result = []

for i in range(-max(A)-1, max(A)+1):
    sum = 0
    for j in range(len(A)):
        sum += abs(A[j] - (i+j+1))
    print(sum)
    result.append(sum)

print(min(result))

Submission status: Wrong Answer

Solution code:
from statistics import mean
import math

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

ans1 = 0
ans2 = 0
mean_A = mean(A)
mean_floor_A = math.floor(mean_A)
mean_ceil_A = math.ceil(mean_A)

for i in range(N):
    ans1 += abs(mean_floor_A - A[i])
    ans2 += abs(mean_ceil_A - A[i])
    
print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
for i in range(N) :
    A[i] -= i + 1
    
mid = sum(A) // N
x = sum([abs(A[i] - mid + 1) for i in range(N)])
y = sum([abs(A[i] - mid) for i in range(N)])
z = sum([abs(A[i] - mid - 1) for i in range(N)])

print(min(x, y, z))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
for i in range(N) :
    A[i] -= i + 1
    
mid = sum(A) // N
x = sum([abs(A[i] - mid) for i in range(N)])
y = sum([abs(A[i] - mid - 1) for i in range(N)])

print(min(x, y))

Submission status: Wrong Answer

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

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

print(sum(ans_list))

Submission status: Wrong Answer

Solution code:
import sys
import statistics

N = int(input())
AAA = list(map(int, sys.stdin.readline().split()))

AAA_minus_i = [A - (i+1) for i, A in enumerate(AAA)]
median_AAA_minus_i = statistics.median(AAA_minus_i)

ans = sum(list(map(lambda x: abs(x-median_AAA_minus_i), AAA_minus_i)))

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
A = [e-i-1 for i,e in enumerate(A)]
mean = sum(A)/len(A)
if mean-int(mean)>=0.5:
    b = int(mean)+1
else:
    b = int(mean)
bs = [b-1,b,b+1]
ans = 1000000000
for b in bs:
    value = sum([abs(e-b) for e in A])
    if value < ans:
        ans = value
print(ans)

Submission status: Wrong Answer

Solution code:
import statistics as st
import numpy as np

def test():
    ans = 0
    n = int(input())
    a = np.array(list(map(int,input().split())))
    i = np.array([i for i in range(1,n+1)])
    b = st.median(a-i)
    for k in range(n):
        ans += abs(a[k]-i[k]-b)
    print(ans)  


if __name__ == "__main__":
    test()

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [int(val) - (i + 1) for i, val in zip(range(n), input().split())]

b = int(round(sum(A) / len(A)))
sad = 0
Sad = []
for j in range(-1, 2):
  b_ = b + j
  for i in range(n):
    sad += abs(A[i] - b_)
  Sad.append(sad)
  sad = 0
print(min(Sad))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [int(val) - (i + 1) for i, val in zip(range(n), input().split())]

b = int(round(sum(A) / len(A)))
sad = 0
Sad = []
for j in range(min(A), max(A) + 1):
  b_ = b + j
  for i in range(n):
    sad += abs(A[i] - b_)
  Sad.append(sad)
print(min(Sad))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [int(val) - (i + 1) for i, val in zip(range(n), input().split())]

b = int(round(sum(A) / len(A)))
sad = 0
Sad = []
for j in range(-1, 2):
  b_ = b + j
  for i in range(n):
    sad += abs(A[i] - b_)
  Sad.append(sad)
print(min(Sad))

Submission status: Wrong Answer

Solution code:
import statistics

N = int(input())

A = list(map(int, input().split()))
c = 0

B = [A[i]-(i+1) for i in range(len(A))]
b = statistics.median(B)

for i, a in enumerate(A):
    c += abs(a - (b + i+1))

print(c)

Submission status: Wrong Answer

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

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

A.sort(reverse=True)

if A[N//2]>=0:print(sum([abs(x) for x in A]))
else:
  t=-A[N//2]
  for i in range(N):
    A[i]+=t
  print(sum([abs(x) for x in A]))

Submission status: Wrong Answer

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

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

if A[N//2]<0:
	t=-A[N//2]
	for i in range(N):
		A[i]+=t

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

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

xs = [x-i for i, x in enumerate(X, 1)]
b, _ = Counter(xs).most_common()[0]
print(sum(abs(x - (b + i)) for i, x in enumerate(X, 1)))

Submission status: Wrong Answer

Solution code:
import statistics
n=int(input())
A=list(map(int,input().split()))
B=sorted([j-i for i,j in enumerate(A)])
B_p=statistics.median(B)
count=0
for i,j in enumerate(A):
    count +=j-B_p-i

print(count)

Submission status: Wrong Answer

Solution code:
import statistics

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

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

b = statistics.median(B)

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

print(sadness)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
A = [A[i] - i - 1 for i in range(N)]
b1 = sum(A) // len(A)
b2 = -(-sum(A) // len(A))
a1 = sum([abs(A[i] - b1) for i in range(N)])
a2 = sum([abs(A[i] - b2) for i in range(N)])
print(min(a1, a2))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
A = [A[i] - i - 1 for i in range(N)]
b1 = sum(A) // len(A)
b2 = -(sum(A) // len(A))
a1 = sum([abs(A[i] - b1) for i in range(N)])
a2 = sum([abs(A[i] - b2) for i in range(N)])
print(min(a1, a2))

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

aAvg = math.floor(sum(a)/len(a))
aSum = 0
# print(sum(a)/len(a), aAvg, a)

for i in range(n):
    aSum += abs(a[i] - aAvg)
print(aSum)

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

aAvg = math.floor(sum(a)/len(a))
aSum = 0
print(sum(a)/len(a), aAvg, a)

for i in range(n):
    aSum += abs(a[i] - aAvg)
print(aSum)

Submission status: Wrong Answer

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

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

ans = 10**10

for i in range(-n-1, n+1):
    temp = kanasis(i)
    if temp < ans:
        ans = temp
        #print(i, kanasis(i))

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A_ = [a-i-1 for i,a in enumerate(A)]
print (A_)
A_.sort()
print (A_)
b = A_[N//2]
print (b)
print(sum([abs(ai-b) for ai in A_]))

Submission status: Wrong Answer

Solution code:
import math

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

blist = []
blist.append(ave // 1)
blist.append((ave + 1) // 1)

ans = 10**15
for b in blist:
    tmp = 0
    for c in C:
        tmp += abs(c - b)
    ans = min(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

blist = []
blist.append(math.floor(ave))
blist.append(math.ceil(ave))

ans = 10**15
for b in blist:
    tmp = 0
    for c in C:
        tmp += abs(c - b)
    ans = min(ans, tmp)

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

blist = []
blist.append(int(ave // 1))
blist.append(int((ave + 1) // 1))

ans = 10**9
for b in blist:
    tmp = 0
    for c in C:
        tmp += abs(c - b)
    ans = min(ans, tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
import statistics
N = int(input())
A = [int(a)-(i+1) for i, a in enumerate(input().split())]
b = statistics.median(A)
ans, prv_ans1, prv_ans2 = float('inf'), float('inf'), float('inf')
dec = True
while dec is True:
  tmp_ans1 = sum([abs(A[i]-b) for i in range(N)])
  tmp_ans2 = sum([abs(A[i]+b) for i in range(N)])
  ans = min([ans, tmp_ans1, tmp_ans2])
  b += 1
  if prv_ans1 < tmp_ans1 and prv_ans2 < tmp_ans2: dec = False
  prv_ans1, prv_ans2 = tmp_ans1, tmp_ans2
print(ans)

Submission status: Wrong Answer

Solution code:
import math
N = int(input())
A = [int(a)-(i+1) for i, a in enumerate(input().split())]
b = math.floor(sum(A)/N)
ans, prv_ans1, prv_ans2 = float('inf'), float('inf'), float('inf')
dec = True
while dec is True:
  tmp_ans1 = sum([abs(A[i]-b) for i in range(N)])
  tmp_ans2 = sum([abs(A[i]+b) for i in range(N)])
  ans = min([ans, tmp_ans1, tmp_ans2])
  b += 1
  if prv_ans1 < tmp_ans1 and prv_ans2 < tmp_ans2: dec = False
  prv_ans1, prv_ans2 = tmp_ans1, tmp_ans2
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
for i in range(N):
  A[i]-=i+1
if sum(A)>=0:
  a = (sum(A)+N-1)//N
  b = a-1
else:
  a=(sum(A)-N+1)//N
  b=a-1
ans1 = 0
ans2 = 0
for i in range(N):
  ans1 += abs(a-A[i])
  ans2 += abs(b-A[i])
ans = (min(ans1,ans2))
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
for i in range(N):
  A[i]-=i+1
if sum(A)>=0:
  a = (sum(A)+N-1)//N
  b = a-1
else:
  a=(sum(A)-N+1)//N
  b=a+1 
ans1 = 0
ans2 = 0
for i in range(N):
  ans1 += abs(a-A[i])
  ans2 += abs(b-A[i])
ans = (min(ans1,ans2))
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

idx=n//2
chuou=a[idx]

x=chuou-(idx+1)

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

Submission status: Wrong Answer

Solution code:
import math
n=int(input())
A=list(map(int,input().split()))
for i in range(n):
    A[i]=A[i]-i-1
b=sum(A)/n
f=math.floor(b)
c=math.ceil(b)
print(min(sum([abs(A[i]-f) for i in range(n)]),sum([abs(A[i]-c) for i in range(n)])))

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
b=round(sum(A)/n)
r=0
for i in range(n):
   r+=abs(A[i]-b)
print(r)

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
print(round(sum(A)/n))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for i in range(n):
  B.append(a[i] - (i+1))
  
B = [0]+sorted(B)
b = B[int(n/2)]

tmp = 0
for i in range(1,n+1):
  tmp += abs(B[i] - b)
  
print(tmp)

Submission status: Wrong Answer

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


def sad(A, b):
    return sum([abs(a - (b + i)) for a, i in zip(A, range(1, len(A) + 1))])


ave = [sum(A) // len(A)]
center= [(N + 1) // 2]

for i in range(1, 4):
    ave.append(ave[0] + i)
    ave.append(ave[0] - i)

    center.append(center[0] + i)
    center.append(center[0] - i)


b_list = [a - c for a in ave for c in center]

print(min([sad(A, b) for b in b_list]))

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)

a.sort()
med = a[N//2-1]
minv = 0
for i in range(N):
  minv += abs(a[i] - med)
    
print(minv)

Submission status: Wrong Answer

Solution code:
from statistics import median
N = int(input())
a = list(map(int,input().split()))

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

med = median(a)
minv = 0
for i in range(N):
  minv += abs(a[i] - med)
    
print(minv)

Submission status: Wrong Answer

Solution code:
import bisect
N=int(input())
a=list(map(int,input().split()))
tmp_a=sorted(a)
if N%2==1:
  ans=0
  med=tmp_a[N//2]
  b=med-(bisect.bisect_right(a,med))
  for i in range(N):
    ans+=abs(a[i]-(b+i+1))
  print(ans)
  print(bisect.bisect_right(a,med))
else:
  med1=tmp_a[N//2]
  med2=tmp_a[N//2+1]
  b1=med1-(bisect.bisect_right(a,med1))
  b2=med2-(bisect.bisect_right(a,med2))
  ans1=0
  ans2=0
  for i in range(N):
    ans1+=abs(a[i]-(b1+i+1))
    ans2+=abs(a[i]-(b2+i+1))
  ans=min(ans1,ans2)
  print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
N=int(input())
a=list(map(int,input().split()))
tmp_a=sorted(a)
if N%2==1:
  ans=0
  med=tmp_a[N//2]
  b=med-(bisect.bisect_right(a,med))
  for i in range(N):
    ans+=abs(a[i]-(b+i+1))
  print(ans)
  print(bisect.bisect_right(a,med))
else:
  med1=tmp_a[N//2]
  med2=tmp_a[N//2+1]
  b1=med1-(a.index(med1)+1)
  b2=med2-(a.index(med2)+1)
  ans1=0
  ans2=0
  for i in range(N):
    ans1+=abs(a[i]-(b1+i+1))
    ans2+=abs(a[i]-(b2+i+1))
  ans=min(ans1,ans2)
  print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
a=list(map(int,input().split()))
min_a=min(a)
max_a=max(a)
mean=(min_a+max_a)//2

tmp=10**19
l=[-1]
for i in range(N):
  num=abs(a[i]-mean)
  if tmp>num:
    tmp=num
    l.append(a[i])
    l.pop(0)
  else:
    pass
index=a.index(l[0])

b=a[index]-index-1
B=[]
for i in range(1,N+1):
  B.append(b+i)
  
ans=0
for i in range(N):
  ans+=abs(a[i]-B[i])
  
print(ans)

Submission status: Wrong Answer

Solution code:
#ABC102C
import numpy as np
n = int(input())
a = list(map(int,input().split()))
b = [0]*n
for i in range(n):
    b[i] = a[i]-(i+1)
ave = round(np.median(b))
ans = 0
for i in range(n):
    ans += abs(b[i]-ave)
print(ans)

Submission status: Wrong Answer

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


def sad(A, b):
    return sum([abs(a - (b + i)) for a, i in zip(A, range(1, len(A) + 1))])


ave = [sum(A) // len(A)]
ave.append(ave[0] + 1)
ave.append(ave[0] - 1)

center= [(N + 1) // 2]
center.append(center[0] + 1)
center.append(center[0] - 1)

b_list = [a - c for a in ave for c in center]

print(min([sad(A, b) for b in b_list]))

Submission status: Wrong Answer

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


def sad(A, b):
    return sum([abs(a - (b + i)) for a, i in zip(A, range(1, len(A) + 1))])


ave = [sum(A) // len(A)]
ave.append(ave[0] + 1)

center= [(N + 1) // 2]
center.append(center[0] + 1)

b_list = [a - c for a in ave for c in center]

print(min([sad(A, b) for b in b_list]))

Submission status: Wrong Answer

Solution code:
n = int(input())
ls = list(map(int, input().split()))
ls = [x - i - 1 for i, x in enumerate(ls)]
m0 = sum(ls) // n
m1 = m0 + 1
res0 = sum([abs(x - m0) for x in ls])
res1 = sum([abs(x - m1) for x in ls])
print(min(res0, res1))

Submission status: Wrong Answer

Solution code:
n = int(input())
ls = list(map(int, input().split()))
ls = [x - i - 1 for i, x in enumerate(ls)]
ls = [abs(x - sum(ls) // n) for x in ls]
print(sum(ls))

Submission status: Wrong Answer

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

Solution code:
import math

N = int(input().strip())
A = list(map(int, input().strip().split(' ')))
B = [a-(i+1) for i,a in enumerate(A)]
avg = sum(B)/len(B)
avg_ceil = math.ceil(avg)
avg_floor = math.floor(avg)
print(min(sum([abs(b-avg_ceil) for b in B]), sum([abs(b-avg_floor) for b in B])))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
sum_a=sum(a)
sum_n=n*(n+1)//2
sum_b=sum_a-sum_n
ans_b=round(sum_b/n)

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
sum_a=sum(a)
sum_n=n*(n+1)//2
sum_b=sum_a-sum_n
ans_b=sum_b//n

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
a_copy = a.copy()
a.sort()
if len(a)%2==1:
  m = (len(a)-1)//2
  mid1 = a[m]
  fir1 = mid1-m
  fin1 = mid1+m
  q = 0
  for num,i in zip(a_copy,range(fir1,fin1+1)):
    q += abs(num-i)
  print(q)
else:
  n = len(a)//2
  mid1 = (a[n-1]+a[n])//2
  fir1 = mid1-n+1
  fin1 = mid1+n
  q = 0
  for num,i in zip(a_copy,range(fir1,fin1+1)):
    q += abs(num-i)
  
  print(q)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
if len(a)%2==1:
  mid1,mid2 = sum(a)//n,(sum(a)+n-1)//n
  fir1,fir2 = mid1-(len(a)-1)//2,mid2-(len(a)-1)//2
  fin1,fin2 = mid1+(len(a)-1)//2,mid2+(len(a)-1)//2 
  q = 0
  for num,i in zip(a,range(fir1,fin1+1)):
    q += abs(num-i)
  p = 0
  for num,i in zip(a,range(fir2,fin2+1)):
    p += abs(num-i)
  print(min(p,q))
else:
  mid1,mid2 = int(sum(a)/n-0.5),int(sum(a)/n+0.5)
  fir1,fir2 = mid1-(len(a)-1)//2,mid2-(len(a)-1)//2
  fin1,fin2 = mid1+len(a)//2,mid2+len(a)//2 
  q = 0
  for num,i in zip(a,range(fir1,fin1+1)):
    q += abs(num-i)
  p = 0
  for num,i in zip(a,range(fir2,fin2+1)):
    p += abs(num-i)
  print(min(p,q))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

A = [A[i] - (i + 1) for i in range(N)]
b = round(sum(A) // N)
ans = 0

for i in A:
    ans += abs(i - b)

print(ans)

Submission status: Wrong Answer

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

A = [A[i] - (i + 1) for i in range(N)]
b = round(sum(A) // N)
print(A, b)
ans = 0

for i in A:
    ans += abs(i - b)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
min_total = 10**9

for b in range(0, max(A) // N + 1):
    total = 0
    for i in range(N):
        total += abs(A[i] - b - i)
    min_total = min(total, min_total)
print(min_total)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
As = list(map(int, input().split()))
Bs = []
for i in range(N):
  b = As[i]-i-1
  Bs.append(b)
#Bs.sort()
m0 = sum(Bs)//N
m1 = m0+1
r0 = sum(map(lambda B: abs(B-m0), Bs))
r1 = sum(map(lambda B: abs(B-m1), Bs))
r = min(r0, r1)
print(r)

Submission status: Wrong Answer

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

    for i in range(N):
        a[i] -= i+1
    s = sum(a)//N
    ans = 0
    for i in range(N):
        ans += abs(a[i] - s)
    print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

a_max = 0
for i in range(n):
  a_max = max(a_max,abs(a[i]-(i+1)))

ans = 0
for i in range(-a_max,a_max):
  sad = 0
  for j in range(n):
    sad += abs(a[j]-(i+j+1))
  if i == 0:
    ans = sad
  else:
    ans = min(ans,sad)
      
print(ans)

Submission status: Wrong Answer

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

a_max = 0
for i in range(n):
  a_max = max(a_max,abs(a[i]-(i+1)))

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

ans_neg = 0
# b = -a_max
for i in range(n):
  ans_neg = ans_neg + abs(a[i]-(-a_max+i+1))

ans_pog = 0
# b = a_max
for i in range(n):
  ans_pog = ans_pog + abs(a[i]-(a_max+i+1))

print(min(ans0,ans_neg,ans_pog))

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
from collections import  Counter
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)
c = Counter(a_li)
num = c.most_common()[0][0]
if sum(a_li) == 0:
    print(0)
else:
    ans = 0
    for i in range(n):
        ans += abs(a_li[i] - num)
    print(ans)

Submission status: Wrong Answer

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%2!=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: Wrong Answer

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

tmp=[As[i-1]-i for i in range(1,n+1)]
import statistics
b=statistics.median(tmp)
ans=[abs(As[i-1]-b-i) for i in range(1,n+1)]

print(sum(ans))

Submission status: Wrong Answer

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

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

# abs(A1-b) + ... + abs(AN-b)　が最小のときのbは
# (abs(A1-b))**2 + ... + (abs(AN-b))**2　が最小のときのbと等しい
# = (A1**2 + ... + AN**2) - 2b*(A1 + ... + AN) + (b**2)*N
# = T - 2bS + (b**2)N
# = N((b - (S/N))**2) + T - ((S**2)/N)
# よって、　b = S/N　のとき最小となる

S = sum(A)
b = S // N

ANS = []
for bb in [b-1, b, b+1]:
  ans = 0
  for n in range(N):
    ans += abs(A[n] - bb)
  ANS.append(ans)

print(min(ANS))

Submission status: Wrong Answer

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

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

# abs(A1-b) + ... + abs(AN-b)　が最小のときのbは
# (abs(A1-b))**2 + ... + (abs(AN-b))**2　が最小のときのbと等しい
# = (A1**2 + ... + AN**2) - 2b*(A1 + ... + AN) + (b**2)*N
# = T - 2bS + (b**2)N
# = N((b - (S/N))**2) + T - ((S**2)/N)
# よって、　b = S/N　のとき最小となる

S = sum(A)

b = S // N

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

print(min(ans1, ans2))

Submission status: Wrong Answer

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

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

S = sum(A)
b = round(S / N)

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

print(ans)

Submission status: Wrong Answer

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

s = 0
for i in range(n):
  a[i] -= i + 1
  s += a[i]
  
b0 = round(s / n)

answer = 10**30
for b in [b0 - 2, b0 - 1, b0, b0 + 1, b0 + 2]:
  ans = 0
  for i in range(n):
    ans += abs(a[i] - b)
  answer = min(ans, answer)
  
print(answer)

Submission status: Wrong Answer

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

s = 0
for i in range(n):
  a[i] -= i + 1
  s += a[i]
  
b = round(s / n)

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
mean = sum(a)//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 [-1, 0, 1]))

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)

#print(A)

b = sum(A) // len(A)

ans = 0
for i in A:
    ans += abs(i - b)

print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
from statistics import median, mean
N = int(input())
A = list(map(int, input().split()))
B = A[:]
for i in range(1, N + 1):
    B[i-1] = B[i-1] - i
b = mean(B)
ans = 0
for i in range(N):
    ans += abs(A[i] - b - i - 1)
print(int(ans))