結果

問題 No.1391 ±1 Abs Sum
コンテスト
ユーザー zeronosu77108
提出日時 2021-02-12 22:37:43
言語 Kotlin
(2.3.20)
コンパイル:
kotlinc _filename_ -include-runtime -d main.jar
実行:
kotlin main.jar
結果
WA  
実行時間 -
コード長 652 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 11,298 ms
コンパイル使用メモリ 510,040 KB
実行使用メモリ 107,024 KB
最終ジャッジ日時 2026-08-19 04:21:43
合計ジャッジ時間 28,602 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 14 WA * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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