// Problem URL:
https://code.google.com/codejam/contest/8224486/dashboard#s=p1&a=0
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace GoogleCodeJam_Practice
{
class Program
{
int R;
int C;
int N;
string ProcessCase ( )
{
int ans=int.MaxValue;
int RxC = R * C;
bool zero=false;
if ( RxC % 2 == 1 )
{
if ( N <= ( RxC / 2 ) + 1 )
ans = 0;
}
else
{
if ( N <= RxC / 2 )
ans = 0;
}
if ( ans != 0 )
{
// alternative 1
List<int> nwalls=new List<int> ( );
for ( int y = 0; y < R; y++ )
{
int x0=( y + 1 ) % 2;
for ( int x = x0; x < C; x += 2 )
{
int nwCanBeRemoved=4;
if ( x == 0 ) nwCanBeRemoved--;
if ( x == C - 1 ) nwCanBeRemoved--;
if ( y == 0 ) nwCanBeRemoved--;
if ( y == R - 1 ) nwCanBeRemoved--;
nwalls.Add ( nwCanBeRemoved );
}
}
nwalls.Sort ( );
nwalls.Reverse ( ); // set up room to remove one by one, start from the one reducing more unhappiness
int nEmptyRoom = R * C - N;
int unhappyness = R * ( C - 1 ) + C * ( R - 1 ); // set to the max possible first
for ( int i = 0; i < nEmptyRoom; i++ )
{
unhappyness -= nwalls[i];
}
ans = unhappyness;
// alternative 2
nwalls = new List<int> ( );
for ( int y = 0; y < R; y++ )
{
int x0=( y % 2 );
for ( int x = x0; x < C; x += 2 )
{
int nwCanBeRemoved=4;
if ( x == 0 ) nwCanBeRemoved--;
if ( x == C - 1 ) nwCanBeRemoved--;
if ( y == 0 ) nwCanBeRemoved--;
if ( y == R - 1 ) nwCanBeRemoved--;
nwalls.Add ( nwCanBeRemoved );
}
}
nwalls.Sort ( );
nwalls.Reverse ( ); // set up room to remove one by one, start from the one reducing more unhappiness
nEmptyRoom = R * C - N;
unhappyness = R * ( C - 1 ) + C * ( R - 1 ); // set to the max possible first
for ( int i = 0; i < nEmptyRoom; i++ )
{
unhappyness -= nwalls[i];
}
if ( unhappyness < ans )
ans = unhappyness;
}
return ans.ToString ( );
}
void Start ( )
{
string file1;
string file2;
file1 = @"e:\test_input.txt";
file1 = @"e:\B-small-practice.in";
StreamReader infile = new StreamReader ( file1 );
file2 = @"e:\ztest.out";
StreamWriter outfile = new StreamWriter ( file2 );
PreProcess ( );
int nCases = int.Parse ( infile.ReadLine ( ) );
for ( int ncase=0; ncase < nCases; ncase++ )
{
string line = infile.ReadLine ( );
string[] inputs = line.Split ( new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries );
R = int.Parse ( inputs[0] );
C = int.Parse ( inputs[1] );
N = int.Parse ( inputs[2] );
// process case
string result = ProcessCase ( );
//WriteOutput ( string.Format ( "R,C,N: {0} {1} {2}", R, C, N ), outfile );
WriteOutput ( string.Format ( "Case #{0}: {1}", ncase + 1, result ), outfile );
}
infile.Close ( );
outfile.Close ( );
}
private void PreProcess ( )
{
}
private void WriteOutput ( string text, StreamWriter outfile )
{
outfile.WriteLine ( text );
Console.WriteLine ( text );
}
static void Main ( string[] args )
{
Program p = new Program ( );
p.Start ( );
}
}
}