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:
n = int(input())
A = [0] + list(map(int, input().split())) + [0]
C = []
c = 0
for i in range(n+1):
  c += 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(c)
  elif A[i] <=A[i-1] <= A[i+1] or A[i] <= A[i+1] <= A[i-1]:
    print(c - abs(min(A[i-1], A[i+1]) - A[i])*2)
  elif A[i-1] <= A[i+1] <= A[i] or A[i+1] <= A[i-1] <= A[i]:
    print(c - abs(A[i] - max(A[i+1], A[i-1]))*2)

Submission status: Accepted

Solution code:
n = int(input())
A = [0] + list(map(int, input().split())) + [0]
C = []
c = 0
for i in range(n+1):
  c += 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(c)
  elif A[i] <=A[i-1] <= A[i+1] or A[i] <= A[i+1] <= A[i-1]:
    print(c - abs(min(A[i-1], A[i+1]) - A[i])*2)
  else:
    print(c - abs(A[i] - max(A[i+1], A[i-1]))*2)

Submission status: Accepted

Solution code:
n = int(input())
A = [0] + list(map(int, input().split())) + [0]
C = []
c = 0
for i in range(n+1):
  c += abs(A[i+1] - A[i])
for i in range(1, n+1):
  print(c - 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=[0]
for i in map(int,input().split()):
    a.append(i)
a.append(0)
s=0
for i in range(n+1):
    s+=abs(a[i]-a[i+1])
ans=0
for i in range(n):
    print(s-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=[0]
a=list(map(int,input().split()))
a.append(0)
A.extend(a)

res=0

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

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

Submission status: Accepted

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

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

for g in range(n):
    print(sumD-abs(a[g+2]-a[g+1])-abs(a[g+1]-a[g])+abs(a[g+2]-a[g]))

Submission status: Accepted

Solution code:
#092_C
n=int(input())
a=[0]+[int(j) for j in input().split()]+[0]
d1=[abs(a[i]-a[i+1]) for i in range(n+1)]
d2=[abs(a[i]-a[i+2]) for i in range(n)]
sum_all=sum(d1)
for i in range(n):
    print(sum_all-d1[i]-d1[i+1]+d2[i])

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
a = list(map(int,input().split()))
a.append(0)
total = 0
for i in range(n+1):
    total += abs(a[i] - a[i-1])
    #print(total,abs(a[i] - a[i-1]))
for i in range(-1,n-1):
    if (a[i+1]-a[i]) *(a[i+2]-a[i+1]) > 0:
        print(total) # 間に挟まれるなら 
    else: print(total - 2 * min(abs(a[i+1] -a[i]) ,abs(a[i+1] -a[i+2])))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
A.append(0)
ary = []
for i, a in enumerate(A):
    if i == 0:
        ary.append(abs(0-a))
    else:
        ary.append(ary[-1]+abs(A[i-1]-a))
for i in range(N):
    ans = 0
    if i != 0:
        ans = ary[i-1]
        ans += abs(A[i-1]-A[i+1])
    else:
        ans += abs(A[1])
    ans += ary[-1]-ary[i+1]
    print(ans)

Submission status: Accepted

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

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

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

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):
        print(All- 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()))
to = abs(a[0]) + abs(a[-1])

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

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

Submission status: Accepted

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

for i in range(N + 1):
    summ += abs(coordinate[i + 1] - coordinate[i])

diff_coorinate = []

for i in range(N):
    distance = abs(coordinate[i + 2] - coordinate[i])
    diff_coorinate.append(distance)

for i in range(N):

    next = coordinate[i + 1] - coordinate[i]
    n_next = coordinate[i + 2] - coordinate[i + 1]
    
    if (next * n_next >= 0):
        ans = summ
    elif (coordinate[i + 2] - coordinate[i] >= 0):
        d = abs(min(next, n_next))
        ans = summ - 2 * d
    else:
        d = max(next, n_next)
        ans = summ - 2 * d
    
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = 0
ol = 0
for i in a:
	s += abs(i-ol)
	ol = i
s += abs(ol)
a.insert(0,0)
a.append(0)
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 (s)
	else:
		print (s-min(abs(a[i-1]-a[i]),abs(a[i]-a[i+1]))*2)

Submission status: Accepted

Solution code:
n = int(input())
a = [0] + list(map(int, input().split())) + [0]
dist = sum([abs(a[i] - a[i + 1]) for i in range(n + 1)])
for i in range(1, n + 1):
    print(dist - abs(a[i] - a[i - 1]) - 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()))
cum_a = [0]
cum_b = [0]
here_a = 0
here_b = 0
for i in range(n):
    cum_a.append(abs(here_a-a[i]) + cum_a[i])
    here_a = a[i]
    cum_b.append(abs(here_b - a[n-1-i]) + cum_b[i])
    here_b = a[n-1-i]
cum_a.append(abs(a[-1])+cum_a[-1])
cum_b.append(abs(a[0]) + cum_b[-1])
here = 0
a.append(0)
for i in range(n):
    l = cum_a[i]
    r = cum_b[n-1-i]
    move = abs(here-a[i+1])
    here = a[i]
    print(l+r+move)

Submission status: Accepted

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

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+1] - a[i])
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-1] - a[i]))

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+1] - A[i])
for j in range(1, N+1):
    print(total + abs(A[j + 1] - A[j - 1]) - abs(A[j + 1] - A[j]) - abs(A[j-1] - A[j]))

Submission status: Accepted

Solution code:
#C - Traveling Plan
N = int(input())
A = list(map(int, input().split()))
DP = []
ans = []
DP.append(abs(0-A[0]))
for i in range(1,N):
    DP.append(abs(A[i]-A[i-1]))

sum_DP = sum(DP)

for j in range(N):
    if j == 0:
        tmp = sum_DP-DP[0]-DP[1]+abs(A[1])+abs(A[N - 1])
    elif j == N-1:
        tmp = sum_DP-DP[N-1]+abs(A[N - 2])
    else:
        tmp = sum_DP + abs(A[N - 1])-DP[j]-DP[j+1]+abs(A[j+1]-A[j-1])
    ans.append(tmp)

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

Submission status: Accepted

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

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

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

for i in range(1, len(a)-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:
#traveling plan
n=int(input())
lists=list(map(int,input().split()))
a=[0 for i in range(n+2)]
for i in range(n):
    a[i+1]=lists[i]

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

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

Submission status: Accepted

Solution code:
import math
N = int(input())
A = [0]
A.extend(list(map(int, input().split())))
A.append(0)

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

for i in range(1, N+1):
  print(hoge + 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())
X = [int(x) for x in input().split()]
sum = 0
X.insert(0,0)
X.append(0)

for n in range(N+1):
    sum += abs(X[n] - X[n+1])


for n in range(N):
    l = abs(X[n] - X[n+1])
    r = abs(X[n+1] - X[n+2])

    if X[n] < X[n+1] and X[n+1] < X[n+2]:
        print(sum)
    elif X[n] > X[n+1] and X[n+1] > X[n+2]:
        print(sum)

    elif l < r:
        sep = 2*abs(X[n] - X[n+1])
        print(sum - sep)
    else:
        sep = 2*abs(X[n+2] - X[n+1])
        print(sum - sep)

Submission status: Accepted

Solution code:

def main():
    n = int(input())
    a_s = [0] + list(map(int,input().split())) + [0]
    default = sum([abs(a_s[i-1] - a_s[i]) for i in range(1,len(a_s))])
    answer = 0
    for i in range(1,len(a_s)-1):
        if (a_s[i] >=  a_s[i-1]) == (a_s[i+1] >= a_s[i]):
            #長さは変わらず
            answer = default
        else:
            answer = default - abs(a_s[i]-a_s[i-1]) - abs(a_s[i+1]-a_s[i]) + abs(a_s[i+1] - a_s[i-1])
        print(answer)





if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def c_traveling_plan():
    N = int(input())
    # 原点から出発して最後に原点に帰るためこうする (forループも楽)
    A = [0] + [int(i) for i in input().split()] + [0]

    total_tour_cost = sum([abs(A[i] - A[i + 1]) for i in range(N + 1)])
    ans = []
    for i in range(1, N + 1):
        # a_{i-1} -> a_i -> a_{i+1} の長さ(delta1)を引き，
        # a_{i-1} -> a_{i+1} の長さ(delta2)を足す
        delta1 = abs(A[i - 1] - A[i]) + abs(A[i] - A[i + 1])
        delta2 = abs(A[i - 1] - A[i + 1])
        ans.append(total_tour_cost - delta1 + delta2)
    return '\n'.join(map(str, ans))

print(c_traveling_plan())

Submission status: Accepted

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

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

for i in range(n):
    t=whole
    t-=abs(X[i+1]-X[i])+abs(X[i+2]-X[i+1])
    t+=abs(X[i+2]-X[i])
    print(t)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
N=int(input())
A=list(map(int,input().split()))
A.insert(0,0)
A.extend([0])
s=0
for i in range(1,N+2):
    s+=abs(A[i]-A[i-1])
for i in range(1,N+1):
    print(s-((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(_) for _ in input().split()]
dis = [0] * (n+1)
dis[0] = abs(a[0])
for i in range(1, n):
    dis[i] = abs(a[i] - a[i-1])
dis[n] = abs(a[n-1])
#print(dis)

total = 0
for i in dis:
    total += abs(i)
#print(total)

ans = [0] * n
ans[0] = total - (dis[0] + dis[1]) + abs(a[1])
for i in range(1, n-1):
    ans[i] = total - (dis[i] + dis[i+1]) + abs(a[i+1] - a[i-1])
ans[n-1] = total - (dis[n-1] + dis[n]) + abs(a[n-2])
#print(ans)
for i in ans:
    print(i)

Submission status: Accepted

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


for i in range(1, n+1):
    if a[i-1] <= a[i+1]:
        if a[i - 1] <= a[i] <= a[i + 1]:
            print(money)
        elif a[i] < a[i - 1]:
            print(money - 2 * (a[i - 1] - a[i]))
        elif a[i + 1] < a[i]:
            print(money - 2 * (a[i] - a[i + 1]))
    else:
        if a[i + 1] <= a[i] <= a[i - 1]:
            print(money)
        elif a[i] < a[i + 1]:
            print(money - 2 * (a[i + 1] - a[i]))
        elif a[i - 1] < a[i]:
            print(money - 2 * (a[i] - a[i - 1]))

Submission status: Accepted

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

l.insert(0, 0)
l.extend([0])
S = 0
ans = 0
for ix, i in enumerate(l[:-1]):
    S += abs(i - l[ix+1])

for ix in range(1,len(l)-1):
    S_i = S - abs(l[ix-1] - l[ix]) - abs(l[ix] - l[ix+1]) + abs(l[ix-1] - l[ix+1])
    print(S_i)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.insert(0,0)   #最初に0
a.insert(n+1,0) #最後に0

res = sum([abs(a[i]-a[i+1]) for i in range(n+1)]) #階差の和

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

Submission status: Accepted

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

for i in range(1, len(a) - 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(map(int, input().split()))
a = [0] + a + [0]

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

c = []
for i in range(n):
    c.append(abs(a[i+2] - a[i]))
c = c + [0]    

sum_b = sum(b)
for i in range(n):
    print(sum_b - sum(b[i:i+2]) + c[i])

Submission status: Accepted

Solution code:
import itertools
import math
import sys
from collections import Counter
from fractions import gcd
from functools import reduce

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

Submission status: Accepted

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

for i in range(N):
    answer = all
    if i == 0:
        prev = 0
    else:
        prev = A[i-1]
    if i == N-1:
        next = 0
    else:
        next = A[i+1]
    curr = A[i]
    if prev > curr and curr < next:
        if prev >= next:
            answer -= abs(curr - next) * 2
        else:
            answer -= abs(curr - prev) * 2
    elif prev < curr and curr > next:
        if prev >= next:
            answer -= abs(curr - prev) * 2
        else:
            answer -= abs(curr - next) * 2
    print(answer)

Submission status: Accepted

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
from collections import Counter
#from scipy.misc import comb

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):
    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()))
A.append(0)

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

D = [abs(A[i+1]-A[i]) for i in range(len(A)-1)]
base = sum(D)

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

Submission status: Accepted

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

"""考え方
累積の移動距離を順番に計算しておく。
その後、累積距離とAの配列から「対象地域をスキップした場合」の総移動距離を出力する。
"""

## 1.累積距離の計算
## accmには初期位置、最後に0に戻る、という2要素が追加されている
accm = [0]+[0 for i in range(N)]
accm[1] = abs(A[0])
for n in range(1,N):
  accm[n+1] = abs(A[n]-A[n-1]) + accm[n]

accm.append(accm[-1]+abs(A[-1]))
base_cost = accm[-1]

## 2.総移動距離を出力
## 「対象インデックスをスキップすることで省略できるコスト」をaccm[-1]から減算する
## この計算は対象インデックスをnとすると、下記の計算式で求められる
## abs(A[n+1]-A[n])+abs(A[n]-A[n-1])-abs(A[n+1]-A[n-1])
for n in range(0,N):
  if n == N-1:
    bene = abs(A[-1])+abs(A[-1]-A[-2])-abs(A[-2])
  elif n == 0:
    bene = abs(A[1]-A[0])+abs(A[0])-abs(A[1])
  else:
    bene = abs(A[n+1]-A[n])+abs(A[n]-A[n-1])-abs(A[n+1]-A[n-1])
  
  ans = base_cost - bene
  print(ans)

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
import queue
import copy

# 最大公約数
def gcd(m,n):
  x = max(m,n)
  y = min(m,n)
  if x%y == 0:
    return y
  else:
    while x%y != 0:
      z = x%y
      x = y
      y = z
    else:
      return z

# 幅優先探索(BFS)でスタート座標(x, y)からの最大距離返す
def bfs(SS, x, y, H, W):#入力(座標系(迷路), x, y, xの端, yの端)
  x_next = [0, 0, -1, 1]
  y_next = [1, -1, 0, 0]
  tempx = x
  tempy = y
  mlength = 0
  q = deque()
  xytuple = (x, y, 0)
  q.append(xytuple)


  while q:
    xytuple = q.popleft()
    SS[xytuple[0]][xytuple[1]] = "x" #探索した場所は探索できないようにする
    for i in range(4): #このforで上下左右を調べてる
      tempx = xytuple[0] + x_next[i]
      tempy = xytuple[1] + y_next[i]
      #print(tempx, tempy, xytuple[2])
      #print(S)
      if(0 <= tempx and tempx < H and 0 <= tempy and tempy < W and SS[tempx][tempy] == "."):
        temptuple = (tempx, tempy, xytuple[2]+1)
        SS[temptuple[0]][temptuple[1]] = "x" #探索した場所は探索できないようにする
        mlength = max(mlength, xytuple[2] +1)
        q.append(temptuple)
      #print(q)
  return mlength

#素因数分解
def prime_factorize(n):
  a = []
  while n % 2 == 0:
    a.append(2)
    n //= 2
  f = 3
  while f * f <= n:
    if n % f == 0:
      a.append(f)
      n //= f
    else:
      f += 2
  if n != 1:
    a.append(n)
  return a

#重複を検出
def has_duplicates(seq):
  return len(seq) != len(set(seq))



#X = str(input()).split()
#a = [int(x) for x in input().split()]


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

a = abs(A[0])

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

a += abs(A[N-1])

print(a-abs(A[0])-abs(A[1]-A[0])+abs(A[1]))

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

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

Submission status: Accepted

Solution code:
def main():
    spots_number = int(input())
    spots = [0] + list(map(int, input().split(" "))) + [0]
    visit_time = [0] * (spots_number + 2)
    for i in range(1, spots_number + 1):
        visit_time[i] = visit_time[i - 1] + abs(spots[i] - spots[i - 1])
    visit_time[-1] = visit_time[-2] + abs(spots[-2])
    for i in range(1, spots_number + 1):
        print(visit_time[i - 1] + visit_time[-1] - visit_time[i + 1] + abs(spots[i + 1] - spots[i - 1]))


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)
lis = [abs(0 - a[0])]
for i in range(n+1):
    lis.append(lis[i] + abs(a[i] - a[i+1]))
# print(lis)
# print(a)

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
a.insert(0, 0)
a.append(0)
t = 0
for i in range(n + 1):
    t += abs(a[i] - a[i + 1])
for i in range(1, n + 1):
    print(t - 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.insert(0,0)
a.insert(n+1,0)
b=[0]*(n+1)

for i in range(n+1):
    b[i]=abs(a[i]-a[i+1])
ans=sum(b)
#print(a,b)
for i in range(n):
    #print(ans,b[i],b[i+1],abs(a[i]-a[i+2]))
    print(ans-b[i]-b[i+1]+abs(a[i]-a[i+2]))

Submission status: Accepted

Solution code:
import sys

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

now = 0
count = 0
t = 0

for i in li:
  t += abs(i - now)
  now = i

t += abs(now)

li.insert(0, 0)
li.append(0)

for n in range(1, N+1):
  print(t - (abs(li[n-1] - li[n])) - abs(li[n] - li[n+1]) + abs(li[n-1] - li[n+1]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.insert(0,0)
l.append(0)
num = 1
sum = 0

for i in range(n+1):
  sum += abs(l[i+1]-l[i])
#print(sum)
    
for i in range(1,n+1):
  s1 = abs(l[i+1]- l[i-1])
  s2 = abs(l[i]- l[i-1])
  s3 = abs(l[i+1]- l[i])
  print(sum + s1 - (s2 + s3))

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = list(map(int, input().split()))
A = np.array([0] + A + [0])
# print('A', A)
D = np.abs(A[1:] - A[:-1])
# print('D', D)

sum_D = D.sum()
for i in range(1, N + 1):
    c = np.abs(A[i - 1] - A[i + 1])
    print(sum_D - (D[i - 1] + D[i]) + c)

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
a=[]
a.append(0)
for i in range(n):
    a.append(x[i])
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):
    s=abs(a[i-1]-a[i+1])
    s1=abs(a[i-1]-a[i])
    s2=abs(a[i]-a[i+1])
    print(sum+s-(s1+s2))

Submission status: Accepted

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

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

Submission status: Accepted

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

for i in range(1, N + 1):
    a = abs(lst[i - 1] - lst[i])
    b = abs(lst[i] - lst[i + 1])
    c = abs(lst[i - 1] - lst[i + 1])
    print(total - a - b + c)

Submission status: Accepted

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

count = 0
for i in range(1,n+2):
    count+=+abs(l[i]-l[i-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(count)
    else:
        print(count-(abs(l[i]-l[i-1])+abs(l[i+1]-l[i]))+abs(l[i+1]-l[i-1]))

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
count = [abs(l[0])]
for i in range(1,n):
    count.append(count[-1]+abs(l[i]-l[i-1]))
count.append(count[-1]+abs(l[n-1]))
if 0 <= l[0] <= l[1] or 0 >= l[0] >= l[1]:
    print(count[-1])
else:
    ans = count[-1]-(abs(l[0])+abs(l[1]-l[0]))+abs(l[1])
    print(ans)
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(count[-1])
    else:
        ans = count[-1]-(abs(l[i]-l[i-1])+abs(l[i+1]-l[i]))+abs(l[i+1]-l[i-1])
        print(ans)
if 0 <= l[n-1] <= l[n-2] or 0>= l[n-1] >= l[n-2]:
    print(count[-1])
else:
    ans = count[-1]-(abs(l[n-1])+abs(l[n-1]-l[n-2]))+abs(l[n-2])
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate
n = int(input())
a = [0] + list(map(int, input().split())) + [0]
cost = [abs(a[i] - a[i+1]) for i in range(n+1)]
acc = [0] + list(accumulate(cost))

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

Submission status: Accepted

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

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

Submission status: Accepted

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

def costs(a):
    prev = 0
    for x in a:
        yield abs(x - prev)
        prev = x

def main():
    input()
    A = [0] + [int(c) for c in input().split()] + [0]
    total = sum(costs(A))
    for i, x in enumerate(A[1:-1],1):
        prev_cost = abs(x - A[i-1])
        succ_cost = abs(A[i+1] - x)
        print(total - (prev_cost+succ_cost) + abs(A[i+1]-A[i-1]))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

def costs(a):
    prev = 0
    for x in a:
        yield abs(x - prev)
        prev = x

def main():
    input()
    A = [0] + [int(c) for c in input().split()] + [0]
    total = sum(costs(A))
    for i, x in enumerate(A):
        if i == 0 or i == len(A)-1: continue
        prev_cost = abs(x - A[i-1])
        succ_cost = abs(A[i+1] - x)
        print(total - (prev_cost+succ_cost) + abs(A[i+1]-A[i-1]))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

def total_cost(a):
    before = 0
    total = 0
    for x in a:
        total += abs(x - before)
        before = x
    return total

def main():
    input()
    A = [0] + [int(c) for c in input().split()] + [0]
    total = total_cost(A)
    for i, x in enumerate(A):
        if i == 0 or i == len(A)-1: continue
        prev_cost = abs(x - A[i-1])
        succ_cost = abs(A[i+1] - x)
        print(total - (prev_cost+succ_cost) + abs(A[i+1]-A[i-1]))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=[0]
a.extend(list(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):
  print(s-abs(a[i+1]-a[i])-abs(a[i]-a[i-1])+abs(a[i+1]-a[i-1]))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [0] + [int(i) for i in input().split()] + [0]
    a = [abs(A[i] - A[i + 1]) for i in range(N + 1)]
    num = sum(a)
    print("\n".join(
        [str(num - a[i] - a[i + 1] + abs(A[i] - A[i + 2]))
         for i in range(N)]))

    return


main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [0] + [int(i) for i in input().split()] + [0]
    a = [abs(A[i + 1] - A[i]) for i in range(N + 1)]
    num = sum(a)
    print("\n".join(
        [str(num - a[i] - a[i + 1] + abs(A[i] - A[i + 2]))
         for i in range(N)]))

    return


main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [0] + [int(i) for i in input().split()] + [0]
    d = [abs(A[i + 1] - A[i]) for i in range(N + 1)]
    num = sum(d)
    print("\n".join(
        [str(num - d[i] - d[i + 1] + abs(A[i] - A[i + 2]))
         for i in range(N)]))

    return


main()

Submission status: Accepted

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

yen = abs(ls[0])

for i in range(n - 1):
    yen += abs(ls[i + 1] - ls[i])

yen += abs(ls[n - 1])

for j in range(n):
    if j == 0:
        print(yen - abs(ls[0]) - abs(ls[1] - ls[0]) + abs(ls[1]))
    elif j == n - 1:
        print(yen - abs(ls[n - 1]) - abs(ls[n - 1] - ls[n - 2]) + abs(ls[n - 2]))
    else:
        print(yen - abs(ls[j + 1] - ls[j]) - abs(ls[j] - ls[j - 1]) + abs(ls[j + 1] - ls[j - 1]))

Submission status: Accepted

Solution code:
n = int(input())
a = [0]+list(map(int,input().split()))+[0]
cost = sum([abs(a[i]-a[i+1]) for i in range(n+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]))