object Main extends App {

  case class State[S, A](run: S => (S, A)) {
    def map[B](f: A => B) = State((s: S) => {
      val (s1, a) = run(s)
      (s1, f(a))
    })

    def flatMap[B](f: A => State[S, B]) = State((s: S) => {
      val (s1, a) = run(s)
      f(a).run(s1)
    })
  }

  object State {
    def unit[S, A](a: A): State[S, A] = State(s => (s, a))
  }

  implicit class ListOps[S, A](stlist: List[State[S, A]]) {
    def sequence: State[S, List[A]] =
      stlist.foldRight(State.unit[S, List[A]](List[A]()))((h: State[S, A], acc: State[S, List[A]]) => h.flatMap(a => acc.map(list => a :: list)))
  }

  def toState(s: String): State[List[Int], Unit] = State((stack: List[Int]) => stack match {
    case (h1 :: h2 :: t) => s match {
      case "*" => ((h2 * h1) :: t, ())
      case "+" => ((h2 + h1) :: t, ())
      case "-" => ((h2 - h1) :: t, ())
      case "/" => ((h2 / h1) :: t, ())
      case i   => (i.toInt :: stack, ())
    }
    case _ => (s.toInt :: stack, ())
  })

  val ans = readLine.split(" ")
    .toList
    .map(toState)
    .sequence
    .run(List[Int]())
    ._1.head
  println(ans)
}
