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:
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = np.array([0] + lr() + [0])
diff = np.diff(A)
total = np.abs(diff).sum()
answer = []
for i in range(N):
    answer.append(total - abs(diff[i]) - abs(diff[i+1]) + abs(A[i+2]-A[i]))

print('\n'.join(map(str, answer)))

Submission status: Accepted

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

for i in range(N):
    if i == 0:
        if (0 <= A[i] <= A[i+1]) or (A[i+1] <= A[i] <= 0):
            tmp = ans 
        else:
            tmp = ans - abs(A[0]) - abs(A[0]-A[1]) + abs(A[1])
        print(tmp)
    elif i == N-1:
        if (0 <= A[i] <= A[i-1]) or (A[i-1] <= A[i] <= 0):
            tmp = ans
        else:
            tmp = ans - abs(A[i]-A[i-1]) - abs(A[i]) + abs(A[i-1])
        print(tmp)
    else:
        if (A[i-1] <= A[i] <= A[i+1]) or (A[i-1] >= A[i] >= A[i+1]):
            print(ans)
        else:
            tmp = ans - abs(A[i]-A[i-1]) - abs(A[i]-A[i+1]) + abs(A[i+1]-A[i-1])
            print(tmp)

Submission status: Accepted

Solution code:
#import numpy as np
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial#, gcd
from bisect import bisect_left, bisect_right #bisect_left(list, value)
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep='\n')

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

dist = 0
difs = [0]*N
for i, a in enu(A[:-2]):
    c = a
    n = A[i+1]
    nn = A[i+2]
    dist += abs(n-c)

    dif = 0 # 消したときに減る量
    flg_dif = (c<=n)^(n<=nn)
    if flg_dif:
        difs[i] = min(2*abs(n-c), 2*abs(nn-n))
dist += abs(A[-2])

# print('dist', dist)
# print('difs', difs)
for i, a in enu(A[1:-1]):
    print(dist-difs[i])

Submission status: Accepted

Solution code:
n = int(input())
As = list(map(int, input().split()))
ais = [0]
ais.extend(As)
m = 0
ais.append(m)
S = 0
for i in range(n-1):
  S += abs(As[i]-As[i+1])
S += abs(As[0]) + abs(As[n-1])
for i in range(1,n+1):
  sb1 = abs(ais[i-1]-ais[i])
  sb2 = abs(ais[i]-ais[i+1])
  ad = abs(ais[i-1]-ais[i+1])
    
  ans = S - sb1 - sb2 + ad
  print(ans)

Submission status: Accepted

Solution code:
n = int(input())
As = list(map(int, input().split()))
S = 0
for i in range(n-1):
  S += abs(As[i]-As[i+1])
S += abs(As[0]) + abs(As[n-1])
for i in range(n):
  if i == 0:
    sb1 = abs(As[i])
    sb2 = abs(As[i]-As[i+1])
    ad = abs(As[i+1])
  elif i == n-1:
    sb1 = abs(As[i-1]-As[i])
    sb2 = abs(As[i])
    ad = abs(As[i-1])
  else:
    sb1 = abs(As[i-1]-As[i])
    sb2 = abs(As[i]-As[i+1])
    ad = abs(As[i-1]-As[i+1])
    
  ans = S - sb1 - sb2 + ad
  print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(1, len(a)-1):
    x = d - abs(a[i] - a[i-1]) - abs(a[i+1] - a[i]) + abs(a[i+1] - a[i-1])
    print(x)

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

for k in range(1, n+1):
    ans=cost+abs(A[k+1]-A[k-1])-(abs(A[k]-A[k-1])+abs(A[k+1]-A[k]))
    print(ans)

Submission status: Accepted

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

s = []
for i in range(n+1):
    s.append(abs(a[i]-a[i+1]))
ss = sum(s)
for i in range(1,n+1):
    print(ss+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()))
pre = 0
sumall = 0

for i in range(N):
   sumall += abs(A[i] - pre)
   pre = A[i]

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

Submission status: Accepted

Solution code:
import copy
n=int(input())
a=[int(x) for x in input().rstrip().split()]
aa=copy.deepcopy(a)
a=[0]+a+[0]
sa=[]
for i in range(n+1):
  sa.append(abs(a[i+1]-a[i]))

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

Submission status: Accepted

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

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

Submission status: Accepted

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

  
for j in range(n):  
  if abs(l[j+2]-l[j])<abs(l[j+1]-l[j])+abs(l[j+2]-l[j+1]):
    if abs(l[j+1]-l[j])<abs(l[j+2]-l[j+1]):
      print(wa-2*abs(l[j+1]-l[j]))
    else:
      print(wa-2*abs(l[j+2]-l[j+1]))
  else:
    print(wa)

Submission status: Accepted

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

Submission status: Accepted

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

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

ref = sum(d)

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

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())
A = [0] + list(map(int,input().split())) + [0]

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


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

Submission status: Accepted

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

for i in range(1, n + 1):
    diff = (abs(plan[i - 1] - plan[i]) + abs(plan[i] - plan[i + 1])) - abs(plan[i - 1] - plan[i + 1])
    print(cost - diff)

Submission status: Accepted

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

for i in range(1, N+1):
    left = locate[i] - locate[i-1]
    right = locate[i+1] - locate[i]
    if left*right < 0:
        print(ALL - 2 * min(abs(left), abs(right)))
    else:
        print(ALL)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy
import bisect

if __name__ == "__main__":
    n = int(input())
    A =[0] +  list(map(int,input().split())) + [0]
    prf = [0]
    for i in range(1,n+2):
        d = abs(A[i] - A[i-1])
        prf.append(prf[i-1] + d)
    for i in range(1,n+1):
        ans = prf[-1] - abs(A[i] - A[i-1]) - abs(A[i+1] - A[i]) + abs(A[i+1]-A[i-1])
        print(ans)

Submission status: Accepted

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

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

for i in range(1, len(a)-1):
    tmp = s + 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_list = [0] + [int(x) for x in input().split()] + [0]
d1_list = [abs(a_list[i + 1] - a_list[i]) for i in range(n + 1)]
d2_list = [abs(a_list[i + 2] - a_list[i]) for i in range(n)]
travel_all = sum(d1_list)
for i in range(n):
    print(travel_all - d1_list[i] - d1_list[i + 1] + d2_list[i])

Submission status: Accepted

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

for i in range(1, n+1):
  if (a0[i]-a0[i-1])*(a0[i+1]-a0[i]) >= 0:
    print(suma)
  else:
    print(suma-abs(a0[i]-a0[i-1])-abs(a0[i+1]-a0[i])+abs(a0[i+1]-a0[i-1]))

Submission status: Accepted

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

costs = [0]
for a, b in zip(A[:-1], A[1:]):
    costs.append(abs(a-b))

from itertools import accumulate
costs_acc = list(accumulate(costs))

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

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(len(A)-1):
    cost.append(abs(A[i]-A[i+1]))
a = sum(cost)
for i in range(1,N+1):
    print(a-abs(A[i]-A[i-1])-abs(A[i+1]-A[i])+abs(A[i+1]-A[i-1]))

Submission status: Accepted

Solution code:
import copy
def main():
    N = int(input())
    A = [0] + [int(a) for a in input().split()]

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

    for i in range(1, N + 1):
        S = copy.deepcopy(total)

        if i != N:
            S -= (abs(A[i] - A[i-1]) + abs(A[i] - A[i + 1]))
            S += abs(A[i - 1] - A[i + 1])

        else:
            S -= (abs(A[i] - A[i - 1]) + abs(A[i] - 0))
            S += abs(A[i - 1] - 0)

        print(S)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())

A = list(map(int,input().split()))
A.append(0)

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

for i in range(n):
    if i == 0:
        s = S + abs(A[1]-0) - (abs(0-A[0])+abs(A[0]-A[1]))
        print(s)
    else:
        s = S + abs(A[i+1]-A[i-1]) - (abs(A[i-1]-A[i])+abs(A[i]-A[i+1]))
        print(s)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

N = INT()
A = LIST()

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

total = sum([abs(A[i]-A[i-1]) for i in range(1, N+2)])
for i in range(1, N+1):
	print(total - 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()))
road = [0] + a + [0]
allcost = 0
for i in range(n+1):
    allcost += abs(road[i+1] - road[i])

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

all_p = abs(a_list[0]) + abs(a_list[-1])
p_list = [0]*(n+1)
p_list[0] = abs(a_list[0])
p_list[-1] = abs(a_list[-1])
for i in range(len(a_list)-1):
    all_p += abs(a_list[i] - a_list[i+1])
    p_list[i+1] = abs(a_list[i] - a_list[i+1])
    
a_list.insert(0 , 0)
a_list.append(0)
for i in range(len(a_list)-2):
    print(all_p - p_list[i] -p_list[i+1] + abs(a_list[i] - a_list[i+2]))

Submission status: Accepted

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

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

for i in range(1,N+1):
    print(ans+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()))
tot = sum([abs(A[i] - A[i - 1]) if i != 0 else abs(A[0]) for i in range(n)]) + abs(A[-1])
A = [0, ] + A + [0, ]
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(tot)
    else:
        print(tot - (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)
ans = []
n = len(A)
S = 0
for i in range(1,n):
    S += abs(A[i]-A[i-1])

for i in range(1,n-1):
    sum = S + abs(A[i+1]-A[i-1])-abs(A[i-1]-A[i])-abs(A[i]-A[i+1])
    ans.append(sum)

for i in ans:
    print(i)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
A = [0] + A + [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):
    reg = S - abs(A[i-1] - A[i]) - abs(A[i] - A[i+1]) + abs(A[i-1] - A[i+1])
    print(reg)

Submission status: Accepted

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

import sys
# import time
# import math
# import numpy as np
# import scipy.sparse.csgraph as cs            # csgraph_from_dense(ndarray, null_value=inf), bellman_ford(G, return_predecessors=True), dijkstra, floyd_warshall
# import random                                # random, uniform, randint, randrange, shuffle, sample
# import string                                # ascii_lowercase, ascii_uppercase, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from datetime import date, datetime          # date.today(), date(year,month,day) => date obj; datetime.now(), datetime(year,month,day,hour,second,microsecond) => datetime obj; subtraction => timedelta obj
# from datetime.datetime import strptime       # strptime('2019/01/01 10:05:20', '%Y/%m/%d/ %H:%M:%S') returns datetime obj
# from datetime import timedelta               # td.days, td.seconds, td.microseconds, td.total_seconds(). abs function is also available.
# from copy import copy, deepcopy              # use deepcopy to copy multi-dimentional matrix without reference
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from fractions import gcd                    # for Python 3.4 (previous contest @AtCoder)



def main():
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79...e+308
    # inf = 2 ** 64 - 1             # (for fast JIT compile in PyPy) 1.84...e+19
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    n = ii()
    L = lmi()
    L = [0] + L + [0]
    default = sum([abs(L[i] - L[i-1]) for i in range(1, n+2)])

    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(default)
        elif L[i] >= L[i-1] and L[i] >= L[i+1]:
            print(default - 2 * (L[i] - max(L[i-1], L[i+1])))
        else:
            print(default - 2 *  (min(L[i+1], L[i-1]) - L[i]))



if __name__ == "__main__":
    main()

Submission status: Accepted

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

total = abs(A[0]) + abs(A[n-1])

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

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
import fractions
import copy
from itertools import permutations
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

N = getN()
A = [0] + getList() + [0]

# dp[i + 1] i番目まで抜かさずに行った時の距離の合計
dp = [0]
sta = A[0]
for i in range(1, N + 2):
    dist = abs(A[i] - sta)
    dp.append(dp[-1] + dist)
    sta = A[i]
for i in range(N):
    opt1 = dp[i]
    opt2 = abs(A[i + 2] - A[i])
    opt3 = dp[-1] - dp[i + 2]
    print(opt1 + opt2 + opt3)

Submission status: Accepted

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

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

s.insert(0,0)
s.insert(n+1,0)
for i in range(1,n+1):#とばす番号
    tmp_ans = ans
    a = s[i-1]
    tmp = s[i]
    b = s[i+1]
    if (b >= tmp and tmp >= a) or (b <= tmp and tmp <= a):
        pass
    else:
        if (a >= b and tmp > a) or (a <= b and tmp < a):
            tmp_ans -= abs(2*(tmp-a))
        else:
            tmp_ans -= abs(2*(tmp-b))
    print(tmp_ans)

Submission status: Accepted

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

dis=0
d=[0]*n

for i in range(1,n+1):
    dis+=abs(a[i]-a[i-1])
    if a[i+1]<a[i] and a[i-1]<a[i]:
        d[i-1]=2*(a[i]-max(a[i-1],a[i+1]))
    if a[i+1]>a[i] and a[i-1]>a[i]:
        d[i-1]=2*(min(a[i-1],a[i+1])-a[i])
dis+=abs(a[n+1]-a[n])

for i in range(n):
    print(dis-d[i])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n = n//a
    count += 1
  if a**count == n:
    return count
  else:
    return count+1

#############
# Main Code #
#############

N = I()
A = IL()

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

S = sum(D)
for i in range(N):
  P(S-D[i]-D[i+1]+C[i])

Submission status: Accepted

Solution code:
N = int(input())
a = [int(i) for i in 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):
    output = S + abs(a[i+1] - a[i-1]) - abs(a[i]-a[i-1]) - abs(a[i+1] - a[i])
    print(output)

Submission status: Accepted

Solution code:
# coding: utf-8
# hello worldと表示する
#dpでできないかな？
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7


n=I()
lis=LI()
lis.insert(0,0)
lis.append(0)
ones=[]
twos=[]
for i in range(n+1):
    ones.append(abs(lis[i+1]-lis[i]))
    if i!=n:
        twos.append(abs(lis[i+2]-lis[i]))
#print(ones)
#print(twos)
u=sum(ones)
for i in range(n):
    ans=u-ones[i]-ones[i+1]+twos[i]
    print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split(' ')))
    full = 0
    for i in range(N + 1):
        if i == 0:
            full += abs(A[0])
        elif i == N:
            full += abs(A[-1])
        else:
            full += abs(A[i] - A[i - 1])
    diff = [0 for _ in range(N)]
    for i in range(N):
        if i == 0:
            diff[0] = abs(A[1]) - (abs(A[1] - A[0]) + abs(A[0]))
        elif i == N - 1:
            diff[N - 1] = abs(A[-2]) - (abs(A[-2] - A[-1]) + abs(A[-1]))
        else:
            diff[i] = abs(A[i + 1] - A[i - 1]) - (abs(A[i + 1] - A[i]) + abs(A[i] - A[i - 1]))
    for d in diff:
        print(full + d)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def resolve():
	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):
		ta = abs(a[i+1] - a[i-1])
		tb = abs(a[i] - a[i-1]) + abs(a[i+1] - a[i])
		print(s - (tb-ta))
resolve()

Submission status: Accepted

Solution code:
def main():
  N = int(input())
  A = [0]
  A.extend(list(map(int, input().split())))
  A.append(0)
  ans_def = 0
  for i in range(1, N+2):
    ans_def += abs(A[i]-A[i-1])
  for i in range(1, N+1):
    print(ans_def-abs(A[i-1]-A[i])-abs(A[i+1]-A[i])+abs(A[i+1]-A[i-1]))
if __name__ == "__main__":
  main()

Submission status: Accepted

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