結果

問題 No.3527 Minimum Abs Sum
コンテスト
ユーザー LyricalMaestro
提出日時 2026-05-26 02:17:33
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
WA  
実行時間 -
コード長 1,596 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 172 ms
コンパイル使用メモリ 85,376 KB
実行使用メモリ 153,912 KB
最終ジャッジ日時 2026-05-26 02:17:47
合計ジャッジ時間 13,517 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other WA * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

# https://yukicoder.me/problems/no/3527

from functools import cmp_to_key

MOD = 10 ** 9 + 7

# 比較関数
def compare_items(a, b):
    if a[0] * b[1] > b[0] * a[1]:
        return 1
    elif a[0] * b[1] < b[0] * a[1]:
        return -1
    else:
        return 0

def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    array = []
    th = 0
    for i in range(N):
        a = A[i]
        b = B[i]
        if abs(a) != 0:
            array.append((b, a))
        else:
            th += abs(b)

    sorted_array = sorted(array, key=cmp_to_key(compare_items))

    sum_a = -sum(A)
    sum_b = sum(B)
    #  x < array[0]
    answers = [(sum_a, sum_b, sorted_array[0])]
    b0, a0 = sorted_array[0]
    sum_a += 2 * a0
    sum_b -= 2 * b0
    for i in range(1, len(array)):
        if sum_a < 0:
            answers.append((sum_a, sum_b, sorted_array[i]))
        else:
            answers.append((sum_a, sum_b, sorted_array[i - 1]))
        b0, a0 = sorted_array[i]
        sum_a += 2 * a0
        sum_b -= 2 * b0
    # array[-1] <= x
    answers.append((sum_a, sum_b, sorted_array[-1]))

    ans = None
    ans_x = None
    for sum_a, sum_b, x in answers:
        y = (sum_a * x[0] - x[1] * sum_b + x[1] * th, x[1])
        if ans is None:
            ans = y
            ans_x = x
        else:
            if compare_items(ans, y) == 1:
                ans = y
                ans_x = x

    answer = ans_x[0] * pow(ans_x[1], MOD - 2, MOD)
    answer %= MOD
    print(answer)



    








if __name__ == "__main__":
    main()
0