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:
import numpy as np

N = int(input())
M = input().rstrip().split()

num=[]

for i in range(N):
  k = int(M[i])
  num.append(k-i-1)
    
num = np.array(num)

ave = int(np.average(num))
print(ave)

min = np.sum(np.absolute(num - ave))
min2 = np.sum(np.absolute(num - ave + 1))
min3 = np.sum(np.absolute(num - ave - 1))

if min > min2 and min3 > min2:
  print(min2)
elif min3 > min:
  print(min)
else:
  print(min3)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

s=10000000
b_list=range(-10000,10000)

for i in range(len(b_list)):
  s_koho=0
  for j in range(len(a_list)):
    s_koho += abs(a_list[j]-(b_list[i]+(j+1)))
  if s_koho<s:
    s=s_koho

print(s)

Submission status: Wrong Answer

Solution code:
#c

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

for i in range(1,n+1):
    a[i-1] -= i
num0 = 0
num1 = a[int(n/2)-1]
num2 = a[int(n/2)]
num3 = int((num1 + num2)/2)
num4 = num3 - 1
num5 = num3 + 1
num6 = num1 + 1
num7 = num1 - 1
num8 = num2 + 1
num9 = num2 - 1

ans0 = 0
for i in range(n):
    ans0 += abs(a[i]-num0)
ans1 = 0
for i in range(n):
    ans1 += abs(a[i]-num1)
ans2 = 0
for i in range(n):
    ans2 += abs(a[i]-num2)
ans3 = 0
for i in range(n):
    ans3 += abs(a[i]-num3)
ans4 = 0
for i in range(n):
    ans4 += abs(a[i]-num4)
ans5 = 0
for i in range(n):
    ans5 += abs(a[i]-num5)
ans6 = 0
for i in range(n):
    ans6 += abs(a[i]-num6)
ans7 = 0
for i in range(n):
    ans7 += abs(a[i]-num7)
ans8 = 0
for i in range(n):
    ans8 += abs(a[i]-num8)
ans9 = 0
for i in range(n):
    ans9 += abs(a[i]-num9)

ans = min(ans0,ans1)
ans = min(ans,ans2)
ans = min(ans,ans3)
ans = min(ans,ans4)
ans = min(ans,ans5)
ans = min(ans,ans6)
ans = min(ans,ans7)
ans = min(ans,ans8)
ans = min(ans,ans9)
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
N = int(input())
A = list(map(int, input().split()))
T = int((N*(N+1))/2)
#print(sum(A),T)
b=int(((sum(A)-T)//N))
#print(b)
Total=0
for i in range(N):
    Total = Total+abs(A[i]-(b+(i+1)))
print(int(Total))

Submission status: Wrong Answer

Solution code:
from collections import Counter

N = int(input())
input_list = []
input_counter = Counter()
ans1 = 0
ans2 = 0
index = 1
for i in input().split():
    item = int(i) - index
    input_list.append(item)
    input_counter[item] += 1
    index += 1
point = sum(input_list) // N
b = max(input_counter, key=input_counter.get)

for i in range(N):
    ans1 += abs(input_list[i] - b)
    ans2 += abs(input_list[i] - point)

print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
counter = 1
ans = 0
while n>=counter:
    ans += abs(a[counter-1]-(0+counter))
    counter+=1
print(ans)

Submission status: Wrong Answer

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

points = [(i+1, y) for i, y in enumerate(A)]

diff_list = [y - x for x, y in points]
diff_list.sort()

def eval_sadness(points, b):
    return sum([abs(y - (b + x)) for x, y in points])

max_b = diff_list[-1]
min_b = diff_list[0]
mid_b = (max_b + min_b)//2



def search(max_b, mid_b, min_b):
#    print("search", max_b, mid_b, min_b)
    
    eval_max = eval_sadness(points, max_b)
    eval_min = eval_sadness(points, min_b)
    eval_mid = eval_sadness(points, mid_b)

    minimum = min(eval_max, eval_mid, eval_min)
    #print(eval_mid)

    if minimum == eval_max:
        return eval_max
    elif minimum == eval_min:
        return eval_min
    elif eval_max == eval_min + 1:
        # no mid position
        return minimum
    else:
        return min(search(max_b, (max_b - mid_b)//2, mid_b), search(mid_b, (mid_b - min_b)//2, min_b))

if max_b == min_b:
    print(eval_sadness(points, min_b))
else:
    print(search(max_b, mid_b, min_b))

Submission status: Wrong Answer

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

p = a[0]
q = 0

now_len = 1
max_len = 1
max_p = p
max_q = 0
total = p

for i in range(n - 1):
    total += a[i + 1]
    t = a[i + 1] - a[i]
    if(t == 1):
        now_len += 1
    else:
        if(max_len < now_len):
            max_len = now_len
            max_p = p
            max_q = q
        now_len = 1
        p = a[i + 1]
        q = i + 1

if(max_len < now_len):
    max_len = now_len
    max_p = p
    max_q = q

ans = 0

b = max_p - max_q
if(max_p == a[0] and max_len == 1):
    b = int(total / n) - int(n / 2)

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

print(ans)

Submission status: Wrong Answer

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

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

subA = []
sub_dic = {}
for i in range(N):
    tmp = An[i]-(i+1)
    subA.append(tmp)
    if tmp in sub_dic:
        sub_dic[tmp] += 1
    else:
        sub_dic[tmp] = 1


# print(subA)
# print(sub_dic)
# # print(subB)


if max(sub_dic.values()) == 1:
    min_v = 0
    for v in subA:
        min_v += abs(v)
    print(min_v)

else:
    # max_sub_k = max([(v,k) for k,v in sub_dic.items()])[1]
    max_sub_k = 0
    max_sub_v = 0
    for k,v in sub_dic.items():
        if max_sub_v < v:
            max_sub_k = k
            max_sub_v = v
        elif max_sub_v == v:
            if max_sub_k < k:
                max_sub_k = k
                max_sub_v = v



    min_v = 0
    for k,v in sub_dic.items():
        min_v += abs((k - max_sub_k) * v)

    print(abs(min_v))

Submission status: Wrong Answer

Solution code:
import math
N = int(input().strip())
A = list(map(int,input().strip().split(' ')))
def kanashimi(b):
    
    out = 0
    for i in range(N):
        out = out + abs(A[i]-(b+(i+1)))
    return out 
ans = 10**10
for i in range(-100,100):
    ans = min(ans,kanashimi(i))
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
a = np.array([int(i) for i in input().split()]).astype('int64')

b = a - np.arange(1,N+1).astype('int64')
d = (b/N).astype('float128')
c = d.sum().astype('int64')
c1,c2 = np.floor(c).astype('int64'), np.ceil(c).astype('int64')
c3, c4, c5, c6 = c1 - 1, c2 + 1, c1 + 1, c2 - 1
min_1 = np.abs(b - c1).sum().astype('int64')
min_2 = np.abs(b - c2).sum().astype('int64')
min_3 = np.abs(b - c3).sum().astype('int64')
min_4 = np.abs(b - c4).sum().astype('int64')
min_5 = np.abs(b - c5).sum().astype('int64')
min_6 = np.abs(b - c6).sum().astype('int64')

print(min(min_1, min_2, min_3, min_4, min_5, min_6))

Submission status: Wrong Answer

Solution code:
from pprint import pprint

N = int(input())
As = list(map(int, input().split(" ")))

As = [A - i for i, A in enumerate(As)]

# pprint(As)

b = sum(As)//N

ans = 0

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

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
M = input().rstrip().split()

num=[]

for i in range(N):
  k = int(M[i])
  num.append(k-i-1)
    
num = np.array(num)

ave = int(np.average(num))
print(ave)

min = np.sum(np.absolute(num - ave))
min2 = np.sum(np.absolute(num - ave + 1))
min3 = np.sum(np.absolute(num - ave - 1))

if min >= min2 and min3 >= min2:
  print(min2)
elif min3 >= min:
  print(min)
else:
  print(min3)

Submission status: Wrong Answer

Solution code:
from collections import Counter

N = int(input())
A = input().split()
A = list(map(int,A))
sa = 0
sa_list = []

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

sa = Counter(sa_list)
sa.most_common()[0]

b = (sa.most_common()[0])[0]

sum_As = 0
for p in range(1,len(A)+1):
	sum_As += abs(A[p-1]-(b+p))
print(sum_As)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*
N = int(input())
nums = list(map(int, input().split()))
nums_s = sorted(nums)
if len(nums)%2 == 0:
    middle = int(len(nums_s)/2)
    middles = [middle-1, middle]
    middles = sorted(middles)
    res = []
    for mid in range(middles[0],middles[1]+1):
        b = nums_s[mid] - (nums.index(nums_s[mid])+1)
        result = 0
        for i in range(1,N+1):
            result += abs(nums[i-1]-(b+i))
        res.append(result)
    print(min(res))
else:
    middle = int(len(nums_s)/2)
    b = nums_s[middle] - (middle+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(raw_input())
a=[]
a=map(int,raw_input().split())
for i in xrange(n):
    a[i]-=i+1
high=max(a)
low=min(a)

ans=10**9

while(high>=low):
    mid=(high+low)/2
    s=0
    for i in xrange(n):
        s+=abs(a[i]-mid)
    if(ans>=s):
        ans=s
        if low<0:
            high=mid-1
        else: low=mid+1
    else:
        if high<0:
            low=mid+1
        else: high=mid-1
print ans

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
counter = 1
ans = 0
while n>counter:
    ans += abs(a[counter-1]-(0+counter))
    counter+=1
print(ans)

Submission status: Wrong Answer

Solution code:
import math
N = int(input().strip())
A = list(map(int,input().strip().split(' ')))
def kanashimi(b):
    
    out = 0
    for i in range(N):
        out = out + abs(A[i]-(b+(i+1)))
    return out 
tmp = sum(A)/N - (N+1)/2
if tmp > 0:
    b = math.floor(tmp)
else:
    b = math.ceil(tmp)

print(kanashimi(b))

Submission status: Wrong Answer

Solution code:
def m():
    n = int(input())
    a = list(map(int, input().split()))
    cnt = 0
    ans = 0
    for i in range(1, n+1):
        cnt += a[i-1]-i
    b = cnt // n
    for j in range(1, n+1):
        ans += abs(a[j-1]-(b+j))
    tmp = 0
    for j in range(1, n+1):
        tmp += abs(a[j-1]-(b+j+1))
    tmp2 = 0
    for j in range(1, n+1):
        tmp2 += abs(a[j-1]-(b+j-1))
    return min(ans,tmp,tmp2)

print(m())

Submission status: Wrong Answer

Solution code:
from math import ceil, floor 
N = int(input())
A = list(map(int, input().split()))

Ai_i = []

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

n = 10

b_ceil = ceil(sum(Ai_i) / len(Ai_i))

bs = list(range(b_ceil-n, b_ceil+n))

abss_ls = [ 0 for i in range(len(bs))]
for i in range(len(bs)):
    for j in range(len(Ai_i)):
        abss_ls[i] += abs(Ai_i[j] - bs[i])

min_abs = min(abss_ls)
print(min_abs)

Submission status: Wrong Answer

Solution code:
import numpy as np
import collections as col
import math
N = int(input())
A = list(map(int, input().split()))

npA = np.array(A)
npi = np.arange(1,N+1)
diff = npA - npi

ave = np.sum(diff) // (N+1)
diff2 = diff - ave
ans1 = np.sum(abs(diff2))

ans2 = 0
tmp = 0
tmp_befor = ans1
b_tmp = ave
while True:
    if b_tmp >= 0:
        b_tmp += 1
    else:
        b_tmp -= 1
    diff2 = 0
    diff2 = diff - b_tmp
    tmp = np.sum(abs(diff2))
    if tmp_befor < tmp:
        break
    tmp_befor = tmp
    ans2 = tmp

if ans1 > ans2:
    print(ans2)
else:
    print(ans1)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
import statistics
N = int(input())
A = list(map(int, input().split(' ')))

p = [v - (i + 1) for i, v in enumerate(A)]
s_p = [i for i, v in list(groupby(sorted(p)))]
cand = [min(s_p), max(s_p), int(statistics.mean(s_p)), int(statistics.median(s_p)), 0]
res = float('INF')
for k in cand:
    res = min(res, sum([abs(i - k) for i in p]))

print(res)

Submission status: Wrong Answer

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

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

ans = float('inf')
for j in range(-100, 100):
    v = 0
    for i in range(N):
        v += abs(A[i] - (b + j))
    ans = min(ans, v)
print(ans)

Submission status: Wrong Answer

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

def k(A, b):
    return sum([abs(a - (b+i+1)) for i,a in enumerate(A)])

s = 0
for i,a in enumerate(A):
    i+=1
    s += a - i

a = s//N

ans = float('inf')
for i in range(a-10,a+10):
    ans = min(ans, k(A,i))
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input().strip())
A=list(map(int, input().strip().split()))
A_posi=[]
A_nega=[]
for i in range(N):
    A[i]=A[i]-(i+1)
    if A[i]<0:
        A_nega.append(A[i]*(-1))
    elif A[i]>0:
        A_posi.append(A[i])
sm=sum(abs(i) for i in A)

A_tgt=A_nega if len(A_nega)>len(A_posi) else A_posi
A_tgt=sorted(A_tgt)

diff=0
old=0
mindiff=0
for i,a in enumerate(A_tgt):
    if old<a:
        diffdiff=-i+(len(A_tgt)-i)
    else:
        continue
#     if diffdiff<0:
#         break
    old=a
    diff = diff+diffdiff
    total_diff=-diff+a*(N-len(A_tgt))
    if total_diff<mindiff:
        mindiff=total_diff
print(sm+mindiff)

Submission status: Wrong Answer

Solution code:
import math

def ret(l, b):
  r = 0
  for i in range(len(l)):
    r += int(math.fabs(l[i] - b - i))
  return r

input()
l = list(map(int, input().split()))
a = (sum(l) - len(l)*(len(l)+1)//2) / len(l)
x = int(a) + 1
y = int(a)
z = int(a) - 1
print(min(ret(l, x), ret(l, y), ret(l,z)))

Submission status: Wrong Answer

Solution code:
N=int(input())

a2=list(map(int,input().split()))
a=[0]*N

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

b1=sum(a)//N

b = list(range(b1-17, b1+17))

sum_min = 1000000000000

for j in b:
    sum_a=0
    for i in range(N):
        sum_a+=abs(a[i]-j)
    if sum_a<sum_min:
        sum_min=sum_a

print(sum_min)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
a = np.array([int(i) for i in input().split()]).astype('int64')

b = a - np.arange(1,N+1).astype('int64')
d = (b/N).astype('float128')
c = d.sum().astype('int64')
c1,c2 = np.floor(c).astype('int64'), np.ceil(c).astype('int64')
c3, c4 = c1 - 1, c2 + 1
min_1 = np.abs(b - c1).sum().astype('int64')
min_2 = np.abs(b - c2).sum().astype('int64')
min_3 = np.abs(b - c3).sum().astype('int64')
min_4 = np.abs(b - c4).sum().astype('int64')

print(min(min_1, min_2, min_3, min_4))

Submission status: Wrong Answer

Solution code:
from collections import Counter
N=int(input())
A = list(map(int,input().split()))

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

ave1=sum(A)//len(A)
ave2=-(-sum(A)//len(A))
median=Counter(A).most_common(1)[0][0]

B=A[:]
C=A[:]
for i in range(0,N):
    A[i]-=ave1
    B[i]-=ave2
    C[i]-=median
    A[i]=abs(A[i])
    B[i]=abs(B[i])
    C[i]=abs(C[i])
print(min(sum(A),sum(B),sum(C)))

Submission status: Wrong Answer

Solution code:
N =int(input())
A= list(map(int,input().split( )))
B = []
b = 0
Ans =float("inf")
while True:
    for i in range(1,N+1):
        a = 0
        a = abs(A[i-1] - i - b)
        B.append(a)
    p = sum(B)
    if p >= Ans:
        break
    else:
        B = []
        Ans = p
        b = b - 1
        
    
print(Ans)

Submission status: Wrong Answer

Solution code:
from pprint import pprint

N = int(input())
As = list(map(int, input().split(" ")))

# As = [A - i for i, A in enumerate(As)]

print(As)

# b = sum([abs(A) for A in As])//N
b = sum(As)//N

print(b)

ans = 0

print([abs(a-b) for a in As])

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

print(ans)

Submission status: Wrong Answer

Solution code:
from math import ceil, floor 
N = int(input())
A = list(map(int, input().split()))

Ai_i = []

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

n = 100

b_ceil = ceil(sum(Ai_i) / len(Ai_i))

bs = list(range(b_ceil-n, b_ceil+n))

abss_ls = [ 0 for i in range(len(bs))]
for i in range(len(bs)):
    for j in range(len(Ai_i)):
        abss_ls[i] += abs(Ai_i[j] - bs[i])

min_abs = min(abss_ls)
print(min_abs)

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_max = max(A_)
A_min = min(A_)

A_b = [sum([abs(a - i) for a in A_]) for i in (b1, b2, A_max, A_min)]

print(min(A_b))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
ck = 10000000000
for b in range(-N,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(b)
        print(ck)
        break

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# AtCoder Beginner Contest


if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    a_total = sum(a)
    n_total = (n * (n + 1)) // 2
    b = (a_total - n_total) // n
    bs = [0, b - 1, b, b + 1, -b, -b + 1, -b - 1]
    ans = float('inf')

    for bi in bs:
        i = 1
        total = 0

        for ai in a:
            total += abs(ai - (bi + i))
            i += 1

        ans = min(ans, total)

    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N=int(input())
A = list(map(int,input().split()))

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

ave1=sum(A)//len(A)
ave2=-(-sum(A)//len(A))
median=Counter(A).most_common(1)[0][0]
print(median)
B=A[:]
C=A[:]
for i in range(0,N):
    A[i]-=ave1
    B[i]-=ave2
    C[i]-=median
    A[i]=abs(A[i])
    B[i]=abs(B[i])
    C[i]=abs(C[i])
print(min(sum(A),sum(B),sum(C)))

Submission status: Wrong Answer

Solution code:
N = int(input())
l = list(map(int, input().split()))
sum=0
for i in range(N):
    sum+=l[i]-(i+1)
k = max(l)

b=int(abs(sum)/N)
if sum<0:
    absum=[abs(l[j]-(j+1)+b) for j in range(N) for b in range(-k,k)]
else:
    absum=[abs(l[j]-(j+1)-b) for j in range(N) for b in range(-k,k)]

print(min(absum))

Submission status: Wrong Answer

Solution code:
n=int(input())
num_str=input().split()
num=list(map(int, num_str))
num=[num[i]-i-1 for i in range(n)]
num_copy=num
ans = 99999999999999
max_abs=max(max(num),abs(min(num)))
for b in range(-max_abs,max_abs):
    ans_element=list(map(lambda x:abs(x-b), num))
    now_ans=sum(ans_element)
    ans=min(ans,now_ans)
    num=num_copy
print(ans)

Submission status: Wrong Answer

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

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

subA = []
sub_dic = {}
for i in range(N):
    tmp = An[i]-(i+1)
    subA.append(tmp)
    if tmp in sub_dic:
        sub_dic[tmp] += 1
    else:
        sub_dic[tmp] = 1


# print(subA)
# print(sub_dic)
# # print(subB)


if max(sub_dic.values()) == 1:
    min_v = 0
    for v in subA:
        min_v += abs(v)
    print(min_v)

else:
    # max_sub_k = max([(v,k) for k,v in sub_dic.items()])[1]
    max_sub_k = 0
    max_sub_v = 0
    for k,v in sub_dic.items():
        if max_sub_v > v:
            max_sub_k = k
            max_sub_v = v
        elif max_sub_v == v:
            if max_sub_k > k:
                max_sub_k = k
                max_sub_v = v



    min_v = 0
    for k,v in sub_dic.items():
        min_v += (k - max_sub_k) * v

    print(abs(min_v))

Submission status: Wrong Answer

Solution code:
import math

N = int(input())
A = [int(a) for a in input().split()]
A = [A[n]-(n+1) for n in range(N)]
b = int(math.floor(sum(A)/N))
ans = 0

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

print(abs(ans))

Submission status: Wrong Answer

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

ami = [ x - i - 1 for i, x in enumerate(aa)]

def sadness(al, b):
    sadl = [abs(x - b) for x in al]
    return sum(sadl)

avami = sum(ami) // n
sadnl = [sadness(ami, i) for i in range(avami-50, avami+50)]
print(min(sadnl))

Submission status: Wrong Answer

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

S1=[]
for i in range(N):
    S1.append(S[i])
S1.sort()
chuouchi=S1[len(S1)//2]
chuouchi2=S1[len(S1)//2-1]
ans=10**18
ansj=0
for i in range(chuouchi-N//2-5,chuouchi-N//2+5):
    goukei=0
    for j in range(N):
        goukei+=abs(S[j]-(i+j+1))
        ansj=j
    if ans>goukei:
        ans=goukei
        
for i in range(chuouchi2-N//2-5,chuouchi2-N//2+5):
    goukei=0
    for j in range(N):
        goukei+=abs(S[j]-(i+j+1))
        ansj=j
    if ans>goukei:
        ans=goukei
        
        
print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

for i in range(1, len(ns)+1):
    ns[i-1] -= i

av = sum(ns)/len(ns)

l = math.floor(av)
l1 = l-1
h = math.ceil(av)
h1 = h+1

ls = 0
hs = 0
l1s = 0
h1s = 0

for na in ns:
    ls += abs(na-l)
    hs += abs(na-h)
    l1s += abs(na-l1)
    h1s += abs(na-h1)


print(min(ls, hs, l1s, h1s))

Submission status: Wrong Answer

Solution code:
#c

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

for i in range(1,n+1):
    a[i-1] -= i
num1 = a[int(n/2)-1]
num2 = a[int(n/2)]
num3 = int((num1 + num2)/2)
num4 = num3 - 1
num5 = num3 + 1
num6 = num1 + 1
num7 = num1 - 1
num8 = num2 + 1
num9 = num2 - 1

ans1 = 0
for i in range(n):
    ans1 += abs(a[i]-num1)
ans2 = 0
for i in range(n):
    ans2 += abs(a[i]-num2)
ans3 = 0
for i in range(n):
    ans3 += abs(a[i]-num3)
ans4 = 0
for i in range(n):
    ans4 += abs(a[i]-num4)
ans5 = 0
for i in range(n):
    ans5 += abs(a[i]-num5)
ans6 = 0
for i in range(n):
    ans6 += abs(a[i]-num6)
ans7 = 0
for i in range(n):
    ans7 += abs(a[i]-num7)
ans8 = 0
for i in range(n):
    ans8 += abs(a[i]-num8)
ans9 = 0
for i in range(n):
    ans9 += abs(a[i]-num9)

ans = min(ans1,ans2)
ans = min(ans,ans3)
ans = min(ans,ans4)
ans = min(ans,ans5)
ans = min(ans,ans6)
ans = min(ans,ans7)
ans = min(ans,ans8)
ans = min(ans,ans9)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
B = [A[i]-i-1 for i in range(N)]
b = int(sum(B)/N + 0.5)
C = [abs(B[i]-b) for i in range(N)]
print(sum(C))

Submission status: Wrong Answer

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

sum_dif_A = 0
for i in range(N):
    sum_dif_A += A[i] - (i+1)
if sum_dif_A >= 0:
    sad_value = sum_dif_A // N
else:
    sad_value = (-sum_dif_A // N)*(-1)
answer = 0
for i in range(N):
    answer += abs(A[i] - (sad_value+i+1))
print(str(answer))

Submission status: Wrong Answer

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

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

ans = float('inf')
for j in range(-10, 11):
    v = 0
    for i in range(N):
        v += abs(A[i] - (b + j))
    ans = min(ans, v)
print(ans)

Submission status: Wrong Answer

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

sum = 0
for i in range(N):
    arr[i] = arr[i] - i - 1
    sum += arr[i]

average = round(sum / N)

res =0
res1 = 0
res2 = 0
for i in range(len(arr)):
    res += abs(average - arr[i])
    res1 += abs(average + 1 - arr[i])
    res2 += abs(average - 1 - arr[i])
print(min(res, res1, res2))

Submission status: Wrong Answer

Solution code:
from collections import Counter

n = int(input())
a = [int(i) for i in input().split()]   
a = [a[i] - (i+1) for i in range(len(a))]

count_num, count = Counter(a).most_common()[0]
ans = sum([abs(i-count_num) for i in a])
print(ans)

Submission status: Wrong Answer

Solution code:


from functools import reduce
b = int(input())
x = list(map(int, input().split()))

absadd = lambda a_i, i, B: abs((a_i-i) -B)

def mk_candi(B):
    ret = 0
    xx = [absadd(ele, idx+1, B) for idx, ele in enumerate(x)]
    xx = reduce(lambda x_0,x_1 : x_0+x_1, xx)
    return xx

candi = [mk_candi(B) for B in range(-b, b)]

print(min(candi))

Submission status: Wrong Answer

Solution code:
import statistics
N = int(input())
A = list(map(int, input().split()))
A_med = []
for i in range(N):
    a = A[i] - (i +1)
    A_med.append(a)
b = statistics.median(A_med)
count = 0
for i in range(N):
    count += abs(A[i]-(b+i+1))
print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

p = a[0]
q = 0

now_len = 1
max_len = 1
max_p = p
max_q = 0

for i in range(n - 1):
    t = a[i + 1] - a[i]
    if(t == 1):
        now_len += 1
    else:
        if(max_len < now_len):
            max_len = now_len
            max_p = p
            max_q = q
        now_len = 1
        p = a[i + 1]
        q = i + 1

if(max_len < now_len):
    max_p = p
    max_q = q

total = 0

b = max_p - max_q

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

print(total)

Submission status: Wrong Answer

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

if n==1:
    print(0)
    sys.exit()

def abs(a):
    if a<0:
        return -a
    else:
        return a

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

ans=10**12
a.sort()
if n<20:
    for b in a:
        S=0
        for j in range(n):
            S+=abs(a[j]-b)
        ans=min(ans,S)
    if ans==10**12:
        ans=0
    print(ans)
    sys.exit()

mid=n//2
b_choice=[a[mid-1],(a[mid-1]+a[mid])//2,a[mid],(a[mid]+a[mid+1])//2,a[mid+1]]
for b in b_choice:
    S=0
    for j in range(n):
        S+=abs(a[j]-b)
    ans=min(ans,S)
if ans==10**12:
    ans=0
print(ans)

Submission status: Wrong Answer

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

print(m())

Submission status: Wrong Answer

Solution code:
N=input()
A=map(int, raw_input().split())
B=range(1,N+1)

C=[]
for x,y in zip(A,B):
	C.append(x-y)

K=set(C)
L=list(K)
L.append(0)
L.sort()

h=len(L)
l=0


ans=float("inf")

for j in range(200):
	m=(h+l)/2
	x=L[m]
	t=0
	for y in C:
		t+=abs(-1*x+y)

	if t<ans:
		ans=t
		h=m
	else:
		l=m
	ans=min(ans,t)

print ans

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
a = np.array([int(i) for i in input().split()])

b = a - np.arange(1,N+1)
c = (b/N).sum()
c1,c2 = np.floor(c).astype('int64'), np.ceil(c).astype('int64')
c3, c4 = c1 - 1, c2 + 1
min_1 = np.abs(b - c1).sum()
min_2 = np.abs(b - c2).sum()
min_3 = np.abs(b - c3).sum()
min_4 = np.abs(b - c4).sum()

print(min(min_1, min_2, min_3, min_4))

Submission status: Wrong Answer

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

B = 0
for i in range(N):
    value = A[i] - (i+1)
    B += value
sigma = B

B = round(B / N)

ans1 = 0
anss = []

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

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

B -= 2
ans3 = 0
for i in range(N):
    ans3 += abs(A[i]-B-i-1)
anss.append(ans3)

print(min(anss))

Submission status: Wrong Answer

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

ami = [ x - i - 1 for i, x in enumerate(aa)]

def sadness(al, b):
    sadl = [abs(x - b) for x in al]
    return sum(sadl)

avami = sum(ami) // n
sadnl = [sadness(ami, i) for i in range(avami-5, avami+5)]
print(min(sadnl))

Submission status: Wrong Answer

Solution code:



def sumarray(n, c, arr):
	sum_ = 0
	for i in range(0,n):
		sum_ += abs(arr[i] - c)
	return sum_
		
def finder(max_, min_, arr):
	max_sum, min_sum = sumarray(n,max_,arr), sumarray(n,min_,arr)

	if max_ == min_ :
		return max_sum
	elif max_sum <= min_sum:
		if (max_ + min_) % 2 != 0:
			ave = int((max_ + min_+ 1)/2)
		else:
			ave = int((max_ + min_)/2)
			
		
		
		return finder(max_, ave, arr)
	elif max_sum > min_sum:
		if (max_ + min_) % 2 != 0:
			ave = int((max_ + min_- 1)/2)
		else:
			ave = int((max_ + min_)/2)
			
		return finder(ave, min_, arr)
	

n = int(input())
arr = input().split(" ")
for i in range(0,n):
	arr[i] = int(arr[i]) - (i+1)


max_, min_ = max(arr), min(arr)

print(finder(max_, min_, arr))

Submission status: Wrong Answer

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


def main():
    N = int(input().strip())
    A = [int(x) for x in input().strip().split()]
    b = 0
    diff = 0
    tmp = calc(b, A)
    while(diff <= 0):
        result = calc(b, A)
        diff = result - tmp
        if diff > 0:
            result = tmp
            break
        tmp = result
        b -= 1
    m = result
    b = 0
    diff = 0
    tmp = calc(b, A)
    while(diff <= 0):
        result = calc(b, A)
        diff = result - tmp
        if diff > 0:
            result = tmp
            break
        tmp = result
        b += 1
    p = result
    print(max([p, m]))


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()))
max_A = 0
min_total = float("inf")

for i, v in enumerate(A):
    if v > max_A:
        max_A = v
        max_i = i+1

if max_A >= max_i:
    diff = max_A - max_i
    for b in range(0, diff):
        total = 0
        for i in range(N):
            total += abs(A[i]-(b+(i+1)))
        if min_total > total:
            min_total = total
else:
    diff = max_i - max_A
    for b in range(-diff, 1):
        total = 0
        for i in range(N):
            total += abs(A[i]-(b+(i+1)))
        if min_total > total:
            min_total = total

print(min_total)

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-1000, ave+1000):
    t = 0
    for i in range(n):
        t += abs(c[i]-b)
    ans = min(ans, t)

print(ans)

Submission status: Wrong Answer

Solution code:
import collections

N = int(input())
A = list(map(int, input().split()))
b = 0
li = []
for i in range(N):
  li.append(abs(A[i]-(b+i+1)))
c = collections.Counter(li)
b = -c.most_common()[0][0]
li.clear()
for i in range(N):
  li.append(abs(A[i]-(b+i+1)))
print(sum(li))

Submission status: Wrong Answer

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

print(sum([abs(x - y) for (x, y) in zip(a, range(1, n+1))]))