結果

問題 No.972 選び方のスコア
ユーザー gew1fw
提出日時 2025-06-12 13:30:19
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 914 bytes
コンパイル時間 706 ms
コンパイル使用メモリ 82,648 KB
実行使用メモリ 110,708 KB
最終ジャッジ日時 2025-06-12 13:35:25
合計ジャッジ時間 8,725 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25 WA * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect

n = int(input())
a = list(map(int, input().split()))
a.sort()
prefix_sum = [0] * (n + 1)
for i in range(n):
    prefix_sum[i+1] = prefix_sum[i] + a[i]

max_s = -float('inf')

for j in range(n):
    aj = a[j]
    # Calculate L: number of elements <= aj
    L = bisect.bisect_right(a, aj)
    # Calculate R: number of elements > aj
    R = n - bisect.bisect_right(a, aj)
    t_max = min(L - 1, R)
    if t_max < 0:
        continue
    # Calculate sum_small: sum of the largest t_max elements <= aj (excluding aj itself)
    start_small = L - 1 - t_max
    if start_small < 0:
        sum_small = prefix_sum[L-1]
    else:
        sum_small = prefix_sum[L-1] - prefix_sum[start_small]
    # Calculate sum_large: sum of the largest t_max elements > aj
    sum_large = prefix_sum[n] - prefix_sum[n - t_max]
    s = sum_small + sum_large - 2 * t_max * aj
    if s > max_s:
        max_s = s

print(max_s)
0