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 ) ; } } }

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


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 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 ; }

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 . * ; 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 ; } }


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 ) ; } } }

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 . * ; 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 ( ) ; } }


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 ; } }

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


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 ( ) ; } }


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 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


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 java . io . * ; import java . math . * ; import java . util . * ; public class Main { static class Tree { final String name ; final ArrayList < Tree > a = new ArrayList < Tree > ( ) ; Tree ( String name ) { this . name = name ; } int size ( ) { if ( a . size ( ) == 0 ) return 1 ; int ans = 1 ; for ( Tree t : a ) ans += t . size ( ) ; return ans ; } void out ( String s ) { System . out . println ( s + name ) ; for ( Tree t : a ) t . out ( s + " ▁ ▁ " ) ; } void out ( ) { out ( " " ) ; } } static void add ( Tree t , String d ) { String [ ] ss = d . substring ( 1 ) . split ( " / " ) ; for ( String s : ss ) { Tree w = null ; for ( Tree r : t . a ) if ( r . name . equals ( s ) ) { w = r ; break ; } if ( w == null ) { w = new Tree ( s ) ; t . a . add ( w ) ; } t = w ; } } public static void main ( String [ ] args ) throws Exception { Scanner in = new Scanner ( new File ( " Main . in " ) ) ; PrintWriter out = new PrintWriter ( new File ( " Main . out " ) ) ; int tests = in . nextInt ( ) ; for ( int test = 1 ; test <= tests ; test ++ ) { out . print ( " Case ▁ # " + test + " : ▁ " ) ; int n = in . nextInt ( ) ; int m = in . nextInt ( ) ; in . nextLine ( ) ; Tree t = new Tree ( " " ) ; for ( int i = 0 ; i < n ; i ++ ) { String d = in . nextLine ( ) ; add ( t , d ) ; } int was = t . size ( ) ; for ( int i = 0 ; i < m ; i ++ ) { String d = in . nextLine ( ) ; add ( t , d ) ; } out . println ( t . size ( ) - was ) ; } out . close ( ) ; } }

import sys NEW_LINE import os NEW_LINE f = open ( sys . argv [ 1 ] ) NEW_LINE def fun ( cs ) : NEW_LINE INDENT dt = [ x . strip ( ) for x in f . readline ( ) . split ( " ▁ " ) ] NEW_LINE n = int ( dt [ 0 ] ) NEW_LINE m = int ( dt [ 1 ] ) NEW_LINE got = set ( ' / ' ) NEW_LINE res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT line = f . readline ( ) . strip ( ) NEW_LINE ps = line . split ( " / " ) NEW_LINE cur = " " NEW_LINE for t in ps : NEW_LINE INDENT cur = cur + " / " + t NEW_LINE if cur not in got : NEW_LINE INDENT got . add ( cur ) NEW_LINE DEDENT DEDENT DEDENT for i in range ( m ) : NEW_LINE INDENT line = f . readline ( ) . strip ( ) NEW_LINE ps = line . split ( " / " ) NEW_LINE cur = " " NEW_LINE for t in ps : NEW_LINE INDENT cur = cur + " / " + t NEW_LINE if cur not in got : NEW_LINE INDENT got . add ( cur ) NEW_LINE res += 1 NEW_LINE DEDENT DEDENT DEDENT print " Case ▁ # % d : ▁ % d " % ( cs , res ) NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT T = int ( f . readline ( ) . strip ( ) ) NEW_LINE for i in range ( T ) : NEW_LINE INDENT fun ( i + 1 ) NEW_LINE DEDENT DEDENT main ( ) NEW_LINE


ntests = int ( input ( ) ) NEW_LINE for tt in range ( 1 , ntests + 1 ) : NEW_LINE INDENT [ N , M ] = [ int ( x ) for x in input ( ) . split ( ) ] NEW_LINE dirs = set ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT p = input ( ) + " / " NEW_LINE for z in range ( len ( p ) - 1 ) : NEW_LINE INDENT if ( p [ z + 1 ] == ' / ' ) : NEW_LINE INDENT dirs . add ( p [ : ( z + 1 ) ] ) NEW_LINE DEDENT DEDENT DEDENT k = len ( dirs ) NEW_LINE for i in range ( M ) : NEW_LINE INDENT p = input ( ) + " / " NEW_LINE for z in range ( len ( p ) - 1 ) : NEW_LINE INDENT if ( p [ z + 1 ] == ' / ' ) : NEW_LINE INDENT dirs . add ( p [ : ( z + 1 ) ] ) NEW_LINE DEDENT DEDENT DEDENT print ( " Case ▁ # " + str ( tt ) + " : ▁ " + str ( len ( dirs ) - k ) ) NEW_LINE DEDENT


from scanf import * NEW_LINE opers = 0 NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . children = { } NEW_LINE DEDENT def addPath ( self , path ) : NEW_LINE INDENT if len ( path ) == 0 : return NEW_LINE if not path [ 0 ] in self . children : NEW_LINE INDENT global opers NEW_LINE opers += 1 NEW_LINE self . children [ path [ 0 ] ] = Node ( ) NEW_LINE DEDENT self . children [ path [ 0 ] ] . addPath ( path [ 1 : ] ) NEW_LINE DEDENT DEDENT def solveCase ( cas ) : NEW_LINE INDENT global opers NEW_LINE n , m = scanf ( " % d % d " ) NEW_LINE root = Node ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT root . addPath ( scanf ( " % s " ) [ 0 ] . split ( " / " ) [ 1 : ] ) NEW_LINE DEDENT opers = 0 NEW_LINE for i in range ( m ) : NEW_LINE INDENT root . addPath ( scanf ( " % s " ) [ 0 ] . split ( " / " ) [ 1 : ] ) NEW_LINE DEDENT print ( " Case ▁ # " + str ( cas ) + " : ▁ " + str ( opers ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT tests = scanf ( " % d " ) [ 0 ] NEW_LINE for i in range ( tests ) : NEW_LINE INDENT solveCase ( i + 1 ) NEW_LINE DEDENT DEDENT


def add_folder ( s ) : NEW_LINE INDENT folder = ' / ' NEW_LINE aux = 0 NEW_LINE idx = 1 NEW_LINE for word in s . split ( ' / ' ) [ 1 : ] : NEW_LINE INDENT folder += word + ' / ' NEW_LINE if folder not in dirs : NEW_LINE INDENT dirs . add ( folder ) NEW_LINE aux += 1 NEW_LINE DEDENT DEDENT return aux NEW_LINE DEDENT f = open ( ' a . in ' , ' r ' ) NEW_LINE T = int ( f . readline ( ) ) NEW_LINE for t_no in range ( 1 , T + 1 ) : NEW_LINE INDENT dirs = set ( ) NEW_LINE N , M = [ int ( x ) for x in f . readline ( ) . split ( ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT s = f . readline ( ) [ : - 1 ] NEW_LINE add_folder ( s ) NEW_LINE DEDENT sol = 0 NEW_LINE for i in range ( M ) : NEW_LINE INDENT s = f . readline ( ) [ : - 1 ] NEW_LINE sol += add_folder ( s ) NEW_LINE DEDENT print ' Case ▁ # % s : ▁ % s ' % ( t_no , sol ) NEW_LINE DEDENT


import sys NEW_LINE T = int ( raw_input ( ) ) NEW_LINE def components ( path ) : NEW_LINE INDENT parts = path . split ( ' / ' ) [ 1 : ] NEW_LINE for i in xrange ( len ( parts ) ) : NEW_LINE INDENT yield ' / ' . join ( parts [ : i + 1 ] ) NEW_LINE DEDENT DEDENT for testCase in xrange ( 1 , T + 1 ) : NEW_LINE INDENT N , M = raw_input ( ) . split ( ) NEW_LINE N , M = int ( N ) , int ( M ) NEW_LINE tree = set ( ) NEW_LINE for i in xrange ( N ) : NEW_LINE INDENT dir = raw_input ( ) NEW_LINE for p in components ( dir ) : NEW_LINE INDENT tree . add ( p ) NEW_LINE DEDENT DEDENT added = 0 NEW_LINE for j in xrange ( M ) : NEW_LINE INDENT dir = raw_input ( ) NEW_LINE for p in components ( dir ) : NEW_LINE INDENT if p not in tree : NEW_LINE INDENT added += 1 NEW_LINE DEDENT tree . add ( p ) NEW_LINE DEDENT DEDENT print " Case ▁ # % d : ▁ % d " % ( testCase , added ) NEW_LINE DEDENT


import java . util . * ; import java . io . * ; import java . math . * ; public class Solution { class Tree { Map < String , Tree > mp = new HashMap < String , Tree > ( ) ; public Tree ( ) { } } Tree root ; int res ; void process ( String s , boolean add ) { s = s . substring ( 1 ) ; String [ ] items = s . split ( " / " ) ; Tree cur = root ; for ( int i = 0 ; i < items . length ; i ++ ) { if ( ! cur . mp . containsKey ( items [ i ] ) ) { cur . mp . put ( items [ i ] , new Tree ( ) ) ; if ( add ) res ++ ; } cur = cur . mp . get ( items [ i ] ) ; } } 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 ( ) ; long time = System . currentTimeMillis ( ) ; for ( int caseNum = 1 ; caseNum <= caseCnt ; caseNum ++ ) { int have = sc . nextInt ( ) , next = sc . nextInt ( ) ; root = new Tree ( ) ; res = 0 ; for ( int i = 0 ; i < have ; i ++ ) { process ( sc . next ( ) , false ) ; } for ( int i = 0 ; i < next ; i ++ ) process ( sc . next ( ) , true ) ; pw . println ( " Case ▁ # " + caseNum + " : ▁ " + res ) ; System . out . println ( " Finished ▁ testcase ▁ " + caseNum + " , ▁ time ▁ = ▁ " + ( System . currentTimeMillis ( ) - time ) ) ; } pw . flush ( ) ; pw . close ( ) ; sc . close ( ) ; } public static void main ( String [ ] args ) throws Exception { ( new Solution ( ) ) . doMain ( ) ; } }


import java . io . File ; import java . io . FileNotFoundException ; import java . io . PrintWriter ; import java . util . * ; public class A { public void solve ( ) throws FileNotFoundException { Scanner in = new Scanner ( new File ( " A - large . in " ) ) ; PrintWriter out = new PrintWriter ( " A - large . out " ) ; int testN = in . nextInt ( ) ; for ( int test = 1 ; test <= testN ; test ++ ) { out . print ( " Case ▁ # " + test + " : ▁ " ) ; int n = in . nextInt ( ) ; int m = in . nextInt ( ) ; String [ ] exists = new String [ n ] ; String [ ] create = new String [ m ] ; Set < String > ex = new HashSet < String > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { exists [ i ] = in . next ( ) ; String [ ] t = exists [ i ] . split ( " / " ) ; String r = " " ; for ( int j = 1 ; j < t . length ; j ++ ) { r = r + " / " + t [ j ] ; ex . add ( r ) ; } } Set < String > need = new TreeSet < String > ( ) ; for ( int i = 0 ; i < m ; i ++ ) { create [ i ] = in . next ( ) ; String [ ] t = create [ i ] . split ( " / " ) ; String r = " " ; for ( int j = 1 ; j < t . length ; j ++ ) { r = r + " / " + t [ j ] ; need . add ( r ) ; } } int res = 0 ; for ( String s : need ) { if ( ! ex . contains ( s ) ) { res ++ ; ex . add ( s ) ; } } out . println ( res ) ; } in . close ( ) ; out . close ( ) ; } public static void main ( String [ ] args ) throws FileNotFoundException { new A ( ) . solve ( ) ; } }


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 ( ) ; } void check ( boolean f , String msg ) { if ( ! f ) { out . close ( ) ; throw new RuntimeException ( msg ) ; } } public void run ( ) { int tn = in . nextInt ( ) ; for ( int test = 1 ; test <= tn ; test ++ ) { int old = in . nextInt ( ) ; int create = in . nextInt ( ) ; in . nextLine ( ) ; Node root = new Node ( ) ; for ( int i = 0 ; i < old ; i ++ ) { add ( root , in . nextLine ( ) . split ( " / " ) , 1 ) ; } int r = 0 ; for ( int i = 0 ; i < create ; i ++ ) { r += add ( root , in . nextLine ( ) . split ( " / " ) , 1 ) ; } out . println ( " Case ▁ # " + test + " : ▁ " + r ) ; } try { close ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } int add ( Node node , String [ ] tokens , int p ) { if ( p >= tokens . length ) { return 0 ; } int r = 0 ; if ( node . children . get ( tokens [ p ] ) == null ) { node . children . put ( tokens [ p ] , new Node ( ) ) ; r = 1 ; } return r + add ( node . children . get ( tokens [ p ] ) , tokens , p + 1 ) ; } public class Node { Map < String , Node > children ; Node ( ) { children = new TreeMap < String , Node > ( ) ; } } public static void main ( String [ ] args ) throws IOException { new Thread ( new Main ( ) ) . start ( ) ; } }


import java . util . * ; import java . io . * ; public class x { static class Directory { int st ; HashMap < String , Directory > h ; Directory ( int cst ) { h = new HashMap < String , Directory > ( ) ; st = cst ; } Directory create ( String name , int sst ) { Directory c = h . get ( name ) ; if ( c != null ) return c ; c = new Directory ( sst ) ; h . put ( name , c ) ; return c ; } int count ( int lst ) { int cur = 0 ; if ( st > lst ) ++ cur ; for ( Directory z : h . values ( ) ) cur += z . count ( lst ) ; return cur ; } } public static void main ( String args [ ] ) throws Exception { Scanner in = new Scanner ( System . in ) ; int t = in . nextInt ( ) ; for ( int tt = 1 ; tt <= t ; tt ++ ) { Directory r = new Directory ( 0 ) ; int n = in . nextInt ( ) , m = in . nextInt ( ) ; for ( int i = 1 ; i <= n + m ; i ++ ) { String [ ] a = in . next ( ) . split ( " / " ) ; Directory c = r ; for ( int j = 1 ; j < a . length ; j ++ ) { c = c . create ( a [ j ] , i ) ; } } System . out . println ( " Case ▁ # " + tt + " : ▁ " + r . count ( n ) ) ; } ; } ; } ;


import java . io . * ; import java . util . * ; public class B { private static String fileName = B . 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 points = in . nextInt ( ) ; int d = in . nextInt ( ) ; List < Integer > xs = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < points ; i ++ ) { int x = in . nextInt ( ) ; int amount = in . nextInt ( ) ; for ( int j = 0 ; j < amount ; j ++ ) { xs . add ( x ) ; } } Collections . sort ( xs ) ; double left = 0 ; double right = 1e12 ; binsearch : while ( left + 0.5e-6 < right ) { System . out . println ( right - left ) ; double time = ( left + right ) / 2 ; double p = - 1e15 ; for ( int x : xs ) { double pp = Math . max ( x - time , p + d ) ; if ( pp > x + time ) { if ( left == time ) { break ; } left = time ; continue binsearch ; } p = pp ; } if ( right == time ) { break ; } right = time ; } out . println ( right ) ; } 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 FileReader ( inputFileName ) ) ; out = new PrintWriter ( outputFileName ) ; int tests = in . nextInt ( ) ; in . nextLine ( ) ; for ( int t = 1 ; t <= tests ; t ++ ) { out . print ( " Case ▁ # " + t + " : ▁ " ) ; new B ( ) . solve ( ) ; System . out . println ( " Case ▁ # " + t + " : ▁ solved " ) ; } in . close ( ) ; out . close ( ) ; } }

fp = open ( ' B - large . in ' , ' r ' ) NEW_LINE n = int ( fp . next ( ) ) NEW_LINE def max_sum_subsequence ( seq ) : NEW_LINE INDENT maxsofar = 0 NEW_LINE maxendinghere = 0 NEW_LINE for s in seq : NEW_LINE INDENT maxendinghere = max ( maxendinghere + s , 0 ) NEW_LINE maxsofar = max ( maxsofar , maxendinghere ) NEW_LINE DEDENT return maxsofar NEW_LINE DEDENT case = 1 NEW_LINE for line in fp : NEW_LINE INDENT result = 0 NEW_LINE npoints , dist = [ int ( x ) for x in line . strip ( ) . split ( ) ] NEW_LINE points = [ ] NEW_LINE nvend = 0 NEW_LINE for x in range ( npoints ) : NEW_LINE INDENT line = [ int ( x ) for x in fp . next ( ) . strip ( ) . split ( ) ] NEW_LINE nvend += line [ 1 ] NEW_LINE for y in range ( line [ 1 ] ) : NEW_LINE INDENT points . append ( line [ 0 ] ) NEW_LINE DEDENT DEDENT totaldist = dist * ( nvend - 1 ) NEW_LINE points . sort ( ) NEW_LINE dists = [ ] NEW_LINE for x in range ( len ( points ) - 1 ) : NEW_LINE INDENT dists . append ( dist - ( points [ x + 1 ] - points [ x ] ) ) NEW_LINE DEDENT result = max_sum_subsequence ( dists ) / 2. NEW_LINE print ' Case ▁ # % d : ▁ % .6f ' % ( case , result ) NEW_LINE case += 1 NEW_LINE DEDENT


import sys NEW_LINE NAME = None NEW_LINE def ok ( points , time , D ) : NEW_LINE INDENT prev = None NEW_LINE for p in points : NEW_LINE INDENT x , cnt = p [ 0 ] , p [ 1 ] NEW_LINE minX = x - time NEW_LINE if prev is not None : NEW_LINE INDENT minX = max ( minX , prev + D ) NEW_LINE DEDENT next = minX + D * ( cnt - 1 ) NEW_LINE if abs ( x - next ) > time : NEW_LINE INDENT return False NEW_LINE DEDENT prev = next NEW_LINE DEDENT return True NEW_LINE DEDENT def getMagicWord ( ) : NEW_LINE INDENT numPoints = nextToken ( int ) NEW_LINE D = nextToken ( int ) * 2 NEW_LINE p = [ ] NEW_LINE pMin = 1000000 NEW_LINE pMax = - pMin NEW_LINE n = 0 NEW_LINE for i in range ( numPoints ) : NEW_LINE INDENT x = nextToken ( int ) * 2 NEW_LINE cnt = nextToken ( int ) NEW_LINE p . append ( ( x , cnt ) ) NEW_LINE DEDENT p . sort ( key = lambda x : x [ 0 ] ) NEW_LINE L , R = - 1 , 10 ** 14 NEW_LINE while R - L > 1 : NEW_LINE INDENT K = ( L + R ) // 2 NEW_LINE if ok ( p , K , D ) : NEW_LINE INDENT R = K NEW_LINE DEDENT else : NEW_LINE INDENT L = K NEW_LINE DEDENT DEDENT return " % .1f " % ( R / 2 ) NEW_LINE DEDENT def nextToken ( func = None ) : NEW_LINE INDENT res = " " NEW_LINE while fin : NEW_LINE INDENT c = fin . read ( 1 ) NEW_LINE if c . isspace ( ) : NEW_LINE INDENT break NEW_LINE DEDENT res += c NEW_LINE DEDENT if func : NEW_LINE INDENT return func ( res ) NEW_LINE DEDENT else : NEW_LINE INDENT return res NEW_LINE DEDENT DEDENT def nextLine ( ) : NEW_LINE INDENT if fin : NEW_LINE INDENT return fin . readline ( ) NEW_LINE DEDENT else : NEW_LINE INDENT return " " NEW_LINE DEDENT DEDENT if NAME : NEW_LINE INDENT fin , fout = open ( NAME + " . in " , " r " ) , open ( NAME + " . out " , " w " ) NEW_LINE DEDENT else : NEW_LINE INDENT fin , fout = sys . stdin , sys . stdout NEW_LINE DEDENT for testNum in range ( nextToken ( int ) ) : NEW_LINE INDENT print ( " Case ▁ # % d : ▁ % s " % ( testNum + 1 , getMagicWord ( ) ) ) NEW_LINE DEDENT


from string import split NEW_LINE import sys NEW_LINE infile = open ( " large _ input . txt " , " r " ) NEW_LINE outfile = open ( " large _ output . txt " , " w " ) NEW_LINE trials = int ( infile . readline ( ) ) NEW_LINE for i in xrange ( trials ) : NEW_LINE INDENT nums = [ int ( x ) for x in split ( infile . readline ( ) ) ] NEW_LINE C = nums [ 0 ] NEW_LINE D = nums [ 1 ] NEW_LINE Locs = [ ] NEW_LINE V = [ ] NEW_LINE for j in xrange ( C ) : NEW_LINE INDENT nums = [ int ( x ) for x in split ( infile . readline ( ) ) ] NEW_LINE Locs . append ( nums [ 0 ] ) NEW_LINE V . append ( nums [ 1 ] ) NEW_LINE DEDENT sums = [ [ 0 for x in xrange ( C ) ] for y in xrange ( C ) ] NEW_LINE for x in xrange ( C ) : NEW_LINE INDENT sums [ x ] [ x ] = V [ x ] NEW_LINE DEDENT for j in xrange ( C ) : NEW_LINE INDENT for k in xrange ( j + 1 , C ) : NEW_LINE INDENT sums [ j ] [ k ] = sums [ j ] [ k - 1 ] + sums [ k ] [ k ] NEW_LINE DEDENT DEDENT best = - 1 NEW_LINE for j in xrange ( C ) : NEW_LINE INDENT for k in xrange ( j , C ) : NEW_LINE INDENT vendors = sums [ j ] [ k ] NEW_LINE minspread = ( vendors - 1.0 ) * D NEW_LINE currspread = float ( Locs [ k ] - Locs [ j ] ) NEW_LINE maxtime = ( minspread - currspread ) / 2.0 NEW_LINE if maxtime > best : NEW_LINE INDENT best = maxtime NEW_LINE DEDENT DEDENT DEDENT s = " Case ▁ # % d : ▁ % f \n " % ( ( i + 1 ) , best ) NEW_LINE outfile . write ( s ) NEW_LINE print s NEW_LINE DEDENT infile . close ( ) NEW_LINE outfile . close ( ) NEW_LINE


class empty : pass ; NEW_LINE def check ( C , D , V , t ) : NEW_LINE INDENT p = - ( 10 ** 100 ) NEW_LINE for v in V : NEW_LINE INDENT w = D * v . V NEW_LINE r = t - D * ( v . V - 1 ) / 2 NEW_LINE if r < 0 or p + w / 2 - v . P > r : NEW_LINE INDENT return False NEW_LINE DEDENT p = max ( v . P - r + w / 2 , p + w ) NEW_LINE DEDENT return True NEW_LINE DEDENT T = input ( ) NEW_LINE for case in range ( 1 , T + 1 ) : NEW_LINE INDENT C , D = map ( int , raw_input ( ) . split ( ) ) NEW_LINE D *= 2 NEW_LINE V = [ ] NEW_LINE for i in range ( C ) : NEW_LINE INDENT t = map ( int , raw_input ( ) . split ( ) ) NEW_LINE V += [ empty ( ) ] NEW_LINE V [ - 1 ] . P = t [ 0 ] * 2 NEW_LINE V [ - 1 ] . V = t [ 1 ] NEW_LINE DEDENT l = 0 NEW_LINE r = 10 ** 100 NEW_LINE while ( r - l > 1 ) : NEW_LINE INDENT m = ( l + r ) / 2 NEW_LINE if check ( C , D , V , m ) : NEW_LINE INDENT r = m NEW_LINE DEDENT else : NEW_LINE INDENT l = m NEW_LINE DEDENT DEDENT if check ( C , D , V , 0 ) : NEW_LINE INDENT r = 0 NEW_LINE DEDENT print " Case ▁ # % s : ▁ % .1f " % ( case , r * 0.5 ) NEW_LINE DEDENT


import sys , re , string , math NEW_LINE ssr = sys . stdin . readline NEW_LINE ssw = sys . stdout . write NEW_LINE D = - 99 NEW_LINE def comb ( L , x ) : NEW_LINE INDENT if not L : NEW_LINE INDENT return [ x ] NEW_LINE DEDENT ( p1 , v1 , t1 ) = L [ - 1 ] NEW_LINE ( p2 , v2 , t2 ) = x NEW_LINE v3 = v1 + v2 NEW_LINE if ( p2 - p1 ) < 0.5 * D * v3 : NEW_LINE INDENT m = 0.5 * D * v3 - ( p2 - p1 ) NEW_LINE if t1 < t2 : NEW_LINE INDENT d = t2 - t1 NEW_LINE mm = max ( 0 , 0.5 * ( m - d ) ) NEW_LINE t3 = t2 + mm NEW_LINE p3 = p2 + mm - 0.5 * D * v1 NEW_LINE DEDENT else : NEW_LINE INDENT d = t1 - t2 NEW_LINE mm = max ( 0 , 0.5 * ( m - d ) ) NEW_LINE t3 = t1 + mm NEW_LINE p3 = p1 - mm + 0.5 * D * v2 NEW_LINE DEDENT L [ - 1 ] = ( p3 , v3 , t3 ) NEW_LINE DEDENT else : NEW_LINE INDENT L . append ( x ) NEW_LINE DEDENT return L NEW_LINE DEDENT def do_one_case ( cnum ) : NEW_LINE INDENT global D NEW_LINE ( C , D ) = map ( int , ssr ( ) . split ( ) ) NEW_LINE L = [ ] ; NEW_LINE for i in range ( C ) : NEW_LINE INDENT ( P , V ) = map ( int , ssr ( ) . split ( ) ) NEW_LINE t = 0.5 * D * ( V - 1 ) NEW_LINE L . append ( ( 1.0 * P , V , t ) ) NEW_LINE DEDENT n = C + 1 NEW_LINE while len ( L ) < n : NEW_LINE INDENT n = len ( L ) NEW_LINE L = reduce ( comb , L , [ ] ) NEW_LINE DEDENT t = max ( [ t for ( P , V , t ) in L ] ) NEW_LINE print " Case ▁ # % d : ▁ % .14g " % ( cnum , t ) NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT N = int ( ssr ( ) . strip ( ) ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT do_one_case ( i + 1 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT main ( ) NEW_LINE DEDENT


package round1 ; import java . io . File ; import java . io . IOException ; import java . io . PrintWriter ; import java . util . Scanner ; public class HotDogs { static class Point { int position , total ; public Point ( int position , int total ) { this . position = position ; this . total = total ; } } static final double eps = 0.0000000001 ; public static void main ( String [ ] args ) throws IOException { Scanner in = new Scanner ( new File ( " hotDog . in " ) ) ; PrintWriter out = new PrintWriter ( new File ( " hotDog . out " ) ) ; int tt = in . nextInt ( ) ; for ( int t = 1 ; t <= tt ; t ++ ) { System . out . println ( t ) ; int c = in . nextInt ( ) ; double d = in . nextInt ( ) ; Point [ ] p = new Point [ c ] ; for ( int i = 0 ; i < c ; i ++ ) { int position = in . nextInt ( ) ; int total = in . nextInt ( ) ; p [ i ] = new Point ( position , total ) ; } double lg = 0 , dg = 1000000000 ; dg *= dg ; int count = 0 ; while ( dg - lg > eps && count < 100000 ) { count ++ ; double s = ( dg + lg ) / 2 ; boolean moze = true ; double tmin = - 1000000000000000000.0 ; for ( int i = 0 ; moze && i < c ; i ++ ) { double t1 = Math . max ( tmin , p [ i ] . position - s ) ; double tlast = t1 + ( double ) ( p [ i ] . total - 1 ) * ( double ) d ; if ( tlast - p [ i ] . position > s + eps ) moze = false ; else tmin = tlast + ( double ) d ; } if ( moze ) dg = s ; else lg = s ; } System . out . println ( dg + " ▁ " + lg ) ; out . printf ( " Case ▁ # % d : ▁ % .10f " , t , ( lg + dg ) / 2 ) ; out . println ( ) ; } out . flush ( ) ; out . close ( ) ; } }


package round1b ; import java . io . FileInputStream ; import java . io . FileNotFoundException ; import java . io . FileOutputStream ; import java . util . ArrayList ; import java . util . Collections ; import qual2011 . Kattio ; public class B { public static void main ( String [ ] args ) throws FileNotFoundException { Kattio io ; io = new Kattio ( new FileInputStream ( " src / round1b / B - large - 0 . in " ) , new FileOutputStream ( " src / round1b / B - large - 0 . out " ) ) ; int cases = io . getInt ( ) ; for ( int i = 1 ; i <= cases ; i ++ ) { io . print ( " Case ▁ # " + i + " : ▁ " ) ; new B ( ) . solve ( io ) ; } io . close ( ) ; } private void solve ( Kattio io ) { int C = io . getInt ( ) , D = io . getInt ( ) ; ArrayList < Long > vendor = new ArrayList < Long > ( ) ; for ( int i = 0 ; i < C ; i ++ ) { int P = io . getInt ( ) , V = io . getInt ( ) ; for ( int j = 0 ; j < V ; j ++ ) { vendor . add ( ( long ) P * 2 ) ; } } D *= 2 ; Collections . sort ( vendor ) ; long lo = 0 , hi = 2000000000000L ; int iter = 0 ; long best = hi ; while ( hi > lo ) { long x = ( hi + lo ) / 2 ; long allowed = vendor . get ( 0 ) - x ; boolean ok = true ; for ( Long v : vendor ) { long t = Math . max ( allowed , v - x ) ; if ( Math . abs ( t - v ) > x ) { ok = false ; break ; } allowed = t + D ; } if ( ok ) { hi = x ; best = x ; } else { lo = x + 1 ; } iter ++ ; } io . println ( best / 2.0 ) ; } }


import java . util . * ; import java . io . * ; import static java . lang . Math . * ; public class B { public static void main ( String [ ] args ) throws Exception { new B ( ) . run ( ) ; } final double EPS = 1e-9 ; void run ( ) throws Exception { Scanner sc = new Scanner ( System . in ) ; int T = sc . nextInt ( ) ; for ( int o = 1 ; o <= T ; o ++ ) { int C = sc . nextInt ( ) ; int D = sc . nextInt ( ) ; ArrayList < Integer > ps = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < C ; i ++ ) { int P = sc . nextInt ( ) ; int V = sc . nextInt ( ) ; for ( int j = 0 ; j < V ; j ++ ) ps . add ( P ) ; } Collections . sort ( ps ) ; double left = 0 , right = 1e13 ; double ans = right * 2 ; for ( int e = 0 ; e < 200 ; e ++ ) { double m = ( left + right ) / 2 ; if ( ok ( ps , D , m ) ) { right = m ; ans = min ( m , ans ) ; } else { left = m ; } } System . out . printf ( " Case ▁ # % d : ▁ % f \n " , o , ans ) ; } } boolean ok ( ArrayList < Integer > ps , int D , double len ) { double last = ps . get ( 0 ) - len ; for ( int i = 1 ; i < ps . size ( ) ; i ++ ) { double p = ps . get ( i ) ; if ( p - last + len < D + EPS ) return false ; if ( p - last - len > D - EPS ) { last = p - len ; } else { last += D ; } } return true ; } } class CL implements Comparable < CL > { int pos ; int num ; CL ( int p , int v ) { pos = p ; num = v ; } public int compareTo ( CL cl ) { return this . pos - cl . pos ; } }


import java . io . IOException ; import java . io . PrintWriter ; import java . util . Scanner ; public class B implements Runnable { public void solve ( ) throws IOException { int tn = in . nextInt ( ) ; for ( int test = 1 ; test <= tn ; test ++ ) { int n = in . nextInt ( ) ; int d = in . nextInt ( ) ; int [ ] v = new int [ n ] ; int [ ] x = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { x [ i ] = in . nextInt ( ) ; v [ i ] = in . nextInt ( ) ; } double l = 0.0 ; double r = 1E14 ; while ( r - l > 0.1 ) { double t = 0.5 * ( l + r ) ; double X = Double . NEGATIVE_INFINITY ; boolean ok = true ; for ( int i = 0 ; i < n ; i ++ ) { X = Math . max ( X + d , x [ i ] - t ) + ( v [ i ] - 1 ) * 1.0 * d ; if ( Math . abs ( X - x [ i ] ) > t ) { ok = false ; break ; } } if ( ok ) { r = t ; } else { l = t ; } } out . println ( " Case ▁ # " + test + " : ▁ " + Math . round ( l + r ) * 0.5 ) ; } } public Scanner in ; public PrintWriter out ; B ( ) throws IOException { in = new Scanner ( System . in ) ; out = new PrintWriter ( System . out ) ; } void close ( ) throws IOException { out . close ( ) ; } void check ( boolean f , String msg ) { if ( ! f ) { out . close ( ) ; throw new RuntimeException ( msg ) ; } } public void run ( ) { try { solve ( ) ; close ( ) ; } catch ( Exception e ) { e . printStackTrace ( out ) ; out . flush ( ) ; throw new RuntimeException ( e ) ; } } public static void main ( String [ ] args ) throws IOException { new Thread ( new B ( ) ) . start ( ) ; } }


import java . io . * ; import java . math . * ; import java . util . * ; public class Main { public static void main ( String [ ] args ) throws Exception { Scanner in = new Scanner ( new File ( " Main . in " ) ) ; PrintWriter out = new PrintWriter ( new File ( " Main . out " ) ) ; int tests = in . nextInt ( ) ; for ( int test = 1 ; test <= tests ; test ++ ) { out . print ( " Case ▁ # " + test + " : ▁ " ) ; int n = in . nextInt ( ) ; int k = in . nextInt ( ) ; int B = in . nextInt ( ) ; int T = in . nextInt ( ) ; int [ ] x = new int [ n ] ; int [ ] v = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) x [ i ] = in . nextInt ( ) ; for ( int i = 0 ; i < n ; i ++ ) v [ i ] = in . nextInt ( ) ; boolean [ ] can = new boolean [ n ] ; int z = 0 ; for ( int i = 0 ; i < n ; i ++ ) { can [ i ] = x [ i ] + v [ i ] * T >= B ; if ( can [ i ] ) z ++ ; } if ( z < k ) { out . println ( " IMPOSSIBLE " ) ; continue ; } int ans = 0 ; z = 0 ; for ( int i = n - 1 ; i >= 0 && k > 0 ; i -- ) if ( can [ i ] ) { ans += z ; k -- ; } else z ++ ; out . println ( ans ) ; } out . close ( ) ; } }

import sys NEW_LINE C = int ( sys . stdin . readline ( ) ) NEW_LINE for c in xrange ( C ) : NEW_LINE INDENT N , K , B , T = [ int ( x ) for x in sys . stdin . readline ( ) . split ( ) ] NEW_LINE X = [ int ( x ) for x in sys . stdin . readline ( ) . split ( ) ] NEW_LINE V = [ int ( x ) for x in sys . stdin . readline ( ) . split ( ) ] NEW_LINE if K == 0 : NEW_LINE INDENT print " Case ▁ # % d : ▁ 0" % ( c + 1 ) NEW_LINE continue NEW_LINE DEDENT good = [ ] NEW_LINE for i in xrange ( N ) : NEW_LINE INDENT if V [ i ] * T >= B - X [ i ] : NEW_LINE INDENT good . append ( i ) NEW_LINE DEDENT DEDENT if len ( good ) < K : NEW_LINE INDENT print " Case ▁ # % d : ▁ IMPOSSIBLE " % ( c + 1 ) NEW_LINE continue NEW_LINE DEDENT good = good [ len ( good ) - K : ] NEW_LINE G = 0 NEW_LINE for g in xrange ( 1 , len ( good ) ) : NEW_LINE INDENT G += g * ( good [ g ] - good [ g - 1 ] - 1 ) NEW_LINE DEDENT G += ( N - good [ - 1 ] - 1 ) * len ( good ) NEW_LINE print " Case ▁ # % d : ▁ % d " % ( c + 1 , G ) NEW_LINE DEDENT


import sys NEW_LINE def line ( ) : NEW_LINE INDENT return sys . stdin . readline ( ) [ : - 1 ] NEW_LINE DEDENT def readList ( ) : NEW_LINE INDENT return map ( eval , line ( ) . split ( ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT numberOfCases = eval ( line ( ) ) NEW_LINE for caseNumber in range ( numberOfCases ) : NEW_LINE INDENT N , K , B , T = readList ( ) NEW_LINE X = readList ( ) NEW_LINE V = readList ( ) NEW_LINE makesIt = [ ( B - X [ i ] + V [ i ] - 1 ) / V [ i ] <= T for i in range ( N ) ] NEW_LINE alreadyThere = 0 NEW_LINE while len ( makesIt ) > 0 and makesIt [ - 1 ] : NEW_LINE INDENT alreadyThere += 1 NEW_LINE makesIt . pop ( - 1 ) NEW_LINE DEDENT needed = 0 NEW_LINE i = len ( makesIt ) - 1 NEW_LINE while i >= 0 and alreadyThere < K : NEW_LINE INDENT if makesIt [ i ] : NEW_LINE INDENT needed += len ( makesIt ) - i - 1 NEW_LINE makesIt . pop ( i ) NEW_LINE alreadyThere += 1 NEW_LINE DEDENT else : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT DEDENT print " Case ▁ # " + str ( caseNumber + 1 ) + " : ▁ " + ( str ( needed ) . replace ( ' L ' , ' ' ) if alreadyThere >= K else ' IMPOSSIBLE ' ) NEW_LINE DEDENT DEDENT


def B ( inpfile ) : NEW_LINE INDENT fin = open ( inpfile , ' r ' ) NEW_LINE fout = open ( inpfile + ' . out ' , ' w ' ) NEW_LINE CNT = int ( fin . readline ( ) ) NEW_LINE for iCNT in range ( CNT ) : NEW_LINE INDENT numbers = map ( int , fin . readline ( ) . rstrip ( ' \n ' ) . split ( ' ▁ ' ) ) NEW_LINE N = numbers [ 0 ] NEW_LINE K = numbers [ 1 ] NEW_LINE B = numbers [ 2 ] NEW_LINE T = numbers [ 3 ] NEW_LINE X = map ( int , fin . readline ( ) . rstrip ( ' \n ' ) . split ( ' ▁ ' ) ) NEW_LINE V = map ( int , fin . readline ( ) . rstrip ( ' \n ' ) . split ( ' ▁ ' ) ) NEW_LINE X . reverse ( ) NEW_LINE V . reverse ( ) NEW_LINE OnTime = 0 NEW_LINE Switches = 0 NEW_LINE Obstacles = 0 NEW_LINE for iN in xrange ( N ) : NEW_LINE INDENT if B - X [ iN ] <= V [ iN ] * T : NEW_LINE INDENT OnTime += 1 NEW_LINE Switches += Obstacles NEW_LINE DEDENT else : NEW_LINE INDENT Obstacles += 1 NEW_LINE DEDENT if OnTime == K : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( OnTime < K ) : NEW_LINE INDENT result = ' IMPOSSIBLE ' NEW_LINE DEDENT else : NEW_LINE INDENT result = str ( Switches ) NEW_LINE DEDENT text = ' Case ▁ # ' + str ( iCNT + 1 ) + ' : ▁ ' + result NEW_LINE print text NEW_LINE fout . write ( text + ' \n ' ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT B ( ' . . \\test\\B - large . in ' ) ; NEW_LINE DEDENT


import fileinput NEW_LINE def main ( ) : NEW_LINE INDENT reader = fileinput . input ( ) NEW_LINE trials = int ( reader . next ( ) ) NEW_LINE for t in range ( 1 , trials + 1 ) : NEW_LINE INDENT result = trial ( reader ) NEW_LINE print " Case ▁ # % d : ▁ % s " % ( t , result ) NEW_LINE DEDENT DEDENT def ints ( reader ) : NEW_LINE INDENT return [ int ( k ) for k in reader . next ( ) . strip ( ) . split ( ) ] NEW_LINE DEDENT def trial ( reader ) : NEW_LINE INDENT n , k , b , t = ints ( reader ) NEW_LINE x = ints ( reader ) NEW_LINE v = ints ( reader ) NEW_LINE times = [ 0 ] * len ( x ) NEW_LINE for i in range ( len ( x ) ) : NEW_LINE INDENT times [ i ] = ( b - x [ i ] ) / float ( v [ i ] ) - t NEW_LINE DEDENT ok , n = swaps ( times , k ) NEW_LINE return n if ok else ' IMPOSSIBLE ' NEW_LINE DEDENT def swaps ( times , k ) : NEW_LINE INDENT if k == 0 : return True , 0 NEW_LINE i = lastfinisher ( times ) NEW_LINE if i < 0 : return False , 0 NEW_LINE cut = times [ : i ] + times [ i + 1 : ] NEW_LINE ok , n = swaps ( cut , k - 1 ) NEW_LINE return ok , n + len ( times ) - i - 1 NEW_LINE DEDENT def lastfinisher ( times ) : NEW_LINE INDENT for i in reversed ( range ( len ( times ) ) ) : NEW_LINE INDENT if times [ i ] <= 0 : return i NEW_LINE DEDENT return - 1 NEW_LINE DEDENT main ( ) NEW_LINE


tn = int ( raw_input ( ) ) NEW_LINE def solve ( a , k ) : NEW_LINE INDENT cnt = 0 NEW_LINE for x in reversed ( a ) : NEW_LINE INDENT if x == 1 : NEW_LINE INDENT k -= 1 NEW_LINE if k == 0 : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT cnt += k NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT for loop in xrange ( tn ) : NEW_LINE INDENT print ' Case ▁ # % d : ' % ( loop + 1 ) , NEW_LINE N , K , B , T = [ int ( x ) for x in raw_input ( ) . split ( ) ] NEW_LINE X = [ int ( x ) for x in raw_input ( ) . split ( ) ] NEW_LINE V = [ int ( x ) for x in raw_input ( ) . split ( ) ] NEW_LINE C = [ ] NEW_LINE cnt = 0 NEW_LINE for i in xrange ( N ) : NEW_LINE INDENT if X [ i ] + V [ i ] * T >= B : NEW_LINE INDENT cnt += 1 NEW_LINE C . append ( 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT C . append ( 0 ) NEW_LINE DEDENT DEDENT if cnt >= K : NEW_LINE INDENT print solve ( C , K ) NEW_LINE DEDENT else : NEW_LINE INDENT print ' IMPOSSIBLE ' NEW_LINE DEDENT DEDENT