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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
 
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rrep(i, n) for(int i = ((int)(n)-1); i >= 0; i--)
#define all(x) (x).begin(),(x).end()
 
using namespace std;
using ll = long long;
 
struct edge { int to, cost; };
 
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> P;
 
const int INF = 1e9;
//const ll INF = 1e18;
const double EPS = 1e-10;
const int MOD = 1e9+7;
const double PI = acos(-1.0);

const int mx4[] = {0,1,0,-1};
const int my4[] = {1,0,-1,0};
const int mx8[] = {0,1,1,1,0,-1,-1,-1};
const int my8[] = {1,1,0,-1,-1,-1,0,1};

int main()
{
	int n;
	cin >> n;
	string s;
	cin >> s;
	int flag = 1;
	for(int i = 0; i < s.size(); i++){
		if(s[i] == '#') flag = 0;
	}
	if(flag || n == 1){
		cout << 0 << endl;
		return 0;
	}

	int w_cnt[s.size()+10] = {},b_cnt[s.size()+10] = {};
	for(int i = 0; i < s.size(); i++){
		if(s[i] == '.') w_cnt[i]++;
		else b_cnt[i]++;
	}

	for(int i = s.size()-2; i >= 0; i--) w_cnt[i] += w_cnt[i+1];
	for(int i = 1; i < s.size(); i++) b_cnt[i] += b_cnt[i-1]; 

	int ma = b_cnt[n-1];
	for(int i = 0; i < s.size(); i++){
		if(s[i] == '#'){
			if(i == 0){
				ma = min(w_cnt[i+1],ma);
			}else if(i == s.size()-1){
				ma = min(b_cnt[i]-1,ma);
			}else{
				ma = min(b_cnt[i]-1+w_cnt[i+1],ma);
			}
		}
	}
	cout << ma << endl;
	return 0;
}