#QHD0000039. Chuyển chỗ ngồi (Armchairs)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Chuyển chỗ ngồi (Armchairs)
Armchairs
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
There are armchairs in a row, numbered to . Each chair initially holds exactly one person () or is empty (). The number of occupied chairs does not exceed half the total.
You may ask a person in an occupied chair to move to a currently empty chair . This operation costs minutes. Operations take place sequentially, not simultaneously, and you may perform as many as needed.
The goal is that every chair that was initially occupied becomes empty. Find the minimum total time needed. If no chair is occupied initially, the answer is .
Input
The first line contains . The second line contains values , each equal to or .
Output
Print one integer: the minimum total time, in minutes.
Subtasks
- Subtask 1 (20 points): ; ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; ; .
Examples
Input
7
1 0 0 1 0 0 1
Output
3
Explanation
The initially occupied seats are ; free seats are . Move the people from to , from to , and from to . Each move costs minute. All initially occupied chairs become empty, and the total of minutes is minimum.