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())
points = list(map(int, input().split(' ')))
points.insert(0, 0)
points.insert(len(points), 0)

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


for i in range(1, len(points) - 1):
    tmp_sum = 0
    if points[i - 1] <= points[i] <= points[i + 1]:
        print(sum)
        continue

    if points[i - 1] <= points[i + 1] <= points[i]:
        print(sum - 2 * abs(points[i] - points[i + 1]))
        continue

    if points[i] <= points[i - 1] <= points[i + 1]:
        print(sum - 2 * abs(points[i] - points[i - 1]))
        continue

    if points[i] <= points[i + 1] <= points[i - 1]:
        print(sum - 2 * abs(points[i] - points[i + 1]))

        continue
    if points[i + 1] <= points[i - 1] <= points[i]:
        print(sum - 2 * abs(points[i] - points[i - 1]))

        continue
    if points[i + 1] <= points[i] <= points[i - 1]:
        print(sum)

        continue

Submission status: Accepted

Solution code:
import math

N = int(input())
list = input().split(" ")
list.insert(0,"0")
list.append("0")

change = 0
now = 0
for i in range(N+1):
    if now < int(list[i]):
        change += int(list[i])-now
        now = int(list[i])
    else:
        change += now-int(list[i])
        now = int(list[i])
change += math.fabs(now)
change = int(change)

for i in range(1,N+1):
   ans = change - math.fabs(int(list[i])-int(list[i-1]))
   ans = ans - math.fabs(int(list[i+1])-int(list[i]))
   ans = ans + math.fabs(int(list[i+1])-int(list[i-1]))
   ans = int(ans)
   print(ans)

Submission status: Accepted

Solution code:
a = int(input())
b = list(map(int, input().split()))
sumroute = 0
ever = 0

for i in range(len(b)):
    sumroute += abs(b[i] - ever)
    ever = b[i]
sumroute += abs(b[-1])
b.insert(0,0)
b.append(0)
for i in range(len(b)-2):
    if (b[i+1]-b[i]) * (b[i+2]-b[i+1]) < 0:
        print(sumroute-2*min(abs(b[i+1]-b[i]),abs(b[i+2]-b[i+1])))
    else:
        print(sumroute)

Submission status: Accepted

Solution code:
try:
    n = int(input().strip())
    spot = [int(i) for i in input().strip().split(" ")]
    spot.insert(0,0)
    spot.append(0)
    max=0
    for m in range(1,len(spot)):
        max+=abs(spot[m-1] - spot[m])
    for i in range(1,n+1):
        sub1=abs(spot[i-1] - spot[i])
        sub2=abs(spot[i] - spot[i+1])
        add1=abs(spot[i-1] - spot[i+1])
        print(max-sub1-sub2+add1)
except EOFError:
    pass

Submission status: Accepted

Solution code:
n = int(input())
dg = input().split(' ')
dg.append(0)
dg  = [0] + dg
sum_0 = sum([abs(int(dg[x]) - int(dg[x+1])) for x in range(len(dg)-1)])
for i in range(1, len(dg)-1):
    
    print(sum_0 - abs((abs(int(dg[i-1])-int(dg[i+1])) - (abs(int(dg[i-1])-int(dg[i])) + abs(int(dg[i])-int(dg[i+1]))))))

Submission status: Accepted

Solution code:
n = int(input())
A = [0]+[int(i) for i in input().split()]+[0]
S = [0]*(n+2)
current = 0
s = 0

for i in range(len(A)):
    s += abs(A[i]-current)
    S[i] = s
    current = A[i]




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

Submission status: Accepted

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


sum = 0
present = 0
for s, t in enumerate(a):
  sum += abs(t-present)
  present = t
sum += abs(present-0)

for i, each in enumerate(a):
  if i == 0:
    prev = 0
    tmp = each
    next = a[i+1]
  elif i == len(a)-1:
    prev = a[i-1]
    tmp = each
    next = 0
  else:
    prev = a[i-1]
    tmp = each
    next = a[i+1]

  before = abs(prev-tmp) + abs(tmp - next)
  after = abs(prev - next)

  print(sum - before + after)

Submission status: Accepted

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

N = int(input())
A = list(map(int, input().split(' ')))

a = 0
I = []
for b in A + [0]:
    I.append(b - a)
    a = b
S = sum(map(abs, I))

for i in range(N):
    j = i + 1
    n = S - (abs(I[i]) + abs(I[i + 1])) + abs(I[i] + I[i + 1])
    print(n)

Submission status: Accepted

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

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

# print(cost) #ぬけがない時の合計
sigma = cost   #fix

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

for j in range(1,N-1):
    cost = sigma
    cost = cost - abs(A[j-1] - A[j]) - abs(A[j] - A[j+1]) + abs(A[j-1] - A[j+1])
    print(cost)

cost = sigma
print(cost - abs(A[N-2] - A[N-1])  -abs(A[N-1]) + abs(A[N-2]))

Submission status: Accepted

Solution code:
def cost(a):
    prev = 0
    total = 0
    for ai in a:
        total += abs(ai - prev)
        prev = ai
    total += abs(0 - prev)
    return total

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

def read_line(*types): return [f(a) for a, f in zip(input().split(), types)]

n, = read_line(int)
a = read_line(*([int] * n))

def price(aa):
    sum = 0

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

    sum += abs(prev)
    return sum

aa = [0] + a + [0]
price_all = 0

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

for i in range(1, len(aa) - 1):
    p = price_all
    p -= abs(aa[i - 1] - aa[i])
    p -= abs(aa[i] - aa[i + 1])
    p += abs(aa[i - 1] - aa[i + 1])
    print(p)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(len(a) - 2):
    d1, d2 = a[i+1] - a[i], a[i+2] - a[i]
    if abs(d1) + abs(d2) == abs(d1 - d2):
        print(dist - 2 * abs(d1))
    elif 0 < d2 < d1:
        print(dist - 2 * abs(d1 - d2))
    elif d1 < d2 < 0:
        print(dist - 2 * abs(d1 - d2))
    else:
        print(dist)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
C - Traveling Plan
https://beta.atcoder.jp/contests/abc092/tasks/arc093_a

"""
import sys
from sys import stdin

input = stdin.readline


def solve(spots, N):
    total_cost = 0
    cp = 0
    for s in spots:
        total_cost += abs(s - cp)
        cp = s
    total_cost += abs(spots[-1])

    ans = []
    spots.append(0)
    spots.insert(0, 0)
    for i in range(1, N):
        if spots[i-1] <= spots[i] <= spots[i+1] or spots[i-1] >= spots[i] >= spots[i+1]:
            ans.append(total_cost)
        else:
            if spots[i] <= spots[i-1] <= spots[i+1]:
                t = abs(spots[i] - spots[i-1]) * 2
            elif spots[i-1] <= spots[i+1] <= spots[i]:
                t = abs(spots[i] - spots[i+1]) * 2
            elif spots[i-1] >= spots[i+1] >= spots[i]:
                t = abs(spots[i] - spots[i+1]) * 2
            else:
                t = abs(spots[i] - spots[i-1]) * 2
            ans.append((total_cost - t))

    spots = spots[:-2]
    total_cost = 0
    cp = 0
    for s in spots:
        total_cost += abs(s - cp)
        cp = s
    total_cost += abs(spots[-1])
    ans.append(total_cost)

    return ans


def main(args):
    N = int(input())
    spots = [int(x) for x in input().split()]
    ans = solve(spots, N)
    print(*ans, sep='\n')


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

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

val=0
bfr=0
a.append(0)

for ai in a:
    val+=abs(ai-bfr)
    bfr=ai

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

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().strip()

n = ni()
a = na()

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

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

Submission status: Accepted

Solution code:
from array import array   
N = int(input())
A = [int(i) for i in input().split()]
# 何も取りやめないときの総コスト
now = 0
cost = 0
for po in A:
    cost += abs(now - po)
    now = po
cost += abs(now)

dist = array('l',[0]+A+[0,0])
# i番目を取りやめる
for i in range(N):
    ans = cost
    ans += (abs(dist[i]-dist[i+2])) - (abs(dist[i]-dist[i+1])+abs(dist[i+1]-dist[i+2]))
    print(ans)

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])]
    costs_cumsum = [abs(A[0])]
    for i in range(1, N):
        cost = abs(A[i] - A[i-1])
        cumsum = cost + costs_cumsum[-1]

        costs.append(cost)
        costs_cumsum.append(cumsum)

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

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

prev = 0
for i in range(N):
    now = A[i]
    nex = A[i + 1]
    ans = total_normal - (abs(now - prev) + abs (nex - now)) + abs(nex - prev)
    print(ans)
    prev = now

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):
    a = abs(A[i-1] - A[i])
    b = abs(A[i] - A[i+1])
    c = abs(A[i-1] - A[i+1])
    print(total - abs(c - (a + b)))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
import sys
n = int(input())
li = list(map(int, input().split()))
# すの距離を計算
d = []
prev=0
for x in li:
  d.append(abs(x-prev))
  prev=x
d.append(abs(0-prev))
dist = sum(d)

# 各ステップ計算
prev=0
post=0
for i in range(n):
  if i==0:
    prev=0
  else:
    prev=li[i-1]
  if i==n-1:
    post=0
  else:
    post=li[i+1]
  nowe = li[i]

  if prev <= nowe and nowe <= post:
    print(dist)
  elif prev >= nowe and nowe >= post:
    print(dist)
  elif prev >= nowe and nowe <= post:
    print(dist-2*min(d[i], d[i+1]))
  elif prev <= nowe and nowe >= post:
    print(dist-2*min(d[i],d[i+1]))

Submission status: Accepted

Solution code:
N = int(input())
zero = ["0"]
LST = input().split(" ")
LST = zero + LST + zero

s = 0
for i in range(len(LST)):
    if i < len(LST)-1:
        s += abs(int(LST[i]) - int(LST[i+1]))
    else:
        s += abs(int(LST[i]))
    

for i in range(len(LST)):
    if i > 0 and i < len(LST)-1:

        minus = 0
        temp = int(LST[i])
        prev = int(LST[i-1])
        past = int(LST[i+1])
        
        if (prev <= temp and temp <= past) or (past <= temp and temp <= prev):
            minus = 0
        elif past < temp:
            minus = abs(temp - past) + abs(temp - prev) - abs(prev - past)
        elif prev > temp:
            minus = abs(temp - past) + abs(temp - prev)- abs(prev - past)

        print(s - minus)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

K=[]
for i in range(1,n+2):
    K.append(abs(A[i]-A[i-1]))

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

Submission status: Accepted

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

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

ans = [0] * N

for i in range(N):
    if i == 0:
        if (int(A[i]) * int(A[i+1]) < 0) or (int(A[i+1]) == 0):
            ans[i] = dis - abs(int(A[i])) * 2
        elif int(A[i]) == 0:
            ans[i] = dis
        else:
            if abs(int(A[i+1])) >= abs(int(A[i])):
                ans[i] = dis
            else:
                ans[i] = dis - abs(int(A[i+1]) - int(A[i])) * 2
    elif i == N-1:
        a = int(A[i-1])
        b = int(A[i])
        if (a*b > 0):
            if abs(b)-abs(a) > 0:
                ans[i] = dis - abs(b-a)*2
            else:
                ans[i] = dis
        else:
            ans[i] = dis - abs(b)*2
    else:
        a = int(A[i-1])
        b = int(A[i]) - a
        c = int(A[i+1]) - a
        if (b * c < 0) or (c == 0):
            ans[i] = dis - abs(b) * 2
        elif b == 0:
            ans[i] = dis
        else:
            if abs(c) >= abs(b):
                ans[i] = dis
            else:
                ans[i] = dis - abs(c - b) * 2


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

Submission status: Accepted

Solution code:
import sys

with sys.stdin as f:
    s_input = f.read()

    s_args = s_input.splitlines()
    n = int(s_args[0])
    sa = s_args[1].split()
    a = [0] + [int(elm) for elm in sa]

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

    D += abs(a[n])
            
    for i in range(1,n+1):
        if i != n:
            print(D - (dist[i-1] + dist[i]) + sd[i])
        else:
            print(D - (dist[n-1] + abs(a[n])) + abs(a[n-1]))
            
sys.exit(0)

Submission status: Accepted

Solution code:
n = int(input())
a = [0] + list(map(int, input().split())) + [0]
m = []
for i in range(n):
    if a[i] <= a[i + 1] <= a[i + 2] or a[i + 2] <= a[i + 1] <= a[i]:
        m.append(0)
    elif a[i] <= a[i + 2] <= a[i + 1] or a[i + 1] <= a[i + 2] <= a[i]:
        m.append(1)
    elif a[i + 1] <= a[i] <= a[i + 2] or a[i + 2] <= a[i] <= a[i + 1]:
        m.append(-1)

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

for i, m0 in enumerate(m):
    if m0 == 0:
        print(s)
    elif m0 == 1:
        print(s - 2 * abs(a[i + 1] - a[i + 2]))
    elif m0 == -1:
        print(s - 2 * abs(a[i] - a[i + 1]))

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    N = int(input())
    A = list(map(int, (input().split(' '))))
    all_cost = abs(0 - A[0])
    for i in range(N-1):
        all_cost += abs(A[i] - A[i+1])
    all_cost += abs(A[N-1] - 0)
    for i in range(N):
        buf = 0
        if i == 0:
            if (A[0] > 0 and A[0] < A[1]) or (A[0] < 0 and A[0] > A[1]):
                buf = 0
            elif (A[0] > 0 and A[0] > A[1]) or (A[0] < 0 and A[0] < A[1]):
                buf += abs(0 - A[0])
                buf += abs(A[0] - A[1])
                buf -= abs(0 - A[1])
        elif i == N-1:
            if (0 < A[N-1] < A[N-2]) or (0 > A[N-1] > A[N-2]):
                buf = 0
            else:
                buf += abs(0 - A[N-1])
                buf += abs(A[N-2] - A[N-1])
                buf -= abs(0 - A[N-2])
        else:
            if (A[i-1] < A[i] < A[i+1]) or (A[i-1] > A[i] > A[i+1]):
                buf = 0
            else:
                buf += abs(A[i-1] - A[i])
                buf += abs(A[i] - A[i+1])
                buf -= abs(A[i-1] - A[i+1])
        print(all_cost - buf)

Submission status: Accepted

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

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

d = [ [abs(A[i] - A[i+1]), abs(A[i] - A[i+2])] for i in range(len(A) - 2)]
d.append([0, 0])
d.insert(0, [0, 0])

fee = 0
for i in d:
    fee += i[0]

res = [d[i+1][0] + d[i][0] - d[i][1] for i in range(2, len(d)-2)]
for r in res:
    print(fee - r)

Submission status: Accepted

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


a2 = [0]
a2.extend(a)
a2.append(0)

max_length = 0

for i in range(len(a2) - 1):
    max_length += abs(a2[i] - a2[i+1])

def checker(a2,j):
    n1,n2,n3 = a2[j-1], a2[j], a2[j+1]
    if n1 > n2 and n2 > n3:
        ans = 0
    elif n1 < n2 and n2 < n3:
        ans = 0
    else :
        ans = 2 * min(abs(n1 - n2), abs(n2 - n3))
        
    return ans

for i in range(len(a2)-2):
    j = i+1
    i_ans = max_length - checker(a2,j)
    print(i_ans)

Submission status: Accepted

Solution code:
import numpy

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

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

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

Submission status: Accepted

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

        elif i < n-1:
            ans = al[-1] + al[i] - al[i+2] + pr[i+1]
        else:
            ans = al[-3] + abs(a[-2])
        print(ans)

Submission status: Accepted

Solution code:
import math
import numpy as np
from operator import mul

# 整数Nを桁ごとのリストにする
# l = [int(x) for x in list(str(N))]

# 入力が文字のリスト
# S = list(input().split())

#入力が数字のリスト
# A = list(map(int, input().split()))

# 入力がリストのリスト
# S = [list(input()) for i in range(H)]


# リストliから重複を削除したリストli_uniqを作成
#li_uniq = []
#for i in li:
#    if x not in li_uniq:
#        li_uniq.append(x)

# 複数行のinput - list版
#list = [list(map(int, input().split())) for i in range(N)]
# 複数行のinput - dictionary版
# key = a, value = b
#for i in range(N):
#    a, b = map(int, input().split())
#    dict[a] = b


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

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

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

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

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

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

Submission status: Accepted

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

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

for i in range(n):

    if i == 0:
        before = 0
    else:
        before = a[i - 1]

    if i == n - 1:
        after = 0
    else:
        after = a[i + 1]

    dist_on = abs(a[i] - before) + abs(after - a[i])
    dist_off = abs(after - before)

    print(total - dist_on + dist_off)

Submission status: Accepted

Solution code:
n, a = input(), [0] + map(int, raw_input().split()) + [0]
r = [abs(a[i] - a[i + 1]) for i in xrange(n + 1)]
f, b, s = [], [], 0
for i in xrange(n + 1):
    f.append(s)
    s += r[i]
s = 0
for i in xrange(n, -1, -1):
    b.append(s)
    s += r[i]
b = b[::-1]
f, b = f[:-1], b[1:]
for i in xrange(n):
    print f[i] + b[i] + abs(a[i] - a[i + 2])

Submission status: Accepted

Solution code:
import sys

Tmp = []
N = int(input())

Tmp = input().rstrip().split(' ')

AllDis = 0
Data = [0 for i in range(N)]
TmpNow = 0


for i in range(N):
    TmpX = int(Tmp[i])
    TmpN = abs(TmpNow - TmpX)
    AllDis += TmpN
    
    if i+1 != N:
        TmpX2 = int(Tmp[i+1])
        TmpN2 = abs(TmpX2 - TmpX)
        Data[i] =  TmpN + TmpN2 - abs(TmpX2 - TmpNow) 
        TmpNow = TmpX
    else:
        TmpN2 = abs(0 - TmpX)
        AllDis += TmpN2
        Data[i] =  TmpN + TmpN2 - abs(0 - TmpNow)
        TmpNow = TmpX
        break

for i in range(N):
    Ans = AllDis - Data[i]
    print(Ans)

Submission status: Accepted

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

#付け足し
a = [0] + a + [0]
n += 2


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

#print(n)
#print(a)
#print(s)


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

Submission status: Accepted

Solution code:
# your code goes here
N = int(input())
point = [0]
point.extend(list(map(int,input().split())))
point.append(0)

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

for i in range(1,N+1):
	ans = total - abs(point[i]-point[i-1]) - abs(point[i+1]-point[i]) + abs(point[i+1]-point[i-1])
	print(ans)

Submission status: Accepted

Solution code:
# coding : utf-8

import numpy as np

N = int(input())
D = input().split()
D = [int(i) for i in D]

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

diff = abs(D[0]) + abs(D[1] - D[0])
net = abs(D[1])
print(alll - diff + net)
for i in range(1,N-1):
    diff = abs(D[i-1] - D[i]) + abs(D[i] - D[i+1])
    net = abs(D[i-1] - D[i+1])
    print(alll - diff + net)
diff = abs(D[N-2] - D[N-1]) + abs(D[N-1])
net = abs(D[N-2])
print(alll - diff + net)

Submission status: Accepted

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


head_rm = points[1:]
tail_rm = points[0:len(points)-1]

amount = sum(abs(head_rm - tail_rm)) + abs(points[0]) + abs(points[-1])

points = np.insert(points, 0, 0)
points = np.insert(points, len(points), 0)

for i in range(1,len(points) - 1):
	if (points[i] <= points[i-1] and points[i-1] <= points[i+1]) or (points[i] >= points[i-1] and points[i-1] >= points[i+1]):
		print(amount - 2 * abs(points[i-1] - points[i]))
	elif (points[i] <= points[i+1] and points[i+1] <= points[i-1]) or (points[i] >= points[i+1] and points[i+1] >= points[i-1]):
		print(amount - 2* abs(points[i+1] - points[i]))
	else:
		print(amount)

Submission status: Accepted

Solution code:
n = int(raw_input().strip())
ais = map(int, raw_input().strip().split())

sp = abs(ais[0]) + abs(ais[-1]) + sum([abs(ais[i + 1] - ais[i]) for i in xrange(n - 1)])

total = [sp for _ in xrange(n)]
for i in xrange(n):
    if i == 0:
        total[i] -= abs(ais[0] - 0)
        total[i] -= abs(ais[1] - ais[0])
    elif i == n - 1:
        total[i] -= abs(0 - ais[n - 1])
        total[i] -= abs(ais[n - 1] - ais[n - 2])
    else:
        total[i] -= abs(ais[i + 1] - ais[i])
        total[i] -= abs(ais[i] - ais[i - 1])

    if i == n - 1:
        total[i] += abs(0 - ais[n - 2])
    elif i == 0:
        total[i] += abs(ais[1] - 0)
    else:
        total[i] += abs(ais[i + 1] - ais[i - 1])

    print total[i]

Submission status: Accepted

Solution code:
#coding:utf-8

N = int(raw_input())

point = map(int, raw_input().split())

prev = 0
total = 0
for i_p in point:
    current = abs(i_p - prev)
    total += current
    prev = i_p

total += abs(0 - prev)

point.insert(0, 0)
point.append(0)

origin = total

#print total

for idx, value in enumerate(point):
    if (idx == 0) or (idx == len(point) - 1):
        continue

    tmp1 = point[idx - 1]
    tmp = point[idx]
    tmp2 = point[idx + 1]

    total -= abs(tmp - tmp1)
    total -= abs(tmp - tmp2)
    total += abs(tmp2 - tmp1)

    print total

    total = origin

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    a.append(0)
    a.insert(0,0)
    d_i=[]
    for i in range(n+1):
        d=abs(a[i]-a[i+1])
        d_i.append(d)
    tot_i=sum(d_i)
    for i in range(1,n+1):
        tot = tot_i - d_i[i-1] - d_i[i] + abs(a[i-1]-a[i+1])
        print(tot)
        
    
main()

Submission status: Accepted

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

points.insert(0,0)
points.insert(len(points),0)

#print points

# 0->A->B->C->D->0 ng
# 0->-->B->C->D->0
# 0->A->-->C->D->0
# 0->A->B->-->D->0
# 0->A->B->C->-->0

# remove i,
# sub i-1=>i and i=>i+1
# add i-1=>i+1

dif_points = []

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

#print dif_points

sum_fee = sum(dif_points)

#print sum_fee

for i in range(N):
    print (sum_fee - dif_points[i]  - dif_points[i+1] + abs(points[i] - points[i+2]))

Submission status: Accepted

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

Submission status: Accepted

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

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

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

for i in range(N):
    print(S + (D2[i] - (D[i]+D[i+1])))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

sum = abs(a[0]) + abs(a[n-1])

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = [0]
A.extend(list(map(int, input().split())))
A.append(0)
B = []
sum_ = abs(A[0]) + abs(A[-2])
for a0, a1, a2 in zip(A, A[1:], A[2:]):
    sum_ += abs(a0-a1)
    if (a0-a1)*(a1-a2) >= 0:
        B.append(0)
    else:
        B.append(2*min(list(map(abs, [a0-a1, a1-a2]))))

for b in B:
    print(sum_ - b)

Submission status: Accepted

Solution code:
from math import fabs
N=int(input())
pos=list(map(int,input().split()))
pos.insert(0,0)
pos.insert(N+1,0)
total_len=0
for i in range(N+1):
    total_len+=fabs(pos[i+1]-pos[i])
    

for i in range(N):
    total=total_len-fabs(pos[i+1]-pos[i])-fabs(pos[i+2]-pos[i+1])+fabs(pos[i+2]-pos[i])
    print(int(total))