Skip to content

Instantly share code, notes, and snippets.

@myeesan
Created January 23, 2015 13:06
Show Gist options
  • Save myeesan/cf52ea2a89a4b755e17d to your computer and use it in GitHub Desktop.
Save myeesan/cf52ea2a89a4b755e17d to your computer and use it in GitHub Desktop.
import scala.io.Source
import java.io.PrintWriter
object TextMessage {
val in = Source.fromFile("small.in").getLines
val out = new PrintWriter("small.out")
case class TestCase(p: Int, k: Int, l: Int, freq: List[Int]) {
def solve: Int = {
val l = freq.sorted.reverse
var currentPlacement: Int = 1
var sum = 0
for (i <- 0 until l.size) {
if (i % k == 0 && i != 0) {
currentPlacement += 1
}
sum += currentPlacement * l(i)
}
sum
}
}
def main(args: Array[String]) {
val nOfTestCases = in.next.toInt
val tests = for {
i <- 1 to nOfTestCases
val Array(p, k, l) = in.next.split(" ").map(_.toInt)
val freq = in.next.split(" ").map(_.toInt).toList
} yield {
TestCase(p, k, l, freq)
}
tests.map(_.solve).zipWithIndex.map(t => s"Case #${t._2 + 1}: ${t._1}").foreach { o => out.println(o); println(o) }
out.flush
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment