import java . util . * ; import java . io . * ; public class Main implements Runnable { public Scanner in ; public PrintWriter out ; Main ( ) throws IOException { in = new Scanner ( new File ( " in " ) ) ; out = new PrintWriter ( new File ( " out " ) ) ; } void close ( ) throws IOException { out . close ( ) ; } public void run ( ) { int tn = in . nextInt ( ) ; for ( int test = 1 ; test <= tn ; test ++ ) { int n = in . nextInt ( ) ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { String s = in . next ( ) ; a [ i ] = s . lastIndexOf ( "1" ) ; } int r = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int j = i ; while ( a [ j ] > i ) { j ++ ; } r += j - i ; int t = a [ j ] ; while ( j > i ) { a [ j ] = a [ j - 1 ] ; j -- ; } a [ i ] = t ; } out . println ( " Case ▁ # " + test + " : ▁ " + r ) ; } try { close ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } public static void main ( String [ ] args ) throws IOException { new Thread ( new Main ( ) ) . start ( ) ; } }

import java . io . BufferedReader ; import java . io . FileReader ; import java . io . PrintWriter ; public class A implements Runnable { public static void main ( String [ ] args ) { new Thread ( new A ( ) ) . start ( ) ; } public void run ( ) { try { solve ( ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } } private void solve ( ) throws Exception { BufferedReader br = new BufferedReader ( new FileReader ( " a . in " ) ) ; PrintWriter pw = new PrintWriter ( " a . out " ) ; int tests = Integer . parseInt ( br . readLine ( ) . trim ( ) ) ; for ( int test = 1 ; test <= tests ; test ++ ) { int n = Integer . parseInt ( br . readLine ( ) . trim ( ) ) ; String [ ] s = new String [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { s [ i ] = br . readLine ( ) . trim ( ) ; } int result = 0 ; for ( int row = 0 ; row < n ; row ++ ) { for ( int i = row ; i < n ; i ++ ) { boolean can = true ; for ( int k = row + 1 ; k < n ; k ++ ) { if ( s [ i ] . charAt ( k ) == '1' ) { can = false ; break ; } } if ( can ) { for ( int k = i - 1 ; k >= row ; k -- ) { String str = s [ k + 1 ] ; s [ k + 1 ] = s [ k ] ; s [ k ] = str ; result ++ ; } break ; } } } pw . println ( " Case ▁ # " + test + " : ▁ " + result ) ; } br . close ( ) ; pw . close ( ) ; } }

import java . io . BufferedReader ; import java . io . FileOutputStream ; import java . io . FileReader ; import java . io . IOException ; import java . io . PrintStream ; public class A { public static void main ( String [ ] args ) throws IOException { BufferedReader br = new BufferedReader ( new FileReader ( " A - large . in " ) ) ; PrintStream ps = new PrintStream ( new FileOutputStream ( " A - large . out " ) ) ; int cases = Integer . parseInt ( br . readLine ( ) ) ; for ( int itr = 1 ; itr <= cases ; itr ++ ) { int size = Integer . parseInt ( br . readLine ( ) ) ; int [ ] board = new int [ size ] ; for ( int i = 0 ; i < size ; i ++ ) { String s = br . readLine ( ) ; for ( int j = 0 ; j < size ; j ++ ) if ( s . charAt ( j ) == '1' ) board [ i ] = j ; } int out = 0 ; boolean done = false ; while ( ! done ) { done = true ; for ( int i = 0 ; i < size ; i ++ ) { if ( board [ i ] > i ) { done = false ; int s = 0 ; for ( int j = i + 1 ; j < size ; j ++ ) if ( board [ j ] <= i ) { s = j ; break ; } for ( int j = s ; j > i ; j -- ) { int t = board [ j ] ; board [ j ] = board [ j - 1 ] ; board [ j - 1 ] = t ; out ++ ; } } } } ps . println ( " Case ▁ # " + itr + " : ▁ " + out ) ; } br . close ( ) ; ps . close ( ) ; } }

import java . io . * ; import java . util . * ; public class A implements Runnable { private static String fileName = A . class . getSimpleName ( ) . replaceFirst ( " _ . * " , " " ) ; private static String inputFileName = fileName + " . in " ; private static String outputFileName = fileName + " . out " ; private static Scanner in ; private static PrintWriter out ; private void solve ( ) { int n = in . nextInt ( ) ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { String s = in . next ( ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( s . charAt ( j ) == '1' ) { a [ i ] = j ; } } } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = i ; ; j ++ ) { if ( a [ j ] <= i ) break ; } for ( int k = j ; k > i ; k -- ) { ans ++ ; int t = a [ k ] ; a [ k ] = a [ k - 1 ] ; a [ k - 1 ] = t ; } } out . println ( ans ) ; } public void run ( ) { int tests = in . nextInt ( ) ; in . nextLine ( ) ; for ( int t = 1 ; t <= tests ; t ++ ) { out . print ( " Case ▁ # " + t + " : ▁ " ) ; solve ( ) ; } } public static void main ( String [ ] args ) throws IOException , InterruptedException { Locale . setDefault ( Locale . US ) ; if ( args . length >= 2 ) { inputFileName = args [ 0 ] ; outputFileName = args [ 1 ] ; } in = new Scanner ( new FileReader ( inputFileName ) ) ; out = new PrintWriter ( outputFileName ) ; Thread thread = new Thread ( new A ( ) ) ; thread . start ( ) ; thread . join ( ) ; in . close ( ) ; out . close ( ) ; } }

import java . util . Scanner ; import java . io . File ; import java . io . PrintWriter ; import java . io . FileNotFoundException ; public class A { public static void main ( String [ ] args ) throws FileNotFoundException { Scanner in = new Scanner ( new File ( " A . in " ) ) ; PrintWriter out = new PrintWriter ( " A . out " ) ; int t = in . nextInt ( ) ; in . nextLine ( ) ; for ( int tn = 0 ; tn < t ; tn ++ ) { out . println ( " Case ▁ # " + ( tn + 1 ) + " : ▁ " + solve ( in ) ) ; } out . close ( ) ; } private static String solve ( Scanner in ) { int n = in . nextInt ( ) ; in . nextLine ( ) ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { String s = in . nextLine ( ) ; int j = n - 1 ; while ( s . charAt ( j ) == '0' && j > 0 ) j -- ; a [ i ] = j ; } int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int j = i ; while ( a [ j ] > i ) j ++ ; int t = a [ j ] ; for ( int k = j ; k > i ; k -- ) a [ k ] = a [ k - 1 ] ; a [ i ] = t ; res += ( j - i ) ; } return " " + res ; } }

import sys NEW_LINE def max1 ( r ) : NEW_LINE INDENT z = [ i for i in range ( len ( r ) ) if r [ i ] == 1 ] NEW_LINE if ( z == [ ] ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return max ( z ) NEW_LINE DEDENT DEDENT def solve ( inp ) : NEW_LINE INDENT inp = [ max1 ( s ) for s in inp ] NEW_LINE ans = 0 NEW_LINE for i in range ( len ( inp ) ) : NEW_LINE INDENT z = min ( [ q for q in range ( len ( inp ) ) if q >= i and inp [ q ] <= i ] ) NEW_LINE r = list ( range ( i , z ) ) NEW_LINE r . reverse ( ) NEW_LINE for j in r : NEW_LINE INDENT inp [ j ] , inp [ j + 1 ] = inp [ j + 1 ] , inp [ j ] NEW_LINE ans += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT rdln = sys . stdin . readline NEW_LINE T = int ( rdln ( ) ) NEW_LINE for t in range ( T ) : NEW_LINE INDENT N = int ( rdln ( ) ) NEW_LINE print ( " Case ▁ # " , t + 1 , " : ▁ " , solve ( [ [ int ( c ) for c in rdln ( ) . strip ( ) ] for w in range ( N ) ] ) , sep = " " ) NEW_LINE DEDENT

def can ( line , idx ) : NEW_LINE INDENT return line [ idx + 1 : ] . find ( '1' ) == - 1 NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT T = int ( raw_input ( ) ) NEW_LINE for t in range ( 1 , T + 1 ) : NEW_LINE INDENT print ' Case ▁ # % d : ' % t , NEW_LINE N = int ( raw_input ( ) ) NEW_LINE A = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT A . append ( raw_input ( ) ) NEW_LINE DEDENT P = [ ] NEW_LINE U = [ 0 ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if not U [ j ] and can ( A [ i ] , j ) : NEW_LINE INDENT P . append ( j ) NEW_LINE U [ j ] = 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT result = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT result += P [ i ] > P [ j ] NEW_LINE DEDENT DEDENT print result NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT

from __future__ import division NEW_LINE import collections NEW_LINE import itertools NEW_LINE import sys NEW_LINE class gcj : NEW_LINE INDENT IN = sys . stdin NEW_LINE number = 0 NEW_LINE @ classmethod NEW_LINE def case ( cls ) : NEW_LINE INDENT cls . number += 1 NEW_LINE return ' Case ▁ # % d : ' % cls . number NEW_LINE DEDENT @ classmethod NEW_LINE def line ( cls , type = str ) : NEW_LINE INDENT line = cls . IN . readline ( ) NEW_LINE return type ( line . strip ( ' \n ' ) ) NEW_LINE DEDENT @ classmethod NEW_LINE def splitline ( cls , type = str ) : NEW_LINE INDENT line = cls . IN . readline ( ) NEW_LINE return [ type ( x ) for x in line . split ( ) ] NEW_LINE DEDENT DEDENT def go ( ) : NEW_LINE INDENT t = gcj . line ( int ) NEW_LINE for _ in xrange ( t ) : NEW_LINE INDENT n = gcj . line ( int ) NEW_LINE data = [ ] NEW_LINE for _ in xrange ( n ) : NEW_LINE INDENT data . append ( [ int ( x ) for x in gcj . line ( ) ] ) NEW_LINE assert len ( data [ - 1 ] ) == n NEW_LINE DEDENT print gcj . case ( ) , solve ( data ) NEW_LINE DEDENT DEDENT def solve ( data ) : NEW_LINE INDENT res = 0 NEW_LINE n = len ( data ) NEW_LINE gdzie = [ 0 ] * n NEW_LINE for i in xrange ( n ) : NEW_LINE INDENT row = data [ i ] NEW_LINE x = n - 1 NEW_LINE while x >= 0 and row [ x ] == 0 : NEW_LINE INDENT x -= 1 NEW_LINE DEDENT gdzie [ i ] = x NEW_LINE DEDENT for i in xrange ( n ) : NEW_LINE INDENT j = min ( j for j in xrange ( i , n ) if gdzie [ j ] <= i ) NEW_LINE for k in xrange ( j , i , - 1 ) : NEW_LINE INDENT gdzie [ k ] , gdzie [ k - 1 ] = gdzie [ k - 1 ] , gdzie [ k ] NEW_LINE res += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT go ( ) NEW_LINE

import sys NEW_LINE import psyco NEW_LINE psyco . full ( ) NEW_LINE def dbg ( a ) : sys . stderr . write ( str ( a ) ) NEW_LINE def readint ( ) : return int ( raw_input ( ) ) NEW_LINE def readfloat ( ) : return float ( raw_input ( ) ) NEW_LINE def readarray ( N , foo ) : return [ foo ( ) for i in xrange ( N ) ] NEW_LINE def readlinearray ( foo ) : return map ( foo , raw_input ( ) . split ( ) ) NEW_LINE def dig ( c ) : NEW_LINE INDENT c = ord ( c ) NEW_LINE if c >= 97 : NEW_LINE INDENT return c - 87 NEW_LINE DEDENT return c - 48 NEW_LINE DEDENT def run_test ( test ) : NEW_LINE INDENT n = readint ( ) NEW_LINE a = readarray ( n , raw_input ) NEW_LINE maxpos = [ 0 ] * n NEW_LINE used = [ False ] * n NEW_LINE for i in xrange ( n ) : NEW_LINE INDENT for j in xrange ( n ) : NEW_LINE INDENT if ( a [ i ] [ j ] == "1" ) : NEW_LINE INDENT maxpos [ i ] = j NEW_LINE DEDENT DEDENT DEDENT res = 0 NEW_LINE for i in xrange ( n ) : NEW_LINE INDENT for j in xrange ( i , n ) : NEW_LINE INDENT if ( maxpos [ j ] > i ) : continue NEW_LINE for k in xrange ( j , i , - 1 ) : NEW_LINE INDENT ( maxpos [ k ] , maxpos [ k - 1 ] ) = ( maxpos [ k - 1 ] , maxpos [ k ] ) NEW_LINE res += 1 NEW_LINE DEDENT break NEW_LINE DEDENT DEDENT print " Case ▁ # % d : ▁ % s " % ( test + 1 , str ( res ) ) NEW_LINE DEDENT for test in range ( readint ( ) ) : NEW_LINE INDENT dbg ( " test ▁ % d \n " % test ) NEW_LINE run_test ( test ) NEW_LINE DEDENT

def readInput ( ) : NEW_LINE INDENT file = open ( " A - large . in " ) NEW_LINE testCaseCount = int ( file . readline ( ) . rstrip ( ) ) NEW_LINE testCases = [ ] NEW_LINE for i in range ( 0 , testCaseCount ) : NEW_LINE INDENT lineCount = int ( file . readline ( ) . strip ( ) ) NEW_LINE lines = [ len ( file . readline ( ) . strip ( ) . rstrip ( '0' ) ) for j in range ( 0 , lineCount ) ] NEW_LINE testCases . append ( lines ) NEW_LINE DEDENT return testCases NEW_LINE DEDENT def numberOfSortSteps ( testCase ) : NEW_LINE INDENT steps = 0 NEW_LINE stop = False NEW_LINE for lineNr in range ( 0 , len ( testCase ) ) : NEW_LINE INDENT for targetLineNr in range ( lineNr , len ( testCase ) ) : NEW_LINE INDENT if testCase [ targetLineNr ] <= lineNr + 1 : NEW_LINE INDENT testCase . insert ( lineNr , testCase . pop ( targetLineNr ) ) NEW_LINE steps += targetLineNr - lineNr NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT return steps NEW_LINE DEDENT testCases = readInput ( ) NEW_LINE testCaseNr = 1 NEW_LINE for testCase in testCases : NEW_LINE INDENT print ' Case ▁ # % s : ▁ % s ' % ( testCaseNr , numberOfSortSteps ( testCase ) ) NEW_LINE testCaseNr += 1 NEW_LINE DEDENT


import java . io . * ; import java . util . * ; public class BotTrust { public static void main ( String [ ] args ) { try { BufferedReader br = new BufferedReader ( new FileReader ( " A - large . in " ) ) ; int T = Integer . parseInt ( br . readLine ( ) ) ; PrintWriter pw = new PrintWriter ( new FileWriter ( " output " ) ) ; for ( int I = 1 ; I <= T ; I ++ ) { StringTokenizer st = new StringTokenizer ( br . readLine ( ) , " ▁ \n \t " , false ) ; int n = Integer . parseInt ( st . nextToken ( ) ) ; int Opos = 1 , Bpos = 1 ; int Oextra = 0 , Bextra = 0 ; int ret = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int nextBot = ( st . nextToken ( ) . equals ( " O " ) ? 0 : 1 ) ; int button = Integer . parseInt ( st . nextToken ( ) ) ; if ( nextBot == 0 ) { int dist = Math . abs ( Opos - button ) ; if ( Oextra >= dist ) dist = 0 ; else dist -= Oextra ; ret += dist + 1 ; Bextra += dist + 1 ; Oextra = 0 ; Opos = button ; } else { int dist = Math . abs ( Bpos - button ) ; if ( Bextra >= dist ) dist = 0 ; else dist -= Bextra ; ret += dist + 1 ; Oextra += dist + 1 ; Bextra = 0 ; Bpos = button ; } } pw . println ( " Case ▁ # " + I + " : ▁ " + ret ) ; } br . close ( ) ; pw . flush ( ) ; pw . close ( ) ; } catch ( IOException ie ) { ie . printStackTrace ( ) ; } } }

import java . util . Scanner ; public class bottrust { public static void main ( String [ ] args ) { Scanner ins = new Scanner ( System . in ) ; int cc ; int ntc = ins . nextInt ( ) ; for ( cc = 1 ; cc <= ntc ; cc ++ ) { int pos [ ] = { 1 , 1 } , time [ ] = { 0 , 0 } ; int cnt = ins . nextInt ( ) ; for ( int i = 0 ; i < cnt ; i ++ ) { String s = ins . next ( ) ; int r = 0 ; if ( s . equals ( " B " ) ) r = 1 ; int p = ins . nextInt ( ) ; int mtime = p - pos [ r ] ; if ( mtime < 0 ) mtime = - mtime ; mtime += time [ r ] + 1 ; if ( mtime <= time [ 1 - r ] ) mtime = time [ 1 - r ] + 1 ; time [ r ] = mtime ; pos [ r ] = p ; } int maxtime = time [ 0 ] ; if ( time [ 1 ] > maxtime ) maxtime = time [ 1 ] ; System . out . printf ( " Case ▁ # % d : ▁ % d \n " , cc , maxtime ) ; } } }

import java . io . BufferedReader ; import java . io . FileReader ; import java . io . IOException ; import java . util . Iterator ; public class InputFileParser implements Iterable < String [ ] > { private final int linesPerCase ; private final int totalCases ; private BufferedReader reader ; private int casesExtracted = 0 ; public InputFileParser ( int linesPerCase , String fileName ) { this . linesPerCase = linesPerCase ; try { FileReader fileReader = new FileReader ( fileName ) ; this . reader = new BufferedReader ( fileReader ) ; String line = this . reader . readLine ( ) ; this . totalCases = Integer . parseInt ( line ) ; } catch ( IOException e ) { throw new IllegalStateException ( e ) ; } } private String readLine ( ) { try { return this . reader . readLine ( ) ; } catch ( IOException e ) { throw new IllegalStateException ( e ) ; } } @ Override public Iterator < String [ ] > iterator ( ) { return new Iterator < String [ ] > ( ) { @ Override public boolean hasNext ( ) { return casesExtracted < totalCases ; } @ Override public String [ ] next ( ) { String [ ] caseLines = new String [ linesPerCase ] ; for ( int i = 0 ; i < linesPerCase ; i ++ ) { caseLines [ i ] = readLine ( ) ; } casesExtracted ++ ; return caseLines ; } @ Override public void remove ( ) { } } ; } }

import java . io . * ; import java . util . * ; import java . util . regex . Pattern ; public class GCJ2011QualA { void solve ( ) { Scanner sc = new Scanner ( System . in ) ; int T = sc . nextInt ( ) ; for ( int testCase = 1 ; testCase <= T ; testCase ++ ) { int N = sc . nextInt ( ) ; int oT = 0 , bT = 0 ; int oP = 1 , bP = 1 ; for ( int i = 0 ; i < N ; i ++ ) { char [ ] c = sc . next ( ) . toCharArray ( ) ; int pos = sc . nextInt ( ) ; if ( c [ 0 ] == ' O ' ) { oT = Math . max ( oT + Math . abs ( pos - oP ) , bT ) + 1 ; oP = pos ; } else { bT = Math . max ( bT + Math . abs ( pos - bP ) , oT ) + 1 ; bP = pos ; } } System . out . println ( " Case ▁ # " + testCase + " : ▁ " + Math . max ( bT , oT ) ) ; } } public static void main ( String args [ ] ) { new GCJ2011QualA ( ) . solve ( ) ; } }

import java . util . * ; import java . io . * ; public class Solution { public void doMain ( ) throws Exception { Scanner sc = new Scanner ( new FileReader ( " input . txt " ) ) ; PrintWriter pw = new PrintWriter ( new FileWriter ( " output . txt " ) ) ; int caseCnt = sc . nextInt ( ) ; for ( int caseNum = 0 ; caseNum < caseCnt ; caseNum ++ ) { pw . print ( " Case ▁ # " + ( caseNum + 1 ) + " : ▁ " ) ; int curO = 1 , spareStepsO = 0 , curB = 1 , spareStepsB = 0 ; int N = sc . nextInt ( ) ; int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { String who = sc . next ( ) ; if ( who . equals ( " O " ) ) { int pos = sc . nextInt ( ) ; int dist = Math . abs ( curO - pos ) + 1 ; dist -= Math . min ( spareStepsO , dist - 1 ) ; res += dist ; spareStepsB += dist ; spareStepsO = 0 ; curO = pos ; } else { int pos = sc . nextInt ( ) ; int dist = Math . abs ( curB - pos ) + 1 ; dist -= Math . min ( spareStepsB , dist - 1 ) ; res += dist ; spareStepsO += dist ; spareStepsB = 0 ; curB = pos ; } } pw . println ( res ) ; } pw . flush ( ) ; pw . close ( ) ; sc . close ( ) ; } public static void main ( String [ ] args ) throws Exception { new Solution ( ) . doMain ( ) ; } }

import sys NEW_LINE import os NEW_LINE def myabs ( a ) : NEW_LINE INDENT if ( a < 0 ) : return - a NEW_LINE else : return a NEW_LINE DEDENT lines = open ( " a2 . in " , " r " ) . readlines ( ) NEW_LINE t = int ( lines [ 0 ] . strip ( ) ) NEW_LINE for ti in range ( 1 , t + 1 ) : NEW_LINE INDENT items = lines [ ti ] . strip ( ) . split ( " ▁ " ) NEW_LINE n = int ( items [ 0 ] ) NEW_LINE o = 1 NEW_LINE b = 1 NEW_LINE o_t = 0 NEW_LINE b_t = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT order_k = items [ i * 2 + 1 ] NEW_LINE order_i = int ( items [ i * 2 + 2 ] ) NEW_LINE if ( order_k == " O " ) : NEW_LINE INDENT o_t += myabs ( o - order_i ) NEW_LINE o = order_i NEW_LINE if ( o_t < b_t ) : o_t = b_t NEW_LINE o_t = o_t + 1 NEW_LINE DEDENT else : NEW_LINE INDENT b_t += myabs ( b - order_i ) NEW_LINE b = order_i NEW_LINE if ( b_t < o_t ) : b_t = o_t NEW_LINE b_t = b_t + 1 NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE if ( o_t > b_t ) : ans = o_t NEW_LINE else : ans = b_t NEW_LINE print " Case ▁ # % d : ▁ % d " % ( ti , ans ) NEW_LINE DEDENT

num_cases = int ( raw_input ( ) ) NEW_LINE for case_num in xrange ( 1 , num_cases + 1 ) : NEW_LINE INDENT line = raw_input ( ) . split ( ) NEW_LINE N = int ( line [ 0 ] ) NEW_LINE colors = [ ] NEW_LINE moves = { ' O ' : [ ] , ' B ' : [ ] } NEW_LINE positions = { ' O ' : 1 , ' B ' : 1 } NEW_LINE count = 0 NEW_LINE for i in xrange ( N ) : NEW_LINE INDENT r , p = line [ 2 * i + 1 ] , int ( line [ 2 * i + 2 ] ) NEW_LINE colors . append ( r ) NEW_LINE moves [ r ] . append ( p ) NEW_LINE DEDENT colors = colors [ : : - 1 ] NEW_LINE moves [ ' O ' ] = moves [ ' O ' ] [ : : - 1 ] NEW_LINE moves [ ' B ' ] = moves [ ' B ' ] [ : : - 1 ] NEW_LINE while colors : NEW_LINE INDENT color = colors . pop ( ) NEW_LINE other_color = ' O ' if color == ' B ' else ' B ' NEW_LINE current = positions [ color ] NEW_LINE other_current = positions [ other_color ] NEW_LINE target = moves [ color ] . pop ( ) NEW_LINE if moves [ other_color ] : NEW_LINE INDENT other_target = moves [ other_color ] [ - 1 ] NEW_LINE DEDENT time_to_push = abs ( target - current ) + 1 NEW_LINE count += time_to_push NEW_LINE positions [ color ] = target NEW_LINE delta = other_target - other_current NEW_LINE if abs ( delta ) <= time_to_push : NEW_LINE INDENT positions [ other_color ] = other_target NEW_LINE DEDENT else : NEW_LINE INDENT sign = - 1 if delta < 0 else 1 NEW_LINE positions [ other_color ] += sign * time_to_push NEW_LINE DEDENT DEDENT print " Case ▁ # % d : ▁ % d " % ( case_num , count ) NEW_LINE DEDENT

import sys NEW_LINE data = [ l . strip ( ) for l in open ( " infile " , " r " ) . readlines ( ) ] NEW_LINE out = open ( " outfile " , " w " ) NEW_LINE class robot : NEW_LINE INDENT def __init__ ( self , col , casedata ) : NEW_LINE INDENT self . color = col NEW_LINE self . pos = 1 NEW_LINE self . cindex = - 2 NEW_LINE self . nextgoal = ' START ' NEW_LINE self . findnextgoal ( casedata ) NEW_LINE DEDENT def movetowardgoal ( self ) : NEW_LINE INDENT if self . nextgoal == ' DONE ' : NEW_LINE INDENT pass NEW_LINE DEDENT elif self . pos < self . nextgoal : NEW_LINE INDENT self . pos += 1 NEW_LINE DEDENT elif self . pos > self . nextgoal : NEW_LINE INDENT self . pos -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT pass NEW_LINE DEDENT DEDENT def findnextgoal ( self , casedata ) : NEW_LINE INDENT self . cindex += 2 NEW_LINE while self . cindex < len ( casedata ) and casedata [ self . cindex ] != self . color : NEW_LINE INDENT self . cindex += 2 NEW_LINE DEDENT try : NEW_LINE INDENT self . nextgoal = int ( casedata [ 1 + self . cindex ] ) NEW_LINE DEDENT except : NEW_LINE INDENT self . nextgoal = ' DONE ' NEW_LINE DEDENT DEDENT DEDENT ncases = int ( data . pop ( 0 ) ) NEW_LINE for case in range ( ncases ) : NEW_LINE INDENT casedata = data . pop ( 0 ) . split ( ' ▁ ' ) NEW_LINE numreqs = casedata . pop ( 0 ) NEW_LINE timetaken = 0 NEW_LINE orange = robot ( ' O ' , casedata ) NEW_LINE blue = robot ( ' B ' , casedata ) NEW_LINE while ( orange . nextgoal != ' DONE ' or blue . nextgoal != ' DONE ' ) : NEW_LINE INDENT timetaken += 1 NEW_LINE if orange . cindex < blue . cindex : NEW_LINE INDENT if orange . nextgoal == orange . pos : NEW_LINE INDENT orange . findnextgoal ( casedata ) NEW_LINE DEDENT else : NEW_LINE INDENT orange . movetowardgoal ( ) NEW_LINE DEDENT blue . movetowardgoal ( ) NEW_LINE DEDENT elif blue . cindex < orange . cindex : NEW_LINE INDENT if blue . nextgoal == blue . pos : NEW_LINE INDENT blue . findnextgoal ( casedata ) NEW_LINE DEDENT else : NEW_LINE INDENT blue . movetowardgoal ( ) NEW_LINE DEDENT orange . movetowardgoal ( ) NEW_LINE DEDENT else : NEW_LINE INDENT sys . exit ( ) NEW_LINE DEDENT DEDENT out . write ( " Case ▁ # " + str ( case + 1 ) + " : ▁ " + str ( timetaken ) + " \n " ) NEW_LINE DEDENT

other = { " B " : " O " , " O " : " B " } NEW_LINE def main ( ) : NEW_LINE INDENT f = open ( " input . txt " ) NEW_LINE testlen = int ( f . readline ( ) . strip ( ) ) NEW_LINE tests = [ ] NEW_LINE for line in f : NEW_LINE INDENT tests . append ( line . strip ( ) ) NEW_LINE DEDENT tests = tests [ : testlen ] NEW_LINE for test , i in zip ( tests , range ( testlen ) ) : NEW_LINE INDENT print " Case ▁ # % d : ▁ % d " % ( i + 1 , solve ( parse ( test ) ) ) NEW_LINE DEDENT DEDENT def parse ( test ) : NEW_LINE INDENT test = test . split ( " ▁ " ) NEW_LINE return zip ( test [ 1 : : 2 ] , map ( int , test [ 2 : : 2 ] ) ) NEW_LINE DEDENT def solve ( test ) : NEW_LINE INDENT time = { " B " : 0 , " O " : 0 } NEW_LINE pos = { " B " : 0 , " O " : 0 } NEW_LINE total = 0 NEW_LINE for nc , np in test : NEW_LINE INDENT d = max ( abs ( pos [ nc ] - np ) - time [ nc ] , 0 ) + 1 NEW_LINE time [ nc ] = 0 NEW_LINE total += d NEW_LINE time [ other [ nc ] ] += d NEW_LINE pos [ nc ] = np NEW_LINE DEDENT return total - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT main ( ) NEW_LINE DEDENT

def run_command ( commands ) : NEW_LINE INDENT bPos = oPos = 1 NEW_LINE bTime = oTime = 0 NEW_LINE time = 0 NEW_LINE for c in commands : NEW_LINE INDENT if c [ 0 ] == ' O ' : NEW_LINE INDENT minTime = abs ( oPos - c [ 1 ] ) - time + oTime NEW_LINE time += max ( minTime , 0 ) + 1 NEW_LINE oTime = time NEW_LINE oPos = c [ 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT minTime = abs ( bPos - c [ 1 ] ) - time + bTime NEW_LINE time += max ( minTime , 0 ) + 1 NEW_LINE bTime = time NEW_LINE bPos = c [ 1 ] NEW_LINE DEDENT DEDENT return time NEW_LINE DEDENT def read_input ( ) : NEW_LINE INDENT line = raw_input ( ) . split ( ) [ 1 : ] NEW_LINE commands = [ ( line [ 2 * i ] , int ( line [ 2 * i + 1 ] ) ) for i in range ( len ( line ) // 2 ) ] NEW_LINE return run_command ( commands ) NEW_LINE DEDENT numCases = input ( ) NEW_LINE for i in range ( 1 , numCases + 1 ) : NEW_LINE INDENT output = read_input ( ) NEW_LINE print " Case ▁ # % d : " % i , output NEW_LINE DEDENT


import java . util . * ; import java . io . * ; public class C { public static void main ( String [ ] args ) throws Exception { Scanner in = new Scanner ( new File ( " C - large . in " ) ) ; PrintWriter out = new PrintWriter ( new FileWriter ( new File ( " C - large . out " ) ) ) ; int t = in . nextInt ( ) ; for ( int x = 0 ; x < t ; x ++ ) { int n = in . nextInt ( ) ; int [ ] xs = new int [ n ] ; int [ ] ys = new int [ n ] ; for ( int y = 0 ; y < n ; y ++ ) { xs [ y ] = in . nextInt ( ) ; ys [ y ] = in . nextInt ( ) ; } out . println ( " Case ▁ # " + ( x + 1 ) + " : " ) ; for ( int z = 0 ; z < xs . length ; z ++ ) { ArrayList < Double > angles = new ArrayList < Double > ( ) ; for ( int a = 0 ; a < xs . length ; a ++ ) { if ( a != z ) { double current = Math . atan2 ( ys [ a ] - ys [ z ] , xs [ a ] - xs [ z ] ) ; angles . add ( current ) ; angles . add ( current + 2.0 * Math . PI ) ; } } Collections . sort ( angles ) ; int index = 0 ; int result = n ; for ( int b = 0 ; b < n ; b ++ ) { while ( index < angles . size ( ) && angles . get ( index ) < angles . get ( b ) + Math . PI + 1E-12 ) { index ++ ; } result = Math . min ( result , n - ( index - b ) - 1 ) ; } if ( n == 1 ) { out . println ( 0 ) ; } else { out . println ( result ) ; } } } out . close ( ) ; } }

import heapq NEW_LINE def ReadIn ( ) : NEW_LINE INDENT t = int ( input ( ) ) NEW_LINE for c in range ( 1 , t + 1 ) : NEW_LINE INDENT m , n = [ int ( x ) for x in input ( ) . split ( ) ] NEW_LINE a = [ int ( x ) for x in input ( ) . split ( ) ] NEW_LINE yield c , n , a NEW_LINE DEDENT DEDENT def OK ( n , a , guess ) : NEW_LINE INDENT return sum ( [ guess // x + 1 for x in a ] ) >= n NEW_LINE DEDENT def Solve ( n , a ) : NEW_LINE INDENT if n <= len ( a ) : return n NEW_LINE lower = 0 NEW_LINE upper = max ( a ) * n NEW_LINE while lower < upper : NEW_LINE INDENT guess = ( lower + upper + 1 ) // 2 NEW_LINE if OK ( n , a , guess ) : upper = guess - 1 NEW_LINE else : lower = guess NEW_LINE DEDENT guess = lower NEW_LINE heap = [ ] NEW_LINE remain = n NEW_LINE for i , x in enumerate ( a ) : NEW_LINE INDENT num = guess // x + 1 NEW_LINE remain -= num NEW_LINE heapq . heappush ( heap , ( num * x - guess , i ) ) NEW_LINE DEDENT ret = heap [ 0 ] [ 1 ] NEW_LINE for r in range ( remain ) : NEW_LINE INDENT t , i = heapq . heappop ( heap ) NEW_LINE heapq . heappush ( heap , ( t + a [ i ] , i ) ) NEW_LINE ret = i NEW_LINE DEDENT return ret + 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT for c , n , a in ReadIn ( ) : NEW_LINE INDENT print ( ' Case ▁ # % d : ▁ % d ' % ( c , Solve ( n , a ) ) ) NEW_LINE DEDENT DEDENT

import math NEW_LINE cases = int ( raw_input ( ) ) NEW_LINE for c in xrange ( cases ) : NEW_LINE INDENT tree_count = int ( raw_input ( ) ) NEW_LINE dims = [ ] NEW_LINE for t in xrange ( tree_count ) : NEW_LINE INDENT dims . append ( map ( int , raw_input ( ) . split ( ) ) ) NEW_LINE DEDENT print " Case ▁ # { } : " . format ( c + 1 ) NEW_LINE for i , p in enumerate ( dims ) : NEW_LINE INDENT rest = [ d for d in dims if d [ 0 ] != p [ 0 ] or d [ 1 ] != p [ 1 ] ] NEW_LINE angles = [ math . atan2 ( r [ 1 ] - p [ 1 ] , r [ 0 ] - p [ 0 ] ) for r in rest ] NEW_LINE angles = angles + [ a + 2 * math . pi for a in angles ] + [ a + 4 * math . pi for a in angles ] NEW_LINE angles . sort ( ) NEW_LINE end = 0 NEW_LINE min_remove = 999999999 NEW_LINE for start in xrange ( len ( angles ) / 2 ) : NEW_LINE INDENT while angles [ end ] < ( angles [ start ] + math . pi - 1e-12 ) : NEW_LINE INDENT end += 1 NEW_LINE DEDENT min_remove = min ( min_remove , end - start - 1 ) NEW_LINE DEDENT if min_remove == 999999999 : NEW_LINE INDENT min_remove = 0 NEW_LINE DEDENT print min_remove NEW_LINE DEDENT DEDENT

from __future__ import division NEW_LINE import bisect NEW_LINE T = int ( raw_input ( ) ) NEW_LINE for test in xrange ( T ) : NEW_LINE INDENT N = int ( raw_input ( ) ) NEW_LINE trees = [ ] NEW_LINE for i in xrange ( N ) : NEW_LINE INDENT x , y = map ( int , raw_input ( ) . split ( ) ) NEW_LINE trees . append ( ( x , y ) ) NEW_LINE DEDENT print " Case ▁ # { } : " . format ( test + 1 ) NEW_LINE for x0 , y0 in trees : NEW_LINE INDENT left = [ ] NEW_LINE right = [ ] NEW_LINE up = 0 NEW_LINE down = 0 NEW_LINE for x1 , y1 in trees : NEW_LINE INDENT x = x1 - x0 NEW_LINE y = y1 - y0 NEW_LINE if x > 0 : NEW_LINE INDENT right . append ( y / x ) NEW_LINE DEDENT elif x < 0 : NEW_LINE INDENT left . append ( y / x ) NEW_LINE DEDENT elif y > 0 : NEW_LINE INDENT up += 1 NEW_LINE DEDENT elif y < 0 : NEW_LINE INDENT down += 1 NEW_LINE DEDENT DEDENT left . sort ( ) NEW_LINE right . sort ( ) NEW_LINE ans = min ( len ( left ) , len ( right ) ) NEW_LINE for i , angle in enumerate ( left ) : NEW_LINE INDENT cut_after = down + len ( left ) - 1 - i + bisect . bisect_left ( right , angle ) NEW_LINE cut_before = up + i + len ( right ) - bisect . bisect_right ( right , angle ) NEW_LINE cut = min ( cut_after , cut_before ) NEW_LINE ans = min ( ans , cut ) NEW_LINE DEDENT print ans NEW_LINE DEDENT DEDENT

from math import atan , pi NEW_LINE if __name__ == " _ _ main _ _ " : NEW_LINE INDENT testcases = input ( ) NEW_LINE for case in xrange ( 1 , testcases + 1 ) : NEW_LINE INDENT num_trees = int ( raw_input ( ) ) NEW_LINE trees = [ [ int ( n ) for n in raw_input ( ) . split ( ) ] for _ in range ( num_trees ) ] NEW_LINE print ( " Case ▁ # % i : " % ( case ) ) NEW_LINE for i in range ( num_trees ) : NEW_LINE INDENT r_angles , l_angles = [ ] , [ ] NEW_LINE for j in range ( num_trees ) : NEW_LINE INDENT if j == i : NEW_LINE INDENT continue NEW_LINE DEDENT d_x , d_y = trees [ j ] [ 0 ] - trees [ i ] [ 0 ] , trees [ j ] [ 1 ] - trees [ i ] [ 1 ] NEW_LINE if d_x > 0 : NEW_LINE INDENT r_angles . append ( atan ( float ( d_y ) / float ( d_x ) ) ) NEW_LINE DEDENT elif d_x < 0 : NEW_LINE INDENT l_angles . append ( atan ( float ( d_y ) / float ( d_x ) ) ) NEW_LINE DEDENT elif d_y < 0 : NEW_LINE INDENT l_angles . append ( pi ) NEW_LINE DEDENT else : NEW_LINE INDENT r_angles . append ( pi ) NEW_LINE DEDENT DEDENT r_angles = sorted ( r_angles ) NEW_LINE l_angles = sorted ( l_angles ) NEW_LINE n_r , n_l = len ( r_angles ) , len ( l_angles ) NEW_LINE min_r , min_l = n_r , n_l NEW_LINE while len ( r_angles ) > 0 or len ( l_angles ) > 0 : NEW_LINE INDENT if len ( r_angles ) > 0 and len ( l_angles ) > 0 : NEW_LINE INDENT next_val = min ( r_angles [ 0 ] , l_angles [ 0 ] ) NEW_LINE DEDENT elif len ( r_angles ) > 0 : NEW_LINE INDENT next_val = r_angles [ 0 ] NEW_LINE DEDENT else : NEW_LINE INDENT next_val = l_angles [ 0 ] NEW_LINE DEDENT from_r , from_l = 0 , 0 NEW_LINE while r_angles and r_angles [ 0 ] == next_val : NEW_LINE INDENT r_angles . pop ( 0 ) NEW_LINE from_r += 1 NEW_LINE DEDENT while l_angles and l_angles [ 0 ] == next_val : NEW_LINE INDENT l_angles . pop ( 0 ) NEW_LINE from_l += 1 NEW_LINE DEDENT n_r -= from_r NEW_LINE n_l -= from_l NEW_LINE min_r = min ( min_r , n_r ) NEW_LINE min_l = min ( min_l , n_l ) NEW_LINE n_r += from_l NEW_LINE n_l += from_r NEW_LINE DEDENT print min ( min_r , min_l ) NEW_LINE DEDENT DEDENT DEDENT

import imp , sys NEW_LINE sys . modules [ " utils " ] = __mod = imp . new_module ( " utils " ) NEW_LINE exec """ # ! / usr / bin / python STRNEWLINE STRNEWLINE from ▁ itertools ▁ import ▁ chain , ▁ repeat , ▁ izip STRNEWLINE STRNEWLINE def ▁ line ( * args ) : STRNEWLINE TABSYMBOL L ▁ = ▁ raw _ input ( ) . strip ( ) . split ( ) STRNEWLINE TABSYMBOL L ▁ = ▁ izip ( ▁ L , ▁ chain ( ▁ args , ▁ repeat ( str ) ▁ ) ▁ ) STRNEWLINE TABSYMBOL return ▁ [ ▁ type ( data ) ▁ for ▁ data , ▁ type ▁ in ▁ L ▁ ] TABSYMBOL STRNEWLINE TABSYMBOL STRNEWLINE def ▁ iline ( ) : ▁ return ▁ map ( ▁ int , ▁ raw _ input ( ) . strip ( ) . split ( ) ▁ ) STRNEWLINE def ▁ fline ( ) : ▁ return ▁ map ( ▁ float , ▁ raw _ input ( ) . strip ( ) . split ( ) ▁ ) """ in vars ( __mod ) NEW_LINE from utils import iline NEW_LINE from math import atan2 , pi NEW_LINE import sys NEW_LINE def test ( ) : NEW_LINE INDENT N , = iline ( ) NEW_LINE P = [ iline ( ) for i in xrange ( N ) ] NEW_LINE def solve ( ) : NEW_LINE INDENT print NEW_LINE for px , py in P : NEW_LINE INDENT Q = [ ( atan2 ( qy - py , qx - px ) , ( qx - px , qy - py ) ) for qx , qy in P if px != qx or py != qy ] NEW_LINE N = len ( Q ) NEW_LINE Q . sort ( ) NEW_LINE Q . extend ( [ ( q + 2 * pi , pos ) for q , pos in Q ] ) NEW_LINE a = 0 NEW_LINE ans = N NEW_LINE for b in xrange ( N , len ( Q ) ) : NEW_LINE INDENT a = max ( a , b - N ) NEW_LINE while a < b and Q [ b ] [ 1 ] [ 0 ] * Q [ a ] [ 1 ] [ 1 ] - Q [ b ] [ 1 ] [ 1 ] * Q [ a ] [ 1 ] [ 0 ] >= 0 : NEW_LINE INDENT a += 1 NEW_LINE DEDENT ans = min ( ans , b - a ) NEW_LINE DEDENT print ans NEW_LINE DEDENT DEDENT return solve NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT T = input ( ) NEW_LINE for i in xrange ( 1 , T + 1 ) : NEW_LINE INDENT print ' Case ▁ # % d : ' % i , NEW_LINE test ( ) ( ) NEW_LINE DEDENT DEDENT


import java . math . BigDecimal ; import java . util . Arrays ; import java . util . Scanner ; public class ProblemA { public static void main ( String [ ] args ) throws Exception { try ( Scanner sc = new Scanner ( System . in ) ) { int cases = sc . nextInt ( ) ; for ( int caseNum = 1 ; caseNum <= cases ; caseNum ++ ) { solve ( sc , caseNum ) ; } } } static void solve ( Scanner sc , int caseNum ) { int N = sc . nextInt ( ) ; int K = sc . nextInt ( ) ; P [ ] ps = new P [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { ps [ i ] = new P ( sc . nextInt ( ) , sc . nextInt ( ) ) ; } Arrays . sort ( ps ) ; double max = - 1 ; for ( int i = 0 ; i < N - K + 1 ; i ++ ) { P [ ] ps2 = Arrays . copyOfRange ( ps , i + 1 , N ) ; Arrays . sort ( ps2 , ( p1 , p2 ) -> ( Long . compare ( p2 . H * p2 . R , p1 . H * p1 . R ) ) ) ; double area = Math . PI * ps [ i ] . R * ps [ i ] . R ; area += Math . PI * ps [ i ] . H * ps [ i ] . R * 2 ; for ( int j = 0 ; j < K - 1 ; j ++ ) { area += Math . PI * ps2 [ j ] . H * ps2 [ j ] . R * 2 ; } max = Math . max ( area , max ) ; } System . out . printf ( " Case ▁ # % d : ▁ % .8f \n " , caseNum , max ) ; } static class P implements Comparable < P > { long R , H ; public P ( long r , long h ) { super ( ) ; R = r ; H = h ; } @ Override public int compareTo ( P arg0 ) { return Long . compare ( arg0 . R , R ) ; } } }

package round1c ; import java . util . ArrayList ; import java . util . List ; public class ProbA extends Prob { void setup ( ) { bin = true ; bout = true ; in = " A - large . in " ; out = " aout - large . txt " ; } @ Override public void main ( ) { setup ( ) ; reDirect ( ) ; int T = scanner . nextInt ( ) ; for ( int cas = 1 ; cas <= T ; cas ++ ) { double ans = run ( ) ; System . out . println ( String . format ( " Case ▁ # % d : ▁ % .9f " , cas , ans ) ) ; } } double run ( ) { int n = scanner . nextInt ( ) ; int k = scanner . nextInt ( ) ; int [ ] [ ] pancake = new int [ n ] [ 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { pancake [ i ] [ 0 ] = scanner . nextInt ( ) ; pancake [ i ] [ 1 ] = scanner . nextInt ( ) ; } double ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { double curr = calcSide ( pancake [ i ] ) + calcArea ( pancake [ i ] [ 0 ] ) ; List < Double > arr = new ArrayList < > ( ) ; for ( int j = 0 ; j < n ; j ++ ) if ( j != i ) { if ( pancake [ j ] [ 0 ] <= pancake [ i ] [ 0 ] ) { arr . add ( calcSide ( pancake [ j ] ) ) ; } } if ( arr . size ( ) < k - 1 ) { continue ; } arr . sort ( ( a , b ) -> - Double . compare ( a , b ) ) ; for ( int j = 0 ; j < k - 1 ; j ++ ) { curr += arr . get ( j ) ; } if ( curr > ans ) { ans = curr ; } } return ans ; } private double calcSide ( int [ ] pancake ) { double r = ( double ) pancake [ 0 ] ; double h = ( double ) pancake [ 1 ] ; return 2.0 * r * h * Math . PI ; } private double calcArea ( int r ) { return Math . PI * ( double ) r * ( double ) r ; } }

import java . io . File ; import java . io . PrintWriter ; import java . util . ArrayList ; import java . util . Arrays ; import java . util . Scanner ; public class A { static Pancake [ ] p ; static Double [ ] [ ] memo ; public static void main ( String [ ] args ) throws Exception { Scanner sc = new Scanner ( new File ( " A . in " ) ) ; PrintWriter out = new PrintWriter ( new File ( " A . out " ) ) ; int T = sc . nextInt ( ) ; for ( int t = 1 ; t <= T ; t ++ ) { int N = sc . nextInt ( ) ; int K = sc . nextInt ( ) ; memo = new Double [ N + 1 ] [ K + 1 ] ; p = new Pancake [ N ] ; for ( int a = 0 ; a < N ; a ++ ) { p [ a ] = new Pancake ( sc . nextInt ( ) , sc . nextInt ( ) ) ; } Arrays . sort ( p ) ; double ans = DP ( N - 1 , K ) ; System . out . printf ( " Case ▁ # % d : ▁ % .12f % n " , t , ans ) ; out . printf ( " Case ▁ # % d : ▁ % .12f % n " , t , ans ) ; } out . close ( ) ; } private static double DP ( int i , int k ) { if ( i < 0 || k <= 0 ) return 0 ; if ( memo [ i ] [ k ] != null ) return memo [ i ] [ k ] ; double ans = DP ( i - 1 , k - 1 ) ; if ( k == 1 ) { ans += Math . PI * p [ i ] . R * p [ i ] . R ; } ans += 2.0 * Math . PI * p [ i ] . R * p [ i ] . H ; ans = Math . max ( ans , DP ( i - 1 , k ) ) ; return memo [ i ] [ k ] = ans ; } static class Pancake implements Comparable < Pancake > { int R , H ; Pancake ( int a , int b ) { R = a ; H = b ; } @ Override public int compareTo ( Pancake that ) { return ( that . R - this . R ) ; } } }

import math NEW_LINE class pannukakku : NEW_LINE INDENT radius = 0 NEW_LINE height = 0 NEW_LINE size_h = 0 NEW_LINE size_r = 0 NEW_LINE def size ( self , current_r ) : NEW_LINE INDENT if ( self . radius > current_r ) : NEW_LINE INDENT return self . size_h + self . size_r - math . pi * current_r * current_r ; NEW_LINE DEDENT return self . size_h ; NEW_LINE DEDENT DEDENT def poista_isoin ( kakut , current_r ) : NEW_LINE INDENT isoin_index = 0 ; NEW_LINE isoin_size = kakut [ 0 ] . size ( current_r ) ; NEW_LINE for p in range ( 1 , len ( kakut ) ) : NEW_LINE INDENT if ( kakut [ p ] . size ( current_r ) > isoin_size ) : NEW_LINE INDENT isoin_index = p ; NEW_LINE isoin_size = kakut [ p ] . size ( current_r ) ; NEW_LINE DEDENT DEDENT return kakut . pop ( isoin_index ) ; NEW_LINE DEDENT t = int ( input ( ) ) NEW_LINE for i in range ( 1 , t + 1 ) : NEW_LINE INDENT pannukakkuja , tilauksen_koko = [ int ( s ) for s in input ( ) . split ( " ▁ " ) ] ; NEW_LINE radiuses = [ 0 for h in range ( pannukakkuja ) ] NEW_LINE heigths = [ 0 for h in range ( pannukakkuja ) ] NEW_LINE sizes = [ 0 for h in range ( pannukakkuja ) ] NEW_LINE kakut = [ pannukakku ( ) for h in range ( pannukakkuja ) ] NEW_LINE for h in range ( 0 , pannukakkuja ) : NEW_LINE INDENT r , s = [ int ( s ) for s in input ( ) . split ( " ▁ " ) ] ; NEW_LINE kakut [ h ] . radius = r ; NEW_LINE kakut [ h ] . heigth = s ; NEW_LINE kakut [ h ] . size_h = math . pi * 2 * r * s ; NEW_LINE kakut [ h ] . size_r = math . pi * r * r ; NEW_LINE DEDENT total = 0 ; NEW_LINE current_r = 0 ; NEW_LINE for p in range ( tilauksen_koko ) : NEW_LINE INDENT isoin = poista_isoin ( kakut , current_r ) ; NEW_LINE total += isoin . size ( current_r ) ; NEW_LINE current_r = max ( current_r , isoin . radius ) ; NEW_LINE DEDENT print ( " Case ▁ # { 0 : . 0f } : ▁ { 1 : . 6f } " . format ( i , total ) ) ; NEW_LINE DEDENT

import argparse NEW_LINE import math NEW_LINE def max_sides ( pancakes , k ) : NEW_LINE INDENT p2 = pancakes NEW_LINE base = p2 [ 0 ] NEW_LINE p2 = p2 [ 1 : ] NEW_LINE sorted_p2 = sorted ( p2 , key = lambda p : p [ 1 ] , reverse = True ) NEW_LINE ps = sorted_p2 [ : k - 1 ] + [ base ] NEW_LINE sum = 0 NEW_LINE for p in ps : NEW_LINE INDENT sum += p [ 1 ] NEW_LINE DEDENT return sum NEW_LINE DEDENT def main ( f_in , f_out ) : NEW_LINE INDENT num_cases = int ( f_in . readline ( ) . strip ( ) ) NEW_LINE for case in range ( 1 , num_cases + 1 ) : NEW_LINE INDENT n , k = f_in . readline ( ) . strip ( ) . split ( ) NEW_LINE n = int ( n ) NEW_LINE k = int ( k ) NEW_LINE pancakes = [ ] NEW_LINE for pancake in range ( n ) : NEW_LINE INDENT r , h = f_in . readline ( ) . strip ( ) . split ( ) NEW_LINE r = int ( r ) NEW_LINE h = int ( h ) NEW_LINE pancakes += [ ( r , 2 * math . pi * r * h ) ] NEW_LINE DEDENT sorted_pancakes = sorted ( pancakes , key = lambda p : p [ 0 ] , reverse = True ) NEW_LINE best_total = 0 NEW_LINE for start_pancake in range ( 0 , n - k + 1 ) : NEW_LINE INDENT s = max_sides ( sorted_pancakes [ start_pancake : ] , k ) NEW_LINE r = sorted_pancakes [ start_pancake ] [ 0 ] NEW_LINE top = math . pi * r * r NEW_LINE best_total = max ( best_total , s + top ) NEW_LINE DEDENT f_out . write ( ' Case ▁ # { } : ▁ { : . 8f } \n ' . format ( case , best_total ) ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT parser = argparse . ArgumentParser ( ) NEW_LINE parser . add_argument ( ' infile ' ) NEW_LINE opts = parser . parse_args ( ) NEW_LINE infile = opts . infile NEW_LINE outfile = infile . split ( ' . ' ) [ 0 ] + ' . out ' NEW_LINE print " Solving ! ▁ in : ▁ { } ▁ - > ▁ out : ▁ { } " . format ( infile , outfile ) NEW_LINE with open ( infile , ' r ' ) as f_in : NEW_LINE INDENT with open ( outfile , ' w ' ) as f_out : NEW_LINE INDENT main ( f_in , f_out ) NEW_LINE DEDENT DEDENT DEDENT

import functools NEW_LINE import math NEW_LINE import sys NEW_LINE sys . setrecursionlimit ( 5000 ) NEW_LINE def f ( pancakes , k ) : NEW_LINE INDENT @ functools . lru_cache ( maxsize = None ) NEW_LINE def recurse ( index , left ) : NEW_LINE INDENT if left == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT if index == len ( pancakes ) : NEW_LINE INDENT return - ( 10 ** 50 ) NEW_LINE DEDENT case1 = recurse ( index + 1 , left ) NEW_LINE case2 = 2 * pancakes [ index ] [ 0 ] * pancakes [ index ] [ 1 ] + recurse ( index + 1 , left - 1 ) NEW_LINE if left == k : NEW_LINE INDENT case2 += pancakes [ index ] [ 0 ] ** 2 NEW_LINE DEDENT return max ( ( case1 , case2 ) ) NEW_LINE DEDENT return math . pi * recurse ( 0 , k ) NEW_LINE DEDENT T = int ( input ( ) ) NEW_LINE for case in range ( 1 , T + 1 ) : NEW_LINE INDENT n , k = map ( int , input ( ) . split ( ) ) NEW_LINE pancakes = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT pancakes . append ( list ( map ( int , input ( ) . split ( ) ) ) ) NEW_LINE DEDENT pancakes = list ( reversed ( sorted ( pancakes ) ) ) NEW_LINE ans = f ( pancakes , k ) NEW_LINE print ( " Case ▁ # % s : ▁ % s " % ( case , ans ) ) NEW_LINE DEDENT

import math NEW_LINE fin = open ( "1 . in " , " r " ) NEW_LINE fout = open ( "1 . out " , " w " ) NEW_LINE T = int ( fin . readline ( ) ) NEW_LINE for t in range ( T ) : NEW_LINE INDENT print str ( t + 1 ) NEW_LINE N , K = map ( int , fin . readline ( ) . split ( ) ) NEW_LINE P = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT r , h = map ( int , fin . readline ( ) . split ( ) ) NEW_LINE P . append ( ( r , h , 2 * h * r ) ) NEW_LINE DEDENT P . sort ( ) NEW_LINE print P NEW_LINE ans = 0 NEW_LINE for j in range ( N - 1 , K - 2 , - 1 ) : NEW_LINE INDENT max_radius = P [ j ] [ 0 ] NEW_LINE area = P [ j ] [ 2 ] + max_radius * max_radius NEW_LINE del P [ j ] NEW_LINE filtered = [ hr for ( r , h , hr ) in P if r <= max_radius ] NEW_LINE filtered . sort ( ) NEW_LINE print filtered NEW_LINE for i in range ( 1 , K ) : NEW_LINE INDENT area += filtered [ - i ] NEW_LINE DEDENT ans = max ( ans , area ) NEW_LINE DEDENT ans *= math . pi NEW_LINE ans = ' { 0 : . 6f } ' . format ( ans ) NEW_LINE fout . write ( " Case ▁ # " + str ( t + 1 ) + " : ▁ " + ans + " \n " ) NEW_LINE DEDENT

import sys NEW_LINE import math NEW_LINE def problem ( pancakes , order_size ) : NEW_LINE INDENT pancakes . sort ( key = lambda x : 2 * math . pi * x [ 0 ] * x [ 1 ] , reverse = True ) NEW_LINE take = pancakes [ : order_size - 1 ] NEW_LINE if len ( take ) != 0 : NEW_LINE INDENT max_radius = max ( x [ 0 ] for x in take ) NEW_LINE DEDENT else : NEW_LINE INDENT max_radius = 0 NEW_LINE DEDENT outsides = sum ( 2 * math . pi * x [ 0 ] * x [ 1 ] for x in take ) NEW_LINE best = 0 NEW_LINE for n in pancakes [ order_size - 1 : ] : NEW_LINE INDENT radius = max ( max_radius , n [ 0 ] ) NEW_LINE size = outsides + 2 * math . pi * n [ 0 ] * n [ 1 ] + math . pi * radius * radius NEW_LINE best = max ( best , size ) NEW_LINE DEDENT return best NEW_LINE DEDENT def nextline ( input_file ) : NEW_LINE INDENT data = " " NEW_LINE while not data : NEW_LINE INDENT data = input_file . readline ( ) NEW_LINE DEDENT return data [ : - 1 ] NEW_LINE DEDENT def intsplit ( s ) : NEW_LINE INDENT return [ int ( x ) for x in s . split ( " ▁ " ) ] NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT result = " " NEW_LINE with sys . stdin if len ( sys . argv ) == 1 else open ( sys . argv [ 1 ] , ' r ' ) as infile : NEW_LINE INDENT number = int ( nextline ( infile ) ) NEW_LINE for run in range ( number ) : NEW_LINE INDENT case = nextline ( infile ) NEW_LINE size , order_size = intsplit ( case ) NEW_LINE pancakes = [ ] NEW_LINE for _ in range ( size ) : NEW_LINE INDENT pancakes . append ( intsplit ( nextline ( infile ) ) ) NEW_LINE DEDENT result += ' Case ▁ # { } : ▁ { } \n ' . format ( 1 + run , problem ( pancakes , order_size ) ) NEW_LINE DEDENT DEDENT if len ( sys . argv ) == 1 : NEW_LINE INDENT print ( result , end = ' ' ) NEW_LINE DEDENT else : NEW_LINE INDENT with open ( sys . argv [ 1 ] . replace ( ' in ' , ' sol ' ) , ' w ' ) as result_file : NEW_LINE INDENT result_file . write ( result ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT


import java . util . * ; import java . math . * ; public class PingPongEasy { static final Scanner sc = new Scanner ( System . in ) ; public static void main ( String [ ] args ) { int cases = sc . nextInt ( ) ; for ( int caseNum = 1 ; caseNum <= cases ; caseNum ++ ) { System . out . print ( " Case ▁ # " + caseNum + " : ▁ " ) ; int X = sc . nextInt ( ) , Y = sc . nextInt ( ) ; boolean [ ] [ ] vis = new boolean [ X ] [ Y ] ; int [ ] [ ] dis = new int [ 2 ] [ 2 ] ; for ( int i = 0 ; i < 2 ; i ++ ) { dis [ i ] [ 0 ] = sc . nextInt ( ) ; dis [ i ] [ 1 ] = sc . nextInt ( ) ; } int x = sc . nextInt ( ) ; int y = sc . nextInt ( ) ; System . out . println ( recurse ( x , y , vis , dis ) ) ; } } private static int recurse ( int x , int y , boolean [ ] [ ] vis , int [ ] [ ] dis ) { if ( x < 0 || x >= vis . length || y < 0 || y >= vis [ x ] . length || vis [ x ] [ y ] ) return 0 ; vis [ x ] [ y ] = true ; int res = 1 ; for ( int i = 0 ; i < 2 ; i ++ ) res += recurse ( x + dis [ i ] [ 0 ] , y + dis [ i ] [ 1 ] , vis , dis ) ; return res ; } }

import sys NEW_LINE def dbg ( a ) : sys . stderr . write ( str ( a ) ) NEW_LINE def readint ( ) : return int ( raw_input ( ) ) NEW_LINE def readfloat ( ) : return float ( raw_input ( ) ) NEW_LINE def readarray ( foo ) : return map ( foo , raw_input ( ) . split ( ) ) NEW_LINE res = 0 NEW_LINE used = { } NEW_LINE q = [ ] NEW_LINE qh , qt = ( 0 , 0 ) NEW_LINE dx1 , dy1 , dx2 , dy2 = ( 0 , 0 , 0 , 0 ) NEW_LINE w , h = ( 0 , 0 ) NEW_LINE def doit ( x , y ) : NEW_LINE INDENT global res NEW_LINE if x < 0 or x >= w : return NEW_LINE if y < 0 or y >= h : return NEW_LINE if ( x , y ) in used : return NEW_LINE res += 1 NEW_LINE used [ ( x , y ) ] = True NEW_LINE global q , qh , qt NEW_LINE q . append ( ( x + dx1 , y + dy1 ) ) NEW_LINE q . append ( ( x + dx2 , y + dy2 ) ) NEW_LINE qt += 2 NEW_LINE DEDENT for test in range ( readint ( ) ) : NEW_LINE INDENT dbg ( " Test ▁ % d \n " % ( test + 1 ) ) NEW_LINE ( w , h ) = readarray ( int ) NEW_LINE ( dx1 , dy1 ) = readarray ( int ) NEW_LINE ( dx2 , dy2 ) = readarray ( int ) NEW_LINE ( x0 , y0 ) = readarray ( int ) NEW_LINE res = 0 NEW_LINE used = { } NEW_LINE q = [ ( x0 , y0 ) ] NEW_LINE qh = 0 NEW_LINE qt = 1 NEW_LINE while qh < qt : NEW_LINE INDENT ( x , y ) = q [ qh ] NEW_LINE doit ( x , y ) NEW_LINE qh += 1 NEW_LINE DEDENT print " Case ▁ # % d : ▁ % d " % ( test + 1 , res ) NEW_LINE DEDENT


import java . io . FileReader ; import java . io . FileWriter ; import java . io . IOException ; import java . io . PrintWriter ; import java . util . ArrayList ; import java . util . Arrays ; import java . util . List ; import java . util . Scanner ; import java . util . SortedSet ; import java . util . TreeSet ; public class ObservationWheel { static Scanner sc ; static PrintWriter out ; public static void main ( String [ ] args ) throws IOException { sc = new Scanner ( new FileReader ( " D - small - attempt0 . in " ) ) ; out = new PrintWriter ( new FileWriter ( " D - small - attempt0 . out " ) ) ; int testCases = sc . nextInt ( ) ; for ( int testCase = 1 ; testCase <= testCases ; testCase ++ ) { out . println ( String . format ( " Case ▁ # % s : ▁ % s " , testCase , solveCase ( ) ) ) ; } out . close ( ) ; } static double solveCase ( ) { String bits = sc . next ( ) ; N = bits . length ( ) ; memo = new double [ 1 << N ] ; Arrays . fill ( memo , - 1.0 ) ; int init = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( bits . charAt ( i ) == ' X ' ) init |= 1 << i ; return solve ( init ) ; } static double solve ( int mask ) { if ( mask == ( 1 << N ) - 1 ) { return 0 ; } if ( memo [ mask ] < - 0.5 ) { double res = 0.0 ; for ( int index = 0 ; index < N ; index ++ ) { for ( int wait = 0 ; wait < N ; wait ++ ) { int exactIndex = ( index + wait ) % N ; if ( ( mask >> exactIndex & 1 ) == 0 ) { res += ( N - wait + solve ( mask | 1 << exactIndex ) ) / N ; break ; } } } memo [ mask ] = res ; } return memo [ mask ] ; } static double [ ] memo ; static int N ; }

import java . util . * ; public class D { static boolean bit_set ( int set , int bit ) { return ( ( set >>> bit ) & 1 ) == 1 ; } static Double [ ] [ ] DP = new Double [ 21 ] [ 1 << 20 ] ; static double f ( int n , int used ) { if ( used == ( 1 << n ) - 1 ) return 0 ; if ( DP [ n ] [ used ] != null ) return DP [ n ] [ used ] ; double ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int nxt = i ; int money = n ; while ( bit_set ( used , nxt ) ) { nxt = ( nxt + 1 ) % n ; money -- ; } ans += 1.0 / n * ( money + f ( n , used | 1 << nxt ) ) ; } return DP [ n ] [ used ] = ans ; } public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; int T = in . nextInt ( ) ; for ( int cas = 1 ; cas <= T ; cas ++ ) { char [ ] S = in . next ( ) . toCharArray ( ) ; int used = 0 ; for ( int i = 0 ; i < S . length ; i ++ ) if ( S [ i ] == ' X ' ) used |= 1 << i ; System . out . printf ( " Case ▁ # % d : ▁ % .10f \n " , cas , f ( S . length , used ) ) ; } } }

import java . io . * ; import java . util . * ; public class D { private static String fileName = D . class . getSimpleName ( ) . replaceFirst ( " _ . * " , " " ) . toLowerCase ( ) ; private static String inputFileName = fileName + " . in " ; private static String outputFileName = fileName + " . out " ; private static Scanner in ; private static PrintWriter out ; private void solve ( ) { String s = in . next ( ) ; int n = s . length ( ) ; int init = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' . ' ) { init |= 1 << i ; } } double [ ] a = new double [ 1 << n ] ; for ( int mask = 1 ; mask <= init ; mask ++ ) { for ( int i = 0 ; i < n ; i ++ ) { int j = 0 ; int k = i ; for ( ; ; ) { if ( ( ( mask >> k ) & 1 ) == 1 ) { break ; } j ++ ; k ++ ; if ( k == n ) { k = 0 ; } } int nm = mask ^ ( 1 << k ) ; a [ mask ] += ( n - j + a [ nm ] ) / n ; } } out . println ( a [ init ] ) ; } public static void main ( String [ ] args ) throws IOException { Locale . setDefault ( Locale . US ) ; if ( args . length >= 2 ) { inputFileName = args [ 0 ] ; outputFileName = args [ 1 ] ; } in = new Scanner ( new File ( inputFileName ) ) ; out = new PrintWriter ( outputFileName ) ; int tests = in . nextInt ( ) ; in . nextLine ( ) ; for ( int t = 1 ; t <= tests ; t ++ ) { out . print ( " Case ▁ # " + t + " : ▁ " ) ; new D ( ) . solve ( ) ; System . out . println ( " Case ▁ # " + t + " : ▁ solved " ) ; } in . close ( ) ; out . close ( ) ; } }

import static java . lang . Math . * ; import static java . util . Arrays . * ; import java . io . * ; import java . util . * ; public class D { Scanner sc = new Scanner ( System . in ) ; int N ; char [ ] oc ; void read ( ) { oc = sc . next ( ) . toCharArray ( ) ; N = oc . length ; } void solve ( ) { double [ ] dp = new double [ 1 << N ] ; for ( int i = ( 1 << N ) - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < N ; j ++ ) { for ( int k = 0 ; k < N ; k ++ ) if ( ( i >> ( ( j + k ) % N ) & 1 ) == 0 ) { dp [ i ] += ( dp [ i | 1 << ( ( j + k ) % N ) ] + N - k ) / N ; break ; } } } int r = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( oc [ i ] == ' X ' ) r |= 1 << i ; System . out . printf ( " % .10f % n " , dp [ r ] ) ; } void run ( ) { int caseN = sc . nextInt ( ) ; for ( int caseID = 1 ; caseID <= caseN ; caseID ++ ) { read ( ) ; System . out . printf ( " Case ▁ # % d : ▁ " , caseID ) ; solve ( ) ; System . out . flush ( ) ; } } void debug ( Object ... os ) { System . err . println ( deepToString ( os ) ) ; } public static void main ( String [ ] args ) { try { System . setIn ( new BufferedInputStream ( new FileInputStream ( args . length > 0 ? args [ 0 ] : ( D . class . getName ( ) + " . in " ) ) ) ) ; } catch ( Exception e ) { } new D ( ) . run ( ) ; } }

import java . util . * ; import static java . lang . Math . * ; import java . io . * ; public class D { public static void p ( Object ... args ) { System . out . println ( Arrays . deepToString ( args ) ) ; } public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; int T = in . nextInt ( ) ; for ( int zz = 1 ; zz <= T ; zz ++ ) { String start = in . next ( ) ; N = start . length ( ) ; int st = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( start . charAt ( i ) == ' X ' ) st |= 1 << i ; } DP = new double [ 1 << N ] ; Arrays . fill ( DP , - 1 ) ; DP [ ( 1 << N ) - 1 ] = 0 ; System . out . format ( " Case ▁ # % d : ▁ % .12f \n " , zz , compute ( st ) ) ; } } static int N ; static double [ ] DP ; static double compute ( int at ) { if ( DP [ at ] != - 1 ) return DP [ at ] ; double ans = 0.0 ; for ( int i = 0 ; i < N ; i ++ ) { int cost = N ; int j = i ; while ( get ( at , j ) ) { j ++ ; cost -- ; if ( j == N ) j = 0 ; } ans += cost + compute ( at | ( 1 << j ) ) ; } ans /= N ; DP [ at ] = ans ; return ans ; } static boolean get ( int num , int bit ) { return ( num & ( 1 << bit ) ) != 0 ; } }

from sys import stdin NEW_LINE memo = [ [ - 1 ] * ( 1 << n ) for n in xrange ( 21 ) ] NEW_LINE for n in xrange ( 21 ) : NEW_LINE INDENT memo [ n ] [ 0 ] = 0.0 NEW_LINE for ss in xrange ( 1 , 1 << n ) : NEW_LINE INDENT if memo [ n ] [ ss ] != - 1 : NEW_LINE INDENT continue NEW_LINE DEDENT s = bin ( ss ) [ 2 : ] . zfill ( n ) NEW_LINE m = [ ] NEW_LINE for k in xrange ( n ) : NEW_LINE INDENT if ss >> k & 1 : NEW_LINE INDENT m . append ( memo [ n ] [ ss ^ ( 1 << k ) ] ) NEW_LINE DEDENT else : NEW_LINE INDENT m . append ( 0.0 ) NEW_LINE DEDENT DEDENT m . reverse ( ) NEW_LINE d = [ 0.0 ] * n NEW_LINE t = s + s NEW_LINE i = n + n - 1 NEW_LINE while t [ i ] == '0' : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT while i >= 0 : NEW_LINE INDENT if t [ i ] == '1' : NEW_LINE INDENT x = m [ i % n ] NEW_LINE d [ i % n ] = n + x NEW_LINE j = i - 1 NEW_LINE while j >= 0 and t [ j ] == '0' : NEW_LINE INDENT d [ j % n ] = n - ( i - j ) + x NEW_LINE j -= 1 NEW_LINE DEDENT i = j NEW_LINE DEDENT DEDENT f = sum ( d ) / n NEW_LINE for i in xrange ( n ) : NEW_LINE INDENT memo [ n ] [ int ( s , 2 ) ] = f NEW_LINE s = s [ - 1 ] + s [ : - 1 ] NEW_LINE DEDENT DEDENT DEDENT def solve ( ) : NEW_LINE INDENT x = stdin . readline ( ) . strip ( ) NEW_LINE n = len ( x ) NEW_LINE t = 0 NEW_LINE for i in xrange ( n ) : NEW_LINE INDENT if x [ i ] == ' . ' : NEW_LINE INDENT t += 1 << ( n - 1 - i ) NEW_LINE DEDENT DEDENT return memo [ n ] [ t ] NEW_LINE DEDENT T = int ( stdin . readline ( ) ) NEW_LINE for i in xrange ( T ) : NEW_LINE INDENT print " Case ▁ # % d : ▁ % .12f " % ( i + 1 , solve ( ) ) NEW_LINE DEDENT

def reader ( inFile ) : NEW_LINE INDENT return inFile . readline ( ) NEW_LINE DEDENT from sys import stderr NEW_LINE def solver ( gonds ) : NEW_LINE INDENT n = len ( gonds ) NEW_LINE k = sum ( [ 1 << i for i in xrange ( n ) if gonds [ i ] == " X " ] ) NEW_LINE exps = [ 0.0 ] * ( 1 << n ) NEW_LINE for i in xrange ( ( 1 << n ) - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( i & k ) == k : NEW_LINE INDENT tot = 0 NEW_LINE for j in xrange ( n ) : NEW_LINE INDENT pay = n NEW_LINE w = j NEW_LINE while ( ( i >> w ) & 1 ) == 1 : NEW_LINE INDENT w = ( w + 1 ) % n NEW_LINE pay -= 1 NEW_LINE DEDENT tot += ( pay + exps [ i + ( 1 << w ) ] ) NEW_LINE DEDENT exps [ i ] = tot / n NEW_LINE DEDENT DEDENT return exps [ k ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT from GCJ import GCJ NEW_LINE GCJ ( reader , solver , " / Users / lpebody / gcj / 2013_3 / d / " , " d " ) . run ( ) NEW_LINE DEDENT

F = None NEW_LINE def f ( s , n ) : NEW_LINE INDENT global F NEW_LINE if s in F : NEW_LINE INDENT return F [ s ] NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT s_ = s [ i : ] + s [ : i ] NEW_LINE if s_ in F : NEW_LINE INDENT return F [ s_ ] NEW_LINE DEDENT DEDENT ans = 0.0 NEW_LINE if len ( [ 1 for c in list ( s ) if c == ' . ' ] ) == 0 : NEW_LINE INDENT F [ s ] = ans NEW_LINE return ans NEW_LINE DEDENT ts = s * 2 NEW_LINE dist = 0 NEW_LINE last = 2 * n NEW_LINE for i in reversed ( range ( 2 * n ) ) : NEW_LINE INDENT if ts [ i ] == ' . ' : NEW_LINE INDENT dist = 0 NEW_LINE last = i NEW_LINE DEDENT else : NEW_LINE INDENT dist += 1 NEW_LINE DEDENT if i < n : NEW_LINE INDENT ps = last if last < n else last - n NEW_LINE ns = s [ : ps ] + ' X ' + s [ ps + 1 : ] NEW_LINE ans += n - ( last - i ) + f ( ns , n ) NEW_LINE DEDENT DEDENT ans /= n NEW_LINE F [ s ] = ans NEW_LINE return ans NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT global F NEW_LINE T = int ( raw_input ( ) ) NEW_LINE for t in range ( 1 , T + 1 ) : NEW_LINE INDENT s = raw_input ( ) . strip ( ' \n ' ) NEW_LINE n = len ( s ) NEW_LINE F = dict ( ) NEW_LINE ans = f ( s , n ) NEW_LINE print " Case ▁ # % d : ▁ % .10f " % ( t , ans ) NEW_LINE DEDENT DEDENT main ( ) NEW_LINE

import sys NEW_LINE sys . path . insert ( 0 , ' / home / rishig / codejam / ' ) NEW_LINE from library import * NEW_LINE d = { } NEW_LINE def ecase ( st , n ) : NEW_LINE INDENT if st [ - 1 ] == ' . ' : NEW_LINE INDENT s = st NEW_LINE DEDENT else : NEW_LINE INDENT f = st . find ( ' . ' ) NEW_LINE if f == - 1 : return 0 NEW_LINE s = st [ f + 1 : ] + st [ : f + 1 ] NEW_LINE DEDENT if s in d : NEW_LINE INDENT return d [ s ] NEW_LINE DEDENT assert s [ - 1 ] == ' . ' NEW_LINE arr = [ ] NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if s [ i ] == ' . ' : NEW_LINE INDENT arr . append ( ( count + 1 , s [ : i ] + ' X ' + s [ i + 1 : ] ) ) NEW_LINE count = 0 NEW_LINE DEDENT else : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT total = n - 1. * sum ( c * ( c - 1 ) / 2 for c , s in arr ) / n NEW_LINE newtot = total + 1. / n * sum ( c * ecase ( s , n ) for c , s in arr ) NEW_LINE d [ s ] = newtot NEW_LINE return newtot NEW_LINE DEDENT def solvecase ( case ) : NEW_LINE INDENT S = f . readline ( ) . strip ( ) NEW_LINE n = len ( S ) NEW_LINE return ecase ( S , n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT f = file ( sys . argv [ 1 ] ) NEW_LINE T = readint ( f ) NEW_LINE sys . stderr . write ( strftime ( " % H : % M : % S \n " ) ) NEW_LINE for case in range ( 1 , T + 1 ) : NEW_LINE INDENT ans = solvecase ( case ) NEW_LINE print ' Case ▁ # % d : ▁ % s ' % ( case , ans ) NEW_LINE if T <= 15 or case == 1 or case % max ( ( T // 10 ) , 5 ) == 0 : NEW_LINE INDENT sys . stderr . write ( ' completed ▁ case ▁ % d , ▁ ' % case ) NEW_LINE sys . stderr . write ( strftime ( " % H : % M : % S \n " ) ) NEW_LINE DEDENT DEDENT DEDENT

rl = raw_input NEW_LINE cases = int ( rl ( ) ) NEW_LINE cache = { } NEW_LINE def go ( n , status ) : NEW_LINE INDENT if status == ( 2 ** n ) - 1 : return 0.0 NEW_LINE key = ( n , status ) NEW_LINE if key in cache : return cache [ key ] NEW_LINE ret = 0.0 NEW_LINE for arr in xrange ( n ) : NEW_LINE INDENT wait = 0 NEW_LINE while status & ( 2 ** ( ( arr + wait ) % n ) ) : NEW_LINE INDENT wait += 1 NEW_LINE DEDENT ret += ( n - wait ) + go ( n , status + ( 2 ** ( ( arr + wait ) % n ) ) ) NEW_LINE DEDENT cache [ key ] = ret / n NEW_LINE return ret / n NEW_LINE DEDENT for cc in xrange ( cases ) : NEW_LINE INDENT occupied = rl ( ) . strip ( ) NEW_LINE n = len ( occupied ) NEW_LINE status = sum ( [ 2 ** i for i in xrange ( n ) if occupied [ i ] == ' X ' ] ) NEW_LINE print ' Case ▁ # % d : ▁ % .10lf ' % ( cc + 1 , go ( n , status ) ) NEW_LINE DEDENT


import java . util . * ; import static java . lang . Math . * ; public class B { public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; int T = in . nextInt ( ) ; for ( int zz = 1 ; zz <= T ; zz ++ ) { int N = in . nextInt ( ) ; int [ ] MC = new int [ 10000 ] ; for ( int i = 0 ; i < N ; i ++ ) { MC [ in . nextInt ( ) - 1 ] ++ ; } int ans = 0 ; int [ ] C = new int [ 10000 ] ; int [ ] E = new int [ 10000 ] ; for ( int i = 1 ; i <= N ; i ++ ) { boolean good = true ; for ( int j = 0 ; j < 10000 ; j ++ ) C [ j ] = MC [ j ] ; Arrays . fill ( E , 0 ) ; for ( int j = 0 ; j < 10000 ; j ++ ) { while ( C [ j ] > 0 ) { boolean legal = true ; if ( j + i > 10000 ) { legal = false ; } else { for ( int k = 0 ; k < i ; k ++ ) if ( C [ j + k ] == 0 ) legal = false ; } if ( legal ) { for ( int k = 0 ; k < i ; k ++ ) C [ j + k ] -- ; if ( j + i < 10000 ) E [ j + i ] ++ ; } else { if ( E [ j ] > 0 ) { E [ j ] -- ; C [ j ] -- ; if ( j + 1 < 10000 ) E [ j + 1 ] ++ ; } else { good = false ; break ; } } } } if ( good ) { ans = i ; } } System . out . format ( " Case ▁ # % d : ▁ % d \n " , zz , ans ) ; } } }

import java . util . * ; import java . io . * ; class B_as { Scanner in ; PrintWriter out ; public void solve ( ) throws IOException { int testNo = in . nextInt ( ) ; for ( int test = 1 ; test <= testNo ; test ++ ) { int n = in . nextInt ( ) ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = in . nextInt ( ) ; } Arrays . sort ( a ) ; int m = 0 ; int [ ] last = new int [ n ] ; int [ ] len = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int blen = Integer . MAX_VALUE ; int best = - 1 ; for ( int j = 0 ; j < m ; j ++ ) { if ( last [ j ] == a [ i ] - 1 && len [ j ] < blen ) { best = j ; blen = len [ j ] ; } } if ( best == - 1 ) { last [ m ] = a [ i ] ; len [ m ] = 1 ; m ++ ; } else { last [ best ] = a [ i ] ; len [ best ] = len [ best ] + 1 ; } } int blen = Integer . MAX_VALUE ; for ( int i = 0 ; i < m ; i ++ ) { if ( len [ i ] < blen ) { blen = len [ i ] ; } } if ( blen == Integer . MAX_VALUE ) { blen = 0 ; } out . println ( " Case ▁ # " + test + " : ▁ " + blen ) ; } } public void run ( ) { try { in = new Scanner ( new File ( " B - large . in " ) ) ; out = new PrintWriter ( new File ( " B - large . out " ) ) ; solve ( ) ; in . close ( ) ; out . close ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } public static void main ( String [ ] arg ) { Locale . setDefault ( Locale . US ) ; new B_as ( ) . run ( ) ; } }

package round3 ; import java . io . File ; import java . io . IOException ; import java . io . PrintWriter ; import java . util . ArrayList ; import java . util . Arrays ; import java . util . Scanner ; public class B { static class Niz { int zadnji , duzina ; Niz ( int element ) { duzina = 1 ; zadnji = element ; } } public static void main ( String [ ] args ) throws IOException { Scanner in = new Scanner ( new File ( " B . in " ) ) ; PrintWriter out = new PrintWriter ( new File ( " B . out " ) ) ; int tt = in . nextInt ( ) ; for ( int ttt = 1 ; ttt <= tt ; ttt ++ ) { int n = in . nextInt ( ) ; int [ ] t = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) t [ i ] = in . nextInt ( ) ; int res = 0 ; if ( n > 0 ) { Arrays . sort ( t ) ; ArrayList < Niz > nizovi = new ArrayList < B . Niz > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { Niz a = null ; for ( Niz niz : nizovi ) if ( niz . zadnji == t [ i ] - 1 && ( a == null || a . duzina > niz . duzina ) ) a = niz ; if ( a == null ) nizovi . add ( new Niz ( t [ i ] ) ) ; else { a . duzina ++ ; a . zadnji = t [ i ] ; } } res = n ; for ( Niz niz : nizovi ) if ( niz . duzina < res ) res = niz . duzina ; } out . printf ( " Case ▁ # % d : ▁ % d " , ttt , res ) ; out . println ( ) ; } out . flush ( ) ; out . close ( ) ; in . close ( ) ; } }

import java . io . * ; import java . util . * ; import java . math . * ; public class Main { static StreamTokenizer in ; static int next ( ) throws Exception { in . nextToken ( ) ; return ( int ) in . nval ; } ; static PrintWriter out ; static String NAME = " b " ; public static void main ( String [ ] args ) throws Exception { in = new StreamTokenizer ( new BufferedReader ( new FileReader ( new File ( NAME + " . in " ) ) ) ) ; out = new PrintWriter ( new File ( NAME + " . out " ) ) ; int tests = next ( ) ; for ( int test = 1 ; test <= tests ; test ++ ) { int n = next ( ) ; if ( n == 0 ) { out . println ( " Case ▁ # " + test + " : ▁ 0" ) ; continue ; } int m = 10005 ; int [ ] k = new int [ m ] ; for ( int i = 0 ; i < n ; i ++ ) k [ next ( ) ] ++ ; int answ = 100000 ; int start = 0 ; int end = 0 ; while ( true ) { while ( start < m && k [ start ] == 0 ) start ++ ; if ( start == m ) break ; end = start ; while ( k [ end ] != 0 ) end ++ ; ArrayList < Integer > s = new ArrayList < Integer > ( ) ; ArrayList < Integer > e = new ArrayList < Integer > ( ) ; for ( int i = start ; i <= end ; i ++ ) { if ( k [ i ] > k [ i - 1 ] ) for ( int j = 0 ; j < k [ i ] - k [ i - 1 ] ; j ++ ) s . add ( i ) ; if ( k [ i ] < k [ i - 1 ] ) for ( int j = 0 ; j < k [ i - 1 ] - k [ i ] ; j ++ ) e . add ( i ) ; } for ( int i = 0 ; i < s . size ( ) ; i ++ ) answ = Math . min ( answ , e . get ( i ) - s . get ( i ) ) ; start = end ; } out . println ( " Case ▁ # " + test + " : ▁ " + answ ) ; } out . close ( ) ; } }

import sun . java2d . pipe . OutlineTextRenderer ; import java . io . File ; import java . io . FileNotFoundException ; import java . io . PrintWriter ; import java . util . Scanner ; public class B { private static final int MAX = 20000 ; private String solve ( Scanner in ) { int n = in . nextInt ( ) ; if ( n == 0 ) { return "0" ; } int [ ] a = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ in . nextInt ( ) - 1 ] ++ ; } int l = 1 ; int r = n + 1 ; main : while ( r > l + 1 ) { int m = ( l + r ) / 2 ; int [ ] aa = a . clone ( ) ; int [ ] b = new int [ MAX + 1 ] ; for ( int i = 0 ; i < MAX ; i ++ ) { while ( aa [ i ] > 0 ) { if ( i > 0 && b [ i - 1 ] > 0 ) { b [ i - 1 ] -- ; b [ i ] ++ ; aa [ i ] -- ; } else { boolean ok = true ; for ( int j = i ; j < i + m ; j ++ ) { if ( aa [ j ] == 0 ) ok = false ; aa [ j ] -- ; } if ( ! ok ) { r = m ; continue main ; } b [ i + m - 1 ] ++ ; } } } l = m ; } return " " + l ; } public static void main ( String [ ] args ) throws FileNotFoundException { Scanner in = new Scanner ( new File ( " input . txt " ) ) ; PrintWriter out = new PrintWriter ( " output . txt " ) ; int T = in . nextInt ( ) ; for ( int i = 0 ; i < T ; i ++ ) { String s = " Case ▁ # " + ( i + 1 ) + " : ▁ " + new B ( ) . solve ( in ) ; out . println ( s ) ; System . out . println ( s ) ; } out . close ( ) ; } }

def solve ( a ) : NEW_LINE INDENT if len ( a ) == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT b = [ ] NEW_LINE cur = a [ 0 ] - 1 NEW_LINE cnt = 0 NEW_LINE for x in a : NEW_LINE INDENT if x == cur : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT else : NEW_LINE INDENT b . append ( cnt ) NEW_LINE cnt = 1 NEW_LINE cur = x NEW_LINE DEDENT DEDENT b . append ( cnt ) NEW_LINE res = 100000 NEW_LINE Q = [ ] NEW_LINE b . append ( 0 ) NEW_LINE for i in range ( 1 , len ( b ) ) : NEW_LINE INDENT while b [ i ] > b [ i - 1 ] : NEW_LINE INDENT Q . append ( i ) NEW_LINE b [ i - 1 ] += 1 NEW_LINE DEDENT while b [ i ] < b [ i - 1 ] : NEW_LINE INDENT x = Q . pop ( 0 ) NEW_LINE b [ i - 1 ] -= 1 NEW_LINE res = min ( res , i - x ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT fi = open ( " input . txt " ) NEW_LINE for turn in range ( int ( fi . readline ( ) ) ) : NEW_LINE INDENT a = map ( int , fi . readline ( ) . split ( ) ) NEW_LINE n = a . pop ( 0 ) NEW_LINE assert n == len ( a ) NEW_LINE a . sort ( ) NEW_LINE res = 100000 NEW_LINE i = 0 NEW_LINE for j in range ( 1 , len ( a ) ) : NEW_LINE INDENT if a [ j ] > a [ j - 1 ] + 1 : NEW_LINE INDENT res = min ( res , solve ( a [ i : j ] ) ) NEW_LINE i = j NEW_LINE DEDENT DEDENT res = min ( res , solve ( a [ i : len ( a ) ] ) ) NEW_LINE print " Case ▁ # { 0 } : ▁ { 1 } " . format ( turn + 1 , res ) NEW_LINE DEDENT

from __future__ import division NEW_LINE from __future__ import with_statement NEW_LINE from __future__ import print_function NEW_LINE def memoized ( func ) : NEW_LINE INDENT mem = { } NEW_LINE def wrapped ( * args ) : NEW_LINE INDENT if args not in mem : NEW_LINE INDENT mem [ args ] = func ( * args ) NEW_LINE DEDENT return mem [ args ] NEW_LINE DEDENT return wrapped NEW_LINE DEDENT TASK = " B " NEW_LINE from collections import Counter NEW_LINE from bisect import insort NEW_LINE print ( " Calculation . . . " ) NEW_LINE with open ( TASK + " . in " ) as infile : NEW_LINE INDENT with open ( TASK + " . out " , mode = " wt " ) as outfile : NEW_LINE INDENT cases = int ( infile . readline ( ) ) NEW_LINE for ncase in range ( cases ) : NEW_LINE INDENT print ( " Case ▁ # { nc } " . format ( nc = ncase + 1 ) ) NEW_LINE cards = [ int ( s ) for s in infile . readline ( ) . split ( ) ] [ 1 : ] NEW_LINE values = Counter ( cards ) NEW_LINE nums = [ values [ i ] for i in xrange ( 0 , 10002 ) ] NEW_LINE starts = [ ] NEW_LINE shortest = 100000 NEW_LINE c = 0 NEW_LINE for i , n in enumerate ( nums ) : NEW_LINE INDENT if n > c : NEW_LINE INDENT starts += [ i ] * ( n - c ) NEW_LINE DEDENT if c > n : NEW_LINE INDENT shortest = min ( shortest , i - starts [ c - n - 1 ] ) NEW_LINE starts = starts [ c - n : ] NEW_LINE DEDENT c = n NEW_LINE DEDENT if len ( cards ) == 0 : NEW_LINE INDENT shortest = 0 NEW_LINE DEDENT outfile . write ( " Case ▁ # { nc } : ▁ { data } \n " . format ( nc = ncase + 1 , data = shortest ) ) NEW_LINE DEDENT DEDENT DEDENT print ( " Calculation ▁ done . " ) NEW_LINE

import sys NEW_LINE def foo ( ifile ) : NEW_LINE INDENT a = [ int ( x ) for x in ifile . readline ( ) . split ( ) ] [ 1 : ] NEW_LINE a . sort ( ) NEW_LINE if len ( a ) == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT bb = { } NEW_LINE for item in a : NEW_LINE INDENT t = 0 NEW_LINE if item - 1 in bb : NEW_LINE INDENT t = bb [ item - 1 ] [ 0 ] NEW_LINE bb [ item - 1 ] = bb [ item - 1 ] [ 1 : ] NEW_LINE if len ( bb [ item - 1 ] ) == 0 : NEW_LINE INDENT del bb [ item - 1 ] NEW_LINE DEDENT DEDENT if item not in bb : NEW_LINE INDENT bb [ item ] = [ ] NEW_LINE DEDENT bb [ item ] . append ( t + 1 ) NEW_LINE bb [ item ] . sort ( ) NEW_LINE DEDENT res = len ( a ) NEW_LINE for k , v in bb . items ( ) : NEW_LINE INDENT res = min ( res , min ( v ) ) NEW_LINE DEDENT return res NEW_LINE DEDENT def main ( ifile , ofile ) : NEW_LINE INDENT n = int ( ifile . readline ( ) ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT ofile . write ( " Case ▁ # % s : ▁ % s \n " % ( i + 1 , foo ( ifile ) ) ) NEW_LINE DEDENT DEDENT main ( sys . stdin , sys . stdout ) NEW_LINE

def check ( n , a ) : NEW_LINE INDENT cnt = 0 NEW_LINE b = [ 0 ] * 1010 NEW_LINE f = [ 0 ] * 1010 NEW_LINE len = [ 0 ] * 1010 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT j = 0 NEW_LINE for k in range ( 1 , cnt + 1 ) : NEW_LINE INDENT if ( b [ k ] + 1 == a [ i ] ) and ( j == 0 or len [ k ] < len [ j ] ) : NEW_LINE INDENT j = k NEW_LINE DEDENT DEDENT if j == 0 : NEW_LINE INDENT cnt += 1 NEW_LINE j = cnt NEW_LINE DEDENT b [ j ] = a [ i ] NEW_LINE len [ j ] += 1 NEW_LINE DEDENT ans = 100000000 NEW_LINE for i in range ( 1 , cnt + 1 ) : NEW_LINE INDENT ans = min ( ans , len [ i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT for c in range ( input ( ) ) : NEW_LINE INDENT a = map ( int , raw_input ( ) . split ( ) ) NEW_LINE n = a [ 0 ] NEW_LINE a = a [ 1 : ] NEW_LINE b = [ 0 ] NEW_LINE a . sort ( ) ; NEW_LINE b . extend ( a ) NEW_LINE ans = check ( n , b ) NEW_LINE if ans == 100000000 : NEW_LINE INDENT ans = 0 NEW_LINE DEDENT print " Case ▁ # " + str ( c + 1 ) + " : ▁ " + str ( ans ) NEW_LINE DEDENT

import sys NEW_LINE from collections import * NEW_LINE def solve ( ) : NEW_LINE INDENT data = map ( int , sys . stdin . readline ( ) . split ( ) ) [ 1 : ] NEW_LINE if not data : NEW_LINE INDENT return 0 NEW_LINE DEDENT c = Counter ( data ) NEW_LINE q = deque ( ) NEW_LINE BIG = 2000 NEW_LINE ret = BIG NEW_LINE it = sorted ( c . iteritems ( ) ) NEW_LINE it . append ( [ it [ - 1 ] [ 0 ] + 1 , 0 ] ) NEW_LINE i_ = it [ 0 ] [ 0 ] - 1 NEW_LINE for i , n in it : NEW_LINE INDENT if i != i_ + 1 : NEW_LINE INDENT ret = min ( ret , min ( [ i_ + 1 - j for j in q ] ) ) NEW_LINE q . clear ( ) NEW_LINE DEDENT while n > len ( q ) : NEW_LINE INDENT q . append ( i ) NEW_LINE DEDENT while len ( q ) > n : NEW_LINE INDENT ret = min ( ret , i - q . popleft ( ) ) NEW_LINE DEDENT i_ = i NEW_LINE DEDENT return ret NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT T = int ( sys . stdin . readline ( ) ) NEW_LINE for t in range ( T ) : NEW_LINE INDENT print " Case ▁ # { } : ▁ { } " . format ( t + 1 , solve ( ) ) NEW_LINE DEDENT DEDENT


import java . util . * ; public class C { static Scanner in ; public static void main ( String [ ] args ) { in = new Scanner ( System . in ) ; int T = in . nextInt ( ) ; for ( int C = 1 ; C <= T ; C ++ ) { System . out . println ( " Case ▁ # " + C + " : ▁ " + solve ( ) ) ; } } public static String solve ( ) { String out = " " ; int a = in . nextInt ( ) , b = in . nextInt ( ) , c = in . nextInt ( ) , limit = in . nextInt ( ) ; int [ ] abT = new int [ 201 ] ; int [ ] bcT = new int [ 201 ] ; int [ ] acT = new int [ 201 ] ; int count = 0 ; for ( int i = a ; i >= 1 ; i -- ) { for ( int j = b ; j >= 1 ; j -- ) { for ( int kk = c ; kk >= 1 ; kk -- ) { int k = ( kk + ( i ) + ( j ) ) % c ; k ++ ; int ab = i * 15 + j ; int ac = i * 15 + k ; int bc = j * 15 + k ; if ( abT [ ab ] < limit && bcT [ bc ] < limit && acT [ ac ] < limit ) { out += " \n " + i + " ▁ " + j + " ▁ " + k ; count ++ ; abT [ ab ] ++ ; bcT [ bc ] ++ ; acT [ ac ] ++ ; } else { } } } } System . err . println ( count ) ; return count + " " + out ; } }

package gcj2016r1c ; import java . io . BufferedReader ; import java . io . BufferedWriter ; import java . io . FileReader ; import java . io . FileWriter ; import java . util . ArrayList ; import java . util . StringTokenizer ; public class ProblemC { public static void main ( String [ ] args ) throws Exception { String fileName = args [ 0 ] ; ProblemC obj = new ProblemC ( ) ; obj . solve ( fileName ) ; } public void solve ( String fileName ) throws Exception { BufferedReader br = new BufferedReader ( new FileReader ( fileName ) ) ; BufferedWriter bw = new BufferedWriter ( new FileWriter ( fileName + " . out " ) ) ; int T = Integer . parseInt ( br . readLine ( ) ) ; for ( int i = 0 ; i < T ; i ++ ) { String str = br . readLine ( ) ; StringTokenizer token = new StringTokenizer ( str , " ▁ " ) ; int J = Integer . parseInt ( token . nextToken ( ) ) ; int P = Integer . parseInt ( token . nextToken ( ) ) ; int S = Integer . parseInt ( token . nextToken ( ) ) ; int K = Integer . parseInt ( token . nextToken ( ) ) ; ArrayList < int [ ] > list = new ArrayList < > ( ) ; int [ ] chk = new int [ P ] ; int s = 0 ; for ( int j = 0 ; j < J ; j ++ ) { for ( int p = 0 ; p < P ; p ++ ) { if ( j != 0 ) { s = chk [ p ] ; } for ( int k = 0 ; k < Math . min ( K , S ) ; k ++ ) { list . add ( new int [ ] { j + 1 , p + 1 , s + 1 } ) ; s = ( s + 1 ) % S ; chk [ p ] = s ; } } } bw . write ( " Case ▁ # " + ( i + 1 ) + " : ▁ " + list . size ( ) ) ; bw . write ( " \r \n " ) ; for ( int [ ] e : list ) { bw . write ( e [ 0 ] + " ▁ " + e [ 1 ] + " ▁ " + e [ 2 ] ) ; bw . write ( " \r \n " ) ; } } bw . close ( ) ; br . close ( ) ; } }

import java . io . File ; import java . io . PrintWriter ; import java . util . Scanner ; public class R1CQ3 { public static void main ( String [ ] args ) { try { String fileName = " C - large " ; Scanner in = new Scanner ( new File ( fileName + " . in " ) ) ; PrintWriter out = new PrintWriter ( fileName + " . out " ) ; int numberOfTestCases = in . nextInt ( ) ; for ( int caseNum = 1 ; caseNum <= numberOfTestCases ; caseNum ++ ) { int J = in . nextInt ( ) ; int P = in . nextInt ( ) ; int S = in . nextInt ( ) ; int k = in . nextInt ( ) ; int dayCount = 0 ; String result = " " ; for ( int j = 1 ; j <= J ; j ++ ) { for ( int p = 1 ; p <= P ; p ++ ) { for ( int i = 0 ; i < S && i < k ; i ++ ) { dayCount ++ ; int a = ( ( ( i + p + j ) % S ) + 1 ) ; result += j + " ▁ " + p + " ▁ " + a + " \n " ; } } } out . println ( " Case ▁ # " + caseNum + " : ▁ " + dayCount + " \n " + result . substring ( 0 , result . length ( ) - 1 ) ) ; } in . close ( ) ; out . close ( ) ; } catch ( Exception ex ) { ex . printStackTrace ( ) ; } } }

package codejamquestion ; import java . io . File ; import java . io . FileNotFoundException ; import java . io . PrintWriter ; import java . util . Scanner ; public class CodeJamQuestion { public static void main ( String [ ] args ) { try { Scanner in = new Scanner ( new File ( " C - large . in " ) ) ; PrintWriter out = new PrintWriter ( " C - large . out " ) ; int tests = in . nextInt ( ) ; for ( int test = 1 ; test <= tests ; test ++ ) { String answer = " Case ▁ # " + test + " : ▁ " ; String outfits = " " ; int days = 0 ; int J = in . nextInt ( ) ; int P = in . nextInt ( ) ; int S = in . nextInt ( ) ; int k = in . nextInt ( ) ; for ( int j = 1 ; j <= J ; j ++ ) { for ( int p = 1 ; p <= P ; p ++ ) { for ( int i = 0 ; i < S && i < k ; i ++ ) { outfits += j + " ▁ " + p + " ▁ " + ( ( ( i + p + j ) % S ) + 1 ) + " \n " ; days ++ ; } } } answer += days + " \n " + outfits ; out . println ( answer ) ; } in . close ( ) ; out . close ( ) ; } catch ( FileNotFoundException ex ) { System . out . println ( " ERROR " ) ; } } }

package Round1 ; import java . io . * ; public class C { public static void main ( String [ ] args ) throws IOException { BufferedReader in = new BufferedReader ( new FileReader ( " / Users / chao / Downloads / C - large . in " ) ) ; FileWriter out = new FileWriter ( " / Users / chao / Desktop / C - large . txt " ) ; String s = in . readLine ( ) ; int m = Integer . parseInt ( s ) ; int [ ] a = new int [ 3 ] ; for ( int cases = 1 ; cases <= m ; cases ++ ) { String [ ] ss = in . readLine ( ) . split ( " ▁ " ) ; for ( int i = 0 ; i < 3 ; i ++ ) { a [ i ] = Integer . parseInt ( ss [ i ] ) ; } int k = Integer . parseInt ( ss [ 3 ] ) ; k = Math . min ( k , a [ 2 ] ) ; int ans = a [ 0 ] * a [ 1 ] * k ; out . write ( " Case ▁ # " + cases + " : ▁ " + ans + " \n " ) ; for ( int i = 1 ; i <= a [ 0 ] ; i ++ ) { for ( int j = 1 ; j <= a [ 1 ] ; j ++ ) { for ( int x = 0 ; x < k ; x ++ ) { int val = ( i + j + x - 2 ) % a [ 2 ] + 1 ; out . write ( i + " ▁ " + j + " ▁ " + val + " \n " ) ; } } } } in . close ( ) ; out . flush ( ) ; out . close ( ) ; } }

T = int ( raw_input ( ) ) NEW_LINE for i in range ( 1 , T + 1 ) : NEW_LINE INDENT j , p , s , k = [ int ( x ) for x in raw_input ( ) . split ( ) ] NEW_LINE print " Case ▁ # % d : " % i , j * p * min ( s , k ) NEW_LINE ans = [ ( x , y ) for x in range ( 1 , j + 1 ) for y in range ( 1 , p + 1 ) for t in range ( min ( s , k ) ) ] NEW_LINE now = 0 NEW_LINE last = - 1 NEW_LINE for i in ans : NEW_LINE INDENT if i [ 0 ] != last : NEW_LINE INDENT now = i [ 0 ] - 1 NEW_LINE last = i [ 0 ] NEW_LINE DEDENT print i [ 0 ] , i [ 1 ] , now % s + 1 NEW_LINE now += 1 NEW_LINE DEDENT DEDENT

from itertools import product NEW_LINE from itertools import combinations NEW_LINE F = open ( ' C - large . in ' ) NEW_LINE O = open ( ' C - large . out ' , ' w ' ) NEW_LINE T = int ( F . readline ( ) ) NEW_LINE print T NEW_LINE for case in range ( 1 , T + 1 ) : NEW_LINE INDENT J , P , S , K = map ( int , F . readline ( ) . rstrip ( ) . split ( ) ) NEW_LINE x = combinations ( list ( str ( i ) for i in range ( 1 , S + 1 ) ) , min ( S , K ) ) NEW_LINE t = [ ] NEW_LINE for i in x : NEW_LINE INDENT t . append ( i ) NEW_LINE DEDENT q = len ( t ) NEW_LINE ans = J * P * min ( S , K ) NEW_LINE O . write ( " Case ▁ # % d : ▁ % d \n " % ( case , ans ) ) NEW_LINE for j in range ( 1 , J + 1 ) : NEW_LINE INDENT for p in range ( 1 , P + 1 ) : NEW_LINE INDENT for s in range ( 1 , min ( S , K ) + 1 ) : NEW_LINE INDENT string = str ( j ) + ' ▁ ' + str ( p ) + ' ▁ ' + str ( ( s + j - 1 + p - 1 ) % S + 1 ) NEW_LINE O . write ( " % s \n " % ( string ) ) NEW_LINE DEDENT DEDENT DEDENT DEDENT F . close ( ) NEW_LINE O . close ( ) NEW_LINE

import pulp NEW_LINE def solve ( J , P , S , K , Case ) : NEW_LINE INDENT Js = range ( J ) NEW_LINE Ps = range ( P ) NEW_LINE Ss = range ( S ) NEW_LINE prob = pulp . LpProblem ( " Sudoku ▁ Problem " , pulp . LpMaximize ) NEW_LINE x = pulp . LpVariable . dicts ( " x " , ( Js , Ps , Ss ) , 0 , 1 , pulp . LpInteger ) NEW_LINE for j in Js : NEW_LINE INDENT for p in Ps : NEW_LINE INDENT prob += pulp . lpSum ( [ x [ j ] [ p ] [ s ] for s in Ss ] ) <= K , " " NEW_LINE DEDENT DEDENT for s in Ss : NEW_LINE INDENT for p in Ps : NEW_LINE INDENT prob += pulp . lpSum ( [ x [ j ] [ p ] [ s ] for j in Js ] ) <= K , " " NEW_LINE DEDENT DEDENT for s in Ss : NEW_LINE INDENT for j in Js : NEW_LINE INDENT prob += pulp . lpSum ( [ x [ j ] [ p ] [ s ] for p in Ps ] ) <= K , " " NEW_LINE DEDENT DEDENT prob += pulp . lpSum ( [ x [ j ] [ p ] [ s ] for j in Js for p in Ps for s in Ss ] ) , " Arbitrary ▁ Objective ▁ Function . ▁ We ▁ solve ▁ feasibility ▁ problem " NEW_LINE prob . solve ( ) NEW_LINE obj = pulp . value ( prob . objective ) NEW_LINE print " Case ▁ # { 0 } : ▁ { 1 } " . format ( Case , int ( obj ) ) NEW_LINE for j in Js : NEW_LINE INDENT for p in Ps : NEW_LINE INDENT for s in Ss : NEW_LINE INDENT val = pulp . value ( x [ j ] [ p ] [ s ] ) NEW_LINE if val == 1 : NEW_LINE INDENT print j + 1 , p + 1 , s + 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return obj NEW_LINE DEDENT T = int ( raw_input ( ) ) + 1 NEW_LINE for tc in xrange ( 1 , T ) : NEW_LINE INDENT J , P , S , K = map ( int , raw_input ( ) . split ( ) ) NEW_LINE solve ( J , P , S , K , tc ) NEW_LINE DEDENT

f = ' c - sample . in ' NEW_LINE f = ' C - small - attempt0 . in ' NEW_LINE f = ' C - large . in ' NEW_LINE inp = map ( str . strip , list ( open ( f ) ) [ 1 : ] ) NEW_LINE def solve ( J , P , S , K ) : NEW_LINE INDENT options = [ ] NEW_LINE for j in range ( J ) : NEW_LINE INDENT for p in range ( P ) : NEW_LINE INDENT for k in range ( min ( S , K ) ) : NEW_LINE INDENT options . append ( [ j , p , ( j + p + k ) % S ] ) NEW_LINE DEDENT DEDENT DEDENT N = J * P * min ( S , K ) NEW_LINE return str ( N ) + ' \n ' + ' \n ' . join ( ' ▁ ' . join ( str ( x + 1 ) for x in op ) for op in options ) NEW_LINE DEDENT t = 1 NEW_LINE while inp : NEW_LINE INDENT vals = map ( int , inp . pop ( 0 ) . split ( ) ) NEW_LINE print ' Case ▁ # % d : ▁ % s ' % ( t , solve ( * vals ) ) NEW_LINE t += 1 NEW_LINE DEDENT

from collections import * NEW_LINE def solve ( J , P , S , K ) : NEW_LINE INDENT Q = set ( ) NEW_LINE A = defaultdict ( int ) NEW_LINE B = defaultdict ( int ) NEW_LINE C = defaultdict ( int ) NEW_LINE for j in range ( J ) : NEW_LINE INDENT for p in range ( P ) : NEW_LINE INDENT for s in range ( min ( S , K ) ) : NEW_LINE INDENT d = ( j + p + s ) % S NEW_LINE Q . add ( ( j , p , d ) ) NEW_LINE A [ ( j , p ) ] += 1 NEW_LINE B [ ( p , d ) ] += 1 NEW_LINE C [ ( d , j ) ] += 1 NEW_LINE DEDENT DEDENT DEDENT assert ( max ( A . values ( ) ) <= K ) NEW_LINE assert ( max ( B . values ( ) ) <= K ) NEW_LINE assert ( max ( C . values ( ) ) <= K ) NEW_LINE assert ( len ( Q ) == J * P * min ( S , K ) ) NEW_LINE return Q NEW_LINE DEDENT def test ( ) : NEW_LINE INDENT for k in range ( 1 , 11 ) : NEW_LINE INDENT for s in range ( 1 , 11 ) : NEW_LINE INDENT for p in range ( 1 , s + 1 ) : NEW_LINE INDENT for j in range ( 1 , p + 1 ) : NEW_LINE INDENT solve ( j , p , s , k ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT T = int ( input ( ) ) NEW_LINE for t in range ( T ) : NEW_LINE INDENT J , P , S , K = map ( int , input ( ) . split ( ) ) NEW_LINE Q = solve ( J , P , S , K ) NEW_LINE print ( " Case ▁ # % d : ▁ % d " % ( t + 1 , len ( Q ) ) ) NEW_LINE for q in Q : NEW_LINE INDENT print ( q [ 0 ] + 1 , q [ 1 ] + 1 , q [ 2 ] + 1 ) NEW_LINE DEDENT DEDENT


import java . io . * ; import java . util . StringTokenizer ; import java . util . Arrays ; public class A { private String solve ( ) throws IOException { int n = nextInt ( ) ; long [ ] x = new long [ n ] ; long [ ] y = new long [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { x [ i ] = nextInt ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { y [ i ] = nextInt ( ) ; } Arrays . sort ( x ) ; Arrays . sort ( y ) ; long ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += x [ i ] * y [ n - 1 - i ] ; } return " " + ans ; } private BufferedReader reader ; private StringTokenizer tt = new StringTokenizer ( " " ) ; private String nextToken ( ) throws IOException { while ( ! tt . hasMoreTokens ( ) ) { tt = new StringTokenizer ( reader . readLine ( ) ) ; } return tt . nextToken ( ) ; } private int nextInt ( ) throws IOException { return Integer . parseInt ( nextToken ( ) ) ; } private void run ( ) throws IOException { reader = new BufferedReader ( new FileReader ( " A - large . in " ) ) ; PrintWriter writer = new PrintWriter ( new File ( " A - large . out " ) ) ; try { int n = nextInt ( ) ; for ( int i = 0 ; i < n ; i ++ ) { writer . print ( " Case ▁ # " + ( i + 1 ) + " : ▁ " ) ; writer . print ( solve ( ) ) ; writer . println ( ) ; } } finally { reader . close ( ) ; writer . close ( ) ; } } public static void main ( String [ ] args ) { try { new A ( ) . run ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } }

import java . io . BufferedReader ; import java . io . FileReader ; import java . io . FileWriter ; import java . util . ArrayList ; import java . util . Collections ; import java . util . List ; public class P1 { public static void main ( String [ ] args ) throws Exception { BufferedReader br = new BufferedReader ( new FileReader ( " p1 . in " ) ) ; String line = " " ; int t = Integer . parseInt ( br . readLine ( ) ) ; FileWriter out = new FileWriter ( " p1 . out " ) ; for ( int i = 0 ; i < t ; i ++ ) { int n = Integer . parseInt ( br . readLine ( ) ) ; String v1 = br . readLine ( ) ; String v2 = br . readLine ( ) ; String [ ] vv1 = v1 . split ( " \\ s + " ) ; String [ ] vv2 = v2 . split ( " \\ s + " ) ; List < Long > iv1 = s2l ( vv1 ) ; List < Long > iv2 = s2l ( vv2 ) ; Collections . sort ( iv1 ) ; Collections . sort ( iv2 ) ; if ( iv1 . size ( ) != iv2 . size ( ) ) System . out . println ( " Oops ! " ) ; long res = 0 ; for ( int j = 0 ; j < iv1 . size ( ) ; j ++ ) { System . out . println ( iv1 . get ( j ) + " ▁ * ▁ " + iv2 . get ( iv1 . size ( ) - 1 - j ) ) ; res += iv1 . get ( j ) * iv2 . get ( iv1 . size ( ) - 1 - j ) ; } out . write ( " Case ▁ # " + ( i + 1 ) + " : ▁ " + res + " \n " ) ; System . out . println ( res ) ; } br . close ( ) ; out . close ( ) ; } private static List < Long > s2l ( String [ ] vv1 ) { List < Long > ret = new ArrayList < Long > ( vv1 . length ) ; for ( int i = 0 ; i < vv1 . length ; i ++ ) ret . add ( Long . parseLong ( vv1 [ i ] ) ) ; return ret ; } }

import java . io . * ; import java . util . * ; public class SolutionA implements Runnable { public static void main ( String [ ] args ) { new Thread ( new SolutionA ( ) ) . run ( ) ; } public void run ( ) { try { Locale . setDefault ( Locale . US ) ; br = new BufferedReader ( new FileReader ( FILENAME + " . in " ) ) ; out = new PrintWriter ( FILENAME + " . out " ) ; solve ( ) ; out . close ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } BufferedReader br ; PrintWriter out ; StringTokenizer st ; boolean eof ; String nextToken ( ) { while ( st == null || ! st . hasMoreTokens ( ) ) { try { st = new StringTokenizer ( br . readLine ( ) ) ; } catch ( Exception e ) { eof = true ; return "0" ; } } return st . nextToken ( ) ; } int nextInt ( ) { return Integer . parseInt ( nextToken ( ) ) ; } long nextLong ( ) { return Long . parseLong ( nextToken ( ) ) ; } double nextDouble ( ) { return Double . parseDouble ( nextToken ( ) ) ; } private static final String FILENAME = " A - large " ; public void solve ( ) throws IOException { int testsn = nextInt ( ) ; for ( int test = 0 ; test < testsn ; test ++ ) { out . print ( " Case ▁ # " + ( test + 1 ) + " : ▁ " ) ; int n = nextInt ( ) ; long [ ] a = new long [ n ] ; long [ ] b = new long [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = nextLong ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { b [ i ] = nextLong ( ) ; } Arrays . sort ( a ) ; Arrays . sort ( b ) ; long ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += a [ i ] * b [ n - 1 - i ] ; } out . println ( ans ) ; } } }

import java . util . * ; public class MinimumProduct { public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; int numCases = in . nextInt ( ) ; for ( int i = 0 ; i < numCases ; i ++ ) doCase ( i + 1 , in ) ; } private static void doCase ( int caseNum , Scanner in ) { int dims = in . nextInt ( ) ; List < Integer > v1 = new ArrayList < Integer > ( dims ) ; for ( int i = 0 ; i < dims ; i ++ ) v1 . add ( in . nextInt ( ) ) ; Collections . sort ( v1 ) ; List < Integer > v2 = new ArrayList < Integer > ( dims ) ; for ( int i = 0 ; i < dims ; i ++ ) v2 . add ( in . nextInt ( ) ) ; Collections . sort ( v2 , new Comparator < Integer > ( ) { @ Override public int compare ( Integer o1 , Integer o2 ) { return - o1 . compareTo ( o2 ) ; } } ) ; long prod = 0 ; for ( int i = 0 ; i < dims ; i ++ ) prod += v1 . get ( i ) * ( long ) v2 . get ( i ) ; System . out . println ( " Case ▁ # " + caseNum + " : ▁ " + prod ) ; } }

import java . io . BufferedReader ; import java . io . FileReader ; import java . io . IOException ; import java . io . PrintWriter ; import java . util . Arrays ; import java . util . StringTokenizer ; public class Solution implements Runnable { private static final String INPUT_FILE_NAME = " a . big . in . txt " ; private static final String OUTPUT_FILE_NAME = " a . big . out . txt " ; private BufferedReader rd ; private PrintWriter wr ; public static void main ( String [ ] args ) { new Thread ( new Solution ( ) ) . start ( ) ; } public void run ( ) { try { solve ( ) ; } catch ( IOException e ) { throw new RuntimeException ( e ) ; } } void solve ( ) throws IOException { rd = new BufferedReader ( new FileReader ( Solution . INPUT_FILE_NAME ) ) ; wr = new PrintWriter ( Solution . OUTPUT_FILE_NAME ) ; String line = rd . readLine ( ) ; StringTokenizer st = new StringTokenizer ( line ) ; int nn = Integer . parseInt ( st . nextToken ( ) ) ; for ( int ii = 0 ; ii < nn ; ++ ii ) { subSolve ( ii ) ; } rd . close ( ) ; wr . close ( ) ; } private void subSolve ( int ii ) throws IOException { String line = rd . readLine ( ) ; StringTokenizer st = new StringTokenizer ( line ) ; int n = Integer . parseInt ( st . nextToken ( ) ) ; long [ ] a = new long [ n ] ; long [ ] b = new long [ n ] ; line = rd . readLine ( ) ; st = new StringTokenizer ( line ) ; for ( int i = 0 ; i < n ; ++ i ) { a [ i ] = Long . parseLong ( st . nextToken ( ) ) ; } line = rd . readLine ( ) ; st = new StringTokenizer ( line ) ; for ( int i = 0 ; i < n ; ++ i ) { b [ i ] = Long . parseLong ( st . nextToken ( ) ) ; } Arrays . sort ( a ) ; Arrays . sort ( b ) ; long result = 0 ; for ( int i = 0 ; i < n ; ++ i ) { result += ( a [ i ] * b [ n - i - 1 ] ) ; } wr . println ( String . format ( " Case ▁ # % d : ▁ % d " , ii + 1 , result ) ) ; } }

import re , sys NEW_LINE def solve ( v1 , v2 ) : NEW_LINE INDENT v1 . sort ( ) NEW_LINE v2 . sort ( reverse = True ) NEW_LINE prods = [ a * b for a , b in zip ( v1 , v2 ) ] NEW_LINE sum = 0 NEW_LINE for p in prods : NEW_LINE INDENT sum += p NEW_LINE DEDENT return sum NEW_LINE DEDENT def parse_input ( ) : NEW_LINE INDENT if len ( sys . argv ) < 2 : NEW_LINE INDENT print " Enter ▁ an ▁ input ▁ filename ▁ please " NEW_LINE return NEW_LINE DEDENT inp = open ( sys . argv [ 1 ] ) NEW_LINE lines = [ re . sub ( r ' [ \n ] ' , ' ' , l ) for l in inp . readlines ( ) ] NEW_LINE num_cases = int ( lines [ 0 ] ) NEW_LINE line_num = 1 NEW_LINE for case in xrange ( num_cases ) : NEW_LINE INDENT n = int ( lines [ line_num ] ) NEW_LINE v1 = [ int ( i ) for i in lines [ line_num + 1 ] . split ( ) ] NEW_LINE v2 = [ int ( i ) for i in lines [ line_num + 2 ] . split ( ) ] NEW_LINE line_num += 3 NEW_LINE soln = solve ( v1 , v2 ) NEW_LINE print " Case ▁ # " + str ( case + 1 ) + " : " , soln NEW_LINE DEDENT DEDENT parse_input ( ) NEW_LINE

f = open ( r ' C : \goog\A - large . in ' ) NEW_LINE numcases = int ( f . readline ( ) ) NEW_LINE for case in range ( numcases ) : NEW_LINE INDENT line = f . readline ( ) . strip ( ) NEW_LINE n = int ( line ) NEW_LINE line = f . readline ( ) . strip ( ) NEW_LINE vect1 = map ( int , line . split ( ' ▁ ' ) ) NEW_LINE line = f . readline ( ) . strip ( ) NEW_LINE vect2 = map ( int , line . split ( ' ▁ ' ) ) NEW_LINE if n != len ( vect1 ) or n != len ( vect2 ) : raise NEW_LINE vect1 . sort ( ) NEW_LINE vect2 . sort ( ) NEW_LINE vect2 . reverse ( ) NEW_LINE total = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT total += vect1 [ i ] * vect2 [ i ] NEW_LINE DEDENT print " Case ▁ # " + str ( case + 1 ) + " : ▁ " + str ( total ) NEW_LINE DEDENT

filename = " A - large . in " NEW_LINE f = open ( filename , ' r ' ) NEW_LINE of = open ( " A - large . out " , ' w ' ) NEW_LINE N = int ( f . readline ( ) ) NEW_LINE for x in xrange ( N ) : NEW_LINE INDENT n = int ( f . readline ( ) ) NEW_LINE a = f . readline ( ) . split ( ' ▁ ' ) NEW_LINE b = f . readline ( ) . split ( ' ▁ ' ) NEW_LINE a = [ int ( y ) for y in a ] NEW_LINE b = [ int ( y ) for y in b ] NEW_LINE a . sort ( ) NEW_LINE b . sort ( reverse = True ) NEW_LINE s = 0 NEW_LINE for y in xrange ( len ( a ) ) : NEW_LINE INDENT s += a [ y ] * b [ y ] NEW_LINE DEDENT print >> of , " Case ▁ # % d : ▁ % d " % ( x + 1 , s ) NEW_LINE DEDENT f . close ( ) NEW_LINE of . close ( ) NEW_LINE

import sys NEW_LINE def debug ( * msg ) : NEW_LINE INDENT DEBUG = not True NEW_LINE if DEBUG : NEW_LINE INDENT for x in msg [ : - 1 ] : NEW_LINE INDENT print x , NEW_LINE DEDENT print msg [ - 1 ] NEW_LINE DEDENT DEDENT def read_int_line ( f ) : NEW_LINE INDENT return int ( f . readline ( ) . rstrip ( ) ) NEW_LINE DEDENT def read_vector ( f ) : NEW_LINE INDENT v = f . readline ( ) . rstrip ( ) . split ( ) NEW_LINE return [ int ( x ) for x in v ] NEW_LINE DEDENT def print_result ( case_num , result ) : NEW_LINE INDENT print ' Case ▁ # % d : ▁ % s ' % ( case_num , result ) NEW_LINE DEDENT def do_test_case ( f , case_num ) : NEW_LINE INDENT n = read_int_line ( f ) NEW_LINE x = read_vector ( f ) NEW_LINE debug ( x ) NEW_LINE y = read_vector ( f ) NEW_LINE debug ( y ) NEW_LINE x . sort ( ) NEW_LINE y . sort ( ) NEW_LINE y . reverse ( ) NEW_LINE s = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT s += x [ i ] * y [ i ] NEW_LINE DEDENT print_result ( case_num , ' % d ' % s ) NEW_LINE DEDENT def main ( input ) : NEW_LINE INDENT f = file ( input ) NEW_LINE n = read_int_line ( f ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT do_test_case ( f , i + 1 ) NEW_LINE DEDENT f . close ( ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT if len ( sys . argv ) < 2 : NEW_LINE INDENT print ' usage : ▁ % s ▁ < input > ' % sys . argv [ 0 ] NEW_LINE sys . exit ( 1 ) NEW_LINE DEDENT main ( sys . argv [ 1 ] ) NEW_LINE DEDENT

import sys NEW_LINE _TYPE = { ' d ' : int , ' l ' : long , ' f ' : float , ' s ' : str } NEW_LINE _buffer = [ ] NEW_LINE def next_token ( ) : NEW_LINE INDENT if not _buffer : NEW_LINE INDENT line = sys . stdin . readline ( ) NEW_LINE _buffer . extend ( line . split ( ) ) NEW_LINE DEDENT return _buffer . pop ( 0 ) NEW_LINE DEDENT def scanf ( format ) : NEW_LINE INDENT if len ( format ) % 2 != 0 : NEW_LINE INDENT raise ValueError NEW_LINE DEDENT for i in xrange ( 0 , len ( format ) , 2 ) : NEW_LINE INDENT if format [ i ] != ' % ' : NEW_LINE INDENT raise ValueError NEW_LINE DEDENT if format [ i + 1 ] not in ' dfs ' : NEW_LINE INDENT raise ValueError NEW_LINE DEDENT DEDENT result = [ ] NEW_LINE for i in xrange ( 1 , len ( format ) , 2 ) : NEW_LINE INDENT token = next_token ( ) NEW_LINE value = _TYPE [ format [ i ] ] ( token ) NEW_LINE result . append ( value ) NEW_LINE DEDENT return tuple ( result ) NEW_LINE DEDENT def printf ( format , * args ) : NEW_LINE INDENT message = format % args NEW_LINE sys . stdout . write ( message ) NEW_LINE return len ( message ) NEW_LINE DEDENT def solve ( ) : NEW_LINE INDENT n , = scanf ( ' % d ' ) NEW_LINE v1 = list ( scanf ( ' % d ' * n ) ) NEW_LINE v2 = list ( scanf ( ' % d ' * n ) ) NEW_LINE v1 . sort ( ) NEW_LINE v2 . sort ( ) NEW_LINE msp = sum ( x * y for x , y in zip ( v1 , v2 [ : : - 1 ] ) ) NEW_LINE return msp NEW_LINE DEDENT num_cases , = scanf ( ' % d ' ) NEW_LINE for case_num in xrange ( num_cases ) : NEW_LINE INDENT result = solve ( ) NEW_LINE printf ( ' Case ▁ # % d : ▁ % s \n ' , case_num + 1 , result ) NEW_LINE DEDENT