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 solve(n, A):
    A = [0] + A + [0]
    diffs = [abs(A[i] - A[i + 1]) for i in range(n + 1)]
    total = sum(diffs)
    return [total - diffs[i] - diffs[i + 1] + abs(A[i] - A[i + 2]) for i in range(n)]

_n = int(input())
_A = list(map(int, input().split()))
print(*solve(_n, _A), sep='\n')

Submission status: Accepted

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

for i in range(N):
  if i==0:
    print(D-abs(A[0])-abs(A[1]-A[0])+abs(A[1]))
  elif i==(N-1):
    print(D-abs(A[N-1])-abs(A[N-1]-A[N-2])+abs(A[N-2]))
  else:
    print(D-abs(A[i+1]-A[i])-abs(A[i]-A[i-1])+abs(A[i+1]-A[i-1]))

Submission status: Accepted

Solution code:
n = int(input())

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

s = 0



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

for i in range(1,n+1):
    if (a[i] - a[i-1])*(a[i+1] - a[i]) < 0:
        if abs(a[i] - a[i-1]) < abs(a[i+1] - a[i]):
            print(s - 2*abs(a[i] - a[i-1]))
        else:
            print(s - 2*abs(a[i+1] - a[i]))
    else :
        print(s)

Submission status: Accepted

Solution code:
N = int(input())

A = [0]
A.extend(map(int, input().split()))
A.append(0)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

each_abs = [abs(nums[0])]
for i in range(1, n):
    each_abs.append(abs(nums[i] - nums[i-1]))
# 0に帰る道
each_abs.append(abs(nums[-1]))
s = sum(each_abs)

add_list = []
for i in range(n):
    if i == 0:
        add_list.append(abs(nums[i+1]))
    elif i == n-1:
        add_list.append(abs(nums[-2]))
    else:
        add_list.append(abs(nums[i+1] - nums[i-1]))

del_list = []
for i in range(n):
    del_list.append(each_abs[i] + each_abs[i+1])

for i in range(n):
    print(s + add_list[i] - del_list[i])

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
n = int(input())

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

list = [0] + list + [0]

total = 0

#まずはすべての地点を経由する場合(最大値)にどれくらいコストがかかるのか計算する
for i in range(n+1):
    total += abs(list[i + 1] - list[i])

#最大値から地点i-1から地点iまでの距離、地点iから地点i+1までの距離を引き、地点i-1から地点i+1までの距離を足してあげる
for i in range(1, n+1):
    print(total - abs(list[i] - list[i-1]) - abs(list[i+1] - list[i]) + abs(list[i+1] - list[i-1]))

Submission status: Accepted

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

de=0 #最初の予定距離
for i in range(n-1):
  de+=abs(a[i+1]-a[i])
de+=abs(a[0])+abs(a[-1])

#リストの最初と最後に0追加すれば処理楽になる（？）
a.append(0)
a.insert(0,0)


for i in range(1,n+1):
  ans=de
  x=a[i-1]
  y=a[i]
  z=a[i+1]
  if a[i-1]<=a[i]<=a[i+1] or a[i-1]>=a[i]>=a[i+1]:
    pass
  elif x<=z<=y or y<=z<=x:
    ans-=abs(a[i]-a[i+1])*2
  else:
    ans-=abs(a[i]-a[i-1])*2
  print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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

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


    for i in range(N):
        if i == 0:
            sabun = abs(abs(A[i] - 0) + abs(A[i] - A[i + 1]) - abs(A[i + 1] - 0))
            print(total - sabun)
        elif i == N - 1:
            sabun = abs(abs(A[i] - A[i - 1]) + abs(A[i]) - abs(A[i - 1]))
            print(total - sabun)
        else:
            sabun = abs(abs(A[i] - A[i - 1]) + abs(A[i] - A[i + 1]) - abs(A[i + 1] - A[i - 1]))
            print(total - sabun)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N=int(input())
A=list(map(int,input().split()))
#print(A)
W=[0]*N
W[0]=abs(A[0])
c=abs(A[N-1])
for i in range(N-1):
    W[i+1]=abs(A[i+1]-A[i])
#print(W[N-1])

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

print(S-W[N-1]+abs(A[N-2]))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

l=0
ans=sum(r)
for i in range(n):
    if l<=a[i]<=a[i+1] or l>=a[i]>=a[i+1]:
        print(ans)
    else:
        print(ans-2*min(r[i],r[i+1]))
    l=a[i]

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

B = []

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

distance = sum(B)

for i in range(1, N+1):
  sub = B[i-1] + B[i]
  add = abs(A[i+1] - A[i-1])
  print(distance - sub + add)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
#同じくint型でリスト化する
lst1 = list(map(int,input().split()))
lst1.insert(0,0)
lst1.append(0)
ans = 0
for i in range(1,n+2):
    ans += abs(lst1[i]-lst1[i-1])

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array([0] + [int(x) for x in input().split()] + [0])
total = np.abs(np.diff(A)).sum()
saved = np.abs(A[1:-1] - A[:-2]) + np.abs(A[1:-1] - A[2:]) - np.abs(A[:-2] - A[2:])
answer = total - saved
print(*answer, sep='\n')

Submission status: Accepted

Solution code:
N = int(input())
# 0地点を加える
A = [0] + list(map(int, input().split())) + [0]

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

# a地点を抜くと(a-1->a)-(a->a+1)が消え、(a-1->a+1)を通る
for i in range(1, N+1):
  aa = abs(A[i-1]-A[i])
  ab = abs(A[i]-A[i+1])
  c =  abs(A[i-1]-A[i+1])
  print(total + c - (aa+ab))

Submission status: Accepted

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

li1 = [0,abs(a[1])]
li2 = [0,abs(a[n])]

for i in range(2,n+1):
    li1.append(abs(a[i-1]-a[i])+li1[len(li1)-1])
    li2.append(abs(a[n-i+1]-a[n-i+2])+li2[len(li2)-1])
    #print(abs(a[n-i+1]-abs(a[n-i+2])))
li1.append(abs(a[n])+li1[len(li1)-1])
li2.append(abs(a[1])+li2[len(li2)-1])

#print(li1)
#print(li2)


for i in range(1,n+1):
    print(li1[i-1]+li2[n-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)
time = 0
now = 0
for i in range(N+2):
    time += abs(A[i]-now)
    now = A[i]
for i in range(1,N+1):
    if A[i-1] <= A[i] <= A[i+1]:
        print(time)
    else:
        print(time-((abs(A[i-1]-A[i])+abs(A[i]-A[i+1]))-abs(A[i-1]-A[i+1])))

Submission status: Accepted

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

dist=0
diff=[0]*(len(A)-2)
for i in range(len(A)-1):
  dist+=abs(A[i+1]-A[i])
  if 1<=i<=len(A)-2:
    diff[i-1]=abs(A[i]-A[i-1])+abs(A[i+1]-A[i])-abs(A[i+1]-A[i-1])
  
for i in range(len(diff)):
  print(dist-diff[i])

Submission status: Accepted

Solution code:
N = int(input())
A = [0]+list(map(int,input().split()))+[0]
suma = 0
for i in range(1,N+2):
    suma += abs(A[i]-A[i-1])
for i in range(1,N+1):
    print(suma - 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()))
total=abs(a[0])+abs(a[-1])
for i in range(1,n):
    total+=abs(a[i]-a[i-1])
a.append(0)
a.insert(0,0)
for i in range(1,n+1):
    d1=abs(a[i-1]-a[i])
    d2=abs(a[i]-a[i+1])
    d3=abs(a[i-1]-a[i+1])
    print(total-(d1+d2)+d3)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N=int(input())
A=[0]
a=list(map(int,input().split()))
for i in range(N):
    A.append(a[i])
A.append(0)
#print(A)
l=0
for i in range(N+2):
    l+=abs(A[i]-A[i-1])
#print(l)
for i in range(1,N+1):
    ans=l
    if (A[i+1]-A[i])*(A[i]-A[i-1])<0:
        ans=l-2*min(abs(A[i+1]-A[i]),abs(A[i]-A[i-1]))
    print(ans)

"""if amin>0:
    for i in range(N):
        if A[i]!=amax:
            print(amax*2)
        else:
            print(amax2*2)
elif amax<0:
    for i in range(N):
        if A[i]!=amin:
            print(-amin*2)
        else:
            print(-amin2*2)
else:
    for i in range(N):
        if A[i]!=amax:
            if A[i]!=amin:
                print("A",amax*2-amin*2)
            else:
                if amin2>=0:
                    print(amax*2)
                else:
                    print(amax*2-amin2*2)
        else:
            if amax2<0:
                print(-amin*2)
            else:
                print(-amin*2+amax2*2)"""

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
# WIP
import sys
sys_input = sys.stdin.readline

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

basic_cost = sum([abs(AAA[i + 1] - A) for i, A in enumerate(AAA[:N + 1])])

for i, A in enumerate(AAA[:N + 1]):
    if i == 0:
        continue
    adjusted_cost = basic_cost - abs(A - AAA[i-1]) - abs(A - AAA[i+1]) + \
        abs(AAA[i+1]-AAA[i-1])
    print(adjusted_cost)

Submission status: Accepted

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

A = [0] + A + [0]
sowa = 0
for i in range(N+1):
    sowa += abs(A[i]-A[i+1])
for i in range(1,N+1):
    print(sowa-(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 = [0] + A + [0]
sowa = 0
for i in range(N+1):
    sowa += abs(A[i]-A[i+1])
for i in range(1,N+1):
    print(sowa-(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())
As = list(map(int, input().split()))

As = [0] + As + [0]
dist = [As[i]-As[i-1] for i in range(1,N+2)]
absdist = [abs(d) for d in dist]
s = sum(absdist)
for i in range(N):
    minus = abs(As[i+1]-As[i]) + abs(As[i+2]-As[i+1])
    plus = abs(As[i+2] - As[i])
    print(s+plus-minus)

Submission status: Accepted

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


def solve(N: int, A: "List[int]"):
    dif = [0]*(N+1) 
    dif2 = [0]*N
    A = [0] + A + [0]
    for i in range(N+1):
        dif[i] = abs(A[i+1]-A[i])
    for i in range(N):
        dif2[i] = abs(A[i+2]-A[i])
    total_distance = sum(dif)
    
    # output answer
    for i in range(N):
        print(total_distance-dif[i]-dif[i+1]+dif2[i])

    return


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, = map(int, input().split())
A.insert(0,0)
A.append(0)

sum = 0

for i in range(1,N+2,1):

    sum = sum + abs(A[i]-A[i-1])


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

Submission status: Accepted

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

for i in range(n+1):
    if i == 0:
        cost.append(a[i])
    else:
        cost.append(a[i]-a[i-1])

sumcost = sum(map(abs,cost))

for i in range(n):
    ans = sumcost
    if cost[i] * cost[i+1] < 0:
        ans -= 2*min(abs(cost[i]),abs(cost[i+1]))
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [0]*(N+2)
B = [0]*N

for i,x in enumerate(input().split()):
  A[i+1] = int(x)

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

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

for i in range(N):
  print(s-B[i])

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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:
import sys
import numpy as np 

n, *a = map(int, sys.stdin.read().split())
a = np.array([0] + a + [0])

def main():
    res1 = np.absolute(a[2:] - a[1:-1]) + np.absolute(a[1:-1] - a[:-2])
    res2 = np.absolute(a[2:] - a[:-2])
    default = np.absolute(a[1:] - a[:-1]).sum()
    
    ans = default - (res1 - res2)
    return ans

if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')

Submission status: Accepted

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

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

DR = [1, -1, 0, 0]
DC = [0, 0, 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 main():
    N = I()
    A = LI()
    cur_x = 0
    tot = 0
    for a in A:
        tot += abs(cur_x - a)
        cur_x = a
    tot += abs(cur_x - 0)
    A.append(0)
    cur_x = 0
    for i in range(N):
        print(tot - abs(cur_x - A[i]) - abs(A[i] - A[i+1]) + abs(cur_x - A[i+1]))
        cur_x = A[i]
main()

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
CUSUM=[0]*(N+2)
if N > 2:
  for i in range(N):
    if i == 0:
      CUSUM[1] = abs(A[0])
    else:
      CUSUM[i+1] = abs(A[i-1] - A[i])
  CUSUM[N+1] = abs(A[N-1])
  M = sum(CUSUM)
  #print(CUSUM)
  for i in range(N):
    if i == 0:
      ANS =  M - CUSUM[i+1] - CUSUM[i+2] + abs(A[i+1])
    elif i == N-1:
      ANS = M - CUSUM[-1] - CUSUM[-2] + abs(A[i-1])
    else:
      ANS = M - CUSUM[i+1] - CUSUM[i+2] + abs(A[i-1] - A[i+1])
    print(ANS)
else:
  print(abs(A[1])*2)
  print(abs(A[0])*2)

Submission status: Accepted

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

Submission status: Accepted

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

B = [0] + A + [0]
total = 0

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

for i in range(1, N + 1):
    b = B[i]
    b_next = B[i + 1]
    b_prev = B[i - 1]
    ans = total - (abs(b - b_prev) + abs(b_next - b)) + abs(b_next - b_prev)
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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 j in range(1, n+1):
    if a[j-1] <= a[j] <= a[j+1] or a[j-1] >= a[j] >= a[j+1]:
        print(cost)
    else:
        print(cost - min(abs(a[j] - a[j-1]), abs(a[j] - a[j+1]))*2)

Submission status: Accepted

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

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

for i in range(1, N + 2 - 1):
    t1 = abs(A[i-1] - A[i]) + abs(A[i] - A[i+1])
    t2 = abs(A[i-1] - A[i+1])
    print(total - t1 + t2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
a=[0]
for i in range(n):
  a.append(h[i])
a.append(0)
s=0
for i in range(n+1):
  s+=abs(a[i]-a[i+1])
for i in range(1,n+1):
  if min(a[i+1],a[i-1])<=a[i]<=max(a[i-1],a[i+1]):
    print(s)
  elif a[i]<=min(a[i-1],a[i+1])<=max(a[i-1],a[i+1]):
    print(s-2*abs(a[i]-min(a[i+1],a[i-1])))
  else:
    print(s-2*abs(max(a[i-1],a[i+1])-a[i]))

Submission status: Accepted

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

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

for i in range(1, N+1):
    dist = D - (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 = list(map(int,input().split()))
A_l = [0,0]
A_l[1:1] = A
D = 0
for i in range(len(A_l) - 1):
  D += abs(A_l[i] - A_l[i + 1])
for i in range(n):
  print(D - abs(A_l[i] - A_l[i + 1])- abs(A_l[i + 1] - A_l[i + 2]) + abs(A_l[i] - A_l[i + 2]))

Submission status: Accepted

Solution code:
#coding:utf-8
N = int(input())
A = list(map(int,input().split()))
A.append(0)
A.insert(0,0)
ans = 0
for n in range(N+1):
    ans+=abs(A[n]-A[n+1])
for n in range(N):
    if (min(A[n],A[n+2])<=A[n+1] and A[n+1]<=max(A[n],A[n+2])):
        print (ans)
    else:
        print (ans - 2 * min(abs(A[n+1]-A[n]),abs(A[n+1]-A[n+2])))

Submission status: Accepted

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

D = [] # Aiをスキップしたときのデルタ値
now = 0

total = 0
for new1, new2 in zip(A[:-1], A[1:]):
    dist = abs(new1 - now)
    total += dist

    no_skip = abs(new2 - new1) + dist
    skip = abs(new2 - now)
    delta = skip - no_skip
    D.append(delta)
    now = new1

for i in range(n):
    ans = total + D[i]
    print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import numpy as np
N = int(input())
A = np.array([0] + [int(x) for x in input().split()] + [0])
A_diff = np.diff(A)
total = abs(A_diff).sum()
for i in range(1,N+1):
    ans = total
    ans -= abs(A_diff[i]) + abs(A_diff[i-1]) 
    ans += abs(A[i+1] - A[i-1])
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
input = sys.stdin.readline

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

a = [0] + a + [0]
#iを取りやめない場合のcost総計
total_cost = sum([abs(i-j) for i,j in zip(a,a[1::])])
#iを取りやめる場合に変化するcost
for i in range(1,n+1):
    decrease_cost_if_stop_i = (abs(a[i-1] - a[i]) + abs(a[i]-a[i+1])) - abs(a[i-1] - a[i+1])
    print(total_cost - decrease_cost_if_stop_i)

Submission status: Accepted

Solution code:
def cost(p, q):
    if p * q >= 0:
        return max(abs(p), abs(q)) * 2
    else:
        return abs(p - q) * 2

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

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
SUM=abs(A[0])
ans=[0]*n
for i in range(n-1):
    SUM+=abs(A[i+1]-A[i])
SUM+=abs(A[n-1])
A.append(0)
for i in range(n):
    if i==0:
        if A[i]<min(A[i+1],0):
            ans[i]=SUM-2*(min(A[i+1],0)-A[i])
        elif A[i]>max(A[i+1],0):
            ans[i]=SUM-2*(A[i]-max(A[i+1],0))
        else:
            ans[i]=SUM
    elif A[i]<min(A[i+1],A[i-1]):
        ans[i]=SUM-2*(min(A[i+1],A[i-1])-A[i])
    elif A[i]>max(A[i+1],A[i-1]):
        ans[i]=SUM-2*(A[i]-max(A[i+1],A[i-1]))
    else:
        ans[i]=SUM
print(*ans,sep='\n')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [0] + [int(i) for i in input().split()] + [0]
total = 0
for i in range(len(A) - 1):
    total += abs(A[i + 1] - A[i])
for i in range(1, len(A) - 1):
    print(
        total - 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 = list(map(int, input().split()))
l.append(0)
adjustment = [0] * n
base_money = 0
position = 0
for i in range(n):
    if position > l[i] and l[i] < l[i+1]:
        if position < l[i+1]:
            adjustment[i] = (abs(l[i + 1] - l[i]) -
                             abs(l[i + 1] - position)) * 2
        else:
            adjustment[i] = (abs(l[i] - position) -
                             abs(l[i + 1] - position)) * 2
    elif position < l[i] and l[i] > l[i + 1]:
        if position < l[i+1]:
            adjustment[i] = (abs(l[i] - position) -
                             abs(l[i + 1] - position)) * 2
        else:
            adjustment[i] = (abs(l[i + 1] - l[i]) -
                             abs(l[i + 1] - position)) * 2
    base_money += abs(l[i] - position)
    position = l[i]
base_money += abs(position)
for i in range(n):
    print(base_money - adjustment[i])

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
alis=[0]+list(map(int,input().split()))+[0]
deflis=[]
for i in range(n+1):
	d=abs(alis[i+1]-alis[i])
	deflis.append(d)
defsum=sum(deflis)
for i in range(n):
	ans=defsum-sum(deflis[i:i+2])+abs(alis[i+2]-alis[i])
	print(ans)

Submission status: Accepted

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

comp = 0
for i in range(len(aa)-1):  
  comp += abs(aa[i+1]-aa[i])

shortcut = [0] * n
for i in range(1,n+1):
  shortcut[i-1] = abs(aa[i]-aa[i-1]) + abs(aa[i+1]-aa[i]) - abs(aa[i+1]-aa[i-1])  
  
for i in range(n):
  print(comp - shortcut[i])

Submission status: Accepted

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

a = [0] + a + [0]

# print(a)
s = 0
for i in range(n + 1):
    s += abs(a[i] - a[i + 1])
    # print(s)
# print(s)
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=list(map(int, input().split()))
a.insert(0,0)
a.insert(n+1,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])+abs(a[i]-a[i+1]))+abs(a[i+1]-a[i-1]))

Submission status: Accepted

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

for i in range(N):
    dif = 0
    if i == 0:
        dif = abs(A[1] - A[0]) + abs(A[0]) - abs(A[1])
    else:
        dif = abs(A[i + 1] - A[i]) + abs(A[i] - A[i - 1]) - abs(A[i + 1] - A[i - 1])
    print(total - dif)

Submission status: Accepted

Solution code:
N = int(input())
A = [0]

A_0 = map(int, input().split())
for i in A_0:
    A.append(i)
A.append(0)
    
sum_A = 0
for i in range(N + 1):
    sum_A += abs(A[i] - A[i + 1])

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.insert(0, 0)
A.append(0)
distance = []
sum_val = 0
for i in range(1,len(A)):
    d = abs(A[i] - A[i - 1])
    sum_val += d
    distance.append(d)
banish_list = []
now_direct = "no"
for next in range(1,len(A)):

    if A[next - 1] < A[next]:
        next_direct = "right"
    elif A[next - 1] > A[next]:
        next_direct = "left"
    else:
        next_direct = "no"
    if now_direct != next_direct and (next_direct != "no" and now_direct != "no"):
        d = min(distance[next - 1] , distance[next - 2])
    else:
        d = 0
    banish_list.append(d)
    now_direct = next_direct
banish_list.pop(0)
for b in banish_list:
    print(sum_val - b * 2)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array([0] + [int(x) for x in input().split()] + [0])
total = np.abs(np.diff(A)).sum()
saved = np.abs(A[1:-1] - A[:-2]) + np.abs(A[1:-1] - A[2:]) - np.abs(A[:-2] - A[2:])
answer = total - saved
print(*answer, sep='\n')

Submission status: Accepted

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

cur = 0
for i in range(N):
    if (cur <= A[i] <= A[i+1]) or (A[i+1] <= A[i] <= cur):
        print(total)
    elif (A[i+1] <= cur <= A[i]) or (A[i] <= cur <= A[i+1]):
        print(total-abs(A[i]-cur)*2)
    elif (A[i] <= A[i+1] <= cur) or (cur <= A[i+1] <= A[i]):
        print(total-abs(A[i+1]-A[i])*2)
    cur = A[i]

Submission status: Accepted

Solution code:
import itertools as it
import collections as col
import heapq as hq
import sys
import copy as cp
sys.setrecursionlimit(10**9)
args = sys.argv
input = sys.stdin.readline


def dump(*objects):
    if len(args) == 2 and args[1] == "DEBUG":
        print(*objects, file=sys.stderr)


N = int(input())
A = [0] + list(map(int, input().split())) + [0]
dump(N, A)
B = [0]*len(A)
C = [0]*len(A)
for i in range(1, len(A)):
    dump(i)
    B[i] = B[i - 1] + abs(A[i] - A[i - 1])
for i in range(len(A) - 2, -1, -1):
    dump(i)
    C[i] = C[i+1] + abs(A[i]-A[i+1])
dump(A, B, C)
for i in range(1, N+1):
    dump(i, B[i-1] + B[N+1] - B[i+1] + abs(A[i-1]-A[i+1]))
    print(B[i-1] + C[i+1] + abs(A[i-1]-A[i+1]))

exit(0)