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: Time Limit Exceeded

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

ans = sum(A)

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

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

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

print(calcAns(imin))

Submission status: Time Limit Exceeded

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

sa = A - np.array(range(1,N+1))
print(min([abs(sa - i).sum() for i in range(sa.min(), sa.max()+1)]))

Submission status: Time Limit Exceeded

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

def abs(x):
    if x>=0:
        return x
    else:
        return x*(-1)

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

k=[]
m=[abs(i) for i in a]
for b in range((-1)*max(m),max(m)+1):
    k.append(0)
    x=[j-b for j in a]
    y=[abs(i) for i in x]
    k[b+max(m)]=sum(y)
    
print(min(k))

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
N = input()
AList = list(map(int, input().split()))
listnum = 1000000000

low = -1000000000
high = 1000000000

mode = 1
p = 0
a = 0
while(True):
    L = []
    center = math.floor((low + high)/2)
    for i in range(-1+center, 2+center):
        for j in range(1,int(N)+1):
            p += abs(AList[j-1] -(i + j))
        L.append(p)        
        p = 0

    if L[1] == min(L):
        print(L[1])
        sys.exit()
    elif L[0] == min(L):
        high = center - 1
    else:
        low = center + 1

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

def mapdict(itemfunc, dictionary):
    return dict(map(itemfunc, dictionary.items())) 

def minus(tup):
    return (tup[0]-1,tup[1]) 
def plus(tup):
    return (tup[0]+1,tup[1]) 

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 = [A[i-1]-i for i in range(1,N+1)]
Adic = Counter(A2)

res = count(Adic)
pl = count(mapdict(plus,Adic))
mn = count(mapdict(minus,Adic))

if pl<res:
    tmp = float("inf")
    while res <= tmp:
        tmp = res
        Adic = mapdict(plus, Adic)
        res = count(Adic)
    res = tmp

elif mn<res:
    tmp = float("inf")
    while res <= tmp:
        tmp = res
        Adic = mapdict(minus, Adic)
        res = count(Adic)
    res = tmp

print(res)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
list1=[int(i) for i in input().split()]
list2=[]
for i in range(N):
    kana=0
    for j in range(N):
        kana+=abs(list1[j]-(min(list1)-N+i+j+1))
    list2.append(kana)
print(min(list2))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
c = []
b = 0
sum = 0
plus = 0
zero = 0
minus = 0
q = False
for index,i in enumerate(a):
    tmp = i - (b+(index+1))
    c.append(tmp)
    if tmp > 0:
        sum += tmp
        plus += 1
    elif tmp == 0:
        zero += 1
    else:
        sum += -1 * tmp
        minus += 1
if plus > minus:
    if plus <= (zero + minus):
        q = True
    b += 1
    flag = True

elif plus == minus:
    q = True
else:
    if minus <= (zero + plus):
        q = True
    b -= 1
    flag = False
if q == False:
    while flag == True:#プラスしていく場合
        tmp_list = []
        sum = sum - plus + zero + minus
        plus = 0
        zero = 0
        minus = 0
        for i in c:
            tmp = i - 1
            tmp_list.append(tmp)
            if tmp > 0:
                plus += 1
            elif tmp == 0:
                zero += 1
            else:
                minus += 1
        c = tmp_list
        if plus <= minus:
            break
    while flag == False:
        tmp_list = []
        sum = sum - minus + zero + plus
        plus = 0
        zero = 0
        minus = 0
        for i in c:
            tmp = i + 1
            tmp_list.append(tmp)
            if tmp > 0:
                plus += 1
            elif tmp == 0:
                zero += 1
            else:
                minus += 1
        c = tmp_list
        if minus <= plus:
            break

print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
l_abs = []
max_v = 0
for i, a_i in enumerate(a):
    if(max_v < a_i):
        max_v = a_i
    l_abs.append(abs((i+1)-a_i))

min = 10 ** 16
if sum(l_abs) == 0:
    min = 0
else:
    for b in range(-(max_v+1), max_v+1):
        sum = 0
        for j, a_j in enumerate(a):
            sum += abs(a_j-(b+j+1))
        if(min > sum):
            min = sum
print(min)

Submission status: Time Limit Exceeded

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

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

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


if min_ans_b < min_ans:
    min_ans = min_ans_b
"""
print(min_ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split()
A = list(map(int,A))
sa = 0
useds = []
min_value = 10000000000000000000

for j in range(1,len(A)+1):
	sa = A[j-1]-j
	if((sa not in useds)==True):
		useds.append(sa)
		b = int(sa)
		sum_As = 0
		for p in range(1,len(A)+1):
			sum_As += abs(A[p-1]-(b+p))

		if(sum_As<min_value):
			min_value = int(sum_As)

print(min_value)

Submission status: Time Limit Exceeded

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)
  if min > k-i-1:
    min = k-i-1
  if max < k-i-1:
    max = k-i-1
    
num = np.array(num)

min2 = 10e9
  
for i in range(min,max+1):
  kk = num - i
  x = np.sum(np.absolute(kk))
  if min2 > x:
    min2 = x

print(min2)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

ave = sum(A)//N
ans = 1145141919810
for i in range(N):
	tmp = 0
	for j in range(N):
		tmp += abs(A[j]-(ave+1-N+i+j))
	ans = min(ans,tmp)
	
print(ans)

Submission status: Time Limit Exceeded

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)
b=0
max_diff=0
if len(A_tgt):
    while b<A_tgt[-1]:
        b+=1
        diff=0
        for i,a in enumerate(A_tgt):
            if a>=b:
                diff+=(len(A_tgt)-i)*b
                break
            else:
                diff+=2*a-b
        diff-=(N-len(A_tgt))*b
        if diff >max_diff:
            max_diff=diff
        else:
            break
print(sm-max_diff)

Submission status: Time Limit Exceeded

Solution code:
# Q3
import numpy as np

def kanashii(a,b):
    kanashisa = 0
    for i in range(len(a)):
        kanashisa+=abs(a[i] - b)
#     print('kanashisa:{}'.format(kanashisa))
    return kanashisa

n = int(input())
a = np.array(list(map(int, input().split())))
arr_i = np.array(list(i+1 for i in range(len(a))))
a2 = a-arr_i
# print(a2)

k=temp =0
for i in range(np.min(a2),np.max(a2)):
    temp = kanashii(a2,i)
#     print("b{} k:{}".format(i, temp))
    if(temp<k or k==0):
        k=temp
print(k)

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
b = [int(i) for i in input().split()]
n = float("inf")
for i in range(a):
    b[i] -= i
c = sum(b)//a
for i in range(c-abs(c)-1,c+abs(c)+1):
    k = 0
    for j in range(a):
        k += abs(b[j] - i)
    if k < n:
        n = k
print(n)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
num=50
count=46463435
ma=max(l)
m=ma//2
mi=min(l)
k=m
b=0
while(num>=0):
    sum=0
    sum1=0
    sum2=0
    sum4=0
    sum3=0
    for i in range(len(l)):
        sum+=abs(l[i]-(b+i+1))
        sum1+=abs(l[i]-(ma+i+1))
        sum3+=abs(l[i]-(m+i+1))
        sum2+=abs(l[i]-(k+i+1))
        sum4+=abs(l[i]-(mi+i+1))
    if(sum<count):
        count=sum
    if(sum4<count):
        count=sum4
    if(sum1<count):
        count=sum1
    if(sum3<count):
        count=sum3
    if(sum2<count):
        count=sum2
    b+=1
    ma-=1
    m-=1
    mi+=1
    k+=1
    num-=1
print(count)

Submission status: Time Limit Exceeded

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

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

min_score = sum(A_minus_i)

scores = []

for i in range(min(A_minus_i), max(A_minus_i)+1):
    score = sum([abs(a - i) for a in A_minus_i])
    scores.append(score)

print(min(scores))

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    B = []
    ans = 10**9

    for i, a in enumerate(A):
        B.append(a - i - 1)

    for b in set(B):
        tmp = 0
        for i, a in enumerate(A):
            tmp += abs(a - i - b - 1)
        ans = min(ans, tmp)

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
N = int(input())

A = list(map(int, input().split()))
heikin = round(sum(A) / N) - 1

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

s = []
t = []

absmax = max(list(map(abs, A)))

for i in range(-absmax - 1  , absmax + 1):
    t = []
    for j in range(N):
        t.append(abs(A[j] +  i))
    s.append(sum(t))

print(min(s))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
t=1
b=10**15
c=-(10**15)
for i in a:
    k=i-t
    b=min(b,k)
    c=max(c,k)
    t+=1
v=10**15
for i in range(b,c+1):
    t=1
    k=0
    for j in a:
        k+=abs(j-(i+t))
        if t==n:
            v=min(v,k)
        t+=1
print(v)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin


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


def main():
    N = int(stdin.readline().rstrip())
    A = [int(x) for x in stdin.readline().rstrip().split()]

    l = -2 * 10 ** 6
    h = 2 * 10 ** 6
    while True:
        idx = (l + h) // 2
        tmp = [get_value(b, A) for b in [idx - 1, idx, idx + 1]]
        if min(tmp) == tmp[1]:
            print(tmp[1])
            return
        elif min(tmp) == tmp[2]:
            l = idx
        else:
            h = idx
        assert l < h


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    B = []
    ans = 10**9

    for i, a in enumerate(A):
        B.append(a - i - 1)

    for b in B:
        tmp = 0
        for i, a in enumerate(A):
            tmp += abs(a - i - b - 1)
        ans = min(ans, tmp)

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
def c_linear_approximation(N, A):
    t = [None for _ in range(len(A))]
    for j, k in enumerate(A):
        t[j] = k - (j+1)
    t_min, t_max = min(t), max(t)
    ans = float('inf')
    for b in range(-abs(t_min), abs(t_max)+1):
        tmp = 0
        for k in t:
            tmp += abs(k-b)
        ans = min(ans, tmp)
    return ans
N = int(input())
A = [int(i) for i in input().split()]
print(c_linear_approximation(N, A))

Submission status: Time Limit Exceeded

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

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

min_sum = 10 ** 9 * 2 * 10 ** 5
for i in range(min(A), max(A) + 1):
    summ = 0
    for j in range(N):
        summ += abs(A[j] - i)
    min_sum = min(min_sum, summ)
print(min_sum)

Submission status: Time Limit Exceeded

Solution code:

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

candidate = []
for j in range(-max(A),max(A)):
    sad = 0
    for i in range(N):
        sad += abs(A[i] - j - i)
    candidate.append(sad)
ans = min(candidate)
print(ans)

Submission status: Time Limit Exceeded

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

boundary = (-max(A)-1, max(A)+1)
sads = []

def sadness(A, b):
    return sum(abs(A[i] - b - i-1) for i in range(len(A)))
for b in range(*boundary):
    sads.append(sadness(A, b))
    
minimum = min(sads)
print(minimum)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split()
i = 0
while i < N:
    A[i] = int(A[i])
    i += 1
i = 0
B = []
while i < N:
    B.append(A[i] - i)
    i += 1
maxB = max(B)
minB = min(B)
M = max(B) - min(B) + 1
C = 100000000000000
i = 0
while i < M:
    K = min(B) + i
    temp = 0
    j = 0
    while j < N:
        temp += abs(B[j] - K)
        j += 1
    if C > temp:
        C = temp
    else:
        i + M
    i += 1
print(C)

Submission status: Time Limit Exceeded

Solution code:
input()
n = [(int(i) - h - 1)  for h, i in enumerate(input().split())]
l = len(n)
m = 10**9
for b in range(min(n), max(n)+1):
    t = 0
    for a_i in n:
        t += abs(a_i - b)
    if t < m:
        m = t
print(m)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
nums = []
total = 0
for i in range(N):
    A[i] = A[i] - (i+1)
    if A[i] not in nums:
        nums.append(A[i])
    total = total + A[i]

avg = (total/N)

'''
up = 0
down = 0
for i in range(N):
    if avg >= A[i]:
        down = down + 1
    else:
        up = up + 1


if down > up:
    if avg < 0:
        avg = int(avg) - 1
    else:
        avg = int(avg)
else:
    avg = round(avg)



sad = 0
for i in range(N):
    sad = sad + abs(A[i]-avg)
'''

l = [int(avg)-1,int(avg),int(avg) + 1]
sad = []
for i in range(3):
    tmp = 0
    for j in range(N):
        tmp = tmp + abs(A[j]-l[i])
    sad.append(tmp)

for i in range(len(nums)):
    tmp = 0
    for j in range(N):
        tmp = tmp + abs(A[j]-nums[i])
    sad.append(tmp)
    
print(min(sad))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python

import sys

def main(argv):
    line = sys.stdin.readline().rstrip("\n")

    while line:
        N = int(line)
        A = [int(a) for a in raw_input().rstrip("\n").split(" ")]
        diff = sorted([a - (i + 1) for i, a in enumerate(A)])
        m = []
        for i in range(N):
            left = 0
            for j in range(0, i):
                left += abs(diff[j] - diff[i])
            right = 0
            for j in range(i + 1, N):
                right += abs(diff[j] - diff[i])
            m.append(left + right)
        print(min(m))

        line = sys.stdin.readline().rstrip("\n")

if __name__ == "__main__":
    main(sys.argv)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
list1=[int(i) for i in input().split()]
list2=[]
for i in range(2*N):
    kana=0
    for j in range(N):
        kana+=abs(list1[j]-(min(list1)-N+i+j))
    list2.append(kana)
print(min(list2))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
ans = -1
for i in range(n):
    a[i] = a[i] - i - 1
count = Counter(a)
for b in count.keys():
    tmp = 0
    for i in range(n):
        tmp += abs(a[i] - b)
    if ans == -1 or tmp < ans:
        ans = tmp
print(ans)

Submission status: Time Limit Exceeded

Solution code:
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)


useds = []
min_value = 10000000000000000000

for j in range(len(sa_list)):
	if((sa_list[j] not in useds)==True):
		useds.append(sa_list[j])
		b = int(sa_list[j])
		sum_As = 0
		for p in range(1,len(A)+1):
			sum_As += abs(A[p-1]-(b+p))

		if(sum_As<min_value):
			min_value = int(sum_As)

print(min_value)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
sadlist = []
for h in range(n):
    a[h] = a[h] - (h + 1)
for i in a:
    sad = 0
    for j in range(n):
        sad = sad + abs(a[j] - i)
    sadlist.append(sad)
print(min(sadlist))

Submission status: Time Limit Exceeded

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

def kei(b):
    ANS=0
    for i in range(N):
        ANS+=abs(A[i]-(b+i+1))
    return ANS

x=kei(A[0])
for i,j in enumerate(A):
    x=min(x,kei(j-i-1))
    
print(x)

Submission status: Time Limit Exceeded

Solution code:


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


n = int(input())
arr = input().split(" ")
numbers = []
for i in range(0,len(arr)):
    numbers.append(int(arr[i]))

max = sorted(numbers)[0]
#print(max)
#print(max)
min = 9223372036854775807
for i in range(-max-n, max+n+1):
   s = sadness(numbers,i,n)
   #print(min)
   #print(s)
   if s < min:
       min = s
print(min)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
num=150
count=46463435
ma=max(l)
m=ma//2
k=m
b=0
while(num>=0):
    sum=0
    sum1=0
    sum2=0
    sum3=0
    for i in range(len(l)):
        sum+=abs(l[i]-(b+i+1))
        sum1+=abs(l[i]-(ma+i+1))
        sum3+=abs(l[i]-(m+i+1))
        sum2+=abs(l[i]-(k+i+1))
    if(sum<count):
        count=sum
    if(sum1<count):
        count=sum1
    if(sum3<count):
        count=sum3
    if(sum2<count):
        count=sum2
    b+=1
    ma-=1
    m-=1
    k+=1
    num-=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin


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


def main():
    N = int(stdin.readline().rstrip())
    A = [int(x) for x in stdin.readline().rstrip().split()]

    l = -2 * 10 ** 5
    h = 2 * 10 ** 5
    while True:
        idx = (l + h) // 2
        tmp = [get_value(b, A) for b in [idx - 1, idx, idx + 1]]
        if min(tmp) == tmp[1]:
            print(tmp[1])
            return
        elif min(tmp) == tmp[2]:
            l = idx
        else:
            h = idx


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

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

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

min_A = min(A_list)
max_A = max(A_list)

sadness = sys.maxsize
for b in range(min_A, max_A + 1):
    tmp_sadness = 0
    for A in A_list:
        tmp_sadness += abs(A - b)
    if(sadness > tmp_sadness):
        sadness = tmp_sadness

print(sadness)

Submission status: Time Limit Exceeded

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

ans=9999999999

n1=min(min(A),max(A)-N)
n2=max(A)

for b in range(n1,n2):
    num=0
    for i,a in enumerate(A):
        num += abs(a-(b+i+1))
    if num<ans: 
        ans=num
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
num=400
count=46463435
ma=max(l)
m=ma//2
k=m
b=0
while(num>=0):
    sum=0
    sum1=0
    sum2=0
    sum3=0
    for i in range(len(l)):
        sum+=abs(l[i]-(b+i+1))
        sum1+=abs(l[i]-(ma+i+1))
        sum3+=abs(l[i]-(m+i+1))
        sum2+=abs(l[i]-(k+i+1))
    if(sum<count):
        count=sum
    if(sum1<count):
        count=sum1
    if(sum3<count):
        count=sum3
    if(sum2<count):
        count=sum2
    b+=1
    ma-=1
    m-=1
    k+=1
    num-=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
INF = 10**10
n = int(input())
a = [int(_) - (i + 1) for i, _ in enumerate(input().split())]

grief = INF
for b in set(a):
    tmp = sum([abs(ai - b) for ai in a])
    if tmp < grief:
        bb = b
        grief = tmp

print(grief)

Submission status: Time Limit Exceeded

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

Ai_i = []

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

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

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

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

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

min_abs = float("inf")
min_index = 0

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

print(min_abs)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split()
i = 0
while i < N:
    A[i] = int(A[i])
    i += 1
i = 0
B = []
while i < N:
    B.append(A[i] - i)
    i += 1
maxB = max(B)
minB = min(B)
M = max(B) - min(B) + 1
C = 100000000000000
i = 0
while i < M:
    K = min(B) + i
    temp = 0
    j = 0
    while j < N:
        temp += abs(B[j] - K)
        j += 1
    if C > temp:
        C = temp
    i += 1
print(C)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
num=600
count=46463435
ma=max(l)
m=ma//2
k=m
b=0
while(num>=0):
    sum=0
    sum1=0
    sum2=0
    sum3=0
    for i in range(len(l)):
        sum+=abs(l[i]-(b+i+1))
        sum1+=abs(l[i]-(ma+i+1))
        sum3+=abs(l[i]-(m+i+1))
        sum2+=abs(l[i]-(k+i+1))
    if(sum<count):
        count=sum
    if(sum1<count):
        count=sum1
    if(sum3<count):
        count=sum3
    if(sum2<count):
        count=sum2
    b+=1
    ma-=1
    m-=1
    k+=1
    num-=1
print(count)

Submission status: Time Limit Exceeded

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

min_sum = -1
i_atmin = -1

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

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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)


ans=float("inf")
for x in K:
	t=0
	for y in C:
		t+=abs(-1*x+y)
	ans=min(ans, abs(t))

print ans

Submission status: Time Limit Exceeded

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

minabs = -1
for b in range(min(A)-N,max(A) + 1):
    sumA = 0
    for i in range(0,N):
        sumA += abs(A[i] - (b+i))
    if minabs == -1:
        minabs = sumA
    if sumA < minabs:
        minabs = sumA
print(minabs)

Submission status: Time Limit Exceeded

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

ans = 999999999
for i in range(-1 * max(a), min(a)):
    tmp = sum([abs(a[j] - (i + (j + 1))) for j in range(n)])
    if ans > tmp:
        ans = tmp
    
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
N = input()
AList = list(map(int, input().split()))

mode = 1
L = []
p = 0
for i in range(-1, 2):
    for j in range(1,int(N)+1):
        p += abs(AList[j-1] -(i + j))
    L.append(p)        
    p = 0

if L[1] == min(L):
    print(L[1])
    sys.exit()
elif L[0] == min(L):
    pre = L[0]
    mode = -1
else:
    pre = L[2]
    mode = 1    
    
for i in range(2 * mode,1000000000 * mode ,mode):
    for j in range(1,int(N)+1):
        p += abs(AList[j-1] -(i + j))
    if pre < p:
        print(pre)
        sys.exit()
    else:
        pre = p
    p = 0

Submission status: Time Limit Exceeded

Solution code:
number = input()
b = input()
b = b.split()
x=[]
y=[]

for j in range(1000000):
    x.append(-j)
x.sort()

for j in range(1000000):
    x.append(j+1)

for h in x:
    sad=0
    for i,b2 in enumerate(b):
        i = int(i)
        b2 = int(b2)
        sad+=abs(b2 - h - i)
    y.append(sad)

print(min(y))

Submission status: Time Limit Exceeded

Solution code:


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


n = int(input())
arr = input().split(" ")
numbers = []
for i in range(0,len(arr)):
    numbers.append(int(arr[i]))

max = sorted(numbers)[len(numbers)-1]
#print(max)
min = 9223372036854775807
for i in range(-max, max):
   s = sadness(numbers,i,n)
   #print(min)
   if s < min:
       min = s
print(min)

Submission status: Time Limit Exceeded

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

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

print(min(ans))

Submission status: Time Limit Exceeded

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


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


sadnl = [sadness(aa, i) for i in range(-10,11)]
sadnli = sadnl.index(min(sadnl))

if -10 < sadnli < 10:
    print(min(sadnl))
elif sadnli <= -10:
    fsadn = min(sadnl)
    while True:
        sadnli -= 1
        nsadn = sadness(aa, sadnli)
        if nsadn > fsadn:
            print(fsadn)
            break
        else:
            fsadn = nsadn
elif sadnli >= 10:
    fsadn = min(sadnl)
    while True:
        sadnli += 1
        nsadn = sadness(aa, sadnli)
        if nsadn > fsadn:
            print(fsadn)
            break
        else:
            fsadn = nsadn

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())), dtype= int)
I = np.arange(1, N+1)
C = A - I
mincost = -1
for b in range(C.min(), C.max()+1):
    cost = np.abs(C - b).sum()
    mincost = cost if mincost < 0 else min(mincost, cost)
print(mincost)

Submission status: Time Limit Exceeded

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


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

bmax = max(A)
bmin = min(A)

for i in range(-bmax, 1):
    temp = sadness(i, A)
    if i == -bmax:
        ans = temp
    if temp < ans:
        ans = temp
print(ans)

Submission status: Time Limit Exceeded

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

n, = read_line(int)

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

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

min_sadness = float('inf')
b = None
for i in range(min(AA), max(AA) + 1):
    sadness = 0
    for a in AA:
        sadness += abs(a - i)

    if min_sadness > sadness:
        min_sadness = sadness
        b = i

print(min_sadness)

Submission status: Time Limit Exceeded

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_abs = float("inf")
min_index = 0

for b in range(-max_abs, max_abs+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: Time Limit Exceeded

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

ans = 999999999
for i in range(-1 * max(a), n):
    tmp = sum([abs(a[j] - (i + (j + 1))) for j in range(n)])
    if ans > tmp:
        ans = tmp

print(ans)

Submission status: Time Limit Exceeded

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

max = max(a)
ans = 10000000
for b in range(max*-1,max):
  sum = 0
  for i, j in enumerate(a):
    i = i+1
    sum += abs(j - (b+i))
  if ans > sum:
    ans = sum

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
INF = 10**10
n = int(input())
a = [int(_) - (i + 1) for i, _ in enumerate(input().split())]
a.sort()

grief = INF
for b in set(a):
    tmp = sum([abs(ai - b) for ai in a])
    if tmp < grief:
        grief = tmp

print(grief)

Submission status: Time Limit Exceeded

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

max_num = max(n_list)
min_num = 0 - max_num

min_sad = float('Inf')

for b in range(min_num, max_num+1):
  sad = 0
  for i, a in enumerate(n_list):
    sad += abs(a-(b+i+1))
  min_sad = min(min_sad, sad)

print(min_sad)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = input().split()
A = []
for i in range(N):
    A.append(int(a[i]))
kanasi = 2 * 10 ** 5 * 10 ** 9
b = max(A)
while(1):
    k = 0
    for i in range(N):
        k += abs(A[i] - (b + i + 1))
    if kanasi > k:
        kanasi = k
    else:
        break
    b -= 1
print(kanasi)

Submission status: Time Limit Exceeded

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


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

bmax = max(A)
bmin = min(A)

for i in range(-bmax, bmax + 1):
    temp = sadness(i, A)
    if i == -bmax:
        ans = temp
    if temp < ans:
        ans = temp
print(ans)

Submission status: Time Limit Exceeded

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

def res(b):
    return np.sum(np.abs(A-(b+np.array(range(1, N+1)))))

tmp = np.sum(A) // N
a = []
for i in range(tmp-100, tmp+100):
    a.append(res(i))
print(min(a))