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:
from statistics import mean
import math

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

b = round(mean(A))

ans1 = 0
ans2 = 0
for i in range(N):
    ans1 += abs(math.floor(mean(A))-A[i])
    ans2 += abs(math.ceil(mean(A)) - A[i])
    
print(min(ans1,ans2))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
import bisect
n=int(input())
lists=list(map(int,input().split()))
uselists=[]
for _ in range(n):
    uselists.append(lists[_]-_-1)
uselist=sorted(uselists)
numbers=uselist[0]
while bisect.bisect_right(uselist,numbers)<n-bisect.bisect_right(uselist,numbers):
    numbers+=1
ans=0
for x in range(n):
    ans+=abs(uselist[x]-numbers)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(a)-(i+1) for i, a in enumerate(input().split())]
ans = prv_ans = float('inf')
for b in range(min(A), max(A)+1):
  tmp_ans = sum([abs(A[i]-b) for i in range(N)])
  if prv_ans < tmp_ans: break
  ans = min(ans, tmp_ans)
  tmp_ans = ans
print(ans)

Submission status: Time Limit Exceeded

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

minv = float('inf')
for i in range(-max(a),max(a)+1):
  cnt = 0
  for j in range(N):
    cnt += abs(a[j] - (i + j))
  if minv > cnt:
    minv = cnt
    
print(minv)

Submission status: Time Limit Exceeded

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

aa = sorted([a[i]-i-1 for i in range(n)])
aa = [_-min(aa) for _ in aa]

if n % 2 == 1:
    idx = n // 2
    res = 0
    for ai in aa[:idx]:
        res += aa[idx] - ai
    for ai in aa[idx:]:
        res += ai - aa[idx]

else:
    idx_1 = n // 2 -1
    idx_2 = n // 2
    res_1 = 0
    for ai in aa[:idx_1]:
        res_1 += aa[idx_1] - ai
    for ai in aa[idx_1:]:
        res_1 += ai - aa[idx_1]
    res_2 = 0
    for ai in aa[:idx_2]:
        res_2 += aa[idx_2] - ai
    for ai in aa[idx_2:]:
        res_2 += ai - aa[idx_2]
    res = min(res_1, res_2)

print(res)

Submission status: Time Limit Exceeded

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

aa = sorted([a[i]-i-1 for i in range(n)])
aa = [_-min(aa) for _ in aa]

sum_left = [0]
tmp = 0
for i in range(1, n):
    tmp += (aa[i] - aa[i-1]) * i
    sum_left.append(tmp)

sum_right = [0]
tmp = 0
for i in range(1, n):
    tmp += (aa[n-i] - aa[n-i-1]) * i
    sum_right.append(tmp)
sum_right = sum_right[::-1]

print(min([sum_left[i]+sum_right[i] for i in range(n)]))

Submission status: Time Limit Exceeded

Solution code:
def solve(n, ax):
    ans = float('inf')
    substract_ax_from_i = []

    for i in range(n):
        substract_ax_from_i.append(ax[i]-i-1)
    #    print('ax: ', ax[i], 'ax-i: ', ax[i]-i-1)
    substract_ax_from_i.sort()
    #print(substract_ax_from_i)

    for i in substract_ax_from_i:
        list_when_choose_i = list(map(lambda x: abs(x-i), substract_ax_from_i))
        sum_of_list_when_choose_i = sum(list_when_choose_i)
        ans = min(ans, sum_of_list_when_choose_i)
        #print(i, list_when_choose_i, ans)

    return ans


if __name__ == '__main__':
    n = int(input())
    ax = list(map(int, input().split()))
    print(solve(n, ax))

Submission status: Time Limit Exceeded

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

d = c[0]
ans = []
count = 0
while c[-1] != 0:
    ans.append(sum([abs(i) for i in c]))
    for e, name in enumerate(c):
       if name >0:
           break
    c = [i - name for i in c]
    if count >0:
        if ans[-1] >= ans[-2]:
            break
    count += 1

ans.append(sum([abs(i) for i in c]))
print(min(ans))

Submission status: Time Limit Exceeded

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

d = []
count = 0
while c[-1] != -1:
    d.append(sum([abs(i) for i in c]))
    for i in range(len(c)):
        c[i] -= 1
    if count > 0:
        if d[-1] >= d[-2]:
            break
    count += 1
print(min(d))

Submission status: Time Limit Exceeded

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

d = []
while c[-1] != -1:
    d.append(sum([abs(i) for i in c]))
    for i in range(len(c)):
        c[i] -= 1

print(min(d))

Submission status: Time Limit Exceeded

Solution code:
def f(b):
    cnt = 0
    for i in range(N):
        cnt += abs(A[i]-(b+(i+1)))
    return cnt

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

b = 0
if f(-1) < f(0):
    while True:
        if f(b-1) > f(b):
            print(f(b))
            exit()
        else:
            b += -1
else:
    while True:
        if f(b) < f(b+1):
            print(f(b))
            exit()
        else:
            b += 1

Submission status: Time Limit Exceeded

Solution code:
from statistics import median as m
n=int(input())
A=list(map(int,input().split()))
B=sorted([A[i]-i-1 for i in range(n)])
print(int(sum([abs(b-m(B)) for b in B])))

Submission status: Time Limit Exceeded

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

N = int(input().split()[0])
a_list = list(map(int, input().split()))
min_b = float('inf')
max_a = max(a_list)

work_list = [a - i - 1 for i, a in enumerate(a_list)]
work_list = list(set(work_list))

for b in work_list:  # 10 ** 5
    total_b = 0
    for i, a in enumerate(a_list):  # 10 ** 5
        total_b += abs(a - (b + i + 1))
    min_b = min(min_b, total_b)

print(min_b)

Submission status: Time Limit Exceeded

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
##############################

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

def right(N, A):
    X = [0] * N
    for i in range(N):
        X[i] = abs(A[i] - (i+1))

    return max(X)

r = right(N, A)
#print(r)

ans = float('inf')
for b in range(-r, r+1):
    c = 0
    for i in range(1, N+1):
        c += abs(A[i-1] - (b+i))

    ans = min(ans, c)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect

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

a2=[]

for i in range(n):
    temp = a[i]-(i+1)
    index = bisect(a2,temp)
    a2.insert(index,temp)

ans = sum( abs(x - a2[n//2]) for x in a2)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from math import sqrt
n = int(input())
a = list(map(int, input().split()))
mean = sum(a)//n
root = int(sqrt(n))

def sadness(b) :
    return sum(abs(a[i]-b-i-1) for i in range(n))

print(min(sadness(mean-n//2+i) for i in range(-root, root)))

Submission status: Time Limit Exceeded

Solution code:
def absFunc(A_, b):
    result = 0
    for a_ in A_:
        result += abs(a_ - b)
    return result
    
input()
A = list(map(int, input().split()))
A_ = []

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

b = 0
result_o = absFunc(A_, b)
result_n = 0

while True:
    b += 1
    result_n = absFunc(A_, b)
    
    if result_n < result_o:
        result_o = result_n
        continue
    break

b = 0
while True:
    b -= 1
    result_n = absFunc(A_, b)
    
    if result_n < result_o:
        result_o = result_n
        continue
    break

print(result_o)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from statistics import mean

N = int(input())
A = list(map(int, input().split()))
A_star = [a - (i + 1) for i, a in enumerate(A)]

def calc_score(b):
    score = 0
    for a in A_star:
        score += abs(a - b)
    return score

def decide_direction(b):
    score0 = calc_score(b)
    score1 = calc_score(b + 1)
    if score0 > score1:
        return 1
    else:
        return -1

b = int(mean(A_star))
direction = decide_direction(b)
pre_score = calc_score(b)

while True:
    b += direction
    score = calc_score(b)
    if score > pre_score:
        print(pre_score)
        break
    else:
        pre_score = score

Submission status: Time Limit Exceeded

Solution code:
import heapq

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

def calc_score(b):
    score = 0
    for i, a in enumerate(A):
        score += abs(a - (b + i + 1))
    return score


def decide_direction():
    score_b0 = calc_score(0)
    score_b1 = calc_score(1)
    
    if score_b0 > score_b1:
        return 1
    else:
        return -1


pre_score = calc_score(0)
b = 0
direction = decide_direction()

while True:
    b += direction
    score = calc_score(b)
    if score > pre_score:
        print(pre_score)
        break
    else:
        pre_score = score

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
total=0
total2=0
count=1
ans=0
for i in range(N):
    total+=A[i]
for i in range(1,N+1):
    total2+=i
while True:
    if total>total2:
        if total//N+total%N>total2:
            count+=(total-total//N+total%N)//N
            total=total//N+total%N
        else:
            break
    elif total<total2:
        if total+total*N<total2:
            count+=total
            total+=total*N
        else:
            break
    else:
        break

if total>total2:
    while True:
        total2 +=N
        count+=1
        if total2>=total:
            if abs(total2-total)<=abs(total2-total-N):
                break
            else:
                count-=1
                break
elif total<total2:
    while True:
        total2 -=N
        count-=1
        if total2<=total:
            if abs(total2-total)<=abs(total2-total-N):
                break
            else:
                count+=1
                break

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

print(ans)

Submission status: Time Limit Exceeded

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


if total>total2:
    while True:
        total2 +=N
        count+=1
        if total2>=total:
            if abs(total2-total)<=abs(total2-total-N):
                break
            else:
                count-=1
                break
elif total<total2:
    while True:
        total2 -=N
        count-=1
        if total2<=total:
            if abs(total2-total)<=abs(total2-total-N):
                break
            else:
                count+=1
                break

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = range(-N//2 + 1, max(A))
S = list()
s = 0

for b in B:
    for idx, it in enumerate(A):
        s += abs(it - (b+idx))
    S.append(s)
    s = 0
    
print(min(S))

Submission status: Time Limit Exceeded

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

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

Min = min(lst)
Max = max(lst)

for b in range(Min,Max+1):
    scr = 0
    for i in range(n):
        scr += abs(lst[i] - b)
    if b == Min:
        Min_scr = scr
    elif scr < Min_scr:
        Min_scr = scr
print(Min_scr)

Submission status: Time Limit Exceeded

Solution code:
def INT(): return int(input())
def LIST(): return list(map(int, input().split()))

INF = float('inf')


def sad(A, b):
    s = 0

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

    return s


def main():
    N = INT()
    A = LIST()
    MA = max(A)
    m = INF

    for i in range(N):
        s = sad(A, MA-i)
        if s < m:
            m = s

    print(m)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def INT(): return int(input())
def LIST(): return list(map(int, input().split()))
 
INF = float('inf')
 
 
def sad(A, b):
    s = 0
 
    for i, a in enumerate(A):
        s += abs(a - (b + i))
 
    return s
 
 
def main():
    N = INT()
    A = LIST()
    m = INF
 
    for i in range(N):
    	if sad(A, max(A)-i) < m:
        	m = sad(A, max(A)-i)

            
    print(m)
        
 
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(raw_input())
nums = map(int, raw_input().split(' '))
diffs = [nums[i] - i - 1 for i in range(N)]

def sunukes(b):
    return sum([abs(nums[i] - (b + 1 + i)) for i in range(N)])

diffs = set(diffs)
ans = 1000000000000
mmm = sum(diffs) / len(diffs)
for d in range(-10000, 10000, 1):
    ans = min(ans, sunukes(d + mmm))
print ans

Submission status: Time Limit Exceeded

Solution code:
N = int(raw_input())
nums = map(int, raw_input().split(' '))
diffs = [nums[i] - i - 1 for i in range(N)]

def sunukes(b):
    return sum([abs(nums[i] - (b + 1 + i)) for i in range(N)])

diffs = set(diffs)
ans = 1000000000000
for d in diffs:
    ans = min(ans, sunukes(d))
print ans

Submission status: Time Limit Exceeded

Solution code:
N = int(raw_input())
nums = map(int, raw_input().split(' '))
def sunukes(b):
    return sum([abs(nums[i] - (b + 1 + i)) for i in range(N)])
l = -int(1e14)
offset = int(1e14)
while True:
    r = l + offset
    tl = sunukes(l)
    tr = sunukes(r)
    if tl - tr < N * offset:
        offset /= 10
    else:
        l += offset
    # print l, tl, r, tr, (tl - tr), (N * offset)                                                                                                                                                           
    if offset < 10000:
        ans = 1000000000000
        for i in range(10001):
            # print (i+l), ',' , sunukes(i + l)                                                                                                                                                             
            ans = min(sunukes(i + l), ans)

        print ans
        break

Submission status: Time Limit Exceeded

Solution code:
N = int(raw_input())
nums = map(int, raw_input().split(' '))
def sunukes(b):
    return sum([abs(nums[i] - (b + 1 + i)) for i in range(N)])
l = -int(1e14)
offset = int(1e10)
while True:
    r = l + offset
    tl = sunukes(l)
    tr = sunukes(r)
    if tl - tr < N * offset:
        offset /= 10
    else:
        l += offset
    # print l, tl, r, tr, (tl - tr), (N * offset)                                                                                                                                                           
    if offset < 10000:
        ans = 1000000000000
        for i in range(10001):
            # print (i+l), ',' , sunukes(i + l)                                                                                                                                                             
            ans = min(sunukes(i + l), ans)

        print ans
        break

Submission status: Time Limit Exceeded

Solution code:
N = int(raw_input())
nums = map(int, raw_input().split(' '))
def sunukes(b):
    return sum([abs(nums[i] - (b + 1 + i)) for i in range(N)])
l = -int(1e9)
offset = 10000000
while True:
    r = l + offset
    tl = sunukes(l)
    tr = sunukes(r)
    if tl - tr < N * offset:
        offset /= 10
    else:
        l += offset
    # print l, tl, r, tr, (tl - tr), (N * offset)                                                                                                                                                           
    if offset < 10000:
        ans = 1000000000000
        for i in range(10001):
            # print (i+l), ',' , sunukes(i + l)                                                                                                                                                             
            ans = min(sunukes(i + l), ans)

        print ans
        break

Submission status: Time Limit Exceeded

Solution code:
N = int(raw_input())
nums = map(int, raw_input().split(' '))
def sunukes(b):
    return sum([abs(nums[i] - (b + 1 + i)) for i in range(N)])
l = -int(1e9)
offset = 1000000
while True:
    r = l + offset
    tl = sunukes(l)
    tr = sunukes(r)
    if tl - tr < N * offset:
        offset /= 10
    else:
        l += offset
    if offset < 1000:
        ans = 1000000000000
        for i in range(1001):
            ans = min(sunukes(i + l), ans)

        print ans
        break

Submission status: Time Limit Exceeded

Solution code:
import numpy as np 
n = int(input())
li = list(map(int, input().split()))
li = np.array([li[i-1] - i for i in range(1, n+1)])
temp = sum(li)
if temp < 0:
    plus = 1
else:
    plus = -1

while 1:
    li += plus
    if (plus == 1 and plus < sum(li)) or (plus == -1 and plus > sum(li)):
        break
    else:
        temp = sum(li)

temp_li = [abs(i) for i in li]
li -= plus
li = [abs(i) for i in li]
print(min(sum(temp_li), sum(li)))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
li = list(map(int,input().split()))
for i in range(n):
  li[i] -= i+1
li.sort()
ans = 10**15
for i in range(n):
  ans = min(ans,i*li[i]-sum(li[:i])+sum(li[i:])-(n-i)*li[i])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# ABC102 C-Linear Approximation

import numpy as np

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

As = [0]*N
for i, j in enumerate(A):
    As[i] = j - (i+1)

As = np.array(As)
temp = np.median(As)
# temp = sum(As) // len(As)
ans1, ans2, ans3 = 0, 0, 0
for i in As:
    ans1 += abs(i - temp)
    ans2 += abs(i - (temp+1))
    ans3 += abs(i - (temp-1))
    
print(int(min(ans1, ans2, ans3)))

Submission status: Time Limit Exceeded

Solution code:
# ABC102C - Linear Approximation (ARC100C)
from statistics import median_high, median_low


def main():
    n = int(input())
    A = tuple(map(int, input().rstrip().split()))
    A = tuple(j - i for i, j in enumerate(A, start=1))
    ans = float("inf")
    for b in range(median_low(A), median_high(A) + 1):
        ans = min(ans, sum(abs(a - b) for a in A))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
a=list(map(int,input().split()))
a=[a[i]-i for i in range(N)]
from statistics import median
print(sum([abs(b-int(median(a))) for b in a]))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = input().split()
a = [int(a[i]) for i in range(n)]
m = max(a)
ss = []
for b in range(-1 * m,m):
  s = 0
  for i in range(n):
    s = s + abs(a[i] - (b+i+1))
  ss.append(s)
print(min(ss))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
m = float("inf")
for i in range(min(a)-n, max(a) + 1):
    kanashisa = 0
    for j in range(n):
        kanashisa += abs(a[j] - (i + j + 1))
    if kanashisa < m:
        m = kanashisa
print(m)

Submission status: Time Limit Exceeded

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

if N == 1:
    print(0)
else:    
    sumlist = []
    for i in range(N-1):
        if i < N // 2:
            sumlist.append(- sum(Alist[:i+1]) + sum(Alist[i+1:]) - Alist[i+1] * (N - 2 * (i + 1)))
        else:
            sumlist.append(- sum(Alist[:i+1]) + sum(Alist[i+1:]) + Alist[i] * (N - 2 * (N - i - 1)))
        
    print(min(sumlist))

Submission status: Time Limit Exceeded

Solution code:
memo={}
def calc(A, b):
    score = memo.get(b, None)
    if not score is None:
        return score
    score = sum(abs(a-b) for a in A)
    memo[b] = score
    return score

def findmin(A, l, u):
    d = (u-l)//3
    if d == 0:
        return min(calc(A, i) for i in range(l, u+1))
    left, right = l + d, u - d
    sl = calc(A, left) 
    sr = calc(A, right) 
    if sl > sr:
        return findmin(A, left, u)
    else:
        return findmin(A, l, right)

def solve(A):
    A = sorted(a-(i+1) for i, a in enumerate(A))
    return findmin(A, A[0], A[-1])

#solve(list(range(1000, 2*10**5)))
N = int(input())
A = [int(x) for x in input().split()]

#abs(A1-(b+1)) + abs(Ai-(b-i)) + abs(AN-(b-N))


# b1 = A[0]    ならば (A[i]-b1) >= 0
# b2 = A[0] -1 ならば (A[i]-b2) > (A[i]-b1) >= 0 なので、スコアは大きくなる

# b = A[-1] ならば (A[i]-b) <= 0

print(solve(A))

Submission status: Time Limit Exceeded

Solution code:
memo={}
def calc(A, b):
    score = memo.get(b, None)
    if not score is None:
        return score
    score = sum(abs(a-b) for a in A)
    memo[b] = score
    return score

def findmin(A, l, u):
    if l + 1 == u:
        return min(calc(A, l), calc(A, u))
    if l == u:
        return calc(A, l)
    d = (u-l)//3
    if d == 0:
        return min(calc(A, i) for i in range(l, u+1))
    left, right = l + d, u - d
    sl = calc(A, left) 
    sr = calc(A, right) 
    if sl > sr:
        return findmin(A, left, u)
    else:
        return findmin(A, l, right)

def solve(A):
    A = sorted(a-(i+1) for i, a in enumerate(A))
    return findmin(A, A[0], A[-1])

#solve(list(range(1000, 2*10**5)))
N = int(input())
A = [int(x) for x in input().split()]

#abs(A1-(b+1)) + abs(Ai-(b-i)) + abs(AN-(b-N))


# b1 = A[0]    ならば (A[i]-b1) >= 0
# b2 = A[0] -1 ならば (A[i]-b2) > (A[i]-b1) >= 0 なので、スコアは大きくなる

# b = A[-1] ならば (A[i]-b) <= 0

print(solve(A))

Submission status: Time Limit Exceeded

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

for i in range(n):
  ai = a[i] - (i+1)
  li.append(ai)
  li_abs.append(abs(ai))

ans = 10**10
for d in range(-max(li_abs),max(li_abs)+1):
  li2 = []
  for i in range(n):
    li2.append(abs(li[i] - d))
  if sum(li2) < ans:
    ans = sum(li2)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
n=int(input())
A=[int(i) for i in input().split()]
for i in range(n):
    A[i]-=i+1
ans=[]
for j in range(min(A),max(A)+1):
    B=A
    for i in range(n):
        B[i]-=j
        B[i]=abs(B[i])
    ans.append(sum(B))
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
L=[A[i]-(i+1) for i in range(N)]
L.sort()
p=min(L)
q=max(L)
g=10**1000
for b in range(p,q+1):
    w=sum(abs(A[i]-(b+i+1)) for i in range(N))
    if w<g:
        g=w
print(g)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
p=min(A[i]-(i+1) for i in range(N))
q=max(A[i]-(i+1) for i in range(N))
g=10**1000
for b in range(p,q+1):
    w=sum(abs(A[i]-(b+i+1)) for i in range(N))
    if w<g:
        g=w
print(g)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
p=min(A[i]-(i+1) for i in range(N))
q=max(A[i]-(i+1) for i in range(N))
g=10**9
for b in range(p,q+1):
    if sum(abs(A[i]-(b+i+1)) for i in range(N))<g:
        g=sum(abs(A[i]-(b+i+1)) for i in range(N))
print(g)

Submission status: Time Limit Exceeded

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

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

def moveb(A):
  global b
  thresh_up = sadness(A, b) - sadness(A, b + 1)
  thresh_dw = sadness(A, b) - sadness(A, b - 1)
  if ((thresh_up > 0) and (thresh_dw < 0)):
    b -= 1
  elif ((thresh_up < 0) and (thresh_dw > 0)):
    b += 1
  elif ((thresh_up < 0) and (thresh_dw < 0)):
    return 'success'
  return 'calculating'
  

b = 0
ans = ''
while ans != 'success':
  moveb(A)

print (ans, sadness(A, b))

Submission status: Time Limit Exceeded

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

def return_kanashisa(b):

	kanashisa = 0

	for a, num in zip(a_lst, range(1, n + 1)):
		kanashisa += abs(a - (b + num))

	return kanashisa

b_lst = []
for b in range(-max(a_lst), max(a_lst)):
	b_lst.append(return_kanashisa(b))
    
print(min(b_lst))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
a = np.array([int(i) for i in input().split()])

min_num = 100000000000

for x in range(-(max(a)), max(a) + 1):
  min_sum = sum(abs(a - np.ones(len(a)) * x - np.array(range(1, n + 1))))
  #print(abs(a - np.array(range(1, n + 1))))
  if min_sum < min_num:
    min_num = min_sum

print(int(min_num))

Submission status: Time Limit Exceeded

Solution code:
def main():
	N = int(input())
	A = list(map(int, input().split()))
	ans = 0
	max_val = 0
	for i in range(N):
		ans += abs(A[i]-(i+1))
		max_val = max(max_val, abs(A[i]-(i+1)))

	for j in range(-max_val, max_val+1):
		this_ans = 0
		for i in range(N):
			this_ans += abs(A[i]-(i+j+1))
		ans = min(ans, this_ans)
	print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a_list = [int(i) for i in input().split()]
d_list = [0 for i in range(N)]
for i in range(N):
    d_list[i] = a_list[i] - (i+1)
ans = 2 * (10 ** 5) * 10 ** 9
for b in range(min(d_list), max(d_list)+1):
    c = 0
    for i in range(N):
        c += abs(a_list[i] - (b + i + 1))
    ans = min(c, ans)
print(ans)

Submission status: Time Limit Exceeded

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

ans = float("inf")
for i in range(min(a),max(a)+1):
  b = [abs(a[ai]-i) for ai in range(len(a))]
  ans = min(ans, sum(b))
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq
import itertools
import operator


inf = float('inf')


# globals
N = 0
A = []
X = []


def set_inputs():
    global N, A
    N = get_int()
    A = get_li()
    return


def f(n):
    return sum(abs(x-n) for x in X)


def main():
    global X
    setrecursionlimit(100000)
    set_inputs()
    X = [a - (i + 1) for i, a in enumerate(A)]
    b = round(float(sum(X)) / float(N))
    ans = f(b)
    a = b - 1
    c = b + 1
    while ans != min(f(a), f(b), f(c)):
        if f(a) < f(b):
            b = a
            a = b - 1
            c = b + 1
        else:
            b = c
            a = b - 1
            c = b + 1
        ans = f(b)
    print(ans)
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types):
    if len(types) == 1:
        return [types[0](input()) for _ in range(n)]
    return zip(*(
        [t(x) for t, x in zip(types, input().split())]
        for _ in range(n)
    ))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

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


min=10**14
for i in range(N):
  sum=0
  p=M[i]
  for j in range(N):
      sum=sum+abs(M[j]-p)
  if sum<min:
    min=sum

print(min)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
x = [int(i) for i in input().split()]
x = [x_ - i for i, x_ in enumerate(x)]
min_x = min(x)
max_x = max(x)
ans = []
for i in range(min_x, max_x+1):
    p = sum([abs(x_ - i) for x_ in x])
    ans.append(p)
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

N = I()
A = LI()

for idx in range(0, len(A)):
    A[idx] -= idx + 1
#  print(A)
# 全探索？
result = 10**12
for a in set(A):
    b = a
    tmp = 0
    for sub_a in A:
        tmp += abs(sub_a- b)
    result = min(result, tmp)
print(result)
exit()
#  b = sum(A) / len(A)
#  print('b', b)
#  if b < 0:
#      b = -(round(-b))
#  else:
#      b = round(b)
#  print('b', b)
#  result = 0
#  for a in A:
#      result += abs(a-b)
#  print(result)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
N=int(input())
A=list(map(int,input().split()))
l=[]
n=1
for i in A:
    l.append(i-n)
    n+=1
c=Counter(l)
c=c.most_common(N)
ans_l=[]
for i in range(len(c)):
    b=c[i][0]
    ans=0
    for j in l:
        ans+=abs(j-b)
    ans_l.append(ans)
print(min(ans_l))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=[0]*n
for i in range(n):
    b[i]=a[i]-(i+1)
s=0
for i in range(n):
    s+=b[i]
mean=s//(len(b))
ans=10**10
mi=mean-10**3
ma=mean+10**3
for i in range(mi,ma):
    w=0
    c=list(map(lambda x:abs(x-i),b))
    for j in range(n):
        w+=c[j]
    if w<ans:
        ans=w
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=[0]*n
for i in range(n):
    b[i]=a[i]-(i+1)
s=0
for i in range(n):
    s+=b[i]
mean=s//(len(b))
ans=10**10
mi=mean-10**4
ma=mean+10**4
for i in range(mi,ma):
    w=0
    c=list(map(lambda x:abs(x-i),b))
    for j in range(n):
        w+=c[j]
    if w<ans:
        ans=w
print(ans)

Submission status: Time Limit Exceeded

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