Problem:
Score : 300 points

Problem Statement

There are N stones arranged in a row. Every stone is painted white or black.
A string S represents the color of the stones. The i-th stone from the left is white if the i-th character of S is ., and the stone is black if the character is #.

Takahashi wants to change the colors of some stones to black or white so that there will be no white stone immediately to the right of a black stone.
Find the minimum number of stones that needs to be recolored.

Constraints

1 \leq N \leq 2\times 10^5

S is a string of length N consisting of . and #.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of stones that needs to be recolored.

Sample Input 1

3
#.#

Sample Output 1

1

It is enough to change the color of the first stone to white.

Sample Input 2

5
#.##.

Sample Output 2

2

Sample Input 3

9
.........

Sample Output 3

0

Solution in PHP7 (7.0.15) (judge verdict: Wrong Answer)

<?php

/*
N個の石が一列に並んでおり、すべての石は白か黒で塗られています。
石の状態は長さNの文字列Sで表され、Sのi文字目が . のとき左からi個目の石が白であり、
# のとき左からi個目の石が黒であることを表します。

高橋君は、0個以上の石の色を黒または白に変更し、
黒い石のすぐ右に白い石があるような箇所がないようにしたいです。
色を変更する必要のある石の個数の最小値を求めてください。
*/

fscanf(STDIN, "%d", $N);
$S = str_split(trim(fgets(STDIN)));

# 全部黒にする場合
$all_black = 0;
for ( $i = 0 ;$i < $N ;$i++ ) {
    if ($S[$i] == '.') {
        $all_black++;
    }
}

# 全部白にする場合
$all_white = 0;
for ( $i = 0 ;$i < $N ;$i++ ) {
    if ($S[$i] == '#') {
        $all_white++;
    }
}

# 連続した黒白の石を交互にした配列を作る
$count_white = 0;
$count_black = 0;
$sum = [];
for ( $i = 0 ;$i < $N ;$i++ ) {
    if ($S[$i] == '#') {
        $count_black++;
        if ($count_white > 0) {
            $sum[] = $count_white;
            $count_white = 0;
        }
    }
    if ($S[$i] == '.') {
        $count_white++;
        if ($count_black > 0) {
            $sum[] = $count_black;        
            $count_black = 0;
        }
    }
}

# 黒から始まっている場合、先頭を白始まりにする
if ($S[0] == '#') {
    array_unshift($sum, 0);
}
 
$index = count($sum);
$ans = [];
for ( $i = 0 ;$i < $index; $i+=2 ) {
    $ans[] = count_white($sum, $index) + count_black($sum, $index);
}
$ans[] = $count_black;
$ans[] = $count_white;

echo min($ans);

function count_white($sum, $cut) {
    $count_white = 0;
    for ( $i = 0 ; $i < $cut ; $i+=2 ) {
        if ($i % 2 == 0) {
            $count_white+= $sum[$i];
        }
    }    
    return $count_white;
}

function count_black($sum, $cut) {
    $N = count($sum);
    $count_black = 0;
    for ( $i = $cut ;$i < $N ;$i+=2 ) {
        if ($i % 2 == 0) {
            $count_black+= $sum[$i];
        }
    }    
    return $count_black;
}