import scala.annotation.tailrec
import scala.collection.mutable

object Main extends App {

  import scala.io.StdIn._
  import scala.math._

  val n = readLine.trim.toInt
  val edges = List.tabulate(n){_ ⇒ readLine.trim.toLong}.sorted(Ordering[Long].reverse)
  println(
    search(edges) match {
      case Some(r) ⇒ r
      case None ⇒ 0
    }
  )
  def search(edges: List[Long]): Option[Long] = {
    val susA = searchSingle(edges).flatMap{case (r, rest) ⇒ searchSingle(rest).map{case (s, _) ⇒ r + s}}
    val susB = searchDouble(edges)
    (susA, susB) match {
      case (Some(a), Some(b)) ⇒ Some(math.max(a, b))
      case (Some(a), _) ⇒ Some(a)
      case (_, Some(b)) ⇒ Some(b)
      case _ ⇒ None
    }
  }
  @tailrec
  def searchSingle(edges: List[Long]): Option[(Long, List[Long])] = {
    edges match {
      case a::b::c::t if a < b + c ⇒ Some(a + b + c, t)
      case _::(r@_::_::_) ⇒ searchSingle(r)
      case _ ⇒ None
    }
  }
  @tailrec
  def searchDouble(edges: List[Long]): Option[Long] = {
    edges match {
      case a::b::c::d::e::f::_ if canMakeTwoDouble(a, b, c, d, e, f) ⇒ Some(a + b + c + d + e + f)
      case _::(r@_::_::_::_::_::_) ⇒ searchDouble(r)
      case _ ⇒ None
    }
  }
  def canMakeTwoDouble(a: Long, b: Long, c: Long, d: Long, e: Long, f: Long): Boolean = {
    //一番を下げる、2番を上げる、3番を上げる
    // 111222
    // 112122 => 111222
    // 112212
    // 112221
    // 121122 => 112122 => 111222
    // 121212 => 112212
    // 121221 => 112221
    // 122112
    // 122121
    // 122211
    a < b + c && d < e + f ||
    a < b + e && c < d + f ||
    a < b + f && c < d + e ||
    a < d + e && b < c + f ||
    a < d + f && b < c + e ||
    a < e + f && b < c + d
  }


  implicit class Extenion[T](val value :T){
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
    def let[R](func: T ⇒ R): R = func(value)
  }
}
