結果

問題 No.1391 ±1 Abs Sum
ユーザー zeronosu77108
提出日時 2021-02-12 22:37:43
言語 Kotlin
(2.1.0)
結果
WA  
実行時間 -
コード長 652 bytes
コンパイル時間 14,765 ms
コンパイル使用メモリ 446,116 KB
実行使用メモリ 112,960 KB
最終ジャッジ日時 2024-07-19 23:28:51
合計ジャッジ時間 42,320 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 14 WA * 20
権限があれば一括ダウンロードができます
コンパイルメッセージ
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] + a[n/2]) / 2 +1) )
    }

    println(ans)
}
0