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

  val Array(n, m) = readLine.trim.split(' ').map(_.toInt)
  val bits = readLine.trim.split(' ').map(_.toInt).toRunLength
  val target = readLine.trim.split(' ').map(_.toInt).toList
  println(solve(bits, target))

  def solve(from: List[Int], to: List[Int]): Int = {
    def swap(from: List[Int], to: List[Int]): Option[Int] = {
      (from, to) match {
        case (Nil, Nil) ⇒ Some(0)
        case (0::t, _) ⇒ straight(t, to)
        case (_, 0::t) ⇒ straight(from, t)
        case (ff::fs::ft, tf::tt) if tf < fs ⇒ straight(ff::fs-tf::ft, tt).map(_ + tf * ff)
        case (ff::fs::ft::fr, tf::tt) ⇒ swap(ff+ft::fr, tf-fs::tt).map(_ + fs * ff)
        case (ff::fs::Nil, tf::tt) ⇒ swap(ff::Nil, tf-fs::tt).map(_ + fs * ff)
        case _ ⇒ None
      }
    }
    def straight(from: List[Int], to: List[Int]): Option[Int] = {
      (from, to) match {
        case (Nil, Nil) ⇒ Some(0)
        case (0::t, _) ⇒ swap(t, to)
        case (_, 0::t) ⇒ swap(from, t)
        case (ff::ft, tf::tt) if ff <= tf ⇒ swap(ft, tf - ff::tt)
        case (ff::ft, tf::tt) ⇒ swap(ff-tf::ft, tt)
        case _ ⇒ None
      }
    }
    List(swap(from, to), straight(from, to)).flatten.min
  }
  implicit class ArrayWrapper(val value: Array[Int]) {
    def toRunLength: List[Int] = {
      var last = 0
      var result = Nil: List[Int]
      for (i ← value.indices.tail){
        if (value(i) != value(i - 1)) {
          result ::= i - last
          last = i
        }
      }
      (value.size - last::result).reverse
    }
  }
  implicit class Wrapper[T](val value: List[T]) {
    def minOption(implicit ord: Ordering[T]): Option[T] = {
      if (value.isEmpty) None
      else Some(value.min)
    }
  }
  implicit class Extension[T](val value: T) extends AnyVal {
    @inline def let[A](@inline func: T ⇒ A): A = func(value)
    @inline def also(@inline func: T ⇒ Unit): T = {func(value); value}
  }
}
