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())
s=list(map(int,input().split()))
s.insert(0,0)
s.append(0)
ans=0
for k in range(N+1):
    ans+=abs(s[k]-s[k+1])
for i in range(N):
    a,b,c=s[i]-s[i+1],s[i+1]-s[i+2],s[i]-s[i+2]
    if a*b>=0:
        print(ans)
    else:
        print(ans-abs(a)-abs(b)+abs(c))

Submission status: Accepted

Solution code:
from collections import deque

N = int(input())
A = deque(map(int, input().split()))

A.append(0)
A.appendleft(0)

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

for i in range(N):
    print(A_sum - (A_diff[i] + A_diff[i+1]) + abs(A[i+2] - A[i]))

Submission status: Accepted

Solution code:
import numpy as np
N=int(input())
A=list(map(int,input().split()))

A.insert(0,0)
A.append(0)
A=np.array(A)
S=np.sum(np.abs(np.diff(A)))

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

Submission status: Accepted

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

N = int(input())

a = [i for i in map(int, input().split())]

a = [0] + a + [0]

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

for i in range(1, N+1):
    print(total + abs(a[i-1] - a[i+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(" ")))
s = 0
a.insert(0, 0)
a.insert(n+1, 0)
for j in range(n+1):
    s += abs(a[j] - a[j+1])
for i in range(1, n+1):
    s_i = s + abs(a[i-1]-a[i+1]) - ((abs(a[i-1] - a[i]) + abs(a[i]-a[i+1])))
    print(s_i)

Submission status: Accepted

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

n = int(input())
spot = [int(x) for x in input().split()]
spot = [0] + spot + [0]
cost = 0
for i in range(len(spot)-1):
    cost += abs(spot[i] - spot[i+1])
change = 0
for k in range(len(spot)-2):
    if (spot[k+1]-spot[k])*(spot[k+2]-spot[k+1]) >= 0:
        change = 0
    else:
        change = 2*min([abs(spot[k+1]-spot[k]),abs(spot[k+2]-spot[k+1])])
    print(cost-change)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#abc092c

n=int(raw_input())
a=map(int,raw_input().split())
a.append(0)
a.insert(0,0)
s=[]
for i in xrange(n+1):
 s.append(abs(a[i+1]-a[i]))
ss=sum(s)
for i in xrange(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()))
A = [0] + A + [0]
total_dist = 0
now = 0
for A_i in A:
    total_dist += abs(A_i - now)
    now = A_i

def solve():
    for i in range(N):
        print(total_dist+abs(A[i] - A[i+2])-abs(A[i]-A[i+1])-abs(A[i+1]-A[i+2]))


solve()

Submission status: Accepted

Solution code:
distanceSum = 0
spotCount = int(input())
spotList = input().split()
spotList = [int(i) for i in spotList]
spotList.append(0)

def calcDistance(num): # 配列num-1番のスポットから配列num番目のスポットへの距離を計算
    if num == 0: #開始地点から配列0番＝最初のスポット
        return abs(spotList[num])
    elif num == spotCount: #最後スポットから開始地点
        return abs(spotList[num-1])
    else:
        return abs(spotList[num]-spotList[num-1])

def calcSkipEffect(num): #配列num番をスキップした時の移動距離への影響を計算
    if num == 0: #最初のスポットの時だけの例外
        return abs(spotList[num+1])-abs(spotList[num])-abs(spotList[num+1]-spotList[num])
    else:
        return abs(spotList[num+1]-spotList[num-1])-abs(spotList[num]-spotList[num-1])-abs(spotList[num+1]-spotList[num])

for i in range(spotCount+1): #総移動回数はスポット数+1
    distanceSum += calcDistance(i)
    
for j in range(spotCount): #配列0～n-1番までの各スポットについて実行
    print(str(distanceSum + calcSkipEffect(j)))

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
if __name__ == "__main__":
    n = int(input())
    a = [0]+list(map(int, input().split()))
    factor = sum([ abs(a[tmp]-a[tmp+1]) for tmp in range(n) ]) + abs(a[-1]-a[0])
    for position in range(1,n+1):
        if position != n:
            difference = abs(a[position-1]-a[position])+abs(a[position]-a[position+1])
            ans = factor-difference+abs(a[position-1]-a[position+1])
        else:
            difference = abs(a[position-1]-a[position])+abs(a[position]-a[0])
            ans = factor-difference+abs(a[position-1]-a[0])
        print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

cost_sum=0

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

n = int(raw_input())
spot = [0] + map(int, raw_input().split()) + [0]

all_sum = 0
for j in range(n+1):
    all_sum = all_sum + abs(spot[j+1] - spot[j])

for i in range(n):
    ans = all_sum - abs(spot[i+1] - spot[i]) - abs(spot[i+2] - spot[i+1]) + abs(spot[i+2] - spot[i])
    print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


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

Submission status: Accepted

Solution code:
n = int(input())
a = str(input()).split()

for i in range(0,n):
    a[i] = int(a[i])
    
def abs(c): 
    if c > 0: 
        return c 
    else: 
        return -c

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


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

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

print(pre-abs(a[n-1]-a[n-2])-abs(a[n-1])+abs(a[n-2]))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

sum_cost = [0 for i in range(0, n + 2)] # sum_cost[i] := i番目の位置につくまでにかかるコスト

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


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

Submission status: Accepted

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

Submission status: Accepted

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

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

visited = []
for i in range(1, N + 1):
    a = abs(A[i - 1] - A[i + 1])
    b = abs(A[i] - A[i - 1]) + abs(A[i] - A[i + 1])
    visited.append(total + (a - b))

for v in visited:
    print(v)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

for i in range(1,len(A)):
    wa = wa + abs(A[i-1] - A[i])
    rem1.append(abs(A[i-1]-A[i]))
    rem2.append(abs(A[i]- A[i+1]) if i+1 < len(A) else 0)
    add.append(abs(A[i-1]-A[i+1]) if i+1 < len(A) else abs(A[i-1]))

for j in range(0,len(A)-2):
    print(wa-rem1[j]-rem2[j]+add[j])

Submission status: Accepted

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

for i in range(1, N + 1):
    abc = abs(A[i - 1] - A[i]) + abs(A[i] - A[i + 1])
    ac = abs(A[i - 1] - A[i + 1])
    print(ALL - abc + ac)

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

Submission status: Accepted

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

#2. make distance list
a.append(0)
a.insert(0,0)
costs = []

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

#3. calculate each cost
total = sum(costs)
for i in range(n):
    print(total - costs[i] - costs[i+1] + abs(a[i+2]-a[i]))

Submission status: Accepted

Solution code:
n = int(input())
array = [int(x) for x in input().split()]

tmp_res = 0
tmp_ici = 0
tmp_array = []
for i in array:
    tmp_array.append(abs(tmp_ici-i))
    tmp_ici = i
tmp_array.append(abs(tmp_ici-0))
tmp_sum = sum(tmp_array)

array = [0] + array + [0]
for i in range(n):
    c = array[i]
    cc = array[i+2]
    a = sum(tmp_array[i:i+2])
    print(tmp_sum-a+abs(c-cc))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
input_array = list(map(int,input().split()))
input_array.insert(0,0)
input_array.append(0)
d1 = []
d2 = []

for i in range(N+1):
    d1.append(abs(input_array[i]-input_array[i+1]))

for i in range(N):
    d2.append(abs(input_array[i]-input_array[i+2]))

total= sum(d1)

for i in range(N):
    print(total-(d1[i]+d1[i+1])+d2[i])

Submission status: Accepted

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

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

for i in range(1, N+1):
    x = C[i-1]
    y = C[i]
    z = abs(A[i-1] - A[i+1])
    print(S - x - y + z)

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(N-1):
    all += abs(A[i+1] - A[i])

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

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

Submission status: Accepted

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

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

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

main()

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))

    # 始点と終点を追加
    A = [0] + A
    A = A + [0]
    cum_map = dict()

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

    for i in range(1, N + 1):
        if i == 1:
            d = abs(A[i + 1] - A[0]) + cum_map[(0, N)] - cum_map[
                (0, i + 1)] + abs(A[N] - A[0])
        elif i == N:
            d = abs(A[N - 1] - A[0]) + cum_map[(0, N - 1)]
        else:
            d = cum_map[(0, i - 1)] + abs(A[N] - A[0]) \
                + abs(A[i + 1] - A[i - 1]) \
                + (cum_map[(0, N)] - cum_map[(0, i + 1)])
        print(d)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))

    # N = 6
    # A = [-679, -2409, -3258, 3095, -3291, -4462]

    # N = 3
    # A = [3, 5, -1]

    # 始点を追加
    A = [0] + A
    A = A + [0]
    cum_map = dict()

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

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

    for i in range(1, N + 1):
        if i == 1:
            d = abs(A[i + 1] - A[0]) + cum_map[(0, N)] - cum_map[
                (0, i + 1)] + abs(A[N] - A[0])
        elif i == N:
            d = abs(A[N - 1] - A[0]) + cum_map[(0, N - 1)]
        else:
            d = cum_map[(0, i - 1)] + abs(A[N] - A[0]) \
                + abs(A[i + 1] - A[i - 1]) \
                + (cum_map[(0, N)] - cum_map[(0, i + 1)])
        # print("i:", d)
        print(d)

    # print(cum_map)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())

inp = map(int,input().split(' '))

A = [0] + [int(i) for i in inp] + [0]
s = np.sum(np.abs(np.diff(A,n=1)))

for j in range(1,N+1):
    df = np.abs(A[j+1]-A[j-1]) - np.abs(A[j]-A[j-1]) - np.abs(A[j+1]-A[j])
    print(int(s+df))

Submission status: Accepted

Solution code:
N = int(input())
*A, = map(int, input().split())
A = [0] + A + [0]
s = sum(abs(b-c) for b, c in zip(A, A[1:]))
s += abs(A[2]) - abs(A[1]) - abs(A[2] - A[1])
B = [s]
for i in range(N-1):
    s += (abs(A[i+3] - A[i+1]) + abs(A[i+1] - A[i])) - (abs(A[i+3] - A[i+2]) + abs(A[i+2] - A[i]))
    B.append(s)
print(*B, sep='\n')

Submission status: Accepted

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


    a.append(0)
    a.insert(0, 0)
    costs = []
    for i in range(n + 1):
        costs.append(abs(a[i + 1] - a[i]))

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

main()

Submission status: Accepted

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

a.append(0)
a.insert(0, 0)
costs = []
for i in range(n + 1):
    costs.append(abs(a[i + 1] - a[i]))

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(int(i) for i in input().split())  

sum = 0
for k in range(N):
  if k == 0:
    sum += abs(a[k])
  elif k == (N-1):
    sum += abs(a[k] - a[k-1]) + abs(a[k])
  else:
    sum += abs(a[k] - a[k-1])

for j in range(N):
  if j == 0:
    if (a[j+1] - a[j]) * (a[j] -0) >= 0:
      print(sum)
    else:
      print(sum - 2 * min(abs(a[j]),abs(a[j]-a[j+1])))
  elif j == N-1:  
    if (a[j] - a[j-1]) * (0 - a[j]) >= 0:
      print(sum)
    else:
      print(sum - 2 * min(abs(a[j]-a[j-1]),abs(a[j])))
  else:
    if (a[j] - a[j-1] ) * (a[j+1] - a[j]) >= 0:
      print(sum)
    else:
      print(sum - 2 * min(abs(a[j]-a[j-1]),abs(a[j+1]-a[j])))

Submission status: Accepted

Solution code:
from sys import stdin 
n = int(stdin.readline())
a = [0] + map(int,stdin.readline().split())
ans = 0 
for i in xrange(1,n+1):
    ans += abs(a[i] - a[i-1])
ans += abs(a[n])
b = []
cur = abs(a[2])
for i in xrange(3,n+1):
    cur += abs(a[i] - a[i-1])
cur += abs(a[n])
b.append(cur)

for i in xrange(2,n):
    cur = ans - abs(a[i]-a[i-1]) - abs(a[i] - a[i+1]) + abs(a[i-1]-a[i+1])
    b.append(cur)
cur = 0
for i in xrange(1,n):
    cur += abs(a[i]-a[i-1])
cur += abs(a[n-1])
b.append(cur)
for i in b:
    print i

Submission status: Accepted

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

dA = []
for i in range(N+1):
    dA.append(A[i+1] - A[i])

sum = sum([abs(i) for i in dA])

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

Submission status: Accepted

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


def money(arr):
    now_x = 0
    total_score = 0

    for i in arr:
        total_score += abs(i-now_x)
        now_x = i

    total_score += abs(now_x)
    return total_score


total = money(a)


for i in range(len(a)-2):
    now_point = a[i]
    break_point = a[i+1]
    next_point = a[i+2]

    break_distance = abs(next_point-now_point)
    normal_distance = abs(break_point-now_point) + \
        abs(next_point - break_point)

    if(break_distance == normal_distance):
        print(total)
    else:
        print(total - abs(break_distance-normal_distance))

Submission status: Accepted

Solution code:
def c_traveling_plan(N, A):
    total = 0  # すべての観光スポットを順に回ったときの費用
    a = [0] + A + [0]  # 0から出発して最後に0に帰るためこうする(forループも楽)
    for i in range(N + 1):
        total += abs(a[i] - a[i + 1])
    ans = ''
    for i in range(1, N + 1):
        # a_{i-1}->a_i->a_{i+1} をつなぐ2つの経路の長さ(delta1)を引き、
        # 新しい経路の長さ(delta2)を足す
        delta1 = abs(a[i - 1] - a[i]) + abs(a[i] - a[i + 1])
        delta2 = abs(a[i - 1] - a[i + 1])
        ans += '{0}\n'.format(total - delta1 + delta2)
    return ans[:-1]
  
N = int(input())
A = [int(i) for i in input().split()]
print(c_traveling_plan(N, A))

Submission status: Accepted

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

dA = []
for i in range(N+1):
    dA.append(A[i+1]-A[i])

sum = sum([abs(i) for i in dA])

for i in range(N):
    if dA[i] * dA[i+1] < 0:
        print(sum - abs(dA[i+1]) - abs(dA[i]) + abs(dA[i+1] + dA[i]))
    else:
        print(sum)

Submission status: Accepted

Solution code:
import itertools as it

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

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
a.insert(0, 0)
a.append(0)
d = 0
for j in range(N+1):
    d += abs(a[j+1] - a[j])
# print(d)

for i in range(1,N+1):
    d_pre = a[i] - a[i-1]
    d_nxt = a[i+1] - a[i]
    # print(d_pre,d_nxt)

    if d_pre * d_nxt>=0:
        out = d
    else:
        out = d - 2 * min(abs(d_nxt), abs(d_pre))
    print(out)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
N = input()
A = map(int, input().split())
A = list(A)

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

for i in range(1, len(A) - 1):
    partial_sum = total_sum - abs(A[i + 1] - A[i]) - abs(A[i] - A[i - 1]) + abs(A[i + 1] - A[i - 1])
    print(partial_sum)

Submission status: Accepted

Solution code:
N = int(input())
a = input().split()
i=0
n = 0
sam = abs(int(a[0]))+abs(int(a[N-1]))
for i in range(N-1):
    sam += abs(int(a[i+1]) - int(a[i]))
for n in range(N):
    if n == 0:
        print(sam - abs(int(a[0])) - abs(int(a[1])-int(a[0])) +abs(int(a[1])))
    elif n == N-1:
        print(sam - abs(int(a[N-1])) - abs(int(a[N-2])-int(a[N-1])) +abs(int(a[N-2])))
    else:
        print(sam - abs(int(a[n])-int(a[n-1])) - abs(int(a[n+1])-int(a[n])) +abs(int(a[n+1])-int(a[n-1])))

Submission status: Accepted

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

    f(N, A)


def f(N, A):
    """
    2 ≤ N ≤ 10^5
    −5000 ≤ Ai ≤ 5000 (1≤ i≤ N)

    第一印象: 累積和?
    →不要,合計のみ必要だった
    """
    costs = [abs(A[0])]
    for i in range(1, N):
        cost = abs(A[i] - A[i-1])
        costs.append(cost)

    # 0へ戻る
    cost_N_to_start = abs(A[-1])
    costs.append(cost_N_to_start)
    costs_cumsum = sum(costs)

    for i in range(N):
        total = costs_cumsum
        # skip: i-1 to i and i to i+1
        total -= (costs[i] + costs[i+1])

        # add: i-1 to i+1
        bef, aft = 0, 0
        if i - 1 >= 0:
            bef = A[i-1]
        if i + 1 < N:
            aft = A[i+1]
        total += abs(bef - aft)

        print(total)


if __name__ == '__main__':
    main()

Submission status: Accepted

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


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

for i in range(1, len(A) - 1):
    partial_sum = total_sum - abs(A[i + 1] - A[i]) - abs(A[i] - A[i - 1]) + abs(A[i + 1] - A[i - 1])
    print partial_sum

Submission status: Accepted

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

sum = 0
for j in range(len(A)-1):
    sum += abs(A[j]-A[j+1])
for i in range(N):
    print(sum - abs(A[i] - A[i+1]) - abs(A[i+1] - A[i+2]) + abs(A[i] - A[i+2]))

Submission status: Accepted

Solution code:
def cost(a):
    total = 0
    for i in range(1, len(a)):
        total += abs(a[i] - a[i-1])
    return total

def cancel(a, i, total):
    prev = a[i-1]
    spot = a[i]
    nxt = a[i+1]
    if (nxt >= prev and spot < prev) or (nxt <= prev and spot > prev):
        return total - 2 * abs(prev - spot)
    if (nxt >= prev and spot > nxt) or (nxt <= prev and spot < nxt):
        return total - 2 * abs(nxt - spot)
    return total
        
    
if __name__ == '__main__':
    _ = int(input())
    a = [0] + list(map(int, input().split()))
    a.append(0)
    total = cost(a)
    for i in range(1, len(a)-1):
        print(cancel(a, i, total))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
N = input()
A = np.array(map(int, raw_input().split()))


A = np.append(np.append([0], A), [0])
total_sum = 0
for i in range(0, len(A) - 1):
    total_sum += np.abs(A[i + 1] - A[i])

for i in range(1, len(A) - 1):
    partial_sum = total_sum - np.abs(A[i + 1] - A[i]) - np.abs(A[i] - A[i - 1]) + np.abs(A[i + 1] - A[i - 1])
    print partial_sum

Submission status: Accepted

Solution code:
import numpy as np

def main():
    SpotNum = int(input())
    SpotCoordinate = [int(i) for i in input().split()]
    
    SpotCoordinate.insert(0, 0)
    SpotCoordinate.append(0)
    
    DiffSpotCoordinate = abs(np.diff(SpotCoordinate))
#    print(DiffSpotCoordinate)
    
#    SumTable = [0 for i in range(100000)]
##    print(SumTable)
#    
#    Diff = abs(SpotCoordinate[0] - SpotCoordinate[2] )
#    result = Diff + sum(DiffSpotCoordinate[2:])
#    print(result)
#    
#    for i in range(1,SpotNum):
#        if(SumTable == []):
#            SumTable[i-1] = sum(DiffSpotCoordinate[:i])
##            print(SumTable[i-1])
#        else:
#            SumTable[i-1] = SumTable[i-2] + sum(DiffSpotCoordinate[i-1:i])
##            print(SumTable[i-1])
#            
#        Diff = abs(SpotCoordinate[i] - SpotCoordinate[i+2] )
##        print(Diff)
#        
#        result = Diff + SumTable[i-1] + sum(DiffSpotCoordinate[i+2:])
##        print(RmSpotCoodinate)
##        print(sum(abs(np.diff(RmSpotCoordinate))))
#        print(result)
    
    SumDiffSpotCoordinate = sum(DiffSpotCoordinate)
    for i in range(1,SpotNum+1):
        result = SumDiffSpotCoordinate - DiffSpotCoordinate[i-1] - DiffSpotCoordinate[i] + abs(SpotCoordinate[i-1] - SpotCoordinate[i+1])
        print(result)
        
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)

default= 0
tmp = 0
for i in range(n+2):
    default += abs(a[i]-tmp)
    tmp = 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(default)
    else:
        distance = abs(a[i] - a[i-1]) + abs(a[i+1] - a[i])
        sa = distance - abs(a[i+1] - a[i-1])
        print(default-sa)

Submission status: Accepted

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

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

    dis = 0
    bef = 0
    for b in a:
        dis += abs(bef - b)
        bef = b
    dis += abs(bef)
    for i in range(n):
        m = 0 if i == 0 else a[i-1]
        l = 0 if i == len(a) - 1 else a[i+1]
        print(dis - (abs(m - a[i]) + abs(a[i] - l) - abs(m - l)))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())

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

yen = 0
x = 0
for i in a:
    yen += abs(i - x)
    x = i
yen += abs(x)

for i in range(n):
    if(i == 0):
        if((0 <= a[i] and a[i] <= a[i+1]) or (0 >= a[i] and a[i] >= a[i+1])):
            print(yen)
        elif(a[i] * a[i+1] < 0):
            print(yen - abs(a[i] * 2))
        elif(a[i] >= a[i+1] or a[i] <= a[i+1]):
            print(yen - abs(a[i+1] -a[i]) * 2)
        else:
            print(yen - abs(a[i] * 2))
    elif(i+1 < n):
        if((a[i-1] <= a[i] and a[i] <= a[i+1]) or (a[i-1] >= a[i] and a[i] >= a[i+1])):
            print(yen)
        elif((a[i-1] >= a[i] and a[i-1] >= a[i+1]) or (a[i-1] <= a[i] and a[i-1] <= a[i+1])):
            print(yen - abs((a[i+1] - a[i]) * 2))
        else:
            print(yen - abs((a[i] - a[i-1]) * 2))
    else:
        if((a[i-1] <= a[i] and a[i] <= 0) or (a[i-1] >= a[i] and a[i] >= 0)):
            print(yen)
        elif(a[i-1] * a[i] < 0):
            print(yen - abs(a[i] * 2))
        else:
            print(yen - abs((a[i-1] - a[i]) * 2))
        break

Submission status: Accepted

Solution code:
def abs(a,b):
    return a-b if a>b else b-a

N=int(input())
A=list(map(int,input().split(' ')))
A.append(0)
skip = []
total = 0
prev = 0
for i in range(len(A)-1):
    diff = abs(prev,A[i])
    total += diff
    skip.append(diff + abs(A[i],A[i+1]) - abs(prev,A[i+1]))
    prev = A[i]
total += abs(A[-1],A[-2])
for i in range(len(skip)):
    print(total - skip[i])

Submission status: Accepted

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


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

for i in range(1, N+1):
    x = abs(A[i-1] - A[i])
    y = abs(A[i] - A[i+1])
    z = abs(A[i-1] - A[i+1])
    print(s - x - y + z)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    P = list(map(int,input().split()))
    P.insert(0, 0)
    Dis = [[abs(P[i]-P[(i+j+1)%(N+1)]) for i in range(N+1)] for j in range(2)]
    temp = sum(Dis[0])
    temp2 = [sum(Dis[0][i:i+2]) for i in range(N)]
    for i in range(N): print(temp-temp2[i]+Dis[1][i])

Submission status: Accepted

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

for i, v in enumerate(a):
    if i != 0 and i != n + 1:
        h = abs(a[i-1]-a[i]) + abs(a[i] - a[i+1])
        u = abs(a[i-1] - a[i+1])
        print(total - (h-u))

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    N = int(stdin.readline().rstrip())
    Ai = [int(x) for x in stdin.readline().rstrip().split()]

    foo = [0] + Ai + [0]
    bar = [abs(foo[i] - foo[i + 1]) for i in range(len(foo) - 1)]
    sumbar = sum(bar)

    spam = [0] + Ai + [0]
    for i in range(N):
        if foo[i] <= foo[i + 1] <= foo[i + 2] or foo[i + 2] <= foo[i + 1] <= foo[i]:
            print(sumbar)
        else:
            ans = sumbar - (bar[i] + bar[i + 1]) + abs(spam[i] - spam[i + 2])
            print(ans)


if __name__ == '__main__':
    main()