[백준 1049 - Kotlin] 기타줄

문제링크

import java.io.BufferedReader
import java.io.BufferedWriter

private lateinit var bufferedReader: BufferedReader
private lateinit var bufferedWriter: BufferedWriter

fun main() {
    bufferedReader = System.`in`.bufferedReader()
    bufferedWriter = System.out.bufferedWriter()

    // 1. get (n, m)
    val (n, m) = bufferedReader
        .readLine()
        .split(" ")
        .map { it.toInt() }

    // 2. get string info
    var minPack = Int.MAX_VALUE
    var minPiece = Int.MAX_VALUE
    for (i in 0 until m) {
        val (pack, piece) = bufferedReader
            .readLine()
            .split(" ")
            .map { it.toInt() }
        if (pack < minPack) minPack = pack
        if (piece < minPiece) minPiece = piece
    }

    if (minPack >= minPiece * 6) bufferedWriter.write("${minPiece * n}")
    else {
        var total = 0
        total += minPack * (n / 6)
        val remain = n % 6
        total += if (minPack > minPiece * remain) minPiece * remain
        else minPack
        bufferedWriter.write("$total")
    }

    bufferedReader.close()
    bufferedWriter.close()
}

좋은 웹페이지 즐겨찾기