結果

問題 No.1391 ±1 Abs Sum
ユーザー 👑 zeronosu77108zeronosu77108
提出日時 2021-02-12 22:39:14
言語 Kotlin
(1.9.23)
結果
WA  
実行時間 -
コード長 728 bytes
コンパイル時間 14,631 ms
コンパイル使用メモリ 416,616 KB
実行使用メモリ 102,216 KB
最終ジャッジ日時 2023-09-27 05:28:23
合計ジャッジ時間 44,712 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 311 ms
57,080 KB
testcase_01 AC 317 ms
57,252 KB
testcase_02 AC 316 ms
56,904 KB
testcase_03 AC 315 ms
56,896 KB
testcase_04 AC 316 ms
57,248 KB
testcase_05 WA -
testcase_06 AC 317 ms
57,180 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 830 ms
70,500 KB
testcase_15 AC 864 ms
70,760 KB
testcase_16 AC 829 ms
78,392 KB
testcase_17 AC 868 ms
76,548 KB
testcase_18 AC 900 ms
79,788 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 1,053 ms
101,576 KB
testcase_32 AC 773 ms
70,032 KB
testcase_33 AC 767 ms
69,932 KB
testcase_34 AC 950 ms
82,436 KB
testcase_35 AC 297 ms
52,740 KB
testcase_36 AC 950 ms
102,216 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:9:23: warning: unnecessary non-null assertion (!!) on a non-null receiver of type Long
    val amin = a.min()!!
                      ^
Main.kt:10:23: warning: unnecessary non-null assertion (!!) on a non-null receiver of type Long
    val amax = a.max()!!
                      ^

ソースコード

diff #

import kotlin.collections.*
import kotlin.math.*

@kotlin.ExperimentalStdlibApi
fun main() {
    val (n, k) = readLine()!!.split(" ").map { it.toInt() }
    val a = readLine()!!.split(" ").map { it.toLong() }.sorted()

    val amin = a.min()!!
    val amax = a.max()!!

    fun f(ai : Long) : Long {
        val l = a.map { abs(ai - it) }.toMutableList().sorted()
        return -l.sum() + l.take(k).sum() * 2
    }

    var ans = minOf(f(amin), f(amax))
    if (n%2 == 1) ans = minOf(ans, f(a[n/2]))
    else {
        ans = minOf(ans,  f((a[n/2-1] + a[n/2]) / 2 ) )
        ans = minOf(ans,  f(a[n/2-1]))
        ans = minOf(ans,  f((a[n/2-1] + a[n/2]) / 2 +1) )
        ans = minOf(ans,  f(a[n/2]))
    }

    println(ans)
}
0