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

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

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


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 java . io . File ; import java . io . FileNotFoundException ; import java . io . IOException ; import java . io . PrintStream ; import java . util . Scanner ; public class B extends AbstractCodeJamSolver { int n , k , b , t ; int [ ] x = new int [ 50 ] ; int [ ] v = new int [ 50 ] ; void input ( ) { n = in . nextInt ( ) ; k = in . nextInt ( ) ; b = in . nextInt ( ) ; t = in . nextInt ( ) ; for ( int i = 0 ; i < n ; i ++ ) x [ i ] = in . nextInt ( ) ; for ( int i = 0 ; i < n ; i ++ ) v [ i ] = in . nextInt ( ) ; } public void solve ( ) { int casen = in . nextInt ( ) ; for ( int casei = 1 ; casei <= casen ; casei ++ ) { input ( ) ; int pass = 0 ; int nopass = 0 ; int swap = 0 ; for ( int i = n - 1 ; i >= 0 && pass < k ; i -- ) { if ( v [ i ] * t >= b - x [ i ] ) { pass ++ ; swap += nopass ; } else { nopass ++ ; } } out . print ( " Case ▁ # " + casei + " : ▁ " ) ; if ( pass >= k ) { out . println ( swap ) ; } else { out . println ( " IMPOSSIBLE " ) ; } } } static public void main ( String [ ] args ) throws IOException { new B ( ) . launch ( " B . txt " ) ; } } abstract class AbstractCodeJamSolver { protected Scanner in ; protected PrintStream out = System . out ; abstract public void solve ( ) ; protected void print ( Object o ) { out . print ( o ) ; } protected void println ( ) { out . println ( ) ; } protected void println ( Object o ) { out . println ( o ) ; } protected void printf ( String f , Object ... o ) { out . format ( f , o ) ; } final public void launch ( String inputFileName ) throws FileNotFoundException { in = new Scanner ( new File ( inputFileName ) ) ; solve ( ) ; } }

import java . util . * ; import java . io . * ; public class R1B { public static void main ( String args [ ] ) throws Exception { Scanner in = new Scanner ( new File ( " B - large . in " ) ) ; PrintStream out = new PrintStream ( new File ( " B - large . out " ) ) ; int cas = in . nextInt ( ) ; for ( int iii = 0 ; iii < cas ; iii ++ ) { int N = in . nextInt ( ) ; int K = in . nextInt ( ) ; int B = in . nextInt ( ) ; int T = in . nextInt ( ) ; int pos [ ] = new int [ N ] ; int speed [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) pos [ i ] = B - in . nextInt ( ) ; for ( int i = 0 ; i < N ; i ++ ) speed [ i ] = in . nextInt ( ) ; for ( int i = 0 ; i < N ; i ++ ) pos [ i ] = ( ( pos [ i ] - 1 ) / speed [ i ] ) + 1 ; int count = 0 ; int seen = 0 ; int done = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( pos [ i ] > T ) { seen ++ ; } else { count += seen ; done ++ ; if ( done == K ) break ; } } out . println ( " Case ▁ # " + ( iii + 1 ) + " : ▁ " + ( done >= K ? " " + count : " IMPOSSIBLE " ) ) ; } out . close ( ) ; } }

import java . util . * ; import java . io . * ; import java . math . * ; public class Solution { public void doMain ( ) throws Exception { Scanner sc = new Scanner ( new FileReader ( " input . txt " ) ) ; PrintWriter pw = new PrintWriter ( new FileWriter ( " output . txt " ) ) ; int caseCnt = sc . nextInt ( ) ; long time = System . currentTimeMillis ( ) ; for ( int caseNum = 1 ; caseNum <= caseCnt ; caseNum ++ ) { pw . print ( " Case ▁ # " + caseNum + " : ▁ " ) ; int N = sc . nextInt ( ) , K = sc . nextInt ( ) ; long B = sc . nextLong ( ) , T = sc . nextInt ( ) ; long [ ] X = new long [ N ] ; long [ ] V = new long [ N ] ; boolean [ ] can = new boolean [ N ] ; for ( int i = 0 ; i < N ; i ++ ) X [ i ] = sc . nextLong ( ) ; for ( int i = 0 ; i < N ; i ++ ) { V [ i ] = sc . nextLong ( ) ; can [ i ] = X [ i ] + T * V [ i ] >= B ; } int good = 0 ; int swaps = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) if ( can [ i ] && good < K ) { good ++ ; for ( int j = i + 1 ; j < N ; j ++ ) if ( ! can [ j ] ) swaps ++ ; } if ( good < K ) pw . println ( " IMPOSSIBLE " ) ; else pw . println ( swaps ) ; 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 ( ) ; } }

package round1 ; import java . io . File ; import java . io . FileNotFoundException ; import java . io . PrintWriter ; import java . util . Scanner ; public class B { public static void main ( String [ ] args ) throws FileNotFoundException { Scanner in = new Scanner ( new File ( B . class . getSimpleName ( ) + " . in " ) ) ; PrintWriter out = new PrintWriter ( new File ( B . class . getSimpleName ( ) + " . out " ) ) ; int T = in . nextInt ( ) ; for ( int i = 0 ; i < T ; i ++ ) { out . println ( " Case ▁ # " + ( i + 1 ) + " : ▁ " + new B ( ) . solve ( in ) ) ; } out . close ( ) ; } private String solve ( Scanner in ) { 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 [ ] z = new boolean [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { z [ i ] = x [ i ] + v [ i ] * t >= b ; } int [ ] q = new int [ n ] ; int m = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( z [ i ] ) { q [ m ++ ] = i ; } } if ( m < k ) return " IMPOSSIBLE " ; int s = 0 ; for ( int i = 0 ; i < k ; i ++ ) { int j = n - i - 1 ; s += ( j - q [ i ] ) ; } return " " + s ; } }

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


import java . util . Random ; import java . util . Scanner ; public class C { static Scanner sc = new Scanner ( System . in ) ; static class Solver { int N ; int [ ] x ; void solve ( ) { N = sc . nextInt ( ) ; x = new int [ N - 1 ] ; for ( int i = 0 ; i < N - 1 ; ++ i ) { x [ i ] = sc . nextInt ( ) - 1 ; } for ( int i = 0 ; i < N - 1 ; ++ i ) { for ( int j = 0 ; j < i ; ++ j ) { if ( x [ j ] > i && x [ j ] < x [ i ] ) { System . out . println ( " Impossible " ) ; return ; } } } int [ ] h = new int [ N ] ; Random rand = new Random ( ) ; while ( true ) { for ( int i = 0 ; i < N ; ++ i ) { h [ i ] = rand . nextInt ( 100000000 + 1 ) ; } boolean success = true ; for ( int i = 0 ; i < N - 1 ; ++ i ) { double slopeMax = Integer . MIN_VALUE ; double mi = 0 ; for ( int j = i + 1 ; j < N ; ++ j ) { double slope = 1.0 * ( h [ j ] - h [ i ] ) / ( j - i ) ; if ( slope > slopeMax ) { slopeMax = slope ; mi = j ; } } if ( mi != x [ i ] ) { success = false ; break ; } } if ( success ) { for ( int i = 0 ; i < N ; ++ i ) { System . out . print ( h [ i ] ) ; if ( i != N - 1 ) { System . out . print ( " ▁ " ) ; } } System . out . println ( ) ; return ; } } } } public static void main ( String [ ] args ) { int T = sc . nextInt ( ) ; for ( int i = 1 ; i <= T ; ++ i ) { System . out . print ( " Case ▁ # " + i + " : ▁ " ) ; Solver solver = new Solver ( ) ; solver . solve ( ) ; } } }

import java . util . * ; import java . awt . * ; import java . math . * ; public class C { public static void main ( String args [ ] ) { Scanner scan = new Scanner ( System . in ) ; int T = scan . nextInt ( ) ; for ( int ca = 1 ; ca <= T ; ca ++ ) { boolean rtn = false ; int n = scan . nextInt ( ) ; int [ ] a = new int [ n - 1 ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) a [ i ] = scan . nextInt ( ) - 1 ; int TRIALS = 10000000 ; int [ ] h = new int [ n ] ; while ( TRIALS -- > 0 ) { boolean good = true ; for ( int i = 0 ; i < n - 1 ; i ++ ) { double m = - 999999999 ; int max = - 1 ; for ( int j = i + 1 ; j < n ; j ++ ) { double tm = ( double ) ( h [ j ] - h [ i ] ) / ( j - i ) ; if ( tm > m ) { m = tm ; max = j ; } } if ( max != a [ i ] ) { good = false ; h [ a [ i ] ] ++ ; break ; } } if ( good ) break ; } System . out . print ( " Case ▁ # " + ca + " : " ) ; if ( TRIALS <= 0 ) System . out . println ( " ▁ Impossible " ) ; else { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( " ▁ " + h [ i ] ) ; System . out . println ( ) ; } } } }

import math NEW_LINE import sys NEW_LINE sys . setrecursionlimit ( 3000 ) NEW_LINE testcases = int ( raw_input ( ) ) NEW_LINE n = None NEW_LINE h = None NEW_LINE output = None NEW_LINE def recurse ( a , b , i , incl ) : NEW_LINE INDENT if a == b : NEW_LINE INDENT return True NEW_LINE DEDENT cont = 1 NEW_LINE j = a NEW_LINE for k in range ( a , b ) : NEW_LINE INDENT if h [ k ] <= k or h [ k ] > b : NEW_LINE INDENT return False NEW_LINE DEDENT if h [ k ] == b : NEW_LINE INDENT output [ k ] = int ( output [ b ] - math . ceil ( incl * ( b - k ) ) - cont ) NEW_LINE cont += 1 NEW_LINE if not recurse ( j , k , i + 1 , float ( output [ b ] - output [ k ] ) / ( b - k ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT j = k + 1 NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT for tc in range ( 1 , testcases + 1 ) : NEW_LINE INDENT n = int ( raw_input ( ) ) NEW_LINE h = map ( lambda a : int ( a ) - 1 , raw_input ( ) . split ( ) ) NEW_LINE output = [ 0 ] * n NEW_LINE output [ n - 1 ] = 1000000000 NEW_LINE if recurse ( 0 , n - 1 , 1 , 0 ) : NEW_LINE INDENT mini = min ( output ) NEW_LINE output = map ( lambda a : a - mini , output ) NEW_LINE print " Case ▁ # % d : ▁ % s " % ( tc , ' ▁ ' . join ( map ( str , output ) ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print " Case ▁ # % d : ▁ Impossible " % tc NEW_LINE DEDENT DEDENT

testCount = int ( raw_input ( ) ) NEW_LINE for testIndex in range ( testCount ) : NEW_LINE INDENT ans = " Case ▁ # " + str ( testIndex + 1 ) + " : ▁ " NEW_LINE n = int ( raw_input ( ) ) NEW_LINE see = [ int ( x ) - 1 for x in raw_input ( ) . split ( " ▁ " ) ] NEW_LINE m = [ n - 1 ] * n NEW_LINE cm = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if i >= m [ cm ] : NEW_LINE INDENT cm -= 1 NEW_LINE DEDENT if see [ i ] > m [ cm ] : NEW_LINE INDENT ans += " Impossible " NEW_LINE break NEW_LINE DEDENT if see [ i ] < m [ cm ] : NEW_LINE INDENT cm += 1 NEW_LINE m [ cm ] = see [ i ] NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT height = [ 0 ] * n NEW_LINE count = [ - 1 ] * n NEW_LINE for i in see : NEW_LINE INDENT count [ i ] += 1 NEW_LINE DEDENT angle = [ 0 ] * n NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT angle [ i ] = count [ see [ i ] ] + angle [ see [ i ] ] NEW_LINE count [ see [ i ] ] -= 1 NEW_LINE height [ i ] = ( see [ i ] - i ) * angle [ i ] + height [ see [ i ] ] NEW_LINE DEDENT m = max ( height ) NEW_LINE for i in height : NEW_LINE INDENT ans += str ( m - i ) + " ▁ " NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT

import random NEW_LINE def check ( heights , hf ) : NEW_LINE INDENT for i in range ( len ( hf ) ) : NEW_LINE INDENT for j in range ( i + 1 , len ( hf ) + 1 ) : NEW_LINE INDENT akt = hf [ i ] - 1 NEW_LINE if j != akt : NEW_LINE INDENT x1 = j - i NEW_LINE y1 = heights [ j ] - heights [ i ] NEW_LINE x2 = akt - i NEW_LINE y2 = heights [ akt ] - heights [ i ] NEW_LINE if x1 * y2 - x2 * y1 <= 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT DEDENT return True NEW_LINE DEDENT def generate ( n ) : NEW_LINE INDENT return [ random . randint ( 0 , 400 ) for i in range ( n ) ] NEW_LINE DEDENT t = int ( raw_input ( ) ) NEW_LINE for tt in range ( 1 , t + 1 ) : NEW_LINE INDENT n = int ( raw_input ( ) ) NEW_LINE line = raw_input ( ) NEW_LINE highest_from = [ int ( i ) for i in line . strip ( ) . split ( ) ] NEW_LINE w = False NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , highest_from [ i ] - 1 ) : NEW_LINE INDENT if highest_from [ j ] > highest_from [ i ] : NEW_LINE INDENT w = True NEW_LINE DEDENT DEDENT DEDENT if w : NEW_LINE INDENT print " Case ▁ # { } : ▁ Impossible " . format ( tt ) NEW_LINE DEDENT else : NEW_LINE INDENT w = False NEW_LINE heights = [ ] NEW_LINE while not w : NEW_LINE INDENT heights = generate ( n ) NEW_LINE w = check ( heights , highest_from ) NEW_LINE DEDENT res = " Case ▁ # { } : ▁ " . format ( tt ) NEW_LINE for i in heights : NEW_LINE INDENT res += str ( i ) + " ▁ " NEW_LINE DEDENT print res NEW_LINE DEDENT DEDENT

import sys NEW_LINE import random NEW_LINE sys . setrecursionlimit ( 3000 ) NEW_LINE def bar ( a , nx , x ) : NEW_LINE INDENT return [ y - nx for y in a [ nx : x ] ] NEW_LINE DEDENT def foo2 ( a , level , height ) : NEW_LINE INDENT picks = [ 0 ] NEW_LINE i = 0 NEW_LINE n = len ( a ) NEW_LINE while True : NEW_LINE INDENT if a [ i ] == n : NEW_LINE INDENT break NEW_LINE DEDENT if a [ i ] > n : NEW_LINE INDENT return None NEW_LINE DEDENT picks . append ( a [ i ] ) NEW_LINE i = a [ i ] NEW_LINE DEDENT picks . append ( n ) NEW_LINE res = [ ] NEW_LINE nx = 0 NEW_LINE for x in picks : NEW_LINE INDENT if x == nx : NEW_LINE INDENT res . append ( height - level * ( n - x ) ) NEW_LINE DEDENT else : NEW_LINE INDENT t = foo2 ( bar ( a , nx , x ) , level + 1 , height - level * ( n - x ) ) NEW_LINE if t is None : NEW_LINE INDENT return None NEW_LINE DEDENT res += t NEW_LINE DEDENT nx = x + 1 NEW_LINE DEDENT return res NEW_LINE DEDENT def foo ( ifile ) : NEW_LINE INDENT n = int ( ifile . readline ( ) ) NEW_LINE a = [ int ( x ) - 1 for x in ifile . readline ( ) . split ( ) ] NEW_LINE level = 0 NEW_LINE height = 1000000 NEW_LINE res = foo2 ( a , level , height ) NEW_LINE if res is None : NEW_LINE INDENT return " Impossible " NEW_LINE DEDENT else : NEW_LINE INDENT return " ▁ " . join ( [ str ( x ) for x in res ] ) NEW_LINE DEDENT DEDENT def main ( ) : NEW_LINE INDENT ifile = sys . stdin NEW_LINE n = int ( ifile . readline ( ) ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ' Case ▁ # % d : ▁ % s ' % ( i + 1 , foo ( ifile ) ) NEW_LINE sys . stdout . flush ( ) NEW_LINE DEDENT DEDENT main ( ) NEW_LINE

from sys import stdin NEW_LINE import numpy as np NEW_LINE ntest = input ( ) NEW_LINE for test in range ( ntest ) : NEW_LINE INDENT n = input ( ) NEW_LINE x = np . array ( [ int ( i ) - 1 for i in stdin . readline ( ) . strip ( ) . split ( ) ] ) NEW_LINE y = np . zeros ( n , np . int ) NEW_LINE fixed = np . zeros ( n , np . bool ) NEW_LINE possible = True NEW_LINE for i in xrange ( n - 1 ) : NEW_LINE INDENT fixed [ x [ i ] ] = True NEW_LINE if True in fixed [ i + 1 : x [ i ] ] : NEW_LINE INDENT possible = False NEW_LINE break NEW_LINE DEDENT for j in xrange ( i + 1 , x [ i ] ) : NEW_LINE INDENT y [ j ] = y [ j ] - x [ i ] + j NEW_LINE DEDENT DEDENT y = y - y . min ( ) NEW_LINE result = ' ▁ ' . join ( [ ' % d ' % i for i in y ] ) if possible else ' Impossible ' NEW_LINE print " Case ▁ # % d : ▁ % s " % ( test + 1 , result ) NEW_LINE DEDENT


package cj2015 . qr ; import java . io . * ; import java . util . * ; public class D { Scanner sc ; PrintWriter pw ; int X , R , C ; public static void main ( String [ ] args ) throws Exception { String filePrefix = args . length > 0 ? args [ 0 ] : " D - large " ; try { new D ( ) . run ( filePrefix ) ; } catch ( Exception e ) { System . err . println ( e ) ; } } public void run ( String filePrefix ) throws Exception { sc = new Scanner ( new FileReader ( filePrefix + " . in " ) ) ; pw = new PrintWriter ( new FileWriter ( filePrefix + " . out " ) ) ; int ntest = sc . nextInt ( ) ; for ( int test = 1 ; test <= ntest ; test ++ ) { read ( sc ) ; pw . print ( " Case ▁ # " + test + " : ▁ " ) ; System . out . print ( " Case ▁ # " + test + " : ▁ " ) ; solve ( ) ; } System . out . println ( " Finished . " ) ; sc . close ( ) ; pw . close ( ) ; } void read ( Scanner sc ) { X = sc . nextInt ( ) ; R = sc . nextInt ( ) ; C = sc . nextInt ( ) ; } void print ( Object s ) { pw . print ( s ) ; System . out . print ( s ) ; } void println ( Object s ) { pw . println ( s ) ; System . out . println ( s ) ; } public void solve ( ) { if ( canFill ( X , R , C ) ) println ( " GABRIEL " ) ; else println ( " RICHARD " ) ; } public boolean canFill ( int x , int r , int c ) { if ( r > c ) { int temp = r ; r = c ; c = temp ; } if ( x >= 7 ) return false ; if ( ( r * c ) % x != 0 ) return false ; if ( x > c || ( x + 1 ) / 2 > r ) return false ; if ( x <= 3 ) return true ; if ( ( x + 1 ) / 2 == r ) if ( x == 4 || x == 6 || ( x == 5 && c == 5 ) ) return false ; return true ; } }

import java . io . BufferedWriter ; import java . io . File ; import java . io . FileWriter ; import java . io . IOException ; import java . util . Scanner ; public class D { static final String FILENAME = " D - large " ; static final String IN = FILENAME + " . in " ; static final String OUT = FILENAME + " . out " ; Scanner sc ; BufferedWriter out ; private void solve ( ) throws IOException { int x = sc . nextInt ( ) ; int r = sc . nextInt ( ) ; int c = sc . nextInt ( ) ; if ( x >= 7 || x > Math . max ( r , c ) || ( x + 1 ) / 2 > Math . min ( r , c ) || ( r * c ) % x != 0 || ( ( x + 1 ) / 2 == Math . min ( r , c ) && ( x == 4 || ( x == 5 && Math . max ( r , c ) < 6 ) || x == 6 ) ) ) { out . write ( " RICHARD " ) ; } else { out . write ( " GABRIEL " ) ; } out . newLine ( ) ; out . flush ( ) ; } private void run ( ) throws IOException { sc = new Scanner ( new File ( IN ) ) ; out = new BufferedWriter ( new FileWriter ( OUT ) ) ; int t = sc . nextInt ( ) ; for ( int i = 1 ; i <= t ; i ++ ) { out . write ( " Case ▁ # " + i + " : ▁ " ) ; solve ( ) ; } sc . close ( ) ; out . close ( ) ; } public static void main ( String args [ ] ) throws Exception { new D ( ) . run ( ) ; } }

import java . io . File ; import java . io . PrintStream ; import java . util . Scanner ; public class D_Large { static final Boolean SAMPLE = false ; static final String PROBLEM = " D " ; static final String INPUT = " large " ; static final String ID = "0" ; static final String PATH = " / Users / wangkai / Documents / codejam - commandline - 1.2 - beta1 / source / " ; public static void main ( String [ ] args ) throws Throwable { Scanner in = SAMPLE ? new Scanner ( System . in ) : new Scanner ( new File ( PATH + PROBLEM + " - " + INPUT + " - " + ID + " . in " ) ) ; PrintStream out = SAMPLE ? System . out : new PrintStream ( PATH + PROBLEM + " - " + INPUT + " - " + ID + " . out " ) ; int test = in . nextInt ( ) ; for ( int t = 1 ; t <= test ; t ++ ) { out . print ( " Case ▁ # " + t + " : ▁ " ) ; int omino = in . nextInt ( ) ; int row = in . nextInt ( ) ; int col = in . nextInt ( ) ; boolean firstPlayerWin ; if ( row * col % omino != 0 ) { firstPlayerWin = true ; } else { int minSide = Math . min ( row , col ) ; int maxSide = Math . max ( row , col ) ; if ( omino == 1 ) { firstPlayerWin = false ; } else if ( omino == 2 ) { firstPlayerWin = false ; } else if ( omino == 3 ) { if ( minSide == 1 ) { firstPlayerWin = true ; } else { firstPlayerWin = false ; } } else if ( omino == 4 ) { if ( minSide <= 2 ) { firstPlayerWin = true ; } else { firstPlayerWin = false ; } } else if ( omino == 5 ) { if ( minSide <= 2 ) { firstPlayerWin = true ; } else if ( minSide == 3 ) { if ( maxSide == 5 ) { firstPlayerWin = true ; } else { firstPlayerWin = false ; } } else { firstPlayerWin = false ; } } else if ( omino == 6 ) { if ( minSide <= 2 ) { firstPlayerWin = true ; } else if ( minSide == 3 ) { firstPlayerWin = true ; } else { firstPlayerWin = false ; } } else { firstPlayerWin = true ; } } out . println ( firstPlayerWin ? " RICHARD " : " GABRIEL " ) ; } out . close ( ) ; in . close ( ) ; System . out . println ( " finish ! " ) ; } }

package gcj2015 . qualif ; import java . io . FileNotFoundException ; import java . io . FileReader ; import java . io . PrintWriter ; import java . util . Scanner ; import java . util . logging . Level ; import java . util . logging . Logger ; public class ExoD { public static void main ( final String [ ] args ) { final String base = " / home / jean / gcj2015 / q / ExoD / " ; final String input = base + " b1 . in " ; final String output = base + " b1 . out " ; try { final Scanner sc = new Scanner ( new FileReader ( input ) ) ; final PrintWriter pw = new PrintWriter ( output ) ; final int n = sc . nextInt ( ) ; sc . nextLine ( ) ; for ( int c = 0 ; c < n ; c ++ ) { System . out . println ( " Test ▁ case ▁ " + ( c + 1 ) + " . . . " ) ; pw . print ( " Case ▁ # " + ( c + 1 ) + " : ▁ " ) ; test ( sc , pw ) ; pw . println ( ) ; } pw . println ( ) ; pw . flush ( ) ; pw . close ( ) ; sc . close ( ) ; } catch ( final FileNotFoundException ex ) { Logger . getLogger ( ExoD . class . getName ( ) ) . log ( Level . SEVERE , null , ex ) ; } } private static void test ( final Scanner sc , final PrintWriter pw ) { final int X = sc . nextInt ( ) ; final int R = sc . nextInt ( ) ; final int C = sc . nextInt ( ) ; if ( X >= 7 || ( R * C ) % X != 0 ) { pw . print ( " RICHARD " ) ; } else { if ( X == 1 || X == 2 || ( X == 3 && ( R != 1 && C != 1 ) ) || ( X == 4 && ( R >= 4 && C > 2 || C >= 4 && R > 2 ) ) || ( X == 5 && ( ( R >= 4 && C >= 4 ) || ( R == 3 && C >= 10 || R >= 10 && C == 3 ) ) ) || ( X == 6 && ( R >= 4 && C >= 4 && ( R >= 6 || C >= 6 ) ) ) ) { pw . print ( " GABRIEL " ) ; } else { pw . print ( " RICHARD " ) ; } } } }

import java . awt . geom . * ; import java . io . * ; import java . math . * ; import java . util . * ; import java . util . regex . * ; import static java . lang . Math . * ; import static java . lang . System . * ; public class D { boolean solve ( int caseNum ) { int x = in . nextInt ( ) ; int r = in . nextInt ( ) ; int c = in . nextInt ( ) ; int max = max ( r , c ) ; int min = min ( r , c ) ; if ( x >= 7 ) return false ; if ( r * c % x != 0 ) return false ; if ( x > max ) return false ; if ( x > min * 2 ) return false ; if ( x < min * 2 - 1 ) return true ; if ( x <= 2 ) return true ; if ( x == 3 ) { if ( min != 2 ) { debug ( " x ▁ = = ▁ 3 ▁ & & ▁ min ▁ ! = ▁ 2" ) ; } return true ; } if ( x == 4 ) { if ( min != 2 ) { debug ( " x ▁ = = ▁ 4 ▁ & & ▁ min ▁ ! = ▁ 2" ) ; } return false ; } if ( x == 5 ) { if ( min != 3 ) { debug ( " x ▁ = = ▁ 5 ▁ & & ▁ min ▁ ! = ▁ 3" ) ; } return max > 5 ; } if ( x == 6 ) { if ( min != 3 ) { debug ( " x ▁ = = ▁ 6 ▁ & & ▁ min ▁ ! = ▁ 3" ) ; } return false ; } debug ( " never ▁ reach " ) ; return true ; } Scanner in = new Scanner ( System . in ) ; public D ( ) throws Exception { int caseCount = in . nextInt ( ) ; for ( int caseNum = 1 ; caseNum <= caseCount ; caseNum ++ ) { out . printf ( " Case ▁ # % d : ▁ " , caseNum ) ; if ( solve ( caseNum ) ) { out . println ( " GABRIEL " ) ; } else { out . println ( " RICHARD " ) ; } } } public static void main ( String [ ] args ) throws Exception { new D ( ) ; } public static void debug ( Object ... arr ) { System . err . println ( Arrays . deepToString ( arr ) ) ; } }

def winner ( X , S , L ) : NEW_LINE INDENT if S * L % X : return ' RICHARD ' NEW_LINE if X <= 2 : return ' GABRIEL ' NEW_LINE if X >= 7 : return ' RICHARD ' NEW_LINE if L < X : return ' RICHARD ' NEW_LINE if 2 * S <= X : return ' RICHARD ' NEW_LINE if ( X , S , L ) == ( 5 , 3 , 5 ) : NEW_LINE INDENT return ' RICHARD ' NEW_LINE DEDENT return ' GABRIEL ' NEW_LINE DEDENT T = int ( input ( ) ) NEW_LINE for case in range ( 1 , T + 1 ) : NEW_LINE INDENT X , R , C = map ( int , input ( ) . split ( ) ) NEW_LINE print ( " Case ▁ # { } : ▁ { } " . format ( case , winner ( X , min ( R , C ) , max ( R , C ) ) ) ) NEW_LINE DEDENT

import sys NEW_LINE def load_cases ( path ) : NEW_LINE INDENT case_list = [ ] NEW_LINE with open ( path , ' r ' ) as fh : NEW_LINE INDENT case_num = int ( fh . readline ( ) . strip ( " \n " ) ) NEW_LINE for i in range ( 0 , case_num ) : NEW_LINE INDENT case = filter ( None , fh . readline ( ) . strip ( " \n " ) . split ( " ▁ " ) ) NEW_LINE case_list . append ( [ int ( item ) for item in case ] ) NEW_LINE DEDENT DEDENT return case_list NEW_LINE DEDENT def calculate_case ( case ) : NEW_LINE INDENT X = case [ 0 ] NEW_LINE R = case [ 1 ] NEW_LINE C = case [ 2 ] NEW_LINE if R > C : NEW_LINE INDENT R = case [ 2 ] NEW_LINE C = case [ 1 ] NEW_LINE DEDENT if ( R * C ) % X != 0 or X >= 7 : NEW_LINE INDENT return False NEW_LINE DEDENT elif X == 1 or X == 2 : NEW_LINE INDENT return True NEW_LINE DEDENT elif X == 3 : NEW_LINE INDENT if R == 1 : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT elif X == 4 : NEW_LINE INDENT if R < 3 : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT elif X == 5 : NEW_LINE INDENT if R < 3 : NEW_LINE INDENT return False NEW_LINE DEDENT elif R == 3 : NEW_LINE INDENT if C == 5 : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT elif X == 6 : NEW_LINE INDENT if R < 4 : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT if len ( sys . argv ) != 3 : NEW_LINE INDENT print ' Usage : ▁ q2 . py ▁ input _ file ▁ output _ file ' NEW_LINE sys . exit ( ) NEW_LINE DEDENT input_file = sys . argv [ 1 ] NEW_LINE output_file = sys . argv [ 2 ] NEW_LINE case_list = load_cases ( input_file ) NEW_LINE with open ( output_file , ' w ' ) as fh : NEW_LINE INDENT case_id = 1 NEW_LINE for case in case_list : NEW_LINE INDENT G_win = calculate_case ( case ) NEW_LINE fh . write ( ' Case ▁ # % d : ▁ % s \n ' % ( case_id , ' GABRIEL ' if G_win == True else ' RICHARD ' ) ) NEW_LINE case_id += 1 NEW_LINE DEDENT DEDENT DEDENT

def solve ( X , R , C ) : NEW_LINE INDENT if ( R * C ) % X > 0 : NEW_LINE INDENT return True NEW_LINE DEDENT big = max ( R , C ) NEW_LINE small = min ( R , C ) NEW_LINE if X >= 7 : NEW_LINE INDENT return True NEW_LINE DEDENT if X > big : NEW_LINE INDENT return True NEW_LINE DEDENT if X > 2 * small : NEW_LINE INDENT return True NEW_LINE DEDENT if big == small : NEW_LINE INDENT return False NEW_LINE DEDENT if big == small + 1 : NEW_LINE INDENT return False NEW_LINE DEDENT if big >= small + 2 : NEW_LINE INDENT if X < 2 * small - 1 : NEW_LINE INDENT return False NEW_LINE DEDENT if X == 2 * small - 1 : NEW_LINE INDENT if X == 1 : NEW_LINE INDENT return False NEW_LINE DEDENT if X == 3 : NEW_LINE INDENT return False NEW_LINE DEDENT if X == 5 : NEW_LINE INDENT return big <= 5 NEW_LINE DEDENT DEDENT if X == 2 * small : NEW_LINE INDENT if X == 2 : NEW_LINE INDENT return False NEW_LINE DEDENT if X == 4 : NEW_LINE INDENT return True NEW_LINE DEDENT if X == 6 : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT raise AssertionError ( ) NEW_LINE DEDENT T = input ( ) NEW_LINE for t in range ( 1 , T + 1 ) : NEW_LINE INDENT ( X , R , C ) = map ( int , raw_input ( ) . split ( ' ▁ ' ) ) NEW_LINE s = solve ( X , R , C ) NEW_LINE winner = " RICHARD " if s else " GABRIEL " NEW_LINE print " Case ▁ # % d : ▁ % s " % ( t , winner ) NEW_LINE DEDENT

__author__ = ' Reuben ' NEW_LINE def solution ( dom_size , width , height ) : NEW_LINE INDENT if dom_size >= 7 or width * height % dom_size != 0 or dom_size > max ( width , height ) : NEW_LINE INDENT return " RICHARD " NEW_LINE DEDENT if dom_size == 1 or dom_size == 2 : NEW_LINE INDENT return " GABRIEL " NEW_LINE DEDENT if dom_size == 3 : NEW_LINE INDENT if height == 1 or width == 1 : NEW_LINE INDENT return " RICHARD " NEW_LINE DEDENT else : NEW_LINE INDENT return " GABRIEL " NEW_LINE DEDENT DEDENT if dom_size == 4 : NEW_LINE INDENT if ( height <= 2 or width <= 2 ) : NEW_LINE INDENT return " RICHARD " NEW_LINE DEDENT else : NEW_LINE INDENT return " GABRIEL " NEW_LINE DEDENT DEDENT if dom_size == 5 : NEW_LINE INDENT if ( height <= 2 or width <= 2 ) or ( width == 3 and ( height < 10 or height % 5 != 0 ) ) or ( height == 3 and ( width < 10 or width % 5 != 0 ) ) : NEW_LINE INDENT return " RICHARD " NEW_LINE DEDENT else : NEW_LINE INDENT return " GABRIEL " NEW_LINE DEDENT DEDENT if dom_size == 6 : NEW_LINE INDENT if ( height <= 3 or width <= 3 ) : NEW_LINE INDENT return " RICHARD " NEW_LINE DEDENT else : NEW_LINE INDENT return " GABRIEL " NEW_LINE DEDENT DEDENT DEDENT f_in = open ( ' file . in ' ) NEW_LINE f_out = open ( ' file . out ' , ' w ' ) NEW_LINE cases = int ( f_in . readline ( ) ) NEW_LINE for i in range ( 1 , cases + 1 ) : NEW_LINE INDENT line = f_in . readline ( ) . split ( ) NEW_LINE dom_size = int ( line [ 0 ] ) NEW_LINE width = int ( line [ 1 ] ) NEW_LINE height = int ( line [ 2 ] ) NEW_LINE s = solution ( dom_size , width , height ) NEW_LINE f_out . write ( " Case ▁ # " + str ( i ) + " : ▁ " + s + " \n " ) NEW_LINE DEDENT

import sys NEW_LINE def solve ( x , r , c ) : NEW_LINE INDENT if ( r * c ) % x != 0 : NEW_LINE INDENT return True NEW_LINE DEDENT if ( r < x ) and ( c < x ) : NEW_LINE INDENT return True NEW_LINE DEDENT if min ( r , c ) * 2 + 1 <= x : NEW_LINE INDENT return True NEW_LINE DEDENT if x < 4 : NEW_LINE INDENT return False NEW_LINE DEDENT if x > 6 : NEW_LINE INDENT return True NEW_LINE DEDENT if x == 4 : NEW_LINE INDENT if min ( r , c ) <= 2 : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if x == 5 : NEW_LINE INDENT if ( min ( r , c ) == 3 ) and ( max ( r , c ) == 5 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if x == 6 : NEW_LINE INDENT if min ( r , c ) == 3 : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT t = int ( sys . stdin . readline ( ) . strip ( ) ) NEW_LINE for i in range ( t ) : NEW_LINE INDENT x , r , c = sys . stdin . readline ( ) . strip ( ) . split ( ) NEW_LINE x = int ( x ) NEW_LINE r = int ( r ) NEW_LINE c = int ( c ) NEW_LINE print " Case ▁ # % d : ▁ % s " % ( i + 1 , " RICHARD " if solve ( x , r , c ) else " GABRIEL " ) NEW_LINE DEDENT


import java . util . Scanner ; public class A { static Scanner sc = new Scanner ( System . in ) ; static char [ ] cs = { ' P ' , ' R ' , ' S ' } ; static String [ ] [ ] dp = new String [ 3 ] [ 13 ] ; public static void main ( String [ ] args ) { dp [ 0 ] [ 0 ] = " P " ; dp [ 1 ] [ 0 ] = " R " ; dp [ 2 ] [ 0 ] = " S " ; for ( int i = 1 ; i < dp [ 0 ] . length ; ++ i ) { for ( int j = 0 ; j < 3 ; ++ j ) { dp [ j ] [ i ] = dp [ j ] [ i - 1 ] + dp [ ( j + 1 ) % 3 ] [ i - 1 ] ; String o = dp [ ( j + 1 ) % 3 ] [ i - 1 ] + dp [ j ] [ i - 1 ] ; if ( o . compareTo ( dp [ j ] [ i ] ) < 0 ) dp [ j ] [ i ] = o ; } } int T = sc . nextInt ( ) ; for ( int i = 1 ; i <= T ; ++ i ) { System . out . print ( " Case ▁ # " + i + " : ▁ " ) ; System . out . println ( solve ( ) ) ; } } static String solve ( ) { int N = sc . nextInt ( ) ; int R = sc . nextInt ( ) ; int P = sc . nextInt ( ) ; int S = sc . nextInt ( ) ; String ret = null ; for ( int i = 0 ; i < 3 ; ++ i ) { int [ ] c = new int [ 3 ] ; for ( char ch : dp [ i ] [ N ] . toCharArray ( ) ) { if ( ch == ' P ' ) c [ 0 ] ++ ; if ( ch == ' R ' ) c [ 1 ] ++ ; if ( ch == ' S ' ) c [ 2 ] ++ ; } if ( c [ 0 ] == P && c [ 1 ] == R && c [ 2 ] == S ) { ret = dp [ i ] [ N ] ; } } return ret == null ? " IMPOSSIBLE " : ret ; } }

def solve ( n , r , p , s ) : NEW_LINE INDENT poss = [ ] NEW_LINE for i in " RPS " : NEW_LINE INDENT m = lex ( i , n ) NEW_LINE cr = cp = cs = 0 NEW_LINE for j in m : NEW_LINE INDENT if j == ' R ' : NEW_LINE INDENT cr += 1 NEW_LINE DEDENT if j == ' S ' : NEW_LINE INDENT cs += 1 NEW_LINE DEDENT if j == ' P ' : NEW_LINE INDENT cp += 1 NEW_LINE DEDENT DEDENT if ( cr == r and cs == s and cp == p ) : NEW_LINE INDENT poss . append ( m ) NEW_LINE DEDENT DEDENT if ( len ( poss ) == 0 ) : NEW_LINE INDENT return " IMPOSSIBLE " NEW_LINE DEDENT return min ( poss ) NEW_LINE DEDENT beats = { " R " : " S " , " S " : " P " , " P " : " R " } NEW_LINE def lex ( c , n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return c NEW_LINE DEDENT p1 = lex ( c , n - 1 ) NEW_LINE p2 = lex ( beats [ c ] , n - 1 ) NEW_LINE if p1 < p2 : NEW_LINE INDENT return p1 + p2 NEW_LINE DEDENT return p2 + p1 NEW_LINE DEDENT t = int ( raw_input ( ) ) NEW_LINE for cas in xrange ( 1 , t + 1 ) : NEW_LINE INDENT ans = solve ( * map ( int , raw_input ( ) . split ( ) ) ) NEW_LINE print " Case ▁ # { } : ▁ { } " . format ( cas , ans ) NEW_LINE DEDENT

def make ( i , n , cur ) : NEW_LINE INDENT if i == n : NEW_LINE INDENT return cur NEW_LINE DEDENT ret = ' ' NEW_LINE for c in cur : NEW_LINE INDENT a , b = None , None NEW_LINE if c == ' P ' : NEW_LINE INDENT a , b = ' P ' , ' R ' NEW_LINE DEDENT elif c == ' R ' : NEW_LINE INDENT a , b = ' R ' , ' S ' NEW_LINE DEDENT elif c == ' S ' : NEW_LINE INDENT a , b = ' P ' , ' S ' NEW_LINE DEDENT l1 = make ( i + 1 , n , a ) NEW_LINE l2 = make ( i + 1 , n , b ) NEW_LINE if l1 < l2 : NEW_LINE INDENT ret += l1 NEW_LINE ret += l2 NEW_LINE DEDENT else : NEW_LINE INDENT ret += l2 NEW_LINE ret += l1 NEW_LINE DEDENT DEDENT return ret NEW_LINE DEDENT T = int ( raw_input ( ) ) NEW_LINE for t in xrange ( T ) : NEW_LINE INDENT N , R , P , S = map ( int , raw_input ( ) . split ( ) ) NEW_LINE opt = ' IMPOSSIBLE ' NEW_LINE answers = [ make ( 0 , N , ' R ' ) , make ( 0 , N , ' P ' ) , make ( 0 , N , ' S ' ) , ] NEW_LINE for ans in answers : NEW_LINE INDENT if ans . count ( ' P ' ) == P and ans . count ( ' R ' ) == R and ans . count ( ' S ' ) == S : NEW_LINE INDENT opt = ans NEW_LINE DEDENT DEDENT print ' Case ▁ # % d : ▁ % s ' % ( t + 1 , opt ) NEW_LINE DEDENT

import sys NEW_LINE in_file = open ( ' a - large . in ' , ' r ' ) NEW_LINE out_file = open ( ' a . out ' , ' w ' ) NEW_LINE sys . stdin = in_file NEW_LINE sys . stdout = out_file NEW_LINE def calc ( winner ) : NEW_LINE INDENT if winner == ' R ' : NEW_LINE INDENT return ' S ' NEW_LINE DEDENT if winner == ' S ' : NEW_LINE INDENT return ' P ' NEW_LINE DEDENT if winner == ' P ' : NEW_LINE INDENT return ' R ' NEW_LINE DEDENT assert False NEW_LINE DEDENT def check ( n , winner ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return winner NEW_LINE DEDENT other = calc ( winner ) NEW_LINE l = check ( n - 1 , winner ) NEW_LINE r = check ( n - 1 , calc ( winner ) ) NEW_LINE if l < r : NEW_LINE INDENT return l + r NEW_LINE DEDENT else : NEW_LINE INDENT return r + l NEW_LINE DEDENT DEDENT T = int ( raw_input ( ) ) NEW_LINE for t in xrange ( T ) : NEW_LINE INDENT n , R , P , S = map ( int , raw_input ( ) . split ( ) ) NEW_LINE ans = None NEW_LINE for win in ( ' R ' , ' P ' , ' S ' ) : NEW_LINE INDENT best = check ( n , win ) NEW_LINE if best . count ( ' R ' ) != R or best . count ( ' P ' ) != P or best . count ( ' S ' ) != S : NEW_LINE INDENT continue NEW_LINE DEDENT if ans == None or best < ans : NEW_LINE INDENT ans = best NEW_LINE DEDENT DEDENT if ans == None : NEW_LINE INDENT ans = ' IMPOSSIBLE ' NEW_LINE DEDENT print ' Case ▁ # % d : ▁ % s ' % ( t + 1 , ans ) NEW_LINE DEDENT in_file . close ( ) NEW_LINE out_file . close ( ) NEW_LINE

def invalid ( n , r , p , s ) : NEW_LINE INDENT N = 1 << n NEW_LINE if max ( r , p , s ) > ( N + 2 ) / 3 : return True NEW_LINE if min ( r , p , s ) < N / 3 : return True NEW_LINE return False NEW_LINE DEDENT def build ( n , r , p , s ) : NEW_LINE INDENT assert not invalid ( n , r , p , s ) NEW_LINE if n == 1 : NEW_LINE INDENT if p > 0 and r > 0 : return ' PR ' NEW_LINE if p > 0 and s > 0 : return ' PS ' NEW_LINE return ' RS ' NEW_LINE DEDENT if p == r > s : NEW_LINE INDENT pl , rl , sl = ( p + 1 ) / 2 , r / 2 , s / 2 NEW_LINE DEDENT elif p == s > r : NEW_LINE INDENT pl , rl , sl = ( p + 1 ) / 2 , r / 2 , s / 2 NEW_LINE DEDENT elif p > r == s : NEW_LINE INDENT pl , rl , sl = p / 2 , ( r + 1 ) / 2 , s / 2 NEW_LINE DEDENT elif r == s > p : NEW_LINE INDENT pl , rl , sl = p / 2 , ( r + 1 ) / 2 , s / 2 NEW_LINE DEDENT elif r > s == p : NEW_LINE INDENT pl , rl , sl = ( p + 1 ) / 2 , r / 2 , s / 2 NEW_LINE DEDENT elif s > r == p : NEW_LINE INDENT pl , rl , sl = ( p + 1 ) / 2 , r / 2 , s / 2 NEW_LINE DEDENT else : NEW_LINE INDENT assert False NEW_LINE DEDENT return build ( n - 1 , rl , pl , sl ) + build ( n - 1 , r - rl , p - pl , s - sl ) NEW_LINE DEDENT _T = int ( raw_input ( ) ) NEW_LINE for _t in range ( 1 , _T + 1 ) : NEW_LINE INDENT n , R , P , S = map ( int , raw_input ( ) . split ( ) ) NEW_LINE if invalid ( n , R , P , S ) : NEW_LINE INDENT res = ' IMPOSSIBLE ' NEW_LINE DEDENT else : NEW_LINE INDENT res = build ( n , R , P , S ) NEW_LINE DEDENT print ' Case ▁ # { } : ▁ { } ' . format ( _t , res ) NEW_LINE DEDENT

players = { ' R ' : ' RS ' , ' S ' : ' SP ' , ' P ' : ' RP ' } NEW_LINE memo = { } NEW_LINE def gen_tourney ( n , winner ) : NEW_LINE INDENT if ( n , winner ) in memo : NEW_LINE INDENT return memo [ n , winner ] NEW_LINE DEDENT if n == 0 : NEW_LINE INDENT result = winner NEW_LINE DEDENT else : NEW_LINE INDENT t1 = gen_tourney ( n - 1 , players [ winner ] [ 0 ] ) NEW_LINE t2 = gen_tourney ( n - 1 , players [ winner ] [ 1 ] ) NEW_LINE if t1 < t2 : NEW_LINE INDENT result = t1 + t2 NEW_LINE DEDENT else : NEW_LINE INDENT result = t2 + t1 NEW_LINE DEDENT DEDENT memo [ n , winner ] = result NEW_LINE return result NEW_LINE DEDENT from collections import Counter NEW_LINE def solve ( n , r , p , s ) : NEW_LINE INDENT desired_hist = Counter ( { ' R ' : r , ' P ' : p , ' S ' : s } ) NEW_LINE for winner in ' RPS ' : NEW_LINE INDENT t = gen_tourney ( n , winner ) NEW_LINE hist = Counter ( t ) NEW_LINE if all ( hist [ c ] == desired_hist [ c ] for c in ' RPS ' ) : NEW_LINE INDENT return t NEW_LINE DEDENT DEDENT return ' IMPOSSIBLE ' NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT import sys NEW_LINE fp = open ( sys . argv [ 1 ] ) NEW_LINE def readline ( ) : NEW_LINE INDENT return fp . readline ( ) . strip ( ) NEW_LINE DEDENT num_cases = int ( readline ( ) ) NEW_LINE for i in xrange ( num_cases ) : NEW_LINE INDENT n , r , p , s = [ int ( x ) for x in readline ( ) . split ( ) ] NEW_LINE print " Case ▁ # % d : ▁ % s " % ( i + 1 , solve ( n , r , p , s ) ) NEW_LINE DEDENT DEDENT


package round2 ; import java . io . BufferedWriter ; import java . io . File ; import java . io . FileWriter ; import java . io . IOException ; import java . util . Scanner ; public class GCJProblemC { static String input = " C - small - attempt0 . in " , output = " c . out " ; public static void main ( String [ ] args ) throws IOException { Scanner sc = new Scanner ( new File ( input ) ) ; BufferedWriter ps = new BufferedWriter ( new FileWriter ( new File ( output ) ) ) ; int tc = sc . nextInt ( ) ; for ( int test = 1 ; test <= tc ; test ++ ) { int n = sc . nextInt ( ) ; int [ ] x1 = new int [ n ] , x2 = new int [ n ] , x3 = new int [ n ] , p = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { x1 [ i ] = sc . nextInt ( ) ; x2 [ i ] = sc . nextInt ( ) ; x3 [ i ] = sc . nextInt ( ) ; p [ i ] = sc . nextInt ( ) ; } double smin = 0.000000 , smax = 1000000 ; while ( smax - smin > 1e-8 ) { double mid = smax + smin ; mid /= 2 ; boolean can = true ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { int dist = Math . abs ( x1 [ i ] - x1 [ j ] ) + Math . abs ( x2 [ i ] - x2 [ j ] ) + Math . abs ( x3 [ i ] - x3 [ j ] ) ; if ( ! ( ( p [ i ] + p [ j ] ) * mid > dist ) ) can = false ; } if ( can ) smax = mid ; else smin = mid ; } String result = " " + smin ; ps . write ( " Case ▁ # " + test + " : ▁ " + result + " \n " ) ; } sc . close ( ) ; ps . close ( ) ; } }

import java . io . * ; import java . util . Locale ; import java . util . StringTokenizer ; public class C { public static void main ( String [ ] args ) throws Exception { BufferedReader in = new BufferedReader ( new FileReader ( " C - large . in " ) ) ; PrintWriter out = new PrintWriter ( " c . out " ) ; int T = Integer . parseInt ( in . readLine ( ) ) ; for ( int i = 1 ; i <= T ; i ++ ) { int n = Integer . parseInt ( in . readLine ( ) ) ; double [ ] x = new double [ n ] ; double [ ] y = new double [ n ] ; double [ ] z = new double [ n ] ; double [ ] p = new double [ n ] ; for ( int j = 0 ; j < n ; j ++ ) { StringTokenizer tz = new StringTokenizer ( in . readLine ( ) ) ; x [ j ] = Integer . parseInt ( tz . nextToken ( ) ) ; y [ j ] = Integer . parseInt ( tz . nextToken ( ) ) ; z [ j ] = Integer . parseInt ( tz . nextToken ( ) ) ; p [ j ] = Integer . parseInt ( tz . nextToken ( ) ) ; } double l = 0 ; double r = 1e8 ; while ( r > l + 1e-7 ) { double pp = ( l + r ) / 2 ; boolean can = true ; outer : for ( int j = 0 ; j < n ; j ++ ) { for ( int k = 0 ; k < n ; k ++ ) { double d = Math . abs ( x [ k ] - x [ j ] ) + Math . abs ( y [ k ] - y [ j ] ) + Math . abs ( z [ k ] - z [ j ] ) ; if ( d > pp * ( p [ k ] + p [ j ] ) ) { can = false ; break outer ; } } } if ( can ) { r = pp ; } else { l = pp ; } } double rr = ( l + r ) / 2 ; out . printf ( Locale . US , " Case ▁ # % d : ▁ % .6f \n " , i , rr ) ; } in . close ( ) ; out . close ( ) ; } }

import java . io . PrintWriter ; import java . io . File ; import java . io . FileNotFoundException ; import java . util . Scanner ; import java . util . Arrays ; public class C { static Scanner in ; static PrintWriter out ; public static void main ( String [ ] args ) throws FileNotFoundException { in = new Scanner ( new File ( " input . txt " ) ) ; out = new PrintWriter ( " output . txt " ) ; int n = in . nextInt ( ) ; for ( int i = 0 ; i < n ; i ++ ) { out . println ( " Case ▁ # " + ( i + 1 ) + " : ▁ " + new C ( ) . solve ( ) ) ; } out . close ( ) ; } private Object solve ( ) { int n = in . nextInt ( ) ; double [ ] [ ] c = new double [ 3 ] [ n ] ; double [ ] p = new double [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { c [ j ] [ i ] = in . nextInt ( ) ; } p [ i ] = in . nextInt ( ) ; } double l = 0 ; double r = 1e100 ; while ( r > l + 1e-9 ) { double m = ( l + r ) / 2 ; double [ ] q = new double [ 8 ] ; Arrays . fill ( q , 1e100 ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 8 ; j ++ ) { double qq = m * p [ i ] ; for ( int k = 0 ; k < 3 ; k ++ ) { if ( ( j & ( 1 << k ) ) > 0 ) { qq += c [ k ] [ i ] ; } else { qq -= c [ k ] [ i ] ; } } q [ j ] = Math . min ( q [ j ] , qq ) ; } } boolean b = true ; for ( int i = 0 ; i < 8 ; i ++ ) { if ( q [ i ] < - q [ i ^ 7 ] ) b = false ; } if ( b ) r = m ; else l = m ; } return " " + r ; } }

inFile = open ( " C - large . in " , " r " ) NEW_LINE outFile = open ( " C - large - out . out " , " w " ) NEW_LINE cases = int ( inFile . readline ( ) ) NEW_LINE for caseNum in range ( 1 , cases + 1 ) : NEW_LINE INDENT shipCount = int ( inFile . readline ( ) ) NEW_LINE shipList = [ ] NEW_LINE for i in range ( shipCount ) : NEW_LINE INDENT ship = inFile . readline ( ) . rstrip ( ) NEW_LINE ship = ship . split ( " ▁ " ) NEW_LINE ship = map ( float , ship ) NEW_LINE shipList . append ( ship ) NEW_LINE DEDENT maxPower = 0.0 NEW_LINE for i in range ( shipCount ) : NEW_LINE INDENT for j in range ( i + 1 , shipCount ) : NEW_LINE INDENT taxiDist = abs ( shipList [ i ] [ 0 ] - shipList [ j ] [ 0 ] ) + abs ( shipList [ i ] [ 1 ] - shipList [ j ] [ 1 ] ) + abs ( shipList [ i ] [ 2 ] - shipList [ j ] [ 2 ] ) NEW_LINE powerSum = shipList [ i ] [ 3 ] + shipList [ j ] [ 3 ] NEW_LINE power = taxiDist / powerSum NEW_LINE if power > maxPower : NEW_LINE INDENT maxPower = power NEW_LINE DEDENT DEDENT DEDENT outputString = " Case ▁ # " + str ( caseNum ) + " : ▁ " + str ( maxPower ) + " \n " NEW_LINE print outputString . rstrip ( ) NEW_LINE outFile . write ( outputString ) NEW_LINE DEDENT inFile . close ( ) NEW_LINE outFile . close ( ) NEW_LINE

from math import fabs NEW_LINE cases = int ( raw_input ( ) ) NEW_LINE for c in xrange ( 1 , cases + 1 ) : NEW_LINE INDENT ns = int ( raw_input ( ) ) NEW_LINE x = [ ] NEW_LINE y = [ ] NEW_LINE z = [ ] NEW_LINE p = [ ] NEW_LINE min_value = 0 NEW_LINE ship1 = - 1 NEW_LINE ship2 = - 1 NEW_LINE if ns == 1 : NEW_LINE INDENT print " Case ▁ # % d : ▁ 0.000000" % c NEW_LINE continue NEW_LINE DEDENT for i in xrange ( ns ) : NEW_LINE INDENT xs , ys , zs , ps = raw_input ( ) . split ( ) NEW_LINE xa = int ( xs ) NEW_LINE ya = int ( ys ) NEW_LINE za = int ( zs ) NEW_LINE pa = int ( ps ) NEW_LINE x . append ( int ( xs ) ) NEW_LINE y . append ( int ( ys ) ) NEW_LINE z . append ( int ( zs ) ) NEW_LINE p . append ( int ( ps ) ) NEW_LINE for j in xrange ( i + 1 ) : NEW_LINE INDENT xb = x [ j ] NEW_LINE yb = y [ j ] NEW_LINE zb = z [ j ] NEW_LINE pb = p [ j ] NEW_LINE value = ( fabs ( xb - xa ) + fabs ( yb - ya ) + fabs ( zb - za ) ) / float ( ( pa + pb ) ) NEW_LINE if value > min_value : NEW_LINE INDENT min_value = value NEW_LINE ship1 = i NEW_LINE ship2 = j NEW_LINE DEDENT DEDENT DEDENT totalpf = float ( p [ ship1 ] ) + float ( p [ ship2 ] ) NEW_LINE scale = float ( p [ ship1 ] ) / float ( totalpf ) NEW_LINE cruiserx = ( x [ ship2 ] - x [ ship1 ] ) * scale + x [ ship1 ] NEW_LINE cruisery = ( y [ ship2 ] - y [ ship1 ] ) * scale + y [ ship1 ] NEW_LINE cruiserz = ( z [ ship2 ] - z [ ship1 ] ) * scale + z [ ship1 ] NEW_LINE power = ( fabs ( x [ ship2 ] - cruiserx ) + fabs ( y [ ship2 ] - cruisery ) + fabs ( z [ ship2 ] - cruiserz ) ) / p [ ship2 ] NEW_LINE print " Case ▁ # % d : ▁ % 8.6f " % ( c , power ) NEW_LINE DEDENT


package cj2015 . qr ; import java . io . * ; import java . util . * ; public class B { Scanner sc ; PrintWriter pw ; int D ; int [ ] P ; public static void main ( String [ ] args ) throws Exception { String filePrefix = args . length > 0 ? args [ 0 ] : " B - large " ; try { new B ( ) . run ( filePrefix ) ; } catch ( Exception e ) { System . err . println ( e ) ; } } public void run ( String filePrefix ) throws Exception { sc = new Scanner ( new FileReader ( filePrefix + " . in " ) ) ; pw = new PrintWriter ( new FileWriter ( filePrefix + " . out " ) ) ; int ntest = sc . nextInt ( ) ; for ( int test = 1 ; test <= ntest ; test ++ ) { read ( sc ) ; pw . print ( " Case ▁ # " + test + " : ▁ " ) ; System . out . print ( " Case ▁ # " + test + " : ▁ " ) ; solve ( ) ; } System . out . println ( " Finished . " ) ; sc . close ( ) ; pw . close ( ) ; } void read ( Scanner sc ) { D = sc . nextInt ( ) ; P = new int [ D ] ; for ( int i = 0 ; i < D ; i ++ ) P [ i ] = sc . nextInt ( ) ; } void print ( Object s ) { pw . print ( s ) ; System . out . print ( s ) ; } void println ( Object s ) { pw . println ( s ) ; System . out . println ( s ) ; } public void solve ( ) { final int INF = 1000000 ; int min = INF ; for ( int x = 1 ; x <= 1000 ; x ++ ) { int time = 0 ; for ( int i = 0 ; i < D ; i ++ ) time += ( P [ i ] + x - 1 ) / x - 1 ; min = Math . min ( min , time + x ) ; } println ( min ) ; } }

import java . io . BufferedWriter ; import java . io . File ; import java . io . FileWriter ; import java . io . IOException ; import java . util . Scanner ; public class B { static final String FILENAME = " B - large " ; static final String IN = FILENAME + " . in " ; static final String OUT = FILENAME + " . out " ; Scanner sc ; BufferedWriter out ; static final int MAXP = 1000 ; private int divideTime ( int [ ] num , int limit ) { int time = 0 ; for ( int i = limit + 1 ; i <= MAXP ; i ++ ) { time += num [ i ] * ( ( i - 1 ) / limit ) ; } return time ; } private void solve ( ) throws IOException { int ans = Integer . MAX_VALUE ; int d = sc . nextInt ( ) ; int [ ] num = new int [ MAXP + 1 ] ; for ( int i = 0 ; i < d ; i ++ ) { num [ sc . nextInt ( ) ] ++ ; } for ( int limit = 1 ; limit <= MAXP ; limit ++ ) { int cur = divideTime ( num , limit ) + limit ; if ( ans > cur ) { ans = cur ; } } out . write ( Integer . toString ( ans ) ) ; out . newLine ( ) ; out . flush ( ) ; } private void run ( ) throws IOException { sc = new Scanner ( new File ( IN ) ) ; out = new BufferedWriter ( new FileWriter ( OUT ) ) ; int t = sc . nextInt ( ) ; for ( int i = 1 ; i <= t ; i ++ ) { out . write ( " Case ▁ # " + i + " : ▁ " ) ; solve ( ) ; } sc . close ( ) ; out . close ( ) ; } public static void main ( String args [ ] ) throws Exception { new B ( ) . run ( ) ; } }

import java . io . * ; import java . util . * ; import java . util . concurrent . * ; public class B { int n ; int [ ] a ; String solve ( ) { Arrays . sort ( a ) ; int m = a [ n - 1 ] ; int ans = m ; for ( int k = 1 ; k <= m ; k ++ ) { int divs = 0 ; for ( int x : a ) { divs += ( x - 1 ) / k ; } ans = Math . min ( ans , k + divs ) ; } return " " + ans ; } public B ( Scanner in ) { n = in . nextInt ( ) ; a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = in . nextInt ( ) ; } } private static String fileName = B . class . getSimpleName ( ) . replaceFirst ( " _ . * " , " " ) . toLowerCase ( ) ; private static String inputFileName = fileName + " . in " ; private static String outputFileName = fileName + " . out " ; public static void main ( String [ ] args ) throws IOException , InterruptedException , ExecutionException { ExecutorService executor = Executors . newFixedThreadPool ( 4 ) ; Locale . setDefault ( Locale . US ) ; Scanner in = new Scanner ( new File ( inputFileName ) ) ; PrintWriter out = new PrintWriter ( outputFileName ) ; int tests = in . nextInt ( ) ; in . nextLine ( ) ; @ SuppressWarnings ( " unchecked " ) Future < String > [ ] outputs = new Future [ tests ] ; for ( int t = 0 ; t < tests ; t ++ ) { final B testCase = new B ( in ) ; final int testCaseNumber = t ; outputs [ t ] = executor . submit ( new Callable < String > ( ) { @ Override public String call ( ) { String answer = testCase . solve ( ) ; String printed = " Case ▁ # " + ( testCaseNumber + 1 ) + " : ▁ " + answer ; System . out . println ( printed ) ; return printed ; } } ) ; } for ( int t = 0 ; t < tests ; t ++ ) { out . println ( outputs [ t ] . get ( ) ) ; } in . close ( ) ; out . close ( ) ; executor . shutdown ( ) ; } }

import java . util . * ; import java . io . * ; public class InfinitePancakes { static final String filename = " C : / Users / Kevin / algs4 / CodeJam / InfinitePancakes / B - large . in " ; static final String output = " largeoutput . txt " ; public static int howLong ( int [ ] cakecounts ) { if ( cakecounts . length < 4 ) return cakecounts . length - 1 ; int ret = Integer . MAX_VALUE ; for ( int maxsize = 2 ; maxsize < cakecounts . length ; ++ maxsize ) { int [ ] cakes = Arrays . copyOf ( cakecounts , cakecounts . length ) ; int numspec = 0 ; int maxstack = cakecounts . length - 1 ; while ( maxstack > maxsize ) { while ( cakes [ maxstack ] > 0 ) { cakes [ maxstack ] -- ; cakes [ maxstack - maxsize ] ++ ; cakes [ maxsize ] ++ ; numspec ++ ; } maxstack -- ; } ret = Math . min ( ret , maxsize + numspec ) ; } return ret ; } public static void main ( String [ ] args ) { try { Scanner sc = new Scanner ( new FileInputStream ( new File ( filename ) ) ) ; int no_of_times = sc . nextInt ( ) ; for ( int i = 0 ; i < no_of_times ; ++ i ) { int D = sc . nextInt ( ) ; int [ ] platenumbers = new int [ D ] ; int maxnumber = 0 ; for ( int j = 0 ; j < D ; ++ j ) { platenumbers [ j ] = sc . nextInt ( ) ; maxnumber = Math . max ( maxnumber , platenumbers [ j ] ) ; } int [ ] cakecounts = new int [ maxnumber + 1 ] ; for ( int j = 0 ; j < D ; ++ j ) { cakecounts [ platenumbers [ j ] ] ++ ; } FileOutputStream fos = new FileOutputStream ( output , true ) ; fos . write ( ( " Case ▁ # " + ( i + 1 ) + " : ▁ " + howLong ( cakecounts ) + " \n " ) . getBytes ( ) ) ; fos . close ( ) ; } sc . close ( ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } } }

package gcj2015 . qualif ; import java . io . FileNotFoundException ; import java . io . FileReader ; import java . io . PrintWriter ; import java . util . ArrayList ; import java . util . Collections ; import java . util . List ; import java . util . Scanner ; import java . util . logging . Level ; import java . util . logging . Logger ; public class ExoB { public static void main ( final String [ ] args ) { final String base = " / home / jean / gcj2015 / q / ExoB / " ; final String input = base + " b1 . in " ; final String output = base + " b2 . out " ; try { final Scanner sc = new Scanner ( new FileReader ( input ) ) ; final PrintWriter pw = new PrintWriter ( output ) ; final int n = sc . nextInt ( ) ; sc . nextLine ( ) ; for ( int c = 0 ; c < n ; c ++ ) { System . out . println ( " Test ▁ case ▁ " + ( c + 1 ) + " . . . " ) ; pw . print ( " Case ▁ # " + ( c + 1 ) + " : ▁ " ) ; test3 ( sc , pw ) ; pw . println ( ) ; } pw . println ( ) ; pw . flush ( ) ; pw . close ( ) ; sc . close ( ) ; } catch ( final FileNotFoundException ex ) { Logger . getLogger ( ExoB . class . getName ( ) ) . log ( Level . SEVERE , null , ex ) ; } } private static void test3 ( final Scanner sc , final PrintWriter pw ) { final int D = sc . nextInt ( ) ; List < Integer > nums = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < D ; i ++ ) { nums . add ( sc . nextInt ( ) ) ; } int max = Collections . max ( nums ) ; int bt = max ; for ( int m = 1 ; m <= max ; m ++ ) { System . out . println ( " for ▁ m ▁ = ▁ " + m ) ; int t = m ; for ( int k : nums ) { if ( k != 0 ) t += ( k % m == 0 ) ? ( k / m ) - 1 : k / m ; } System . out . println ( " t ▁ = ▁ " + t ) ; if ( t < bt ) { bt = t ; } } pw . print ( bt ) ; } }

MAXP = 1001 NEW_LINE def solve ( P ) : NEW_LINE INDENT best = MAXP NEW_LINE for level in range ( 1 , MAXP ) : NEW_LINE INDENT moves = 0 NEW_LINE for p in P : NEW_LINE INDENT moves += ( p - 1 ) / level NEW_LINE DEDENT if moves + level < best : NEW_LINE INDENT best = moves + level NEW_LINE DEDENT DEDENT return best NEW_LINE DEDENT T = input ( ) NEW_LINE for t in range ( 1 , T + 1 ) : NEW_LINE INDENT D = input ( ) NEW_LINE P = map ( int , raw_input ( ) . split ( ' ▁ ' ) ) NEW_LINE s = solve ( P ) NEW_LINE print " Case ▁ # % d : ▁ % d " % ( t , s ) NEW_LINE DEDENT

import math NEW_LINE def numberofsteps ( lst , bites ) : NEW_LINE INDENT output = bites NEW_LINE for item in lst : NEW_LINE INDENT output += ( int ( math . ceil ( item / bites ) ) - 1 ) NEW_LINE DEDENT return output NEW_LINE DEDENT def process ( lst ) : NEW_LINE INDENT m = max ( lst ) NEW_LINE return min ( [ numberofsteps ( lst , x ) for x in range ( 1 , m + 1 ) ] ) NEW_LINE DEDENT import sys NEW_LINE with open ( sys . argv [ 1 ] , " r " ) as fileIN : NEW_LINE INDENT inputLines = fileIN . readlines ( ) NEW_LINE DEDENT inputLines = [ line . strip ( ) for line in inputLines ] NEW_LINE with open ( sys . argv [ 2 ] , " w " ) as fileOUT : NEW_LINE INDENT numberOfCases = int ( inputLines . pop ( 0 ) ) NEW_LINE for num in range ( numberOfCases ) : NEW_LINE INDENT numberofplates = int ( inputLines . pop ( 0 ) ) NEW_LINE inputlist = [ int ( x ) for x in inputLines . pop ( 0 ) . rstrip ( ) . split ( ' ▁ ' ) ] NEW_LINE fileOUT . write ( ' Case ▁ # ' + str ( num + 1 ) + ' : ▁ ' + str ( process ( inputlist ) ) + ' \n ' ) NEW_LINE DEDENT DEDENT

import math NEW_LINE f_in = open ( ' file . in ' ) NEW_LINE f_out = open ( ' file . out ' , ' w ' ) NEW_LINE def solution ( plates ) : NEW_LINE INDENT min = max ( plates ) NEW_LINE for i in range ( 1 , max ( plates ) ) : NEW_LINE INDENT candidate = i + sum ( [ math . ceil ( plate / i ) - 1 for plate in plates ] ) NEW_LINE if candidate < min : NEW_LINE INDENT min = candidate NEW_LINE DEDENT DEDENT return min NEW_LINE DEDENT cases = int ( f_in . readline ( ) ) NEW_LINE for i in range ( 1 , cases + 1 ) : NEW_LINE INDENT f_in . readline ( ) NEW_LINE plates = [ int ( i ) for i in f_in . readline ( ) . split ( ) ] NEW_LINE f_out . write ( " Case ▁ # " + str ( i ) + " : ▁ " + str ( solution ( plates ) ) + " \n " ) NEW_LINE DEDENT

DESCRIPTION = """ STRNEWLINE """ NEW_LINE import os NEW_LINE import sys NEW_LINE import argparse NEW_LINE def perr ( msg ) : NEW_LINE INDENT sys . stderr . write ( " % s " % msg ) NEW_LINE sys . stderr . flush ( ) NEW_LINE DEDENT def pinfo ( msg ) : NEW_LINE INDENT sys . stdout . write ( " % s " % msg ) NEW_LINE sys . stdout . flush ( ) NEW_LINE DEDENT def runcmd ( cmd ) : NEW_LINE INDENT perr ( " % s \n " % cmd ) NEW_LINE os . system ( cmd ) NEW_LINE DEDENT def getargs ( ) : NEW_LINE INDENT parser = argparse . ArgumentParser ( description = DESCRIPTION , formatter_class = argparse . RawTextHelpFormatter ) NEW_LINE parser . add_argument ( ' infile ' , type = str , help = ' input ▁ file ' ) NEW_LINE parser . add_argument ( ' outfile ' , type = str , nargs = ' ? ' , default = None , help = ' output ▁ file ' ) NEW_LINE return parser . parse_args ( ) NEW_LINE DEDENT def steps_to_get_Pmax_eq_p ( P , Pmax ) : NEW_LINE INDENT nstep = 0 NEW_LINE for p in P : NEW_LINE INDENT nstep += ( ( p - 1 ) / Pmax ) NEW_LINE DEDENT return nstep NEW_LINE DEDENT def solve ( D , P ) : NEW_LINE INDENT min_costs = max ( P ) NEW_LINE for Pmax in range ( 1 , max ( P ) + 1 ) : NEW_LINE INDENT N = steps_to_get_Pmax_eq_p ( P , Pmax ) NEW_LINE costs = N + Pmax NEW_LINE if costs < min_costs : NEW_LINE INDENT min_costs = costs NEW_LINE DEDENT DEDENT return min_costs NEW_LINE DEDENT def main ( args ) : NEW_LINE INDENT if None == args . outfile : NEW_LINE INDENT outfile = sys . stdout NEW_LINE DEDENT else : NEW_LINE INDENT outfile = open ( args . outfile , " w " ) NEW_LINE DEDENT with open ( args . infile ) as infile : NEW_LINE INDENT T = int ( infile . readline ( ) ) NEW_LINE for i in range ( 1 , T + 1 ) : NEW_LINE INDENT D = int ( infile . readline ( ) ) NEW_LINE P = [ int ( p ) for p in infile . readline ( ) . split ( ) ] NEW_LINE outfile . write ( " Case ▁ # % d : ▁ % d \n " % ( i , solve ( D , P ) ) ) NEW_LINE DEDENT DEDENT if None != args . outfile : NEW_LINE INDENT outfile . close ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( getargs ( ) ) NEW_LINE DEDENT

import sys NEW_LINE import math NEW_LINE def load_cases ( path ) : NEW_LINE INDENT case_list = [ ] NEW_LINE with open ( path , ' r ' ) as fh : NEW_LINE INDENT case_num = int ( fh . readline ( ) . strip ( " \n " ) ) NEW_LINE for i in range ( 0 , case_num ) : NEW_LINE INDENT D = int ( fh . readline ( ) . strip ( " \n " ) ) NEW_LINE P = filter ( None , fh . readline ( ) . strip ( " \n " ) . split ( " ▁ " ) ) NEW_LINE if D != len ( P ) : NEW_LINE INDENT print ' incorrect ▁ file ▁ at ▁ case ▁ % d ' % i NEW_LINE sys . exit ( ) NEW_LINE DEDENT case_list . append ( [ int ( item ) for item in P ] ) NEW_LINE DEDENT DEDENT return case_list NEW_LINE DEDENT def calculate_case ( P ) : NEW_LINE INDENT max_num = max ( P ) NEW_LINE return min ( sum ( [ int ( ( num - 0.5 ) / m ) for num in P ] ) + m for m in range ( max_num , 0 , - 1 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT if len ( sys . argv ) != 3 : NEW_LINE INDENT print ' Usage : ▁ q2 . py ▁ input _ file ▁ output _ file ' NEW_LINE sys . exit ( ) NEW_LINE DEDENT input_file = sys . argv [ 1 ] NEW_LINE output_file = sys . argv [ 2 ] NEW_LINE case_list = load_cases ( input_file ) NEW_LINE with open ( output_file , ' w ' ) as fh : NEW_LINE INDENT case_id = 1 NEW_LINE for case in case_list : NEW_LINE INDENT time = calculate_case ( case ) NEW_LINE fh . write ( ' Case ▁ # % d : ▁ % d \n ' % ( case_id , time ) ) NEW_LINE case_id += 1 NEW_LINE DEDENT DEDENT DEDENT


import java . io . * ; import java . util . * ; public class A { PrintWriter out ; Scanner in ; public static void main ( String [ ] args ) throws Exception { new A ( ) . solve ( ) ; } Map < String , Integer > nums = new HashMap ( ) ; String [ ] names = new String [ 1010 ] ; List < String > [ ] ing = new List [ 1010 ] ; int go ( String name ) { int ind = nums . get ( name ) ; List < Integer > deps = new ArrayList ( ) ; for ( String dep : ing [ ind ] ) { if ( Character . isLowerCase ( dep . charAt ( 0 ) ) ) { } else { deps . add ( go ( dep ) ) ; } } Collections . sort ( deps ) ; int ans = 0 ; for ( int i = deps . size ( ) - 1 ; i >= 0 ; -- i ) { int have = deps . size ( ) - i - 1 ; have += deps . get ( i ) ; if ( have > ans ) { ans = have ; } } ans = Math . max ( ans , deps . size ( ) + 1 ) ; return ans ; } void solve ( ) throws Exception { Locale . setDefault ( Locale . ENGLISH ) ; out = new PrintWriter ( new FileOutputStream ( " a . out " ) ) ; in = new Scanner ( new BufferedReader ( new InputStreamReader ( new FileInputStream ( " a . in " ) ) ) ) ; int N = in . nextInt ( ) ; for ( int t = 1 ; t <= N ; ++ t ) { out . print ( " Case ▁ # " + t + " : ▁ " ) ; int n = in . nextInt ( ) ; for ( int i = 0 ; i < n ; ++ i ) { String s = in . next ( ) ; int m = in . nextInt ( ) ; names [ i ] = s ; nums . put ( s , i ) ; ing [ i ] = new ArrayList ( ) ; for ( int j = 0 ; j < m ; ++ j ) { ing [ i ] . add ( in . next ( ) ) ; } } out . println ( go ( names [ 0 ] ) ) ; } out . close ( ) ; } }

import java . util . * ; public class A { static HashMap < String , Integer > hm = new HashMap ( ) ; static int X ; static int id ( String s ) { if ( s . matches ( " [ a - z ] + " ) ) return - 1 ; if ( hm . containsKey ( s ) ) return hm . get ( s ) ; else hm . put ( s , X ) ; return X ++ ; } static int [ ] [ ] ls ; static int go ( int id ) { if ( id == - 1 ) return - 1000000 ; int [ ] cnt = new int [ ls [ id ] . length ] ; int mixes = 0 ; for ( int i = 0 ; i < cnt . length ; i ++ ) { if ( ls [ id ] [ i ] >= 0 ) mixes ++ ; cnt [ i ] = go ( ls [ id ] [ i ] ) ; } Arrays . sort ( cnt ) ; int ret = 1 ; for ( int i = 0 ; i < cnt . length ; i ++ ) { ret = Math . max ( ret , cnt [ i ] + cnt . length - i - 1 ) ; } return Math . max ( ret , mixes + 1 ) ; } public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int C = sc . nextInt ( ) ; String s ; for ( int i = 0 ; i < C ; i ++ ) { hm = new HashMap ( ) ; X = 0 ; int N = sc . nextInt ( ) ; ls = new int [ N ] [ ] ; for ( int j = 0 ; j < N ; j ++ ) { int id1 = id ( sc . next ( ) ) ; ls [ id1 ] = new int [ sc . nextInt ( ) ] ; for ( int k = 0 ; k < ls [ id1 ] . length ; k ++ ) { ls [ id1 ] [ k ] = id ( s = sc . next ( ) ) ; } Arrays . sort ( ls [ id1 ] ) ; } int req = go ( 0 ) ; System . out . printf ( " Case ▁ # % d : ▁ % d \n " , i + 1 , req ) ; } } }

from __future__ import with_statement NEW_LINE import re , heapq NEW_LINE import sys NEW_LINE def addlBowlsFor ( mixture , mixtures , unusedBowls = 0 , p = False ) : NEW_LINE INDENT if mixture . islower ( ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT ingreds = mixtures [ mixture ] NEW_LINE ingredLeastUse = sorted ( ingreds , None , lambda x : addlBowlsFor ( x , mixtures , unusedBowls ) ) NEW_LINE bowlsINeed = 0 NEW_LINE big = 0 NEW_LINE for ingred in reversed ( ingredLeastUse ) : NEW_LINE INDENT if ingred . isupper ( ) : NEW_LINE INDENT addlBowls = addlBowlsFor ( ingred , mixtures , unusedBowls , p ) NEW_LINE unusedBowls += addlBowls - 1 NEW_LINE bowlsINeed += addlBowls NEW_LINE DEDENT DEDENT if unusedBowls == 0 : NEW_LINE INDENT bowlsINeed += 1 NEW_LINE DEDENT return bowlsINeed NEW_LINE DEDENT def doWork ( first , mixtures ) : NEW_LINE INDENT return addlBowlsFor ( first , mixtures , 0 , True ) NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT with open ( sys . argv [ 1 ] ) as f : NEW_LINE INDENT cases = int ( f . readline ( ) ) NEW_LINE for i in xrange ( cases ) : NEW_LINE INDENT mixtureCount = int ( f . readline ( ) ) NEW_LINE first = None NEW_LINE mixtures = { } NEW_LINE for mi in xrange ( mixtureCount ) : NEW_LINE INDENT data = f . readline ( ) . strip ( ) NEW_LINE data = re . split ( r ' \s + ' , data ) NEW_LINE if not first : NEW_LINE INDENT first = data [ 0 ] NEW_LINE DEDENT mixtures [ data [ 0 ] ] = data [ 2 : ] NEW_LINE DEDENT result = doWork ( first , mixtures ) NEW_LINE print " Case ▁ # % d : ▁ % d " % ( i + 1 , result ) NEW_LINE DEDENT DEDENT DEDENT main ( ) NEW_LINE