시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
1 초 128 MB 1056 384 282 41.349%

문제

백준이는 한 작은 회사에 취직했다. 이 회사에서 백준이는 소스 코드의 뒤죽박죽인 인덴트를 고치고 있다. 인덴트는 각 줄을 탭 키를 이용해 들여 쓰는 것을 말한다.

다행히 백준이가 사용하는 편집기는 연속된 줄을 그룹으로 선택하고, 여기에서 각 줄의 앞에 탭을 추가하거나, 삭제할 수 있다.

백준이를 도와 코드의 뒤죽박죽인 인덴트를 예쁘게 고치는 방법을 생각해보자.

줄의 개수 N과 각 줄의 앞에 있는 탭의 개수와 올바른 탭의 개수가 주어진다.

이때, 한 번 편집을 할 때, 다음과 같은 명령을 수행할 수 있다.

  • 연속된 줄을 그룹으로 선택한다.
  • 선택된 줄의 앞에 탭 1개를 추가하거나 삭제한다.

위의 두 명령은 각각 선택된 줄의 개수와 상관없이 한 번의 편집이다.

만약, 선택한 줄 중에 단 한 줄이라도 탭이 없을 경우에는, 탭을 삭제하는 명령을 수행할 수 없다.

백준이가 몇 번 편집 만에 코드의 인덴트를 올바르게 고칠 수 있는지 구하는 프로그램을 작성하시오. 이때, 편집 회수의 최솟값을 구해야 한다.

입력

 

첫째 줄에 줄의 개수 N(N<=1,000)이 주어진다. 둘째 줄에는 현재 줄에 있는 탭의 개수가 주어지며, 1번째 줄부터 순서대로 주어진다. 탭의 개수는 0보다 크거나 같고, 80보다 작거나 같은 정수이다. 셋째 줄에는 각 줄의 올바른 탭의 개수가 주어진다. 1번째 줄부터 순서대로 주어지며, 이 값도 0보다 크거나 같고, 80보다 작거나 같은 정수이다.

 

출력

첫째 줄에 코드의 인덴트를 올바르게 고치는 편집 회수의 최솟값을 출력한다.

예제 입력 1

4
1 2 3 4
3 1 1 0

예제 출력 1

6
W3sicHJvYmxlbV9pZCI6IjI4NzkiLCJwcm9ibGVtX2xhbmciOiIwIiwidGl0bGUiOiJcdWNmNTRcdWI1MjlcdWM3NDAgXHVjNjA4XHVjMDU4XHVhYzh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BpbnRlZ2VycyBLPHN1Yj5pPFwvc3ViPiAoMCAmbGU7IEs8c3ViPmk8XC9zdWI+ICZsZTsgODApLCBzcGVjaWZ5aW5nIHRoZSBudW1iZXIgb2YgdGFicyB0aGF0IFp2b25rZWMgd291bGQgbGlrZSBhdCB0aGUgc3RhcnQgb2YgaS10aCBsaW5lLiZuYnNwOzxcL3A+XHJcbiIsIm91dHB1dCI6IjxwPlRoZSBmaXJzdCBhbmQgb25seSBsaW5lIG9mIG91dHB1dCBtdXN0IGNvbnRhaW4gdGhlIHJlcXVpcmVkIG51bWJlciwgYXMgc3BlY2lmaWVkIGluIHRoZSBwcm9ibGVtIHN0YXRlbWVudC4mbmJzcDs8XC9wPlxyXG4iLCJoaW50IjoiIiwib3JpZ2luYWwiOiIxIiwicHJvYmxlbV9sYW5nX2NvZGUiOiJcdWM2MDFcdWM1YjQifV0=

출처

Contest > Croatian Open Competition in Informatics > COCI 2010/2011 > Contest #1 5번

  • 문제를 번역한 사람: baekjoon
  • 문제의 오타를 찾은 사람: jh05013