結果

問題 No.3268 As Seen in Toasters
ユーザー Iroha_3856
提出日時 2025-09-12 22:11:18
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,051 bytes
コンパイル時間 282 ms
コンパイル使用メモリ 82,180 KB
実行使用メモリ 68,900 KB
最終ジャッジ日時 2025-09-12 23:39:40
合計ジャッジ時間 4,929 ms
ジャッジサーバーID
(参考情報)
judge5 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 3
other RE * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

from random import randint
from more_itertools import distinct_permutations

def f(x, y):
    if x < 0 and y < 0:
        return -x - y
    else:
        return abs(x-y)

def cost(A):
    ret = 0
    for i in range(len(A)-1):
        ret += f(A[i], A[i+1])
    return ret
    
# A = [randint(-10, 10) for i in range(8)]
# A = [117, 96, 102, 55, -104, -123, -108, 79, -131]
# mncost = 10**9
# for X in distinct_permutations(A):
#     if cost(X) < mncost:
#         mncost = cost(X)
# print(mncost)
# for X in distinct_permutations(A):
#     if cost(X) == mncost:
#         print(X)

N = int(input())
A = list(map(int, input().split()))
if min(A) >= 0:
    #普通に
    print(max(A) - min(A))
elif max(A) < 0:
    A.sort()
    ans = sum(-a for a in A)
    ans += A[0]
    ans += A[1]
    print(ans)
else:
    A.sort()
    #両端
    ans = 0
    if A[1] < 0:
        for a in A:
            if a < 0:
                ans += -2 * a
        ans += A[0]
        ans += A[1]
        ans += max(A) * 2
    #片方
    ans = min(ans, cost(A))
    print(ans)
0