package Yukicoder /** * Created by hichikawa on 2015/11/12. */ fun main(args: Array) { fun readLineLongArray() : List { val str = readLine() as String val arrStr = str.split(" ") val ret = arrStr.map { it.toLong() } return ret } fun readLineLong() : Long { val str = readLine() as String return str.toLong() } fun readLineInt() : Int { val str = readLine() as String return str.toInt() } fun readLineIntArray() : List { val str = readLine() as String val arrStr = str.split(" ") val ret = arrStr.map { it.toInt() } return ret } val num = readLineInt() var countSpace = 0.toLong() as Long var maxNum = Integer.MIN_VALUE for (i in 0..(num-1)) { val inputArrInt = readLineIntArray() val lenLine = inputArrInt[0] + inputArrInt[1] * 4 if (maxNum == Integer.MIN_VALUE) { maxNum = lenLine } else if (maxNum == lenLine) { } else if (maxNum > lenLine) { if (maxNum % 2 == lenLine % 2) { countSpace += ((maxNum - lenLine) / 2).toLong() } else { println(-1) return } } else { if (maxNum % 2 == lenLine % 2) { countSpace += ((lenLine - maxNum) * i / 2).toLong() maxNum = lenLine } else { println(-1) return } } } println(countSpace) }