object Main extends App {
  val src: List[List[Int]] = io.Source.stdin.getLines.map(_.split(" ").map(_.toInt).toList).toList.init
  val tuples = src.zip(src.tail)
  val filteredTup = tuples.filter(tuples.indexOf(_) % 2 == 0)

  def maxPair(sum: List[Int], nums: List[Int]): Option[Int] = {
    val sumList = (for { i <- nums
                         j <- nums
                         if (i+j) <= sum(1)
                         if (nums.indexOf(i) != nums.indexOf(j))
                   } yield (i+j))

    if (sumList.isEmpty) None else Some(sumList.max)
  }

  val li = for { (sum, nums) <- filteredTup
            } yield maxPair(sum, nums)

  li.map { case Some(x) => println(x)
           case None => println("NONE")
  }
}

