object Main extends App {
  import scala.io.StdIn._

  val Array(n, m) = readLine.trim.split(' ').map(_.toInt)
  val first = readLine.trim.split(' ').map(_.toInt).toList
  val second = readLine.trim.split(' ').map(_.toInt).toList
  println(solve(first, second))
  def solve(first: List[Int], second: List[Int]): Int = {
    val memo = Array.tabulate(2){_ ⇒ Array.tabulate(2){_ ⇒ Array.tabulate(first.length + 1){f ⇒ Array.tabulate(second.length + 1){s ⇒ Array.tabulate(f + s + 1){_ ⇒ None: Option[Int]}}}}}
    def init(f: List[Int], s: List[Int]): Int = {
      f match {
        case h::t if h != -1 ⇒ math.max(secondTurn(t, s, 1, 0, 1, h, 0), secondTurn(f, s, 0, 0))
        case h::t ⇒ math.max(secondTurn(t, s, 1, 0, 1, 0, 0, 0), secondTurn(f, s, 0, 0, 0, 0, 0, 1))
        case Nil ⇒ secondTurn(f, s, 0, 0)
      }
    }
    def firstTurn(f: List[Int], s: List[Int], usedF: Int, usedS: Int, stackSize: Int = 0, lastF: Int = 0, lastS: Int = 0, passCount: Int = 0): Int = {
      if (passCount == 2) 0
      else memo(0)(passCount)(usedF)(usedS)(stackSize) match {
        case Some(r) ⇒ r
        case None ⇒
          (f match {
            case Nil if stackSize == 0 ⇒ secondTurn(f, s, usedF, usedS, 0, 0, 0, passCount + 1)
            case Nil ⇒ secondTurn(f, s, usedF, usedS) + lastF - lastS
            case h::t if stackSize == 0 ⇒
              if (h != -1) math.max(secondTurn(f, s, usedF, usedS, 0, 0, 0, passCount + 1), secondTurn(t, s, usedF + 1, usedS, 1, h, 0))
              else math.max(secondTurn(f, s, usedF, usedS, 0, 0, 0, passCount + 1), secondTurn(t, s, usedF + 1, usedS, 1, 0, 0))
            case h::t ⇒
              if (h != -1) math.max(secondTurn(f, s, usedF, usedS) + lastF - lastS, secondTurn(t, s, usedF + 1, usedS, stackSize + 1, lastF + h, lastS))
              else math.max(secondTurn(f, s, usedF, usedS) + lastF - lastS, secondTurn(t, s, usedF + 1, usedS, stackSize + 1, lastF, 0))
          }).also{i ⇒ memo(0)(passCount)(usedF)(usedS)(stackSize) = Some(i)}
      }
    }
    def secondTurn(f: List[Int], s: List[Int], usedF: Int, usedS: Int, stackSize: Int = 0, lastF: Int = 0, lastS: Int = 0, passCount: Int = 0): Int = {
      if (passCount == 2) 0
      else memo(1)(passCount)(usedF)(usedS)(stackSize) match {
        case Some(r) ⇒ r
        case None ⇒
          (s match {
            case Nil if stackSize == 0 ⇒ firstTurn(f, s, usedF, usedS, 0, 0, 0, passCount + 1)
            case Nil ⇒ firstTurn(f, s, usedF, usedS) + lastF - lastS
            case h::t if stackSize == 0 ⇒
              if (h != -1) math.min(firstTurn(f, s, usedF, usedS, 0, 0,0, passCount + 1), firstTurn(f, t, usedF, usedS + 1, 1, 0, h))
              else math.min(firstTurn(f, s, usedF, usedS, 0, 0,0, passCount + 1), firstTurn(f, t, usedF, usedS + 1, 1, 0, 0))
            case h::t ⇒
              if (h != -1) math.min(firstTurn(f, s, usedF, usedS) + lastF - lastS, firstTurn(f, t, usedF, usedS + 1, stackSize + 1, lastF, lastS + h))
              else math.min(firstTurn(f, s, usedF, usedS) + lastF - lastS, firstTurn(f, t, usedF, usedS + 1, stackSize + 1, 0, lastS))
          }).also{i ⇒ memo(1)(passCount)(usedF)(usedS)(stackSize) = Some(i)}
      }
    }
    init(first, second)
  }
  implicit class Scope[T](val value: T) extends AnyVal {
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
  }
}


Measured memory_bytes: 62028