Problem:


Score : 300 points

Problem Statement

Snuke has an integer sequence A of length N.

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

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

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

Find the minimum possible sadness of Snuke.

Constraints

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

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible sadness of Snuke.

Sample Input 1

5
2 2 3 5 5

Sample Output 1

2

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

Sample Input 2

9
1 2 3 4 5 6 7 8 9

Sample Output 2

0

Sample Input 3

6
6 5 4 3 2 1

Sample Output 3

18

Sample Input 4

7
1 1 1 1 2 3 4

Sample Output 4

6



Sample input:
5
2 2 3 5 5


Sample output:
2

Submission status: Runtime Error

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

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

An.sort()

m = N-1
if (m % 2 == 0):
    cent = (An[m/2] + An[m/2 + 1]) // 2
else:
    cent = An[(m+1)/2]

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

print(ans)

Submission status: Runtime Error

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

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

def SAD(C,b):
	S = 0
	for i in range(N):
		S += abs(C[i] - b)
	return S

MAX, MIN = max(C), min(C)

while MIN != MAX:
	d = (MAX-MIN)//3
	x1 = MIN + d
	x2 = MIN + 2*d
	s1 = SAD(C,x1)
	s2 = SAD(C,x2)
#	print((x1,s1,"vs",x2,s2))
	if s1 > s2:
		MIN = x1
	else:
		MAX = x2
print(s1)

Submission status: Runtime Error

Solution code:
import math
n = int(input())
a = list(map(int,input().split()))
l = len(a)
b = [a[i]-(i+1) for i in range(n)]
b = sorted(b)
if l%2==1:
    mid = b[l//2+1]
    sum = 0
    for i in range(n):
        sum += abs(a[i] - mid - (i+1))
else:
    mid = (b[l//2] + b[l//2+1]) / 2
    sum1 = 0
    sum2 = 0
    for i in range(n):
        sum1 += abs(a[i] - math.floor(mid) - (i+1))
        sum2 += abs(a[i] - math.ceil(mid) - (i+1))
    sum = min(sum1, sum2)
print(sum)

Submission status: Runtime Error

Solution code:
import numpy as np
N=int(input())
A=np.array(list(map(int,input().split(' '))))
A=A-np.arange(N)
Amed=np.int(A.median())
err=[]
for b in (Amed,Amed+1):
    err.append(np.sum(np.abs(A-b)))
print(min(err))

Submission status: Runtime Error

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

An.sort()

m = N-1
if (m % 2 == 0):
    cent = (An[m//2] + An[m//2 + 1]) // 2
else:
    cent = An[(m+1)//2]

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

print(ans)

Submission status: Runtime Error

Solution code:
# C - Linear Approximation
# https://beta.atcoder.jp/contests/abc102/tasks/arc100_a

# C_Linear_Approximation.py

# 7
# 1 1 1 1 2 3 4
#import sys

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

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

a.sort()
if N % 2 != 0:
    mid = (N-1)/2 +1
else:
    mid = N / 2
b = a[mid]

sadness_left = b * (mid-1) - sum(a[:mid])
sadness = sum(a) - b * N + sadness_left * 2

print(sadness)

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
A = list(map(lambda x:int(x),input().split(" ")))
npA = np.array(A)
B = npA-(1+np.arange(len(npA)))
opt_b = statistics.median(B)
print(int(np.sum(np.abs(B-opt_b))))

Submission status: Runtime Error

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

if N % 2 != 0:
    b = sub[int(N-1/2)+1]
    res = 0
    for i in range(N):
        res += abs(A[i]-(b+i+1))
else:
    b1 = sub[int(N-1/2)]
    b2 = sub[int(N-1/2)+1]
    res1 = 0
    for i in range(N):
        res1 += abs(A[i]-(b1+i+1))
    res2 = 0
    for i in range(N):
        res2 += abs(A[i]-(b2+i+1))
    res = min(res1,res2)

print(res)

Submission status: Runtime Error

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

An.sort()

m = N-1
if (m % 2 == 0):
    cent = (An[m//2] + An[m//2 + 1]) // 2
else:
    cent = An[(m+1)//2]

ans = sum(An) - N * cent

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

S1=[]
for i in range(N):
    S1.append(S[i])
S1.sort()
chuouchi=statistics.median_high(S)
chuouchi2=statistics.median_low(S)
chuouchi3=statistics.median(S)
ans=10**20
ansj=0
A=chuouchi-N//2-4
B=chuouchi-N//2+4
C=chuouchi2-N//2-4
D=chuouchi2-N//2+4
E=chuouchi3-N//2-4
F=chuouchi3-N//2+4
for i in range(A,B):
    goukei=0
    for j in range(N):
        goukei+=abs(S[j]-(i+j+1))
        
    if ans>goukei:
        ans=goukei
        
for i in range(C,D):
    goukei=0
    for j in range(N):
        goukei+=abs(S[j]-(i+j+1))
        
    if ans>goukei:
        ans=goukei

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

Submission status: Runtime Error

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

if N % 2 != 0:
    b = sub[int(N/2)+1]
    res = 0
    for i in range(N):
        res += abs(A[i]-(b+i+1))
else:
    b1 = sub[int(N/2)]
    b2 = sub[int(N/2)+1]
    res1 = 0
    for i in range(N):
        res1 += abs(A[i]-(b1+i+1))
    res2 = 0
    for i in range(N):
        res2 += abs(A[i]-(b2+i+1))
    res = min(res1,res2)

print(res)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = []
 
for k in range(N):
    B.append(A[k]-(k+1))

C = sorted(B)

if N%2==0:
    b = C[N//2]
else:
    b = (C[N//2]+C[1+N//2])//2

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

print(ans)

Submission status: Runtime Error

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)]
res = 0

if N % 2 != 0:
    b = sub[int(N/2)+1]
    res = 0
    for i in range(N):
        res += abs(A[i]-(b+i+1))
else:
    b1 = sub[int(N/2)]
    b2 = sub[int(N/2)+1]
    res1 = 0
    for i in range(N):
        res1 += abs(A[i]-(b1+i+1))
    res2 = 0
    for i in range(N):
        res2 += abs(A[i]-(b2+i+1))
    res = min(res1,res2)

print(res)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int, input().rstrip().split()))
B=list(A[i]-i for i in range(N))
C=B.sort()
work=C[N//2]
testA=sum(abs(B[i]-work) for i in range(N))
testB=sum(abs(B[i]-work-1) for i in range(N))
testC=sum(abs(B[i]-work+1) for i in range(N))
output=min(testA,testB,testC)
print(output)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
Ad = [A[i]-(i+1) for i in range(N)]
max_Ad = max(abs(Ad))
min_total = float("inf")

for b in range(0, max_Ad+1):
    map(lambda x: x-b, Ad)
    total = sum(abs(Ad))
    if min_total > total:
        min_total = total
print(min_total)

Submission status: Runtime Error

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=[]
C=[]
for k in range(min(A),max(A)+1):
    B.append(A[:])

    for i in range(0,N):
        B[k][i]-=k

        B[k][i]=abs(B[k][i])
    C.append(sum(B[k]))
print(min(C))

Submission status: Runtime Error

Solution code:
こんなん解けへんやん普通

Submission status: Runtime Error

Solution code:
from collections import Counter

def mapdict(n, dictionary):
    return dict(map(lambda t:(t[0]-n,t[1]), dictionary.items()))

def count(dic):
    count = 0
    for k,v in dic.items():
        count += abs(k*v)
    return count

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

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


Adic = Counter(A2)
Ax  = A2[N//2]
Adic = mapdict(Ax, Adic)

res = count(Adic)
pl = count(mapdict(A2[N//2 +1],Adic))
mn = count(mapdict(A2[N//2 -1],Adic))


if pl<=res:
    tmp = pl
    tmpc = 1
    while tmp <= res:
        tmpc += 1
        if N//2 + tmpc > N-1:
            break
        res = tmp
        Adic = mapdict(A2[N//2 +tmpc], Adic)
        tmp = count(Adic)
        

elif mn<=res:
    tmp = mn
    tmpc = 1
    while tmp <= res:
        tmpc += 1
        if N//2 - tmpc < 0:
            break
        res = tmp
        Adic = mapdict(A2[N//2 -tmpc], Adic)
        tmp = count(Adic)

print(res)

Submission status: Runtime Error

Solution code:
line=input()
line=line.rstrip().split(" ")
list1=[]
for i in range(N):
    list1.append(int(line[i])-i-1)
#print(list1)

list2=[]
list3=[]
for j in range(2):
    for k in range(N):
        list2.append(abs(list1[k]-1-j))
        
    list3.append(sum(list2))
    list2=[]
#print(list3)

if list3[0]==list3[1]:
    print(list3[0])
elif list3[0]<list3[1]:
    list2=[]
    list3=[]
    count=100000000000000000
    for l in range(1000000000):
        for m in range(N):
            list2.append(abs(list1[m]-1+l))
        if count>=sum(list2):
            count=sum(list2)
            #print(list2)
            list2=[]
            l=l+10
        else:
            count=100000000000000000
            for n in range(l,l+11):
                for o in range(N):
                    list2.append(abs(list1[o]-1+(n-10)))
                if count>=sum(list2):
                    count=sum(list2)
                    list2=[]
                else:
                    print(count)
                    break
                    
            
        
else: 
    list2=[]
    list3=[]
    count=100000000000000000
    for l in range(1000000000):
        for m in range(N):
            list2.append(abs(list1[m]-1-l))
        if count>=sum(list2):
            count=sum(list2)
            list2=[]
            l=l+10
        else:
            count=100000000000000000
            for n in range(l,l+11):
                for o in range(N):
                    list2.append(abs(list1[o]-1+(n-10)))
                if count>=sum(list2):
                    count=sum(list2)
                    list2=[]
                else:
                    print(count)
                    break

Submission status: Runtime Error

Solution code:
import collections

N = int(input())
A = list(map(int, input().split()))
li = []
b = 0
if max(A) > (sum(A) - max(A):
  b = -max(A)
  for i in range(N):
    li.append(abs(A[i]-(b+i+1)))
  print(sum(li))
else :
  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: Runtime Error

Solution code:
N = input()
aryA = input().split(" ")

N = int(N)
aryA = [int(s) for s in aryA]
n = len(aryA)

tot = []
totlist = []
b = N // 2

for m in range(b):
    for x in range(n):
        tot.append(abs(aryA[x] - (m + x + 1)))
    totlist.append(sum(tot))

print(min(totlist))

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
import copy

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [int(input_raw[0])]
    input_raw = read_func().strip().split()
    A_i = []
    for i in range(N):
        A_i.append(long(input_raw[i]) - i)


    ave_A = (sum(A_i)/N)
    A_i.sort()
    
    b1 = (ave_A)
    b2 = ave_A + 1
    b3 = A_i[N/2]
    b4 = A_i[N/2 + 1]
    b5 = -b1
    b6 = -b2
    score1 = 0L
    score2 = 0L
    score3 = 0L
    score4 = 0L
    score5 = 0L
    score6 = 0L
    for i in range(N):
        score1 += abs(b1 - A_i[i])
        score2 += abs(b2 - A_i[i])
        score3 += abs(b3 - A_i[i])
        score4 += abs(b4 - A_i[i])
        score5 += abs(b5 - A_i[i])
        score6 += abs(b6 - A_i[i])

    print min(score1, score2, score3, score4,score5,score6)




if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import numpy as np
N=int(input())
A=np.array(list(map(int,input().split(' '))))
A=A-np.arange(N)
err=[]
for b in range(A.min(),A.max()):
    err.append(np.sum(np.abs(A-b)))
print(min(err))

Submission status: Runtime Error

Solution code:
N=int(input())
a=list(map(int,input().split()))
b=[]
for i in range(N):
    c=b.append(abs(a[i])-(i+1))
d=min(abs(c))
e=[]
for i in range(N):
    f=e.append(abs(c[i]-d))
    
for i in range(N):
    sum+=f[i]
print(sum)

Submission status: Runtime Error

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


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

min = A[int(N/2)]
max = A[int(N/2)+1]+1
sad = 2e15
for b in range(min, max):
    s = 0
    for i in range(N):
        s += abs(A[i] - b)

    if s < sad:
        sad = s

print("{}".format(sad))

Submission status: Runtime Error

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

for b in range(-N/2,max(A)):
    sad = 0
    for i in range(N):
        sad += abs(A[i] - (b + i + 1))
    ans.append(sad)

print(min(ans))

Submission status: Runtime Error

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:
    fans=10000000000
    for j in range(100):
        for i in range(n):
            ans1+=abs(arr[i]-l-j)
            ans2+=abs(arr[i]-l+j)
        fans=min(fans,ans1,ans2)
    print(fans)

Submission status: Runtime Error

Solution code:
import math
n=int(input())
num_str=input().split()
num=list(map(int, num_str))
num=[num[i]-i-1 for i in range(n)]
ans = math.inf
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)
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

def calc(A, B):
    return sum([abs(A[i]-B[i]) for i in range(len(A))])
def n(A, B):
    flag = True
    prev = calc(A, B)
    while flag:
        B -= 1
        c = calc(A, B)
        if c < prev:
            prev=c
        else:
            flag=False
    return prev
def p(A, B):
    flag = True
    prev = calc(A, B)
    while flag:
        B += 1
        c = calc(A, B)
        if c < prev:
            prev=c
        else:
            flag=False
    return prev
  
N = int(input())
A = np.array(list(map(int, input().split())))
B = np.array(list(range(A.mean()-(N//2), A.mean()-(N//2)+N)))

m = calc(A, B)
_n = calc(A, B-1)
_p = calc(A, B+1)
if _n < m:
    print(n(A, B-1))
elif _p < m:
    print(p(A, B-1))
else:
    print(m)

Submission status: Runtime Error

Solution code:
import sympy as sym

if __name__ == '__main__':

    x = sym.symbols('x', real=True)

    length = int(input())
    array = list(map(int, input().split()))
    formula = 0
    abs_max = 1000000000

    for num, value in enumerate(array):
        num += 1
        formula += sym.Abs(value - (x + num))
        if abs_max > value - num:
            abs_max = abs(value - num)

    result = 10000000000

    for num in range(0, abs_max + 1):
        _result = sym.expand(formula).subs([(x, -num)])
        if result > _result:
            result = _result

    print(result)

Submission status: Runtime Error

Solution code:
import math
n=int(input())
num_str=input().split()
num=list(map(int, num_str))
num=[num[i]-i-1 for i in range(n)]
ans = math.inf
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)
print(int(ans))

Submission status: Runtime Error

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)
     
    c = 10
     
    x = min([sum([abs(s1[j] - (ave + j-i)) for j in range(N)]) for i in range(c)])
    y = min([sum([abs(s1[j] - (ave + j+i)) for j in range(N)]) for i in range(c)])
     
     
     
    print(min(x,y))

Submission status: Runtime Error

Solution code:
import sympy as sym

if __name__ == '__main__':

    x = sym.symbols('x', real=True)

    length = int(input())
    array = list(map(int, input().split()))
    formula = 0
    abs_max = 1000000000

    for num, value in enumerate(array):
        num += 1
        formula += sym.Abs(value - (x + num))
        if abs_max > value - num:
            abs_max = abs(value - num)

    b_list = sym.solve(formula, x)
    result = 10000000000

    for num in reversed(range(0, abs_max + 1)):
        _result = sym.expand(formula).subs([(x, -num)])
        if result > _result:
            result = _result

    print(result)

Submission status: Runtime Error

Solution code:
import math

def kanashimi(b):
    
    out = 0
    for i in range(N):
        out = out + abs(A[i]-(b+(i+1)))
    return out 
ans = 10**20
for i in range(-100,100):
    ans = min(ans,kanashimi(i))
print(ans)

Submission status: Runtime Error

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

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

print(min(ans))

Submission status: Runtime Error

Solution code:
import math
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 = math.inf
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: Runtime Error

Solution code:
import sympy as sym

if __name__ == '__main__':

    x = sym.symbols('x', real=True)

    length = int(input())
    array = list(map(int, input().split()))
    formula = 0
    abs_max = 1000000000

    for num, value in enumerate(array):
        num += 1
        formula += sym.Abs(value - (x + num))
        if abs_max > value - num:
            abs_max = abs(value - num)

    b_list = sym.solve(formula, x)
    result = 10000000000

    for num in range(0, abs_max + 1):
        _result = sym.expand(formula).subs([(x, -num)])
        if result > _result:
            result = _result

    print(result)

Submission status: Runtime Error

Solution code:
def BinSearch(A, hi, lo):
    b = (hi + lo) // 2
    tmp0 = FindAns(A, b - 1)
    tmp1 = FindAns(A, b)
    if hi == lo:
        tmp2 = FindAns(A, b + 1)
        return min(tmp0, tmp1, tmp2)
    if tmp0 < tmp1:
        return BinSearch(A, b - 1, lo)
    else:
        return BinSearch(A, hi, b + 1)
    
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
A = sorted(A)
hi = A[int(len(A) / 2 + 1) if int(len(A) / 2 + 1) <  len(A)  else int(len(A) / 2)]
lo = A[int(len(A) / 2 - 1) if int(len(A) / 2 - 1) >= 0 else int(len(A) / 2)]

print(BinSearch(A, hi, lo))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

sum = 0
ans = []

for b in range(-int(N / 2), int(N / 2)):
    for i in range(N):
        sum += abs(A[i] - (b + i))
    else:
        ans.append(sum)
        sum = 0

print(min(ans))

Submission status: Runtime Error

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<50:
    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
if a[mid]<0:
    b_choice=a[mid-20:mid]
else:
    b_choice=a[mid:mid+20]
for b in range(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: Runtime Error

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**15
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: Runtime Error

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<40:
    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
if a[mid]<0:
    b_choice=a[mid-20:mid]
else:
    b_choice=a[mid:mid+20]
for b in range(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: Runtime Error

Solution code:
import math
n=int(input())
num_str=input().split()
num=list(map(int, num_str))
num=[num[i]-i-1 for i in range(n)]
ans = math.inf
max_abs=max(max(num),abs(min(num)))

for b in range(max_abs*2):
    ans_element=list(map(lambda x:abs(x-b), num))
    now_ans=sum(ans_element)
    ans=min(ans,now_ans)
print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int, input().split()))
import numpy as np
diff=np.array(A)-np.arange(N)
diff=diff.tolist()
c = collections.Counter(diff)
c.most_common()
diff=abs(np.array(diff)-np.full(N,c.most_common()[0][0]))
print(sum(diff.tolist()))

Submission status: Runtime Error

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**15
ansj=0
for i in range(chuouchi-N//2-10,chuouchi-N//2+10):
    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-10,chuouchi2-N//2+10):
    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: Runtime Error

Solution code:
import statistics
import math

N = int(input())
A = list(map(int, input().split(" ")))
A_i = [0]*N
K_1 = 0
K_2 = 0
K_3 = 0

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

b_0 = statistics.mode(A_i)

b_1 = int(b_0)
b_2 = b_1 - 1
b_3 = b_1 + 1

for i in range(N):
    K_1 = K_1 + abs(A_i[i] - b_1)
    K_2 = K_2 + abs(A_i[i] - b_2)
    K_3 = K_3 + abs(A_i[i] - b_3)

print(min(K_1, K_2, K_3))

Submission status: Runtime Error

Solution code:
import collections
N=int(input())
l=list(map(lambda x:int(x),input().split(' ')))
l=[l[i]-(i+1) for i in range(N)]
dl=collections.Counter(l)
ans=[0]*(len(dl)//2)
for i in range(len(dl)//2):
    k=dl.most_common()[i][0] 
    k*=-1
    ans[i]=sum([abs(l[j]+k) for j in range(N)])
print(min(ans))

Submission status: Runtime Error

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

sum = 0
for n in range(N):
    sum += An[n] - (n+1)
    
a, b, c = sum//N, sum//N + 1

ans = [0]*3
for n in range(N):
    ans[0] += abs(An[n] - (a+n+1))
    ans[0] += abs(An[n] - (b+n+1))
    ans[1] += abs(An[n] - (c+n+1))

print(min(ans))

Submission status: Runtime Error

Solution code:
def BinSearch(A, hi, lo):
    b = (hi + lo) // 2
    tmp0 = FindAns(A, b - 1)
    tmp1 = FindAns(A, b)
    if hi == lo:
        tmp2 = FindAns(A, b + 1)
        return min(tmp0, tmp1, tmp2)
    if tmp0 < tmp1:
        return BinSearch(A, b - 1, lo)
    else:
        return BinSearch(A, hi, lo + 1)
    
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
A = sorted(A)
hi = A[int(len(A) / 2 + 1)]
lo = A[int(len(A) / 2 - 1)]

print(BinSearch(A, hi, lo))

Submission status: Runtime Error

Solution code:
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
    for idx, ele in enumerate(x):
        ret += absadd(ele, idx+1, B)

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

print(min(candi))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
lst = [int(x) for x in input().split()]
a = 0
for i in range(n):
    lst[i] = lst[i] - (i + 1)
lst.sort()
def sum(k):
    a = 0
    for i in range(n):
        a += abs(lst[i]-k)
    return a
ans = 0
if ans % 2 == 0:
    ans = min([sum(lst[int(n/2)]),sum(lst[int(n/2)+1])])
else:
    ans = sum(lst[int(n/2)+1])
print(str(ans))

Submission status: Runtime Error

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

b0, b1, b2, b3 = -int(1e9), 0, 0, int(1e9)
iter = int(1e2)


for _ in range(iter):
    b1 = (2*b0 + b3) /3
    b2 = (b0 + 2*b3) /3
    
    f1, f2 = 0, 0
    for i in range(n):
        f1 += abs(a[i] - b1 - i)
        f2 += abs(a[i] - b2 - i)
    
    if f1 > f2: b0 = b1
    else:       b3 = b2
    
    if abs(b2 - b1) < 0.01: break
                            
print(round(f(round(b1))))

Submission status: Runtime Error

Solution code:
import math

n=int(input())
a=list(map(int,input().split()))
b=[a[i]-(i+1) for i in range(n)]
sumb2=0
sum2=0
for bb in b:
    sumb2 += (bb**2)

for bb in b:
    sum2+=bb*2
bb=[math.ceil(sum2/(2*n)), math.floor(sum2/(2*n))]
ans=math.inf

for bbb in bb:
    sumans=0
    for aa in b:
        sumans+=(math.fabs(aa-bbb))
    ans=min(ans,sumans)

print(int(ans))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = []
cnt = 1
for num in a:
    num -= cnt
    b.append(num)
    cnt += 1
test = [int(mean(b))-1,int(mean(b)),int(mean(b))+1]
ans = []
s = 9999999999999
for i in test:
    cnt = 1
    c = []
    for num in a:
        st = 0
        st = abs(num - (i+cnt))
        c.append(st)
        cnt += 1
    ans.append(sum(c))
print(min(ans))

Submission status: Runtime Error

Solution code:
from collections import counter
n = int(input())
a = [int(i) for i in input().split()]
output = 0
b = [abs(elem - i) for i, elem in enumerate(a, 1)]
c = counter(b)
d = c.most_common()

surume = 0
if len(d) == 1:
    surume = d[0][0]
else:
    if d[0][1] == d[1][1]:
        surume = 0
    else:
        surume = d[0][0]
e = [abs(abs(elem - i) - abs(surume)) for i, elem in enumerate(a, 1)]
print(sum(e))

Submission status: Runtime Error

Solution code:
n = int(input())
lst = [int(x) for x in input().split()]
a = 0
for i in range(n):
    lst[i] = lst[i] - (i + 1)
def sum(k):
    a = 0
    for i in range(n):
        a += abs(lst[i]-k)
    return a
ans = min([sum(lst[int(n/2)]),sum(lst[int(n/2)+1])])
print(str(ans))

Submission status: Runtime Error

Solution code:
import numpy as np

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

num=[]
min = 10e9
max = 0

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

ave = int(np.average(num))

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

if min > min2:
  print(min2)
else:

Submission status: Runtime Error

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
bb = 0

bs = list(range(min_, max_+1))
l = len(bs)

if sum(Ai_i) < 0:
    min_ = 0
elif sum(Ai_i) > 0:
    max_ = 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: Runtime Error

Solution code:
n = int(input())
list = [int(i) for i in input().split()]
 
n_max = max(list)
n_min = min(list)
 
list2 = [(list[i]-(i+1)) for i in range(n)]
 
lowlim = min(list2)
hilim  = max(list2)
 
sorrow = []
for b in range(lowlim, hilim):
    tmp = int(0)
    for j in range(n):
        tmp += abs(list2[j]-b)
    sorrow.append(tmp)
 
print(min(sorrow))

Submission status: Runtime Error

Solution code:
from collections import Counter
n  = int(input())
arr = list(map(int,input().split()))
dif = []
for i,j in enumerate(arr):
    dif.append(j - (i + 1))
count = Counter(dif)
b = count.most_common()[0][0] if sum(dif) != 0 else  0
print(sum(list(map(lambda x:abs(x - b) , dif))))
--- abc/102 ‹master* ?› »

Submission status: Runtime Error

Solution code:
import statistics
import math

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)))
if max(A) == N:  
  print(sum(li))
else :
  mode = max(li)
  mode = statistics.mode(li)
  b = -mode
  li.clear()
  for i in range(N):
    li.append(abs(A[i]-(b+i+1)))
  print(sum(li))

Submission status: Runtime Error

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

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

print(ans)

Submission status: Runtime Error

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)

sadnl = [sadness(ami, i) for i in range(min(ami),max(ami))]
print(min(sadnl))