Problem:


Score : 300 points

Problem Statement

There are N sightseeing spots on the x-axis, numbered 1, 2, ..., N.
Spot i is at the point with coordinate A_i.
It costs |a - b| yen (the currency of Japan) to travel from a point with coordinate a to another point with coordinate b along the axis.

You planned a trip along the axis.
In this plan, you first depart from the point with coordinate 0, then visit the N spots in the order they are numbered, and finally return to the point with coordinate 0.

However, something came up just before the trip, and you no longer have enough time to visit all the N spots, so you decided to choose some i and cancel the visit to Spot i.
You will visit the remaining spots as planned in the order they are numbered.
You will also depart from and return to the point with coordinate 0 at the beginning and the end, as planned.

For each i = 1, 2, ..., N, find the total cost of travel during the trip when the visit to Spot i is canceled.

Constraints

2 \leq N \leq 10^5

-5000 \leq A_i \leq 5000 (1 \leq i \leq N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print N lines.
In the i-th line, print the total cost of travel during the trip when the visit to Spot i is canceled.

Sample Input 1

3
3 5 -1

Sample Output 1

12
8
10

Spot 1, 2 and 3 are at the points with coordinates 3, 5 and -1, respectively.
For each i, the course of the trip and the total cost of travel when the visit to Spot i is canceled, are as follows:

For i = 1, the course of the trip is 0 \rightarrow 5 \rightarrow -1 \rightarrow 0 and the total cost of travel is 5 + 6 + 1 = 12 yen.

For i = 2, the course of the trip is 0 \rightarrow 3 \rightarrow -1 \rightarrow 0 and the total cost of travel is 3 + 4 + 1 = 8 yen.

For i = 3, the course of the trip is 0 \rightarrow 3 \rightarrow 5 \rightarrow 0 and the total cost of travel is 3 + 2 + 5 = 10 yen.

Sample Input 2

5
1 1 1 2 0

Sample Output 2

4
4
4
2
4

Sample Input 3

6
-679 -2409 -3258 3095 -3291 -4462

Sample Output 3

21630
21630
19932
8924
21630
19288



Sample input:
3
3 5 -1


Sample output:
12
8
10

Submission status: Accepted

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

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

if min(0,A[1]) <=  A[0] <= max(0, A[1]):
    print(S)
elif A[0] < min(0,A[1]):
    print(S+2*(A[0] - min(0,A[1])))
elif max(0, A[1]) < A[0]:
    print(S+2*(max(0, A[1]) - A[0]))
    
for i in range(1, N-1):
    if min(A[i-1],A[i+1]) <=  A[i] <= max(A[i-1], A[i+1]):
        print(S)
    elif A[i] < min(A[i-1],A[i+1]):
        print(S+2*(A[i] - min(A[i-1],A[i+1])))
    elif max(A[i-1], A[i+1]) < A[i]:
        print(S+2*(max(A[i-1], A[i+1]) - A[i]))

if min(A[-2],0) <=  A[N-1] <= max(A[-2], 0):
    print(S)
elif A[N-1] < min(A[-2],0):
    print(S+2*(A[N-1] - min(A[-2],0)))
elif max(A[-2], 0) < A[N-1]:
    print(S+2*(max(A[-2], 0) - A[N-1]))

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
a = [0];a.extend(readInts());a.append(0)
base = sum([abs(a[i]-a[i+1]) for i in range(n+1)])

for i in range(1,n+1):
	print(base-(abs(a[i-1]-a[i])+abs(a[i]-a[i+1]))+abs(a[i-1]-a[i+1]), end=" ")

Submission status: Accepted

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

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

import sys
input = sys.stdin.readline

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

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

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

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

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

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

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

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

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

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

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

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

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

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

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

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

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

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

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

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]

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

#############
# Main Code #
#############
N = I()
A = IL()

POS = 0
SUM = 0
for a in A:
  SUM += abs(POS-a)
  POS = a
SUM += abs(POS)

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

Submission status: Accepted

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

full_cost = 0

a_list = [0] + a_list + [0]

for i in range(n+1):
    full_cost += abs(a_list[i] - a_list[i+1])

for i in range(1, n+1):
    cost_diff = abs(a_list[i-1] - a_list[i+1]) - \
        abs(a_list[i] - a_list[i+1]) - abs(a_list[i-1] - a_list[i])
    print(full_cost + cost_diff)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
from math import gcd

from math import factorial as f

from math import ceil, floor, sqrt
import math

import bisect
import re
import heapq

from copy import deepcopy
import itertools
from itertools import permutations

from sys import exit

ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(map(int, input().split()))

yes = "Yes"
no = "No"


def main():
    n = ii()
    a = li()
    a.insert(0, 0)
    a.append(0)
    rin = []

    for i in range(n + 1):
        rin.append(abs(a[i] - a[i + 1]))
    drin = []
    for i in range(n):
        drin.append(abs(a[i] - a[i + 2]))
    tmpall = sum(rin)

    for i in range(n):
        print(tmpall - rin[i] - rin[i + 1] + drin[i])


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
import functools
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
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)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n = k()
a = [0] + l() + [0]
sa = []

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

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

Submission status: Accepted

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


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

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

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

for i in range(1,len(A)-1):
    minus=abs(A[i-1]-A[i])+abs(A[i]-A[i+1])
    plus=abs(A[i-1]-A[i+1])
    ans=all_cost-minus+plus
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


n = I()
a = LI()
a = [0] + a + [0]
sum_a = sum([abs(a[i] - a[i - 1]) for i in range(1, n + 2)])
ans = []
for i in range(1, n + 1):
    diff = - abs(a[i] - a[i - 1]) - abs(a[i + 1] - a[i]) + \
        abs(a[i + 1] - a[i - 1])
    cost = sum_a + diff
    ans.append(cost)
print(*ans, sep='\n')

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

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

Submission status: Accepted

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

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

print(sm-abs(a[n-1]-a[n])-abs(a[n])+abs(a[n-1]))

Submission status: Accepted

Solution code:
import bisect,copy,heapq,itertools,string
from collections import *
from math import *
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

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

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

Submission status: Accepted

Solution code:
import copy
n=int(input())
a=[0]+list(map(int,input().split()))+[0]
b=[]
ans=0
for i in range(n+1):
    t=abs(a[i]-a[i+1])
    b.append(t)
    ans+=t
for i in range(1,n+1):
    tmp=copy.deepcopy(ans)
    tmp-=b[i-1]
    tmp-=b[i]
    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.insert(0,0)
A.append(0)
_A = []

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


for i in range(0,N):
  allS = S
  allS -= (_A[i]+_A[i+1]-abs(A[i+2]-A[i]))
  print(allS)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    max_val = 0
    A = [0] + A + [0]
    for i in range(len(A)):
        max_val += abs(A[i]-A[i-1])
    for i in range(1,len(A)-1):
        print(max_val + abs(A[i-1]-A[i+1]) - abs(A[i-1]-A[i]) - abs(A[i]-A[i+1]))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

all_price = 0
now = 0
for i in range(N + 1):
    if (i == N):
        all_price += abs(A[i - 1])
        break
    all_price += abs(A[i] - now)
    now = A[i]


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

    elif (i == N - 1):
        print(all_price + abs(A[i - 1] - 0) -
              (abs(A[i] - A[i - 1]) + abs(abs(A[i] - 0))))

    else:
        print(all_price + 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, *a = map(int, open(0).read().split())
a = [0] + a + [0]
all_sum = sum([abs(a[i] - a[i+1]) for i in range(n+1)])
for i in range(1,n+1):
    print(all_sum - abs(a[i-1] - a[i]) - abs(a[i] - a[i+1]) + abs(a[i-1] - a[i+1]))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/arc093/tasks/arc093_a
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def mina(*argv, sub=1): return list(map(lambda x: x - sub, argv))
# 受け渡されたすべての要素からsubだけ引く.リストを*をつけて展開しておくこと


def a_int(): return int(read())


def ints(): return list(map(int, read().split()))


def read_col(H):
    '''H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_tuple(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_matrix(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter, xor, add
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right
from functools import reduce

N = a_int()
A = [0] + ints() + [0]
d_nx = [abs(x - y) for x, y in zip(A, A[1:])]  # 一つとなりへ移動するための距離
# d_nx[i]はA[i]→A[i+1]までの距離
d_nnx = [abs(x - y) for x, y in zip(A, A[2:])]  # 2つ隣に移動するための距離
# d_nnx[i] はA[i]→A[i+2]までの距離
total = sum(d_nx)
ans = []
for i in range(N):
    ans.append(total - (d_nx[i] + d_nx[i + 1]) + d_nnx[i])
print(*ans, sep='\n')

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

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

Submission status: Accepted

Solution code:
import numpy as np

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

difA = np.diff(A)
dis = abs(difA)
PM = difA//dis
S = sum(dis)

for i in range(N):
  if PM[i] == PM[i+1]:
    print(S)
  else:
    print(S - (dis[i] + dis[i+1]) + abs(difA[i] + difA[i+1]))

Submission status: Accepted

Solution code:
N=int(input())
c=list(map(int, input().split()))
C=[0]
C.extend(c)
C.append(0)
sa=[]
for i in range(1,N+2):
  sa.append(abs(C[i]-C[i-1]))
moto=sum(sa)
for i in range(1,N+1):
  c1=C[i-1]<=C[i] and C[i]<=C[i+1]
  c2=C[i-1]>=C[i] and C[i]>=C[i+1]
  if c1 or c2:
    print(moto)
  else:
    sub=abs(C[i]-C[i-1])+abs(C[i+1]-C[i])
    print(moto-sub+abs(C[i-1]-C[i+1]))

Submission status: Accepted

Solution code:
from functools import reduce
from decimal import *
from operator import mul

def main():
    n = int(input())
    a = input_list()
    a.insert(0, 0)
    a.append(0)
    total = 0
    for i in range(1, n+2):
        total += abs(a[i] - a[i-1])
    for i in range(1, n+1):
        tobashi = abs(a[i+1] - a[i-1])
        skip = abs(a[i] - a[i-1]) + abs(a[i+1] - a[i])
        print(total - skip + tobashi)

def count_tento(l, cur):
    count = 0
    for v in l:
        if v > cur:
            count += 1
    return count

def input_list():
    return list(map(int, input().split()))

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
num=[abs(a[0])]
def ab(num1):
    return abs(num1)
def ap(num2):
    num.append(num2)
for i in range(n-1):
    ap(ab(a[i]-a[i+1]))
ap(ab(a[-1]))
num4=sum(num)
print(num4-num[0]-num[1]+ab(a[1]))
for i in range(n-2):
    print(num4-num[i+1]-num[i+2]+ab(a[i+2]-a[i]))
print(num4-num[-1]-num[-2]+ab(a[-2]))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# AtCoder Regular Contest 093
# C - Traveling Plan
# https://atcoder.jp/contests/arc093/tasks/arc093_a
N = int(input())
*A, = map(int, input().split())

A = [0]+A+[0]
sun = 0
pre = 0
for i, a in enumerate(A):
    sun += abs(a-pre)
    pre = a

pre = 0
for i, a in enumerate(A[:-1]):
    if i == 0:
        continue
    print(sun - abs(pre - a) - abs(a - A[i+1]) + abs(pre - A[i+1]))
    pre = a

Submission status: Accepted

Solution code:
import math
import string
import collections
from collections import Counter
from collections import deque
from decimal import Decimal
import sys
import fractions
from operator import itemgetter
import itertools
import copy


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor


# coordinates
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
n = int(input())
a = readints()
a.insert(0, 0)
a.append(0)
# print(a)
cost = 0
for i in range(len(a)-1):
    cost += abs(a[i]-a[i+1])
#print('c', cost)
for i in range(1, len(a)-1):
    #print(i-1, i, i+1)
    print(cost - (abs(a[i-1]-a[i])+abs(a[i]-a[i+1])) + abs(a[i-1]-a[i+1]))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.append(0)
now = 0
d = 0
for e in A[:-1]:
    d += abs(now-e)
    now = e
d += abs(now)

ans = []
prev = 0

for k in range(N):
    if prev <= A[k] <= A[k+1] or A[k+1] <= A[k] <= prev:
        ans.append(d)
    elif A[k] < prev < A[k+1] or A[k+1] < prev < A[k]:
        ans.append(d-2*abs(A[k]-prev))
    else:
        ans.append(d-2*abs(A[k]-A[k+1]))

    prev = A[k]

for e in ans:
    print(e)

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
#from fractions import gcd
#from itertools import combinations # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
A = [0] + readInts() + [0]
all = 0
for i in range(1,n+2):
    all += abs(A[i-1] - A[i])
#print(all)
nya = all
for i in range(1,n+1):
    all = nya
    all -= abs(A[i-1] - A[i])
    all -= abs(A[i] - A[i+1])
    all += abs(A[i-1] - A[i+1])
    print(all)

Submission status: Accepted

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

time = [0 for _ in range(n+1)]
time_s = [0 for _ in range(n)]
time[0] = abs(a[0])
time[-1] = abs(a[-1])
time_s[0] = abs(a[1])
time_s[-1] = abs(a[-2])
for i in range(1, n):
    time[i] = abs(a[i]-a[i-1])
for i in range(1, n-1):
    time_s[i] = abs(a[i+1]-a[i-1])
t = sum(time)
for i in range(n):
    print(t-time[i]-time[i+1]+time_s[i])

Submission status: Accepted

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

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

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

for i in range(1, N+1):
    print(ori-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)
tot = 0
for i in range(1,N+1):
    tot += abs(A[i]-A[i-1])
tot += abs(A[0]-A[N])
for i in range(1,N):
    a = tot-abs(A[i]-A[i-1])-abs(A[i+1]-A[i])+abs(A[i+1]-A[i-1])
    print(a)
a = tot-abs(A[N]-A[N-1])-abs(0-A[N])+abs(0-A[N-1])
print(a)

Submission status: Accepted

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

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

ans = []
for i in range(1,N+1):
    if A[i-1]<=A[i]:
        if A[i]<=A[i+1]:
            ans.append(total)
        else:
            minus = (A[i]-max(A[i-1],A[i+1]))*2
            ans.append(total-minus)
    else:
        if A[i]>=A[i+1]:
            ans.append(total)
        else:
            minus = (min(A[i-1],A[i+1])-A[i])*2
            ans.append(total-minus)
print(*ans, sep='\n')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

l = [0]
l.extend(list(map(int, input().split())))
l.append(0)

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

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

Submission status: Accepted

Solution code:
N = int(input())
d = [0]*(N+1)
A = [0]+list(map(int, input().split()))+[0]
ac =[0]*(N+2)

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


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

Submission status: Accepted

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

#3 5 -1 
#3 2 6 1 
dis=[0]*(N+1)
for i in range(N+1):
    if i==N:
        #print("ok", i)
        dis[i]=abs(0-A[i-1])
    elif i==0:
        dis[i]=abs(A[0]-0)
    else:
        #print(i)
        dis[i]=abs(A[i]-A[i-1])
SUM=sum(dis)
A=A+[0]
for i in range(N):
    ans=SUM-dis[i+1]-dis[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]
abl = []
sum = 0

for i in range(N+1):
  dis = abs(A[i+1] - A[i])
  sum += dis
  abl.append(dis)
#print(abl)

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

Submission status: Accepted

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

def resolve():
    n=int(input())
    l=[0]+list(map(int,input().split()))+[0]
    max=0
    for i in range(n+1):
        max+=abs(l[i+1]-l[i])
    for j in range(1,n+1):
        x=abs(l[j+1]-l[j])
        y=abs(l[j]-l[j-1])
        z=abs(l[j+1]-l[j-1])
        print(max-x-y+z)
resolve()

Submission status: Accepted

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

def resolve():
    n=int(input())
    l=[0]+list(map(int,input().split()))+[0]
    max=0
    for i in range(n+1):
        max+=abs(l[i+1]-l[i])
    for j in range(1,n+1):
        if l[j-1]<l[j]<l[j+1] or l[j-1]>l[j]>l[j+1]:
            print(max)
        else:
            x=abs(l[j+1]-l[j])
            y=abs(l[j]-l[j-1])
            z=abs(l[j+1]-l[j-1])
            print(max-x-y+z)
resolve()

Submission status: Accepted

Solution code:
n, *alst = map(int, open(0).read().split())
alst += [0]
s = abs(alst[0]) + sum(abs(alst[i] - alst[i + 1]) for i in range(n))
print(s + abs(alst[1]) - (abs(alst[0]) + abs(alst[0] - alst[1])))
for i in range(1, n):
  print(s + abs(alst[i - 1] - alst[i + 1]) - (abs(alst[i - 1] - alst[i]) + abs(alst[i] - alst[i + 1])))

Submission status: Accepted

Solution code:
import itertools

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

acc = []
prev = 0
t = 0
for i in range(N+2):
  if i == 0:
    continue
  a = A[i]
  t += abs(a - prev)
  acc.append(t)
  prev = a
  
ans = []
for i in range(N+1):
  if i == 0:
    continue
  t = 0

  if 1 < i:
    t += acc[i-2]
  if i < N:
    t += acc[N] - acc[i]
  t += abs(A[i+1] - A[i-1])
    
  ans.append(t)
  
print("\n".join(map(str,ans)))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
a_li = list(map(int,input().split()))
a_li.append(0)
dum = 0
ans = 0
for i in range(n+1):
    ans += abs(a_li[i]-dum)
    dum = a_li[i]
dum = 0
for i in range(n):
    if dum <= a_li[i] <= a_li[i+1]:
        print(ans)
    else:
        print(ans-(abs(dum-a_li[i]) + abs(a_li[i]-a_li[i+1]))+abs(dum-a_li[i+1]))
    dum = a_li[i]

Submission status: Accepted

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

arr=np.array(a)

dif=np.diff(arr)
difsum=np.sum(np.abs(dif))
for i in range(1,n+1):
    offset=abs(dif[i]+dif[i-1])-abs(dif[i])-abs(dif[i-1])
    print(difsum+offset)

Submission status: Accepted

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


arr=np.array(l)
arr2=np.append(np.hstack((0,arr)),0)

dif=np.diff(arr2).tolist()
differ=np.sum(np.abs(dif))
for i in range(1,n+1):
    difference=abs(dif[i]+dif[i-1])-abs(dif[i])-abs(dif[i-1])
    print(differ+difference)

Submission status: Accepted

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


arr=np.array(l)
arr2=np.append(np.hstack((0,arr)),0)

dif=np.diff(arr2)
differ=np.sum(np.abs(dif))
for i in range(1,n+1):
    difference=abs(dif[i]+dif[i-1])-abs(dif[i])-abs(dif[i-1])
    print(differ+difference)

Submission status: Accepted

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


arr=np.array(l)
arr2=np.append(np.hstack((0,arr)),0)
differ=np.sum(np.abs(np.diff(arr2)))
dif=np.diff(arr2)
for i in range(1,n+1):
    difference=abs(dif[i]+dif[i-1])-abs(dif[i])-abs(dif[i-1])
    print(differ+difference)

Submission status: Accepted

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


arr=np.array(l)
arr2=np.append(np.hstack((0,arr)),0)
differ=sum(np.abs(np.diff(arr2)))
dif=np.diff(arr2)
for i in range(1,n+1):
    difference=abs(dif[i]+dif[i-1])-abs(dif[i])-abs(dif[i-1])
    print(differ+difference)

Submission status: Accepted

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


arr=np.array(l)
arr2=np.append(np.hstack((0,arr)),0)
differ=sum(abs(np.diff(arr2)))
dif=np.diff(arr2)
for i in range(1,n+1):
    difference=abs(dif[i]+dif[i-1])-abs(dif[i])-abs(dif[i-1])
    print(differ+difference)

Submission status: Accepted

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

S = sum(dis)
for i in range(1, n+1):
    A = a[i-1]
    B = a[i]
    C = a[i+1]
    if A > C:
        A, C = C, A
    if A<=B<=C:
        print(S)
    else:
        print(S-2*min(abs(A-B), abs(C-B)))