import java.util.Scanner object Problem297 { def calcMaxValue(digits: Seq[Int], ops: Seq[String]): Long = { val maxNumberOfDigits = digits.size - ops.size val (maxDigitsList, remainDigits) = digits.reverse.splitAt(maxNumberOfDigits) val maxDigit = maxDigitsList.mkString.toLong val remainSignedDigits: Seq[Int] = remainDigits.zip(ops).map(x => if (x._2 == "+") x._1 else -x._1) maxDigit + remainSignedDigits.sum } def calcMinValue(digits: Seq[Int], ops: Seq[String]): Long = { if (ops.forall(_ == "+")) { val groupedInt = digits.reverse.grouped(ops.size + 1).map(x => x.sum) groupedInt.zipWithIndex.map(x => x._1 * Math.pow(10, x._2).toInt).sum } else { val maxNumberOfDigits = digits.size - ops.size val (maxDigitsList, otherDigits) = digits.reverse.splitAt(maxNumberOfDigits) val remainOps = ops.init.reverse val (minDigit, remainDigits) = (otherDigits.last, otherDigits.init) val remainSignedDigits: Seq[Int] = remainDigits.zip(remainOps).map(x => if (x._2 == "+") x._1 else -x._1) val maxDigit = maxDigitsList.mkString.toLong minDigit + remainSignedDigits.sum - maxDigit } } 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) } }