Cupboards (Codeforces 248A)
Tags: Implement, implementation

One foggy Stockholm morning, Karlsson decided to snack on some jam in his friend Lillebror Svantenson's house. Fortunately for Karlsson, there wasn't anybody in his friend's house. Karlsson was not going to be hungry any longer, so he decided to get some food in the house.Karlsson's gaze immediately fell on n wooden cupboards, standing in the kitchen. He immediately realized that these cupboards have hidden jam stocks. Karlsson began to fly greedily around the kitchen, opening and closing the cupboards' doors, grab and empty all the jars of jam that he could find.And now all jars of jam are empty, Karlsson has had enough and does not want to leave traces of his stay, so as not to let down his friend. Each of the cupboards has two doors: the left one and the right one. Karlsson remembers that when he rushed to the kitchen, all the cupboards' left doors were in the same position (open or closed), similarly, all the cupboards' right doors were in the same position (open or closed). Karlsson wants the doors to meet this condition as well by the time the family returns. Karlsson does not remember the position of all the left doors, also, he cannot remember the position of all the right doors. Therefore, it does not matter to him in what position will be all left or right doors. It is important to leave all the left doors in the same position, and all the right doors in the same position. For example, all the left doors may be closed, and all the right ones may be open.Karlsson needs one second to open or close a door of a cupboard. He understands that he has very little time before the family returns, so he wants to know the minimum number of seconds t, in which he is able to bring all the cupboard doors in the required position.Your task is to write a program that will determine the required number of seconds t.

Input: The first input line contains a single integer n — the number of cupboards in the kitchen (2 ≤ n ≤ 104). Then follow n lines, each containing two integers li and ri (0 ≤ li, ri ≤ 1). Number li equals one, if the left door of the i-th cupboard is opened, otherwise number li equals zero. Similarly, number ri equals one, if the right door of the i-th cupboard is opened, otherwise number ri equals zero.The numbers in the lines are separated by single spaces.

Output: In the only output line print a single integer t — the minimum number of seconds Karlsson needs to change the doors of all cupboards to the position he needs.


Sample Inputs:
5
0 1
1 0
0 1
1 1
0 1

Sample Outputs:
3

Solution (Python):
def i():
    n = int(input())
    l = r = 0
    for x in range(n):
        s = input()
        l += s[0] == '1'
        r += s[2] == '1'
    c = min([l, n - l])
    c += min(r, n - r)
    print(c)
i()

Solution (Python):
n = int(input())
left = 0
right = 0
for _ in range(n):
    (l, r) = map(int, input().split())
    left += l
    right += r
print(min(n - left, left) + min(n - right, right))

Solution (Python):
ans = int(input())
arr1 = []
arr2 = []
for _ in range(ans):
    (a, b) = map(int, input().split())
    arr1.append(a)
    arr2.append(b)
count_arr1_0 = arr1.count(0)
count_arr1_1 = arr1.count(1)
count_arr2_0 = arr2.count(0)
count_arr2_1 = arr2.count(1)
print(min(count_arr1_0, count_arr1_1) + min(count_arr2_0, count_arr2_1))