import java.util.Scanner object Problem297 { def calcMaxValue(digits: Seq[Int], ops: Seq[String]): Long = { val maxNumberOfDigits = digits.size - ops.size val maxDigits = digits.reverse.take(maxNumberOfDigits).mkString.toLong val remainDigits = digits.reverse.drop(maxNumberOfDigits) val remainValues: Seq[Int] = remainDigits.zip(ops).map(x => if (x._2 == "+") x._1 else -x._1) maxDigits + remainValues.sum } def calcMinValue(digits: Seq[Int], ops: Seq[String]): Long = { if (ops.forall(_ == "+")) { val groupedInt: Seq[Int] = digits.reverse.grouped(ops.size + 1).map(x => x.sum).toSeq groupedInt.zipWithIndex.map(x => x._1 * Math.pow(10, x._2).toInt).sum } else { val maxNumberOfDigits = digits.size - ops.size val maxDigits = digits.reverse.take(maxNumberOfDigits).mkString.toLong val remainDigits = digits.reverse.drop(maxNumberOfDigits) val minDigit = remainDigits.last val remainOtherDigits = remainDigits.init val remainOps = ops.init.reverse val remainValues: Seq[Int] = remainOtherDigits.zip(remainOps).map(x => if (x._2 == "+") x._1 else -x._1) minDigit + remainValues.sum - maxDigits } } def proc(c: Seq[String]): (Long, Long) = { val digits: Seq[Int] = c.filter(_.matches("[0-9]")).map(_.toInt).sorted val ops: Seq[String] = c.filter(_.matches("[+-]")).sorted (calcMaxValue(digits, ops), calcMinValue(digits, ops)) } def main(args: Array[String]) { val sc = new Scanner(System.in) val n = sc.nextInt() val c = Seq.fill(n)(sc.next()) val result: (Long, Long) = proc(c) println(result._1 + " " + result._2) } }