Skip to content

Instantly share code, notes, and snippets.

/TMO.scala Secret

Created January 23, 2015 12:55
Show Gist options
  • Save anonymous/884ec745c0afd67c2aa7 to your computer and use it in GitHub Desktop.
Save anonymous/884ec745c0afd67c2aa7 to your computer and use it in GitHub Desktop.
import java.io.{FileOutputStream, FileInputStream}
import scala.io.StdIn
object Main extends App {
Console.setIn(new FileInputStream("A-large-practice.in"))
Console.setOut(new FileOutputStream("A-large-practice.out"))
def solve(k: Int, frequency: Array[Long]) : Long = frequency.sortWith(_ > _)
.grouped(k.toInt).zipWithIndex
.foldLeft(0L)((acc, x) => acc + x._1.sum * (x._2 + 1))
val cases = StdIn.readLine().toInt
(1 to cases) foreach {n =>
val Array(_, k, _) = StdIn.readLine().split(" ")
val frequency = StdIn.readLine().split(" ").map(_.toLong)
println(s"Case #$n: ${solve(k.toInt, frequency)}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment