import scala.collection.mutable import scala.io.StdIn.* import scala.math.* import scala.util.chaining.* def distance(a: String, b: String): Int = val result = Array.fill(2){Array.tabulate(b.length + 1){i => i}} for i <- a.indices do val prev = i & 1 val current = (i & 1) ^ 1 result(current)(0) = i + 1 for j <- b.indices do if a(i) == b(j) then result(current)(j + 1) = min(result(prev)(j), min(result(current)(j), result(prev)(j + 1)) + 1) else result(current)(j + 1) = min(result(prev)(j), min(result(current)(j), result(prev)(j + 1))) + 1 result(a.length & 1).last @main def main = val Array(n, m) = readLine().split(' ').map(_.toInt) val from = readLine().split(' ').map(_.toInt) val to = readLine().split(' ').map(_.toInt) val fromStr = mutable.StringBuilder() for a <- from do fromStr.append(' ') for _ <- 0 until a do fromStr.append('1') val toStr = mutable.StringBuilder() for b <- to do toStr.append(' ') for _ <- 0 until b do toStr.append('1') val result = distance(fromStr.toString(), toStr.toString()) println(result)