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:
input()
a = [0] + list(map(int, input().split())) + [0]
a = [j - i for i, j in zip(a, a[1:])]
x = sum(abs(i) for i in a)
for i, j in zip(a, a[1:]):
    if i * j >= 0:
        print(x)
    else:
        print(x - 2 * (min(abs(i), abs(j))))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = map(int, read().split())

    B = [0]
    B.extend(A)
    B.append(0)

    M = len(B)

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

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

    print(*ans, sep='\n')
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

ans_lst = [total_fee] * N
for j in range(N):
    ans_lst[j] -= abs(A[j+1]-A[j])
    ans_lst[j] -= abs(A[j+2]-A[j+1])
    ans_lst[j] += abs(A[j+2]-A[j])
    print(ans_lst[j])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    inlis = list(map(int, input().split()))

    total = 0
    mae = 0

    for i in range(n):
        total += abs(inlis[i] - mae)
        mae = inlis[i]
    total += abs(inlis[-1])
    inlis.append(0)

    #i = 0
    if 0 <= inlis[0] and inlis[0] > inlis[1]:
        if inlis[1] >0:
            print(total - 2 * abs(inlis[1] - inlis[0]))
        else:
            print(total - 2 * abs(inlis[0]))
    elif 0 >= inlis[0] and inlis[0]< inlis[1]:
        if inlis[1] < 0:
            print(total - 2 * abs(inlis[1] - inlis[0]))
        else:
            print(total - 2 * abs(inlis[0]))
    else:
        print(total)

    for j in range(1, n):
        if inlis[j-1] <= inlis[j] <= inlis[j+1] or inlis[j-1]>= inlis[j] >= inlis[j+1]:
            print(total)
        elif inlis[j-1] <= inlis[j] and inlis[j] > inlis[j+1]:
            if inlis[j-1] >= inlis[j+1]:
                print(total - 2 * abs(inlis[j-1] - inlis[j]))
            else:
                print(total- 2 * abs(inlis[j+1] - inlis[j]))
        elif inlis[j-1]>= inlis[j] and inlis[j] < inlis[j+1]:
            if inlis[j-1] >= inlis[j+1]:
                print(total - 2 * abs(inlis[j+1] - inlis[j]))
            else:
                print(total - 2 * abs(inlis[j-1] - inlis[j]))
    



        


if __name__ == "__main__":
    main()

Submission status: Accepted

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

for j in range(n):
    ans=temp-b[j]-b[j+1]+abs(a[j+2]-a[j])
    print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N=int(input())
A=np.array(list(map(int,input().split())))
A=np.append(A,0)
A=np.insert(A,0,0)
S=sum(abs(np.diff(A[:])))

for i in range(1,len(A)-1):
  if A[i-1]==A[i]:
    print(S)
  elif (A[i-1]<A[i] and A[i]<A[i+1]) or (A[i-1]>A[i] and 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())
X = list(map(int, input().split()))
X.append(0)
load = 0
now = 0
for i in range(N + 1):
  load += abs(X[i] - now)
  now = X[i]
  
#print(load)  

now = 0
for i in range(N):
  if now <= X[i] <= X[i + 1]:
    print(load)
  elif X[i + 1] <= X[i] <= now:
    print(load)
  else:
    ll = 2 * min(abs(X[i] - now), abs(X[i] - X[i + 1]))
    print(load - ll)
  now = X[i]

Submission status: Accepted

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

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

now=0
for i in range(N):
  if i != N-1:
    diff = abs(A[i+1]-now)-abs(A[i]-now)-abs(A[i+1]-A[i])
    now = A[i]
  else:
    diff = abs(0-now)-abs(A[i]-now)-abs(0-A[i])
  print(SUM + diff)

Submission status: Accepted

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

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

prev = 0
A.append(0)
for i in range(N):
    x = abs(A[i + 1] - prev)
    y = abs(A[i] - prev) + abs(A[i + 1] - A[i])
    print(cost - (y - x))
    prev = A[i]

Submission status: Accepted

Solution code:
n,ais,cumul,res =int(raw_input()),[0] + map(int,raw_input().split()) + [0],0,[]
for i in range(1,len(ais)):
	if i < len(ais) -1: res.append(-abs(ais[i] - ais[i-1]) - abs(ais[i+1] - ais[i]) + abs(ais[i+1] - ais[i-1]))
	cumul += abs(ais[i] - ais[i-1])
for v in res: print v + cumul

Submission status: Accepted

Solution code:
n = int(raw_input())
ais =[0] + map(int,raw_input().split()) + [0]
skip = -float('inf')
cumul = 0
res  = []
for i in range(1,len(ais)-1):
	val = abs(ais[i] - ais[i-1])
	skip = -abs(ais[i] - ais[i-1]) - abs(ais[i+1] - ais[i]) + abs(ais[i+1] - ais[i-1])
	res.append(skip)
	cumul += val
cumul += abs(ais[-1] - ais[-2])
for i in range(len(res)):
	print res[i] + cumul

Submission status: Accepted

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

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

total +=  abs(a[0]) + abs(a[-1])

a.insert(0, 0)
a.append(0)
for i in range(1,n+1):
    if a[i-1] <= a[i] and a[i] <= a[i+1]:
        print(total)
    elif a[i+1] <= a[i] and a[i] <= a[i-1]:
        print(total)
    elif max(a[i-1], a[i+1]) < a[i]:
        print(total - abs(a[i] - max(a[i-1], a[i+1]))*2)
    elif a[i] < min(a[i-1], a[i+1]):
        print(total - abs(a[i] - min(a[i-1], a[i+1]))*2)

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

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

for i in range(1,len(a)-1):
    if a[i-1] < a[i] < a[i+1]:
        ans = cost
    elif a[i-1] > a[i] > a[i+1]:
        ans = cost
    else:
        ans = cost - abs(a[i-1] - a[i]) - abs(a[i] - a[i+1]) + abs(a[i-1] - a[i+1])
    print(ans)

Submission status: Accepted

Solution code:
N=int(input())+2
A=[0]
A.extend(list(map(int,input().split())))
A.append(0)
B=sum(abs(A[i]-A[i-1]) for i in range(1,N))
for i in range(1,N-1):
  print(B-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]+[int(X) for X in input().split()]+[0]
Cost = sum(abs(A[T+1]-A[T]) for T in range(N+1))
for T in range(1,N+1):
    Cut = Cost+abs(A[T+1]-A[T-1])-abs(A[T]-A[T-1])-abs(A[T+1]-A[T])
    print(Cut)

Submission status: Accepted

Solution code:
N = int(input())
A = [0]+list(map(int,input().split()))+[0]
B = [abs(a-b) for a,b in zip(A,A[1:])]
C = sum(B)

for n in range(N):
  print(C+abs(A[n]-A[n+2])-(B[n]+B[n+1]))

Submission status: Accepted

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

ans = 0
for i in range(N+1):#当初の計画
    ans+=abs(A[i+1]-A[i])

for i in range(1,N+1,1):#やめること
    sabun= -abs(A[i]-A[i-1])-abs(A[i+1]-A[i])
    sabun += abs(A[i+1]-A[i-1])
    print(ans+sabun)

Submission status: Accepted

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

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

dist_cum = [0] * (N+2)
for i in range(N+1):
    dist_cum[i+1] = dist_cum[i] + dist[i+1]

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

Submission status: Accepted

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

for i in a:
    total += abs(i-pre)
    pre = i

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

Submission status: Accepted

Solution code:
from copy import deepcopy
n=int(input())
A=list(map(int, input().split()))
B=[0]+deepcopy(A)+[0]
total=0
b=0
for a in A:
    total+=abs(a-b)
    b=a
total+=abs(b)
for i in range(1,n+1):
    z,x,c=B[i-1], B[i], B[i+1]
    print(total-abs(x-z)-abs(c-x)+abs(c-z))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect

sys.setrecursionlimit(10**6)
def zz():
    return list(map(int, sys.stdin.readline().split()))


def z():
    return int(sys.stdin.readline())


def S():
    return sys.stdin.readline()


def C(line):
    return [sys.stdin.readline() for _ in range(line)]


N=z()
A = zz()
A.append(0)
diff = [0]*N
prev = 0
A_ = [0]
A_.extend(A)
sum_ = 0
for i in range(len(A_)-1):
    sum_ += abs(A_[i] - A_[i + 1])
    
for i in (range(1,N+1)):
    sub = abs(A_[i]-A_[i-1]) + abs(A_[i] - A_[i+1])
    add = abs(A_[i - 1] - A_[i + 1])
    print(sum_ + add - sub)

Submission status: Accepted

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

res = 0

al.append(0)
al.insert(0,0)

for i in range(n+1):
    res += abs(al[i+1]-al[i])

for j in range(1,n+1):
    if (al[j+1]-al[j]) * (al[j]-al[j-1]) >= 0:
        print(res)
    else:
        print(res - min(abs(al[j+1]-al[j]),abs(al[j]-al[j-1]))*2)

Submission status: Accepted

Solution code:
N = int(input())
# 0スタート0エンドなので付け加えておく
a_li = [0] + list(map(int, input().split())) + [0]
total = 0
for i in range(1, len(a_li)):
    total += abs(a_li[i] - a_li[i - 1])
for i in range(1, len(a_li) - 1):
    # a[i-1], a[i], a[i+1]のうちa[i]が消えたら…
    print(total - abs(a_li[i] - a_li[i - 1]) - abs(a_li[i + 1] - a_li[i]) + abs(a_li[i + 1] - a_li[i - 1]))

Submission status: Accepted

Solution code:
N = int(input())
a_li = list(map(int, input().split()))
# 最初と最後を加えておく
total = abs(a_li[0]) + abs(a_li[-1])
for i in range(1, N):
    total += abs(a_li[i] - a_li[i - 1])
# 最初を取り除く
# 0, a[0], a[1]のうちa[0]が消えたら…
print(total - abs(a_li[0]) - abs(a_li[0] - a_li[1]) + abs(a_li[1]))
for i in range(1, N - 1):
    # a[i-1], a[i], a[i+1]のうちa[i]が消えたら…
    print(total - abs(a_li[i] - a_li[i - 1]) - abs(a_li[i + 1] - a_li[i]) + abs(a_li[i + 1] - a_li[i - 1]))
# 最後を取り除く
# a[-2], a[-1], 0のうちa[-1]が消えたら…
print(total - abs(a_li[-1]) - abs(a_li[-1] - a_li[-2]) + abs(a_li[-2]))

Submission status: Accepted

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

A_sum=0
a_start=0
for a in A:
    A_sum += abs(a_start-a)
    a_start=a
a=0
A_sum += abs(a_start-a)

A_=[0]
A_.extend(A)
A_.append(0)
for a_idx in range(1,len(A_)-1):
    a_before = A_[a_idx-1]
    a_ = A_[a_idx]
    a_after = A_[a_idx+1]
    print(A_sum-(abs(a_-a_before)+abs(a_-a_after)-abs(a_before-a_after)))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n=ni()
a=nl()

a = [0]+a+[0]

d=[]

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

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

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


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    A = [0] + il() + [0]
    M = 0
    for n in range(N + 1):
        M += abs(A[n] - A[n + 1])

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from copy import deepcopy
def main():
    n, *a = map(int, read().split())

    if n == 2:
        r = [abs(a[1] * 2), abs(a[0] * 2)]
        print(*r, sep='\n')
        sys.exit()

    amin = min(a)
    # 全aiを移動するコストを出す
    # 便宜のため、aの前後に0座標を追加する。
    a.insert(0, 0)
    a.append(0)
    # 単純化のため、aにマイナス座標がある(＝左の端がマイナス)なら、
    # 左の端＝座標0のコピーをつくる。マイナスではないなら単純コピー。
    if amin < 0:
        a2 = [i - amin for i in a] # aminが正負どちらでもこれでOK
    else:
        a2 = deepcopy(a)
    # aを全て回った場合のコストを出す
    cost = 0
    for i1 in range(n + 1):
        cost += abs(a2[i1] - a2[i1 + 1])
    # rに答を格納する。aの前後に１要素足しているので長さを合わせる。
    r = [cost] * (n + 2)
    # a[i]それぞれをパスする場合のコストを出す。場合分けする。
    for j in range(1, n + 1):
        if a2[j - 1] <= a2[j] <= a2[j + 1]:
            continue
        elif a2[j - 1] >= a2[j] >= a2[j + 1]:
            continue
        elif a2[j - 1] <= a2[j] and a2[j + 1] <= a2[j]:
            nearer_dis = min(abs(a2[j - 1] - a2[j]), abs(a2[j] - a2[j + 1]))
            r[j] -= nearer_dis * 2
        else:
            nearer_dis = min(abs(a2[j] - a2[j - 1]), abs(a2[j + 1] - a2[j]))
            r[j] -= nearer_dis * 2
    print(*r[1:-1], sep='\n')


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

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

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = [0] + list(map(int, input().split())) + [0]
    cost = 0
    pre = 0
    for a in A[1:]:
        cost += abs(a - pre)
        pre = a

    for i, a in enumerate(A):
        if i == 0 or i == N + 1: continue
        print(cost - abs(a - A[i - 1]) - abs(a - A[i + 1]) + 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()))

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

for i in range(1, len(a)-1):
    if a[i-1] <= a[i] <= a[i+1] or a[i-1] >= a[i] >= a[i+1]:
        print(d)
    else:
        print(d - 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())
lst=list(map(int,input().split()))
idou=[abs(lst[0])]
for i in range(1,n):
    idou.append(abs(lst[i]-lst[i-1]))
idou.append(abs(lst[-1]))
kyori=sum(idou)


kyo=kyori-(idou[0]+idou[1])
kyo+=abs(lst[1])
print(kyo)

for i in range(1,n-1):
    kyo=kyori-(idou[i]+idou[i+1])
    kyo+=(abs(lst[i+1]-lst[i-1]))
    print(kyo)

kyo=kyori-(idou[-1]+idou[-2])
kyo+=abs(lst[-2])
print(kyo)

Submission status: Accepted

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

N = int(input())
A = list(map(int,input().split()))
b = [0]
for i in range(N):
    b.append(A[i])
b.append(0)

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

for j in range(1,N+1):
    sumc = sumb
    sumc = sumc - abs(b[j+1]-b[j])-abs(b[j]-b[j-1])+abs(b[j+1]-b[j-1])
    print(sumc)

Submission status: Accepted

Solution code:
n=int(input())
l=[0]+list(map(int,input().split()))+[0]
d=[abs(a-b) for a,b in zip(l,l[1:])]
m=sum(d)
for i in range(n):
    print(m+abs(l[i]-l[i+2])-(d[i]+d[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):
    if (a[i-1] <= a[i] <= a[i+1]) or (a[i-1] >= a[i] >= a[i+1]):
        print(ans)
    else:
        print(ans-2*min(abs(a[i-1]-a[i]), abs(a[i]-a[i+1])))

Submission status: Accepted

Solution code:
n=int(input())
#a,b=map(int,input().split())
al=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]
d=[0]
d=d+al
d.append(0)
totald=0
for i in range(1,n+2):
    totald+=abs(d[i]-d[i-1])

for i in range(1,n+1):
    minus=abs(d[i]-d[i-1])+abs(d[i+1]-d[i])
    add=abs(d[i+1]-d[i-1])
    print(totald-minus+add)

Submission status: Accepted

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

    cumsum = [0] * (n+1)
    for i in range(1, n+2):
        cumsum[i-1] = abs(As[i] - As[i-1])
    cumsum_sum = sum(cumsum)
    for i in range(1, n+1):
        ans = cumsum_sum - cumsum[i-1] - cumsum[i] + abs(As[i+1] - As[i-1])
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    N = in_n()
    A = [0] + in_nl() + [0]
    N = N + 2

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

    ans = [0] * (N - 2)
    for i in range(1, N - 1):
        if A[i - 1] <= A[i] and A[i] <= A[i + 1]:
            ans[i - 1] = dsum
        elif A[i - 1] >= A[i] and A[i] >= A[i + 1]:
            ans[i - 1] = dsum
        elif A[i - 1] <= A[i] and A[i] >= A[i + 1]:
            ans[i - 1] = dsum - abs(A[i - 1] - A[i]) - abs(A[i] - A[i + 1]) + abs(A[i - 1] - A[i + 1])
        elif A[i - 1] >= A[i] and A[i] <= A[i + 1]:
            ans[i - 1] = dsum - abs(A[i - 1] - A[i]) - abs(A[i] - A[i + 1]) + abs(A[i - 1] - A[i + 1])

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

B=[0]+A+[0]
count=[0]*N
S=abs(B[1]-0)
i=1
while i<=N:
    if B[i-1]<=B[i]<=B[i+1] or B[i-1]>=B[i]>=B[i+1]:
        count[i-1]=0
    else:
        count[i-1]=2*min(abs(B[i]-B[i-1]),abs(B[i+1]-B[i]))
    S+=abs(B[i+1]-B[i])
    i+=1

for c in count:
    print(S-c)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
A.append(0)
sum=0
for i in range(N+1):
  if i==0:
    sum+=abs(A[0])
  else:
    sum+=abs(A[i-1]-A[i])
for i in range(N):
  if i==0:
    print(sum+(abs(A[1])-abs(A[1]-A[0])-abs(A[0])))
  else:
    print(sum+(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, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

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


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    A = [0] + il() + [0]
    sm = 0
    for n in range(1, N + 2):
        sm += abs(A[n - 1] - A[n])

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

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


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    A = [0] + il() + [0]
    mx = max(A)
    mn = min(A)
    sm = 0
    for n in range(1, N + 2):
        sm += abs(A[n - 1] - A[n])

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
spot = [0] + list(map(int, input().split())) + [0]
plst = [0] * n
dissum = 0
for i in range(1, n + 1):
  plst[i - 1] = (abs(spot[i + 1] - spot[i - 1])) - (abs(spot[i + 1] - spot[i]) + abs(spot[i] - spot[i - 1]))
  dissum += abs(spot[i] - spot[i - 1])
dissum += abs(spot[n])
for i in range(n):
  print(dissum + plst[i])

Submission status: Accepted

Solution code:
n = int(input())
spot = [0] + list(map(int, input().split())) + [0]
mlst = []
plst = []
dissum = 0
for i in range(1, n + 1):
  mlst.append(abs(spot[i + 1] - spot[i]) + abs(spot[i] - spot[i - 1]))
  plst.append(abs(spot[i + 1] - spot[i - 1]))
  dissum += abs(spot[i] - spot[i - 1])
dissum += abs(spot[n])
for i in range(n):
  print(dissum - mlst[i] + plst[i])

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
A = [0] + A + [0]
B = []
for i in range(1,len(A)):
    B.append(abs(A[i]-A[i-1]))
S = sum(B)
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:
import sys

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def main():
    N = ii()
    A = [0]+list(mi())+[0]
    S = sum(abs(A[i]-A[i+1]) for i in range(N+1))
    l = [S-abs(A[i]-A[i+1])-abs(A[i+1]-A[i+2])+abs(A[i]-A[i+2]) for i in range(N)]
    print(*l, sep='\n')


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

N = i()
A = [0]+l()+[0]
d = []
for i in range(N+1):
    d.append(abs(A[i+1]-A[i]))
ans = sum(d)
for i in range(N):
    print(ans+abs(A[i]-A[i+2])-d[i]-d[i+1])

Submission status: Accepted

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

Submission status: Accepted

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

full_root = [0] + A + [0]
full_place = [abs(full_root[i] - full_root[i + 1]) for i in range(N + 1)]
full_res = sum(full_place)
# check
for ind in range(1, N + 1):
    res = full_res - sum(full_place[ind - 1:ind + 1]) + abs(full_root[ind - 1] - full_root[ind + 1])

    print(res)

Submission status: Accepted

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

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

#a1に行かないとき
sm1=sm_al-abs(a[1]-a[0])-abs(a[0])+abs(a[1])
print(sm1)
#a2-an-1に行かないとき
for i in range(1,n-1):
    sm=sm_al-abs(a[i+1]-a[i])-abs(a[i]-a[i-1])+abs(a[i+1]-a[i-1])
    print(sm)
#anに行かない
smn=sm_al-abs(a[n-1]-a[n-2])-abs(a[n-1])+abs(a[n-2])
print(smn)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n, *a = map(int, read().split())

l = [0,*a,0]


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

for i in range(1, n + 1):
    delcost = abs(l[i + 1] - l[i]) + abs(l[i] - l[i - 1])
    addcost = abs(l[i + 1] - l[i - 1])
    print(s - delcost + addcost)

Submission status: Accepted

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

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

Submission status: Accepted

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

d_1 = []
for i in range(1, n+2):
  d_1.append(abs(a[i]-a[i-1]))
d_2 = []
for i in range(1, n+1):
  d_2.append(abs(a[i+1]-a[i-1]))

s_d = sum(d_1)
for i in range(1, n+1):
  print(s_d - d_1[i-1] - d_1[i] + d_2[i-1])