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: Accepted

Solution code:
def is_a_mid(a, b, c):
    return (b <= a <= c) or (c <= a <= b)


def is_b_mid(a, b, c):
    return (a <= b <= c) or (c <= b <= a)


def solve(n, a):
    total = 0
    ans = []

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

    for i, j, k in zip(a, a[1:], a[2:]):
        if is_a_mid(i, j, k):
            ans.append(total-2*(abs(j-i)))
        elif is_b_mid(i, j, k):
            ans.append(total)
        else:
            ans.append(total+abs(i-k) - abs(i-j) - abs(k-j))

    return ans


if __name__ == '__main__':
    n = int(input())
    a = [0] + list(map(int, input().split()))+[0]
    for i in solve(n, a):
        print(i)

Submission status: Accepted

Solution code:
from itertools import accumulate

N = int(input())
A = list(map(int, input().split()))
A.append(0)
buff = [abs(A[0])]
buff2 = [abs(A[1])]


for i in range(2, N+1):
    buff2.append(abs(A[i]-A[i-2]))
# print('buff2')
# print(buff2)
# print('')
for i in range(1, N+1):
    buff.append(abs(A[i]-A[i-1]))

# print(buff)
mm = sum(buff)
for i in range(N):
    print(mm-buff[i]-buff[i+1]+buff2[i])

# acc = list(accumulate(buff))
# print(acc)0pp0

Submission status: Accepted

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

dat = [0] + dat + [0]
total = 0
cur = 0
for i in range(n + 2):
    total += abs(cur - dat[i])
    cur = dat[i]
total += abs(cur)
cur = 0
for i in range(n):
    j = i + 1
    if dat[j-1] < dat[j] < dat[j+1]:
        # 間に挟まれているパターン１
        #print("pat1")
        print(total)
    elif dat[j-1] > dat[j] > dat[j+1]:
        # 間に挟まれているパターン2
        #print("pat2")
        print(total)
    else:
        # この場合は間に挟まれていない
        #print("{0} {1} {2}".format(total, abs(dat[j-1] -dat[j]) , abs(dat[j-1] -dat[j+1])))
        print(total - abs(dat[j-1] -dat[j]) - abs(dat[j] -dat[j+1]) + abs(dat[j-1] - dat[j+1]))

Submission status: Accepted

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

Submission status: Accepted

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

if N == 2:
    print(2*abs(a[1]-0))
    print(2*abs(a[0]-0))
else:
    lto = [0]*(N+1)
    lflo = [0]*(N+1)
    lto[0] = abs(a[0]-0)
    for i in range(1,N):lto[i] = abs(a[i] - a[i-1]) + lto[i-1]

    lflo[N] = abs(a[N-1]-0)
    for j in range(N-1,0,-1):lflo[j] = abs(a[j]-a[j-1]) + lflo[j+1]
    for i in range(N):
        t = f = con = 0
        if i==0:
            t = abs(a[1]-0)
            f = lflo[i+2]
        elif i == N-1:
            t = lto[i-1]
            f = abs(a[N-2] - 0)
        else:
            t = lto[i-1]
            f = lflo[i+2]
            con = abs(a[i+1]-a[i-1])
        print(t+con+f)

Submission status: Accepted

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

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

T = [0] * N
for i in range(N):
    T[i] = abs(A[i + 2] - A[i])

cost = sum(S)
for i in range(N):
    print(cost - S[i] - S[i + 1] + T[i])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import numpy as np
import statistics
from statistics import mean, median,variance,stdev
import math

def main():
	N = int(input())
	A = input().split(" ")
	A = [int(s) for s in A]
	
	base = 0
	for i, val in enumerate(A):
		if i == 0:
			tmp = abs(0 - A[i])
		else:
			tmp = abs(A[i-1] - A[i])
		base += tmp
		
	base += abs(A[-1] - 0)
		
	for i, val in enumerate(A):
		tmp = 0
		if i == 0:
			tmp = base + abs(0 - A[i+1]) - (abs(0 - A[i]) + abs(A[i] - A[i+1]))
		elif i == N-1:
			tmp = base + abs(A[i-1] - 0) - (abs(A[i-1] - A[i]) + abs(A[i] - 0))
		else:
			tmp = base + abs(A[i-1] - A[i+1]) - (abs(A[i-1] - A[i]) + abs(A[i] - A[i+1]))
			
		print(tmp)
	
if __name__ == "__main__":
	main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
l=[0]+list(map(int,input().split()))+[0]
ans=sum(abs(l[i+1]-l[i]) for i in range(n+1))
for i in range(1,n+1):
  if l[i-1]<=l[i]<=l[i+1] or l[i-1]>=l[i]>=l[i+1]:
    print(ans)
  else:
    print(ans-2*min(abs(l[i-1]-l[i]),abs(l[i+1]-l[i])))

Submission status: Accepted

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

B = [0 for i in range(n+1)]
for i in range(n+1):
    B[i] = abs(A[i+1] - A[i])
su = sum(B)
for i in range(1,n+1):
    print(su-B[i-1]-B[i]+abs(A[i+1]-A[i-1]))

Submission status: Accepted

Solution code:
N = int(input())
# N < 10 ^ 5
A = [0] + [int(x) for x in input().split()] + [0]
# cost = | a - b |

c = 0 
for i in range(0, N+2):
    c += abs(A[i] - A[i-1])
   
# i, i+1, i+2
for i in range(0, N):
    a1, a2, a3 = A[i], A[i+1], A[i+2]
    d = 0
    if   a1 <= a2 <= a3:
        d = 0
    elif a1 <= a3 <= a2: # 1 3 2
        d = abs(a2 - a3)*2
    elif a2 <= a1 <= a3:
        d = abs(a1 - a2)*2
    elif a2 <= a3 <= a1:
        d = abs(a2 - a3)*2
    elif a3 <= a1 <= a2:
        d = abs(a1 - a2)*2
    elif a3 <= a2 <= a1:
        d = 0
    print(c-d)

Submission status: Accepted

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

for i in range(1, n+1):
    ans = maxAns
    if (min(a[i-1], a[i], a[i+1]) == a[i] or max(a[i-1], a[i], a[i+1]) == a[i]):
        ans -= cost[i-1] + cost[i] - abs(cost[i-1] - cost[i])
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
l = [0]+l+[0]
sum_l = 0
s = 0
for i in range(n+2):
    g = l[i]
    sum_l += abs(g-s)
    s = g
for i in range(1,n+1):
    ans = sum_l +abs(l[i+1]-l[i-1])-(abs(l[i]-l[i-1])+abs(l[i+1]-l[i]))
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

sm = 0

for i in range(n-1):
    sm += abs(li[i] - li[i+1])
else:
    sm += abs(li[0])
    sm += abs(li[n-1])
    
for i in range(n):
    if i == 0:
        print(sm - abs(li[0]) + abs(li[1]) - abs(li[i] - li[i+1]))
    elif i == n-1:
        print(sm - abs(li[n-1]) + abs(li[n-2]) - abs(li[n-2] - li[n-1]))
    else:
        print(sm - abs(li[i] - li[i+1]) -abs(li[i-1] - li[i]) + abs(li[i-1] - li[i+1]))

Submission status: Accepted

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

a=[0]
for i in range(n):
    a.append(b[i])
a.append(0)

minus=[0]*n
ans=abs(a[1]-a[0])
for i in range(1,n+1):
    ans+=abs(a[i+1]-a[i])
    if (a[i-1]<=a[i] and a[i]<=a[i+1]) or (a[i-1]>=a[i] and a[i]>= a[i+1]):
        continue
    else:
        minus[i-1]=2*min(abs(a[i]-a[i-1]), abs(a[i]-a[i+1]))
for i in range(n):
    print(ans-minus[i])

Submission status: Accepted

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

sum_=sum([abs(A[i+1]-A[i]) for i in range(N+1)])

for i in range(N):
    if (A[i+1]-A[i]>0 and A[i+2]-A[i+1]<0) or (A[i+1]-A[i]<0 and A[i+2]-A[i+1]>0):
        ans=2*min(abs(A[i+1]-A[i]),abs(A[i+2]-A[i+1]))
        print(sum_-ans)
    else:
        print(sum_)

Submission status: Accepted

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

A = [0] + A + [0]

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


for i in range(1, N+1):
    print(s + 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 = [0]        # 始点
totalcost = 0
now = 0

# トータルコストを求める
for i in input().split():
    nextstep = int(i)
    a_list.append(nextstep)
    totalcost += abs(nextstep - now)
    now = nextstep
totalcost += abs(now - 0)
a_list.append(0)    # 終点

for i in range(1, n + 1):
    # 各点を除いた経路を前後の位置関係から計算する
    bef = a_list[i - 1]
    now = a_list[i]
    aft = a_list[i + 1]

    removecost = 0

    if bef < now < aft or aft < now < bef:
        # 除く点が中央にある場合は、いずれにせよ通る
        pass
    elif bef < now and aft < now and aft <= bef:
        removecost = abs(now - bef) * 2
    elif bef < now and aft < now and bef <= aft:
        removecost = abs(now - aft) * 2
    elif now < bef and now < aft and bef <= aft:
        removecost = abs(now - bef) * 2
    elif now < bef and now < aft and aft <= bef:
        removecost = abs(now - aft) * 2
    print(totalcost - removecost)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

N =  int(input())

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

A = [0]
for i in a:
    A.append(i)

A.append(0)


mon = 0

for i in range(len(A)-1):
    mon += max( A[i+1] - A[i] , A[i] - A[i+1])

for i in range(N):
    i += 1

    now = mon - ( max( A[i+1] - A[i] , A[i] - A[i+1]) + max( A[i-1] - A[i] , A[i] - A[i-1]) ) + max( A[i+1] - A[i-1] , A[i-1] - A[i+1])

    print (now)

Submission status: Accepted

Solution code:
from functools import reduce
import math

def main():
    # 文字列の2進数を数値にする
    # '101' → '5'
    # 文字列の頭に'0b'をつけてint()にわたす
    # binary = int('0b'+'101',0)

    # 2進数で立っているbitを数える
    # 101(0x5) → 2
    # cnt_bit = bin(5).count('1')
    
    # N! を求める
    # f = math.factorial(N)

    # N の逆元
    # N_inv = pow(N,MOD-2,MOD)

    # nCr
    # Nの階乗 * rの階乗の逆元 * n-rの階乗の逆元
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    INF = float("inf")

    # 大きな素数
    MOD = 10**9+7
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    N = int(input())
    A = list(int(_) for _ in input().split())
    A = [0] + A
    A.append(0)
    ans = []
    A_sum = 0
    for i in range(len(A)):
        A_sum+=abs(A[i]-A[i-1])
    for i in range(1,N+1):
        tmp = A_sum - abs(A[i]-A[i-1]) - abs(A[i+1]-A[i]) + abs(A[i+1]-A[i-1])
        ans.append(tmp)
    print('\n'.join(str(_) for _ in ans))
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(1, N + 1):
  if (A[i - 1] <= A[i] and A[i] <= A[i + 1]) or (A[i - 1] >= A[i] and A[i] >= A[i + 1]):
    print(all_dist)
  else:
    print(all_dist - abs(A[i] - A[i - 1]) - abs(A[i + 1] - A[i]) + abs(A[i + 1] - A[i - 1]))

Submission status: Accepted

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

Submission status: Accepted

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


for i in range(1, n+1):
    ans = d_all - abs(lst[i-1] - lst[i]) - abs(lst[i] - lst[i+1]) + abs(lst[i-1] - lst[i+1])
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())

A = [0]
for i in input().split():
  A.append(int(i))
A.append(0)

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

for i in range(N):
  a1 = A[i]
  a2 = A[i+1]
  a3 = A[i+2]
  if (a1 <= a2 <= a3) or (a3 <= a2 <= a1):
    print(S)
  elif (a1 <= a3 <= a2) or (a2 <= a3 <= a1):
    print(S - 2 * abs(a3 - a2))
  else:
    print(S - 2 * abs(a1 - a2))

Submission status: Accepted

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

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

for j in range(N):
    print(money-abs(A[j+1]-A[j])-abs(A[j+2]-A[j+1])+abs(A[j+2]-A[j]))

Submission status: Accepted

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
a.insert(0,0),a.append(0)
k,sum = [],0
for i in range(1,len(a)):
    k.append(abs(a[i-1]-a[i]))
    sum += abs(a[i-1]-a[i])
#print(k,sum)
for i in range(1,N+1):
    if a[i-1]>=a[i]>=a[i+1] or a[i-1]<=a[i]<=a[i+1]:
        print(sum)
    else:
        print(sum-(k[i-1]+k[i])+abs(a[i-1]-a[i+1]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

for i in range(1,n+1):
    print(cost + 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())
xs = list(map(int, input().split()))
xs = [0] + xs + [0]
dist = {}

total = 0

for i in range(n+1):
  dist[(i, i+1)] = abs(xs[i+1] - xs[i])
  total += dist[(i, i+1)]

for i in range(n):
  dist[(i, i+2)] = abs(xs[i+2] - xs[i])

for i in range(1, n+1):
  d = total - dist[(i-1, i)] - dist[(i, i+1)] + dist[(i-1, i+1)]
  print(d)

Submission status: Accepted

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

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

A_total = sum(A_n)
A_added = [0] + A + [0]
for i in range(1, N + 1):
    if A_added[i + 1] >= A_added[i] and A_added[i] >= A_added[i - 1]:
        print(A_total)
    elif A_added[i + 1] >= A_added[i - 1] and A_added[i - 1] >= A_added[i]:
        print(A_total - 2 * abs(A_added[i] - A_added[i - 1]))
    elif A_added[i] >= A_added[i + 1] and A_added[i + 1] >= A_added[i - 1]:
        print(A_total - 2 * abs(A_added[i + 1] - A_added[i]))
    elif A_added[i] >= A_added[i - 1] and A_added[i - 1] >= A_added[i + 1]:
        print(A_total - 2 * abs(A_added[i] - A_added[i - 1]))
    elif A_added[i - 1] >= A_added[i] and A_added[i] >= A_added[i + 1]:
        print(A_total)
    elif A_added[i - 1] >= A_added[i + 1] and A_added[i + 1] >= A_added[i]:
        print(A_total - 2 * abs(A_added[i] - A_added[i + 1]))
    else:
        print(0)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
A = deque(map(int, input().split()))
A.appendleft(0)
A.append(0)

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

Submission status: Accepted

Solution code:
# ARC093C - Traveling Plan
N = int(input())
A = [0] + list(map(int, input().rstrip().split())) + [0]
S = 0
for i in range(1, N + 2):
    S += abs(A[i - 1] - A[i])

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
str = input().split()
A = []
A.append(0)
for i in range(N):
    A.append(int(str[i]))
A.append(0)
SUM = 0
distance = []

for i in range(1,N+2):
    distance.append(abs(A[i] - A[i-1]))
    SUM = SUM + abs(A[i]-A[i-1])

for i in range(1,N+1):
    print(SUM - distance[i] - distance[i-1] + abs(A[i+1] - A[i-1]))

Submission status: Accepted

Solution code:
n=int(input())
a=[0]+[int(i) for i in input().split()]+[0]
cost=[abs(a[i+1]-a[i]) for i in range(n+1)]
C=sum(cost)
#print(cost)
for i in range(1,n+1):
    Co=C-(abs(a[i+1]-a[i]))-abs((a[i]-a[i-1]))+abs(a[i+1]-a[i-1])
    print(Co)

Submission status: Accepted

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

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

for i in range(1,N+1):
  if A[i-1]<=A[i]<=A[i+1] or A[i-1]>=A[i]>=A[i+1]:
    print(cost)
  else:
    print(cost-min(abs(A[i-1]-A[i]), abs(A[i+1]-A[i]))*2)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  l=[0]+l+[0]

  sm=sum([abs(l[i+1]-l[i]) for i in range(len(l)-1)])

  for i in range(1,len(l)-1):
    a=abs(l[i]-l[i-1])
    b=abs(l[i+1]-l[i])
    c=abs(l[i+1]-l[i-1])
    print(sm-(a+b-c))

main()

Submission status: Accepted

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

Submission status: Accepted

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

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

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


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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
now = 0
ans = []

A.append(0)
A.insert(0,0)
for i in range(len(A)-1):
    ans.append(abs(A[i+1]-A[i]))

sm = sum(ans)

for j in range(1,len(A)-1):
    print(sm-ans[j]-ans[j-1]+abs(A[j+1]-A[j-1]))

Submission status: Accepted

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

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

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


for i in range(1, N+1):
    if a[i-1] <= a[i] <= a[i+1] or a[i-1] >= a[i] >= a[i+1]:
        print(all_sum)
    elif a[i-1] <= a[i]  and a[i-1] >=  a[i+1]:
        #増減
        print(all_sum - abs(a[i] - a[i-1]) * 2)
    elif  a[i-1] <= a[i]  and a[i-1] <= a[i+1]:
        #増減
        print(all_sum - abs(a[i] - a[i+1]) * 2)
    elif a[i-1] >= a[i]  and a[i-1] >=  a[i+1]:
        #減増
        print(all_sum - abs(a[i] - a[i+1]) * 2)
    elif a[i-1] >= a[i]  and a[i-1] <=  a[i+1]:
        #減増
        print(all_sum - abs(a[i] - a[i-1]) * 2)
    else:
        print(i, "yukkuri")

Submission status: Accepted

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

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

base = sum([abs(i) for i in gap])

for j in range(n):
  answer = base - (abs(gap[j]) + abs(gap[j + 1])) + abs(gap[j] + gap[j + 1])
  print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=I()
  _a=LI()

  sm=0
  b=0
  for x in _a+[0]:
    sm+=abs(b-x)
    b=x

  a=[0]+_a+[0]

  for i in range(n):
    x1=abs(a[i+1]-a[i])
    x2=abs(a[i+2]-a[i+1])
    x3=abs(a[i+2]-a[i])
    y=x1+x2-x3
    print(sm-y)

main()

Submission status: Accepted

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

Submission status: Accepted

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


def solve(N: int, A: "List[int]"):

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

    for i in range(len(A)):
        cp = A[i]
        pp = 0
        np = 0
        if i>0:
            pp = A[i-1]
        if (i+1) < len(A):
            np = A[i+1]
                   
        if pp <= cp <= np or pp >= cp >= np:
            print(cost)
        elif np <= pp <= cp or cp <= pp <= np:
            print(cost - abs(pp-cp)*2)
        elif cp <= np <= pp or cp >= np >= pp:
            print(cost - abs(np-cp)*2)
        


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
A_dis=[abs(A[0]-0)]+list(abs(A[i+1]-A[i]) for i in range(N-1))+[abs(0-A[N-1])]
total=sum(A_dis)
for j in range(N):
    if j!=0 and j!=N-1:
        print(total-abs(A[j]-A[j-1])-abs(A[j+1]-A[j])+abs(A[j+1]-A[j-1]))
    elif j==0:
        print(total- abs(A[j]) - abs(A[j + 1] - A[j])+abs(A[j+1]))
    else:
        print(total- abs(A[j]) - abs(A[j - 1] - A[j]) + abs(A[j - 1]))

Submission status: Accepted

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

total = 0
lst = []
for i in range(1, N + 1):
    total += abs(A[i]-A[i-1])
    lst += [abs(A[i+1]-A[i-1])-(abs(A[i-1]-A[i])+abs(A[i]-A[i+1]))]
total += abs(A[N]-A[N+1])
# print (total)
# print (lst)
for i in range(N):
    print (total+lst[i])

Submission status: Accepted

Solution code:
N = int(input())
A = [0] + [int(i) for i in input().split()] + [0]
R = A[::-1]
s = [0]
t = [0]
for i in range(N):
  s.append(s[i] + abs(A[i] - A[i + 1]))
  t.append(t[i] + abs(R[i] - R[i + 1]))
for i in range(N):
  print(s[i] + t[N - 1 - i] + abs(A[i] - A[i + 2]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
_,a=open(0)
a=[0]+list(map(int,a.split()))+[0]
A=abs
c=sum(A(j-i)for i,j in zip(a,a[1:]))
for i,j,k in zip(a,a[1:],a[2:]):print(c-A(j-i)-A(k-j)+A(k-i))

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
a=[0]+a+[0]
A=abs
c=sum(A(j-i)for i,j in zip(a,a[1:]))
for i,j,k in zip(a,a[1:],a[2:]):print(c-A(j-i)-A(k-j)+A(k-i))

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
a=[0]+a+[0]
c=sum(abs(j-i)for i,j in zip(a,a[1:]))
for i,j,k in zip(a,a[1:],a[2:]):print(c-abs(j-i)-abs(k-j)+abs(k-i))

Submission status: Accepted

Solution code:
N, *A = map(int, open(0).read().split())
A = [0] + A + [0]
diff = [abs(A[i + 1] - A[i]) for i in range(N + 1)]
diff_i = [abs(A[i + 2] - A[i]) for i in range(N)]
a = sum(diff)
for i in range(N):
  print(a + diff_i[i] -  (diff[i] + diff[i + 1]))

Submission status: Accepted

Solution code:
N = int(input())
A = [0] + [int(i) for i in input().split()] + [0]
S = ans = 0
for i in range(N+1):
    S += abs(A[i]-A[i+1])
for j in range(1,N+1):
    ans =  S+abs(A[j-1]-A[j+1])-(abs(A[j-1]-A[j])+abs(A[j]-A[j+1]))
    print(ans)

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):
  ans = total - abs(a[i+1] - a[i]) - abs(a[i] - a[i-1]) + abs(a[i+1] - a[i-1])
  print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

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

s = 0

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

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

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):
  ans = total - abs(a[i+1] - a[i]) - abs(a[i] - a[i-1]) + abs(a[i+1] - a[i-1])
  print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=[0]
A+=map(int,input().split())
A.append(0)
S=0

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.insert(0,0)
A.append(0)
cost = []
for i in range(N+1):
    cost.append(abs(A[i+1]-A[i]))
seed_of_ans = sum(cost)
for i in range(N):
    L = [A[i+2],A[i+1],A[i]]
    L.sort()
    if max(L) == A[i+1] or min(L) == A[i+1]:
        print(seed_of_ans-abs(A[i+2]-A[i+1])-abs(A[i+1]-A[i])+abs(A[i+2]-A[i]))
    else:
        print(seed_of_ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
l=[0]+list(map(int,input().split()))+[0]
S=sum([abs(l[i]-l[i-1]) for i in range(1,n+2)])
for i in range(1,n+1):
    m,M=min(l[i-1],l[i+1]),max(l[i-1],l[i+1])
    if m<=l[i]<=M:
        print(S)
    else:
        print(S-2*min(abs(m-l[i]),abs(M-l[i])))

Submission status: Accepted

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

total = abs(A[0])
cancel = [0 for _ in range(N)]
cancel[0] = abs(A[1]) - (abs(A[1] - A[0]) + abs(A[0]))

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

total += abs(A[N-1]-A[N-2]) + abs(A[N-1])
cancel[N-1] = abs(A[N-2]) - (abs(A[N-1]) + abs(A[N-1] - A[N-2]))

for c in cancel:
    print(total+c)