import java . io . BufferedReader ; import java . io . BufferedWriter ; import java . io . File ; import java . io . FileReader ; import java . io . FileWriter ; import java . io . InputStream ; import java . io . InputStreamReader ; import java . io . PrintWriter ; import java . util . StringTokenizer ; public class a { public static void main ( String [ ] Args ) throws Exception { FS sc = new FS ( new File ( " A - large . in " ) ) ; PrintWriter out = new PrintWriter ( new BufferedWriter ( new FileWriter ( new File ( " a . out " ) ) ) ) ; int cc = 0 ; int t = sc . nextInt ( ) ; while ( t -- > 0 ) { out . printf ( " Case ▁ # % d : ▁ " , ++ cc ) ; String s = sc . next ( ) ; out . printf ( " % s " , foo ( s ) ) ; out . println ( " " ) ; } out . close ( ) ; } public static String foo ( String s ) throws Exception { if ( s . length ( ) == 0 ) return " " ; int last = ' A ' ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { last = Math . max ( last , s . charAt ( i ) ) ; } for ( int i = s . length ( ) - 1 ; i >= 0 ; i -- ) { if ( s . charAt ( i ) == last ) { return ( char ) ( last ) + foo ( s . substring ( 0 , i ) ) + s . substring ( i + 1 ) ; } } throw new Exception ( " BANANAS " ) ; } public static class FS { BufferedReader br ; StringTokenizer st ; FS ( InputStream in ) throws Exception { br = new BufferedReader ( new InputStreamReader ( in ) ) ; st = new StringTokenizer ( br . readLine ( ) ) ; } FS ( File in ) throws Exception { br = new BufferedReader ( new FileReader ( in ) ) ; st = new StringTokenizer ( br . readLine ( ) ) ; } String next ( ) throws Exception { if ( st . hasMoreTokens ( ) ) return st . nextToken ( ) ; st = new StringTokenizer ( br . readLine ( ) ) ; return next ( ) ; } int nextInt ( ) throws Exception { return Integer . parseInt ( next ( ) ) ; } } }


class Case : NEW_LINE INDENT def __init__ ( self , fin , fout , n ) : NEW_LINE INDENT self . _fin = fin NEW_LINE self . _fout = fout NEW_LINE self . _n = n NEW_LINE self . _line = None NEW_LINE DEDENT def readInt ( self ) : NEW_LINE INDENT if self . _line is None : NEW_LINE INDENT self . _line = self . _fin . readline ( ) . split ( " ▁ " ) NEW_LINE DEDENT result = int ( self . _line . pop ( 0 ) ) NEW_LINE if len ( self . _line ) == 0 : NEW_LINE INDENT self . _line = None NEW_LINE DEDENT return result NEW_LINE DEDENT def readLine ( self ) : NEW_LINE INDENT line = self . _fin . readline ( ) NEW_LINE if line [ - 1 ] == " \n " : NEW_LINE INDENT return line [ : - 1 ] NEW_LINE DEDENT return line NEW_LINE DEDENT def output ( self , s ) : NEW_LINE INDENT if s is None : NEW_LINE INDENT return NEW_LINE DEDENT s = str ( s ) NEW_LINE if " \n " in s : NEW_LINE INDENT self . _fout . write ( " Case ▁ # % d : % s " % ( self . _n , s ) ) NEW_LINE DEDENT else : NEW_LINE INDENT self . _fout . write ( " Case ▁ # % d : ▁ % s \n " % ( self . _n , s ) ) NEW_LINE DEDENT DEDENT DEDENT def run ( fileName , solve ) : NEW_LINE INDENT fin = open ( fileName ) NEW_LINE fout = open ( fileName + " . out " , " w " ) NEW_LINE caseCount = int ( fin . readline ( ) ) NEW_LINE for i in range ( caseCount ) : NEW_LINE INDENT case = Case ( fin , fout , i + 1 ) NEW_LINE case . output ( solve ( case ) ) NEW_LINE DEDENT DEDENT


import sys NEW_LINE def compute ( S ) : NEW_LINE INDENT if S == ' ' : NEW_LINE INDENT return ' ' NEW_LINE DEDENT x = max ( S ) NEW_LINE p = S . find ( x ) NEW_LINE y = compute ( S [ : p ] ) NEW_LINE z = filter ( lambda c : c != x , S [ p : ] ) NEW_LINE return ( x * ( len ( S ) - p - len ( z ) ) ) + y + z NEW_LINE DEDENT def parse ( ) : NEW_LINE INDENT S = sys . stdin . readline ( ) . strip ( ) NEW_LINE return S , NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT sys . setrecursionlimit ( 100000 ) NEW_LINE T = int ( sys . stdin . readline ( ) . strip ( ) ) NEW_LINE count = 1 NEW_LINE part = 0 NEW_LINE if len ( sys . argv ) == 3 : NEW_LINE INDENT part = int ( sys . argv [ 1 ] ) NEW_LINE count = int ( sys . argv [ 2 ] ) NEW_LINE DEDENT for i in xrange ( T ) : NEW_LINE INDENT data = parse ( ) NEW_LINE if i * count >= part * T and i * count < ( part + 1 ) * T : NEW_LINE INDENT result = compute ( * data ) NEW_LINE print " Case ▁ # % d : ▁ % s " % ( i + 1 , result ) NEW_LINE DEDENT DEDENT DEDENT


from sys import stdin , stdout NEW_LINE def solve ( test_id ) : NEW_LINE INDENT s = stdin . readline ( ) . strip ( ) NEW_LINE r = ' ' NEW_LINE for a in s : NEW_LINE INDENT if len ( r ) > 0 and r [ 0 ] <= a : NEW_LINE INDENT r = a + r NEW_LINE DEDENT else : NEW_LINE INDENT r = r + a NEW_LINE DEDENT DEDENT print ( " Case ▁ # { } : ▁ { } " . format ( test_id , r ) ) NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT t = int ( stdin . readline ( ) ) NEW_LINE for i in range ( t ) : NEW_LINE INDENT solve ( i + 1 ) NEW_LINE DEDENT DEDENT main ( ) NEW_LINE


import sys NEW_LINE import itertools NEW_LINE def ans ( S ) : NEW_LINE INDENT ans = " " NEW_LINE for x in S : NEW_LINE INDENT if ( x + ans ) > ( ans + x ) : NEW_LINE INDENT ans = x + ans NEW_LINE DEDENT else : NEW_LINE INDENT ans = ans + x NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT s = sys . stdin . readline ( ) NEW_LINE T = int ( s ) NEW_LINE for i in xrange ( 0 , T ) : NEW_LINE INDENT S = sys . stdin . readline ( ) NEW_LINE print " Case ▁ # { 0 } : ▁ { 1 } " . format ( i + 1 , ans ( S . strip ( ) ) ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT main ( ) NEW_LINE DEDENT


import java . util . * ; public class B { static Scanner in ; public static void main ( String [ ] args ) { in = new Scanner ( System . in ) ; int T = in . nextInt ( ) ; for ( int C = 1 ; C <= T ; C ++ ) { System . out . println ( " Case ▁ # " + C + " : ▁ " + solve ( ) ) ; } } public static String solve ( ) { String out = " " ; int B = in . nextInt ( ) ; long M = in . nextLong ( ) - 1 ; String binary = " " ; while ( M > 0 ) { binary += M % 2 ; M = M / 2 ; } System . err . println ( binary ) ; if ( binary . length ( ) > B - 2 ) { out = " IMPOSSIBLE " ; } else { out = " POSSIBLE " ; for ( int i = 0 ; i < B ; i ++ ) { String line = " " ; for ( int j = 0 ; j <= i ; j ++ ) { line += "0" ; } for ( int j = 0 ; j < B - 2 - i ; j ++ ) { line += "1" ; } if ( i < B - 1 ) { if ( i == 0 ) { line += "1" ; } else if ( i - 1 < binary . length ( ) ) { line += binary . charAt ( i - 1 ) ; } else { line += "0" ; } } out += " \n " + line ; } } return out ; } }

from sys import stdin NEW_LINE from string import zfill NEW_LINE def each_case ( B , M ) : NEW_LINE INDENT if M < 2 ** ( B - 2 ) : NEW_LINE INDENT print zfill ( bin ( M ) [ 2 : ] , B - 1 ) + '0' NEW_LINE for i in xrange ( B - 1 ) : NEW_LINE INDENT print '0' * ( i + 2 ) + '1' * ( B - i - 2 ) NEW_LINE DEDENT DEDENT elif M == 2 ** ( B - 2 ) : NEW_LINE INDENT print '0' + '1' * ( B - 1 ) NEW_LINE for i in xrange ( B - 1 ) : NEW_LINE INDENT print '0' * ( i + 2 ) + '1' * ( B - i - 2 ) NEW_LINE DEDENT DEDENT DEDENT T = int ( stdin . readline ( ) ) NEW_LINE for t in xrange ( 1 , T + 1 ) : NEW_LINE INDENT B , M = map ( int , stdin . readline ( ) . split ( ) ) NEW_LINE print ' Case ▁ # { } : ▁ { } ' . format ( t , ' POSSIBLE ' if ( M <= 2 ** ( B - 2 ) ) else ' IMPOSSIBLE ' ) NEW_LINE each_case ( B , M ) NEW_LINE DEDENT


def solve ( b , m ) : NEW_LINE INDENT if m > 2 ** ( b - 2 ) : NEW_LINE INDENT print ( " IMPOSSIBLE " ) NEW_LINE return NEW_LINE DEDENT print ( " POSSIBLE " ) NEW_LINE if m == 2 ** ( b - 2 ) : NEW_LINE INDENT print ( "0" + "1" * ( b - 1 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT l = [ ( m >> i ) & 1 for i in range ( b - 2 ) ] NEW_LINE print ( "0" + " " . join ( map ( str , reversed ( l ) ) ) + "0" ) NEW_LINE DEDENT for i in range ( 1 , b ) : NEW_LINE INDENT print ( "0" * ( i + 1 ) + "1" * ( b - i - 1 ) ) NEW_LINE DEDENT DEDENT t = int ( input ( ) ) NEW_LINE for i in range ( t ) : NEW_LINE INDENT print ( " Case ▁ # " + str ( i + 1 ) + " : " , end = " ▁ " ) NEW_LINE b , m = map ( int , input ( ) . split ( ) ) NEW_LINE solve ( b , m ) NEW_LINE DEDENT


import sys NEW_LINE def solve_test ( inp ) : NEW_LINE INDENT B , M = map ( int , inp . readline ( ) . split ( ) ) NEW_LINE if M > 2 ** ( B - 2 ) : NEW_LINE INDENT return ' IMPOSSIBLE ' NEW_LINE DEDENT if M == 2 ** ( B - 2 ) : NEW_LINE INDENT first = '0' + '1' * ( B - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT first = '0' + bin ( M ) [ 2 : ] . zfill ( B - 2 ) + '0' NEW_LINE DEDENT print ( bin ( M ) , bin ( M ) [ 2 : ] . zfill ( B - 2 ) ) NEW_LINE lines = [ '0' * i + '1' * ( B - i ) for i in range ( 2 , B + 1 ) ] NEW_LINE return ' POSSIBLE \n ' + ' \n ' . join ( [ first ] + lines ) NEW_LINE DEDENT def solve_dumb ( inp ) : NEW_LINE INDENT return '1' NEW_LINE DEDENT inp = open ( sys . argv [ 1 ] ) NEW_LINE inp_dumb = open ( sys . argv [ 1 ] ) NEW_LINE out = open ( sys . argv [ 1 ] . rsplit ( ' . ' , 1 ) [ 0 ] + ' . out ' , ' w ' ) NEW_LINE out_dumb = open ( sys . argv [ 1 ] . rsplit ( ' . ' , 1 ) [ 0 ] + ' . dumb . out ' , ' w ' ) NEW_LINE n_tests = int ( inp . readline ( ) ) NEW_LINE for i in range ( n_tests ) : NEW_LINE INDENT ans = solve_test ( inp ) NEW_LINE print ( " Case ▁ # % d : ▁ " % ( i + 1 ) + ans , file = out ) NEW_LINE ans_dumb = solve_dumb ( inp_dumb ) NEW_LINE print ( " Case ▁ # % d : ▁ " % ( i + 1 ) + ans_dumb , file = out_dumb ) NEW_LINE if ans != ans_dumb : NEW_LINE INDENT print ( ' Wrong ' , i + 1 , file = sys . stderr ) NEW_LINE DEDENT DEDENT out . close ( ) NEW_LINE


f = open ( ' large2 . in ' ) NEW_LINE T = int ( f . readline ( ) . strip ( ) ) NEW_LINE for k in range ( T ) : NEW_LINE INDENT case = f . readline ( ) . split ( ) NEW_LINE B = int ( case [ 0 ] ) NEW_LINE M = int ( case [ 1 ] ) NEW_LINE if ( M > ( 2 ** ( B - 2 ) ) ) : NEW_LINE INDENT print " Case ▁ # % d : ▁ IMPOSSIBLE " % ( k + 1 ) NEW_LINE continue NEW_LINE DEDENT print " Case ▁ # % d : ▁ POSSIBLE " % ( k + 1 ) NEW_LINE matrix = [ ] NEW_LINE for i in range ( B ) : NEW_LINE INDENT oneLine = [ ] NEW_LINE for j in range ( B ) : NEW_LINE INDENT oneLine . append ( "0" ) NEW_LINE DEDENT matrix . append ( oneLine ) NEW_LINE DEDENT if ( M > 0 ) : NEW_LINE INDENT M -= 1 NEW_LINE matrix [ 0 ] [ B - 1 ] = "1" NEW_LINE DEDENT totalPossible = 1 NEW_LINE lastIdx = 1 NEW_LINE for i in range ( B - 2 ) : NEW_LINE INDENT currentIdx = B - i - 2 NEW_LINE if ( M == 0 ) : NEW_LINE INDENT lastIdx = currentIdx + 1 NEW_LINE break NEW_LINE DEDENT if ( M >= totalPossible ) : NEW_LINE INDENT M -= totalPossible NEW_LINE totalPossible *= 2 NEW_LINE otherIdx = currentIdx NEW_LINE while ( otherIdx < B - 1 ) : NEW_LINE INDENT otherIdx += 1 NEW_LINE matrix [ currentIdx ] [ otherIdx ] = "1" NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT otherIdx = currentIdx NEW_LINE while ( otherIdx < B - 1 ) : NEW_LINE INDENT otherIdx += 1 NEW_LINE totalPossible /= 2 NEW_LINE if ( totalPossible == 0 ) : NEW_LINE INDENT totalPossible = 1 NEW_LINE DEDENT if ( M >= totalPossible ) : NEW_LINE INDENT M -= totalPossible NEW_LINE matrix [ currentIdx ] [ otherIdx ] = "1" NEW_LINE DEDENT DEDENT lastIdx = currentIdx NEW_LINE break NEW_LINE DEDENT DEDENT while ( lastIdx < B - 1 ) : NEW_LINE INDENT matrix [ 0 ] [ lastIdx ] = "1" NEW_LINE lastIdx += 1 NEW_LINE DEDENT for line in matrix : NEW_LINE INDENT print " " . join ( line ) NEW_LINE DEDENT DEDENT


def f ( b , m ) : NEW_LINE INDENT if m > ( 1 << ( b - 2 ) ) : NEW_LINE INDENT return [ ] NEW_LINE DEDENT x = [ [ 1 if ( ( i > j ) and ( i < ( b - 1 ) ) ) else 0 for i in xrange ( b ) ] for j in xrange ( b ) ] NEW_LINE m -= 1 NEW_LINE x [ 0 ] [ b - 1 ] = 1 NEW_LINE for i in xrange ( 1 , b - 1 ) : NEW_LINE INDENT if ( m >> ( i - 1 ) ) & 1 : NEW_LINE INDENT x [ i ] [ b - 1 ] = 1 NEW_LINE DEDENT DEDENT return x NEW_LINE DEDENT from sys import stdin NEW_LINE def getint ( ) : NEW_LINE INDENT return int ( stdin . readline ( ) ) NEW_LINE DEDENT def getints ( ) : NEW_LINE INDENT return tuple ( int ( z ) for z in stdin . readline ( ) . split ( ) ) NEW_LINE DEDENT for cn in xrange ( 1 , 1 + getint ( ) ) : NEW_LINE INDENT ( b , m ) = getints ( ) NEW_LINE sol = f ( b , m ) NEW_LINE if len ( sol ) == 0 : NEW_LINE INDENT output = " IMPOSSIBLE " NEW_LINE DEDENT else : NEW_LINE INDENT output = " POSSIBLE \n " + " \n " . join ( " " . join ( str ( k ) for k in row ) for row in sol ) NEW_LINE DEDENT print " Case ▁ # { } : ▁ { } " . format ( cn , output ) NEW_LINE DEDENT


import java . io . File ; import java . io . FileInputStream ; import java . io . FileOutputStream ; import java . io . IOException ; import java . util . Scanner ; public class Main { public static void main ( String [ ] args ) throws IOException { Scanner scanner = new Scanner ( System . in ) ; int t = scanner . nextInt ( ) ; for ( int x = 1 ; x <= t ; ++ x ) { int b = scanner . nextInt ( ) ; long m = scanner . nextLong ( ) ; System . out . print ( " Case ▁ # " + x + " : ▁ " ) ; if ( m > Math . pow ( 2 , b - 2 ) ) { System . out . print ( " IMPOSSIBLE \n " ) ; continue ; } else System . out . print ( " POSSIBLE \n " ) ; int [ ] [ ] slides = new int [ b ] [ b ] ; for ( int i = 0 ; i < b ; ++ i ) { for ( int j = i + 1 ; j < b ; ++ j ) { slides [ i ] [ j ] = 1 ; } } String str = Long . toBinaryString ( m - 1 ) + " " ; if ( str . equals ( "0" ) ) str = " " ; while ( str . length ( ) < b - 1 ) { str = "0" + str ; } str += "1" ; System . out . println ( str ) ; for ( int i = 1 ; i < b ; ++ i ) { for ( int j = 0 ; j < b ; ++ j ) { System . out . print ( slides [ i ] [ j ] ) ; } System . out . print ( " \n " ) ; } } } }


import java . io . BufferedReader ; import java . io . FileReader ; import java . io . IOException ; import java . io . PrintWriter ; import java . util . StringTokenizer ; public class B { public static void main ( String [ ] args ) { new B ( ) . run ( ) ; } BufferedReader br ; StringTokenizer in ; PrintWriter out ; public String nextToken ( ) throws IOException { while ( in == null || ! in . hasMoreTokens ( ) ) { in = new StringTokenizer ( br . readLine ( ) ) ; } return in . nextToken ( ) ; } public int nextInt ( ) throws IOException { return Integer . parseInt ( nextToken ( ) ) ; } public long nextLong ( ) throws IOException { return Long . parseLong ( nextToken ( ) ) ; } public void solve ( ) throws IOException { int n = nextInt ( ) ; long b = nextLong ( ) ; if ( b > 1L << ( n - 2 ) ) { out . println ( " IMPOSSIBLE " ) ; return ; } int [ ] [ ] a = new int [ n ] [ n ] ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { a [ i ] [ j ] = 1 ; } } b -- ; a [ 0 ] [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( b % 2 == 1 ) { a [ 0 ] [ i ] = 1 ; } b /= 2 ; } out . println ( " POSSIBLE " ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { out . print ( a [ i ] [ j ] ) ; } out . println ( ) ; } } public void run ( ) { try { br = new BufferedReader ( new FileReader ( " input . txt " ) ) ; out = new PrintWriter ( " output . txt " ) ; int t = nextInt ( ) ; for ( int i = 0 ; i < t ; i ++ ) { out . print ( String . format ( " Case ▁ # % d : ▁ " , i + 1 ) ) ; solve ( ) ; } out . close ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } } }


import java . util . * ; import java . io . * ; public class B { static int N ; static int ans ; public static void main ( String ... orange ) throws Exception { Scanner input = new Scanner ( System . in ) ; int numCases = input . nextInt ( ) ; for ( int n = 0 ; n < numCases ; n ++ ) { int B = input . nextInt ( ) ; long M = input . nextLong ( ) ; if ( M > ( 1L << ( B - 2 ) ) ) { System . out . printf ( " Case ▁ # % d : ▁ IMPOSSIBLE \n " , n + 1 ) ; continue ; } boolean [ ] [ ] slides = new boolean [ B ] [ B ] ; if ( M == ( 1L << ( B - 2 ) ) ) { for ( int i = 0 ; i < B ; i ++ ) for ( int j = i + 1 ; j < B ; j ++ ) slides [ i ] [ j ] = true ; } for ( int i = 0 ; i + 1 < B ; i ++ ) for ( int j = i + 1 ; j + 1 < B ; j ++ ) slides [ i ] [ j ] = true ; for ( int i = 0 ; i + 2 < B ; i ++ ) if ( ( M & ( 1L << i ) ) > 0 ) slides [ i + 1 ] [ B - 1 ] = true ; System . out . printf ( " Case ▁ # % d : ▁ POSSIBLE \n " , n + 1 ) ; for ( int i = 0 ; i < B ; i ++ ) { for ( int j = 0 ; j < B ; j ++ ) System . out . print ( slides [ i ] [ j ] ? '1' : '0' ) ; System . out . println ( ) ; } } } }


package Round1 ; import java . io . * ; public class B { public static void main ( String [ ] args ) throws IOException { BufferedReader in = new BufferedReader ( new FileReader ( " / Users / chao / Downloads / B - large . in " ) ) ; FileWriter out = new FileWriter ( " / Users / chao / Desktop / B - large . txt " ) ; String s = in . readLine ( ) ; int m = Integer . parseInt ( s ) ; for ( int cases = 1 ; cases <= m ; cases ++ ) { String [ ] ss = in . readLine ( ) . split ( " ▁ " ) ; int B = Integer . parseInt ( ss [ 0 ] ) ; long M = Long . parseLong ( ss [ 1 ] ) ; out . write ( " Case ▁ # " + cases + " : ▁ " ) ; int n = B ; long max = pow ( B - 2 ) ; if ( max >= M ) { out . write ( " POSSIBLE \n " ) ; int [ ] [ ] map = new int [ n ] [ n ] ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { map [ i ] [ j ] = 1 ; } } for ( int k = 1 ; k < n ; k ++ ) { if ( k != n - 1 ) max = max / 2 ; if ( max <= M ) { M -= max ; map [ 0 ] [ k ] = 1 ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { out . write ( " " + map [ i ] [ j ] ) ; } out . write ( " \n " ) ; } } else { out . write ( " IMPOSSIBLE \n " ) ; } } in . close ( ) ; out . flush ( ) ; out . close ( ) ; } private static long pow ( int n ) { long x = 1 ; for ( int i = 0 ; i < n ; i ++ ) { x *= 2 ; } return x ; } }


import java . io . * ; import java . util . * ; public class teachingassistant { private static InputReader in ; private static PrintWriter out ; public static boolean SUBMIT = true ; public static final String NAME = " A - large " ; private static void main2 ( ) throws IOException { char [ ] c = in . next ( ) . toCharArray ( ) ; int n = c . length ; char [ ] stack = new char [ n ] ; int idx = 0 ; int need = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( idx > 0 && stack [ idx - 1 ] == c [ i ] ) { idx -- ; continue ; } if ( i + idx >= n ) { need ++ ; idx -- ; } else { stack [ idx ++ ] = c [ i ] ; } } out . println ( ( n - need ) * 5 ) ; } public static void main ( String [ ] args ) throws IOException { if ( SUBMIT ) { in = new InputReader ( new FileInputStream ( new File ( NAME + " . in " ) ) ) ; out = new PrintWriter ( new BufferedWriter ( new FileWriter ( NAME + " . out " ) ) ) ; } else { in = new InputReader ( System . in ) ; out = new PrintWriter ( System . out , true ) ; } int numCases = in . nextInt ( ) ; for ( int test = 1 ; test <= numCases ; test ++ ) { out . print ( " Case ▁ # " + test + " : ▁ " ) ; main2 ( ) ; } out . close ( ) ; System . exit ( 0 ) ; } static class InputReader { public BufferedReader reader ; public StringTokenizer tokenizer ; public InputReader ( InputStream stream ) { reader = new BufferedReader ( new InputStreamReader ( stream ) , 32768 ) ; tokenizer = null ; } public String next ( ) { while ( tokenizer == null || ! tokenizer . hasMoreTokens ( ) ) { try { tokenizer = new StringTokenizer ( reader . readLine ( ) ) ; } catch ( IOException e ) { throw new RuntimeException ( e ) ; } } return tokenizer . nextToken ( ) ; } public int nextInt ( ) { return Integer . parseInt ( next ( ) ) ; } } }

def solve ( seq ) : NEW_LINE INDENT memo = { } NEW_LINE def opt ( lo , hi ) : NEW_LINE INDENT if ( lo , hi ) in memo : NEW_LINE INDENT return memo [ lo , hi ] NEW_LINE DEDENT if lo == hi : NEW_LINE INDENT result = 0 NEW_LINE DEDENT else : NEW_LINE INDENT best = None NEW_LINE if hi == len ( seq ) : NEW_LINE INDENT o1 = opt ( lo + 1 , hi ) NEW_LINE if o1 is not None : NEW_LINE INDENT best = max ( best , o1 ) NEW_LINE DEDENT DEDENT for i in xrange ( lo + 1 , hi ) : NEW_LINE INDENT o1 = opt ( lo + 1 , i ) NEW_LINE o2 = opt ( i + 1 , hi ) NEW_LINE if o1 is not None and o2 is not None : NEW_LINE INDENT best = max ( best , o1 + ( 10 if seq [ lo ] == seq [ i ] else 5 ) + o2 ) NEW_LINE DEDENT DEDENT result = best NEW_LINE DEDENT memo [ lo , hi ] = result NEW_LINE return result NEW_LINE DEDENT return opt ( 0 , len ( seq ) ) 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 seq = readline ( ) NEW_LINE print " Case ▁ # % d : ▁ % d " % ( i + 1 , solve ( seq ) ) NEW_LINE DEDENT DEDENT


import sys , math , random , operator NEW_LINE from string import ascii_lowercase NEW_LINE from string import ascii_uppercase NEW_LINE from fractions import Fraction , gcd NEW_LINE from decimal import Decimal , getcontext NEW_LINE from itertools import product , permutations , combinations NEW_LINE from Queue import Queue , PriorityQueue NEW_LINE from collections import deque , defaultdict , Counter NEW_LINE getcontext ( ) . prec = 100 NEW_LINE MOD = 10 ** 9 + 7 NEW_LINE INF = float ( " + inf " ) NEW_LINE if sys . subversion [ 0 ] != " CPython " : NEW_LINE INDENT raw_input = lambda : sys . stdin . readline ( ) . rstrip ( ) NEW_LINE DEDENT pr = lambda * args : sys . stdout . write ( " ▁ " . join ( str ( x ) for x in args ) + " \n " ) NEW_LINE epr = lambda * args : sys . stderr . write ( " ▁ " . join ( str ( x ) for x in args ) + " \n " ) NEW_LINE die = lambda * args : pr ( * args ) ^ exit ( 0 ) NEW_LINE read_str = raw_input NEW_LINE read_strs = lambda : raw_input ( ) . split ( ) NEW_LINE read_int = lambda : int ( raw_input ( ) ) NEW_LINE read_ints = lambda : map ( int , raw_input ( ) . split ( ) ) NEW_LINE read_float = lambda : float ( raw_input ( ) ) NEW_LINE read_floats = lambda : map ( float , raw_input ( ) . split ( ) ) NEW_LINE def solve_brute ( ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT def solve_fast ( s ) : NEW_LINE INDENT st = [ ] NEW_LINE ans = 0 NEW_LINE for c in s : NEW_LINE INDENT if st and st [ - 1 ] == c : NEW_LINE INDENT st . pop ( ) NEW_LINE ans += 10 NEW_LINE DEDENT else : NEW_LINE INDENT st . append ( c ) NEW_LINE DEDENT DEDENT ans += ( len ( st ) // 2 ) * 5 NEW_LINE return ans NEW_LINE DEDENT t = read_int ( ) NEW_LINE for t in xrange ( 1 , t + 1 ) : NEW_LINE INDENT s = read_str ( ) NEW_LINE ans_fast = solve_fast ( s ) NEW_LINE if 0 : NEW_LINE INDENT ans_brute = solve_brute ( s ) NEW_LINE if ans_fast != ans_brute : NEW_LINE INDENT print " BAD " , ans_brute , ans_fast NEW_LINE quit ( ) NEW_LINE DEDENT DEDENT print " Case ▁ # % d : ▁ % d " % ( t , ans_fast ) NEW_LINE DEDENT


import os , inspect NEW_LINE problemName = ' A ' NEW_LINE testCase = ' large ' NEW_LINE attempt = 0 NEW_LINE def solution ( preferences ) : NEW_LINE INDENT totalTaken = 0 NEW_LINE holding = [ ] NEW_LINE points = 0 NEW_LINE for c in preferences : NEW_LINE INDENT if len ( holding ) == 0 or holding [ - 1 ] != c : NEW_LINE INDENT if totalTaken == len ( preferences ) / 2 : NEW_LINE INDENT holding . pop ( ) NEW_LINE points += 5 NEW_LINE DEDENT else : NEW_LINE INDENT holding . append ( c ) NEW_LINE totalTaken += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT holding . pop ( ) NEW_LINE points += 10 NEW_LINE DEDENT DEDENT return points NEW_LINE DEDENT currentDir = os . path . dirname ( os . path . abspath ( inspect . getfile ( inspect . currentframe ( ) ) ) ) NEW_LINE if testCase in [ ' large ' , ' example ' ] : NEW_LINE INDENT inputString = problemName + ( ' - % s ' % testCase ) NEW_LINE outputString = problemName + ( ' - % s ' % testCase ) NEW_LINE DEDENT else : NEW_LINE INDENT inputString = problemName + ( ' - % s - attempt % d ' % ( testCase , attempt ) ) NEW_LINE outputString = problemName + ( ' - % s ' % testCase ) NEW_LINE DEDENT inFile = os . path . join ( currentDir , ' inputfiles ' , ' % s . in ' % inputString ) NEW_LINE outFile = os . path . join ( currentDir , ' outputfiles ' , ' % s . out ' % outputString ) NEW_LINE if os . path . exists ( outFile ) : NEW_LINE INDENT os . remove ( outFile ) NEW_LINE DEDENT with open ( inFile , ' r ' ) as inputfile : NEW_LINE INDENT numberOfCases = int ( inputfile . readline ( ) ) NEW_LINE for case in xrange ( 1 , numberOfCases + 1 ) : NEW_LINE INDENT result = solution ( inputfile . readline ( ) . strip ( ) ) NEW_LINE with open ( outFile , ' a ' ) as f : NEW_LINE INDENT f . write ( ' Case ▁ # % d : ▁ % d \n ' % ( case , result ) ) NEW_LINE DEDENT DEDENT DEDENT


def initialize_solver ( ) : NEW_LINE INDENT pass NEW_LINE DEDENT def solve_testcase ( ) : NEW_LINE INDENT s = read ( False , str ) NEW_LINE c1 = 0 NEW_LINE c2 = 0 NEW_LINE j1 = 0 NEW_LINE j2 = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if i % 2 == 0 : NEW_LINE INDENT if s [ i ] == " C " : NEW_LINE INDENT c1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT j1 += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if s [ i ] == " C " : NEW_LINE INDENT c2 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT j2 += 1 NEW_LINE DEDENT DEDENT DEDENT return 10 * ( min ( [ c1 , c2 ] ) + min ( [ j1 , j2 ] ) ) + 5 * abs ( c1 - c2 ) NEW_LINE DEDENT output_format = " Case ▁ # % d : ▁ " NEW_LINE filename = input ( ) . strip ( ) NEW_LINE sfile = None NEW_LINE tfile = None NEW_LINE if filename != " " : NEW_LINE INDENT sfile = open ( filename + " . in " , " r " ) NEW_LINE sfile . seek ( 0 ) NEW_LINE tfile = open ( filename + " . out " , " w " ) NEW_LINE DEDENT def read ( split = True , rettype = int ) : NEW_LINE INDENT if sfile : NEW_LINE INDENT input_line = sfile . readline ( ) . strip ( ) NEW_LINE DEDENT else : NEW_LINE INDENT input_line = input ( ) . strip ( ) NEW_LINE DEDENT if split : NEW_LINE INDENT return list ( map ( rettype , input_line . split ( ) ) ) NEW_LINE DEDENT else : NEW_LINE INDENT return rettype ( input_line ) NEW_LINE DEDENT DEDENT def write ( s = " \n " ) : NEW_LINE INDENT if s is None : s = " " NEW_LINE if isinstance ( s , list ) : s = " ▁ " . join ( map ( str , s ) ) NEW_LINE s = str ( s ) NEW_LINE if tfile : NEW_LINE INDENT tfile . write ( s ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( s , end = " " ) NEW_LINE DEDENT DEDENT if output_format == 0 : NEW_LINE INDENT solve_testcase ( ) NEW_LINE DEDENT else : NEW_LINE INDENT initialize_solver ( ) NEW_LINE total_cases = read ( split = False ) NEW_LINE for case_number in range ( 1 , total_cases + 1 ) : NEW_LINE INDENT write ( output_format . replace ( " % d " , str ( case_number ) ) ) NEW_LINE write ( solve_testcase ( ) ) NEW_LINE write ( " \n " ) NEW_LINE DEDENT DEDENT if tfile is not None : tfile . close ( ) NEW_LINE


T = input ( ) NEW_LINE from collections import defaultdict NEW_LINE def solve ( S ) : NEW_LINE INDENT dp = [ { ' ' : 0 } ] NEW_LINE for i , ch in enumerate ( S ) : NEW_LINE INDENT last = dp [ - 1 ] NEW_LINE nxt = defaultdict ( int ) NEW_LINE for pre , val in last . iteritems ( ) : NEW_LINE INDENT if not ( len ( S ) - i < len ( pre ) ) : NEW_LINE INDENT st = pre + ch NEW_LINE nxt [ st ] = max ( nxt [ st ] , val ) NEW_LINE DEDENT if pre : NEW_LINE INDENT sc = val + ( 10 if ch == pre [ - 1 ] else 5 ) NEW_LINE st = pre [ : - 1 ] NEW_LINE nxt [ st ] = max ( nxt [ st ] , sc ) NEW_LINE DEDENT DEDENT dp . append ( nxt ) NEW_LINE DEDENT return max ( dp [ - 1 ] . values ( ) ) NEW_LINE DEDENT for i in range ( 1 , T + 1 ) : NEW_LINE INDENT S = raw_input ( ) . strip ( ) NEW_LINE print ' Case ▁ # { } : ▁ { } ' . format ( i , solve ( S ) ) NEW_LINE DEDENT


package cj2016 . r3 ; import java . io . * ; import java . util . * ; public class A { Scanner sc ; PrintWriter pw ; char [ ] c ; public static void main ( String [ ] args ) throws Exception { String filePrefix = args . length > 0 ? args [ 0 ] : " A - large " ; try { new A ( ) . 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 ) { c = sc . next ( ) . toCharArray ( ) ; } void print ( Object s ) { pw . print ( s ) ; System . out . print ( s ) ; } void println ( Object s ) { pw . println ( s ) ; System . out . println ( s ) ; } void solve ( ) { int N = c . length ; int score = 0 ; LinkedList < Character > stack = new LinkedList < Character > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! stack . isEmpty ( ) && stack . peek ( ) == c [ i ] ) { stack . pop ( ) ; score += 10 ; } else { stack . push ( c [ i ] ) ; } } println ( score + stack . size ( ) / 2 * 5 ) ; } }


import java . io . OutputStream ; import java . io . FilenameFilter ; import java . util . Locale ; import java . io . FileOutputStream ; import java . io . IOException ; import java . io . FileInputStream ; import java . io . File ; import java . io . InputStream ; import java . io . PrintWriter ; import java . util . Scanner ; import java . util . LinkedList ; public class Main { public static void main ( String [ ] args ) { Locale . setDefault ( Locale . US ) ; InputStream inputStream ; try { final String regex = " A - ( small | large ) . * [ . ] in " ; File directory = new File ( " . " ) ; File [ ] candidates = directory . listFiles ( new FilenameFilter ( ) { public boolean accept ( File dir , String name ) { return name . matches ( regex ) ; } } ) ; File toRun = null ; for ( File candidate : candidates ) { if ( toRun == null || candidate . lastModified ( ) > toRun . lastModified ( ) ) toRun = candidate ; } inputStream = new FileInputStream ( toRun ) ; } catch ( IOException e ) { throw new RuntimeException ( e ) ; } OutputStream outputStream ; try { outputStream = new FileOutputStream ( " a . out " ) ; } catch ( IOException e ) { throw new RuntimeException ( e ) ; } Scanner in = new Scanner ( inputStream ) ; PrintWriter out = new PrintWriter ( outputStream ) ; TaskA solver = new TaskA ( ) ; int testCount = Integer . parseInt ( in . next ( ) ) ; for ( int i = 1 ; i <= testCount ; i ++ ) solver . solve ( i , in , out ) ; out . close ( ) ; } static class TaskA { public void solve ( int testNumber , Scanner in , PrintWriter out ) { String s = in . next ( ) ; int pairs = 0 ; LinkedList < Character > list = new LinkedList < > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { char next = s . charAt ( i ) ; char last = list . size ( ) > 0 ? list . peekLast ( ) : ' x ' ; if ( last == next ) { pairs ++ ; list . pollLast ( ) ; } else { list . add ( next ) ; } } out . printf ( " Case ▁ # % d : ▁ % d \n " , testNumber , pairs * 10 + 5 * list . size ( ) / 2 ) ; } } }


import java . io . * ; import java . util . * ; public class Assistant { public void solve ( ) { String s = in . nextToken ( ) ; char [ ] stack = new char [ s . length ( ) ] ; int size = 0 ; for ( char c : s . toCharArray ( ) ) { if ( size > 0 && c == stack [ size - 1 ] ) { size -- ; } else { stack [ size ++ ] = c ; } } out . println ( 5 * ( size / 2 ) + 10 * ( s . length ( ) - size ) / 2 ) ; } public void run ( ) { try { in = new FastScanner ( " input . txt " ) ; out = new PrintWriter ( " output . txt " ) ; int tests = in . nextInt ( ) ; for ( int i = 1 ; i <= tests ; i ++ ) { long time = System . currentTimeMillis ( ) ; out . printf ( " Case ▁ # % d : ▁ " , i ) ; solve ( ) ; System . err . printf ( " Solved ▁ case ▁ # % d ▁ in ▁ % d ▁ ms \n " , i , System . currentTimeMillis ( ) - time ) ; } out . close ( ) ; } catch ( FileNotFoundException e ) { } } FastScanner in ; PrintWriter out ; class FastScanner { BufferedReader br ; StringTokenizer st ; public FastScanner ( String fileName ) { try { br = new BufferedReader ( new FileReader ( fileName ) ) ; } catch ( FileNotFoundException e ) { } } String nextToken ( ) { while ( st == null || ! st . hasMoreElements ( ) ) { try { st = new StringTokenizer ( br . readLine ( ) ) ; } catch ( IOException e ) { } } return st . nextToken ( ) ; } int nextInt ( ) { return Integer . parseInt ( nextToken ( ) ) ; } long nextLong ( ) { return Long . parseLong ( nextToken ( ) ) ; } double nextDouble ( ) { return Double . parseDouble ( nextToken ( ) ) ; } } public static void main ( String [ ] args ) { new Assistant ( ) . run ( ) ; } }


package gcj . gcj2016 . round3 ; import java . io . PrintWriter ; import java . util . Scanner ; import java . util . Stack ; public class A { public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; PrintWriter out = new PrintWriter ( System . out ) ; int t = in . nextInt ( ) ; for ( int cs = 1 ; cs <= t ; cs ++ ) { char [ ] c = in . next ( ) . toCharArray ( ) ; out . println ( String . format ( " Case ▁ # % d : ▁ % d " , cs , solve ( c ) ) ) ; } out . flush ( ) ; } private static int solve ( char [ ] mood ) { int n = mood . length ; int score = 0 ; int code = 0 ; int jam = 0 ; Stack < Character > stk = new Stack < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( stk . size ( ) == 0 ) { stk . push ( mood [ i ] ) ; } else { if ( stk . peek ( ) == mood [ i ] ) { stk . pop ( ) ; score += 10 ; } else { int pool = stk . size ( ) ; int left = n - i ; if ( pool + 1 <= left - 1 ) { stk . push ( mood [ i ] ) ; } else { stk . pop ( ) ; score += 5 ; } } } } return score ; } }


import java . util . * ; public class Main { public static void main ( String args [ ] ) { ( new Main ( ) ) . solve ( ) ; } void solve ( ) { Scanner cin = new Scanner ( System . in ) ; int T = cin . nextInt ( ) ; for ( int C = 1 ; C <= T ; ++ C ) { int Row = cin . nextInt ( ) ; int Col = cin . nextInt ( ) ; int Num = cin . nextInt ( ) ; System . out . println ( " Case ▁ # " + C + " : ▁ " + solve ( Row , Col , Num ) ) ; } } int solve ( int R , int C , int N ) { List < Integer > all = new ArrayList < Integer > ( ) ; List < Integer > all2 = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < R ; ++ i ) { for ( int j = 0 ; j < C ; ++ j ) { int wall = 0 ; if ( i > 0 ) { ++ wall ; } if ( j > 0 ) { ++ wall ; } if ( i + 1 < R ) { ++ wall ; } if ( j + 1 < C ) { ++ wall ; } if ( ( i + j ) % 2 == 0 ) { all . add ( wall ) ; } else { all2 . add ( wall ) ; } } } int ret = 0 ; Collections . sort ( all ) ; for ( int i = 0 ; i < all . size ( ) - ( R * C - N ) ; ++ i ) { ret += all . get ( i ) ; } int ret2 = 0 ; Collections . sort ( all2 ) ; for ( int i = 0 ; i < all2 . size ( ) - ( R * C - N ) ; ++ i ) { ret2 += all2 . get ( i ) ; } return Math . min ( ret , ret2 ) ; } }

from sys import stdin , stderr NEW_LINE from math import log10 NEW_LINE def solve ( R , C , N ) : NEW_LINE INDENT if R == 1 or C == 1 : NEW_LINE INDENT L = R * C NEW_LINE if L % 2 == 1 : NEW_LINE INDENT return max ( N - L / 2 - 1 , 0 ) * 2 NEW_LINE DEDENT else : NEW_LINE INDENT if N <= L / 2 : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return 1 + ( N - L / 2 - 1 ) * 2 NEW_LINE DEDENT DEDENT DEDENT if ( R * C ) % 2 == 0 : NEW_LINE INDENT if N <= R * C / 2 : return 0 NEW_LINE inner = ( R - 2 ) * ( C - 2 ) / 2 NEW_LINE edge = R + C - 4 NEW_LINE conner = 2 NEW_LINE DEDENT else : NEW_LINE INDENT if N <= ( R * C + 1 ) / 2 : return 0 NEW_LINE if N <= ( R * C + 1 ) / 2 + 1 : return 3 NEW_LINE inner = ( ( R - 2 ) * ( C - 2 ) + 1 ) / 2 NEW_LINE edge = R + C - 6 NEW_LINE conner = 4 NEW_LINE pass NEW_LINE DEDENT ret = ( R - 1 ) * C + ( C - 1 ) * R NEW_LINE n = R * C - N NEW_LINE if n <= inner : return ret - n * 4 NEW_LINE ret -= inner * 4 NEW_LINE n -= inner NEW_LINE if n <= edge : return ret - n * 3 NEW_LINE ret -= edge * 3 NEW_LINE n -= edge NEW_LINE return ret - n * 2 NEW_LINE DEDENT T = int ( stdin . readline ( ) ) NEW_LINE for t in range ( T ) : NEW_LINE INDENT R , C , N = ( int ( wd ) for wd in stdin . readline ( ) . split ( ) ) NEW_LINE print " Case ▁ # % i : " % ( t + 1 ) , NEW_LINE print solve ( R , C , N ) NEW_LINE DEDENT


import sys NEW_LINE infile = None NEW_LINE outfile = None NEW_LINE def readline ( ) : NEW_LINE INDENT x = infile . readline ( ) NEW_LINE if len ( x ) > 0 and x [ - 1 ] == ' \n ' : NEW_LINE INDENT return x [ : - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT return x NEW_LINE DEDENT DEDENT def readint ( ) : NEW_LINE INDENT return int ( readline ( ) ) NEW_LINE DEDENT def readfloat ( ) : NEW_LINE INDENT return float ( readline ( ) ) NEW_LINE DEDENT def readints ( ) : NEW_LINE INDENT return [ int ( x ) for x in readline ( ) . split ( ) ] NEW_LINE DEDENT def readfloats ( ) : NEW_LINE INDENT return [ float ( x ) for x in readline ( ) . split ( ) ] NEW_LINE DEDENT def writeline ( x ) : NEW_LINE INDENT outfile . write ( x + ' \n ' ) NEW_LINE DEDENT def writecase ( casenum , answer ) : NEW_LINE INDENT outfile . write ( ' Case ▁ # { : d } : ▁ { } \n ' . format ( casenum , answer ) ) NEW_LINE DEDENT def run ( main ) : NEW_LINE INDENT global infile , outfile NEW_LINE args = sys . argv NEW_LINE infile = sys . stdin NEW_LINE outfile = sys . stdout NEW_LINE if len ( args ) == 2 : NEW_LINE INDENT if args [ 1 ] != ' - ' : NEW_LINE INDENT infile = open ( args [ 1 ] , ' r ' ) NEW_LINE if args [ 1 ] . endswith ( ' . in ' ) : NEW_LINE INDENT outfile = open ( args [ 1 ] [ : - 3 ] + ' . out ' , ' w ' ) NEW_LINE DEDENT DEDENT DEDENT elif len ( args ) == 3 : NEW_LINE INDENT if args [ 1 ] != ' - ' : NEW_LINE INDENT infile = open ( args [ 1 ] , ' r ' ) NEW_LINE DEDENT if args [ 2 ] != ' - ' : NEW_LINE INDENT outfile = open ( args [ 2 ] , ' w ' ) NEW_LINE DEDENT DEDENT elif len ( args ) > 3 : NEW_LINE INDENT print ( " Expected ▁ 0 , ▁ 1 , ▁ or ▁ 2 ▁ arguments , ▁ not ▁ { } " . format ( len ( args ) - 1 ) ) NEW_LINE print ( args ) NEW_LINE return NEW_LINE DEDENT t = readint ( ) NEW_LINE for casenum in range ( 1 , t + 1 ) : NEW_LINE INDENT main ( casenum ) NEW_LINE DEDENT if infile is not sys . stdin : NEW_LINE INDENT infile . close ( ) NEW_LINE DEDENT if outfile is not sys . stdout : NEW_LINE INDENT outfile . close ( ) NEW_LINE DEDENT DEDENT


def run ( x , r , c , k ) : NEW_LINE INDENT a = [ [ 0 ] * c for i in range ( r ) ] NEW_LINE for i in range ( r ) : NEW_LINE INDENT for j in range ( ( i & 1 ) ^ x , c , 2 ) : NEW_LINE INDENT if i > 0 : NEW_LINE INDENT a [ i - 1 ] [ j ] += 1 NEW_LINE DEDENT if j > 0 : NEW_LINE INDENT a [ i ] [ j - 1 ] += 1 NEW_LINE DEDENT if i + 1 < r : NEW_LINE INDENT a [ i + 1 ] [ j ] += 1 NEW_LINE DEDENT if j + 1 < c : NEW_LINE INDENT a [ i ] [ j + 1 ] += 1 NEW_LINE DEDENT DEDENT DEDENT b = [ j for i in a for j in i ] NEW_LINE b . sort ( ) NEW_LINE return sum ( b [ : k ] ) NEW_LINE DEDENT n = input ( ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT r , c , k = [ int ( x ) for x in raw_input ( ) . split ( ) ] NEW_LINE ans = min ( run ( 0 , r , c , k ) , run ( 1 , r , c , k ) ) NEW_LINE print " Case ▁ # % d : ▁ % d " % ( i , ans ) NEW_LINE DEDENT


from functools import * NEW_LINE inf = open ( ' B - large . in ' ) NEW_LINE ouf = open ( ' B - large . out ' , ' w ' ) NEW_LINE input = lambda : inf . readline ( ) . strip ( ) NEW_LINE print = partial ( print , file = ouf ) NEW_LINE def solve ( ) : NEW_LINE INDENT r , c , n = map ( int , input ( ) . split ( ) ) NEW_LINE if r > c : NEW_LINE INDENT r , c = c , r NEW_LINE DEDENT if n <= ( r * c ) // 2 + ( r * c ) % 2 : NEW_LINE INDENT print ( 0 ) NEW_LINE return NEW_LINE DEDENT free = r * c - n NEW_LINE cur = ( r - 1 ) * c + ( c - 1 ) * r NEW_LINE if r == 1 : NEW_LINE INDENT if c % 2 == 0 : NEW_LINE INDENT ways = [ [ 2 ] * ( c // 2 - 1 ) + [ 1 ] ] NEW_LINE DEDENT else : NEW_LINE INDENT ways = [ [ 2 ] * ( c // 2 ) , [ 2 ] * ( c // 2 - 1 ) + [ 1 , 1 ] ] NEW_LINE DEDENT DEDENT elif r * c % 2 == 0 : NEW_LINE INDENT ways = [ [ 4 ] * ( ( r - 2 ) * ( c - 2 ) // 2 ) + [ 3 ] * ( r - 2 + c - 2 ) + [ 2 , 2 ] ] NEW_LINE DEDENT else : NEW_LINE INDENT ways = [ [ 4 ] * ( ( r - 2 ) * ( c - 2 ) // 2 + 1 ) + [ 3 ] * ( r - 3 + c - 3 ) + [ 2 ] * 4 , [ 4 ] * ( ( r - 2 ) * ( c - 2 ) // 2 ) + [ 3 ] * ( r - 1 + c - 1 ) , ] NEW_LINE DEDENT print ( cur - max ( sum ( way [ : free ] ) for way in ways ) ) NEW_LINE DEDENT tests = int ( input ( ) ) NEW_LINE for z in range ( tests ) : NEW_LINE INDENT print ( " Case ▁ # { } : ▁ " . format ( z + 1 ) , end = ' ' ) NEW_LINE solve ( ) NEW_LINE DEDENT ouf . close ( ) NEW_LINE


import sys NEW_LINE def debug ( * args ) : NEW_LINE INDENT print ( * args , file = sys . stderr ) NEW_LINE DEDENT def permutations ( n , k ) : NEW_LINE INDENT if k == 0 : NEW_LINE INDENT yield [ False ] * n NEW_LINE return NEW_LINE DEDENT for p in permutations ( n - 1 , k - 1 ) : NEW_LINE INDENT yield p + [ True ] NEW_LINE DEDENT if n > k : NEW_LINE INDENT for p in permutations ( n - 1 , k ) : NEW_LINE INDENT yield p + [ False ] NEW_LINE DEDENT DEDENT DEDENT def cost ( p , r , c ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( r ) : NEW_LINE INDENT for j in range ( c - 1 ) : NEW_LINE INDENT if p [ i * c + j ] and p [ i * c + j + 1 ] : NEW_LINE INDENT result += 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( r - 1 ) : NEW_LINE INDENT for j in range ( c ) : NEW_LINE INDENT if p [ i * c + j ] and p [ i * c + j + c ] : NEW_LINE INDENT result += 1 NEW_LINE DEDENT DEDENT DEDENT return result NEW_LINE DEDENT fin = sys . stdin NEW_LINE T = int ( fin . readline ( ) ) NEW_LINE for case in range ( 1 , T + 1 ) : NEW_LINE INDENT R , C , N = map ( int , fin . readline ( ) . split ( ) ) NEW_LINE best = R * C * 10 NEW_LINE for p in permutations ( R * C , N ) : NEW_LINE INDENT best = min ( best , cost ( p , R , C ) ) NEW_LINE DEDENT result = best NEW_LINE print ( " Case ▁ # % d : ▁ % s " % ( case , result ) ) NEW_LINE DEDENT


import java . util . * ; public class B { String solve ( Scanner sc ) { int R = sc . nextInt ( ) ; int C = sc . nextInt ( ) ; int N = sc . nextInt ( ) ; int min = Integer . MAX_VALUE ; for ( int i = 0 ; i < 1 << ( R * C ) ; i ++ ) { if ( Integer . bitCount ( i ) == N ) { int s = 0 ; boolean [ ] [ ] b = new boolean [ R ] [ C ] ; for ( int r = 0 ; r < R ; r ++ ) { for ( int c = 0 ; c < C ; c ++ ) { b [ r ] [ c ] = ( i & ( 1 << ( r * C + c ) ) ) != 0 ; if ( b [ r ] [ c ] ) { if ( r > 0 && b [ r - 1 ] [ c ] ) { s ++ ; } if ( c > 0 && b [ r ] [ c - 1 ] ) { s ++ ; } } } } min = Math . min ( min , s ) ; } } return " " + min ; } public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int T = sc . nextInt ( ) ; for ( int cs = 1 ; cs <= T ; cs ++ ) { String res = new B ( ) . solve ( sc ) ; System . out . println ( " Case ▁ # " + cs + " : ▁ " + res ) ; } sc . close ( ) ; } }


import java . util . * ; public class NN { static Scanner sc = new Scanner ( System . in ) ; static int k ; { k = sc . nextInt ( ) ; for ( int kk = 1 ; kk <= k ; kk ++ ) { System . out . print ( " Case ▁ # " + kk + " : ▁ " ) ; int r = sc . nextInt ( ) ; int c = sc . nextInt ( ) ; int n = sc . nextInt ( ) ; int a = val ( r , c , n ) ; System . out . println ( val ( r , c , n ) ) ; } } static int thing ( int r , int c , int n , int basegood , int corn , int edg ) { if ( n <= basegood ) return 0 ; n -= basegood ; if ( n <= corn ) return 2 * n ; int result = 2 * corn ; n -= corn ; if ( n <= edg ) return result + 3 * n ; result += 3 * edg ; n -= edg ; return result + 4 * n ; } static int special ( int size , int n ) { if ( size % 2 == 0 ) { if ( n <= size / 2 ) return 0 ; return 2 * ( n - size / 2 ) - 1 ; } else { if ( n <= size / 2 + 1 ) return 0 ; return 2 * ( n - size / 2 - 1 ) ; } } static int val ( int r , int c , int n ) { if ( r == 1 ) return special ( c , n ) ; if ( c == 1 ) return special ( r , n ) ; if ( ( r * c ) % 2 == 0 ) return thing ( r , c , n , r * c / 2 , 2 , r + c - 4 ) ; else return Math . min ( thing ( r , c , n , r * c / 2 + 1 , 0 , r + c - 2 ) , thing ( r , c , n , r * c / 2 , 4 , r + c - 6 ) ) ; } public static void main ( String [ ] args ) { new NN ( ) ; } }


import java . io . BufferedWriter ; import java . io . File ; import java . io . FileWriter ; import java . io . PrintWriter ; import java . util . Scanner ; public class b { public static void main ( String [ ] Args ) throws Exception { Scanner sc = new Scanner ( new File ( " B - large . in " ) ) ; PrintWriter out = new PrintWriter ( new BufferedWriter ( new FileWriter ( new File ( " b . out " ) ) ) ) ; int cc = 0 ; int t = sc . nextInt ( ) ; while ( t -- > 0 ) { String x = sc . next ( ) ; for ( int i = 0 ; i < x . length ( ) ; i ++ ) for ( int j = 0 ; j + 1 < x . length ( ) ; j ++ ) if ( x . charAt ( j ) > x . charAt ( j + 1 ) ) { String nx = x . substring ( 0 , j ) ; nx = nx + ( char ) ( x . charAt ( j ) - 1 ) ; while ( nx . length ( ) < x . length ( ) ) nx = nx + "9" ; x = nx ; } while ( x . length ( ) > 1 && x . charAt ( 0 ) == '0' ) x = x . substring ( 1 ) ; out . printf ( " Case ▁ # % d : ▁ % s % n " , ++ cc , x ) ; } out . close ( ) ; } }

def tidy ( N ) : NEW_LINE INDENT return list ( N ) == sorted ( N ) NEW_LINE DEDENT def solve ( N ) : NEW_LINE INDENT if N == '0' : NEW_LINE INDENT return ' ' NEW_LINE DEDENT if tidy ( N ) or len ( N ) <= 1 : NEW_LINE INDENT return N NEW_LINE DEDENT return solve ( str ( int ( N [ : - 1 ] ) - 1 ) ) + '9' NEW_LINE DEDENT T = int ( input ( ) ) NEW_LINE for case_number in range ( 1 , T + 1 ) : NEW_LINE INDENT N = input ( ) NEW_LINE print ( ' Case ▁ # % d : ' % case_number , solve ( N ) ) NEW_LINE DEDENT


infilecode = " BLI " NEW_LINE import sys NEW_LINE mapping = { " A " : " A " , " B " : " B " , " C " : " C " , " D " : " D " , " E " : " E " , " X " : " example " , " S " : " - small " , " L " : " - large " , " P " : " - practice " , "0" : " - attempt0" , "1" : " - attempt1" , "2" : " - attempt2" , " I " : " . in " , " T " : " . txt " } NEW_LINE infile = " " . join ( mapping [ c ] for c in infilecode ) NEW_LINE outfile = infile . replace ( " . in " , " " ) + " . out . txt " NEW_LINE sys . stdin = open ( infile , ' r ' ) NEW_LINE output = open ( outfile , ' w ' ) NEW_LINE T = int ( input ( ) ) NEW_LINE for case in range ( 1 , T + 1 ) : NEW_LINE INDENT N = int ( input ( ) ) NEW_LINE print ( N ) NEW_LINE a = str ( N ) NEW_LINE l = len ( a ) NEW_LINE for i in range ( l - 1 , 0 , - 1 ) : NEW_LINE INDENT if a [ i ] >= a [ i - 1 ] : NEW_LINE INDENT continue NEW_LINE DEDENT a = a [ : i ] + "0" * ( l - i ) NEW_LINE N = int ( a ) - 1 NEW_LINE a = str ( N ) NEW_LINE DEDENT answer = a NEW_LINE print ( " Case ▁ # % d : " % case , answer ) NEW_LINE print ( " Case ▁ # % d : " % case , answer , file = output ) NEW_LINE DEDENT


def solve ( n ) : NEW_LINE INDENT digits = [ ] NEW_LINE while n : NEW_LINE INDENT digits . append ( n % 10 ) NEW_LINE n //= 10 NEW_LINE DEDENT digits = list ( reversed ( digits ) ) NEW_LINE ans = None NEW_LINE for i in range ( len ( digits ) ) : NEW_LINE INDENT if i and digits [ i ] < digits [ i - 1 ] : NEW_LINE INDENT break NEW_LINE DEDENT if i == 0 or digits [ i ] - 1 >= digits [ i - 1 ] : NEW_LINE INDENT if digits [ i ] - 1 == 0 : NEW_LINE INDENT ans = [ 9 ] * ( len ( digits ) - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT ans = digits [ : i ] + [ digits [ i ] - 1 ] + [ 9 ] * ( len ( digits ) - i - 1 ) NEW_LINE DEDENT DEDENT if i == len ( digits ) - 1 : NEW_LINE INDENT ans = digits NEW_LINE DEDENT DEDENT return ' ' . join ( map ( str , ans ) ) NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT ntest = int ( input ( ) ) NEW_LINE for i in range ( ntest ) : NEW_LINE INDENT n = int ( input ( ) ) NEW_LINE ans = solve ( n ) NEW_LINE print ( " Case ▁ # % d : ▁ % s " % ( i + 1 , ans ) ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE NEW_LINE DEDENT


IN = open ( ' input . txt ' , ' r ' ) NEW_LINE OUT = open ( ' output . txt ' , ' w ' ) NEW_LINE NUM_TESTS = int ( IN . readline ( ) ) NEW_LINE for test in xrange ( NUM_TESTS ) : NEW_LINE INDENT N = int ( IN . readline ( ) ) NEW_LINE while True : NEW_LINE INDENT l = [ int ( c ) for c in str ( N ) ] NEW_LINE for i in xrange ( len ( l ) - 1 ) : NEW_LINE INDENT if l [ i ] > l [ i + 1 ] : NEW_LINE INDENT N = int ( str ( N ) [ : i + 1 ] + '0' * ( len ( l ) - i - 1 ) ) - 1 NEW_LINE break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT OUT . write ( ' Case ▁ # { } : ▁ { } \n ' . format ( test + 1 , N ) ) NEW_LINE print test + 1 , N NEW_LINE DEDENT IN . close ( ) NEW_LINE OUT . close ( ) NEW_LINE


from __future__ import print_function NEW_LINE import sys NEW_LINE from sys import stdin NEW_LINE def eprint ( * args , ** kwargs ) : NEW_LINE INDENT print ( * args , file = sys . stderr , ** kwargs ) NEW_LINE DEDENT def ln ( f = int ) : NEW_LINE INDENT return list ( map ( f , stdin . readline ( ) . strip ( ) . split ( ) ) ) NEW_LINE DEDENT T , = ln ( ) NEW_LINE INF = float ( ' inf ' ) NEW_LINE for test in range ( T ) : NEW_LINE INDENT s , = ln ( str ) NEW_LINE s = list ( map ( int , list ( s ) ) ) NEW_LINE prev = 9 NEW_LINE res = " " NEW_LINE ind = None NEW_LINE for i in range ( 1 , len ( s ) ) : NEW_LINE INDENT if s [ i ] < s [ i - 1 ] : NEW_LINE INDENT ind = i NEW_LINE break NEW_LINE DEDENT DEDENT if ind != None : NEW_LINE INDENT for i in range ( ind + 1 , len ( s ) ) : NEW_LINE INDENT s [ i ] = 9 NEW_LINE DEDENT while ind != 0 and s [ ind ] < s [ ind - 1 ] : NEW_LINE INDENT s [ ind - 1 ] -= 1 NEW_LINE s [ ind ] = 9 NEW_LINE ind -= 1 NEW_LINE DEDENT DEDENT res = int ( " " . join ( list ( map ( str , s ) ) ) ) NEW_LINE print ( " Case ▁ # " + str ( test + 1 ) + " : ▁ " + str ( res ) ) NEW_LINE DEDENT


import java . io . * ; import java . util . StringTokenizer ; public class B { private String solveTest ( ) throws IOException { String s = next ( ) ; int i = 0 ; while ( i < s . length ( ) - 1 && s . charAt ( i + 1 ) >= s . charAt ( i ) ) { i ++ ; } if ( i == s . length ( ) - 1 ) return s ; while ( i > 0 && s . charAt ( i - 1 ) == s . charAt ( i ) ) i -- ; String res = " " ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { res += j < i ? s . charAt ( j ) : j == i ? ( char ) ( s . charAt ( j ) - 1 ) : '9' ; } if ( res . charAt ( 0 ) == '0' ) res = res . substring ( 1 ) ; return res ; } private void solve ( ) throws IOException { int n = nextInt ( ) ; for ( int i = 0 ; i < n ; i ++ ) { String res = solveTest ( ) ; System . out . println ( " Case ▁ # " + ( i + 1 ) + " : ▁ " + res ) ; out . println ( " Case ▁ # " + ( i + 1 ) + " : ▁ " + res ) ; } } BufferedReader br ; StringTokenizer st ; PrintWriter out ; String next ( ) throws IOException { while ( st == null || ! st . hasMoreTokens ( ) ) { st = new StringTokenizer ( br . readLine ( ) ) ; } return st . nextToken ( ) ; } int nextInt ( ) throws IOException { return Integer . parseInt ( next ( ) ) ; } public static void main ( String [ ] args ) throws FileNotFoundException { new B ( ) . run ( ) ; } private void run ( ) throws FileNotFoundException { br = new BufferedReader ( new FileReader ( this . getClass ( ) . getSimpleName ( ) . substring ( 0 , 1 ) + " . in " ) ) ; out = new PrintWriter ( this . getClass ( ) . getSimpleName ( ) . substring ( 0 , 1 ) + " . out " ) ; try { solve ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } out . close ( ) ; } }


package qualification ; import java . io . * ; import java . util . * ; public class B_TidyNumbers { private static final String FILENAME = " B - large " ; private static final boolean STANDARD_OUTPUT = false ; public static void main ( String [ ] args ) throws Throwable { try ( BufferedReader in = new BufferedReader ( new FileReader ( FILENAME + " . in " ) ) ) { try ( PrintStream out = ! STANDARD_OUTPUT ? new PrintStream ( FILENAME + " . out " ) : System . out ) { for ( int t = 1 , T = Integer . parseInt ( in . readLine ( ) ) ; t <= T ; t ++ ) { char [ ] N = in . readLine ( ) . toCharArray ( ) ; int i = 1 , k = 0 ; for ( i = 1 ; i < N . length && N [ i ] >= N [ i - 1 ] ; i ++ ) { } if ( i < N . length ) { for ( k = i - 1 ; k >= 0 && N [ k ] == N [ i - 1 ] ; k -- ) { } N [ ++ k ] -- ; Arrays . fill ( N , k + 1 , N . length , '9' ) ; k = k == 0 && N [ 0 ] == '0' ? 1 : 0 ; } out . println ( " Case ▁ # " + t + " : ▁ " + new String ( N , k , N . length - k ) ) ; } } } } }


package com . company ; import java . io . FileInputStream ; import java . io . FileNotFoundException ; import java . io . FileOutputStream ; import java . io . PrintStream ; import java . util . Arrays ; import java . util . Scanner ; public class ProbB { private Scanner scanner = new Scanner ( System . in ) ; public void main ( ) { reDirect ( ) ; int T = scanner . nextInt ( ) ; for ( int cas = 1 ; cas <= T ; cas ++ ) { long ans = run ( ) ; System . out . println ( " Case ▁ # " + cas + " : ▁ " + ans ) ; } } private long run ( ) { long input = scanner . nextLong ( ) ; char [ ] arr = Long . valueOf ( input ) . toString ( ) . toCharArray ( ) ; int x = arr . length ; for ( int i = 0 ; i < arr . length - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) { x = i + 1 ; break ; } } if ( x == arr . length ) { return input ; } int y = x - 1 ; while ( y > 0 && arr [ y ] == arr [ y - 1 ] ) y -- ; arr [ y ] -- ; for ( int i = y + 1 ; i < arr . length ; i ++ ) { arr [ i ] = '9' ; } return Long . valueOf ( new String ( arr ) ) ; } private void reDirect ( ) { try { FileInputStream fileInputStream = new FileInputStream ( " B - large . in " ) ; scanner = new Scanner ( fileInputStream ) ; PrintStream ps = new PrintStream ( new FileOutputStream ( " bout - large . txt " ) ) ; System . setOut ( ps ) ; } catch ( FileNotFoundException e ) { e . printStackTrace ( ) ; } } }


package qualifier ; import java . io . BufferedReader ; import java . io . InputStreamReader ; import java . util . Scanner ; public class BTidyNumbers { public static void main ( String [ ] args ) { try ( Scanner sc = new Scanner ( new BufferedReader ( new InputStreamReader ( System . in ) ) ) ) { int tests = sc . nextInt ( ) ; for ( int t = 1 ; t <= tests ; t ++ ) { int [ ] digits = sc . next ( ) . chars ( ) . map ( i -> i - '0' ) . toArray ( ) ; for ( int x = 0 ; x < digits . length - 1 ; x ++ ) { if ( digits [ x ] > digits [ x + 1 ] ) { digits [ x ] -- ; for ( int y = x + 1 ; y < digits . length ; y ++ ) digits [ y ] = 9 ; while ( digits [ x ] < 0 || ( x > 0 && digits [ x ] < digits [ x - 1 ] ) ) { digits [ x ] = 9 ; digits [ x - 1 ] -- ; x -- ; } break ; } } int start = 0 ; while ( digits [ start ] == 0 ) start ++ ; StringBuilder sb = new StringBuilder ( ) ; for ( int x = start ; x < digits . length ; x ++ ) sb . append ( ( char ) ( digits [ x ] + '0' ) ) ; System . out . printf ( " Case ▁ # % d : ▁ % s % n " , t , sb . toString ( ) ) ; } } } }


import java . util . Scanner ; public class B { static Scanner sc = new Scanner ( System . in ) ; static int N ; static long P ; public static void main ( String [ ] args ) throws Exception { int T = sc . nextInt ( ) ; for ( int t = 1 ; t <= T ; ++ t ) { N = sc . nextInt ( ) ; P = sc . nextLong ( ) ; System . out . println ( " Case ▁ # " + t + " : ▁ " + ( solveGuarantee ( 1L << N , P ) - 1 ) + " ▁ " + ( solveCould ( 1L << N , P ) - 1 ) ) ; } } static long solveGuarantee ( long n , long p ) { if ( p == n ) return n ; if ( p <= n / 2 ) return 1 ; return solveGuarantee ( n / 2 , p - n / 2 ) * 2 + 1 ; } static long solveCould ( long n , long p ) { if ( p == n ) return n ; if ( p >= n / 2 ) return n - 1 ; long c = solveCould ( n / 2 , p ) ; return c + c - 1 ; } }

def lpos ( N , L ) : NEW_LINE INDENT return 2 ** N - 2 ** ( N - L ) NEW_LINE DEDENT def wpos ( N , W ) : NEW_LINE INDENT return 2 ** ( N - W ) - 1 NEW_LINE DEDENT def hforl ( N , L ) : NEW_LINE INDENT r = 0 NEW_LINE for i in xrange ( L ) : NEW_LINE INDENT r = 2 * r + 2 NEW_LINE DEDENT return min ( 2 ** N - 1 , r ) NEW_LINE DEDENT def hforw ( N , W ) : NEW_LINE INDENT r = 0 NEW_LINE for i in xrange ( W ) : NEW_LINE INDENT r = 2 * r + 1 NEW_LINE DEDENT return max ( 0 , 2 ** N - r - 1 ) NEW_LINE DEDENT def solve ( N , P ) : NEW_LINE INDENT maxl = 0 NEW_LINE while maxl <= N and lpos ( N , maxl ) < P : maxl += 1 NEW_LINE maxl -= 1 NEW_LINE guaranteed = hforl ( N , maxl ) NEW_LINE minw = N NEW_LINE while minw >= 0 and wpos ( N , minw ) < P : minw -= 1 NEW_LINE minw += 1 NEW_LINE could = hforw ( N , minw ) NEW_LINE return ( guaranteed , could ) NEW_LINE DEDENT T = input ( ) NEW_LINE for t in xrange ( T ) : NEW_LINE INDENT N , P = map ( int , raw_input ( ) . split ( ) ) NEW_LINE s = solve ( N , P ) NEW_LINE print ' Case ▁ # % s : ▁ % s ▁ % s ' % ( t + 1 , s [ 0 ] , s [ 1 ] ) NEW_LINE DEDENT


code = " B - large " NEW_LINE infile = code + " . in " NEW_LINE outfile = code + " . out " NEW_LINE def willwin ( n , s ) : NEW_LINE INDENT pos = 0 NEW_LINE for i in xrange ( n ) : NEW_LINE INDENT if s > 0 : NEW_LINE INDENT pos += 2 ** ( n - i - 1 ) NEW_LINE s = ( s - 1 ) // 2 NEW_LINE DEDENT DEDENT return pos NEW_LINE DEDENT def maywin ( n , w ) : NEW_LINE INDENT pos = 0 NEW_LINE for i in xrange ( n ) : NEW_LINE INDENT if w > 0 : NEW_LINE INDENT w = ( w - 1 ) // 2 NEW_LINE DEDENT else : NEW_LINE INDENT pos += 2 ** ( n - i - 1 ) NEW_LINE DEDENT DEDENT return pos NEW_LINE DEDENT def solve ( n , p ) : NEW_LINE INDENT if p == 2 ** n : NEW_LINE INDENT return p - 1 , p - 1 NEW_LINE DEDENT low , high = 0 , 2 ** n - 1 NEW_LINE while high - low > 1 : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE if willwin ( n , mid ) < p : NEW_LINE INDENT low = mid NEW_LINE DEDENT else : NEW_LINE INDENT high = mid NEW_LINE DEDENT DEDENT will = low NEW_LINE low , high = 0 , 2 ** n - 1 NEW_LINE while high - low > 1 : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE if maywin ( n , 2 ** n - mid - 1 ) < p : NEW_LINE INDENT low = mid NEW_LINE DEDENT else : NEW_LINE INDENT high = mid NEW_LINE DEDENT DEDENT may = low NEW_LINE return will , may NEW_LINE DEDENT lines = [ s . strip ( ) for s in open ( infile ) ] NEW_LINE c = int ( lines [ 0 ] ) NEW_LINE f = open ( outfile , " w " ) NEW_LINE lineno = 1 NEW_LINE for i in range ( 1 , c + 1 ) : NEW_LINE INDENT n , p = map ( int , lines [ lineno ] . split ( ) ) NEW_LINE r = solve ( n , p ) NEW_LINE lineno += 1 NEW_LINE msg = " Case ▁ # { 0 } : ▁ { 1[0 ] } ▁ { 1[1 ] } " . format ( i , r ) NEW_LINE print msg NEW_LINE print >> f , msg NEW_LINE DEDENT f . close ( ) NEW_LINE


import sys NEW_LINE import re NEW_LINE import math NEW_LINE import string NEW_LINE f = open ( sys . argv [ 1 ] , ' r ' ) NEW_LINE num = int ( f . readline ( ) ) NEW_LINE for i in xrange ( num ) : NEW_LINE INDENT l = f . readline ( ) NEW_LINE N , P = [ int ( x ) for x in l . split ( ) ] NEW_LINE if 2 ** N == P : NEW_LINE INDENT print ' Case ▁ # { } : ' . format ( i + 1 ) , 2 ** N - 1 , 2 ** N - 1 NEW_LINE continue NEW_LINE DEDENT j = 0 NEW_LINE while P >= 2 ** j : NEW_LINE INDENT j += 1 NEW_LINE DEDENT j -= 1 NEW_LINE k = 1 NEW_LINE while P > 2 ** N - 2 ** ( N - k ) : NEW_LINE INDENT k += 1 NEW_LINE DEDENT print ' Case ▁ # { } : ' . format ( i + 1 ) , 2 ** k - 2 , 2 ** N - 2 ** ( N - j ) NEW_LINE DEDENT


from sys import * NEW_LINE from heapq import * NEW_LINE import math NEW_LINE def write ( msg ) : NEW_LINE INDENT stdout . write ( msg ) NEW_LINE fo . write ( msg ) NEW_LINE DEDENT def writeln ( msg ) : NEW_LINE INDENT write ( str ( msg ) + ' \n ' ) NEW_LINE DEDENT def readint ( ) : NEW_LINE INDENT return int ( fi . readline ( ) ) NEW_LINE DEDENT def readints ( ) : NEW_LINE INDENT return [ int ( X ) for X in fi . readline ( ) . split ( ) ] NEW_LINE DEDENT def readstr ( ) : NEW_LINE INDENT return fi . readline ( ) . rstrip ( ) NEW_LINE DEDENT fni = " % s - % s - % s . in " % ( argv [ 1 ] , argv [ 2 ] , argv [ 3 ] ) NEW_LINE fno = " % s - % s - % s . out " % ( argv [ 1 ] , argv [ 2 ] , argv [ 3 ] ) NEW_LINE fi = open ( fni , ' r ' ) NEW_LINE fo = open ( fno , ' w ' ) NEW_LINE def ffail ( start , step , num , prizes ) : NEW_LINE INDENT if prizes >= num : NEW_LINE INDENT return start + step * ( num - 1 ) + 1 NEW_LINE DEDENT if prizes <= 0 : NEW_LINE INDENT return start NEW_LINE DEDENT if 2 * prizes <= num : NEW_LINE INDENT return start + step NEW_LINE DEDENT return ffail ( start + step , step * 2 , num / 2 , prizes - num / 2 ) NEW_LINE DEDENT def wwin ( start , step , num , prizes ) : NEW_LINE INDENT if prizes >= num : NEW_LINE INDENT return start + step * ( num - 1 ) NEW_LINE DEDENT if prizes <= 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT return wwin ( start , step * 2 , num / 2 , prizes ) NEW_LINE DEDENT num_cases = readint ( ) NEW_LINE for case in range ( 1 , 1 + num_cases ) : NEW_LINE INDENT ( N , P ) = readints ( ) NEW_LINE g = ffail ( 0 , 1 , 2 ** N , P ) - 1 NEW_LINE w = wwin ( 0 , 1 , 2 ** N , P ) NEW_LINE writeln ( " Case ▁ # % d : ▁ % d ▁ % d " % ( case , g , w ) ) NEW_LINE DEDENT


def reader ( inFile ) : NEW_LINE INDENT return tuple ( inFile . getInts ( ) ) NEW_LINE DEDENT def solver ( ( N , p ) ) : NEW_LINE INDENT wins = [ ( ( ( p - 1 ) >> i ) & 1 ) == 0 for i in xrange ( N ) ] [ : : - 1 ] NEW_LINE a1 = 0 NEW_LINE for win in wins : NEW_LINE INDENT if not win : NEW_LINE INDENT a1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT a1 = ( ( 1 << ( a1 + 1 ) ) - 2 ) if ( a1 < N ) else ( ( 1 << N ) - 1 ) NEW_LINE a2 = 0 NEW_LINE for win in wins : NEW_LINE INDENT if win : NEW_LINE INDENT a2 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( a2 == len ( [ win for win in wins if win ] ) ) : NEW_LINE INDENT a2 -= 1 NEW_LINE DEDENT a2 = ( 1 << N ) - ( 1 << ( a2 + 1 ) ) NEW_LINE return " % d ▁ % d " % ( a1 , a2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT from GCJ import GCJ NEW_LINE GCJ ( reader , solver , " / Users / lpebody / gcj / 2013_2 / b / " , " b " ) . run ( ) NEW_LINE DEDENT


package contest ; import java . util . * ; import java . io . * ; public class ManyPrizes { final static String PROBLEM_NAME = " mprizes " ; final static String WORK_DIR = " D : \\ Gcj\ \" + PROBLEM_NAME + " \ \" ; void solve ( Scanner sc , PrintWriter pw ) { int N = sc . nextInt ( ) ; long P = sc . nextLong ( ) ; long ans1 = - 1 ; long sum = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { sum += ( 1L << i ) ; if ( P <= sum ) { ans1 = ( 1L << ( N - i ) ) - 1 ; break ; } } if ( ans1 == - 1 ) ans1 = ( 1L << N ) ; long ans2 = - 1 ; int needWins = 0 ; while ( ( 1L << ( N - needWins ) ) > P ) needWins ++ ; ans2 = ( 1L << N ) - ( 1L << needWins ) + 1 ; ans1 -- ; ans2 -- ; pw . println ( ans1 + " ▁ " + ans2 ) ; } public static void main ( String [ ] args ) throws Exception { Scanner sc = new Scanner ( new FileReader ( WORK_DIR + " input . txt " ) ) ; PrintWriter pw = new PrintWriter ( new FileWriter ( WORK_DIR + " output . txt " ) ) ; int caseCnt = sc . nextInt ( ) ; for ( int caseNum = 0 ; caseNum < caseCnt ; caseNum ++ ) { System . out . println ( " Processing ▁ test ▁ case ▁ " + ( caseNum + 1 ) ) ; pw . print ( " Case ▁ # " + ( caseNum + 1 ) + " : ▁ " ) ; new ManyPrizes ( ) . solve ( sc , pw ) ; } pw . flush ( ) ; pw . close ( ) ; sc . close ( ) ; } }


import java . io . * ; import java . math . BigInteger ; import java . util . * ; public class B { void solve ( ) throws IOException { int t = nextInt ( ) ; for ( int testCase = 0 ; testCase < t ; testCase ++ ) { long n = nextInt ( ) ; long p = nextLong ( ) ; long max = 1l << n ; long a1 = 0 , a2 = max - 2 ; if ( p == 1 ) { a1 = a2 = 0 ; } else if ( p == max ) { a1 = a2 = max - 1 ; } else { long x = max >> 1l ; long y = x >> 1l ; long z = 2 ; while ( p > x ) { a1 += z ; z <<= 1l ; x |= y ; y >>= 1l ; } x = max >> 1l ; z = 2 ; while ( p < x ) { a2 -= z ; z <<= 1l ; x >>= 1l ; } } out . printf ( " Case ▁ # % d : ▁ % d ▁ % d \n " , testCase + 1 , a1 , a2 ) ; } } public static void main ( String [ ] args ) throws IOException { new B ( ) . run ( ) ; } void run ( ) throws IOException { reader = new BufferedReader ( new FileReader ( " input . txt " ) ) ; out = new PrintWriter ( new FileWriter ( " output . txt " ) ) ; tokenizer = null ; solve ( ) ; reader . close ( ) ; out . flush ( ) ; } BufferedReader reader ; StringTokenizer tokenizer ; PrintWriter out ; int nextInt ( ) throws IOException { return Integer . parseInt ( nextToken ( ) ) ; } long nextLong ( ) throws IOException { return Long . parseLong ( nextToken ( ) ) ; } double nextDouble ( ) throws IOException { return Double . parseDouble ( nextToken ( ) ) ; } String nextToken ( ) throws IOException { while ( tokenizer == null || ! tokenizer . hasMoreTokens ( ) ) { tokenizer = new StringTokenizer ( reader . readLine ( ) ) ; } return tokenizer . nextToken ( ) ; } }


import java . math . BigInteger ; import java . util . * ; public class B { static BigInteger B ( long n ) { return BigInteger . valueOf ( n ) ; } static BigInteger A ( BigInteger A , BigInteger B ) { return A . add ( B ) ; } static BigInteger M ( BigInteger A , BigInteger B ) { return A . multiply ( B ) ; } public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; int T = in . nextInt ( ) ; for ( int cas = 1 ; cas <= T ; cas ++ ) { int n = in . nextInt ( ) ; BigInteger P = new BigInteger ( in . next ( ) ) ; BigInteger last_pos = B ( 2 ) . pow ( n ) . subtract ( P ) ; int min_ones = 0 ; for ( min_ones = 0 ; min_ones <= n ; min_ones ++ ) { BigInteger val = B ( 0 ) ; for ( int i = 0 ; i < min_ones ; i ++ ) val = A ( val , B ( 2 ) . pow ( n - 1 - i ) ) ; if ( val . compareTo ( last_pos ) >= 0 ) break ; } BigInteger last_win = B ( 2 ) . pow ( n ) . subtract ( B ( 2 ) . pow ( min_ones ) ) ; int min_zeroes = 0 ; for ( min_zeroes = 0 ; min_zeroes <= n ; min_zeroes ++ ) { BigInteger val = B ( 2 ) . pow ( n ) . subtract ( B ( 1 ) ) ; for ( int i = 0 ; i < min_zeroes ; i ++ ) val = val . subtract ( B ( 2 ) . pow ( n - 1 - i ) ) ; if ( val . compareTo ( last_pos ) < 0 ) break ; } BigInteger last_guarantee = B ( 2 ) . pow ( min_zeroes ) . subtract ( B ( 2 ) ) ; if ( last_pos . equals ( B ( 0 ) ) ) last_guarantee = B ( 2 ) . pow ( n ) . subtract ( B ( 1 ) ) ; System . out . printf ( " Case ▁ # % d : ▁ % s ▁ % s \n " , cas , last_guarantee , last_win ) ; } } }


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 ++ ) { String s = " Case ▁ # " + ( i + 1 ) + " : ▁ " + new B ( ) . solve ( in ) ; out . println ( s ) ; System . out . println ( s ) ; } out . close ( ) ; } private String solve ( Scanner in ) { int n = in . nextInt ( ) ; long p = in . nextLong ( ) ; p -- ; long b = 1 ; if ( p == ( ( 1L << n ) - 1 ) ) { b = ( ( 1L << n ) - 1 ) + 2 ; } else { long pp = ( 1L << n ) - 1 - ( p + 1 ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( ( ( pp >> i ) & 1 ) == 0 ) { b *= 2 ; } else { for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( ( ( pp >> j ) & 1 ) == 0 ) { b *= 2 ; break ; } } break ; } } } long pp = ( 1L << n ) - 1 - p ; long c = 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( ( ( pp >> i ) & 1 ) == 1 ) { c *= 2 ; } else { for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( ( ( pp >> j ) & 1 ) == 1 ) { c *= 2 ; break ; } } break ; } } return ( b - 2 ) + " ▁ " + ( ( 1L << n ) - c ) ; } }


import java . util . * ; import static java . lang . Math . * ; public class B { static void p ( Object ... o ) { System . out . println ( Arrays . deepToString ( o ) ) ; } public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; int T = in . nextInt ( ) ; for ( int zz = 1 ; zz <= T ; zz ++ ) { int N = in . nextInt ( ) ; int [ ] A = new int [ N ] ; int [ ] B = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { A [ i ] = in . nextInt ( ) ; B [ i ] = in . nextInt ( ) ; } int [ ] have = new int [ N ] ; int stars = 0 ; int ans = 0 ; next : while ( stars < 2 * N ) { int best = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { if ( have [ i ] == 2 ) continue ; if ( stars >= B [ i ] ) { stars += 2 - have [ i ] ; have [ i ] = 2 ; ans ++ ; continue next ; } if ( have [ i ] == 1 ) continue ; if ( stars < A [ i ] ) continue ; if ( best == - 1 || B [ i ] > B [ best ] ) { best = i ; } } if ( best == - 1 ) break ; have [ best ] = 1 ; ans ++ ; stars ++ ; } if ( stars == 2 * N ) System . out . format ( " Case ▁ # % d : ▁ % d \n " , zz , ans ) ; else System . out . format ( " Case ▁ # % d : ▁ Too ▁ Bad \n " , zz , ans ) ; } } }

import heapq NEW_LINE def alphabet ( n , z ) : NEW_LINE INDENT coins = 0 NEW_LINE turns = 0 NEW_LINE coined = [ 0 ] * n NEW_LINE while coins < 2 * n : NEW_LINE INDENT won = False NEW_LINE for i in xrange ( n ) : NEW_LINE INDENT if z [ i ] [ 1 ] <= coins and coined [ i ] < 2 : NEW_LINE INDENT won = True NEW_LINE coins += 2 - coined [ i ] NEW_LINE coined [ i ] = 2 NEW_LINE turns += 1 NEW_LINE DEDENT DEDENT if not won : NEW_LINE INDENT maxi = - 1 NEW_LINE maxm = - 1 NEW_LINE for i in xrange ( n ) : NEW_LINE INDENT if coined [ i ] == 0 and z [ i ] [ 0 ] <= coins and z [ i ] [ 1 ] > maxm : NEW_LINE INDENT maxm = z [ i ] [ 1 ] NEW_LINE maxi = i NEW_LINE DEDENT DEDENT if maxm == - 1 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT coined [ maxi ] = 1 NEW_LINE coins += 1 NEW_LINE turns += 1 NEW_LINE DEDENT DEDENT DEDENT return turns NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT fn = open ( " B - large . in " , " r " ) NEW_LINE tcase = int ( fn . readline ( ) ) NEW_LINE for x in range ( tcase ) : NEW_LINE INDENT a = int ( fn . readline ( ) ) NEW_LINE l = [ ] NEW_LINE for i in xrange ( a ) : NEW_LINE INDENT kk = fn . readline ( ) . split ( ) NEW_LINE l . append ( ( int ( kk [ 0 ] ) , int ( kk [ 1 ] ) ) ) NEW_LINE DEDENT yy = alphabet ( a , l ) NEW_LINE if yy == - 1 : NEW_LINE INDENT print " Case ▁ # % d : ▁ Too ▁ Bad " % ( x + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print " Case ▁ # % d : ▁ % d " % ( x + 1 , yy ) NEW_LINE DEDENT DEDENT DEDENT


for t in range ( int ( raw_input ( ) ) ) : NEW_LINE INDENT N = int ( raw_input ( ) ) NEW_LINE levels = [ ] NEW_LINE for i in xrange ( N ) : NEW_LINE INDENT levels . append ( map ( int , raw_input ( ) . split ( ) ) ) NEW_LINE DEDENT r = 0 NEW_LINE s = 0 NEW_LINE while True : NEW_LINE INDENT c = False NEW_LINE for i in xrange ( N ) : NEW_LINE INDENT if levels [ i ] [ 1 ] != - 1 and levels [ i ] [ 1 ] <= s : NEW_LINE INDENT if levels [ i ] [ 0 ] != - 1 : NEW_LINE INDENT s += 1 NEW_LINE DEDENT s += 1 NEW_LINE r += 1 NEW_LINE levels [ i ] = ( - 1 , - 1 ) NEW_LINE c = True NEW_LINE break NEW_LINE DEDENT DEDENT if c : NEW_LINE INDENT continue NEW_LINE DEDENT max_2v = - 1 NEW_LINE max_2i = - 1 NEW_LINE for i in xrange ( N ) : NEW_LINE INDENT if levels [ i ] [ 0 ] != - 1 and levels [ i ] [ 0 ] <= s : NEW_LINE INDENT if levels [ i ] [ 1 ] > max_2v : NEW_LINE INDENT max_2v = levels [ i ] [ 1 ] NEW_LINE max_2i = i NEW_LINE DEDENT DEDENT DEDENT if max_2v != - 1 : NEW_LINE INDENT r += 1 NEW_LINE s += 1 NEW_LINE levels [ max_2i ] [ 0 ] = - 1 NEW_LINE continue NEW_LINE DEDENT break NEW_LINE DEDENT if s == N * 2 : NEW_LINE INDENT print ' Case ▁ # % d : ▁ % d ' % ( t + 1 , r ) NEW_LINE DEDENT else : NEW_LINE INDENT print ' Case ▁ # % d : ▁ Too ▁ Bad ' % ( t + 1 ) NEW_LINE DEDENT DEDENT


import sys , re , string , math , fractions , itertools NEW_LINE from fractions import Fraction NEW_LINE ssr = sys . stdin . readline NEW_LINE ssw = sys . stdout . write NEW_LINE def rdline ( ) : return ssr ( ) . strip ( ) NEW_LINE def rdstrs ( ) : return ssr . split ( ) NEW_LINE def rdints ( ) : return map ( int , ssr ( ) . split ( ) ) NEW_LINE def do_one_case ( cnum ) : NEW_LINE INDENT ( N , ) = rdints ( ) NEW_LINE abi = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT ( a , b ) = rdints ( ) NEW_LINE abi . append ( ( a , b , i ) ) NEW_LINE DEDENT d = N * [ 0 ] NEW_LINE s = 0 NEW_LINE n = 0 NEW_LINE while s < 2 * N : NEW_LINE INDENT ( b , i ) = min ( [ ( b , i ) for ( a , b , i ) in abi if d [ i ] < 2 ] ) NEW_LINE if b <= s : NEW_LINE INDENT s += 2 - d [ i ] NEW_LINE d [ i ] = 2 NEW_LINE n += 1 NEW_LINE continue NEW_LINE DEDENT bai = [ ( b , a , i ) for ( a , b , i ) in abi if d [ i ] == 0 and a <= s ] NEW_LINE if not bai : NEW_LINE INDENT print " Case ▁ # % d : ▁ Too ▁ Bad " % ( cnum , ) NEW_LINE return NEW_LINE DEDENT ( b , a , i ) = max ( bai ) NEW_LINE s += 1 NEW_LINE d [ i ] = 1 NEW_LINE n += 1 NEW_LINE DEDENT print " Case ▁ # % d : ▁ % d " % ( cnum , n ) NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT N = int ( rdline ( ) ) 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


def input ( ) : NEW_LINE INDENT with open ( ' b . in ' ) as file : NEW_LINE INDENT tests_count = int ( file . readline ( ) . strip ( ) ) NEW_LINE for i in xrange ( tests_count ) : NEW_LINE INDENT levels_count = int ( file . readline ( ) . strip ( ) ) NEW_LINE levels_requirements = [ ] NEW_LINE for j in xrange ( levels_count ) : NEW_LINE INDENT levels_requirements . append ( tuple ( int ( n ) for n in file . readline ( ) . strip ( ) . split ( ) ) ) NEW_LINE DEDENT yield levels_requirements NEW_LINE DEDENT DEDENT DEDENT def output ( answers ) : NEW_LINE INDENT with open ( ' b . out ' , ' w ' ) as file : NEW_LINE INDENT for i , answer in enumerate ( answers ) : NEW_LINE INDENT file . write ( ' Case ▁ # % s : ▁ % s \n ' % ( i + 1 , answer ) ) NEW_LINE DEDENT DEDENT DEDENT def choose_level ( levels_requirements , levels_solved , stars_earned ) : NEW_LINE INDENT for i in xrange ( len ( levels_requirements ) ) : NEW_LINE INDENT result = levels_solved [ i ] NEW_LINE if result < 2 : NEW_LINE INDENT if levels_requirements [ i ] [ 1 ] <= stars_earned : NEW_LINE INDENT levels_solved [ i ] = 2 NEW_LINE return 2 - result NEW_LINE DEDENT DEDENT DEDENT candidate_i = - 1 NEW_LINE maximum_2_stars = - 1 NEW_LINE for i in xrange ( len ( levels_requirements ) ) : NEW_LINE INDENT result = levels_solved [ i ] NEW_LINE if result == 0 : NEW_LINE INDENT if levels_requirements [ i ] [ 0 ] <= stars_earned : NEW_LINE INDENT if levels_requirements [ i ] [ 1 ] > maximum_2_stars : NEW_LINE INDENT maximum_2_stars = levels_requirements [ i ] [ 1 ] NEW_LINE candidate_i = i NEW_LINE DEDENT DEDENT DEDENT DEDENT if candidate_i == - 1 : NEW_LINE INDENT return 0 NEW_LINE DEDENT levels_solved [ candidate_i ] = 1 NEW_LINE return 1 NEW_LINE DEDENT def solve ( levels_requirements ) : NEW_LINE INDENT levels_solved = [ 0 ] * len ( levels_requirements ) NEW_LINE stars_earned = 0 NEW_LINE games_played = 0 NEW_LINE while True : NEW_LINE INDENT if all ( l == 2 for l in levels_solved ) : NEW_LINE INDENT return games_played NEW_LINE DEDENT new_stars = choose_level ( levels_requirements , levels_solved , stars_earned ) NEW_LINE if not new_stars : NEW_LINE INDENT return ' Too ▁ Bad ' NEW_LINE DEDENT else : NEW_LINE INDENT stars_earned += new_stars NEW_LINE games_played += 1 NEW_LINE DEDENT DEDENT DEDENT def main ( ) : NEW_LINE INDENT answers = ( solve ( levels_requirements ) for levels_requirements in input ( ) ) NEW_LINE output ( answers ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT


from Queue import PriorityQueue NEW_LINE with open ( ' B - large . in ' , ' r ' ) as fin : NEW_LINE INDENT with open ( ' output . txt ' , ' w ' ) as fout : NEW_LINE INDENT numcases = int ( fin . readline ( ) ) NEW_LINE for i in range ( 1 , numcases + 1 ) : NEW_LINE INDENT line = [ int ( j ) for j in fin . readline ( ) . split ( ) ] NEW_LINE numlevels = line [ 0 ] NEW_LINE doables = [ ] NEW_LINE levels = PriorityQueue ( ) NEW_LINE for j in range ( numlevels ) : NEW_LINE INDENT line = [ int ( k ) for k in fin . readline ( ) . split ( ) ] NEW_LINE levels . put ( ( line [ 0 ] , line [ 1 ] ) ) NEW_LINE DEDENT stars = 0 NEW_LINE played = 0 NEW_LINE while True : NEW_LINE INDENT while not levels . empty ( ) : NEW_LINE INDENT nextlevel = levels . get ( ) NEW_LINE if nextlevel [ 0 ] <= stars : NEW_LINE INDENT if nextlevel [ 1 ] == None : NEW_LINE INDENT played += 1 NEW_LINE stars += 1 NEW_LINE DEDENT else : NEW_LINE INDENT doables . append ( nextlevel [ 1 ] ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT levels . put ( nextlevel ) NEW_LINE break NEW_LINE DEDENT DEDENT doables . sort ( ) NEW_LINE if len ( doables ) == 0 : NEW_LINE INDENT if not levels . empty ( ) : NEW_LINE INDENT played = - 1 NEW_LINE DEDENT break NEW_LINE DEDENT else : NEW_LINE INDENT lowlevel = doables [ 0 ] NEW_LINE played += 1 NEW_LINE if lowlevel <= stars : NEW_LINE INDENT stars += 2 NEW_LINE doables = doables [ 1 : ] NEW_LINE DEDENT else : NEW_LINE INDENT stars += 1 NEW_LINE levels . put ( ( doables [ - 1 ] , None ) ) NEW_LINE del doables [ - 1 ] NEW_LINE DEDENT DEDENT DEDENT fout . write ( " Case ▁ # " ) NEW_LINE fout . write ( str ( i ) ) NEW_LINE fout . write ( " : ▁ " ) NEW_LINE if ( played >= 0 ) : NEW_LINE INDENT fout . write ( str ( played ) ) NEW_LINE DEDENT else : NEW_LINE INDENT fout . write ( " Too ▁ Bad " ) NEW_LINE DEDENT fout . write ( ' \n ' ) NEW_LINE print ( i ) NEW_LINE DEDENT DEDENT DEDENT


import java . io . File ; import java . io . PrintWriter ; import java . util . Arrays ; import java . util . Scanner ; public class B { static int [ ] visit = new int [ 1024 ] ; static int [ ] a = new int [ 1024 ] ; static int [ ] b = new int [ 1024 ] ; public static void main ( String [ ] args ) throws Exception { Scanner s = new Scanner ( new File ( " B . in " ) ) ; PrintWriter out = new PrintWriter ( new File ( " B . out " ) ) ; int T = s . nextInt ( ) ; for ( int tc = 1 ; tc <= T ; tc ++ ) { out . print ( " Case ▁ # " + tc + " : ▁ " ) ; int N = s . nextInt ( ) ; Arrays . fill ( visit , 0 ) ; for ( int i = 0 ; i < N ; i ++ ) { a [ i ] = s . nextInt ( ) ; b [ i ] = s . nextInt ( ) ; } int ans = 0 ; int stars = 0 ; boolean progress = true ; outer : while ( progress ) { if ( stars == 2 * N ) break ; ans ++ ; progress = false ; for ( int i = 0 ; i < N ; i ++ ) { if ( visit [ i ] == 0 && b [ i ] <= stars ) { progress = true ; stars += 2 ; visit [ i ] = 2 ; continue outer ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( visit [ i ] == 1 && b [ i ] <= stars ) { progress = true ; stars += 1 ; visit [ i ] = 2 ; continue outer ; } } int bestb = 0 ; int besti = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( visit [ i ] == 0 && a [ i ] <= stars ) { progress = true ; if ( bestb < b [ i ] ) { bestb = b [ i ] ; besti = i ; } } } if ( progress ) { visit [ besti ] = 1 ; stars += 1 ; } } if ( stars == 2 * N ) out . println ( ans ) ; else out . println ( " Too ▁ Bad " ) ; } out . close ( ) ; } }


import java . util . Comparator ; import java . util . HashSet ; import java . util . Scanner ; import java . util . TreeSet ; public class b { Scanner in = new Scanner ( System . in ) ; public static void main ( String [ ] args ) { new b ( ) . go ( ) ; } private void go ( ) { int numc = in . nextInt ( ) ; IN : for ( int cnum = 0 ; cnum < numc ; cnum ++ ) { int n = in . nextInt ( ) ; int [ ] [ ] q = new int [ n ] [ 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { q [ i ] [ 0 ] = in . nextInt ( ) ; q [ i ] [ 1 ] = in . nextInt ( ) ; } int min = 0 , stars = 0 ; HashSet < Integer > chose1 = new HashSet < Integer > ( ) , chose2 = new HashSet < Integer > ( ) ; OUT : for ( int i = 0 ; i < 2 * n ; i ++ ) { if ( stars == 2 * n ) break ; int max = 0 , maxIndex = - 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ! chose2 . contains ( j ) ) { if ( ! chose1 . contains ( j ) ) { if ( q [ j ] [ 1 ] <= stars ) { stars += 2 ; min ++ ; chose2 . add ( j ) ; continue OUT ; } if ( q [ j ] [ 0 ] <= stars && q [ j ] [ 1 ] > max ) { max = q [ j ] [ 1 ] ; maxIndex = j ; } } if ( q [ j ] [ 1 ] <= stars ) { stars += 1 ; min ++ ; chose2 . add ( j ) ; chose1 . remove ( j ) ; continue OUT ; } } } if ( maxIndex == - 1 ) { System . out . printf ( " Case ▁ # % d : ▁ Too ▁ Bad \n " , cnum + 1 ) ; continue IN ; } chose1 . add ( maxIndex ) ; min ++ ; stars += 1 ; } System . out . printf ( " Case ▁ # % d : ▁ % d \n " , cnum + 1 , min ) ; } } }


import java . util . * ; import java . math . * ; public class Main { static class Level implements Comparable < Level > { public int low ; public int high ; public Level ( int low , int high ) { this . low = low ; this . high = high ; } @ Override public int compareTo ( Level o ) { return high - o . high ; } } public static void main ( String [ ] args ) throws Exception { Scanner scan = new Scanner ( System . in ) ; int task = scan . nextInt ( ) ; int current = 1 ; while ( task -- > 0 ) { int N = scan . nextInt ( ) ; Level [ ] arr = new Level [ N ] ; int [ ] status = new int [ N ] ; int finished = 0 ; int star = 0 ; for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = new Level ( scan . nextInt ( ) , scan . nextInt ( ) ) ; } Arrays . sort ( arr ) ; int result = 0 ; while ( finished < N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( status [ i ] == 2 ) { continue ; } if ( star >= arr [ i ] . high ) { star += 2 - status [ i ] ; status [ i ] = 2 ; finished ++ ; result ++ ; } } if ( finished == N ) { break ; } int select = - 1 ; int max = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { if ( status [ i ] >= 1 ) { continue ; } if ( star >= arr [ i ] . low ) { if ( arr [ i ] . high > max ) { select = i ; max = arr [ i ] . high ; } } } if ( select == - 1 ) { result = - 1 ; break ; } else { result ++ ; status [ select ] = 1 ; star ++ ; } } System . out . println ( " Case ▁ # " + current + " : ▁ " + ( result == - 1 ? " Too ▁ Bad " : result ) ) ; current ++ ; } } }


import java . util . Scanner ; public class B { private static Scanner sc ; public static void main ( String [ ] args ) { sc = new Scanner ( System . in ) ; int t = sc . nextInt ( ) ; sc . nextLine ( ) ; for ( int i = 0 ; i < t ; i ++ ) { System . out . printf ( " Case ▁ # % d : ▁ % s \n " , i + 1 , exec ( ) ) ; } } public static String exec ( ) { int n = sc . nextInt ( ) ; int [ ] s1 = new int [ n ] ; int [ ] s2 = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { s1 [ i ] = sc . nextInt ( ) ; s2 [ i ] = sc . nextInt ( ) ; } int stars = 0 ; int goal = n * 2 ; int [ ] obtained = new int [ n ] ; int plays = 0 ; loop : while ( stars < goal ) { for ( int i = 0 ; i < n ; i ++ ) { if ( s2 [ i ] > stars ) continue ; if ( obtained [ i ] == 2 ) continue ; stars += 2 - obtained [ i ] ; obtained [ i ] = 2 ; plays ++ ; continue loop ; } int s2OfWinner = - 1 ; int idxOfWinner = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( obtained [ i ] != 0 ) continue ; if ( s1 [ i ] > stars ) continue ; if ( s2 [ i ] < s2OfWinner ) continue ; s2OfWinner = s2 [ i ] ; idxOfWinner = i ; } if ( s2OfWinner == - 1 ) return " Too ▁ Bad " ; stars ++ ; obtained [ idxOfWinner ] = 1 ; plays ++ ; } return String . valueOf ( plays ) ; } }


import java . util . * ; import java . io . * ; import java . math . * ; import java . awt . * ; import static java . lang . Math . * ; import static java . lang . Integer . parseInt ; import static java . lang . Double . parseDouble ; import static java . lang . Long . parseLong ; import static java . lang . System . * ; import static java . util . Arrays . * ; import static java . util . Collection . * ; public class A { public static void main ( String [ ] args ) throws IOException { BufferedReader br = new BufferedReader ( new InputStreamReader ( in ) ) ; int T = parseInt ( br . readLine ( ) ) ; for ( int t = 0 ; t ++ < T ; ) { String [ ] line = br . readLine ( ) . split ( " ▁ " ) ; int A = parseInt ( line [ 0 ] ) , B = parseInt ( line [ 1 ] ) ; line = br . readLine ( ) . split ( " ▁ " ) ; double [ ] P = new double [ A + 1 ] ; P [ 0 ] = 1 ; for ( int a = 0 ; a < A ; a ++ ) P [ a + 1 ] = parseDouble ( line [ a ] ) * P [ a ] ; double best = 2 + B ; for ( int a = 0 ; a <= A ; a ++ ) best = min ( best , 2 * a + ( B - A ) + 1 + ( 1 - P [ A - a ] ) * ( B + 1 ) ) ; out . println ( " Case ▁ # " + t + " : ▁ " + best ) ; } } }

import sys NEW_LINE def compute ( A , B , p ) : NEW_LINE INDENT q = 1.0 NEW_LINE smin = B + 2.0 NEW_LINE for i in xrange ( len ( p ) ) : NEW_LINE INDENT q *= p [ i ] NEW_LINE s = ( A - i - 1 ) + q * ( B - i ) + ( 1.0 - q ) * ( B - i + B + 1 ) NEW_LINE if s < smin : NEW_LINE INDENT smin = s NEW_LINE DEDENT DEDENT return " % f " % smin NEW_LINE DEDENT def parse ( ) : NEW_LINE INDENT A , B = map ( int , sys . stdin . readline ( ) . strip ( ) . split ( ' ▁ ' ) ) NEW_LINE p = map ( float , sys . stdin . readline ( ) . strip ( ) . split ( ' ▁ ' ) ) NEW_LINE return ( A , B , p ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT T = int ( sys . stdin . readline ( ) . strip ( ) ) NEW_LINE for i in xrange ( T ) : NEW_LINE INDENT data = parse ( ) NEW_LINE result = compute ( * data ) NEW_LINE print " Case ▁ # % d : ▁ % s " % ( i + 1 , result ) NEW_LINE DEDENT DEDENT


import sys NEW_LINE import logging NEW_LINE if len ( sys . argv ) > 1 : NEW_LINE INDENT sampledata = False NEW_LINE infname = sys . argv [ 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT sampledata = True NEW_LINE scriptname = sys . argv [ 0 ] NEW_LINE problemletter = scriptname [ : scriptname . index ( ' . ' ) ] NEW_LINE infname = problemletter + ' - example . in ' NEW_LINE DEDENT outfname = infname [ : infname . index ( ' . ' ) ] + ' . out ' NEW_LINE with open ( infname ) as f : NEW_LINE INDENT text = f . read ( ) NEW_LINE DEDENT lines = text . splitlines ( ) NEW_LINE linesiter = iter ( lines ) NEW_LINE nextline = linesiter . next NEW_LINE ofile = open ( outfname , ' w ' ) NEW_LINE sys . stdout = ofile NEW_LINE T = int ( nextline ( ) ) NEW_LINE for t in range ( 1 , T + 1 ) : NEW_LINE INDENT A , B = map ( int , nextline ( ) . split ( ) ) NEW_LINE ps = map ( float , nextline ( ) . split ( ) ) NEW_LINE best = 1 + B + 1 NEW_LINE pcorr = 1.0 NEW_LINE for i , p in enumerate ( ps ) : NEW_LINE INDENT pcorr *= p NEW_LINE ncorr = ( B - A ) + 2 * ( A - i - 1 ) + 1 NEW_LINE pwr = 1 - pcorr NEW_LINE nwr = ncorr + B + 1 NEW_LINE exp = pcorr * ncorr + pwr * nwr NEW_LINE best = min ( best , exp ) NEW_LINE DEDENT print ' Case ▁ # % s : ' % t , best NEW_LINE DEDENT sys . stdout = sys . __stdout__ NEW_LINE ofile . close ( ) NEW_LINE if sampledata : NEW_LINE INDENT base = problemletter + ' - example . ' NEW_LINE outfile = base + ' out ' NEW_LINE rightfile = base + ' right ' NEW_LINE out = open ( outfile ) . read ( ) NEW_LINE right = open ( rightfile ) . read ( ) NEW_LINE if out == right : NEW_LINE INDENT print ' Congrats , ▁ your ▁ output ▁ matches ▁ sample ▁ output ' NEW_LINE DEDENT else : NEW_LINE INDENT print ' OUTPUT ▁ MISMATCH ' NEW_LINE import os NEW_LINE os . system ( ' diff ▁ % s ▁ % s ' % ( outfile , rightfile ) ) NEW_LINE DEDENT DEDENT


import os , sys , math NEW_LINE def prod ( probs ) : NEW_LINE INDENT x = 1.0 NEW_LINE for p in probs : NEW_LINE INDENT x *= p NEW_LINE DEDENT return x NEW_LINE DEDENT def calcFinish ( A , B , probs ) : NEW_LINE INDENT probRight = prod ( probs ) NEW_LINE return ( ( probRight * ( B - A + 1 ) ) + ( ( 1.0 - probRight ) * ( B - A + 1 + B + 1 ) ) ) NEW_LINE DEDENT def calcEnter ( A , B , probs ) : NEW_LINE INDENT return 1 + B + 1 NEW_LINE DEDENT def calcBackspaces ( A , B , n , probs ) : NEW_LINE INDENT afterBackspace = calcFinish ( A - n , B , probs [ : A - n ] ) NEW_LINE return n + afterBackspace NEW_LINE DEDENT def main ( filename ) : NEW_LINE INDENT fileLines = open ( filename , ' r ' ) . readlines ( ) NEW_LINE index = 0 NEW_LINE numCases = int ( fileLines [ index ] [ : - 1 ] ) NEW_LINE index += 1 NEW_LINE for caseNum in range ( numCases ) : NEW_LINE INDENT caseStr = fileLines [ index ] [ : - 1 ] NEW_LINE index += 1 NEW_LINE nums = [ int ( x ) for x in caseStr . split ( ' ▁ ' ) ] NEW_LINE A = nums [ 0 ] NEW_LINE B = nums [ 1 ] NEW_LINE caseStr = fileLines [ index ] [ : - 1 ] NEW_LINE index += 1 NEW_LINE probs = [ float ( x ) for x in caseStr . split ( ' ▁ ' ) ] NEW_LINE if len ( probs ) != A : NEW_LINE INDENT print " WRONG ▁ LENGTH " NEW_LINE sys . exit ( 1 ) NEW_LINE DEDENT finishStrokes = calcFinish ( A , B , probs ) NEW_LINE enterStrokes = calcEnter ( A , B , probs ) NEW_LINE numBackspaces = [ calcBackspaces ( A , B , n , probs ) for n in range ( 1 , A ) ] NEW_LINE if len ( numBackspaces ) == 0 : NEW_LINE INDENT answer = min ( finishStrokes , enterStrokes ) NEW_LINE DEDENT else : NEW_LINE INDENT answer = min ( finishStrokes , enterStrokes , min ( numBackspaces ) ) NEW_LINE DEDENT print " Case ▁ # % d : ▁ % f " % ( caseNum + 1 , answer ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( sys . argv [ 1 ] ) NEW_LINE DEDENT


with open ( ' A - large . in ' , ' r ' ) as fin : NEW_LINE INDENT with open ( ' output . txt ' , ' w ' ) as fout : NEW_LINE INDENT numcases = int ( fin . readline ( ) ) NEW_LINE for i in range ( 1 , numcases + 1 ) : NEW_LINE INDENT line = [ int ( j ) for j in fin . readline ( ) . split ( ) ] NEW_LINE charstyped = line [ 0 ] NEW_LINE numchars = line [ 1 ] NEW_LINE line = [ float ( j ) for j in fin . readline ( ) . split ( ) ] NEW_LINE bestcost = numchars + 2 NEW_LINE correctprob = 1 NEW_LINE for idx in range ( charstyped ) : NEW_LINE INDENT correctprob = correctprob * line [ idx ] NEW_LINE currcost = ( numchars + 1 ) * ( 1 - correctprob ) + ( charstyped - idx - 1 ) + ( numchars - idx - 1 ) + 1 NEW_LINE if currcost < bestcost : NEW_LINE INDENT bestcost = currcost NEW_LINE DEDENT DEDENT fout . write ( " Case ▁ # " ) NEW_LINE fout . write ( str ( i ) ) NEW_LINE fout . write ( " : ▁ " ) NEW_LINE fout . write ( str ( bestcost ) ) NEW_LINE fout . write ( ' \n ' ) NEW_LINE print ( i ) NEW_LINE DEDENT DEDENT DEDENT


testCount = int ( raw_input ( ) ) NEW_LINE for testIndex in range ( testCount ) : NEW_LINE INDENT ans = " Case ▁ # " + str ( testIndex + 1 ) + " : ▁ " NEW_LINE n , m = [ int ( x ) for x in raw_input ( ) . split ( " ▁ " ) ] NEW_LINE p = [ float ( x ) for x in raw_input ( ) . split ( " ▁ " ) ] NEW_LINE minType = m + 1.0 NEW_LINE p1 = reduce ( lambda x , y : x * y , p , 1.0 ) NEW_LINE type = ( m - n ) * p1 + ( m - n + 1 + m ) * ( 1 - p1 ) NEW_LINE minType = min ( type , minType ) NEW_LINE p1 = 1.0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT p1 *= p [ i - 1 ] NEW_LINE type = ( n - i + m - i ) * p1 + ( n - i + m - i + 1 + m ) * ( 1 - p1 ) NEW_LINE minType = min ( type , minType ) NEW_LINE DEDENT print ( ans + str ( minType + 1.0 ) ) NEW_LINE DEDENT


import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; int inputs = in . nextInt ( ) ; Q : for ( int _ = 1 ; _ <= inputs ; _ ++ ) { int a = in . nextInt ( ) ; int b = in . nextInt ( ) ; double exp = 2 + b ; double [ ] prob = new double [ a + 1 ] ; prob [ 0 ] = 1 ; for ( int i = 0 ; i < a ; i ++ ) { prob [ i + 1 ] = prob [ i ] * ( in . nextDouble ( ) ) ; } double exp2 = prob [ a ] * ( b - a + 1 ) + ( 1 - prob [ a ] ) * ( 2 * b - a + 2 ) ; if ( exp2 < exp ) exp = exp2 ; exp2 = 0 ; for ( int i = 0 ; i < a ; i ++ ) { exp2 = prob [ i ] * ( 2 * ( a - i ) + ( b - a ) + 1 ) + ( 1 - prob [ i ] ) * ( ( 2 * ( a - i ) + ( b - a ) + 1 ) + b + 1 ) ; if ( exp2 < exp ) exp = exp2 ; } System . out . printf ( " Case ▁ # " + _ + " : ▁ % .6f \n " , exp ) ; } } }


import java . io . * ; import java . util . * ; public class PasswordProblem { void solve ( ) throws Exception { int a = nextInt ( ) ; int b = nextInt ( ) ; double [ ] p = new double [ a ] ; for ( int i = 0 ; i < a ; i ++ ) { p [ i ] = nextDouble ( ) ; } double [ ] prob = p . clone ( ) ; for ( int i = 1 ; i < a ; i ++ ) { prob [ i ] = prob [ i - 1 ] * prob [ i ] ; } double ans = b + 2 ; for ( int i = a - 1 ; i >= 0 ; i -- ) { double curAns = ( a - i - 1 ) + ( b - i - 1 ) + 1 + ( 1 - prob [ i ] ) * ( b + 1 ) ; ans = Math . min ( ans , curAns ) ; } out . println ( ans ) ; } void run ( ) { try { in = new BufferedReader ( new FileReader ( " input . txt " ) ) ; out = new PrintWriter ( " output . txt " ) ; int tests = nextInt ( ) ; for ( int i = 0 ; i < tests ; i ++ ) { out . print ( " Case ▁ # " + ( i + 1 ) + " : ▁ " ) ; solve ( ) ; } out . close ( ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; System . exit ( 1 ) ; } } BufferedReader in ; StringTokenizer st ; PrintWriter out ; final String filename = new String ( " PasswordProblem " ) . toLowerCase ( ) ; String nextToken ( ) throws Exception { while ( st == null || ! st . hasMoreTokens ( ) ) st = new StringTokenizer ( in . readLine ( ) ) ; return st . nextToken ( ) ; } int nextInt ( ) throws Exception { return Integer . parseInt ( nextToken ( ) ) ; } long nextLong ( ) throws Exception { return Long . parseLong ( nextToken ( ) ) ; } double nextDouble ( ) throws Exception { return Double . parseDouble ( nextToken ( ) ) ; } public static void main ( String [ ] args ) { new PasswordProblem ( ) . run ( ) ; } }


import java . math . BigDecimal ; import java . math . MathContext ; import java . math . RoundingMode ; import java . util . Arrays ; import java . util . Scanner ; public class A { private static Scanner sc ; private static long start ; public static void main ( String [ ] args ) { sc = new Scanner ( System . in ) ; int t = sc . nextInt ( ) ; sc . nextLine ( ) ; for ( int i = 0 ; i < t ; i ++ ) { start = System . currentTimeMillis ( ) ; System . out . printf ( " Case ▁ # % d : ▁ % s \n " , i + 1 , exec ( ) ) ; long end = System . currentTimeMillis ( ) ; } } private static final MathContext ROUND_CONTEXT = new MathContext ( 8 , RoundingMode . HALF_EVEN ) ; public static String exec ( ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; BigDecimal [ ] bd = new BigDecimal [ a ] ; for ( int i = 0 ; i < a ; i ++ ) bd [ i ] = new BigDecimal ( sc . next ( ) ) ; BigDecimal [ ] rs = new BigDecimal [ a + 2 ] ; rs [ 0 ] = new BigDecimal ( b + 2 ) ; int backspacesNeeded = a ; double runningChance = 1.0 ; for ( int i = 0 ; i < a ; i ++ ) { int charsNeededOnSuccess = backspacesNeeded + b - i + 1 ; int charsNeededOnFail = charsNeededOnSuccess + b + 1 ; double onSuccess = runningChance * charsNeededOnSuccess ; double onFail = ( 1.0 - runningChance ) * charsNeededOnFail ; rs [ i + 1 ] = new BigDecimal ( onSuccess + onFail ) ; runningChance *= bd [ i ] . doubleValue ( ) ; backspacesNeeded -- ; } rs [ a + 1 ] = new BigDecimal ( ( runningChance * ( b - a + 1 ) ) + ( ( 1.0 - runningChance ) * ( b - a + 1 + b + 1 ) ) ) ; Arrays . sort ( rs ) ; BigDecimal result = rs [ 0 ] ; return String . format ( " % .06f " , result ) ; } }


import java . io . File ; import java . io . PrintWriter ; import java . util . Scanner ; public class A { static double [ ] logP = new double [ 1 << 17 ] ; public static void main ( String [ ] args ) throws Exception { Scanner s = new Scanner ( new File ( " A . in " ) ) ; PrintWriter out = new PrintWriter ( new File ( " A . out " ) ) ; int T = s . nextInt ( ) ; for ( int tc = 1 ; tc <= T ; tc ++ ) { out . print ( " Case ▁ # " + tc + " : ▁ " ) ; int A = s . nextInt ( ) , B = s . nextInt ( ) ; double best = A + B + 1 ; logP [ 0 ] = Math . log ( s . nextDouble ( ) ) ; best = Math . min ( best , ( 2 * ( A - 1 ) + ( B - A ) + 1 ) + ( B + 1 ) - ( B + 1 ) * Math . exp ( logP [ 0 ] ) ) ; for ( int i = 1 ; i < A ; i ++ ) { logP [ i ] = logP [ i - 1 ] + Math . log ( s . nextDouble ( ) ) ; best = Math . min ( best , ( 2 * ( A - 1 - i ) + ( B - A ) + 1 ) + ( B + 1 ) - ( B + 1 ) * Math . exp ( logP [ i ] ) ) ; } best = Math . min ( best , B + 2 ) ; out . println ( best ) ; } out . close ( ) ; } }


import java . util . * ; import java . io . * ; public class A { public static void main ( String [ ] args ) throws Exception { Scanner in = new Scanner ( new File ( " A - small - attempt0 . in " ) ) ; PrintWriter out = new PrintWriter ( new FileWriter ( new File ( " A - small . out " ) ) ) ; int t = in . nextInt ( ) ; for ( int x = 0 ; x < t ; x ++ ) { int row1 = in . nextInt ( ) - 1 ; HashSet < Integer > cards = new HashSet < Integer > ( ) ; for ( int y = 0 ; y < 4 ; y ++ ) { for ( int z = 0 ; z < 4 ; z ++ ) { int card = in . nextInt ( ) ; if ( y == row1 ) { cards . add ( card ) ; } } } int row2 = in . nextInt ( ) - 1 ; int chosen = - 1 ; int count = 0 ; for ( int a = 0 ; a < 4 ; a ++ ) { for ( int b = 0 ; b < 4 ; b ++ ) { int card = in . nextInt ( ) ; if ( a == row2 && cards . contains ( card ) ) { chosen = card ; count ++ ; } } } out . print ( " Case ▁ # " + ( x + 1 ) + " : ▁ " ) ; if ( count == 1 ) { out . println ( chosen ) ; } else if ( count == 0 ) { out . println ( " Volunteer ▁ cheated ! " ) ; } else { out . println ( " Bad ▁ magician ! " ) ; } } out . close ( ) ; } }

import sys NEW_LINE def main ( ) : NEW_LINE INDENT f = open ( sys . argv [ 1 ] ) NEW_LINE T = int ( f . readline ( ) . strip ( ) ) NEW_LINE for i in range ( 1 , T + 1 ) : NEW_LINE INDENT row1 = getRow ( f ) NEW_LINE row2 = getRow ( f ) NEW_LINE cards = list ( row1 & row2 ) NEW_LINE if len ( cards ) == 1 : NEW_LINE INDENT print " Case ▁ # % d : ▁ % s " % ( i , cards [ 0 ] ) NEW_LINE DEDENT elif len ( cards ) > 1 : NEW_LINE INDENT print " Case ▁ # % d : ▁ Bad ▁ magician ! " % i NEW_LINE DEDENT else : NEW_LINE INDENT print " Case ▁ # % d : ▁ Volunteer ▁ cheated ! " % i NEW_LINE DEDENT DEDENT DEDENT def getRow ( f ) : NEW_LINE INDENT n = int ( f . readline ( ) . strip ( ) ) NEW_LINE row = [ ] NEW_LINE for i in range ( 1 , 5 ) : NEW_LINE INDENT l = f . readline ( ) NEW_LINE if n == i : NEW_LINE INDENT row = set ( l . strip ( ) . split ( ) ) NEW_LINE DEDENT DEDENT return row NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT


import math NEW_LINE import sys NEW_LINE def findsol ( C1 , C2 ) : NEW_LINE INDENT a = set ( C1 ) . intersection ( set ( C2 ) ) NEW_LINE if len ( a ) == 0 : NEW_LINE INDENT b = ' Volunteer ▁ cheated ! ' NEW_LINE DEDENT if len ( a ) == 1 : NEW_LINE INDENT b = str ( a . pop ( ) ) NEW_LINE DEDENT if len ( a ) > 1 : NEW_LINE INDENT b = ' Bad ▁ magician ! ' NEW_LINE DEDENT return b NEW_LINE DEDENT def convertnums ( s ) : NEW_LINE INDENT a = [ ] NEW_LINE ii = 0 NEW_LINE for jj in range ( len ( s ) ) : NEW_LINE INDENT if s [ jj ] == ' ▁ ' : NEW_LINE INDENT if ( ii < jj ) : NEW_LINE INDENT a . append ( int ( s [ ii : jj ] ) ) NEW_LINE ii = jj + 1 NEW_LINE DEDENT DEDENT DEDENT a . append ( int ( s [ ii : jj ] ) ) NEW_LINE return a NEW_LINE DEDENT fidi = open ( ' A - small - attempt0 . in ' , ' r ' ) NEW_LINE fido = open ( ' a . out ' , ' w ' ) NEW_LINE T = fidi . readline ( ) NEW_LINE T = int ( T ) NEW_LINE for ii in range ( 1 , T + 1 ) : NEW_LINE INDENT R1 = fidi . readline ( ) NEW_LINE R1 = int ( R1 ) NEW_LINE for jj in range ( 4 ) : NEW_LINE INDENT C = fidi . readline ( ) NEW_LINE if ( jj == R1 - 1 ) : NEW_LINE INDENT C1 = convertnums ( C ) NEW_LINE DEDENT DEDENT R2 = fidi . readline ( ) NEW_LINE R2 = int ( R2 ) NEW_LINE for jj in range ( 4 ) : NEW_LINE INDENT C = fidi . readline ( ) NEW_LINE if ( jj == R2 - 1 ) : NEW_LINE INDENT C2 = convertnums ( C ) NEW_LINE DEDENT DEDENT a = findsol ( C1 , C2 ) NEW_LINE fido . write ( ' Case ▁ # ' + str ( ii ) + ' : ▁ ' + str ( a ) + ' \n ' ) NEW_LINE print ( ' Case ▁ # ' , str ( ii ) , ' : ▁ ' , str ( a ) ) NEW_LINE DEDENT fidi . close ( ) NEW_LINE fido . close ( ) NEW_LINE


from collections import deque , defaultdict NEW_LINE E = 0.0 NEW_LINE def main ( ) : NEW_LINE INDENT f = open ( ' A . in ' , ' r ' ) NEW_LINE fout = open ( ' A . out ' , ' w ' ) NEW_LINE T = int ( f . readline ( ) ) NEW_LINE for t in range ( 1 , T + 1 ) : NEW_LINE INDENT print ' Case ▁ # ' + str ( t ) + ' : ' , NEW_LINE r1 = int ( f . readline ( ) ) NEW_LINE s1 = getSet ( f , r1 ) NEW_LINE r2 = int ( f . readline ( ) ) NEW_LINE s2 = getSet ( f , r2 ) NEW_LINE res = s1 & s2 NEW_LINE if ( len ( res ) == 1 ) : NEW_LINE INDENT print list ( res ) [ 0 ] NEW_LINE DEDENT elif ( len ( res ) > 1 ) : NEW_LINE INDENT print ' Bad ▁ magician ! ' NEW_LINE DEDENT else : NEW_LINE INDENT print ' Volunteer ▁ cheated ! ' NEW_LINE DEDENT DEDENT DEDENT def getSet ( f , r ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( 1 , 4 + 1 ) : NEW_LINE INDENT cards = f . readline ( ) . split ( ) NEW_LINE if i == r : NEW_LINE INDENT for card in cards : NEW_LINE INDENT s . add ( card ) NEW_LINE DEDENT DEDENT DEDENT return s NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT main ( ) NEW_LINE DEDENT