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# (Mono 4.6.2.0) (judge verdict: Accepted)

// Undone
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
public class Program {
  public static void Main() {
    int N = cin(); string S = ReadStr();
    // int W = 0, B = 0;
    List<int> count = new List<int>(); // first and last means white count
    if (S[0] == '#') count.Add(0);
    char last = ' ';
    for (int i = 0; i < N; i++) {
      if (S[i] == last) {
        count[count.Count()-1] = count.Last() + 1;
      } else {
        count.Add(1);
        last = S[i];
      }
    }
    if (S[N-1] == '#') count.Add(0);

    // 本番で累積和使ったの初めて、嬉しい。
    int[] sumW = new int[count.Count()+1], sumB = new int[count.Count()+1];
    for (int i = 0; i < count.Count(); i++) {
      if (i % 2 == 0) {
        sumW[i+1] = sumW[i] + count[i];
        sumB[i+1] = sumB[i];
      } else {
        sumB[i+1] = sumB[i] + count[i];
        sumW[i+1] = sumW[i];
      }
    }

    // どこまで W にするかを検討すればよい
    int ans = int.MaxValue;
    for (int i = 1; i < count.Count()+1; i+=2) {
      ans = Math.Min(ans, sumB[i]-sumB[0] + sumW[count.Count()]-sumW[i]);
    }
    ans = (ans == N) ? 0 : ans;
    Console.WriteLine(ans);
  }

  private static List<int> _var;
  public static int cin() { if (_var == null || _var.Count() == 0) { _var = Console.ReadLine().Split(' ').Select(int.Parse).ToList<int>(); } int ans = _var[0]; _var.RemoveAt(0); return ans; }
  public static void Swap<T>(ref T a, ref T b) {T temp = a;a = b;b = temp;}
  public static string ReadStr() { return Console.ReadLine(); }
  public static int ReadInt() { return int.Parse(Console.ReadLine()); }
  public static double ReadDouble() { return double.Parse(Console.ReadLine()); }
  public static long ReadLong() { return long.Parse(Console.ReadLine()); }
  public static char ReadChar() { return Console.ReadLine()[0]; }
  public static string[] ReadStringArray() { return Console.ReadLine().Split(' '); }
  public static int[] ReadIntArray() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
  public static double[] ReadDoubleArray() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); }
  public static long[] ReadLongArray() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
  public static char[] ReadCharArray() { return Console.ReadLine().ToArray(); }
  public static string[] ReadCharStringArray() { return Console.ReadLine().ToCharArray().Select(c => c.ToString()).ToArray(); }
}