結果

問題 No.1687 What the Heck?
ユーザー 👑 箱
提出日時 2021-09-24 22:11:41
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 499 ms / 2,000 ms
コード長 1,092 bytes
コンパイル時間 17,911 ms
コンパイル使用メモリ 423,768 KB
実行使用メモリ 62,280 KB
最終ジャッジ日時 2023-09-18 21:30:13
合計ジャッジ時間 27,645 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 286 ms
54,536 KB
testcase_01 AC 282 ms
52,596 KB
testcase_02 AC 286 ms
54,572 KB
testcase_03 AC 287 ms
54,708 KB
testcase_04 AC 280 ms
52,480 KB
testcase_05 AC 280 ms
52,368 KB
testcase_06 AC 281 ms
52,824 KB
testcase_07 AC 430 ms
58,048 KB
testcase_08 AC 451 ms
62,152 KB
testcase_09 AC 432 ms
57,752 KB
testcase_10 AC 411 ms
58,056 KB
testcase_11 AC 421 ms
55,936 KB
testcase_12 AC 495 ms
62,136 KB
testcase_13 AC 499 ms
60,568 KB
testcase_14 AC 490 ms
60,360 KB
testcase_15 AC 486 ms
62,280 KB
testcase_16 AC 483 ms
62,272 KB
testcase_17 AC 365 ms
53,016 KB
testcase_18 AC 486 ms
62,120 KB
testcase_19 AC 286 ms
52,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter
import java.util.*
import kotlin.math.*

fun PrintWriter.solve() {
    val n = nextInt()
    val a = Array(n) { nextInt() - 1 }
    val pos = Array(n) { 0 }
    for (i in 0 until n) {
        pos[a[i]] = i
    }
    var max = 0L
    var v = n * (n + 1L) / 2
    for (i in n - 1 downTo 0) {
        v -= 2 * (pos[i] + 1)
        max = maxOf(max, v)
        v += pos[i] + 1
    }
    println(max)
}

fun main() {
    Thread(null, {
        val writer = PrintWriter(System.out, false)
        writer.solve()
        writer.flush()
    }, "solve", 1.shl(26))
        .apply { setUncaughtExceptionHandler { _, e -> e.printStackTrace(); kotlin.system.exitProcess(1) } }
        .apply { start() }.join()
}

// region Scanner
private var st = StringTokenizer("")
private val br = System.`in`.bufferedReader()

fun next(): String {
    while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine())
    return st.nextToken()
}

fun nextInt() = next().toInt()
fun nextLong() = next().toLong()
fun nextLine() = br.readLine()
fun nextDouble() = next().toDouble()
// endregion
0