結果

問題 No.972 選び方のスコア
ユーザー ntuda
提出日時 2021-11-10 22:05:19
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,191 bytes
コンパイル時間 200 ms
コンパイル使用メモリ 82,164 KB
実行使用メモリ 105,572 KB
最終ジャッジ日時 2024-11-21 14:33:15
合計ジャッジ時間 5,532 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 17 WA * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from functools import lru_cache

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

if N < 3:
    print(0)
    sys.exit()
A.sort()

B = [0] * (N + 1)
for i in range(N):
    B[i + 1] = B[i] + A[i]

@lru_cache(maxsize=None)
def g(x, y):
    return B[N] - B[N - y] + B[x] - B[x - y] - (2 * y) * A[x]

@lru_cache(maxsize=None)
def f(x):
    lb = 1
    ub = min(x, N - x - 1)
    t1 = (ub + 2 * lb) // 3
    t2 = -(-2 * ub - lb) // 3

    while ub - lb > 2:  # ???
        a, b, c, d = g(x, lb), g(x, t1), g(x, t2), g(x, ub)
        if a <= b <= c:  # ???
            lb = t1
        elif b >= c >= d:  # ???
            ub = t2
        else:
            break
        t1 = (ub + 2 * lb) // 3
        t2 = -(-2 * ub - lb) // 3
    return (max(g(x, lb), g(x, t1), g(x, t2), g(x, ub)))


lb = 1
ub = N - 2
t1 = (ub + 2 * lb) // 3
t2 = -(-2 * ub - lb) // 3
i = 0
while ub - lb > 2:
    a, b, c, d = f(lb), f(t1), f(t2), f(ub)
    if a <= b <= c:
        lb = t1
    elif b >= c >= d:
        ub = t2
    else:
        break

    t1 = (ub + 2 * lb) // 3
    t2 = -(-2 * ub - lb) // 3

#print(lb,t1,t2,ub)
#print(f(lb),f(t1),f(t2),f(ub))
ans = max(f(lb), f(t1), f(t2), f(ub))
print(ans)
0