Problem:


Score : 300 points

Problem Statement

There are N sightseeing spots on the x-axis, numbered 1, 2, ..., N.
Spot i is at the point with coordinate A_i.
It costs |a - b| yen (the currency of Japan) to travel from a point with coordinate a to another point with coordinate b along the axis.

You planned a trip along the axis.
In this plan, you first depart from the point with coordinate 0, then visit the N spots in the order they are numbered, and finally return to the point with coordinate 0.

However, something came up just before the trip, and you no longer have enough time to visit all the N spots, so you decided to choose some i and cancel the visit to Spot i.
You will visit the remaining spots as planned in the order they are numbered.
You will also depart from and return to the point with coordinate 0 at the beginning and the end, as planned.

For each i = 1, 2, ..., N, find the total cost of travel during the trip when the visit to Spot i is canceled.

Constraints

2 \leq N \leq 10^5

-5000 \leq A_i \leq 5000 (1 \leq i \leq N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print N lines.
In the i-th line, print the total cost of travel during the trip when the visit to Spot i is canceled.

Sample Input 1

3
3 5 -1

Sample Output 1

12
8
10

Spot 1, 2 and 3 are at the points with coordinates 3, 5 and -1, respectively.
For each i, the course of the trip and the total cost of travel when the visit to Spot i is canceled, are as follows:

For i = 1, the course of the trip is 0 \rightarrow 5 \rightarrow -1 \rightarrow 0 and the total cost of travel is 5 + 6 + 1 = 12 yen.

For i = 2, the course of the trip is 0 \rightarrow 3 \rightarrow -1 \rightarrow 0 and the total cost of travel is 3 + 4 + 1 = 8 yen.

For i = 3, the course of the trip is 0 \rightarrow 3 \rightarrow 5 \rightarrow 0 and the total cost of travel is 3 + 2 + 5 = 10 yen.

Sample Input 2

5
1 1 1 2 0

Sample Output 2

4
4
4
2
4

Sample Input 3

6
-679 -2409 -3258 3095 -3291 -4462

Sample Output 3

21630
21630
19932
8924
21630
19288



Sample input:
3
3 5 -1


Sample output:
12
8
10

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.insert(0, 0)
a.append(0)
cost = [abs(x - y) for x, y in zip(a[:-1], a[1:])]
for i in range(n):
    print(sum(cost) - cost[i] - cost[i + 1] + abs(a[i] - a[i + 2]))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.insert(0,0)
a.append(0)
cost = [abs(x - y) for x ,y in zip(a[:-1], a[1:])]
for i in range(n):
    c = cost.copy()
    c.pop(i + 1)
    c.pop(i)
    c.append(abs(a[i] - a[i+2]))
    print(sum(c))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

N = int(input())

A = [int(x) for x in stdin.readline().rstrip().split()]

A.insert(0,0)

A.append(0)

for i in range(2,len(A)):
  B = A[:i-1]+A[i:]
  C = [abs(B[i+1]-B[i]) for i in range(len(B)-1)]
  print(sum(C))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

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

A.insert(0,0)

A.append(0)

for i in range(2,len(A)):
  B = A[:i-1]+A[i:]
  C = [abs(B[i+1]-B[i]) for i in range(len(B)-1)]
  print(sum(C))

Submission status: Time Limit Exceeded

Solution code:
import copy

N = int(input())

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

A.insert(0,0)

A.append(0)

for i in range(1,len(A)-1):
  B = copy.copy(A)
  del B[i]
  C = [abs(B[i+1]-B[i]) for i in range(len(B)-1)]
  print(sum(C))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.append(0)
a.insert(0, 0)
inter = []

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

for j in range(n):
    diff = inter[j]+inter[j+1]-abs(a[j]-a[j+2])
    ans = sum(inter)-diff
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.append(0)
a.insert(0, 0)
inter = []

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

for j in range(n):
    diff = inter[j]+inter[j+1]-abs(a[j]-a[j+2])
    ans = sum(inter)-diff
    print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N):
    num = 0
    pos = 0
    for ai, av in enumerate(A):
        if ai == i:
            continue
        num += abs(pos - av)
        pos = av
    num += abs(pos - 0)
    print(num)

Submission status: Time Limit Exceeded

Solution code:
import fractions
from functools import reduce
from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(_) for _ in stdin.readline().rstrip().split()]
for n in range(N):
    path = [0]+A[0:n]+A[n+1:]+[0]
    print(sum(abs(path[i]-path[i-1]) for i, _ in enumerate(path)))

Submission status: Time Limit Exceeded

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

a.append(0)
a.insert(0, 0)

for i in range(1, n+1):
    total = 0
    for j in range(1, n+2):
        if j == i + 1:
            total += abs(a[j] - a[j-2])
        elif j != i:
            total += abs(a[j] - a[j-1])
    print(total)

Submission status: Time Limit Exceeded

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

import numpy as np
a = np.array(a)

for i in range(n):
    a_new = np.delete(a, i)
    a_new = np.insert(a_new, 0, 0)
    a_new = np.append(a_new, 0)
    print(np.sum(np.abs(np.diff(a_new))))

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
ar = list(map(int,input().split(" ")))
for i in range(a):
    count= 0
    x = 0
    for j in range(a):
        if i != j:
            count += abs(x - ar[j])
            x = ar[j]
    count += abs(x - 0)
    print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
L = list(map(int,input().split()))
L.append(0)

for i in range(N) :
    T = list(L)
    T.pop(i)
    d = 0
    for j in range(N-1) :
        d += abs(T[j+1]-T[j])
    d += abs(T[0])
    print(d)

Submission status: Time Limit Exceeded

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

A.insert(0,0)
A.append(0)
dist = []
dist_warp = []

for i in range(1,N+2):
    dist.append(abs(A[i]-A[i-1]))
    if i != 1:
        dist_warp.append(abs(A[i]-A[i-2]))

for i in range(1,N+1):
    print(sum(dist)-dist[i-1]-dist[i]+dist_warp[i-1])

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
list_a = list(map(int, input().split()))
list_memo = []
for i in range(0,n):
    if i == 0:
        list_memo.append(list_a[i])
    else:
        list_memo.append(list_a[i]-list_a[i-1])
list_memo.append(-list_a[n-1])

for i in range(0,n):
    ans = 0
    for j in range(0,n+1):
        if j != i and j != i + 1:
            ans += abs(list_memo[j])
    ans += abs(list_memo[i+1] + list_memo[i])
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
L = []
A.insert(0,0)
A.insert(N+1,0)
for i in range(1,N+1):
  k = 0
  L.append(A[i])
  del (A[i])
  for j in range(N):
    k += abs(A[j]-A[j+1])
  A.insert(i,L[i-1])
  print(k)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def getinputdata():

    # 配列初期化
    array_result = []
    
    data = input()
    
    array_result.append(data.split(" "))

    flg = 1

    try:

        while flg:

            data = input()

            if(data != ""):
                
                array_result.append(data.split(" "))

            else:

                flg = 0
    finally:

        return array_result

arr_data = getinputdata()

n = int(arr_data[0][0])

arr=[int(x) for x in arr_data[1]]

for i in range(0,n):

    mysum=0
    before=0
    
    list01=list(arr[:i]+arr[i+1:])
    arr_temp=[]
    for i in range(0,len(list01)):
        
        arr_temp.append(abs(before-list01[i]))
    
        before=list01[i]
     
    arr_temp.append(abs(list01[i]))    
    print(sum(arr_temp))

Submission status: Time Limit Exceeded

Solution code:
def getinputdata():

    # 配列初期化
    array_result = []
    
    data = input()
    
    array_result.append(data.split(" "))

    flg = 1

    try:

        while flg:

            data = input()

            if(data != ""):
                
                array_result.append(data.split(" "))

            else:

                flg = 0
    finally:

        return array_result

arr_data = getinputdata()

n = int(arr_data[0][0])

arr=[int(x) for x in arr_data[1]]

#print(n,arr)

mysum=0

for i in range(0,n):

    mysum=0
    before=0
    
    list01=list(arr[:i]+arr[i+1:])
    
    for i in range(0,len(list01)):
        
        mysum+=abs(before-list01[i])
    
        before=list01[i]
        
    mysum+=abs(list01[i])    
    print(mysum)

Submission status: Time Limit Exceeded

Solution code:
def getinputdata():

    # 配列初期化
    array_result = []
    
    data = input()
    
    array_result.append(data.split(" "))

    flg = 1

    try:

        while flg:

            data = input()

            if(data != ""):
                
                array_result.append(data.split(" "))

            else:

                flg = 0
    finally:

        return array_result

arr_data = getinputdata()

n = int(arr_data[0][0])

arr=[int(x) for x in arr_data[1]]

#print(n,arr)

mysum=0

for i in range(0,n):

    mysum=0
    before=0
    
    for v in arr[:i]+arr[i+1:]:
        
        mysum+=abs(before-v)
        
        before=v
    
    mysum+=abs(before)
        
    print(mysum)

Submission status: Time Limit Exceeded

Solution code:
def solve(n, xs, k):
    ss = [0] + [abs(a - b) for (a, b) in zip(xs + [0], [0] + xs)]
    for i in range(1, n + 1):
        ss[i + 1] += ss[i]
    cost2 = [abs(a - b) for (a, b) in zip(xs[1:] + [0], [0] + xs[:n - 1])]
    return ss[n + 1] - ss[k + 2] + ss[k] + cost2[k]

if __name__ == "__main__":
    n = int(input())
    xs = [int(s) for s in input().split()]
    for i in range(n):
        print(solve(n, xs, i))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())

spot = np.array(list(map(int, input().split())))

for i in range(N):
    temp = np.zeros(N)
    temp[1:] = np.delete(spot, i)
    print(int(np.abs(np.roll(temp, 1) - temp).sum()))

Submission status: Time Limit Exceeded

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



for i in range(N):
    now = 0
    money = 0
    for j in range(0,i):
        money = money + abs(A[j]-now)
        now = A[j]
    for j in range(i+1,N):
        money = money + abs(A[j]-now)
        now = A[j]
    money = money + abs(now)
    print(money)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
lis = [0] + list(map(int, input().split())) + [0]
tmp = []
for j in range(len(lis)-1):
    tmp.append(lis[j+1] - lis[j])
for i in range(len(tmp)-1):
    a = tmp[:i] + [tmp[i]+tmp[i+1]] + tmp[i+2:]
    print(sum(map(abs,a)))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
lis = [0] + list(map(int, input().split())) + [0]
def abs(m):
  if m>0:
    return m
  return(-m)
tmp = []
for j in range(len(lis)-1):
    tmp.append(lis[j+1] - lis[j])
for i in range(len(tmp)-1):
    a = tmp[:i] + [tmp[i]+tmp[i+1]] + tmp[i+2:]
    print(sum(map(abs,a)))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
lis = list(map(int, input().split()))
def abs(m):
  if m>0:
    return m
  return(-m)
def mysum(data):
  ans = 0
  data = [0] + data+[0]
  for j in range(len(data)-1):
    ans += abs(data[j] - data[j+1])
  return(ans)
for i in range(len(lis)):
  tmp = lis[:i] + lis[i+1:]
  print(mysum(tmp))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
nowp = 0
cost = []
for i in range(N):
  cost.append(0)
  for j in range(N):
    if i != j:
      cost[i] += abs(nowp - A[j])
      nowp = A[j]
  cost[i] += abs(nowp)
  nowp = 0

for i in range(N):
  print('%d ' %cost[i])

Submission status: Time Limit Exceeded

Solution code:
_ = int(input())
li = list(map(int, input().split()))
def get_price(a,b):
    if a>=b: return a-b
    else: return b-a
def delete_one_location(li,i):
    li.pop(i)
    return li
def get_sum_price(li, del_i):
    ali = li[:]
    ali = delete_one_location(ali, del_i)
    sp = 0
    for i in range(len(ali)):
        if i == 0:
            sp += get_price(0, ali[i])
        if i == len(ali)-1:
            sp += get_price(ali[i], 0)
        else:
            sp += get_price(ali[i], ali[i+1])
    return sp
def simulate_all(li):
    ssp = []
    for i in range(len(li)):
        print(get_sum_price(li, i))
simulate_all(li)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    A = list([0])
    A.extend(a)
    A.append(0)
    
    sum = 0
    for i in range(n):
        root = list(A[:])
        del root[i+1]
        for j in range(1, n+1):
            sum += abs(root[j]-root[j-1])
        print(sum)
        sum = 0

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    A = list([0])
    for j in range(n):
        A.append(a[j])
    A.append(0)
    
    sum = 0
    for i in range(n):
        root = list(A[:])
        del root[i+1]
        for j in range(1, n+1):
            sum += abs(root[j]-root[j-1])
        print(sum)
        sum = 0

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    #print(a)

    sum = 0
    for i in range(n):
        root = list([0])
        for j in range(n):
            if j!=i:
                root.append(a[j])
        root.append(0)
        #print(root)
        for j in range(1, n+1):
            sum += abs(root[j]-root[j-1])
        print(sum)
        sum = 0

Submission status: Time Limit Exceeded

Solution code:
import copy

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

    sum = 0
    for i in range(n):
        root = list([0])
        for j in range(n):
            if j!=i:
                root.append(a[j])
        root.append(0)
        #print(root)
        for j in range(1, n+1):
            sum += abs(root[j]-root[j-1])
        print(sum)
        sum = 0

Submission status: Time Limit Exceeded

Solution code:
def traveling_plan(A,I):
    sum = 0
    x = 0
    for a in A:
        if not a == I:
            sum += abs(a - x)
            x = a
    sum += abs(x)
    
    return sum


if __name__ == '__main__':

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

    for a in A:
        sum = traveling_plan(A,a)
        print(sum)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

N = int(sys.stdin.readline())

A = sys.stdin.readline().split(' ')


for rem in range(len(A)):
	pay = 0
	B = A[:]
	del(B[rem])

	for i in range(len(B)):

		a = int(B[i])
		s = int(B[i - 1]) if i > 0 else 0

		l = a - s
		pay += l if l > 0 else -l

	lastPoint = int(B[len(B) - 1])
	pay += lastPoint if lastPoint > 0 else -lastPoint

	print(pay)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

N = int(sys.stdin.readline())

A = sys.stdin.readline().split(' ')

for rem in range(len(A)):
	pay = 0
	B = []
	for i in range(len(A)):
		if i != rem:
			B.append(int(A[i]))

	for i in range(len(B)):

		a = B[i]
		s = B[i - 1] if i > 0 else 0

		l = a - s
		pay += l if l > 0 else -l

	lastPoint = B[len(B) - 1]
	pay += lastPoint if lastPoint > 0 else -lastPoint

	print(pay)

Submission status: Time Limit Exceeded

Solution code:
def calc_dist(lst):
    s = 0
    lst.insert(0, 0)
    lst.append(0)
    for i in range(len(lst) - 1):
        s += abs(lst[i] - lst[i+1])
    return s

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

for i in range(N):
    lst = A.copy()
    lst.pop(i)
    s = calc_dist(lst)
    print(s)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# get a integer
num = int(raw_input())
# get two integers separated with half-width break
array = map(int, raw_input().split())
# get a string
#s = raw_input()

for i in range(0,num):
    state = 0
    way = 0
    for j in range(0,num):
        if i != j:
            way += abs(array[j] - state)
            state = array[j]
    way += abs(state)
    print(way)

Submission status: Time Limit Exceeded

Solution code:
#coding: utf-8
N = int(input())
A= list(map(int,input().split()))
A = [0] + A + [0]
Adiff = list(range(N+1))
def diff(X):
    diff_x = 0
    for i in range(len(X)-1):
        diff_x += abs(X[i] - X[i+1])
    return diff_x
for i in range(N):
    B = A[:]
    del B[i+1]
    diffB = diff(B)
    print(diffB)

Submission status: Time Limit Exceeded

Solution code:
#coding: utf-8
N = int(input())
A= list(map(int,input().split()))
A = [0] + A + [0]
Adiff = list(range(N+1))
def diff(X):
    diff_x = list(range(len(X)-1))
    for i in range(len(X)-1):
        diff_x[i] = abs(X[i] - X[i+1])
    return diff_x
for i in range(N):
    B = A[:]
    del B[i+1]
    diffB = diff(B)
    SUM = 0
    for j in range(N):
        SUM += diffB[j]
    print(SUM)

Submission status: Time Limit Exceeded

Solution code:
import copy

N = int(input())
As = list(map(int, input().split()))
As = [0] + As + [0]


def dist(a, b):
    return abs(a - b)


dAs = [dist(As[i], As[i+1]) for i in range(N+1)]


def rem(i):
    ret = copy.deepcopy(dAs)
    print('copy', ret)
    ret[i] = dist(As[max(0, i-1)], As[i+1])
    print('rett', ret, i)
    ret = ret[:max(0, i-1)] + ret[i:]
    print('As', As)
    print('ret', ret)
    return ret

def calc(i):
    return sum(dAs) - dist(As[i], As[i-1]) - dist(As[i+1], As[i]) + dist(As[i+1], As[i-1])


for i in range(1, N+1):
    print(calc(i))

Submission status: Time Limit Exceeded

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

for i in range(len(lis)):
    sum_num = 0
    before = 0
    lislis = [0] + lis[0:i] + lis[i + 1:len(lis)] + [0]
    for index, j in enumerate(lislis):
        if index + 1 < len(lislis):
            # print('hello')
            # print(abs(j - lislis[index + 1]))
            sum_num += abs(j - lislis[index + 1])
    print(sum_num)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(lambda x: int(x), input().split(' ')))
for i in range(n):
  a_ = [0] + a[0:i] + a[i+1:] + [0]
  s = 0
  for j in range(len(a_) - 1):
    s += abs(a_[j + 1] - a_[j])
  print(s)

Submission status: Time Limit Exceeded

Solution code:
def run(lst, i):
    lst = [0]+lst+[0]
    cost = 0
    a = lst[:i]+lst[i+1:]
    for a,b in zip(a,a[1:]):
        cost+=abs(b-a)
    return cost

N = int(input())
l = list(map(int,input().split()))
for i in range(1,N+1):
    print(run(l,i))

Submission status: Accepted

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

A_list.insert(0, 0)
A_list.append(0)
A_list_abs = []
A_sum = 0

for i in range(1, N+2):
    temp = abs(A_list[i] - A_list[i-1])
    A_list_abs.append(temp)
    A_sum += temp
#print(A_list_abs, A_sum)

for i in range(N):
    if (A_list[i] <= A_list[i+1] <= A_list[i+2]) or (A_list[i] >= A_list[i+1] >= A_list[i+2]):
        print(A_sum)
    else:
        print(A_sum - A_list_abs[i] - A_list_abs[i+1] + abs(A_list_abs[i] - A_list_abs[i+1]))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return float(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()


def main():
	n=II()
	A=[0]+LI()+[0]
	total=0


	for i,j in zip(A[1:],A[:-1]):
		total+=abs(i-j)


	for i in range(1,n+1):
		print(total-abs(A[i]-A[i-1])-abs(A[i+1]-A[i])+abs(A[i+1]-A[i-1]))











if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
tot = abs(A[0])
for i in range(1,N):
    tot += abs(A[i]-A[i-1])
tot += abs(A[N-1])
for i in range(N):
    if i==0:
        cnt = tot-(abs(A[0])+abs(A[1]-A[0]))
        cnt += abs(A[1])
    elif i==N-1:
        cnt = tot-(abs(A[N-1]-A[N-2])+abs(A[N-1]))
        cnt += abs(A[N-2])
    else:
        cnt = tot-(abs(A[i]-A[i-1])+abs(A[i+1]-A[i]))
        cnt += abs(A[i+1]-A[i-1])
    print(cnt)

Submission status: Accepted

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

for i in range(1, n+1):
    print(sum_cost - (abs(al[i]-al[i+1])+abs(al[i]-al[i-1])) + abs(al[i-1]-al[i+1]))

Submission status: Accepted

Solution code:
from itertools import accumulate


N, *A = map(int, open(0).read().split())

A = [0] + A + [0]
P = list(accumulate([abs(A[i] - A[i + 1]) for i in range(N + 1)], initial=0))

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
aa = [0] + a + [0]
d = [0]*(n+1)
for i in range(n+1):
  d[i] = abs(aa[i]-aa[i+1])
s = sum(d)
for i in range(n):
  print(s - d[i] - d[i+1] + abs(aa[i+2]-aa[i]))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
res = 0
pre = 0
for a in A:
    res += abs(a-pre)
    pre = a
else:
    res += abs(0-pre)

#print(res)
ans = [0]*n
A = [0]+A+[0]
for i in range(n):
    j = i+1
    temp = res-abs(A[j-1]-A[j])-abs(A[j+1]-A[j])+abs(A[j+1]-A[j-1])
    ans[i] = temp
print(*ans, sep='\n')

Submission status: Accepted

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

def main():
    n = int(input())
    a = [0] + list(map(int, input().split())) + [0]
    dist = [abs(a[i] - a[i - 1]) for i in range(1, n + 2)]
    total = sum(dist)
    ans = [0] * n
    for i in range(n):
        ans[i] = total - dist[i] - dist[i + 1] + abs(a[i] - a[i + 2])
    print(*ans, sep='\n')


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
al = [0] + list(map(int, input().split())) + [0]
bl = [0]*(n+1)
for i in range(n+1):
    bl[i] = abs(al[i] - al[i+1])
cost = sum(bl)

for i in range(n):
    print(cost - sum(bl[i:i+2]) + abs(al[i] - al[i+2]))

Submission status: Accepted

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

all = 0

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

for i in range(1, n+1):
	dist = all - abs(a[i-1] - a[i]) - abs(a[i] - a[i+1]) + abs(a[i-1] - a[i+1])
	print(dist)

Submission status: Accepted

Solution code:
n = int(input())

A = [0]+list(map(int,input().split()))+[0]

fee = []
for i in range(1,n+2):
    fee.append(abs(A[i]-A[i-1]))

a = sum(fee)

for i in range(n):
    print(a-fee[i]-fee[i+1]+abs(A[i]-A[i+2]))

Submission status: Accepted

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

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

for i in range(1,n+1):
    print(total + abs(a[i-1] - a[i + 1]) - abs(a[i - 1] - a[i]) - abs(a[i] - a[i + 1]))

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
A=[0]+A+[0]
ans=0
LC=[0]*(n+2)
for i in range(1,n+1):
    ans+= abs(A[i]-A[i-1])
    if A[i-1]<A[i] and A[i]>A[i+1]:
        LC[i]= 2*( A[i]- max(A[i-1],A[i+1]) )
    if A[i-1]>A[i] and A[i]<A[i+1]:
        LC[i]= -2*(A[i]- min(A[i-1],A[i+1])) 
ans+= abs(A[n])
for i in range(n):
    print(ans-LC[i+1])
#print(ans,LC)

Submission status: Accepted

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

for i in range(N):
    if i == 0:
        before = 0
        after = A[i+1]
    elif i == N-1:
        before = A[i-1]
        after = 0
    else:
        before = A[i-1]
        after = A[i+1]

    if before <= A[i] <= after or \
        after <= A[i] <= before:
        print(no_change)
    else:
        c = no_change - min(abs(A[i]-before),abs(A[i]-after))*2
        print(c)

Submission status: Accepted

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

td = cnt + abs(a[0]) + abs(a[-1])

for i in range(1,n-1):
  x = abs(a[i-1]-a[i]) + abs(a[i]-a[i+1]) - abs(a[i+1] -a[i-1])
  arr[i] = x
arr[0] = abs(a[0]) + abs(a[0] - a[1]) - abs(a[1])
arr[-1] = abs(a[n-2] - a[n-1]) + abs(a[n-1]) - abs(a[n-2])
            
ans = [td - x  for x in arr]

for i in range(n):
  print(ans[i])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush
from math import *

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x):
        self.setdefault(x,0)
        self[x] += 1

N,A = ilint()
A = [0]+A[:]+[0]
B = [abs(A[i+1]-A[i]) for i in range(N+1)]
ans = sum(B)
for i in range(N):
    tmp = ans-B[i]-B[i+1]+abs(A[i+2]-A[i])
    print(tmp)

Submission status: Accepted

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

A.insert(0, 0)
A.append(0)

money = 0

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

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

Submission status: Accepted

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

money = 0

last = 0
for a in A:
    money += abs(last - a)
    last = a
money += abs(last)

for i in range(N):
    if i == 0:
        t1 = A[i]
        t2 = A[i + 1]
        if int(t1 >= 0) ^ int(t2 >= 0):
            print(money - abs(t1) * 2)
        elif abs(t1) > abs(t2):
            print(money - abs(t1 - t2) * 2)
        else:
            print(money)

    elif i == N - 1:
        t1 = A[i]
        t2 = A[i - 1]
        if int(t1 >= 0) ^ int(t2 >= 0):
            print(money - abs(t1) * 2)
        elif abs(t1) > abs(t2):
            print(money - abs(t1 - t2) * 2)
        else:
            print(money)

    else:
        a0 = A[i - 1]
        a1 = A[i]
        a2 = A[i + 1]
        if (a0 <= a1 and a1 <= a2) or (a0 >= a1 and a1 >= a2):
            print(money)
        elif (a0 <= a2 and a2 <= a1) or (a0 >= a2 and a2 >= a1):
            print(money - abs(a2 - a1) * 2)
        else:
            print(money - abs(a1 - a0) * 2)

Submission status: Accepted

Solution code:
n=int(input())
x=[0]
x.extend(list(map(int, input().split())))
x.extend([0])
cst=0
#print(x)
for i in range(1,n+2):
  cst+=abs(x[i]-x[i-1])
for i in range(1,n+1):
  print(cst-abs(x[i]-x[i-1])-abs(x[i]-x[i+1])+abs(x[i-1]-x[i+1]))

Submission status: Accepted

Solution code:
#53 C - Traveling Plan TLE
from collections import deque 
N = int(input())
A = deque(map(int,input().split()))
A.appendleft(0)
A.append(0)
A = list(A)

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

ans = []
for j in range(1,N+1):
    s = S -(abs(A[j]-A[j-1]) + abs(A[j+1]-A[j])) + abs(A[j+1]-A[j-1])
    ans.append(s)

for a in ans:
    print(a)

Submission status: Accepted

Solution code:
N = int(input())
Alist = [0] + list(map(int,input().split()))
Sum = 0
for i in range(1,len(Alist)):
    Sum += abs(Alist[i] - Alist[i-1])
Sum += abs(Alist[-1])
Answer= []
for i in range(1,len(Alist)-1):
    Ans = Sum
    Ans = Ans - abs(Alist[i]-Alist[i-1]) - abs(Alist[i] - Alist[i+1]) + abs(Alist[i+1] - Alist[i-1])
    Answer.append(Ans)
Ans = Sum
Ans = Ans - abs(Alist[-1]) - abs(Alist[-1]-Alist[-2]) + abs(Alist[-2])
Answer.append(Ans)
for a in Answer:
    print(a)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
# tobitobi[i] -> distance [i - 1] to [i + 1]
tobitobi = [0] * n
for i in range(1, n - 1):
    tobitobi[i] = abs(a[i - 1] - a[i + 1])
sum_of_dis = abs(a[0]) + abs(a[n - 1])
for i in range(1, n):
    sum_of_dis += abs(a[i] - a[i - 1])
ans = [0] * (n)
for i in range(n):
    if i == 0:
        ans[i] = sum_of_dis - abs(a[i]) - abs(a[i] - a[i + 1]) + abs(a[i + 1])
    elif i == n - 1:
        ans[i] = sum_of_dis - abs(a[i]) - abs(a[i] - a[i - 1]) + abs(a[i - 1])
    else:
        ans[i] = sum_of_dis - (abs(a[i - 1] - a[i]) + abs(a[i] - a[i + 1])) + tobitobi[i]
for ANS in ans:
    print(ANS)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [0] + list(map(int, input().split())) + [0]

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

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = [0] + list(map(int,input().split())) + [0]
ans = 0
anslist = [0] * n
for j in range(1, n + 2):
    if j == 1:
        ans += abs(a[j])
    elif j == n + 1:
        ans += abs(a[n])
    else:
        ans += abs(a[j]-a[j - 1])

for i in range(1, n + 1):
    anslist[i - 1] = ans - abs(a[i - 1] - a[i]) - abs(a[i] - a[i + 1]) + abs(a[i - 1] - a[i + 1])
            
for k in anslist:
    print(k)

Submission status: Accepted

Solution code:
N = int(input())
A = [0]
A = A + list(map(int,input().split()))
A.append(0)
SA = 0
for i in range(N+1):
    SA += abs(A[i]-A[i+1])
for i in range(1,N+1):
    X = abs(A[i-1]-A[i])+abs(A[i+1]-A[i])
    Y = abs(A[i-1]-A[i+1])
    print(SA-X+Y)