結果

問題 No.615 集合に分けよう
ユーザー GrayCoderGrayCoder
提出日時 2017-12-16 03:06:03
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 165 ms / 2,000 ms
コード長 559 bytes
コンパイル時間 100 ms
コンパイル使用メモリ 10,852 KB
実行使用メモリ 18,988 KB
最終ジャッジ日時 2023-08-21 07:37:54
合計ジャッジ時間 2,875 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,792 KB
testcase_01 AC 16 ms
7,820 KB
testcase_02 AC 16 ms
8,224 KB
testcase_03 AC 16 ms
8,204 KB
testcase_04 AC 16 ms
7,812 KB
testcase_05 AC 16 ms
7,860 KB
testcase_06 AC 16 ms
7,872 KB
testcase_07 AC 16 ms
8,296 KB
testcase_08 AC 16 ms
7,952 KB
testcase_09 AC 16 ms
7,876 KB
testcase_10 AC 16 ms
7,856 KB
testcase_11 AC 16 ms
7,856 KB
testcase_12 AC 16 ms
8,196 KB
testcase_13 AC 16 ms
7,824 KB
testcase_14 AC 17 ms
8,232 KB
testcase_15 AC 20 ms
9,384 KB
testcase_16 AC 16 ms
8,324 KB
testcase_17 AC 34 ms
13,968 KB
testcase_18 AC 107 ms
17,664 KB
testcase_19 AC 113 ms
17,716 KB
testcase_20 AC 123 ms
17,484 KB
testcase_21 AC 139 ms
17,896 KB
testcase_22 AC 165 ms
18,988 KB
testcase_23 AC 53 ms
14,284 KB
testcase_24 AC 98 ms
17,288 KB
testcase_25 AC 16 ms
8,200 KB
testcase_26 AC 16 ms
8,296 KB
testcase_27 AC 26 ms
9,116 KB
testcase_28 AC 26 ms
9,060 KB
testcase_29 AC 35 ms
13,700 KB
testcase_30 AC 59 ms
15,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    N, K = map(int, input().split())
    if N == K:
        print(0)
        exit()
    A = tuple(map(int, input().split()))
    if K == 1:
        print(max(A) - min(A))
        exit()

    a = sorted(A)
    slice_ = [0] * (N - 1)
    for i in range(1, N):
        slice_[i-1] = (a[i] - a[i-1], i)
    slice_.sort(reverse=True)

    splt = sorted(slice_[:K-1], key=lambda x: x[1])
    s = 0
    total = 0
    for _, e in splt:
        total += max(a[s:e]) - min(a[s:e])
        s = e
    total += max(a[s:]) - min(a[s:])
    print(total)

main()
0