#include using namespace std; #define bokusunny ios::sync_with_stdio(false), cin.tie(nullptr); void solve() { int N, M; cin >> N >> M; vector A(N); for (int i = 0; i < N; i++) cin >> A[i]; vector B(M); for (int i = 0; i < M; i++) cin >> B[i]; vector AA; for (auto &a : A) { AA.push_back(0); for (int i = 0; i < a; i++) AA.push_back(1); } vector BB; for (auto &b : B) { BB.push_back(0); for (int i = 0; i < b; i++) BB.push_back(1); } const int INFINT = 1 << 30; int sz_a = (int)AA.size(); int sz_b = (int)BB.size(); vector dp(sz_a + 1, vector(sz_b + 1, INFINT)); dp[0][0] = 0; for (int i = 0; i <= sz_a; i++) { for (int j = 0; j <= sz_b; j++) { if (i > 0) dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1); if (j > 0) dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1); if (i > 0 && j > 0) { dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + (int)(AA[i - 1] != BB[j - 1])); } } } cout << dp[sz_a][sz_b] << endl; } int main() { bokusunny; solve(); return 0; }