id
stringlengths
2
6
java
stringlengths
48
5.92k
python
stringlengths
33
11.1k
T300
import java . util . * ; import java . io . * ; import static java . lang . System . in ; public class Main { static long R , B , x , y ; public static void main ( String [ ] args ) throws IOException { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int X = sc . nextInt ( ) ; int [ ] w = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) w [ i ] = sc . nextInt ( ) ; long ans = help ( w , X ) ; System . out . println ( ans ) ; } static long help ( int [ ] w , int X ) { int n = w . length ; if ( n == 1 ) { return w [ 0 ] == X ? 1 : 0 ; } int ans = 0 ; int p = n / 2 ; HashMap < Integer , Integer > dic = getDic ( Arrays . copyOfRange ( w , p , n ) ) ; int [ ] first = Arrays . copyOfRange ( w , 0 , p ) ; for ( int i = 0 ; i < ( 1 << p ) ; i ++ ) { int cur = getSum ( first , i ) ; ans += dic . getOrDefault ( X - cur , 0 ) ; } return ans ; } static HashMap < Integer , Integer > getDic ( int [ ] a ) { int q = a . length ; HashMap < Integer , Integer > ans = new HashMap < > ( ) ; for ( int i = 0 ; i < ( 1 << q ) ; i ++ ) { int cur = getSum ( a , i ) ; int val = ans . getOrDefault ( cur , 0 ) + 1 ; ans . put ( cur , val ) ; } return ans ; } static int getSum ( int [ ] a , int mask ) { int sum = 0 ; for ( int i = 0 ; i < a . length ; i ++ ) { if ( ( mask & ( 1 << i ) ) > 0 ) sum += a [ i ] ; } return sum ; } }
from bisect import bisect , bisect_left NEW_LINE def inpl ( ) : return list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE N , X = inpl ( ) NEW_LINE W = [ int ( input ( ) ) for _ in range ( N ) ] NEW_LINE A = N // 2 NEW_LINE B = N - A NEW_LINE C = [ ] NEW_LINE D = [ ] NEW_LINE for i in range ( 2 ** A ) : NEW_LINE INDENT C . append ( sum ( [ W [ a ] * ( i >> a & 1 ) for a in range ( A ) ] ) ) NEW_LINE DEDENT for j in range ( 2 ** B ) : NEW_LINE INDENT D . append ( sum ( [ W [ b + A ] * ( j >> b & 1 ) for b in range ( B ) ] ) ) NEW_LINE DEDENT ans = 0 NEW_LINE C = sorted ( C ) NEW_LINE D = sorted ( D ) NEW_LINE for c in C [ : bisect ( C , X ) ] : NEW_LINE INDENT ans += bisect ( D , X - c ) - bisect_left ( D , X - c ) NEW_LINE DEDENT print ( ans ) NEW_LINE
T301
import java . util . * ; import java . io . * ; import static java . lang . System . in ; public class Main { static long R , B , x , y ; public static void main ( String [ ] args ) throws IOException { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int X = sc . nextInt ( ) ; int [ ] w = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) w [ i ] = sc . nextInt ( ) ; long ans = help ( w , X ) ; System . out . println ( ans ) ; } static long help ( int [ ] w , int X ) { int n = w . length ; if ( n == 1 ) { return w [ 0 ] == X ? 1 : 0 ; } int ans = 0 ; int p = n / 2 ; HashMap < Integer , Integer > dic = getDic ( Arrays . copyOfRange ( w , p , n ) ) ; int [ ] first = Arrays . copyOfRange ( w , 0 , p ) ; for ( int i = 0 ; i < ( 1 << p ) ; i ++ ) { int cur = getSum ( first , i ) ; ans += dic . getOrDefault ( X - cur , 0 ) ; } return ans ; } static HashMap < Integer , Integer > getDic ( int [ ] a ) { int q = a . length ; HashMap < Integer , Integer > ans = new HashMap < > ( ) ; for ( int i = 0 ; i < ( 1 << q ) ; i ++ ) { int cur = getSum ( a , i ) ; int val = ans . getOrDefault ( cur , 0 ) + 1 ; ans . put ( cur , val ) ; } return ans ; } static int getSum ( int [ ] a , int mask ) { int sum = 0 ; for ( int i = 0 ; i < a . length ; i ++ ) { if ( ( mask & ( 1 << i ) ) > 0 ) sum += a [ i ] ; } return sum ; } }
import math , string , itertools , fractions , heapq , collections , re , array , bisect , sys , random , time , copy , functools NEW_LINE sys . setrecursionlimit ( 10 ** 7 ) NEW_LINE inf = 10 ** 20 NEW_LINE mod = 10 ** 9 + 7 NEW_LINE def LI ( ) : return [ int ( x ) for x in sys . stdin . readline ( ) . split ( ) ] NEW_LINE def LI_ ( ) : return [ int ( x ) - 1 for x in sys . stdin . readline ( ) . split ( ) ] NEW_LINE def LF ( ) : return [ float ( x ) for x in sys . stdin . readline ( ) . split ( ) ] NEW_LINE def LS ( ) : return sys . stdin . readline ( ) . split ( ) NEW_LINE def I ( ) : return int ( sys . stdin . readline ( ) ) NEW_LINE def F ( ) : return float ( sys . stdin . readline ( ) ) NEW_LINE def S ( ) : return input ( ) NEW_LINE def main ( ) : NEW_LINE INDENT N , X = LI ( ) NEW_LINE W = sorted ( [ I ( ) for _ in range ( N ) ] , reverse = True ) NEW_LINE d = collections . defaultdict ( int ) NEW_LINE d [ 0 ] = 1 NEW_LINE for c in W [ N // 2 : ] : NEW_LINE INDENT t = collections . defaultdict ( int ) NEW_LINE for k , v in d . items ( ) : NEW_LINE INDENT if k + c > X : NEW_LINE INDENT continue NEW_LINE DEDENT t [ k + c ] += v NEW_LINE DEDENT for k , v in t . items ( ) : NEW_LINE INDENT d [ k ] += v NEW_LINE DEDENT DEDENT d2 = collections . defaultdict ( int ) NEW_LINE d2 [ 0 ] = 1 NEW_LINE for c in W [ : N // 2 ] : NEW_LINE INDENT t = collections . defaultdict ( int ) NEW_LINE for k , v in d2 . items ( ) : NEW_LINE INDENT if k + c > X : NEW_LINE INDENT continue NEW_LINE DEDENT t [ k + c ] += v NEW_LINE DEDENT for k , v in t . items ( ) : NEW_LINE INDENT d2 [ k ] += v NEW_LINE DEDENT DEDENT r = 0 NEW_LINE for k , v in d . items ( ) : NEW_LINE INDENT r += v * d2 [ X - k ] NEW_LINE DEDENT return r NEW_LINE DEDENT print ( main ( ) ) NEW_LINE
T302
import java . util . * ; import java . io . * ; import static java . lang . System . in ; public class Main { static long R , B , x , y ; public static void main ( String [ ] args ) throws IOException { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int X = sc . nextInt ( ) ; int [ ] w = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) w [ i ] = sc . nextInt ( ) ; long ans = help ( w , X ) ; System . out . println ( ans ) ; } static long help ( int [ ] w , int X ) { int n = w . length ; if ( n == 1 ) { return w [ 0 ] == X ? 1 : 0 ; } int ans = 0 ; int p = n / 2 ; HashMap < Integer , Integer > dic = getDic ( Arrays . copyOfRange ( w , p , n ) ) ; int [ ] first = Arrays . copyOfRange ( w , 0 , p ) ; for ( int i = 0 ; i < ( 1 << p ) ; i ++ ) { int cur = getSum ( first , i ) ; ans += dic . getOrDefault ( X - cur , 0 ) ; } return ans ; } static HashMap < Integer , Integer > getDic ( int [ ] a ) { int q = a . length ; HashMap < Integer , Integer > ans = new HashMap < > ( ) ; for ( int i = 0 ; i < ( 1 << q ) ; i ++ ) { int cur = getSum ( a , i ) ; int val = ans . getOrDefault ( cur , 0 ) + 1 ; ans . put ( cur , val ) ; } return ans ; } static int getSum ( int [ ] a , int mask ) { int sum = 0 ; for ( int i = 0 ; i < a . length ; i ++ ) { if ( ( mask & ( 1 << i ) ) > 0 ) sum += a [ i ] ; } return sum ; } }
def make ( n , r ) : NEW_LINE INDENT v = { 0 : 1 } NEW_LINE for _ in range ( n ) : NEW_LINE INDENT s = int ( input ( ) ) NEW_LINE w = dict ( v ) NEW_LINE for k , val in v . items ( ) : NEW_LINE INDENT w [ k + s ] = v . get ( k + s , 0 ) + val NEW_LINE DEDENT v = w NEW_LINE DEDENT return sorted ( v . items ( ) , reverse = r ) NEW_LINE DEDENT N , X = map ( int , input ( ) . split ( ) ) NEW_LINE S = make ( N // 2 , 0 ) NEW_LINE T = make ( N - N // 2 , 1 ) NEW_LINE ans = 0 NEW_LINE it = iter ( T ) NEW_LINE nn = ( None , None ) NEW_LINE t , w = next ( it , nn ) NEW_LINE for s , v in S : NEW_LINE INDENT while w and s + t > X : NEW_LINE INDENT t , w = next ( it , nn ) NEW_LINE DEDENT if w and s + t == X : NEW_LINE INDENT ans += v * w NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE
T303
import java . util . * ; import java . io . * ; import static java . lang . System . in ; public class Main { static long R , B , x , y ; public static void main ( String [ ] args ) throws IOException { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int X = sc . nextInt ( ) ; int [ ] w = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) w [ i ] = sc . nextInt ( ) ; long ans = help ( w , X ) ; System . out . println ( ans ) ; } static long help ( int [ ] w , int X ) { int n = w . length ; if ( n == 1 ) { return w [ 0 ] == X ? 1 : 0 ; } int ans = 0 ; int p = n / 2 ; HashMap < Integer , Integer > dic = getDic ( Arrays . copyOfRange ( w , p , n ) ) ; int [ ] first = Arrays . copyOfRange ( w , 0 , p ) ; for ( int i = 0 ; i < ( 1 << p ) ; i ++ ) { int cur = getSum ( first , i ) ; ans += dic . getOrDefault ( X - cur , 0 ) ; } return ans ; } static HashMap < Integer , Integer > getDic ( int [ ] a ) { int q = a . length ; HashMap < Integer , Integer > ans = new HashMap < > ( ) ; for ( int i = 0 ; i < ( 1 << q ) ; i ++ ) { int cur = getSum ( a , i ) ; int val = ans . getOrDefault ( cur , 0 ) + 1 ; ans . put ( cur , val ) ; } return ans ; } static int getSum ( int [ ] a , int mask ) { int sum = 0 ; for ( int i = 0 ; i < a . length ; i ++ ) { if ( ( mask & ( 1 << i ) ) > 0 ) sum += a [ i ] ; } return sum ; } }
from collections import defaultdict NEW_LINE import sys , heapq , bisect , math , itertools , string , queue , datetime NEW_LINE sys . setrecursionlimit ( 10 ** 8 ) NEW_LINE INF = float ( ' inf ' ) NEW_LINE mod = 10 ** 9 + 7 NEW_LINE eps = 10 ** - 7 NEW_LINE AtoZ = [ chr ( i ) for i in range ( 65 , 65 + 26 ) ] NEW_LINE atoz = [ chr ( i ) for i in range ( 97 , 97 + 26 ) ] NEW_LINE def inpl ( ) : return list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE def inpl_s ( ) : return list ( input ( ) . split ( ) ) NEW_LINE N , X = inpl ( ) NEW_LINE ww = [ int ( input ( ) ) for i in range ( N ) ] NEW_LINE k = N // 2 NEW_LINE flags1 = itertools . product ( [ 0 , 1 ] , repeat = k ) NEW_LINE flags2 = itertools . product ( [ 0 , 1 ] , repeat = N - k ) NEW_LINE dd1 = defaultdict ( int ) NEW_LINE ed = [ ] NEW_LINE dd2 = defaultdict ( int ) NEW_LINE for flag in flags1 : NEW_LINE INDENT tmp = 0 NEW_LINE for i , fl in enumerate ( flag ) : NEW_LINE INDENT if fl : NEW_LINE INDENT tmp += ww [ i ] NEW_LINE DEDENT DEDENT if dd1 [ tmp ] == 0 : NEW_LINE INDENT ed . append ( tmp ) NEW_LINE DEDENT dd1 [ tmp ] += 1 NEW_LINE DEDENT for flag in flags2 : NEW_LINE INDENT tmp = 0 NEW_LINE for i , fl in enumerate ( flag ) : NEW_LINE INDENT if fl : NEW_LINE INDENT tmp += ww [ i + k ] NEW_LINE DEDENT DEDENT dd2 [ tmp ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE for e in ed : NEW_LINE INDENT ans += dd1 [ e ] * dd2 [ X - e ] NEW_LINE DEDENT print ( ans ) NEW_LINE
T304
import java . util . * ; import java . io . * ; import static java . lang . System . in ; public class Main { static long R , B , x , y ; public static void main ( String [ ] args ) throws IOException { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int X = sc . nextInt ( ) ; int [ ] w = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) w [ i ] = sc . nextInt ( ) ; long ans = help ( w , X ) ; System . out . println ( ans ) ; } static long help ( int [ ] w , int X ) { int n = w . length ; if ( n == 1 ) { return w [ 0 ] == X ? 1 : 0 ; } int ans = 0 ; int p = n / 2 ; HashMap < Integer , Integer > dic = getDic ( Arrays . copyOfRange ( w , p , n ) ) ; int [ ] first = Arrays . copyOfRange ( w , 0 , p ) ; for ( int i = 0 ; i < ( 1 << p ) ; i ++ ) { int cur = getSum ( first , i ) ; ans += dic . getOrDefault ( X - cur , 0 ) ; } return ans ; } static HashMap < Integer , Integer > getDic ( int [ ] a ) { int q = a . length ; HashMap < Integer , Integer > ans = new HashMap < > ( ) ; for ( int i = 0 ; i < ( 1 << q ) ; i ++ ) { int cur = getSum ( a , i ) ; int val = ans . getOrDefault ( cur , 0 ) + 1 ; ans . put ( cur , val ) ; } return ans ; } static int getSum ( int [ ] a , int mask ) { int sum = 0 ; for ( int i = 0 ; i < a . length ; i ++ ) { if ( ( mask & ( 1 << i ) ) > 0 ) sum += a [ i ] ; } return sum ; } }
import sys NEW_LINE from collections import defaultdict , Counter NEW_LINE from itertools import product , groupby , count , permutations , combinations NEW_LINE from math import pi , sqrt , ceil , floor NEW_LINE from collections import deque NEW_LINE from bisect import bisect , bisect_left , bisect_right NEW_LINE from string import ascii_lowercase NEW_LINE INF = float ( " inf " ) NEW_LINE sys . setrecursionlimit ( 10 ** 7 ) NEW_LINE dy = [ 0 , - 1 , 0 , 1 ] NEW_LINE dx = [ 1 , 0 , - 1 , 0 ] NEW_LINE def inside ( y : int , x : int , H : int , W : int ) -> bool : return 0 <= y < H and 0 <= x < W NEW_LINE def main ( ) : NEW_LINE INDENT N , X = map ( int , input ( ) . split ( ) ) NEW_LINE w_list = [ int ( input ( ) ) for _ in range ( N ) ] NEW_LINE w_list1 , w_list2 = w_list [ : N // 2 ] , w_list [ N // 2 : ] NEW_LINE n = len ( w_list1 ) NEW_LINE s = [ ] NEW_LINE for b in range ( 2 ** n ) : NEW_LINE INDENT total = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if b & 1 << i : NEW_LINE INDENT total += w_list1 [ i ] NEW_LINE DEDENT DEDENT s . append ( total ) NEW_LINE DEDENT n = len ( w_list2 ) NEW_LINE d = defaultdict ( int ) NEW_LINE for b in range ( 2 ** n ) : NEW_LINE INDENT total = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if b & 1 << i : NEW_LINE INDENT total += w_list2 [ i ] NEW_LINE DEDENT DEDENT d [ total ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE for a in s : NEW_LINE INDENT if a > X : NEW_LINE INDENT continue NEW_LINE DEDENT ans += d [ X - a ] NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT
T305
import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int num = sc . nextInt ( ) * 2 ; long length [ ] = new long [ num ] ; for ( int i = 0 ; i < num ; i ++ ) { length [ i ] = sc . nextLong ( ) ; } Arrays . sort ( length ) ; long ans = 0 ; for ( int i = 0 ; i < num ; i += 2 ) { ans += length [ i ] ; } System . out . println ( ans ) ; } }
import math NEW_LINE def getInt ( ) : return int ( input ( ) ) NEW_LINE def getIntList ( ) : return [ int ( x ) for x in input ( ) . split ( ) ] NEW_LINE def zeros ( n ) : return [ 0 ] * n NEW_LINE def zeros2 ( n , m ) : return [ zeros ( m ) for i in range ( n ) ] NEW_LINE class Debug ( ) : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . debug = True NEW_LINE DEDENT def off ( self ) : NEW_LINE INDENT self . debug = False NEW_LINE DEDENT def dmp ( self , x , cmt = ' ' ) : NEW_LINE INDENT if self . debug : NEW_LINE INDENT if cmt != ' ' : NEW_LINE INDENT w = cmt + ' : ▁ ' + str ( x ) NEW_LINE DEDENT else : NEW_LINE INDENT w = str ( x ) NEW_LINE DEDENT print ( w ) NEW_LINE DEDENT return x NEW_LINE DEDENT DEDENT def prob ( ) : NEW_LINE INDENT d = Debug ( ) NEW_LINE d . off ( ) NEW_LINE N = getInt ( ) NEW_LINE d . dmp ( ( N ) , ' N ' ) NEW_LINE L = getIntList ( ) NEW_LINE d . dmp ( ( L ) , ' L ' ) NEW_LINE L . sort ( ) NEW_LINE count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT count += min ( L [ i * 2 ] , L [ i * 2 + 1 ] ) NEW_LINE DEDENT return count NEW_LINE DEDENT ans = prob ( ) NEW_LINE print ( ans ) NEW_LINE
T306
import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int num = sc . nextInt ( ) * 2 ; long length [ ] = new long [ num ] ; for ( int i = 0 ; i < num ; i ++ ) { length [ i ] = sc . nextLong ( ) ; } Arrays . sort ( length ) ; long ans = 0 ; for ( int i = 0 ; i < num ; i += 2 ) { ans += length [ i ] ; } System . out . println ( ans ) ; } }
n , l = open ( 0 ) ; print ( sum ( sorted ( map ( int , l . split ( ) ) ) [ : : 2 ] ) ) NEW_LINE
T307
import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int num = sc . nextInt ( ) * 2 ; long length [ ] = new long [ num ] ; for ( int i = 0 ; i < num ; i ++ ) { length [ i ] = sc . nextLong ( ) ; } Arrays . sort ( length ) ; long ans = 0 ; for ( int i = 0 ; i < num ; i += 2 ) { ans += length [ i ] ; } System . out . println ( ans ) ; } }
N = int ( input ( ) ) NEW_LINE L = [ ] NEW_LINE for n in [ int ( i ) for i in input ( ) . split ( ) ] : NEW_LINE INDENT L . append ( n ) NEW_LINE DEDENT L . sort ( ) NEW_LINE SUM = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT x = L . pop ( ) NEW_LINE y = L . pop ( ) NEW_LINE if x < y : NEW_LINE INDENT SUM = SUM + x NEW_LINE DEDENT elif x >= y : NEW_LINE INDENT SUM = SUM + y NEW_LINE DEDENT DEDENT print ( SUM ) NEW_LINE
T308
import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int num = sc . nextInt ( ) * 2 ; long length [ ] = new long [ num ] ; for ( int i = 0 ; i < num ; i ++ ) { length [ i ] = sc . nextLong ( ) ; } Arrays . sort ( length ) ; long ans = 0 ; for ( int i = 0 ; i < num ; i += 2 ) { ans += length [ i ] ; } System . out . println ( ans ) ; } }
N = input ( ) NEW_LINE L = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE def quicksort ( array ) : NEW_LINE INDENT if len ( array ) < 2 : NEW_LINE INDENT return array NEW_LINE DEDENT else : NEW_LINE INDENT pivot = array [ 0 ] NEW_LINE less = [ i for i in array [ 1 : ] if i <= pivot ] NEW_LINE greater = [ i for i in array [ 1 : ] if i > pivot ] NEW_LINE return quicksort ( less ) + [ pivot ] + quicksort ( greater ) NEW_LINE DEDENT DEDENT guzai = 0 NEW_LINE for i in range ( int ( N ) ) : NEW_LINE INDENT guzai += int ( quicksort ( L ) [ int ( 2 * i ) ] ) NEW_LINE DEDENT print ( guzai ) NEW_LINE
T309
import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int num = sc . nextInt ( ) * 2 ; long length [ ] = new long [ num ] ; for ( int i = 0 ; i < num ; i ++ ) { length [ i ] = sc . nextLong ( ) ; } Arrays . sort ( length ) ; long ans = 0 ; for ( int i = 0 ; i < num ; i += 2 ) { ans += length [ i ] ; } System . out . println ( ans ) ; } }
import bisect NEW_LINE N = input ( ) NEW_LINE Ls = map ( int , input ( ) . split ( " ▁ " ) ) NEW_LINE Ls = sorted ( Ls ) NEW_LINE ans = sum ( [ L for i , L in enumerate ( Ls ) if i % 2 == 0 ] ) NEW_LINE print ( ans ) NEW_LINE
T310
import java . util . * ; import static java . lang . System . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int N = sc . nextInt ( ) ; Integer [ ] L = new Integer [ 2 * N ] ; for ( int i = 0 ; i < 2 * N ; i ++ ) { L [ i ] = sc . nextInt ( ) ; } Arrays . sort ( L , Collections . reverseOrder ( ) ) ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { ans += Math . min ( L [ 2 * i ] , L [ 2 * i + 1 ] ) ; } out . println ( ans ) ; } }
import math NEW_LINE def getInt ( ) : return int ( input ( ) ) NEW_LINE def getIntList ( ) : return [ int ( x ) for x in input ( ) . split ( ) ] NEW_LINE def zeros ( n ) : return [ 0 ] * n NEW_LINE def zeros2 ( n , m ) : return [ zeros ( m ) for i in range ( n ) ] NEW_LINE class Debug ( ) : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . debug = True NEW_LINE DEDENT def off ( self ) : NEW_LINE INDENT self . debug = False NEW_LINE DEDENT def dmp ( self , x , cmt = ' ' ) : NEW_LINE INDENT if self . debug : NEW_LINE INDENT if cmt != ' ' : NEW_LINE INDENT w = cmt + ' : ▁ ' + str ( x ) NEW_LINE DEDENT else : NEW_LINE INDENT w = str ( x ) NEW_LINE DEDENT print ( w ) NEW_LINE DEDENT return x NEW_LINE DEDENT DEDENT def prob ( ) : NEW_LINE INDENT d = Debug ( ) NEW_LINE d . off ( ) NEW_LINE N = getInt ( ) NEW_LINE d . dmp ( ( N ) , ' N ' ) NEW_LINE L = getIntList ( ) NEW_LINE d . dmp ( ( L ) , ' L ' ) NEW_LINE L . sort ( ) NEW_LINE count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT count += min ( L [ i * 2 ] , L [ i * 2 + 1 ] ) NEW_LINE DEDENT return count NEW_LINE DEDENT ans = prob ( ) NEW_LINE print ( ans ) NEW_LINE
T311
import java . util . * ; import static java . lang . System . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int N = sc . nextInt ( ) ; Integer [ ] L = new Integer [ 2 * N ] ; for ( int i = 0 ; i < 2 * N ; i ++ ) { L [ i ] = sc . nextInt ( ) ; } Arrays . sort ( L , Collections . reverseOrder ( ) ) ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { ans += Math . min ( L [ 2 * i ] , L [ 2 * i + 1 ] ) ; } out . println ( ans ) ; } }
n , l = open ( 0 ) ; print ( sum ( sorted ( map ( int , l . split ( ) ) ) [ : : 2 ] ) ) NEW_LINE
T312
import java . util . * ; import static java . lang . System . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int N = sc . nextInt ( ) ; Integer [ ] L = new Integer [ 2 * N ] ; for ( int i = 0 ; i < 2 * N ; i ++ ) { L [ i ] = sc . nextInt ( ) ; } Arrays . sort ( L , Collections . reverseOrder ( ) ) ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { ans += Math . min ( L [ 2 * i ] , L [ 2 * i + 1 ] ) ; } out . println ( ans ) ; } }
N = int ( input ( ) ) NEW_LINE L = [ ] NEW_LINE for n in [ int ( i ) for i in input ( ) . split ( ) ] : NEW_LINE INDENT L . append ( n ) NEW_LINE DEDENT L . sort ( ) NEW_LINE SUM = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT x = L . pop ( ) NEW_LINE y = L . pop ( ) NEW_LINE if x < y : NEW_LINE INDENT SUM = SUM + x NEW_LINE DEDENT elif x >= y : NEW_LINE INDENT SUM = SUM + y NEW_LINE DEDENT DEDENT print ( SUM ) NEW_LINE
T313
import java . util . * ; import static java . lang . System . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int N = sc . nextInt ( ) ; Integer [ ] L = new Integer [ 2 * N ] ; for ( int i = 0 ; i < 2 * N ; i ++ ) { L [ i ] = sc . nextInt ( ) ; } Arrays . sort ( L , Collections . reverseOrder ( ) ) ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { ans += Math . min ( L [ 2 * i ] , L [ 2 * i + 1 ] ) ; } out . println ( ans ) ; } }
N = input ( ) NEW_LINE L = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE def quicksort ( array ) : NEW_LINE INDENT if len ( array ) < 2 : NEW_LINE INDENT return array NEW_LINE DEDENT else : NEW_LINE INDENT pivot = array [ 0 ] NEW_LINE less = [ i for i in array [ 1 : ] if i <= pivot ] NEW_LINE greater = [ i for i in array [ 1 : ] if i > pivot ] NEW_LINE return quicksort ( less ) + [ pivot ] + quicksort ( greater ) NEW_LINE DEDENT DEDENT guzai = 0 NEW_LINE for i in range ( int ( N ) ) : NEW_LINE INDENT guzai += int ( quicksort ( L ) [ int ( 2 * i ) ] ) NEW_LINE DEDENT print ( guzai ) NEW_LINE
T314
import java . util . * ; import static java . lang . System . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int N = sc . nextInt ( ) ; Integer [ ] L = new Integer [ 2 * N ] ; for ( int i = 0 ; i < 2 * N ; i ++ ) { L [ i ] = sc . nextInt ( ) ; } Arrays . sort ( L , Collections . reverseOrder ( ) ) ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { ans += Math . min ( L [ 2 * i ] , L [ 2 * i + 1 ] ) ; } out . println ( ans ) ; } }
import bisect NEW_LINE N = input ( ) NEW_LINE Ls = map ( int , input ( ) . split ( " ▁ " ) ) NEW_LINE Ls = sorted ( Ls ) NEW_LINE ans = sum ( [ L for i , L in enumerate ( Ls ) if i % 2 == 0 ] ) NEW_LINE print ( ans ) NEW_LINE
T315
import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStream ; import java . io . InputStreamReader ; import java . lang . reflect . Array ; import java . util . Arrays ; import java . util . StringTokenizer ; public class Main { static BufferedReader reader ; static StringTokenizer tokenizer ; static void init ( InputStream input ) { reader = new BufferedReader ( new InputStreamReader ( input ) ) ; tokenizer = new StringTokenizer ( " " ) ; } static String next ( ) throws IOException { while ( ! tokenizer . hasMoreTokens ( ) ) { tokenizer = new StringTokenizer ( reader . readLine ( ) ) ; } return tokenizer . nextToken ( ) ; } static int nextInt ( ) throws IOException { return Integer . parseInt ( next ( ) ) ; } static double nextDouble ( ) throws IOException { return Double . parseDouble ( next ( ) ) ; } public static void main ( String [ ] args ) throws IOException { init ( System . in ) ; int n = nextInt ( ) ; int A [ ] = new int [ 2 * n ] ; for ( int i = 0 ; i < 2 * n ; i ++ ) { A [ i ] = nextInt ( ) ; } Arrays . sort ( A ) ; int sum = 0 ; for ( int i = 0 ; i < 2 * n ; i += 2 ) { if ( A [ i ] < A [ i + 1 ] ) { sum += A [ i ] ; } else { sum += A [ i + 1 ] ; } } System . out . println ( sum ) ; } }
import math NEW_LINE def getInt ( ) : return int ( input ( ) ) NEW_LINE def getIntList ( ) : return [ int ( x ) for x in input ( ) . split ( ) ] NEW_LINE def zeros ( n ) : return [ 0 ] * n NEW_LINE def zeros2 ( n , m ) : return [ zeros ( m ) for i in range ( n ) ] NEW_LINE class Debug ( ) : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . debug = True NEW_LINE DEDENT def off ( self ) : NEW_LINE INDENT self . debug = False NEW_LINE DEDENT def dmp ( self , x , cmt = ' ' ) : NEW_LINE INDENT if self . debug : NEW_LINE INDENT if cmt != ' ' : NEW_LINE INDENT w = cmt + ' : ▁ ' + str ( x ) NEW_LINE DEDENT else : NEW_LINE INDENT w = str ( x ) NEW_LINE DEDENT print ( w ) NEW_LINE DEDENT return x NEW_LINE DEDENT DEDENT def prob ( ) : NEW_LINE INDENT d = Debug ( ) NEW_LINE d . off ( ) NEW_LINE N = getInt ( ) NEW_LINE d . dmp ( ( N ) , ' N ' ) NEW_LINE L = getIntList ( ) NEW_LINE d . dmp ( ( L ) , ' L ' ) NEW_LINE L . sort ( ) NEW_LINE count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT count += min ( L [ i * 2 ] , L [ i * 2 + 1 ] ) NEW_LINE DEDENT return count NEW_LINE DEDENT ans = prob ( ) NEW_LINE print ( ans ) NEW_LINE
T316
import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStream ; import java . io . InputStreamReader ; import java . lang . reflect . Array ; import java . util . Arrays ; import java . util . StringTokenizer ; public class Main { static BufferedReader reader ; static StringTokenizer tokenizer ; static void init ( InputStream input ) { reader = new BufferedReader ( new InputStreamReader ( input ) ) ; tokenizer = new StringTokenizer ( " " ) ; } static String next ( ) throws IOException { while ( ! tokenizer . hasMoreTokens ( ) ) { tokenizer = new StringTokenizer ( reader . readLine ( ) ) ; } return tokenizer . nextToken ( ) ; } static int nextInt ( ) throws IOException { return Integer . parseInt ( next ( ) ) ; } static double nextDouble ( ) throws IOException { return Double . parseDouble ( next ( ) ) ; } public static void main ( String [ ] args ) throws IOException { init ( System . in ) ; int n = nextInt ( ) ; int A [ ] = new int [ 2 * n ] ; for ( int i = 0 ; i < 2 * n ; i ++ ) { A [ i ] = nextInt ( ) ; } Arrays . sort ( A ) ; int sum = 0 ; for ( int i = 0 ; i < 2 * n ; i += 2 ) { if ( A [ i ] < A [ i + 1 ] ) { sum += A [ i ] ; } else { sum += A [ i + 1 ] ; } } System . out . println ( sum ) ; } }
n , l = open ( 0 ) ; print ( sum ( sorted ( map ( int , l . split ( ) ) ) [ : : 2 ] ) ) NEW_LINE
T317
import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStream ; import java . io . InputStreamReader ; import java . lang . reflect . Array ; import java . util . Arrays ; import java . util . StringTokenizer ; public class Main { static BufferedReader reader ; static StringTokenizer tokenizer ; static void init ( InputStream input ) { reader = new BufferedReader ( new InputStreamReader ( input ) ) ; tokenizer = new StringTokenizer ( " " ) ; } static String next ( ) throws IOException { while ( ! tokenizer . hasMoreTokens ( ) ) { tokenizer = new StringTokenizer ( reader . readLine ( ) ) ; } return tokenizer . nextToken ( ) ; } static int nextInt ( ) throws IOException { return Integer . parseInt ( next ( ) ) ; } static double nextDouble ( ) throws IOException { return Double . parseDouble ( next ( ) ) ; } public static void main ( String [ ] args ) throws IOException { init ( System . in ) ; int n = nextInt ( ) ; int A [ ] = new int [ 2 * n ] ; for ( int i = 0 ; i < 2 * n ; i ++ ) { A [ i ] = nextInt ( ) ; } Arrays . sort ( A ) ; int sum = 0 ; for ( int i = 0 ; i < 2 * n ; i += 2 ) { if ( A [ i ] < A [ i + 1 ] ) { sum += A [ i ] ; } else { sum += A [ i + 1 ] ; } } System . out . println ( sum ) ; } }
N = int ( input ( ) ) NEW_LINE L = [ ] NEW_LINE for n in [ int ( i ) for i in input ( ) . split ( ) ] : NEW_LINE INDENT L . append ( n ) NEW_LINE DEDENT L . sort ( ) NEW_LINE SUM = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT x = L . pop ( ) NEW_LINE y = L . pop ( ) NEW_LINE if x < y : NEW_LINE INDENT SUM = SUM + x NEW_LINE DEDENT elif x >= y : NEW_LINE INDENT SUM = SUM + y NEW_LINE DEDENT DEDENT print ( SUM ) NEW_LINE
T318
import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStream ; import java . io . InputStreamReader ; import java . lang . reflect . Array ; import java . util . Arrays ; import java . util . StringTokenizer ; public class Main { static BufferedReader reader ; static StringTokenizer tokenizer ; static void init ( InputStream input ) { reader = new BufferedReader ( new InputStreamReader ( input ) ) ; tokenizer = new StringTokenizer ( " " ) ; } static String next ( ) throws IOException { while ( ! tokenizer . hasMoreTokens ( ) ) { tokenizer = new StringTokenizer ( reader . readLine ( ) ) ; } return tokenizer . nextToken ( ) ; } static int nextInt ( ) throws IOException { return Integer . parseInt ( next ( ) ) ; } static double nextDouble ( ) throws IOException { return Double . parseDouble ( next ( ) ) ; } public static void main ( String [ ] args ) throws IOException { init ( System . in ) ; int n = nextInt ( ) ; int A [ ] = new int [ 2 * n ] ; for ( int i = 0 ; i < 2 * n ; i ++ ) { A [ i ] = nextInt ( ) ; } Arrays . sort ( A ) ; int sum = 0 ; for ( int i = 0 ; i < 2 * n ; i += 2 ) { if ( A [ i ] < A [ i + 1 ] ) { sum += A [ i ] ; } else { sum += A [ i + 1 ] ; } } System . out . println ( sum ) ; } }
N = input ( ) NEW_LINE L = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE def quicksort ( array ) : NEW_LINE INDENT if len ( array ) < 2 : NEW_LINE INDENT return array NEW_LINE DEDENT else : NEW_LINE INDENT pivot = array [ 0 ] NEW_LINE less = [ i for i in array [ 1 : ] if i <= pivot ] NEW_LINE greater = [ i for i in array [ 1 : ] if i > pivot ] NEW_LINE return quicksort ( less ) + [ pivot ] + quicksort ( greater ) NEW_LINE DEDENT DEDENT guzai = 0 NEW_LINE for i in range ( int ( N ) ) : NEW_LINE INDENT guzai += int ( quicksort ( L ) [ int ( 2 * i ) ] ) NEW_LINE DEDENT print ( guzai ) NEW_LINE
T319
import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStream ; import java . io . InputStreamReader ; import java . lang . reflect . Array ; import java . util . Arrays ; import java . util . StringTokenizer ; public class Main { static BufferedReader reader ; static StringTokenizer tokenizer ; static void init ( InputStream input ) { reader = new BufferedReader ( new InputStreamReader ( input ) ) ; tokenizer = new StringTokenizer ( " " ) ; } static String next ( ) throws IOException { while ( ! tokenizer . hasMoreTokens ( ) ) { tokenizer = new StringTokenizer ( reader . readLine ( ) ) ; } return tokenizer . nextToken ( ) ; } static int nextInt ( ) throws IOException { return Integer . parseInt ( next ( ) ) ; } static double nextDouble ( ) throws IOException { return Double . parseDouble ( next ( ) ) ; } public static void main ( String [ ] args ) throws IOException { init ( System . in ) ; int n = nextInt ( ) ; int A [ ] = new int [ 2 * n ] ; for ( int i = 0 ; i < 2 * n ; i ++ ) { A [ i ] = nextInt ( ) ; } Arrays . sort ( A ) ; int sum = 0 ; for ( int i = 0 ; i < 2 * n ; i += 2 ) { if ( A [ i ] < A [ i + 1 ] ) { sum += A [ i ] ; } else { sum += A [ i + 1 ] ; } } System . out . println ( sum ) ; } }
import bisect NEW_LINE N = input ( ) NEW_LINE Ls = map ( int , input ( ) . split ( " ▁ " ) ) NEW_LINE Ls = sorted ( Ls ) NEW_LINE ans = sum ( [ L for i , L in enumerate ( Ls ) if i % 2 == 0 ] ) NEW_LINE print ( ans ) NEW_LINE
T320
import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; String line = sc . nextLine ( ) ; int N = Integer . parseInt ( line ) ; line = sc . nextLine ( ) ; String [ ] l1l2 = line . split ( " ▁ " ) ; List < Integer > kusi = new ArrayList < > ( ) ; for ( int i = 0 ; i < l1l2 . length ; i ++ ) { kusi . add ( Integer . parseInt ( l1l2 [ i ] ) ) ; } Collections . sort ( kusi ) ; calc ( kusi ) ; } public static void calc ( List < Integer > list ) { int total = 0 ; for ( int i = 0 ; i < list . size ( ) ; i = i + 2 ) { total += list . get ( i ) ; } System . out . println ( total ) ; } }
import math NEW_LINE def getInt ( ) : return int ( input ( ) ) NEW_LINE def getIntList ( ) : return [ int ( x ) for x in input ( ) . split ( ) ] NEW_LINE def zeros ( n ) : return [ 0 ] * n NEW_LINE def zeros2 ( n , m ) : return [ zeros ( m ) for i in range ( n ) ] NEW_LINE class Debug ( ) : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . debug = True NEW_LINE DEDENT def off ( self ) : NEW_LINE INDENT self . debug = False NEW_LINE DEDENT def dmp ( self , x , cmt = ' ' ) : NEW_LINE INDENT if self . debug : NEW_LINE INDENT if cmt != ' ' : NEW_LINE INDENT w = cmt + ' : ▁ ' + str ( x ) NEW_LINE DEDENT else : NEW_LINE INDENT w = str ( x ) NEW_LINE DEDENT print ( w ) NEW_LINE DEDENT return x NEW_LINE DEDENT DEDENT def prob ( ) : NEW_LINE INDENT d = Debug ( ) NEW_LINE d . off ( ) NEW_LINE N = getInt ( ) NEW_LINE d . dmp ( ( N ) , ' N ' ) NEW_LINE L = getIntList ( ) NEW_LINE d . dmp ( ( L ) , ' L ' ) NEW_LINE L . sort ( ) NEW_LINE count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT count += min ( L [ i * 2 ] , L [ i * 2 + 1 ] ) NEW_LINE DEDENT return count NEW_LINE DEDENT ans = prob ( ) NEW_LINE print ( ans ) NEW_LINE
T321
import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; String line = sc . nextLine ( ) ; int N = Integer . parseInt ( line ) ; line = sc . nextLine ( ) ; String [ ] l1l2 = line . split ( " ▁ " ) ; List < Integer > kusi = new ArrayList < > ( ) ; for ( int i = 0 ; i < l1l2 . length ; i ++ ) { kusi . add ( Integer . parseInt ( l1l2 [ i ] ) ) ; } Collections . sort ( kusi ) ; calc ( kusi ) ; } public static void calc ( List < Integer > list ) { int total = 0 ; for ( int i = 0 ; i < list . size ( ) ; i = i + 2 ) { total += list . get ( i ) ; } System . out . println ( total ) ; } }
n , l = open ( 0 ) ; print ( sum ( sorted ( map ( int , l . split ( ) ) ) [ : : 2 ] ) ) NEW_LINE
T322
import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; String line = sc . nextLine ( ) ; int N = Integer . parseInt ( line ) ; line = sc . nextLine ( ) ; String [ ] l1l2 = line . split ( " ▁ " ) ; List < Integer > kusi = new ArrayList < > ( ) ; for ( int i = 0 ; i < l1l2 . length ; i ++ ) { kusi . add ( Integer . parseInt ( l1l2 [ i ] ) ) ; } Collections . sort ( kusi ) ; calc ( kusi ) ; } public static void calc ( List < Integer > list ) { int total = 0 ; for ( int i = 0 ; i < list . size ( ) ; i = i + 2 ) { total += list . get ( i ) ; } System . out . println ( total ) ; } }
N = int ( input ( ) ) NEW_LINE L = [ ] NEW_LINE for n in [ int ( i ) for i in input ( ) . split ( ) ] : NEW_LINE INDENT L . append ( n ) NEW_LINE DEDENT L . sort ( ) NEW_LINE SUM = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT x = L . pop ( ) NEW_LINE y = L . pop ( ) NEW_LINE if x < y : NEW_LINE INDENT SUM = SUM + x NEW_LINE DEDENT elif x >= y : NEW_LINE INDENT SUM = SUM + y NEW_LINE DEDENT DEDENT print ( SUM ) NEW_LINE
T323
import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; String line = sc . nextLine ( ) ; int N = Integer . parseInt ( line ) ; line = sc . nextLine ( ) ; String [ ] l1l2 = line . split ( " ▁ " ) ; List < Integer > kusi = new ArrayList < > ( ) ; for ( int i = 0 ; i < l1l2 . length ; i ++ ) { kusi . add ( Integer . parseInt ( l1l2 [ i ] ) ) ; } Collections . sort ( kusi ) ; calc ( kusi ) ; } public static void calc ( List < Integer > list ) { int total = 0 ; for ( int i = 0 ; i < list . size ( ) ; i = i + 2 ) { total += list . get ( i ) ; } System . out . println ( total ) ; } }
N = input ( ) NEW_LINE L = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE def quicksort ( array ) : NEW_LINE INDENT if len ( array ) < 2 : NEW_LINE INDENT return array NEW_LINE DEDENT else : NEW_LINE INDENT pivot = array [ 0 ] NEW_LINE less = [ i for i in array [ 1 : ] if i <= pivot ] NEW_LINE greater = [ i for i in array [ 1 : ] if i > pivot ] NEW_LINE return quicksort ( less ) + [ pivot ] + quicksort ( greater ) NEW_LINE DEDENT DEDENT guzai = 0 NEW_LINE for i in range ( int ( N ) ) : NEW_LINE INDENT guzai += int ( quicksort ( L ) [ int ( 2 * i ) ] ) NEW_LINE DEDENT print ( guzai ) NEW_LINE
T324
import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; String line = sc . nextLine ( ) ; int N = Integer . parseInt ( line ) ; line = sc . nextLine ( ) ; String [ ] l1l2 = line . split ( " ▁ " ) ; List < Integer > kusi = new ArrayList < > ( ) ; for ( int i = 0 ; i < l1l2 . length ; i ++ ) { kusi . add ( Integer . parseInt ( l1l2 [ i ] ) ) ; } Collections . sort ( kusi ) ; calc ( kusi ) ; } public static void calc ( List < Integer > list ) { int total = 0 ; for ( int i = 0 ; i < list . size ( ) ; i = i + 2 ) { total += list . get ( i ) ; } System . out . println ( total ) ; } }
import bisect NEW_LINE N = input ( ) NEW_LINE Ls = map ( int , input ( ) . split ( " ▁ " ) ) NEW_LINE Ls = sorted ( Ls ) NEW_LINE ans = sum ( [ L for i , L in enumerate ( Ls ) if i % 2 == 0 ] ) NEW_LINE print ( ans ) NEW_LINE
T325
import java . util . Arrays ; import java . util . Scanner ; public class Main { public static void main ( String [ ] args ) { Scanner scanner = new Scanner ( System . in ) ; int spitCount = Integer . valueOf ( scanner . nextLine ( ) ) * 2 ; int spits [ ] = new int [ spitCount ] ; for ( int i = 0 ; i < spitCount ; i ++ ) { spits [ i ] = scanner . nextInt ( ) ; } Arrays . sort ( spits ) ; int dishSum = 0 ; for ( int i = 0 ; i < spitCount ; i ++ ) { if ( i % 2 == 0 ) { dishSum += spits [ i ] ; } } System . out . println ( dishSum ) ; } }
import math NEW_LINE def getInt ( ) : return int ( input ( ) ) NEW_LINE def getIntList ( ) : return [ int ( x ) for x in input ( ) . split ( ) ] NEW_LINE def zeros ( n ) : return [ 0 ] * n NEW_LINE def zeros2 ( n , m ) : return [ zeros ( m ) for i in range ( n ) ] NEW_LINE class Debug ( ) : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . debug = True NEW_LINE DEDENT def off ( self ) : NEW_LINE INDENT self . debug = False NEW_LINE DEDENT def dmp ( self , x , cmt = ' ' ) : NEW_LINE INDENT if self . debug : NEW_LINE INDENT if cmt != ' ' : NEW_LINE INDENT w = cmt + ' : ▁ ' + str ( x ) NEW_LINE DEDENT else : NEW_LINE INDENT w = str ( x ) NEW_LINE DEDENT print ( w ) NEW_LINE DEDENT return x NEW_LINE DEDENT DEDENT def prob ( ) : NEW_LINE INDENT d = Debug ( ) NEW_LINE d . off ( ) NEW_LINE N = getInt ( ) NEW_LINE d . dmp ( ( N ) , ' N ' ) NEW_LINE L = getIntList ( ) NEW_LINE d . dmp ( ( L ) , ' L ' ) NEW_LINE L . sort ( ) NEW_LINE count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT count += min ( L [ i * 2 ] , L [ i * 2 + 1 ] ) NEW_LINE DEDENT return count NEW_LINE DEDENT ans = prob ( ) NEW_LINE print ( ans ) NEW_LINE
T326
import java . util . Arrays ; import java . util . Scanner ; public class Main { public static void main ( String [ ] args ) { Scanner scanner = new Scanner ( System . in ) ; int spitCount = Integer . valueOf ( scanner . nextLine ( ) ) * 2 ; int spits [ ] = new int [ spitCount ] ; for ( int i = 0 ; i < spitCount ; i ++ ) { spits [ i ] = scanner . nextInt ( ) ; } Arrays . sort ( spits ) ; int dishSum = 0 ; for ( int i = 0 ; i < spitCount ; i ++ ) { if ( i % 2 == 0 ) { dishSum += spits [ i ] ; } } System . out . println ( dishSum ) ; } }
n , l = open ( 0 ) ; print ( sum ( sorted ( map ( int , l . split ( ) ) ) [ : : 2 ] ) ) NEW_LINE
T327
import java . util . Arrays ; import java . util . Scanner ; public class Main { public static void main ( String [ ] args ) { Scanner scanner = new Scanner ( System . in ) ; int spitCount = Integer . valueOf ( scanner . nextLine ( ) ) * 2 ; int spits [ ] = new int [ spitCount ] ; for ( int i = 0 ; i < spitCount ; i ++ ) { spits [ i ] = scanner . nextInt ( ) ; } Arrays . sort ( spits ) ; int dishSum = 0 ; for ( int i = 0 ; i < spitCount ; i ++ ) { if ( i % 2 == 0 ) { dishSum += spits [ i ] ; } } System . out . println ( dishSum ) ; } }
N = int ( input ( ) ) NEW_LINE L = [ ] NEW_LINE for n in [ int ( i ) for i in input ( ) . split ( ) ] : NEW_LINE INDENT L . append ( n ) NEW_LINE DEDENT L . sort ( ) NEW_LINE SUM = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT x = L . pop ( ) NEW_LINE y = L . pop ( ) NEW_LINE if x < y : NEW_LINE INDENT SUM = SUM + x NEW_LINE DEDENT elif x >= y : NEW_LINE INDENT SUM = SUM + y NEW_LINE DEDENT DEDENT print ( SUM ) NEW_LINE
T328
import java . util . Arrays ; import java . util . Scanner ; public class Main { public static void main ( String [ ] args ) { Scanner scanner = new Scanner ( System . in ) ; int spitCount = Integer . valueOf ( scanner . nextLine ( ) ) * 2 ; int spits [ ] = new int [ spitCount ] ; for ( int i = 0 ; i < spitCount ; i ++ ) { spits [ i ] = scanner . nextInt ( ) ; } Arrays . sort ( spits ) ; int dishSum = 0 ; for ( int i = 0 ; i < spitCount ; i ++ ) { if ( i % 2 == 0 ) { dishSum += spits [ i ] ; } } System . out . println ( dishSum ) ; } }
N = input ( ) NEW_LINE L = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE def quicksort ( array ) : NEW_LINE INDENT if len ( array ) < 2 : NEW_LINE INDENT return array NEW_LINE DEDENT else : NEW_LINE INDENT pivot = array [ 0 ] NEW_LINE less = [ i for i in array [ 1 : ] if i <= pivot ] NEW_LINE greater = [ i for i in array [ 1 : ] if i > pivot ] NEW_LINE return quicksort ( less ) + [ pivot ] + quicksort ( greater ) NEW_LINE DEDENT DEDENT guzai = 0 NEW_LINE for i in range ( int ( N ) ) : NEW_LINE INDENT guzai += int ( quicksort ( L ) [ int ( 2 * i ) ] ) NEW_LINE DEDENT print ( guzai ) NEW_LINE
T329
import java . util . Arrays ; import java . util . Scanner ; public class Main { public static void main ( String [ ] args ) { Scanner scanner = new Scanner ( System . in ) ; int spitCount = Integer . valueOf ( scanner . nextLine ( ) ) * 2 ; int spits [ ] = new int [ spitCount ] ; for ( int i = 0 ; i < spitCount ; i ++ ) { spits [ i ] = scanner . nextInt ( ) ; } Arrays . sort ( spits ) ; int dishSum = 0 ; for ( int i = 0 ; i < spitCount ; i ++ ) { if ( i % 2 == 0 ) { dishSum += spits [ i ] ; } } System . out . println ( dishSum ) ; } }
import bisect NEW_LINE N = input ( ) NEW_LINE Ls = map ( int , input ( ) . split ( " ▁ " ) ) NEW_LINE Ls = sorted ( Ls ) NEW_LINE ans = sum ( [ L for i , L in enumerate ( Ls ) if i % 2 == 0 ] ) NEW_LINE print ( ans ) NEW_LINE
T330
import java . util . * ; class Main { public static void main ( String args [ ] ) { Scanner sc = new Scanner ( System . in ) ; char [ ] array = sc . nextLine ( ) . toCharArray ( ) ; try { for ( int i = array . length - 1 ; i >= 0 ; i -- ) { if ( i >= 6 ) { if ( array [ i - 6 ] == ' d ' && array [ i - 5 ] == ' r ' && array [ i - 4 ] == ' e ' && array [ i - 3 ] == ' a ' && array [ i - 2 ] == ' m ' && array [ i - 1 ] == ' e ' && array [ i ] == ' r ' ) { i -= 6 ; continue ; } } if ( i >= 5 ) { if ( array [ i - 5 ] == ' e ' && array [ i - 4 ] == ' r ' && array [ i - 3 ] == ' a ' && array [ i - 2 ] == ' s ' && array [ i - 1 ] == ' e ' && array [ i ] == ' r ' ) { i -= 5 ; continue ; } } if ( ( array [ i - 4 ] == ' d ' && array [ i - 3 ] == ' r ' && array [ i - 2 ] == ' e ' && array [ i - 1 ] == ' a ' && array [ i ] == ' m ' ) || ( array [ i - 4 ] == ' e ' && array [ i - 3 ] == ' r ' && array [ i - 2 ] == ' a ' && array [ i - 1 ] == ' s ' && array [ i ] == ' e ' ) ) { i -= 4 ; } else { System . out . println ( " NO " ) ; return ; } } } catch ( ArrayIndexOutOfBoundsException e ) { System . out . println ( " NO " ) ; return ; } System . out . println ( " YES " ) ; } }
s = list ( input ( ) ) NEW_LINE c = 0 NEW_LINE while len ( s ) > 0 : NEW_LINE INDENT p = "1" NEW_LINE q = "1" NEW_LINE r = "1" NEW_LINE if len ( s ) < 5 : NEW_LINE INDENT break NEW_LINE DEDENT if len ( s ) <= 7 : NEW_LINE INDENT if len ( s ) == 5 : NEW_LINE INDENT if s == list ( " dream " ) or s == list ( " erase " ) : NEW_LINE INDENT c = 1 NEW_LINE break NEW_LINE DEDENT DEDENT elif len ( s ) == 6 : NEW_LINE INDENT if s == list ( " eraser " ) : NEW_LINE INDENT c = 1 NEW_LINE break NEW_LINE DEDENT DEDENT elif len ( s ) == 7 : NEW_LINE INDENT if s == list ( " dreamer " ) : NEW_LINE INDENT c = 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT p = s [ len ( s ) - 5 : len ( s ) ] NEW_LINE q = s [ len ( s ) - 6 : len ( s ) ] NEW_LINE r = s [ len ( s ) - 7 : len ( s ) ] NEW_LINE if p == list ( " dream " ) or p == list ( " erase " ) : NEW_LINE INDENT del s [ len ( s ) - 5 : len ( s ) ] NEW_LINE DEDENT elif q == list ( " eraser " ) : NEW_LINE INDENT del s [ len ( s ) - 6 : len ( s ) ] NEW_LINE DEDENT elif r == list ( " dreamer " ) : NEW_LINE INDENT del s [ len ( s ) - 7 : len ( s ) ] NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT print ( " YES " if c == 1 else " NO " ) NEW_LINE
T331
import java . util . * ; class Main { public static void main ( String args [ ] ) { Scanner sc = new Scanner ( System . in ) ; char [ ] array = sc . nextLine ( ) . toCharArray ( ) ; try { for ( int i = array . length - 1 ; i >= 0 ; i -- ) { if ( i >= 6 ) { if ( array [ i - 6 ] == ' d ' && array [ i - 5 ] == ' r ' && array [ i - 4 ] == ' e ' && array [ i - 3 ] == ' a ' && array [ i - 2 ] == ' m ' && array [ i - 1 ] == ' e ' && array [ i ] == ' r ' ) { i -= 6 ; continue ; } } if ( i >= 5 ) { if ( array [ i - 5 ] == ' e ' && array [ i - 4 ] == ' r ' && array [ i - 3 ] == ' a ' && array [ i - 2 ] == ' s ' && array [ i - 1 ] == ' e ' && array [ i ] == ' r ' ) { i -= 5 ; continue ; } } if ( ( array [ i - 4 ] == ' d ' && array [ i - 3 ] == ' r ' && array [ i - 2 ] == ' e ' && array [ i - 1 ] == ' a ' && array [ i ] == ' m ' ) || ( array [ i - 4 ] == ' e ' && array [ i - 3 ] == ' r ' && array [ i - 2 ] == ' a ' && array [ i - 1 ] == ' s ' && array [ i ] == ' e ' ) ) { i -= 4 ; } else { System . out . println ( " NO " ) ; return ; } } } catch ( ArrayIndexOutOfBoundsException e ) { System . out . println ( " NO " ) ; return ; } System . out . println ( " YES " ) ; } }
from collections import deque NEW_LINE def solve ( s ) : NEW_LINE INDENT stack = deque ( ) NEW_LINE stack . append ( s ) NEW_LINE while len ( stack ) > 0 : NEW_LINE INDENT top = stack . pop ( ) NEW_LINE if top == ' ' : NEW_LINE INDENT return ' YES ' NEW_LINE DEDENT if top [ : 5 ] == ' dream ' : NEW_LINE INDENT if top [ 5 : 7 ] == ' er ' : NEW_LINE INDENT stack . append ( top [ 7 : ] ) NEW_LINE DEDENT stack . append ( top [ 5 : ] ) NEW_LINE DEDENT elif top [ : 5 ] == ' erase ' : NEW_LINE INDENT if len ( top ) > 5 and top [ 5 ] == ' r ' : NEW_LINE INDENT stack . append ( top [ 6 : ] ) NEW_LINE DEDENT else : NEW_LINE INDENT stack . append ( top [ 5 : ] ) NEW_LINE DEDENT DEDENT DEDENT return ' NO ' NEW_LINE DEDENT print ( solve ( input ( ) ) ) NEW_LINE
T332
import java . util . * ; class Main { public static void main ( String args [ ] ) { Scanner sc = new Scanner ( System . in ) ; char [ ] array = sc . nextLine ( ) . toCharArray ( ) ; try { for ( int i = array . length - 1 ; i >= 0 ; i -- ) { if ( i >= 6 ) { if ( array [ i - 6 ] == ' d ' && array [ i - 5 ] == ' r ' && array [ i - 4 ] == ' e ' && array [ i - 3 ] == ' a ' && array [ i - 2 ] == ' m ' && array [ i - 1 ] == ' e ' && array [ i ] == ' r ' ) { i -= 6 ; continue ; } } if ( i >= 5 ) { if ( array [ i - 5 ] == ' e ' && array [ i - 4 ] == ' r ' && array [ i - 3 ] == ' a ' && array [ i - 2 ] == ' s ' && array [ i - 1 ] == ' e ' && array [ i ] == ' r ' ) { i -= 5 ; continue ; } } if ( ( array [ i - 4 ] == ' d ' && array [ i - 3 ] == ' r ' && array [ i - 2 ] == ' e ' && array [ i - 1 ] == ' a ' && array [ i ] == ' m ' ) || ( array [ i - 4 ] == ' e ' && array [ i - 3 ] == ' r ' && array [ i - 2 ] == ' a ' && array [ i - 1 ] == ' s ' && array [ i ] == ' e ' ) ) { i -= 4 ; } else { System . out . println ( " NO " ) ; return ; } } } catch ( ArrayIndexOutOfBoundsException e ) { System . out . println ( " NO " ) ; return ; } System . out . println ( " YES " ) ; } }
S = input ( ) NEW_LINE S = S [ : : - 1 ] NEW_LINE S = S . replace ( ' resare ' , ' ' ) NEW_LINE S = S . replace ( ' esare ' , ' ' ) NEW_LINE S = S . replace ( ' remaerd ' , ' ' ) NEW_LINE S = S . replace ( ' maerd ' , ' ' ) NEW_LINE if S : NEW_LINE INDENT print ( ' NO ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' YES ' ) NEW_LINE DEDENT
T333
import java . util . * ; class Main { public static void main ( String args [ ] ) { Scanner sc = new Scanner ( System . in ) ; char [ ] array = sc . nextLine ( ) . toCharArray ( ) ; try { for ( int i = array . length - 1 ; i >= 0 ; i -- ) { if ( i >= 6 ) { if ( array [ i - 6 ] == ' d ' && array [ i - 5 ] == ' r ' && array [ i - 4 ] == ' e ' && array [ i - 3 ] == ' a ' && array [ i - 2 ] == ' m ' && array [ i - 1 ] == ' e ' && array [ i ] == ' r ' ) { i -= 6 ; continue ; } } if ( i >= 5 ) { if ( array [ i - 5 ] == ' e ' && array [ i - 4 ] == ' r ' && array [ i - 3 ] == ' a ' && array [ i - 2 ] == ' s ' && array [ i - 1 ] == ' e ' && array [ i ] == ' r ' ) { i -= 5 ; continue ; } } if ( ( array [ i - 4 ] == ' d ' && array [ i - 3 ] == ' r ' && array [ i - 2 ] == ' e ' && array [ i - 1 ] == ' a ' && array [ i ] == ' m ' ) || ( array [ i - 4 ] == ' e ' && array [ i - 3 ] == ' r ' && array [ i - 2 ] == ' a ' && array [ i - 1 ] == ' s ' && array [ i ] == ' e ' ) ) { i -= 4 ; } else { System . out . println ( " NO " ) ; return ; } } } catch ( ArrayIndexOutOfBoundsException e ) { System . out . println ( " NO " ) ; return ; } System . out . println ( " YES " ) ; } }
def db ( x ) : NEW_LINE INDENT global debug NEW_LINE if debug : NEW_LINE INDENT print ( x ) NEW_LINE DEDENT DEDENT def gen ( tstr ) : NEW_LINE INDENT if len ( tstr ) <= len ( S ) : NEW_LINE INDENT for w in words : NEW_LINE INDENT yield tstr + w NEW_LINE for w2 in gen ( tstr + w ) : NEW_LINE INDENT yield w2 NEW_LINE DEDENT DEDENT DEDENT DEDENT def solve ( ) : NEW_LINE INDENT ans = False NEW_LINE for genstr in gen ( ' ' ) : NEW_LINE INDENT db ( genstr ) NEW_LINE if genstr == S : NEW_LINE INDENT ans = True NEW_LINE break NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def solve2 ( ) : NEW_LINE INDENT stack = list ( words ) NEW_LINE while stack != [ ] : NEW_LINE INDENT str = stack . pop ( ) NEW_LINE db ( ( ' pop ' , str ) ) NEW_LINE if len ( str ) < len ( S ) : NEW_LINE INDENT for w in words : NEW_LINE INDENT stack . append ( str + w ) NEW_LINE DEDENT DEDENT elif len ( str ) == len ( S ) and str == S : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def solve3 ( ) : NEW_LINE INDENT string = S + ' ▁ ' NEW_LINE n = len ( string ) NEW_LINE while n > 1 : NEW_LINE INDENT for w in words : NEW_LINE INDENT db ( ( string . rfind ( w , 0 , n ) , len ( w ) , n ) ) NEW_LINE if string . rfind ( w , 0 , n ) + len ( w ) + 1 == n : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT db ( ( n , w ) ) NEW_LINE n -= len ( w ) NEW_LINE DEDENT return True NEW_LINE DEDENT debug = False NEW_LINE words = ( ' dream ' , ' dreamer ' , ' erase ' , ' eraser ' ) NEW_LINE S = input ( ) NEW_LINE db ( S ) NEW_LINE if solve3 ( ) : NEW_LINE INDENT print ( ' YES ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' NO ' ) NEW_LINE DEDENT
T334
import java . util . * ; class Main { public static void main ( String args [ ] ) { Scanner sc = new Scanner ( System . in ) ; char [ ] array = sc . nextLine ( ) . toCharArray ( ) ; try { for ( int i = array . length - 1 ; i >= 0 ; i -- ) { if ( i >= 6 ) { if ( array [ i - 6 ] == ' d ' && array [ i - 5 ] == ' r ' && array [ i - 4 ] == ' e ' && array [ i - 3 ] == ' a ' && array [ i - 2 ] == ' m ' && array [ i - 1 ] == ' e ' && array [ i ] == ' r ' ) { i -= 6 ; continue ; } } if ( i >= 5 ) { if ( array [ i - 5 ] == ' e ' && array [ i - 4 ] == ' r ' && array [ i - 3 ] == ' a ' && array [ i - 2 ] == ' s ' && array [ i - 1 ] == ' e ' && array [ i ] == ' r ' ) { i -= 5 ; continue ; } } if ( ( array [ i - 4 ] == ' d ' && array [ i - 3 ] == ' r ' && array [ i - 2 ] == ' e ' && array [ i - 1 ] == ' a ' && array [ i ] == ' m ' ) || ( array [ i - 4 ] == ' e ' && array [ i - 3 ] == ' r ' && array [ i - 2 ] == ' a ' && array [ i - 1 ] == ' s ' && array [ i ] == ' e ' ) ) { i -= 4 ; } else { System . out . println ( " NO " ) ; return ; } } } catch ( ArrayIndexOutOfBoundsException e ) { System . out . println ( " NO " ) ; return ; } System . out . println ( " YES " ) ; } }
def YESNO ( ans , yes = " YES " , no = " NO " ) : NEW_LINE INDENT print ( [ no , yes ] [ ans ] ) NEW_LINE DEDENT II = lambda : int ( input ( ) ) NEW_LINE MI = lambda : map ( int , input ( ) . split ( ) ) NEW_LINE MIL = lambda : list ( MI ( ) ) NEW_LINE MIS = lambda : input ( ) . split ( ) NEW_LINE def check ( s ) : NEW_LINE INDENT if s == " " : NEW_LINE INDENT return True NEW_LINE DEDENT if s [ : 5 ] in [ " dream " , " erase " ] : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT S = input ( ) NEW_LINE i = 0 NEW_LINE l = len ( S ) NEW_LINE while i < l : NEW_LINE INDENT if S [ i : i + 7 ] == " dreamer " and check ( S [ i + 7 : ] ) : NEW_LINE INDENT i += 7 NEW_LINE DEDENT elif S [ i : i + 6 ] == " eraser " and check ( S [ i + 6 : ] ) : NEW_LINE INDENT i += 6 NEW_LINE DEDENT elif check ( S [ i : ] ) : NEW_LINE INDENT i += 5 NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT YESNO ( main ( ) ) NEW_LINE DEDENT
T335
import java . io . * ; import java . util . * ; public class Main { static class FastScanner { BufferedReader br ; StringTokenizer st ; public FastScanner ( ) { try { br = new BufferedReader ( new InputStreamReader ( System . in ) ) ; st = new StringTokenizer ( br . readLine ( ) ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } } public String next ( ) { if ( st . hasMoreTokens ( ) ) return st . nextToken ( ) ; try { st = new StringTokenizer ( br . readLine ( ) ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } return st . nextToken ( ) ; } public int nextInt ( ) { return Integer . parseInt ( next ( ) ) ; } public long nextLong ( ) { return Long . parseLong ( next ( ) ) ; } public double nextDouble ( ) { return Double . parseDouble ( next ( ) ) ; } public String nextLine ( ) { String line = " " ; if ( st . hasMoreTokens ( ) ) line = st . nextToken ( ) ; else try { return br . readLine ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } while ( st . hasMoreTokens ( ) ) line += " ▁ " + st . nextToken ( ) ; return line ; } } public static void main ( String [ ] args ) { FastScanner sc = new FastScanner ( ) ; PrintWriter pw = new PrintWriter ( System . out ) ; String s = sc . nextLine ( ) ; int k = s . length ( ) ; while ( k > 0 ) { if ( k >= 5 && s . substring ( k - 5 , k ) . equals ( " dream " ) ) { k -= 5 ; } else if ( k >= 7 && s . substring ( k - 7 , k ) . equals ( " dreamer " ) ) { k -= 7 ; } else if ( k >= 5 && s . substring ( k - 5 , k ) . equals ( " erase " ) ) { k -= 5 ; } else if ( k >= 6 && s . substring ( k - 6 , k ) . equals ( " eraser " ) ) { k -= 6 ; } else { pw . println ( " NO " ) ; pw . close ( ) ; return ; } } pw . println ( " YES " ) ; pw . close ( ) ; } }
s = list ( input ( ) ) NEW_LINE c = 0 NEW_LINE while len ( s ) > 0 : NEW_LINE INDENT p = "1" NEW_LINE q = "1" NEW_LINE r = "1" NEW_LINE if len ( s ) < 5 : NEW_LINE INDENT break NEW_LINE DEDENT if len ( s ) <= 7 : NEW_LINE INDENT if len ( s ) == 5 : NEW_LINE INDENT if s == list ( " dream " ) or s == list ( " erase " ) : NEW_LINE INDENT c = 1 NEW_LINE break NEW_LINE DEDENT DEDENT elif len ( s ) == 6 : NEW_LINE INDENT if s == list ( " eraser " ) : NEW_LINE INDENT c = 1 NEW_LINE break NEW_LINE DEDENT DEDENT elif len ( s ) == 7 : NEW_LINE INDENT if s == list ( " dreamer " ) : NEW_LINE INDENT c = 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT p = s [ len ( s ) - 5 : len ( s ) ] NEW_LINE q = s [ len ( s ) - 6 : len ( s ) ] NEW_LINE r = s [ len ( s ) - 7 : len ( s ) ] NEW_LINE if p == list ( " dream " ) or p == list ( " erase " ) : NEW_LINE INDENT del s [ len ( s ) - 5 : len ( s ) ] NEW_LINE DEDENT elif q == list ( " eraser " ) : NEW_LINE INDENT del s [ len ( s ) - 6 : len ( s ) ] NEW_LINE DEDENT elif r == list ( " dreamer " ) : NEW_LINE INDENT del s [ len ( s ) - 7 : len ( s ) ] NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT print ( " YES " if c == 1 else " NO " ) NEW_LINE
T336
import java . io . * ; import java . util . * ; public class Main { static class FastScanner { BufferedReader br ; StringTokenizer st ; public FastScanner ( ) { try { br = new BufferedReader ( new InputStreamReader ( System . in ) ) ; st = new StringTokenizer ( br . readLine ( ) ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } } public String next ( ) { if ( st . hasMoreTokens ( ) ) return st . nextToken ( ) ; try { st = new StringTokenizer ( br . readLine ( ) ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } return st . nextToken ( ) ; } public int nextInt ( ) { return Integer . parseInt ( next ( ) ) ; } public long nextLong ( ) { return Long . parseLong ( next ( ) ) ; } public double nextDouble ( ) { return Double . parseDouble ( next ( ) ) ; } public String nextLine ( ) { String line = " " ; if ( st . hasMoreTokens ( ) ) line = st . nextToken ( ) ; else try { return br . readLine ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } while ( st . hasMoreTokens ( ) ) line += " ▁ " + st . nextToken ( ) ; return line ; } } public static void main ( String [ ] args ) { FastScanner sc = new FastScanner ( ) ; PrintWriter pw = new PrintWriter ( System . out ) ; String s = sc . nextLine ( ) ; int k = s . length ( ) ; while ( k > 0 ) { if ( k >= 5 && s . substring ( k - 5 , k ) . equals ( " dream " ) ) { k -= 5 ; } else if ( k >= 7 && s . substring ( k - 7 , k ) . equals ( " dreamer " ) ) { k -= 7 ; } else if ( k >= 5 && s . substring ( k - 5 , k ) . equals ( " erase " ) ) { k -= 5 ; } else if ( k >= 6 && s . substring ( k - 6 , k ) . equals ( " eraser " ) ) { k -= 6 ; } else { pw . println ( " NO " ) ; pw . close ( ) ; return ; } } pw . println ( " YES " ) ; pw . close ( ) ; } }
from collections import deque NEW_LINE def solve ( s ) : NEW_LINE INDENT stack = deque ( ) NEW_LINE stack . append ( s ) NEW_LINE while len ( stack ) > 0 : NEW_LINE INDENT top = stack . pop ( ) NEW_LINE if top == ' ' : NEW_LINE INDENT return ' YES ' NEW_LINE DEDENT if top [ : 5 ] == ' dream ' : NEW_LINE INDENT if top [ 5 : 7 ] == ' er ' : NEW_LINE INDENT stack . append ( top [ 7 : ] ) NEW_LINE DEDENT stack . append ( top [ 5 : ] ) NEW_LINE DEDENT elif top [ : 5 ] == ' erase ' : NEW_LINE INDENT if len ( top ) > 5 and top [ 5 ] == ' r ' : NEW_LINE INDENT stack . append ( top [ 6 : ] ) NEW_LINE DEDENT else : NEW_LINE INDENT stack . append ( top [ 5 : ] ) NEW_LINE DEDENT DEDENT DEDENT return ' NO ' NEW_LINE DEDENT print ( solve ( input ( ) ) ) NEW_LINE
T337
import java . io . * ; import java . util . * ; public class Main { static class FastScanner { BufferedReader br ; StringTokenizer st ; public FastScanner ( ) { try { br = new BufferedReader ( new InputStreamReader ( System . in ) ) ; st = new StringTokenizer ( br . readLine ( ) ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } } public String next ( ) { if ( st . hasMoreTokens ( ) ) return st . nextToken ( ) ; try { st = new StringTokenizer ( br . readLine ( ) ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } return st . nextToken ( ) ; } public int nextInt ( ) { return Integer . parseInt ( next ( ) ) ; } public long nextLong ( ) { return Long . parseLong ( next ( ) ) ; } public double nextDouble ( ) { return Double . parseDouble ( next ( ) ) ; } public String nextLine ( ) { String line = " " ; if ( st . hasMoreTokens ( ) ) line = st . nextToken ( ) ; else try { return br . readLine ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } while ( st . hasMoreTokens ( ) ) line += " ▁ " + st . nextToken ( ) ; return line ; } } public static void main ( String [ ] args ) { FastScanner sc = new FastScanner ( ) ; PrintWriter pw = new PrintWriter ( System . out ) ; String s = sc . nextLine ( ) ; int k = s . length ( ) ; while ( k > 0 ) { if ( k >= 5 && s . substring ( k - 5 , k ) . equals ( " dream " ) ) { k -= 5 ; } else if ( k >= 7 && s . substring ( k - 7 , k ) . equals ( " dreamer " ) ) { k -= 7 ; } else if ( k >= 5 && s . substring ( k - 5 , k ) . equals ( " erase " ) ) { k -= 5 ; } else if ( k >= 6 && s . substring ( k - 6 , k ) . equals ( " eraser " ) ) { k -= 6 ; } else { pw . println ( " NO " ) ; pw . close ( ) ; return ; } } pw . println ( " YES " ) ; pw . close ( ) ; } }
S = input ( ) NEW_LINE S = S [ : : - 1 ] NEW_LINE S = S . replace ( ' resare ' , ' ' ) NEW_LINE S = S . replace ( ' esare ' , ' ' ) NEW_LINE S = S . replace ( ' remaerd ' , ' ' ) NEW_LINE S = S . replace ( ' maerd ' , ' ' ) NEW_LINE if S : NEW_LINE INDENT print ( ' NO ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' YES ' ) NEW_LINE DEDENT
T338
import java . io . * ; import java . util . * ; public class Main { static class FastScanner { BufferedReader br ; StringTokenizer st ; public FastScanner ( ) { try { br = new BufferedReader ( new InputStreamReader ( System . in ) ) ; st = new StringTokenizer ( br . readLine ( ) ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } } public String next ( ) { if ( st . hasMoreTokens ( ) ) return st . nextToken ( ) ; try { st = new StringTokenizer ( br . readLine ( ) ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } return st . nextToken ( ) ; } public int nextInt ( ) { return Integer . parseInt ( next ( ) ) ; } public long nextLong ( ) { return Long . parseLong ( next ( ) ) ; } public double nextDouble ( ) { return Double . parseDouble ( next ( ) ) ; } public String nextLine ( ) { String line = " " ; if ( st . hasMoreTokens ( ) ) line = st . nextToken ( ) ; else try { return br . readLine ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } while ( st . hasMoreTokens ( ) ) line += " ▁ " + st . nextToken ( ) ; return line ; } } public static void main ( String [ ] args ) { FastScanner sc = new FastScanner ( ) ; PrintWriter pw = new PrintWriter ( System . out ) ; String s = sc . nextLine ( ) ; int k = s . length ( ) ; while ( k > 0 ) { if ( k >= 5 && s . substring ( k - 5 , k ) . equals ( " dream " ) ) { k -= 5 ; } else if ( k >= 7 && s . substring ( k - 7 , k ) . equals ( " dreamer " ) ) { k -= 7 ; } else if ( k >= 5 && s . substring ( k - 5 , k ) . equals ( " erase " ) ) { k -= 5 ; } else if ( k >= 6 && s . substring ( k - 6 , k ) . equals ( " eraser " ) ) { k -= 6 ; } else { pw . println ( " NO " ) ; pw . close ( ) ; return ; } } pw . println ( " YES " ) ; pw . close ( ) ; } }
def db ( x ) : NEW_LINE INDENT global debug NEW_LINE if debug : NEW_LINE INDENT print ( x ) NEW_LINE DEDENT DEDENT def gen ( tstr ) : NEW_LINE INDENT if len ( tstr ) <= len ( S ) : NEW_LINE INDENT for w in words : NEW_LINE INDENT yield tstr + w NEW_LINE for w2 in gen ( tstr + w ) : NEW_LINE INDENT yield w2 NEW_LINE DEDENT DEDENT DEDENT DEDENT def solve ( ) : NEW_LINE INDENT ans = False NEW_LINE for genstr in gen ( ' ' ) : NEW_LINE INDENT db ( genstr ) NEW_LINE if genstr == S : NEW_LINE INDENT ans = True NEW_LINE break NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def solve2 ( ) : NEW_LINE INDENT stack = list ( words ) NEW_LINE while stack != [ ] : NEW_LINE INDENT str = stack . pop ( ) NEW_LINE db ( ( ' pop ' , str ) ) NEW_LINE if len ( str ) < len ( S ) : NEW_LINE INDENT for w in words : NEW_LINE INDENT stack . append ( str + w ) NEW_LINE DEDENT DEDENT elif len ( str ) == len ( S ) and str == S : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def solve3 ( ) : NEW_LINE INDENT string = S + ' ▁ ' NEW_LINE n = len ( string ) NEW_LINE while n > 1 : NEW_LINE INDENT for w in words : NEW_LINE INDENT db ( ( string . rfind ( w , 0 , n ) , len ( w ) , n ) ) NEW_LINE if string . rfind ( w , 0 , n ) + len ( w ) + 1 == n : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT db ( ( n , w ) ) NEW_LINE n -= len ( w ) NEW_LINE DEDENT return True NEW_LINE DEDENT debug = False NEW_LINE words = ( ' dream ' , ' dreamer ' , ' erase ' , ' eraser ' ) NEW_LINE S = input ( ) NEW_LINE db ( S ) NEW_LINE if solve3 ( ) : NEW_LINE INDENT print ( ' YES ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' NO ' ) NEW_LINE DEDENT
T339
import java . io . * ; import java . util . * ; public class Main { static class FastScanner { BufferedReader br ; StringTokenizer st ; public FastScanner ( ) { try { br = new BufferedReader ( new InputStreamReader ( System . in ) ) ; st = new StringTokenizer ( br . readLine ( ) ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } } public String next ( ) { if ( st . hasMoreTokens ( ) ) return st . nextToken ( ) ; try { st = new StringTokenizer ( br . readLine ( ) ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } return st . nextToken ( ) ; } public int nextInt ( ) { return Integer . parseInt ( next ( ) ) ; } public long nextLong ( ) { return Long . parseLong ( next ( ) ) ; } public double nextDouble ( ) { return Double . parseDouble ( next ( ) ) ; } public String nextLine ( ) { String line = " " ; if ( st . hasMoreTokens ( ) ) line = st . nextToken ( ) ; else try { return br . readLine ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } while ( st . hasMoreTokens ( ) ) line += " ▁ " + st . nextToken ( ) ; return line ; } } public static void main ( String [ ] args ) { FastScanner sc = new FastScanner ( ) ; PrintWriter pw = new PrintWriter ( System . out ) ; String s = sc . nextLine ( ) ; int k = s . length ( ) ; while ( k > 0 ) { if ( k >= 5 && s . substring ( k - 5 , k ) . equals ( " dream " ) ) { k -= 5 ; } else if ( k >= 7 && s . substring ( k - 7 , k ) . equals ( " dreamer " ) ) { k -= 7 ; } else if ( k >= 5 && s . substring ( k - 5 , k ) . equals ( " erase " ) ) { k -= 5 ; } else if ( k >= 6 && s . substring ( k - 6 , k ) . equals ( " eraser " ) ) { k -= 6 ; } else { pw . println ( " NO " ) ; pw . close ( ) ; return ; } } pw . println ( " YES " ) ; pw . close ( ) ; } }
def YESNO ( ans , yes = " YES " , no = " NO " ) : NEW_LINE INDENT print ( [ no , yes ] [ ans ] ) NEW_LINE DEDENT II = lambda : int ( input ( ) ) NEW_LINE MI = lambda : map ( int , input ( ) . split ( ) ) NEW_LINE MIL = lambda : list ( MI ( ) ) NEW_LINE MIS = lambda : input ( ) . split ( ) NEW_LINE def check ( s ) : NEW_LINE INDENT if s == " " : NEW_LINE INDENT return True NEW_LINE DEDENT if s [ : 5 ] in [ " dream " , " erase " ] : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT S = input ( ) NEW_LINE i = 0 NEW_LINE l = len ( S ) NEW_LINE while i < l : NEW_LINE INDENT if S [ i : i + 7 ] == " dreamer " and check ( S [ i + 7 : ] ) : NEW_LINE INDENT i += 7 NEW_LINE DEDENT elif S [ i : i + 6 ] == " eraser " and check ( S [ i + 6 : ] ) : NEW_LINE INDENT i += 6 NEW_LINE DEDENT elif check ( S [ i : ] ) : NEW_LINE INDENT i += 5 NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT YESNO ( main ( ) ) NEW_LINE DEDENT
T340
import java . util . * ; import java . io . * ; public class Main { static boolean match ( String temp , int start , String str ) { if ( start + temp . length ( ) > str . length ( ) ) return false ; for ( int j = 0 ; j < temp . length ( ) ; j ++ ) { if ( temp . charAt ( j ) != str . charAt ( j + start ) ) return false ; } return true ; } public static void main ( String [ ] args ) throws Exception { String array [ ] = new String [ ] { " dreamer " , " dream " , " eraser " , " erase " } ; BufferedReader br = new BufferedReader ( new InputStreamReader ( System . in ) ) ; String str = br . readLine ( ) ; int start = 0 ; int last = - 1 ; while ( start < str . length ( ) && start >= 0 ) { boolean flag = false ; for ( int j = 0 ; j < 4 ; j ++ ) { if ( match ( array [ j ] , start , str ) ) { start += array [ j ] . length ( ) ; flag = true ; last = j ; break ; } } if ( ! flag && last == 0 ) { start -= 2 ; last = - 1 ; } else if ( ! flag ) break ; } if ( start == str . length ( ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
s = list ( input ( ) ) NEW_LINE c = 0 NEW_LINE while len ( s ) > 0 : NEW_LINE INDENT p = "1" NEW_LINE q = "1" NEW_LINE r = "1" NEW_LINE if len ( s ) < 5 : NEW_LINE INDENT break NEW_LINE DEDENT if len ( s ) <= 7 : NEW_LINE INDENT if len ( s ) == 5 : NEW_LINE INDENT if s == list ( " dream " ) or s == list ( " erase " ) : NEW_LINE INDENT c = 1 NEW_LINE break NEW_LINE DEDENT DEDENT elif len ( s ) == 6 : NEW_LINE INDENT if s == list ( " eraser " ) : NEW_LINE INDENT c = 1 NEW_LINE break NEW_LINE DEDENT DEDENT elif len ( s ) == 7 : NEW_LINE INDENT if s == list ( " dreamer " ) : NEW_LINE INDENT c = 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT p = s [ len ( s ) - 5 : len ( s ) ] NEW_LINE q = s [ len ( s ) - 6 : len ( s ) ] NEW_LINE r = s [ len ( s ) - 7 : len ( s ) ] NEW_LINE if p == list ( " dream " ) or p == list ( " erase " ) : NEW_LINE INDENT del s [ len ( s ) - 5 : len ( s ) ] NEW_LINE DEDENT elif q == list ( " eraser " ) : NEW_LINE INDENT del s [ len ( s ) - 6 : len ( s ) ] NEW_LINE DEDENT elif r == list ( " dreamer " ) : NEW_LINE INDENT del s [ len ( s ) - 7 : len ( s ) ] NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT print ( " YES " if c == 1 else " NO " ) NEW_LINE
T341
import java . util . * ; import java . io . * ; public class Main { static boolean match ( String temp , int start , String str ) { if ( start + temp . length ( ) > str . length ( ) ) return false ; for ( int j = 0 ; j < temp . length ( ) ; j ++ ) { if ( temp . charAt ( j ) != str . charAt ( j + start ) ) return false ; } return true ; } public static void main ( String [ ] args ) throws Exception { String array [ ] = new String [ ] { " dreamer " , " dream " , " eraser " , " erase " } ; BufferedReader br = new BufferedReader ( new InputStreamReader ( System . in ) ) ; String str = br . readLine ( ) ; int start = 0 ; int last = - 1 ; while ( start < str . length ( ) && start >= 0 ) { boolean flag = false ; for ( int j = 0 ; j < 4 ; j ++ ) { if ( match ( array [ j ] , start , str ) ) { start += array [ j ] . length ( ) ; flag = true ; last = j ; break ; } } if ( ! flag && last == 0 ) { start -= 2 ; last = - 1 ; } else if ( ! flag ) break ; } if ( start == str . length ( ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
from collections import deque NEW_LINE def solve ( s ) : NEW_LINE INDENT stack = deque ( ) NEW_LINE stack . append ( s ) NEW_LINE while len ( stack ) > 0 : NEW_LINE INDENT top = stack . pop ( ) NEW_LINE if top == ' ' : NEW_LINE INDENT return ' YES ' NEW_LINE DEDENT if top [ : 5 ] == ' dream ' : NEW_LINE INDENT if top [ 5 : 7 ] == ' er ' : NEW_LINE INDENT stack . append ( top [ 7 : ] ) NEW_LINE DEDENT stack . append ( top [ 5 : ] ) NEW_LINE DEDENT elif top [ : 5 ] == ' erase ' : NEW_LINE INDENT if len ( top ) > 5 and top [ 5 ] == ' r ' : NEW_LINE INDENT stack . append ( top [ 6 : ] ) NEW_LINE DEDENT else : NEW_LINE INDENT stack . append ( top [ 5 : ] ) NEW_LINE DEDENT DEDENT DEDENT return ' NO ' NEW_LINE DEDENT print ( solve ( input ( ) ) ) NEW_LINE
T342
import java . util . * ; import java . io . * ; public class Main { static boolean match ( String temp , int start , String str ) { if ( start + temp . length ( ) > str . length ( ) ) return false ; for ( int j = 0 ; j < temp . length ( ) ; j ++ ) { if ( temp . charAt ( j ) != str . charAt ( j + start ) ) return false ; } return true ; } public static void main ( String [ ] args ) throws Exception { String array [ ] = new String [ ] { " dreamer " , " dream " , " eraser " , " erase " } ; BufferedReader br = new BufferedReader ( new InputStreamReader ( System . in ) ) ; String str = br . readLine ( ) ; int start = 0 ; int last = - 1 ; while ( start < str . length ( ) && start >= 0 ) { boolean flag = false ; for ( int j = 0 ; j < 4 ; j ++ ) { if ( match ( array [ j ] , start , str ) ) { start += array [ j ] . length ( ) ; flag = true ; last = j ; break ; } } if ( ! flag && last == 0 ) { start -= 2 ; last = - 1 ; } else if ( ! flag ) break ; } if ( start == str . length ( ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
S = input ( ) NEW_LINE S = S [ : : - 1 ] NEW_LINE S = S . replace ( ' resare ' , ' ' ) NEW_LINE S = S . replace ( ' esare ' , ' ' ) NEW_LINE S = S . replace ( ' remaerd ' , ' ' ) NEW_LINE S = S . replace ( ' maerd ' , ' ' ) NEW_LINE if S : NEW_LINE INDENT print ( ' NO ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' YES ' ) NEW_LINE DEDENT
T343
import java . util . * ; import java . io . * ; public class Main { static boolean match ( String temp , int start , String str ) { if ( start + temp . length ( ) > str . length ( ) ) return false ; for ( int j = 0 ; j < temp . length ( ) ; j ++ ) { if ( temp . charAt ( j ) != str . charAt ( j + start ) ) return false ; } return true ; } public static void main ( String [ ] args ) throws Exception { String array [ ] = new String [ ] { " dreamer " , " dream " , " eraser " , " erase " } ; BufferedReader br = new BufferedReader ( new InputStreamReader ( System . in ) ) ; String str = br . readLine ( ) ; int start = 0 ; int last = - 1 ; while ( start < str . length ( ) && start >= 0 ) { boolean flag = false ; for ( int j = 0 ; j < 4 ; j ++ ) { if ( match ( array [ j ] , start , str ) ) { start += array [ j ] . length ( ) ; flag = true ; last = j ; break ; } } if ( ! flag && last == 0 ) { start -= 2 ; last = - 1 ; } else if ( ! flag ) break ; } if ( start == str . length ( ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
def db ( x ) : NEW_LINE INDENT global debug NEW_LINE if debug : NEW_LINE INDENT print ( x ) NEW_LINE DEDENT DEDENT def gen ( tstr ) : NEW_LINE INDENT if len ( tstr ) <= len ( S ) : NEW_LINE INDENT for w in words : NEW_LINE INDENT yield tstr + w NEW_LINE for w2 in gen ( tstr + w ) : NEW_LINE INDENT yield w2 NEW_LINE DEDENT DEDENT DEDENT DEDENT def solve ( ) : NEW_LINE INDENT ans = False NEW_LINE for genstr in gen ( ' ' ) : NEW_LINE INDENT db ( genstr ) NEW_LINE if genstr == S : NEW_LINE INDENT ans = True NEW_LINE break NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def solve2 ( ) : NEW_LINE INDENT stack = list ( words ) NEW_LINE while stack != [ ] : NEW_LINE INDENT str = stack . pop ( ) NEW_LINE db ( ( ' pop ' , str ) ) NEW_LINE if len ( str ) < len ( S ) : NEW_LINE INDENT for w in words : NEW_LINE INDENT stack . append ( str + w ) NEW_LINE DEDENT DEDENT elif len ( str ) == len ( S ) and str == S : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def solve3 ( ) : NEW_LINE INDENT string = S + ' ▁ ' NEW_LINE n = len ( string ) NEW_LINE while n > 1 : NEW_LINE INDENT for w in words : NEW_LINE INDENT db ( ( string . rfind ( w , 0 , n ) , len ( w ) , n ) ) NEW_LINE if string . rfind ( w , 0 , n ) + len ( w ) + 1 == n : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT db ( ( n , w ) ) NEW_LINE n -= len ( w ) NEW_LINE DEDENT return True NEW_LINE DEDENT debug = False NEW_LINE words = ( ' dream ' , ' dreamer ' , ' erase ' , ' eraser ' ) NEW_LINE S = input ( ) NEW_LINE db ( S ) NEW_LINE if solve3 ( ) : NEW_LINE INDENT print ( ' YES ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' NO ' ) NEW_LINE DEDENT
T344
import java . util . * ; import java . io . * ; public class Main { static boolean match ( String temp , int start , String str ) { if ( start + temp . length ( ) > str . length ( ) ) return false ; for ( int j = 0 ; j < temp . length ( ) ; j ++ ) { if ( temp . charAt ( j ) != str . charAt ( j + start ) ) return false ; } return true ; } public static void main ( String [ ] args ) throws Exception { String array [ ] = new String [ ] { " dreamer " , " dream " , " eraser " , " erase " } ; BufferedReader br = new BufferedReader ( new InputStreamReader ( System . in ) ) ; String str = br . readLine ( ) ; int start = 0 ; int last = - 1 ; while ( start < str . length ( ) && start >= 0 ) { boolean flag = false ; for ( int j = 0 ; j < 4 ; j ++ ) { if ( match ( array [ j ] , start , str ) ) { start += array [ j ] . length ( ) ; flag = true ; last = j ; break ; } } if ( ! flag && last == 0 ) { start -= 2 ; last = - 1 ; } else if ( ! flag ) break ; } if ( start == str . length ( ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
def YESNO ( ans , yes = " YES " , no = " NO " ) : NEW_LINE INDENT print ( [ no , yes ] [ ans ] ) NEW_LINE DEDENT II = lambda : int ( input ( ) ) NEW_LINE MI = lambda : map ( int , input ( ) . split ( ) ) NEW_LINE MIL = lambda : list ( MI ( ) ) NEW_LINE MIS = lambda : input ( ) . split ( ) NEW_LINE def check ( s ) : NEW_LINE INDENT if s == " " : NEW_LINE INDENT return True NEW_LINE DEDENT if s [ : 5 ] in [ " dream " , " erase " ] : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT S = input ( ) NEW_LINE i = 0 NEW_LINE l = len ( S ) NEW_LINE while i < l : NEW_LINE INDENT if S [ i : i + 7 ] == " dreamer " and check ( S [ i + 7 : ] ) : NEW_LINE INDENT i += 7 NEW_LINE DEDENT elif S [ i : i + 6 ] == " eraser " and check ( S [ i + 6 : ] ) : NEW_LINE INDENT i += 6 NEW_LINE DEDENT elif check ( S [ i : ] ) : NEW_LINE INDENT i += 5 NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT YESNO ( main ( ) ) NEW_LINE DEDENT
T345
import java . util . Scanner ; public class Main { public static void main ( String [ ] args ) { try ( Scanner sc = new Scanner ( System . in ) ; ) { new Main ( ) . solve ( sc ) ; } } void solve ( Scanner sc ) { char [ ] [ ] addeds = { " dream " . toCharArray ( ) , " dreamer " . toCharArray ( ) , " erase " . toCharArray ( ) , " eraser " . toCharArray ( ) } ; char [ ] s = sc . next ( ) . toCharArray ( ) ; int endIndex = s . length ; while ( endIndex > 0 ) { boolean f = false ; for ( char [ ] added : addeds ) { if ( isMatch ( s , added , endIndex ) ) { endIndex -= added . length ; f = true ; break ; } } if ( f == false ) { System . out . println ( " NO " ) ; return ; } } System . out . println ( " YES " ) ; } boolean isMatch ( char [ ] s , char [ ] added , int endIndex ) { if ( endIndex - added . length < 0 ) { return false ; } for ( int i = 0 ; i < added . length ; i ++ ) { if ( added [ i ] != s [ endIndex - added . length + i ] ) { return false ; } } return true ; } }
s = list ( input ( ) ) NEW_LINE c = 0 NEW_LINE while len ( s ) > 0 : NEW_LINE INDENT p = "1" NEW_LINE q = "1" NEW_LINE r = "1" NEW_LINE if len ( s ) < 5 : NEW_LINE INDENT break NEW_LINE DEDENT if len ( s ) <= 7 : NEW_LINE INDENT if len ( s ) == 5 : NEW_LINE INDENT if s == list ( " dream " ) or s == list ( " erase " ) : NEW_LINE INDENT c = 1 NEW_LINE break NEW_LINE DEDENT DEDENT elif len ( s ) == 6 : NEW_LINE INDENT if s == list ( " eraser " ) : NEW_LINE INDENT c = 1 NEW_LINE break NEW_LINE DEDENT DEDENT elif len ( s ) == 7 : NEW_LINE INDENT if s == list ( " dreamer " ) : NEW_LINE INDENT c = 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT p = s [ len ( s ) - 5 : len ( s ) ] NEW_LINE q = s [ len ( s ) - 6 : len ( s ) ] NEW_LINE r = s [ len ( s ) - 7 : len ( s ) ] NEW_LINE if p == list ( " dream " ) or p == list ( " erase " ) : NEW_LINE INDENT del s [ len ( s ) - 5 : len ( s ) ] NEW_LINE DEDENT elif q == list ( " eraser " ) : NEW_LINE INDENT del s [ len ( s ) - 6 : len ( s ) ] NEW_LINE DEDENT elif r == list ( " dreamer " ) : NEW_LINE INDENT del s [ len ( s ) - 7 : len ( s ) ] NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT print ( " YES " if c == 1 else " NO " ) NEW_LINE
T346
import java . util . Scanner ; public class Main { public static void main ( String [ ] args ) { try ( Scanner sc = new Scanner ( System . in ) ; ) { new Main ( ) . solve ( sc ) ; } } void solve ( Scanner sc ) { char [ ] [ ] addeds = { " dream " . toCharArray ( ) , " dreamer " . toCharArray ( ) , " erase " . toCharArray ( ) , " eraser " . toCharArray ( ) } ; char [ ] s = sc . next ( ) . toCharArray ( ) ; int endIndex = s . length ; while ( endIndex > 0 ) { boolean f = false ; for ( char [ ] added : addeds ) { if ( isMatch ( s , added , endIndex ) ) { endIndex -= added . length ; f = true ; break ; } } if ( f == false ) { System . out . println ( " NO " ) ; return ; } } System . out . println ( " YES " ) ; } boolean isMatch ( char [ ] s , char [ ] added , int endIndex ) { if ( endIndex - added . length < 0 ) { return false ; } for ( int i = 0 ; i < added . length ; i ++ ) { if ( added [ i ] != s [ endIndex - added . length + i ] ) { return false ; } } return true ; } }
from collections import deque NEW_LINE def solve ( s ) : NEW_LINE INDENT stack = deque ( ) NEW_LINE stack . append ( s ) NEW_LINE while len ( stack ) > 0 : NEW_LINE INDENT top = stack . pop ( ) NEW_LINE if top == ' ' : NEW_LINE INDENT return ' YES ' NEW_LINE DEDENT if top [ : 5 ] == ' dream ' : NEW_LINE INDENT if top [ 5 : 7 ] == ' er ' : NEW_LINE INDENT stack . append ( top [ 7 : ] ) NEW_LINE DEDENT stack . append ( top [ 5 : ] ) NEW_LINE DEDENT elif top [ : 5 ] == ' erase ' : NEW_LINE INDENT if len ( top ) > 5 and top [ 5 ] == ' r ' : NEW_LINE INDENT stack . append ( top [ 6 : ] ) NEW_LINE DEDENT else : NEW_LINE INDENT stack . append ( top [ 5 : ] ) NEW_LINE DEDENT DEDENT DEDENT return ' NO ' NEW_LINE DEDENT print ( solve ( input ( ) ) ) NEW_LINE
T347
import java . util . Scanner ; public class Main { public static void main ( String [ ] args ) { try ( Scanner sc = new Scanner ( System . in ) ; ) { new Main ( ) . solve ( sc ) ; } } void solve ( Scanner sc ) { char [ ] [ ] addeds = { " dream " . toCharArray ( ) , " dreamer " . toCharArray ( ) , " erase " . toCharArray ( ) , " eraser " . toCharArray ( ) } ; char [ ] s = sc . next ( ) . toCharArray ( ) ; int endIndex = s . length ; while ( endIndex > 0 ) { boolean f = false ; for ( char [ ] added : addeds ) { if ( isMatch ( s , added , endIndex ) ) { endIndex -= added . length ; f = true ; break ; } } if ( f == false ) { System . out . println ( " NO " ) ; return ; } } System . out . println ( " YES " ) ; } boolean isMatch ( char [ ] s , char [ ] added , int endIndex ) { if ( endIndex - added . length < 0 ) { return false ; } for ( int i = 0 ; i < added . length ; i ++ ) { if ( added [ i ] != s [ endIndex - added . length + i ] ) { return false ; } } return true ; } }
S = input ( ) NEW_LINE S = S [ : : - 1 ] NEW_LINE S = S . replace ( ' resare ' , ' ' ) NEW_LINE S = S . replace ( ' esare ' , ' ' ) NEW_LINE S = S . replace ( ' remaerd ' , ' ' ) NEW_LINE S = S . replace ( ' maerd ' , ' ' ) NEW_LINE if S : NEW_LINE INDENT print ( ' NO ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' YES ' ) NEW_LINE DEDENT
T348
import java . util . Scanner ; public class Main { public static void main ( String [ ] args ) { try ( Scanner sc = new Scanner ( System . in ) ; ) { new Main ( ) . solve ( sc ) ; } } void solve ( Scanner sc ) { char [ ] [ ] addeds = { " dream " . toCharArray ( ) , " dreamer " . toCharArray ( ) , " erase " . toCharArray ( ) , " eraser " . toCharArray ( ) } ; char [ ] s = sc . next ( ) . toCharArray ( ) ; int endIndex = s . length ; while ( endIndex > 0 ) { boolean f = false ; for ( char [ ] added : addeds ) { if ( isMatch ( s , added , endIndex ) ) { endIndex -= added . length ; f = true ; break ; } } if ( f == false ) { System . out . println ( " NO " ) ; return ; } } System . out . println ( " YES " ) ; } boolean isMatch ( char [ ] s , char [ ] added , int endIndex ) { if ( endIndex - added . length < 0 ) { return false ; } for ( int i = 0 ; i < added . length ; i ++ ) { if ( added [ i ] != s [ endIndex - added . length + i ] ) { return false ; } } return true ; } }
def db ( x ) : NEW_LINE INDENT global debug NEW_LINE if debug : NEW_LINE INDENT print ( x ) NEW_LINE DEDENT DEDENT def gen ( tstr ) : NEW_LINE INDENT if len ( tstr ) <= len ( S ) : NEW_LINE INDENT for w in words : NEW_LINE INDENT yield tstr + w NEW_LINE for w2 in gen ( tstr + w ) : NEW_LINE INDENT yield w2 NEW_LINE DEDENT DEDENT DEDENT DEDENT def solve ( ) : NEW_LINE INDENT ans = False NEW_LINE for genstr in gen ( ' ' ) : NEW_LINE INDENT db ( genstr ) NEW_LINE if genstr == S : NEW_LINE INDENT ans = True NEW_LINE break NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def solve2 ( ) : NEW_LINE INDENT stack = list ( words ) NEW_LINE while stack != [ ] : NEW_LINE INDENT str = stack . pop ( ) NEW_LINE db ( ( ' pop ' , str ) ) NEW_LINE if len ( str ) < len ( S ) : NEW_LINE INDENT for w in words : NEW_LINE INDENT stack . append ( str + w ) NEW_LINE DEDENT DEDENT elif len ( str ) == len ( S ) and str == S : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def solve3 ( ) : NEW_LINE INDENT string = S + ' ▁ ' NEW_LINE n = len ( string ) NEW_LINE while n > 1 : NEW_LINE INDENT for w in words : NEW_LINE INDENT db ( ( string . rfind ( w , 0 , n ) , len ( w ) , n ) ) NEW_LINE if string . rfind ( w , 0 , n ) + len ( w ) + 1 == n : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT db ( ( n , w ) ) NEW_LINE n -= len ( w ) NEW_LINE DEDENT return True NEW_LINE DEDENT debug = False NEW_LINE words = ( ' dream ' , ' dreamer ' , ' erase ' , ' eraser ' ) NEW_LINE S = input ( ) NEW_LINE db ( S ) NEW_LINE if solve3 ( ) : NEW_LINE INDENT print ( ' YES ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' NO ' ) NEW_LINE DEDENT
T349
import java . util . Scanner ; public class Main { public static void main ( String [ ] args ) { try ( Scanner sc = new Scanner ( System . in ) ; ) { new Main ( ) . solve ( sc ) ; } } void solve ( Scanner sc ) { char [ ] [ ] addeds = { " dream " . toCharArray ( ) , " dreamer " . toCharArray ( ) , " erase " . toCharArray ( ) , " eraser " . toCharArray ( ) } ; char [ ] s = sc . next ( ) . toCharArray ( ) ; int endIndex = s . length ; while ( endIndex > 0 ) { boolean f = false ; for ( char [ ] added : addeds ) { if ( isMatch ( s , added , endIndex ) ) { endIndex -= added . length ; f = true ; break ; } } if ( f == false ) { System . out . println ( " NO " ) ; return ; } } System . out . println ( " YES " ) ; } boolean isMatch ( char [ ] s , char [ ] added , int endIndex ) { if ( endIndex - added . length < 0 ) { return false ; } for ( int i = 0 ; i < added . length ; i ++ ) { if ( added [ i ] != s [ endIndex - added . length + i ] ) { return false ; } } return true ; } }
def YESNO ( ans , yes = " YES " , no = " NO " ) : NEW_LINE INDENT print ( [ no , yes ] [ ans ] ) NEW_LINE DEDENT II = lambda : int ( input ( ) ) NEW_LINE MI = lambda : map ( int , input ( ) . split ( ) ) NEW_LINE MIL = lambda : list ( MI ( ) ) NEW_LINE MIS = lambda : input ( ) . split ( ) NEW_LINE def check ( s ) : NEW_LINE INDENT if s == " " : NEW_LINE INDENT return True NEW_LINE DEDENT if s [ : 5 ] in [ " dream " , " erase " ] : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT S = input ( ) NEW_LINE i = 0 NEW_LINE l = len ( S ) NEW_LINE while i < l : NEW_LINE INDENT if S [ i : i + 7 ] == " dreamer " and check ( S [ i + 7 : ] ) : NEW_LINE INDENT i += 7 NEW_LINE DEDENT elif S [ i : i + 6 ] == " eraser " and check ( S [ i + 6 : ] ) : NEW_LINE INDENT i += 6 NEW_LINE DEDENT elif check ( S [ i : ] ) : NEW_LINE INDENT i += 5 NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT YESNO ( main ( ) ) NEW_LINE DEDENT
T350
import java . util . Scanner ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; String inputText = sc . next ( ) ; char [ ] p1 = { ' m ' , ' a ' , ' e ' , ' r ' , ' d ' } ; char [ ] p2 = { ' r ' , ' e ' , ' m ' , ' a ' , ' e ' , ' r ' , ' d ' } ; char [ ] p3 = { ' e ' , ' s ' , ' a ' , ' r ' , ' e ' } ; char [ ] p4 = { ' r ' , ' e ' , ' s ' , ' a ' , ' r ' , ' e ' } ; char [ ] inputArray = inputText . toCharArray ( ) ; int wordIndex = 0 ; char [ ] targetArray = null ; for ( int i = inputArray . length - 1 ; i >= 0 ; i -- ) { if ( wordIndex == 0 ) { if ( inputArray [ i ] == ' m ' ) { targetArray = p1 ; } else if ( inputArray [ i ] == ' e ' ) { targetArray = p3 ; } else if ( inputArray [ i ] == ' r ' ) { if ( i - 2 > 0 ) { if ( inputArray [ i - 2 ] == ' m ' ) { targetArray = p2 ; } else if ( inputArray [ i - 2 ] == ' s ' ) { targetArray = p4 ; } else { System . out . println ( " NO " ) ; return ; } } else { System . out . println ( " NO " ) ; return ; } } else { System . out . println ( " NO " ) ; return ; } } else { if ( targetArray [ wordIndex ] != inputArray [ i ] ) { System . out . println ( " NO " ) ; return ; } } if ( wordIndex < targetArray . length - 1 ) { wordIndex ++ ; } else { wordIndex = 0 ; targetArray = null ; } } System . out . println ( " YES " ) ; } }
s = list ( input ( ) ) NEW_LINE c = 0 NEW_LINE while len ( s ) > 0 : NEW_LINE INDENT p = "1" NEW_LINE q = "1" NEW_LINE r = "1" NEW_LINE if len ( s ) < 5 : NEW_LINE INDENT break NEW_LINE DEDENT if len ( s ) <= 7 : NEW_LINE INDENT if len ( s ) == 5 : NEW_LINE INDENT if s == list ( " dream " ) or s == list ( " erase " ) : NEW_LINE INDENT c = 1 NEW_LINE break NEW_LINE DEDENT DEDENT elif len ( s ) == 6 : NEW_LINE INDENT if s == list ( " eraser " ) : NEW_LINE INDENT c = 1 NEW_LINE break NEW_LINE DEDENT DEDENT elif len ( s ) == 7 : NEW_LINE INDENT if s == list ( " dreamer " ) : NEW_LINE INDENT c = 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT p = s [ len ( s ) - 5 : len ( s ) ] NEW_LINE q = s [ len ( s ) - 6 : len ( s ) ] NEW_LINE r = s [ len ( s ) - 7 : len ( s ) ] NEW_LINE if p == list ( " dream " ) or p == list ( " erase " ) : NEW_LINE INDENT del s [ len ( s ) - 5 : len ( s ) ] NEW_LINE DEDENT elif q == list ( " eraser " ) : NEW_LINE INDENT del s [ len ( s ) - 6 : len ( s ) ] NEW_LINE DEDENT elif r == list ( " dreamer " ) : NEW_LINE INDENT del s [ len ( s ) - 7 : len ( s ) ] NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT print ( " YES " if c == 1 else " NO " ) NEW_LINE
T351
import java . util . Scanner ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; String inputText = sc . next ( ) ; char [ ] p1 = { ' m ' , ' a ' , ' e ' , ' r ' , ' d ' } ; char [ ] p2 = { ' r ' , ' e ' , ' m ' , ' a ' , ' e ' , ' r ' , ' d ' } ; char [ ] p3 = { ' e ' , ' s ' , ' a ' , ' r ' , ' e ' } ; char [ ] p4 = { ' r ' , ' e ' , ' s ' , ' a ' , ' r ' , ' e ' } ; char [ ] inputArray = inputText . toCharArray ( ) ; int wordIndex = 0 ; char [ ] targetArray = null ; for ( int i = inputArray . length - 1 ; i >= 0 ; i -- ) { if ( wordIndex == 0 ) { if ( inputArray [ i ] == ' m ' ) { targetArray = p1 ; } else if ( inputArray [ i ] == ' e ' ) { targetArray = p3 ; } else if ( inputArray [ i ] == ' r ' ) { if ( i - 2 > 0 ) { if ( inputArray [ i - 2 ] == ' m ' ) { targetArray = p2 ; } else if ( inputArray [ i - 2 ] == ' s ' ) { targetArray = p4 ; } else { System . out . println ( " NO " ) ; return ; } } else { System . out . println ( " NO " ) ; return ; } } else { System . out . println ( " NO " ) ; return ; } } else { if ( targetArray [ wordIndex ] != inputArray [ i ] ) { System . out . println ( " NO " ) ; return ; } } if ( wordIndex < targetArray . length - 1 ) { wordIndex ++ ; } else { wordIndex = 0 ; targetArray = null ; } } System . out . println ( " YES " ) ; } }
from collections import deque NEW_LINE def solve ( s ) : NEW_LINE INDENT stack = deque ( ) NEW_LINE stack . append ( s ) NEW_LINE while len ( stack ) > 0 : NEW_LINE INDENT top = stack . pop ( ) NEW_LINE if top == ' ' : NEW_LINE INDENT return ' YES ' NEW_LINE DEDENT if top [ : 5 ] == ' dream ' : NEW_LINE INDENT if top [ 5 : 7 ] == ' er ' : NEW_LINE INDENT stack . append ( top [ 7 : ] ) NEW_LINE DEDENT stack . append ( top [ 5 : ] ) NEW_LINE DEDENT elif top [ : 5 ] == ' erase ' : NEW_LINE INDENT if len ( top ) > 5 and top [ 5 ] == ' r ' : NEW_LINE INDENT stack . append ( top [ 6 : ] ) NEW_LINE DEDENT else : NEW_LINE INDENT stack . append ( top [ 5 : ] ) NEW_LINE DEDENT DEDENT DEDENT return ' NO ' NEW_LINE DEDENT print ( solve ( input ( ) ) ) NEW_LINE
T352
import java . util . Scanner ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; String inputText = sc . next ( ) ; char [ ] p1 = { ' m ' , ' a ' , ' e ' , ' r ' , ' d ' } ; char [ ] p2 = { ' r ' , ' e ' , ' m ' , ' a ' , ' e ' , ' r ' , ' d ' } ; char [ ] p3 = { ' e ' , ' s ' , ' a ' , ' r ' , ' e ' } ; char [ ] p4 = { ' r ' , ' e ' , ' s ' , ' a ' , ' r ' , ' e ' } ; char [ ] inputArray = inputText . toCharArray ( ) ; int wordIndex = 0 ; char [ ] targetArray = null ; for ( int i = inputArray . length - 1 ; i >= 0 ; i -- ) { if ( wordIndex == 0 ) { if ( inputArray [ i ] == ' m ' ) { targetArray = p1 ; } else if ( inputArray [ i ] == ' e ' ) { targetArray = p3 ; } else if ( inputArray [ i ] == ' r ' ) { if ( i - 2 > 0 ) { if ( inputArray [ i - 2 ] == ' m ' ) { targetArray = p2 ; } else if ( inputArray [ i - 2 ] == ' s ' ) { targetArray = p4 ; } else { System . out . println ( " NO " ) ; return ; } } else { System . out . println ( " NO " ) ; return ; } } else { System . out . println ( " NO " ) ; return ; } } else { if ( targetArray [ wordIndex ] != inputArray [ i ] ) { System . out . println ( " NO " ) ; return ; } } if ( wordIndex < targetArray . length - 1 ) { wordIndex ++ ; } else { wordIndex = 0 ; targetArray = null ; } } System . out . println ( " YES " ) ; } }
S = input ( ) NEW_LINE S = S [ : : - 1 ] NEW_LINE S = S . replace ( ' resare ' , ' ' ) NEW_LINE S = S . replace ( ' esare ' , ' ' ) NEW_LINE S = S . replace ( ' remaerd ' , ' ' ) NEW_LINE S = S . replace ( ' maerd ' , ' ' ) NEW_LINE if S : NEW_LINE INDENT print ( ' NO ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' YES ' ) NEW_LINE DEDENT
T353
import java . util . Scanner ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; String inputText = sc . next ( ) ; char [ ] p1 = { ' m ' , ' a ' , ' e ' , ' r ' , ' d ' } ; char [ ] p2 = { ' r ' , ' e ' , ' m ' , ' a ' , ' e ' , ' r ' , ' d ' } ; char [ ] p3 = { ' e ' , ' s ' , ' a ' , ' r ' , ' e ' } ; char [ ] p4 = { ' r ' , ' e ' , ' s ' , ' a ' , ' r ' , ' e ' } ; char [ ] inputArray = inputText . toCharArray ( ) ; int wordIndex = 0 ; char [ ] targetArray = null ; for ( int i = inputArray . length - 1 ; i >= 0 ; i -- ) { if ( wordIndex == 0 ) { if ( inputArray [ i ] == ' m ' ) { targetArray = p1 ; } else if ( inputArray [ i ] == ' e ' ) { targetArray = p3 ; } else if ( inputArray [ i ] == ' r ' ) { if ( i - 2 > 0 ) { if ( inputArray [ i - 2 ] == ' m ' ) { targetArray = p2 ; } else if ( inputArray [ i - 2 ] == ' s ' ) { targetArray = p4 ; } else { System . out . println ( " NO " ) ; return ; } } else { System . out . println ( " NO " ) ; return ; } } else { System . out . println ( " NO " ) ; return ; } } else { if ( targetArray [ wordIndex ] != inputArray [ i ] ) { System . out . println ( " NO " ) ; return ; } } if ( wordIndex < targetArray . length - 1 ) { wordIndex ++ ; } else { wordIndex = 0 ; targetArray = null ; } } System . out . println ( " YES " ) ; } }
def db ( x ) : NEW_LINE INDENT global debug NEW_LINE if debug : NEW_LINE INDENT print ( x ) NEW_LINE DEDENT DEDENT def gen ( tstr ) : NEW_LINE INDENT if len ( tstr ) <= len ( S ) : NEW_LINE INDENT for w in words : NEW_LINE INDENT yield tstr + w NEW_LINE for w2 in gen ( tstr + w ) : NEW_LINE INDENT yield w2 NEW_LINE DEDENT DEDENT DEDENT DEDENT def solve ( ) : NEW_LINE INDENT ans = False NEW_LINE for genstr in gen ( ' ' ) : NEW_LINE INDENT db ( genstr ) NEW_LINE if genstr == S : NEW_LINE INDENT ans = True NEW_LINE break NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def solve2 ( ) : NEW_LINE INDENT stack = list ( words ) NEW_LINE while stack != [ ] : NEW_LINE INDENT str = stack . pop ( ) NEW_LINE db ( ( ' pop ' , str ) ) NEW_LINE if len ( str ) < len ( S ) : NEW_LINE INDENT for w in words : NEW_LINE INDENT stack . append ( str + w ) NEW_LINE DEDENT DEDENT elif len ( str ) == len ( S ) and str == S : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def solve3 ( ) : NEW_LINE INDENT string = S + ' ▁ ' NEW_LINE n = len ( string ) NEW_LINE while n > 1 : NEW_LINE INDENT for w in words : NEW_LINE INDENT db ( ( string . rfind ( w , 0 , n ) , len ( w ) , n ) ) NEW_LINE if string . rfind ( w , 0 , n ) + len ( w ) + 1 == n : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT db ( ( n , w ) ) NEW_LINE n -= len ( w ) NEW_LINE DEDENT return True NEW_LINE DEDENT debug = False NEW_LINE words = ( ' dream ' , ' dreamer ' , ' erase ' , ' eraser ' ) NEW_LINE S = input ( ) NEW_LINE db ( S ) NEW_LINE if solve3 ( ) : NEW_LINE INDENT print ( ' YES ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' NO ' ) NEW_LINE DEDENT
T354
import java . util . Scanner ; public class Main { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; String inputText = sc . next ( ) ; char [ ] p1 = { ' m ' , ' a ' , ' e ' , ' r ' , ' d ' } ; char [ ] p2 = { ' r ' , ' e ' , ' m ' , ' a ' , ' e ' , ' r ' , ' d ' } ; char [ ] p3 = { ' e ' , ' s ' , ' a ' , ' r ' , ' e ' } ; char [ ] p4 = { ' r ' , ' e ' , ' s ' , ' a ' , ' r ' , ' e ' } ; char [ ] inputArray = inputText . toCharArray ( ) ; int wordIndex = 0 ; char [ ] targetArray = null ; for ( int i = inputArray . length - 1 ; i >= 0 ; i -- ) { if ( wordIndex == 0 ) { if ( inputArray [ i ] == ' m ' ) { targetArray = p1 ; } else if ( inputArray [ i ] == ' e ' ) { targetArray = p3 ; } else if ( inputArray [ i ] == ' r ' ) { if ( i - 2 > 0 ) { if ( inputArray [ i - 2 ] == ' m ' ) { targetArray = p2 ; } else if ( inputArray [ i - 2 ] == ' s ' ) { targetArray = p4 ; } else { System . out . println ( " NO " ) ; return ; } } else { System . out . println ( " NO " ) ; return ; } } else { System . out . println ( " NO " ) ; return ; } } else { if ( targetArray [ wordIndex ] != inputArray [ i ] ) { System . out . println ( " NO " ) ; return ; } } if ( wordIndex < targetArray . length - 1 ) { wordIndex ++ ; } else { wordIndex = 0 ; targetArray = null ; } } System . out . println ( " YES " ) ; } }
def YESNO ( ans , yes = " YES " , no = " NO " ) : NEW_LINE INDENT print ( [ no , yes ] [ ans ] ) NEW_LINE DEDENT II = lambda : int ( input ( ) ) NEW_LINE MI = lambda : map ( int , input ( ) . split ( ) ) NEW_LINE MIL = lambda : list ( MI ( ) ) NEW_LINE MIS = lambda : input ( ) . split ( ) NEW_LINE def check ( s ) : NEW_LINE INDENT if s == " " : NEW_LINE INDENT return True NEW_LINE DEDENT if s [ : 5 ] in [ " dream " , " erase " ] : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT S = input ( ) NEW_LINE i = 0 NEW_LINE l = len ( S ) NEW_LINE while i < l : NEW_LINE INDENT if S [ i : i + 7 ] == " dreamer " and check ( S [ i + 7 : ] ) : NEW_LINE INDENT i += 7 NEW_LINE DEDENT elif S [ i : i + 6 ] == " eraser " and check ( S [ i + 6 : ] ) : NEW_LINE INDENT i += 6 NEW_LINE DEDENT elif check ( S [ i : ] ) : NEW_LINE INDENT i += 5 NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT YESNO ( main ( ) ) NEW_LINE DEDENT
T355
import java . util . * ; import java . awt . Point ; public class Main { public static void main ( String [ ] args ) { new Main ( ) . solve ( ) ; } public void solve ( ) { Scanner scanner = new Scanner ( System . in ) ; long H = scanner . nextLong ( ) ; long W = scanner . nextLong ( ) ; int N = scanner . nextInt ( ) ; Point [ ] point = new Point [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { int row = scanner . nextInt ( ) ; int col = scanner . nextInt ( ) ; point [ i ] = new Point ( col , row ) ; } HashMap < Point , Integer > hashmap = new HashMap < Point , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int y = - 1 ; y < 2 ; y ++ ) { for ( int x = - 1 ; x < 2 ; x ++ ) { Point p = new Point ( point [ i ] . x + x , point [ i ] . y + y ) ; if ( hashmap . get ( p ) == null ) { hashmap . put ( p , 1 ) ; } else { hashmap . put ( p , hashmap . get ( p ) + 1 ) ; } } } } long [ ] counter = new long [ 10 ] ; for ( int i = 0 ; i < counter . length ; i ++ ) { counter [ i ] = 0 ; } long sum = ( H - 2 ) * ( W - 2 ) ; for ( HashMap . Entry < Point , Integer > map : hashmap . entrySet ( ) ) { if ( 1 < map . getKey ( ) . x && map . getKey ( ) . x < W && 1 < map . getKey ( ) . y && map . getKey ( ) . y < H ) { counter [ map . getValue ( ) ] ++ ; sum -- ; } } counter [ 0 ] = sum ; for ( int i = 0 ; i < counter . length ; i ++ ) { System . out . println ( counter [ i ] ) ; } } }
import sys NEW_LINE input = sys . stdin . readline NEW_LINE sys . setrecursionlimit ( 10000 ) NEW_LINE H , W , N = ( int ( i ) for i in input ( ) . split ( ) ) NEW_LINE if N == 0 : NEW_LINE INDENT reslis = [ 0 ] * 10 NEW_LINE reslis [ 0 ] = ( H - 2 ) * ( W - 2 ) - sum ( reslis ) NEW_LINE DEDENT else : NEW_LINE INDENT L = [ ] NEW_LINE for k in range ( N ) : NEW_LINE INDENT a , b = ( int ( i ) for i in input ( ) . split ( ) ) NEW_LINE for i in range ( 3 ) : NEW_LINE INDENT for j in range ( 3 ) : NEW_LINE INDENT if 1 <= a - 2 + i <= H - 2 and 1 <= b - 2 + j <= W - 2 : NEW_LINE INDENT L . append ( ( a - 2 + i , b - 2 + j ) ) NEW_LINE DEDENT DEDENT DEDENT DEDENT L . sort ( ) NEW_LINE temp = None NEW_LINE cc = 1 NEW_LINE reslis = [ 0 ] * 10 NEW_LINE for se in L : NEW_LINE INDENT if temp == se : NEW_LINE INDENT cc += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if temp == None : NEW_LINE INDENT pass NEW_LINE DEDENT else : NEW_LINE INDENT reslis [ cc ] += 1 NEW_LINE cc = 1 NEW_LINE DEDENT temp = se NEW_LINE DEDENT DEDENT reslis [ cc ] += 1 NEW_LINE reslis [ 0 ] = ( H - 2 ) * ( W - 2 ) - sum ( reslis ) NEW_LINE DEDENT for i in reslis : NEW_LINE INDENT print ( i ) NEW_LINE DEDENT
T356
import java . util . * ; import java . awt . Point ; public class Main { public static void main ( String [ ] args ) { new Main ( ) . solve ( ) ; } public void solve ( ) { Scanner scanner = new Scanner ( System . in ) ; long H = scanner . nextLong ( ) ; long W = scanner . nextLong ( ) ; int N = scanner . nextInt ( ) ; Point [ ] point = new Point [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { int row = scanner . nextInt ( ) ; int col = scanner . nextInt ( ) ; point [ i ] = new Point ( col , row ) ; } HashMap < Point , Integer > hashmap = new HashMap < Point , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int y = - 1 ; y < 2 ; y ++ ) { for ( int x = - 1 ; x < 2 ; x ++ ) { Point p = new Point ( point [ i ] . x + x , point [ i ] . y + y ) ; if ( hashmap . get ( p ) == null ) { hashmap . put ( p , 1 ) ; } else { hashmap . put ( p , hashmap . get ( p ) + 1 ) ; } } } } long [ ] counter = new long [ 10 ] ; for ( int i = 0 ; i < counter . length ; i ++ ) { counter [ i ] = 0 ; } long sum = ( H - 2 ) * ( W - 2 ) ; for ( HashMap . Entry < Point , Integer > map : hashmap . entrySet ( ) ) { if ( 1 < map . getKey ( ) . x && map . getKey ( ) . x < W && 1 < map . getKey ( ) . y && map . getKey ( ) . y < H ) { counter [ map . getValue ( ) ] ++ ; sum -- ; } } counter [ 0 ] = sum ; for ( int i = 0 ; i < counter . length ; i ++ ) { System . out . println ( counter [ i ] ) ; } } }
f = lambda : map ( int , input ( ) . split ( ) ) NEW_LINE h , w , n = f ( ) NEW_LINE c = [ ( h - 2 ) * ( w - 2 ) ] + [ 0 ] * 9 NEW_LINE d = { } NEW_LINE while n : NEW_LINE INDENT n -= 1 ; x , y = f ( ) NEW_LINE for i in range ( 9 ) : a = x + i % 3 , y + i // 3 ; g = h >= a [ 0 ] > 2 < a [ 1 ] <= w ; t = d [ a ] = d . get ( a , 0 ) + g ; c [ t - g ] -= 1 ; c [ t ] += 1 NEW_LINE DEDENT print ( * c ) NEW_LINE
T357
import java . util . * ; import java . awt . Point ; public class Main { public static void main ( String [ ] args ) { new Main ( ) . solve ( ) ; } public void solve ( ) { Scanner scanner = new Scanner ( System . in ) ; long H = scanner . nextLong ( ) ; long W = scanner . nextLong ( ) ; int N = scanner . nextInt ( ) ; Point [ ] point = new Point [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { int row = scanner . nextInt ( ) ; int col = scanner . nextInt ( ) ; point [ i ] = new Point ( col , row ) ; } HashMap < Point , Integer > hashmap = new HashMap < Point , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int y = - 1 ; y < 2 ; y ++ ) { for ( int x = - 1 ; x < 2 ; x ++ ) { Point p = new Point ( point [ i ] . x + x , point [ i ] . y + y ) ; if ( hashmap . get ( p ) == null ) { hashmap . put ( p , 1 ) ; } else { hashmap . put ( p , hashmap . get ( p ) + 1 ) ; } } } } long [ ] counter = new long [ 10 ] ; for ( int i = 0 ; i < counter . length ; i ++ ) { counter [ i ] = 0 ; } long sum = ( H - 2 ) * ( W - 2 ) ; for ( HashMap . Entry < Point , Integer > map : hashmap . entrySet ( ) ) { if ( 1 < map . getKey ( ) . x && map . getKey ( ) . x < W && 1 < map . getKey ( ) . y && map . getKey ( ) . y < H ) { counter [ map . getValue ( ) ] ++ ; sum -- ; } } counter [ 0 ] = sum ; for ( int i = 0 ; i < counter . length ; i ++ ) { System . out . println ( counter [ i ] ) ; } } }
from collections import defaultdict NEW_LINE def inpl ( ) : return [ int ( i ) for i in input ( ) . split ( ) ] NEW_LINE HW = defaultdict ( lambda : 0 ) NEW_LINE direction = [ ( 0 , 0 ) , ( 1 , 1 ) , ( 1 , 0 ) , ( 1 , - 1 ) , ( 0 , - 1 ) , ( - 1 , - 1 ) , ( - 1 , 0 ) , ( - 1 , 1 ) , ( 0 , 1 ) ] NEW_LINE H , W , N = inpl ( ) NEW_LINE for _ in range ( N ) : NEW_LINE INDENT a , b = inpl ( ) NEW_LINE for dh , dw in direction : NEW_LINE INDENT if 2 <= a + dh <= H - 1 and 2 <= b + dw <= W - 1 : NEW_LINE INDENT HW [ ( a + dh , b + dw ) ] += 1 NEW_LINE DEDENT DEDENT DEDENT ans = [ 0 for i in range ( 10 ) ] NEW_LINE print ( ( H - 2 ) * ( W - 2 ) - len ( HW ) ) NEW_LINE for j in HW . values ( ) : NEW_LINE INDENT ans [ j ] += 1 NEW_LINE DEDENT for i in ans [ 1 : ] : NEW_LINE INDENT print ( i ) NEW_LINE DEDENT
T358
import java . util . * ; import java . awt . Point ; public class Main { public static void main ( String [ ] args ) { new Main ( ) . solve ( ) ; } public void solve ( ) { Scanner scanner = new Scanner ( System . in ) ; long H = scanner . nextLong ( ) ; long W = scanner . nextLong ( ) ; int N = scanner . nextInt ( ) ; Point [ ] point = new Point [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { int row = scanner . nextInt ( ) ; int col = scanner . nextInt ( ) ; point [ i ] = new Point ( col , row ) ; } HashMap < Point , Integer > hashmap = new HashMap < Point , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int y = - 1 ; y < 2 ; y ++ ) { for ( int x = - 1 ; x < 2 ; x ++ ) { Point p = new Point ( point [ i ] . x + x , point [ i ] . y + y ) ; if ( hashmap . get ( p ) == null ) { hashmap . put ( p , 1 ) ; } else { hashmap . put ( p , hashmap . get ( p ) + 1 ) ; } } } } long [ ] counter = new long [ 10 ] ; for ( int i = 0 ; i < counter . length ; i ++ ) { counter [ i ] = 0 ; } long sum = ( H - 2 ) * ( W - 2 ) ; for ( HashMap . Entry < Point , Integer > map : hashmap . entrySet ( ) ) { if ( 1 < map . getKey ( ) . x && map . getKey ( ) . x < W && 1 < map . getKey ( ) . y && map . getKey ( ) . y < H ) { counter [ map . getValue ( ) ] ++ ; sum -- ; } } counter [ 0 ] = sum ; for ( int i = 0 ; i < counter . length ; i ++ ) { System . out . println ( counter [ i ] ) ; } } }
from collections import defaultdict NEW_LINE h , w , n = map ( int , input ( ) . split ( ) ) NEW_LINE abd = defaultdict ( int ) NEW_LINE for _ in range ( n ) : NEW_LINE INDENT a , b = map ( int , input ( ) . split ( ) ) NEW_LINE abd [ ( a , b ) ] += 1 NEW_LINE abd [ ( a , b - 1 ) ] += 1 NEW_LINE abd [ ( a , b - 2 ) ] += 1 NEW_LINE abd [ ( a - 1 , b ) ] += 1 NEW_LINE abd [ ( a - 1 , b - 1 ) ] += 1 NEW_LINE abd [ ( a - 1 , b - 2 ) ] += 1 NEW_LINE abd [ ( a - 2 , b ) ] += 1 NEW_LINE abd [ ( a - 2 , b - 1 ) ] += 1 NEW_LINE abd [ ( a - 2 , b - 2 ) ] += 1 NEW_LINE DEDENT suma = [ 0 for _ in range ( 10 ) ] NEW_LINE for a , b in abd . keys ( ) : NEW_LINE INDENT if 0 < a < h - 1 and 0 < b < w - 1 : NEW_LINE INDENT suma [ abd [ ( a , b ) ] ] += 1 NEW_LINE DEDENT DEDENT suma [ 0 ] = ( h - 2 ) * ( w - 2 ) - sum ( suma ) NEW_LINE print ( * suma , sep = ' \n ' ) NEW_LINE
T359
import java . util . * ; import java . awt . Point ; public class Main { public static void main ( String [ ] args ) { new Main ( ) . solve ( ) ; } public void solve ( ) { Scanner scanner = new Scanner ( System . in ) ; long H = scanner . nextLong ( ) ; long W = scanner . nextLong ( ) ; int N = scanner . nextInt ( ) ; Point [ ] point = new Point [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { int row = scanner . nextInt ( ) ; int col = scanner . nextInt ( ) ; point [ i ] = new Point ( col , row ) ; } HashMap < Point , Integer > hashmap = new HashMap < Point , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int y = - 1 ; y < 2 ; y ++ ) { for ( int x = - 1 ; x < 2 ; x ++ ) { Point p = new Point ( point [ i ] . x + x , point [ i ] . y + y ) ; if ( hashmap . get ( p ) == null ) { hashmap . put ( p , 1 ) ; } else { hashmap . put ( p , hashmap . get ( p ) + 1 ) ; } } } } long [ ] counter = new long [ 10 ] ; for ( int i = 0 ; i < counter . length ; i ++ ) { counter [ i ] = 0 ; } long sum = ( H - 2 ) * ( W - 2 ) ; for ( HashMap . Entry < Point , Integer > map : hashmap . entrySet ( ) ) { if ( 1 < map . getKey ( ) . x && map . getKey ( ) . x < W && 1 < map . getKey ( ) . y && map . getKey ( ) . y < H ) { counter [ map . getValue ( ) ] ++ ; sum -- ; } } counter [ 0 ] = sum ; for ( int i = 0 ; i < counter . length ; i ++ ) { System . out . println ( counter [ i ] ) ; } } }
import sys , collections NEW_LINE def check_range ( y , x , H , W ) : NEW_LINE INDENT return 0 < x and x < W and 0 < y and y < H NEW_LINE DEDENT def solve ( ) : NEW_LINE INDENT H , W , N = map ( int , input ( ) . split ( ) ) NEW_LINE L = [ 0 for _ in range ( 10 ) ] NEW_LINE L [ 0 ] = ( H - 2 ) * ( W - 2 ) NEW_LINE NL = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT y , x = map ( int , input ( ) . split ( ) ) NEW_LINE x -= 1 NEW_LINE y -= 1 NEW_LINE for i in range ( - 1 , 2 ) : NEW_LINE INDENT for j in range ( - 1 , 2 ) : NEW_LINE INDENT if check_range ( y + i , x + j , H - 1 , W - 1 ) : NEW_LINE INDENT NL . append ( ( y + i , x + j ) ) NEW_LINE DEDENT DEDENT DEDENT DEDENT NLD = collections . Counter ( NL ) NEW_LINE for v in NLD . values ( ) : NEW_LINE INDENT L [ v ] += 1 NEW_LINE L [ 0 ] -= 1 NEW_LINE DEDENT for v in L : NEW_LINE INDENT print ( v ) NEW_LINE DEDENT DEDENT solve ( ) NEW_LINE
T360
import java . util . * ; class Point implements Comparable < Point > { int a , b ; Point ( int a , int b ) { this . a = a ; this . b = b ; } public int hashCode ( ) { return ( ( a & 0xFFFF0000 ) | ( b >> 16 ) ) ^ ( ( b & 0xFFFF0000 ) | ( a >> 16 ) ) ; } public boolean equals ( Object object ) { Point point = ( Point ) object ; return this . a == point . a && this . b == point . b ; } public int compareTo ( Point p ) { if ( this . a != p . a ) { return this . a - p . a ; } return this . b - p . b ; } } public class Main { public static void main ( String [ ] args ) { Scanner s = new Scanner ( System . in ) ; int h = s . nextInt ( ) ; int w = s . nextInt ( ) ; int n = s . nextInt ( ) ; HashMap < Point , Integer > map = new HashMap < Point , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int a = s . nextInt ( ) - 1 ; int b = s . nextInt ( ) - 1 ; for ( int da = Math . max ( 0 , a - 2 ) ; da <= Math . min ( h - 3 , a ) ; da ++ ) { for ( int db = Math . max ( 0 , b - 2 ) ; db <= Math . min ( w - 3 , b ) ; db ++ ) { Point p = new Point ( da , db ) ; if ( map . get ( p ) == null ) { map . put ( p , 1 ) ; } else { map . put ( p , map . get ( p ) + 1 ) ; } } } } long [ ] output = new long [ 10 ] ; output [ 0 ] = ( long ) ( h - 2 ) * ( long ) ( w - 2 ) ; for ( int value : map . values ( ) ) { output [ 0 ] -- ; output [ value ] ++ ; } for ( long value : output ) { System . out . println ( value ) ; } } }
import sys NEW_LINE input = sys . stdin . readline NEW_LINE sys . setrecursionlimit ( 10000 ) NEW_LINE H , W , N = ( int ( i ) for i in input ( ) . split ( ) ) NEW_LINE if N == 0 : NEW_LINE INDENT reslis = [ 0 ] * 10 NEW_LINE reslis [ 0 ] = ( H - 2 ) * ( W - 2 ) - sum ( reslis ) NEW_LINE DEDENT else : NEW_LINE INDENT L = [ ] NEW_LINE for k in range ( N ) : NEW_LINE INDENT a , b = ( int ( i ) for i in input ( ) . split ( ) ) NEW_LINE for i in range ( 3 ) : NEW_LINE INDENT for j in range ( 3 ) : NEW_LINE INDENT if 1 <= a - 2 + i <= H - 2 and 1 <= b - 2 + j <= W - 2 : NEW_LINE INDENT L . append ( ( a - 2 + i , b - 2 + j ) ) NEW_LINE DEDENT DEDENT DEDENT DEDENT L . sort ( ) NEW_LINE temp = None NEW_LINE cc = 1 NEW_LINE reslis = [ 0 ] * 10 NEW_LINE for se in L : NEW_LINE INDENT if temp == se : NEW_LINE INDENT cc += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if temp == None : NEW_LINE INDENT pass NEW_LINE DEDENT else : NEW_LINE INDENT reslis [ cc ] += 1 NEW_LINE cc = 1 NEW_LINE DEDENT temp = se NEW_LINE DEDENT DEDENT reslis [ cc ] += 1 NEW_LINE reslis [ 0 ] = ( H - 2 ) * ( W - 2 ) - sum ( reslis ) NEW_LINE DEDENT for i in reslis : NEW_LINE INDENT print ( i ) NEW_LINE DEDENT
T361
import java . util . * ; class Point implements Comparable < Point > { int a , b ; Point ( int a , int b ) { this . a = a ; this . b = b ; } public int hashCode ( ) { return ( ( a & 0xFFFF0000 ) | ( b >> 16 ) ) ^ ( ( b & 0xFFFF0000 ) | ( a >> 16 ) ) ; } public boolean equals ( Object object ) { Point point = ( Point ) object ; return this . a == point . a && this . b == point . b ; } public int compareTo ( Point p ) { if ( this . a != p . a ) { return this . a - p . a ; } return this . b - p . b ; } } public class Main { public static void main ( String [ ] args ) { Scanner s = new Scanner ( System . in ) ; int h = s . nextInt ( ) ; int w = s . nextInt ( ) ; int n = s . nextInt ( ) ; HashMap < Point , Integer > map = new HashMap < Point , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int a = s . nextInt ( ) - 1 ; int b = s . nextInt ( ) - 1 ; for ( int da = Math . max ( 0 , a - 2 ) ; da <= Math . min ( h - 3 , a ) ; da ++ ) { for ( int db = Math . max ( 0 , b - 2 ) ; db <= Math . min ( w - 3 , b ) ; db ++ ) { Point p = new Point ( da , db ) ; if ( map . get ( p ) == null ) { map . put ( p , 1 ) ; } else { map . put ( p , map . get ( p ) + 1 ) ; } } } } long [ ] output = new long [ 10 ] ; output [ 0 ] = ( long ) ( h - 2 ) * ( long ) ( w - 2 ) ; for ( int value : map . values ( ) ) { output [ 0 ] -- ; output [ value ] ++ ; } for ( long value : output ) { System . out . println ( value ) ; } } }
f = lambda : map ( int , input ( ) . split ( ) ) NEW_LINE h , w , n = f ( ) NEW_LINE c = [ ( h - 2 ) * ( w - 2 ) ] + [ 0 ] * 9 NEW_LINE d = { } NEW_LINE while n : NEW_LINE INDENT n -= 1 ; x , y = f ( ) NEW_LINE for i in range ( 9 ) : a = x + i % 3 , y + i // 3 ; g = h >= a [ 0 ] > 2 < a [ 1 ] <= w ; t = d [ a ] = d . get ( a , 0 ) + g ; c [ t - g ] -= 1 ; c [ t ] += 1 NEW_LINE DEDENT print ( * c ) NEW_LINE
T362
import java . util . * ; class Point implements Comparable < Point > { int a , b ; Point ( int a , int b ) { this . a = a ; this . b = b ; } public int hashCode ( ) { return ( ( a & 0xFFFF0000 ) | ( b >> 16 ) ) ^ ( ( b & 0xFFFF0000 ) | ( a >> 16 ) ) ; } public boolean equals ( Object object ) { Point point = ( Point ) object ; return this . a == point . a && this . b == point . b ; } public int compareTo ( Point p ) { if ( this . a != p . a ) { return this . a - p . a ; } return this . b - p . b ; } } public class Main { public static void main ( String [ ] args ) { Scanner s = new Scanner ( System . in ) ; int h = s . nextInt ( ) ; int w = s . nextInt ( ) ; int n = s . nextInt ( ) ; HashMap < Point , Integer > map = new HashMap < Point , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int a = s . nextInt ( ) - 1 ; int b = s . nextInt ( ) - 1 ; for ( int da = Math . max ( 0 , a - 2 ) ; da <= Math . min ( h - 3 , a ) ; da ++ ) { for ( int db = Math . max ( 0 , b - 2 ) ; db <= Math . min ( w - 3 , b ) ; db ++ ) { Point p = new Point ( da , db ) ; if ( map . get ( p ) == null ) { map . put ( p , 1 ) ; } else { map . put ( p , map . get ( p ) + 1 ) ; } } } } long [ ] output = new long [ 10 ] ; output [ 0 ] = ( long ) ( h - 2 ) * ( long ) ( w - 2 ) ; for ( int value : map . values ( ) ) { output [ 0 ] -- ; output [ value ] ++ ; } for ( long value : output ) { System . out . println ( value ) ; } } }
from collections import defaultdict NEW_LINE def inpl ( ) : return [ int ( i ) for i in input ( ) . split ( ) ] NEW_LINE HW = defaultdict ( lambda : 0 ) NEW_LINE direction = [ ( 0 , 0 ) , ( 1 , 1 ) , ( 1 , 0 ) , ( 1 , - 1 ) , ( 0 , - 1 ) , ( - 1 , - 1 ) , ( - 1 , 0 ) , ( - 1 , 1 ) , ( 0 , 1 ) ] NEW_LINE H , W , N = inpl ( ) NEW_LINE for _ in range ( N ) : NEW_LINE INDENT a , b = inpl ( ) NEW_LINE for dh , dw in direction : NEW_LINE INDENT if 2 <= a + dh <= H - 1 and 2 <= b + dw <= W - 1 : NEW_LINE INDENT HW [ ( a + dh , b + dw ) ] += 1 NEW_LINE DEDENT DEDENT DEDENT ans = [ 0 for i in range ( 10 ) ] NEW_LINE print ( ( H - 2 ) * ( W - 2 ) - len ( HW ) ) NEW_LINE for j in HW . values ( ) : NEW_LINE INDENT ans [ j ] += 1 NEW_LINE DEDENT for i in ans [ 1 : ] : NEW_LINE INDENT print ( i ) NEW_LINE DEDENT
T363
import java . util . * ; class Point implements Comparable < Point > { int a , b ; Point ( int a , int b ) { this . a = a ; this . b = b ; } public int hashCode ( ) { return ( ( a & 0xFFFF0000 ) | ( b >> 16 ) ) ^ ( ( b & 0xFFFF0000 ) | ( a >> 16 ) ) ; } public boolean equals ( Object object ) { Point point = ( Point ) object ; return this . a == point . a && this . b == point . b ; } public int compareTo ( Point p ) { if ( this . a != p . a ) { return this . a - p . a ; } return this . b - p . b ; } } public class Main { public static void main ( String [ ] args ) { Scanner s = new Scanner ( System . in ) ; int h = s . nextInt ( ) ; int w = s . nextInt ( ) ; int n = s . nextInt ( ) ; HashMap < Point , Integer > map = new HashMap < Point , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int a = s . nextInt ( ) - 1 ; int b = s . nextInt ( ) - 1 ; for ( int da = Math . max ( 0 , a - 2 ) ; da <= Math . min ( h - 3 , a ) ; da ++ ) { for ( int db = Math . max ( 0 , b - 2 ) ; db <= Math . min ( w - 3 , b ) ; db ++ ) { Point p = new Point ( da , db ) ; if ( map . get ( p ) == null ) { map . put ( p , 1 ) ; } else { map . put ( p , map . get ( p ) + 1 ) ; } } } } long [ ] output = new long [ 10 ] ; output [ 0 ] = ( long ) ( h - 2 ) * ( long ) ( w - 2 ) ; for ( int value : map . values ( ) ) { output [ 0 ] -- ; output [ value ] ++ ; } for ( long value : output ) { System . out . println ( value ) ; } } }
from collections import defaultdict NEW_LINE h , w , n = map ( int , input ( ) . split ( ) ) NEW_LINE abd = defaultdict ( int ) NEW_LINE for _ in range ( n ) : NEW_LINE INDENT a , b = map ( int , input ( ) . split ( ) ) NEW_LINE abd [ ( a , b ) ] += 1 NEW_LINE abd [ ( a , b - 1 ) ] += 1 NEW_LINE abd [ ( a , b - 2 ) ] += 1 NEW_LINE abd [ ( a - 1 , b ) ] += 1 NEW_LINE abd [ ( a - 1 , b - 1 ) ] += 1 NEW_LINE abd [ ( a - 1 , b - 2 ) ] += 1 NEW_LINE abd [ ( a - 2 , b ) ] += 1 NEW_LINE abd [ ( a - 2 , b - 1 ) ] += 1 NEW_LINE abd [ ( a - 2 , b - 2 ) ] += 1 NEW_LINE DEDENT suma = [ 0 for _ in range ( 10 ) ] NEW_LINE for a , b in abd . keys ( ) : NEW_LINE INDENT if 0 < a < h - 1 and 0 < b < w - 1 : NEW_LINE INDENT suma [ abd [ ( a , b ) ] ] += 1 NEW_LINE DEDENT DEDENT suma [ 0 ] = ( h - 2 ) * ( w - 2 ) - sum ( suma ) NEW_LINE print ( * suma , sep = ' \n ' ) NEW_LINE
T364
import java . util . * ; class Point implements Comparable < Point > { int a , b ; Point ( int a , int b ) { this . a = a ; this . b = b ; } public int hashCode ( ) { return ( ( a & 0xFFFF0000 ) | ( b >> 16 ) ) ^ ( ( b & 0xFFFF0000 ) | ( a >> 16 ) ) ; } public boolean equals ( Object object ) { Point point = ( Point ) object ; return this . a == point . a && this . b == point . b ; } public int compareTo ( Point p ) { if ( this . a != p . a ) { return this . a - p . a ; } return this . b - p . b ; } } public class Main { public static void main ( String [ ] args ) { Scanner s = new Scanner ( System . in ) ; int h = s . nextInt ( ) ; int w = s . nextInt ( ) ; int n = s . nextInt ( ) ; HashMap < Point , Integer > map = new HashMap < Point , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int a = s . nextInt ( ) - 1 ; int b = s . nextInt ( ) - 1 ; for ( int da = Math . max ( 0 , a - 2 ) ; da <= Math . min ( h - 3 , a ) ; da ++ ) { for ( int db = Math . max ( 0 , b - 2 ) ; db <= Math . min ( w - 3 , b ) ; db ++ ) { Point p = new Point ( da , db ) ; if ( map . get ( p ) == null ) { map . put ( p , 1 ) ; } else { map . put ( p , map . get ( p ) + 1 ) ; } } } } long [ ] output = new long [ 10 ] ; output [ 0 ] = ( long ) ( h - 2 ) * ( long ) ( w - 2 ) ; for ( int value : map . values ( ) ) { output [ 0 ] -- ; output [ value ] ++ ; } for ( long value : output ) { System . out . println ( value ) ; } } }
import sys , collections NEW_LINE def check_range ( y , x , H , W ) : NEW_LINE INDENT return 0 < x and x < W and 0 < y and y < H NEW_LINE DEDENT def solve ( ) : NEW_LINE INDENT H , W , N = map ( int , input ( ) . split ( ) ) NEW_LINE L = [ 0 for _ in range ( 10 ) ] NEW_LINE L [ 0 ] = ( H - 2 ) * ( W - 2 ) NEW_LINE NL = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT y , x = map ( int , input ( ) . split ( ) ) NEW_LINE x -= 1 NEW_LINE y -= 1 NEW_LINE for i in range ( - 1 , 2 ) : NEW_LINE INDENT for j in range ( - 1 , 2 ) : NEW_LINE INDENT if check_range ( y + i , x + j , H - 1 , W - 1 ) : NEW_LINE INDENT NL . append ( ( y + i , x + j ) ) NEW_LINE DEDENT DEDENT DEDENT DEDENT NLD = collections . Counter ( NL ) NEW_LINE for v in NLD . values ( ) : NEW_LINE INDENT L [ v ] += 1 NEW_LINE L [ 0 ] -= 1 NEW_LINE DEDENT for v in L : NEW_LINE INDENT print ( v ) NEW_LINE DEDENT DEDENT solve ( ) NEW_LINE
T365
import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; long h = in . nextLong ( ) , w = in . nextLong ( ) ; int n = in . nextInt ( ) ; Map < Long , Integer > map = new HashMap < > ( ) ; int [ ] dx = { 0 , 0 , 1 , 1 , 1 , 0 , - 1 , - 1 , - 1 } ; int [ ] dy = { 0 , - 1 , - 1 , 0 , 1 , 1 , 1 , 0 , - 1 } ; for ( int i = 0 ; i < n ; i ++ ) { int a = in . nextInt ( ) - 1 , b = in . nextInt ( ) - 1 ; for ( int k = 0 ; k < 9 ; k ++ ) { int y = a + dy [ k ] , x = b + dx [ k ] ; if ( x < 1 || w - 1 <= x || y < 1 || h - 1 <= y ) { continue ; } long key = ( long ) y << 32 | x ; Integer val = map . get ( key ) ; map . put ( key , val == null ? 1 : val + 1 ) ; } } System . out . println ( ( h - 2 ) * ( w - 2 ) - map . size ( ) ) ; long [ ] cnt = new long [ 10 ] ; for ( long key : map . keySet ( ) ) { cnt [ map . get ( key ) ] ++ ; } for ( int i = 1 ; i < 10 ; i ++ ) { System . out . println ( cnt [ i ] ) ; } } }
import sys NEW_LINE input = sys . stdin . readline NEW_LINE sys . setrecursionlimit ( 10000 ) NEW_LINE H , W , N = ( int ( i ) for i in input ( ) . split ( ) ) NEW_LINE if N == 0 : NEW_LINE INDENT reslis = [ 0 ] * 10 NEW_LINE reslis [ 0 ] = ( H - 2 ) * ( W - 2 ) - sum ( reslis ) NEW_LINE DEDENT else : NEW_LINE INDENT L = [ ] NEW_LINE for k in range ( N ) : NEW_LINE INDENT a , b = ( int ( i ) for i in input ( ) . split ( ) ) NEW_LINE for i in range ( 3 ) : NEW_LINE INDENT for j in range ( 3 ) : NEW_LINE INDENT if 1 <= a - 2 + i <= H - 2 and 1 <= b - 2 + j <= W - 2 : NEW_LINE INDENT L . append ( ( a - 2 + i , b - 2 + j ) ) NEW_LINE DEDENT DEDENT DEDENT DEDENT L . sort ( ) NEW_LINE temp = None NEW_LINE cc = 1 NEW_LINE reslis = [ 0 ] * 10 NEW_LINE for se in L : NEW_LINE INDENT if temp == se : NEW_LINE INDENT cc += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if temp == None : NEW_LINE INDENT pass NEW_LINE DEDENT else : NEW_LINE INDENT reslis [ cc ] += 1 NEW_LINE cc = 1 NEW_LINE DEDENT temp = se NEW_LINE DEDENT DEDENT reslis [ cc ] += 1 NEW_LINE reslis [ 0 ] = ( H - 2 ) * ( W - 2 ) - sum ( reslis ) NEW_LINE DEDENT for i in reslis : NEW_LINE INDENT print ( i ) NEW_LINE DEDENT
T366
import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; long h = in . nextLong ( ) , w = in . nextLong ( ) ; int n = in . nextInt ( ) ; Map < Long , Integer > map = new HashMap < > ( ) ; int [ ] dx = { 0 , 0 , 1 , 1 , 1 , 0 , - 1 , - 1 , - 1 } ; int [ ] dy = { 0 , - 1 , - 1 , 0 , 1 , 1 , 1 , 0 , - 1 } ; for ( int i = 0 ; i < n ; i ++ ) { int a = in . nextInt ( ) - 1 , b = in . nextInt ( ) - 1 ; for ( int k = 0 ; k < 9 ; k ++ ) { int y = a + dy [ k ] , x = b + dx [ k ] ; if ( x < 1 || w - 1 <= x || y < 1 || h - 1 <= y ) { continue ; } long key = ( long ) y << 32 | x ; Integer val = map . get ( key ) ; map . put ( key , val == null ? 1 : val + 1 ) ; } } System . out . println ( ( h - 2 ) * ( w - 2 ) - map . size ( ) ) ; long [ ] cnt = new long [ 10 ] ; for ( long key : map . keySet ( ) ) { cnt [ map . get ( key ) ] ++ ; } for ( int i = 1 ; i < 10 ; i ++ ) { System . out . println ( cnt [ i ] ) ; } } }
f = lambda : map ( int , input ( ) . split ( ) ) NEW_LINE h , w , n = f ( ) NEW_LINE c = [ ( h - 2 ) * ( w - 2 ) ] + [ 0 ] * 9 NEW_LINE d = { } NEW_LINE while n : NEW_LINE INDENT n -= 1 ; x , y = f ( ) NEW_LINE for i in range ( 9 ) : a = x + i % 3 , y + i // 3 ; g = h >= a [ 0 ] > 2 < a [ 1 ] <= w ; t = d [ a ] = d . get ( a , 0 ) + g ; c [ t - g ] -= 1 ; c [ t ] += 1 NEW_LINE DEDENT print ( * c ) NEW_LINE
T367
import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; long h = in . nextLong ( ) , w = in . nextLong ( ) ; int n = in . nextInt ( ) ; Map < Long , Integer > map = new HashMap < > ( ) ; int [ ] dx = { 0 , 0 , 1 , 1 , 1 , 0 , - 1 , - 1 , - 1 } ; int [ ] dy = { 0 , - 1 , - 1 , 0 , 1 , 1 , 1 , 0 , - 1 } ; for ( int i = 0 ; i < n ; i ++ ) { int a = in . nextInt ( ) - 1 , b = in . nextInt ( ) - 1 ; for ( int k = 0 ; k < 9 ; k ++ ) { int y = a + dy [ k ] , x = b + dx [ k ] ; if ( x < 1 || w - 1 <= x || y < 1 || h - 1 <= y ) { continue ; } long key = ( long ) y << 32 | x ; Integer val = map . get ( key ) ; map . put ( key , val == null ? 1 : val + 1 ) ; } } System . out . println ( ( h - 2 ) * ( w - 2 ) - map . size ( ) ) ; long [ ] cnt = new long [ 10 ] ; for ( long key : map . keySet ( ) ) { cnt [ map . get ( key ) ] ++ ; } for ( int i = 1 ; i < 10 ; i ++ ) { System . out . println ( cnt [ i ] ) ; } } }
from collections import defaultdict NEW_LINE def inpl ( ) : return [ int ( i ) for i in input ( ) . split ( ) ] NEW_LINE HW = defaultdict ( lambda : 0 ) NEW_LINE direction = [ ( 0 , 0 ) , ( 1 , 1 ) , ( 1 , 0 ) , ( 1 , - 1 ) , ( 0 , - 1 ) , ( - 1 , - 1 ) , ( - 1 , 0 ) , ( - 1 , 1 ) , ( 0 , 1 ) ] NEW_LINE H , W , N = inpl ( ) NEW_LINE for _ in range ( N ) : NEW_LINE INDENT a , b = inpl ( ) NEW_LINE for dh , dw in direction : NEW_LINE INDENT if 2 <= a + dh <= H - 1 and 2 <= b + dw <= W - 1 : NEW_LINE INDENT HW [ ( a + dh , b + dw ) ] += 1 NEW_LINE DEDENT DEDENT DEDENT ans = [ 0 for i in range ( 10 ) ] NEW_LINE print ( ( H - 2 ) * ( W - 2 ) - len ( HW ) ) NEW_LINE for j in HW . values ( ) : NEW_LINE INDENT ans [ j ] += 1 NEW_LINE DEDENT for i in ans [ 1 : ] : NEW_LINE INDENT print ( i ) NEW_LINE DEDENT
T368
import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; long h = in . nextLong ( ) , w = in . nextLong ( ) ; int n = in . nextInt ( ) ; Map < Long , Integer > map = new HashMap < > ( ) ; int [ ] dx = { 0 , 0 , 1 , 1 , 1 , 0 , - 1 , - 1 , - 1 } ; int [ ] dy = { 0 , - 1 , - 1 , 0 , 1 , 1 , 1 , 0 , - 1 } ; for ( int i = 0 ; i < n ; i ++ ) { int a = in . nextInt ( ) - 1 , b = in . nextInt ( ) - 1 ; for ( int k = 0 ; k < 9 ; k ++ ) { int y = a + dy [ k ] , x = b + dx [ k ] ; if ( x < 1 || w - 1 <= x || y < 1 || h - 1 <= y ) { continue ; } long key = ( long ) y << 32 | x ; Integer val = map . get ( key ) ; map . put ( key , val == null ? 1 : val + 1 ) ; } } System . out . println ( ( h - 2 ) * ( w - 2 ) - map . size ( ) ) ; long [ ] cnt = new long [ 10 ] ; for ( long key : map . keySet ( ) ) { cnt [ map . get ( key ) ] ++ ; } for ( int i = 1 ; i < 10 ; i ++ ) { System . out . println ( cnt [ i ] ) ; } } }
from collections import defaultdict NEW_LINE h , w , n = map ( int , input ( ) . split ( ) ) NEW_LINE abd = defaultdict ( int ) NEW_LINE for _ in range ( n ) : NEW_LINE INDENT a , b = map ( int , input ( ) . split ( ) ) NEW_LINE abd [ ( a , b ) ] += 1 NEW_LINE abd [ ( a , b - 1 ) ] += 1 NEW_LINE abd [ ( a , b - 2 ) ] += 1 NEW_LINE abd [ ( a - 1 , b ) ] += 1 NEW_LINE abd [ ( a - 1 , b - 1 ) ] += 1 NEW_LINE abd [ ( a - 1 , b - 2 ) ] += 1 NEW_LINE abd [ ( a - 2 , b ) ] += 1 NEW_LINE abd [ ( a - 2 , b - 1 ) ] += 1 NEW_LINE abd [ ( a - 2 , b - 2 ) ] += 1 NEW_LINE DEDENT suma = [ 0 for _ in range ( 10 ) ] NEW_LINE for a , b in abd . keys ( ) : NEW_LINE INDENT if 0 < a < h - 1 and 0 < b < w - 1 : NEW_LINE INDENT suma [ abd [ ( a , b ) ] ] += 1 NEW_LINE DEDENT DEDENT suma [ 0 ] = ( h - 2 ) * ( w - 2 ) - sum ( suma ) NEW_LINE print ( * suma , sep = ' \n ' ) NEW_LINE
T369
import java . util . * ; public class Main { public static void main ( String [ ] args ) { Scanner in = new Scanner ( System . in ) ; long h = in . nextLong ( ) , w = in . nextLong ( ) ; int n = in . nextInt ( ) ; Map < Long , Integer > map = new HashMap < > ( ) ; int [ ] dx = { 0 , 0 , 1 , 1 , 1 , 0 , - 1 , - 1 , - 1 } ; int [ ] dy = { 0 , - 1 , - 1 , 0 , 1 , 1 , 1 , 0 , - 1 } ; for ( int i = 0 ; i < n ; i ++ ) { int a = in . nextInt ( ) - 1 , b = in . nextInt ( ) - 1 ; for ( int k = 0 ; k < 9 ; k ++ ) { int y = a + dy [ k ] , x = b + dx [ k ] ; if ( x < 1 || w - 1 <= x || y < 1 || h - 1 <= y ) { continue ; } long key = ( long ) y << 32 | x ; Integer val = map . get ( key ) ; map . put ( key , val == null ? 1 : val + 1 ) ; } } System . out . println ( ( h - 2 ) * ( w - 2 ) - map . size ( ) ) ; long [ ] cnt = new long [ 10 ] ; for ( long key : map . keySet ( ) ) { cnt [ map . get ( key ) ] ++ ; } for ( int i = 1 ; i < 10 ; i ++ ) { System . out . println ( cnt [ i ] ) ; } } }
import sys , collections NEW_LINE def check_range ( y , x , H , W ) : NEW_LINE INDENT return 0 < x and x < W and 0 < y and y < H NEW_LINE DEDENT def solve ( ) : NEW_LINE INDENT H , W , N = map ( int , input ( ) . split ( ) ) NEW_LINE L = [ 0 for _ in range ( 10 ) ] NEW_LINE L [ 0 ] = ( H - 2 ) * ( W - 2 ) NEW_LINE NL = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT y , x = map ( int , input ( ) . split ( ) ) NEW_LINE x -= 1 NEW_LINE y -= 1 NEW_LINE for i in range ( - 1 , 2 ) : NEW_LINE INDENT for j in range ( - 1 , 2 ) : NEW_LINE INDENT if check_range ( y + i , x + j , H - 1 , W - 1 ) : NEW_LINE INDENT NL . append ( ( y + i , x + j ) ) NEW_LINE DEDENT DEDENT DEDENT DEDENT NLD = collections . Counter ( NL ) NEW_LINE for v in NLD . values ( ) : NEW_LINE INDENT L [ v ] += 1 NEW_LINE L [ 0 ] -= 1 NEW_LINE DEDENT for v in L : NEW_LINE INDENT print ( v ) NEW_LINE DEDENT DEDENT solve ( ) NEW_LINE
T370
import java . util . * ; class Point implements Comparable < Point > { int a , b ; Point ( int a , int b ) { this . a = a ; this . b = b ; } public int hashCode ( ) { return a ^ b ; } public boolean equals ( Object object ) { Point point = ( Point ) object ; return this . a == point . a && this . b == point . b ; } public int compareTo ( Point p ) { if ( this . a != p . a ) { return this . a - p . a ; } return this . b - p . b ; } } public class Main { public static void main ( String [ ] args ) { Scanner s = new Scanner ( System . in ) ; int h = s . nextInt ( ) ; int w = s . nextInt ( ) ; int n = s . nextInt ( ) ; TreeMap < Point , Integer > map = new TreeMap < Point , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int a = s . nextInt ( ) - 1 ; int b = s . nextInt ( ) - 1 ; for ( int da = Math . max ( 0 , a - 2 ) ; da <= Math . min ( h - 3 , a ) ; da ++ ) { for ( int db = Math . max ( 0 , b - 2 ) ; db <= Math . min ( w - 3 , b ) ; db ++ ) { Point p = new Point ( da , db ) ; if ( map . get ( p ) == null ) { map . put ( p , 1 ) ; } else { map . put ( p , map . get ( p ) + 1 ) ; } } } } long [ ] output = new long [ 10 ] ; output [ 0 ] = ( long ) ( h - 2 ) * ( long ) ( w - 2 ) ; for ( int value : map . values ( ) ) { output [ 0 ] -- ; output [ value ] ++ ; } for ( long value : output ) { System . out . println ( value ) ; } } }
import sys NEW_LINE input = sys . stdin . readline NEW_LINE sys . setrecursionlimit ( 10000 ) NEW_LINE H , W , N = ( int ( i ) for i in input ( ) . split ( ) ) NEW_LINE if N == 0 : NEW_LINE INDENT reslis = [ 0 ] * 10 NEW_LINE reslis [ 0 ] = ( H - 2 ) * ( W - 2 ) - sum ( reslis ) NEW_LINE DEDENT else : NEW_LINE INDENT L = [ ] NEW_LINE for k in range ( N ) : NEW_LINE INDENT a , b = ( int ( i ) for i in input ( ) . split ( ) ) NEW_LINE for i in range ( 3 ) : NEW_LINE INDENT for j in range ( 3 ) : NEW_LINE INDENT if 1 <= a - 2 + i <= H - 2 and 1 <= b - 2 + j <= W - 2 : NEW_LINE INDENT L . append ( ( a - 2 + i , b - 2 + j ) ) NEW_LINE DEDENT DEDENT DEDENT DEDENT L . sort ( ) NEW_LINE temp = None NEW_LINE cc = 1 NEW_LINE reslis = [ 0 ] * 10 NEW_LINE for se in L : NEW_LINE INDENT if temp == se : NEW_LINE INDENT cc += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if temp == None : NEW_LINE INDENT pass NEW_LINE DEDENT else : NEW_LINE INDENT reslis [ cc ] += 1 NEW_LINE cc = 1 NEW_LINE DEDENT temp = se NEW_LINE DEDENT DEDENT reslis [ cc ] += 1 NEW_LINE reslis [ 0 ] = ( H - 2 ) * ( W - 2 ) - sum ( reslis ) NEW_LINE DEDENT for i in reslis : NEW_LINE INDENT print ( i ) NEW_LINE DEDENT
T371
import java . util . * ; class Point implements Comparable < Point > { int a , b ; Point ( int a , int b ) { this . a = a ; this . b = b ; } public int hashCode ( ) { return a ^ b ; } public boolean equals ( Object object ) { Point point = ( Point ) object ; return this . a == point . a && this . b == point . b ; } public int compareTo ( Point p ) { if ( this . a != p . a ) { return this . a - p . a ; } return this . b - p . b ; } } public class Main { public static void main ( String [ ] args ) { Scanner s = new Scanner ( System . in ) ; int h = s . nextInt ( ) ; int w = s . nextInt ( ) ; int n = s . nextInt ( ) ; TreeMap < Point , Integer > map = new TreeMap < Point , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int a = s . nextInt ( ) - 1 ; int b = s . nextInt ( ) - 1 ; for ( int da = Math . max ( 0 , a - 2 ) ; da <= Math . min ( h - 3 , a ) ; da ++ ) { for ( int db = Math . max ( 0 , b - 2 ) ; db <= Math . min ( w - 3 , b ) ; db ++ ) { Point p = new Point ( da , db ) ; if ( map . get ( p ) == null ) { map . put ( p , 1 ) ; } else { map . put ( p , map . get ( p ) + 1 ) ; } } } } long [ ] output = new long [ 10 ] ; output [ 0 ] = ( long ) ( h - 2 ) * ( long ) ( w - 2 ) ; for ( int value : map . values ( ) ) { output [ 0 ] -- ; output [ value ] ++ ; } for ( long value : output ) { System . out . println ( value ) ; } } }
f = lambda : map ( int , input ( ) . split ( ) ) NEW_LINE h , w , n = f ( ) NEW_LINE c = [ ( h - 2 ) * ( w - 2 ) ] + [ 0 ] * 9 NEW_LINE d = { } NEW_LINE while n : NEW_LINE INDENT n -= 1 ; x , y = f ( ) NEW_LINE for i in range ( 9 ) : a = x + i % 3 , y + i // 3 ; g = h >= a [ 0 ] > 2 < a [ 1 ] <= w ; t = d [ a ] = d . get ( a , 0 ) + g ; c [ t - g ] -= 1 ; c [ t ] += 1 NEW_LINE DEDENT print ( * c ) NEW_LINE
T372
import java . util . * ; class Point implements Comparable < Point > { int a , b ; Point ( int a , int b ) { this . a = a ; this . b = b ; } public int hashCode ( ) { return a ^ b ; } public boolean equals ( Object object ) { Point point = ( Point ) object ; return this . a == point . a && this . b == point . b ; } public int compareTo ( Point p ) { if ( this . a != p . a ) { return this . a - p . a ; } return this . b - p . b ; } } public class Main { public static void main ( String [ ] args ) { Scanner s = new Scanner ( System . in ) ; int h = s . nextInt ( ) ; int w = s . nextInt ( ) ; int n = s . nextInt ( ) ; TreeMap < Point , Integer > map = new TreeMap < Point , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int a = s . nextInt ( ) - 1 ; int b = s . nextInt ( ) - 1 ; for ( int da = Math . max ( 0 , a - 2 ) ; da <= Math . min ( h - 3 , a ) ; da ++ ) { for ( int db = Math . max ( 0 , b - 2 ) ; db <= Math . min ( w - 3 , b ) ; db ++ ) { Point p = new Point ( da , db ) ; if ( map . get ( p ) == null ) { map . put ( p , 1 ) ; } else { map . put ( p , map . get ( p ) + 1 ) ; } } } } long [ ] output = new long [ 10 ] ; output [ 0 ] = ( long ) ( h - 2 ) * ( long ) ( w - 2 ) ; for ( int value : map . values ( ) ) { output [ 0 ] -- ; output [ value ] ++ ; } for ( long value : output ) { System . out . println ( value ) ; } } }
from collections import defaultdict NEW_LINE def inpl ( ) : return [ int ( i ) for i in input ( ) . split ( ) ] NEW_LINE HW = defaultdict ( lambda : 0 ) NEW_LINE direction = [ ( 0 , 0 ) , ( 1 , 1 ) , ( 1 , 0 ) , ( 1 , - 1 ) , ( 0 , - 1 ) , ( - 1 , - 1 ) , ( - 1 , 0 ) , ( - 1 , 1 ) , ( 0 , 1 ) ] NEW_LINE H , W , N = inpl ( ) NEW_LINE for _ in range ( N ) : NEW_LINE INDENT a , b = inpl ( ) NEW_LINE for dh , dw in direction : NEW_LINE INDENT if 2 <= a + dh <= H - 1 and 2 <= b + dw <= W - 1 : NEW_LINE INDENT HW [ ( a + dh , b + dw ) ] += 1 NEW_LINE DEDENT DEDENT DEDENT ans = [ 0 for i in range ( 10 ) ] NEW_LINE print ( ( H - 2 ) * ( W - 2 ) - len ( HW ) ) NEW_LINE for j in HW . values ( ) : NEW_LINE INDENT ans [ j ] += 1 NEW_LINE DEDENT for i in ans [ 1 : ] : NEW_LINE INDENT print ( i ) NEW_LINE DEDENT
T373
import java . util . * ; class Point implements Comparable < Point > { int a , b ; Point ( int a , int b ) { this . a = a ; this . b = b ; } public int hashCode ( ) { return a ^ b ; } public boolean equals ( Object object ) { Point point = ( Point ) object ; return this . a == point . a && this . b == point . b ; } public int compareTo ( Point p ) { if ( this . a != p . a ) { return this . a - p . a ; } return this . b - p . b ; } } public class Main { public static void main ( String [ ] args ) { Scanner s = new Scanner ( System . in ) ; int h = s . nextInt ( ) ; int w = s . nextInt ( ) ; int n = s . nextInt ( ) ; TreeMap < Point , Integer > map = new TreeMap < Point , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int a = s . nextInt ( ) - 1 ; int b = s . nextInt ( ) - 1 ; for ( int da = Math . max ( 0 , a - 2 ) ; da <= Math . min ( h - 3 , a ) ; da ++ ) { for ( int db = Math . max ( 0 , b - 2 ) ; db <= Math . min ( w - 3 , b ) ; db ++ ) { Point p = new Point ( da , db ) ; if ( map . get ( p ) == null ) { map . put ( p , 1 ) ; } else { map . put ( p , map . get ( p ) + 1 ) ; } } } } long [ ] output = new long [ 10 ] ; output [ 0 ] = ( long ) ( h - 2 ) * ( long ) ( w - 2 ) ; for ( int value : map . values ( ) ) { output [ 0 ] -- ; output [ value ] ++ ; } for ( long value : output ) { System . out . println ( value ) ; } } }
from collections import defaultdict NEW_LINE h , w , n = map ( int , input ( ) . split ( ) ) NEW_LINE abd = defaultdict ( int ) NEW_LINE for _ in range ( n ) : NEW_LINE INDENT a , b = map ( int , input ( ) . split ( ) ) NEW_LINE abd [ ( a , b ) ] += 1 NEW_LINE abd [ ( a , b - 1 ) ] += 1 NEW_LINE abd [ ( a , b - 2 ) ] += 1 NEW_LINE abd [ ( a - 1 , b ) ] += 1 NEW_LINE abd [ ( a - 1 , b - 1 ) ] += 1 NEW_LINE abd [ ( a - 1 , b - 2 ) ] += 1 NEW_LINE abd [ ( a - 2 , b ) ] += 1 NEW_LINE abd [ ( a - 2 , b - 1 ) ] += 1 NEW_LINE abd [ ( a - 2 , b - 2 ) ] += 1 NEW_LINE DEDENT suma = [ 0 for _ in range ( 10 ) ] NEW_LINE for a , b in abd . keys ( ) : NEW_LINE INDENT if 0 < a < h - 1 and 0 < b < w - 1 : NEW_LINE INDENT suma [ abd [ ( a , b ) ] ] += 1 NEW_LINE DEDENT DEDENT suma [ 0 ] = ( h - 2 ) * ( w - 2 ) - sum ( suma ) NEW_LINE print ( * suma , sep = ' \n ' ) NEW_LINE
T374
import java . util . * ; class Point implements Comparable < Point > { int a , b ; Point ( int a , int b ) { this . a = a ; this . b = b ; } public int hashCode ( ) { return a ^ b ; } public boolean equals ( Object object ) { Point point = ( Point ) object ; return this . a == point . a && this . b == point . b ; } public int compareTo ( Point p ) { if ( this . a != p . a ) { return this . a - p . a ; } return this . b - p . b ; } } public class Main { public static void main ( String [ ] args ) { Scanner s = new Scanner ( System . in ) ; int h = s . nextInt ( ) ; int w = s . nextInt ( ) ; int n = s . nextInt ( ) ; TreeMap < Point , Integer > map = new TreeMap < Point , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int a = s . nextInt ( ) - 1 ; int b = s . nextInt ( ) - 1 ; for ( int da = Math . max ( 0 , a - 2 ) ; da <= Math . min ( h - 3 , a ) ; da ++ ) { for ( int db = Math . max ( 0 , b - 2 ) ; db <= Math . min ( w - 3 , b ) ; db ++ ) { Point p = new Point ( da , db ) ; if ( map . get ( p ) == null ) { map . put ( p , 1 ) ; } else { map . put ( p , map . get ( p ) + 1 ) ; } } } } long [ ] output = new long [ 10 ] ; output [ 0 ] = ( long ) ( h - 2 ) * ( long ) ( w - 2 ) ; for ( int value : map . values ( ) ) { output [ 0 ] -- ; output [ value ] ++ ; } for ( long value : output ) { System . out . println ( value ) ; } } }
import sys , collections NEW_LINE def check_range ( y , x , H , W ) : NEW_LINE INDENT return 0 < x and x < W and 0 < y and y < H NEW_LINE DEDENT def solve ( ) : NEW_LINE INDENT H , W , N = map ( int , input ( ) . split ( ) ) NEW_LINE L = [ 0 for _ in range ( 10 ) ] NEW_LINE L [ 0 ] = ( H - 2 ) * ( W - 2 ) NEW_LINE NL = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT y , x = map ( int , input ( ) . split ( ) ) NEW_LINE x -= 1 NEW_LINE y -= 1 NEW_LINE for i in range ( - 1 , 2 ) : NEW_LINE INDENT for j in range ( - 1 , 2 ) : NEW_LINE INDENT if check_range ( y + i , x + j , H - 1 , W - 1 ) : NEW_LINE INDENT NL . append ( ( y + i , x + j ) ) NEW_LINE DEDENT DEDENT DEDENT DEDENT NLD = collections . Counter ( NL ) NEW_LINE for v in NLD . values ( ) : NEW_LINE INDENT L [ v ] += 1 NEW_LINE L [ 0 ] -= 1 NEW_LINE DEDENT for v in L : NEW_LINE INDENT print ( v ) NEW_LINE DEDENT DEDENT solve ( ) NEW_LINE
T375
import java . util . HashMap ; import java . util . Scanner ; import java . awt . Point ; public class Main { public static void main ( String [ ] args ) { new Main ( ) . solve ( ) ; } public void solve ( ) { Scanner scanner = new Scanner ( System . in ) ; long H = scanner . nextLong ( ) ; long W = scanner . nextLong ( ) ; int N = scanner . nextInt ( ) ; Point [ ] point = new Point [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { int row = scanner . nextInt ( ) ; int col = scanner . nextInt ( ) ; point [ i ] = new Point ( col , row ) ; } HashMap < Point , Integer > hashmap = new HashMap < Point , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int y = - 1 ; y < 2 ; y ++ ) { for ( int x = - 1 ; x < 2 ; x ++ ) { Point p = new Point ( point [ i ] . x + x , point [ i ] . y + y ) ; if ( hashmap . get ( p ) == null ) { hashmap . put ( p , 1 ) ; } else { hashmap . put ( p , hashmap . get ( p ) + 1 ) ; } } } } long [ ] counter = new long [ 10 ] ; for ( int i = 0 ; i < counter . length ; i ++ ) { counter [ i ] = 0 ; } long sum = ( H - 2 ) * ( W - 2 ) ; for ( HashMap . Entry < Point , Integer > map : hashmap . entrySet ( ) ) { if ( 1 < map . getKey ( ) . x && map . getKey ( ) . x < W && 1 < map . getKey ( ) . y && map . getKey ( ) . y < H ) { counter [ map . getValue ( ) ] ++ ; sum -- ; } } counter [ 0 ] = sum ; for ( int i = 0 ; i < counter . length ; i ++ ) { System . out . println ( counter [ i ] ) ; } } }
import sys NEW_LINE input = sys . stdin . readline NEW_LINE sys . setrecursionlimit ( 10000 ) NEW_LINE H , W , N = ( int ( i ) for i in input ( ) . split ( ) ) NEW_LINE if N == 0 : NEW_LINE INDENT reslis = [ 0 ] * 10 NEW_LINE reslis [ 0 ] = ( H - 2 ) * ( W - 2 ) - sum ( reslis ) NEW_LINE DEDENT else : NEW_LINE INDENT L = [ ] NEW_LINE for k in range ( N ) : NEW_LINE INDENT a , b = ( int ( i ) for i in input ( ) . split ( ) ) NEW_LINE for i in range ( 3 ) : NEW_LINE INDENT for j in range ( 3 ) : NEW_LINE INDENT if 1 <= a - 2 + i <= H - 2 and 1 <= b - 2 + j <= W - 2 : NEW_LINE INDENT L . append ( ( a - 2 + i , b - 2 + j ) ) NEW_LINE DEDENT DEDENT DEDENT DEDENT L . sort ( ) NEW_LINE temp = None NEW_LINE cc = 1 NEW_LINE reslis = [ 0 ] * 10 NEW_LINE for se in L : NEW_LINE INDENT if temp == se : NEW_LINE INDENT cc += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if temp == None : NEW_LINE INDENT pass NEW_LINE DEDENT else : NEW_LINE INDENT reslis [ cc ] += 1 NEW_LINE cc = 1 NEW_LINE DEDENT temp = se NEW_LINE DEDENT DEDENT reslis [ cc ] += 1 NEW_LINE reslis [ 0 ] = ( H - 2 ) * ( W - 2 ) - sum ( reslis ) NEW_LINE DEDENT for i in reslis : NEW_LINE INDENT print ( i ) NEW_LINE DEDENT
T376
import java . util . HashMap ; import java . util . Scanner ; import java . awt . Point ; public class Main { public static void main ( String [ ] args ) { new Main ( ) . solve ( ) ; } public void solve ( ) { Scanner scanner = new Scanner ( System . in ) ; long H = scanner . nextLong ( ) ; long W = scanner . nextLong ( ) ; int N = scanner . nextInt ( ) ; Point [ ] point = new Point [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { int row = scanner . nextInt ( ) ; int col = scanner . nextInt ( ) ; point [ i ] = new Point ( col , row ) ; } HashMap < Point , Integer > hashmap = new HashMap < Point , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int y = - 1 ; y < 2 ; y ++ ) { for ( int x = - 1 ; x < 2 ; x ++ ) { Point p = new Point ( point [ i ] . x + x , point [ i ] . y + y ) ; if ( hashmap . get ( p ) == null ) { hashmap . put ( p , 1 ) ; } else { hashmap . put ( p , hashmap . get ( p ) + 1 ) ; } } } } long [ ] counter = new long [ 10 ] ; for ( int i = 0 ; i < counter . length ; i ++ ) { counter [ i ] = 0 ; } long sum = ( H - 2 ) * ( W - 2 ) ; for ( HashMap . Entry < Point , Integer > map : hashmap . entrySet ( ) ) { if ( 1 < map . getKey ( ) . x && map . getKey ( ) . x < W && 1 < map . getKey ( ) . y && map . getKey ( ) . y < H ) { counter [ map . getValue ( ) ] ++ ; sum -- ; } } counter [ 0 ] = sum ; for ( int i = 0 ; i < counter . length ; i ++ ) { System . out . println ( counter [ i ] ) ; } } }
f = lambda : map ( int , input ( ) . split ( ) ) NEW_LINE h , w , n = f ( ) NEW_LINE c = [ ( h - 2 ) * ( w - 2 ) ] + [ 0 ] * 9 NEW_LINE d = { } NEW_LINE while n : NEW_LINE INDENT n -= 1 ; x , y = f ( ) NEW_LINE for i in range ( 9 ) : a = x + i % 3 , y + i // 3 ; g = h >= a [ 0 ] > 2 < a [ 1 ] <= w ; t = d [ a ] = d . get ( a , 0 ) + g ; c [ t - g ] -= 1 ; c [ t ] += 1 NEW_LINE DEDENT print ( * c ) NEW_LINE
T377
import java . util . HashMap ; import java . util . Scanner ; import java . awt . Point ; public class Main { public static void main ( String [ ] args ) { new Main ( ) . solve ( ) ; } public void solve ( ) { Scanner scanner = new Scanner ( System . in ) ; long H = scanner . nextLong ( ) ; long W = scanner . nextLong ( ) ; int N = scanner . nextInt ( ) ; Point [ ] point = new Point [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { int row = scanner . nextInt ( ) ; int col = scanner . nextInt ( ) ; point [ i ] = new Point ( col , row ) ; } HashMap < Point , Integer > hashmap = new HashMap < Point , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int y = - 1 ; y < 2 ; y ++ ) { for ( int x = - 1 ; x < 2 ; x ++ ) { Point p = new Point ( point [ i ] . x + x , point [ i ] . y + y ) ; if ( hashmap . get ( p ) == null ) { hashmap . put ( p , 1 ) ; } else { hashmap . put ( p , hashmap . get ( p ) + 1 ) ; } } } } long [ ] counter = new long [ 10 ] ; for ( int i = 0 ; i < counter . length ; i ++ ) { counter [ i ] = 0 ; } long sum = ( H - 2 ) * ( W - 2 ) ; for ( HashMap . Entry < Point , Integer > map : hashmap . entrySet ( ) ) { if ( 1 < map . getKey ( ) . x && map . getKey ( ) . x < W && 1 < map . getKey ( ) . y && map . getKey ( ) . y < H ) { counter [ map . getValue ( ) ] ++ ; sum -- ; } } counter [ 0 ] = sum ; for ( int i = 0 ; i < counter . length ; i ++ ) { System . out . println ( counter [ i ] ) ; } } }
from collections import defaultdict NEW_LINE def inpl ( ) : return [ int ( i ) for i in input ( ) . split ( ) ] NEW_LINE HW = defaultdict ( lambda : 0 ) NEW_LINE direction = [ ( 0 , 0 ) , ( 1 , 1 ) , ( 1 , 0 ) , ( 1 , - 1 ) , ( 0 , - 1 ) , ( - 1 , - 1 ) , ( - 1 , 0 ) , ( - 1 , 1 ) , ( 0 , 1 ) ] NEW_LINE H , W , N = inpl ( ) NEW_LINE for _ in range ( N ) : NEW_LINE INDENT a , b = inpl ( ) NEW_LINE for dh , dw in direction : NEW_LINE INDENT if 2 <= a + dh <= H - 1 and 2 <= b + dw <= W - 1 : NEW_LINE INDENT HW [ ( a + dh , b + dw ) ] += 1 NEW_LINE DEDENT DEDENT DEDENT ans = [ 0 for i in range ( 10 ) ] NEW_LINE print ( ( H - 2 ) * ( W - 2 ) - len ( HW ) ) NEW_LINE for j in HW . values ( ) : NEW_LINE INDENT ans [ j ] += 1 NEW_LINE DEDENT for i in ans [ 1 : ] : NEW_LINE INDENT print ( i ) NEW_LINE DEDENT
T378
import java . util . HashMap ; import java . util . Scanner ; import java . awt . Point ; public class Main { public static void main ( String [ ] args ) { new Main ( ) . solve ( ) ; } public void solve ( ) { Scanner scanner = new Scanner ( System . in ) ; long H = scanner . nextLong ( ) ; long W = scanner . nextLong ( ) ; int N = scanner . nextInt ( ) ; Point [ ] point = new Point [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { int row = scanner . nextInt ( ) ; int col = scanner . nextInt ( ) ; point [ i ] = new Point ( col , row ) ; } HashMap < Point , Integer > hashmap = new HashMap < Point , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int y = - 1 ; y < 2 ; y ++ ) { for ( int x = - 1 ; x < 2 ; x ++ ) { Point p = new Point ( point [ i ] . x + x , point [ i ] . y + y ) ; if ( hashmap . get ( p ) == null ) { hashmap . put ( p , 1 ) ; } else { hashmap . put ( p , hashmap . get ( p ) + 1 ) ; } } } } long [ ] counter = new long [ 10 ] ; for ( int i = 0 ; i < counter . length ; i ++ ) { counter [ i ] = 0 ; } long sum = ( H - 2 ) * ( W - 2 ) ; for ( HashMap . Entry < Point , Integer > map : hashmap . entrySet ( ) ) { if ( 1 < map . getKey ( ) . x && map . getKey ( ) . x < W && 1 < map . getKey ( ) . y && map . getKey ( ) . y < H ) { counter [ map . getValue ( ) ] ++ ; sum -- ; } } counter [ 0 ] = sum ; for ( int i = 0 ; i < counter . length ; i ++ ) { System . out . println ( counter [ i ] ) ; } } }
from collections import defaultdict NEW_LINE h , w , n = map ( int , input ( ) . split ( ) ) NEW_LINE abd = defaultdict ( int ) NEW_LINE for _ in range ( n ) : NEW_LINE INDENT a , b = map ( int , input ( ) . split ( ) ) NEW_LINE abd [ ( a , b ) ] += 1 NEW_LINE abd [ ( a , b - 1 ) ] += 1 NEW_LINE abd [ ( a , b - 2 ) ] += 1 NEW_LINE abd [ ( a - 1 , b ) ] += 1 NEW_LINE abd [ ( a - 1 , b - 1 ) ] += 1 NEW_LINE abd [ ( a - 1 , b - 2 ) ] += 1 NEW_LINE abd [ ( a - 2 , b ) ] += 1 NEW_LINE abd [ ( a - 2 , b - 1 ) ] += 1 NEW_LINE abd [ ( a - 2 , b - 2 ) ] += 1 NEW_LINE DEDENT suma = [ 0 for _ in range ( 10 ) ] NEW_LINE for a , b in abd . keys ( ) : NEW_LINE INDENT if 0 < a < h - 1 and 0 < b < w - 1 : NEW_LINE INDENT suma [ abd [ ( a , b ) ] ] += 1 NEW_LINE DEDENT DEDENT suma [ 0 ] = ( h - 2 ) * ( w - 2 ) - sum ( suma ) NEW_LINE print ( * suma , sep = ' \n ' ) NEW_LINE
T379
import java . util . HashMap ; import java . util . Scanner ; import java . awt . Point ; public class Main { public static void main ( String [ ] args ) { new Main ( ) . solve ( ) ; } public void solve ( ) { Scanner scanner = new Scanner ( System . in ) ; long H = scanner . nextLong ( ) ; long W = scanner . nextLong ( ) ; int N = scanner . nextInt ( ) ; Point [ ] point = new Point [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { int row = scanner . nextInt ( ) ; int col = scanner . nextInt ( ) ; point [ i ] = new Point ( col , row ) ; } HashMap < Point , Integer > hashmap = new HashMap < Point , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int y = - 1 ; y < 2 ; y ++ ) { for ( int x = - 1 ; x < 2 ; x ++ ) { Point p = new Point ( point [ i ] . x + x , point [ i ] . y + y ) ; if ( hashmap . get ( p ) == null ) { hashmap . put ( p , 1 ) ; } else { hashmap . put ( p , hashmap . get ( p ) + 1 ) ; } } } } long [ ] counter = new long [ 10 ] ; for ( int i = 0 ; i < counter . length ; i ++ ) { counter [ i ] = 0 ; } long sum = ( H - 2 ) * ( W - 2 ) ; for ( HashMap . Entry < Point , Integer > map : hashmap . entrySet ( ) ) { if ( 1 < map . getKey ( ) . x && map . getKey ( ) . x < W && 1 < map . getKey ( ) . y && map . getKey ( ) . y < H ) { counter [ map . getValue ( ) ] ++ ; sum -- ; } } counter [ 0 ] = sum ; for ( int i = 0 ; i < counter . length ; i ++ ) { System . out . println ( counter [ i ] ) ; } } }
import sys , collections NEW_LINE def check_range ( y , x , H , W ) : NEW_LINE INDENT return 0 < x and x < W and 0 < y and y < H NEW_LINE DEDENT def solve ( ) : NEW_LINE INDENT H , W , N = map ( int , input ( ) . split ( ) ) NEW_LINE L = [ 0 for _ in range ( 10 ) ] NEW_LINE L [ 0 ] = ( H - 2 ) * ( W - 2 ) NEW_LINE NL = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT y , x = map ( int , input ( ) . split ( ) ) NEW_LINE x -= 1 NEW_LINE y -= 1 NEW_LINE for i in range ( - 1 , 2 ) : NEW_LINE INDENT for j in range ( - 1 , 2 ) : NEW_LINE INDENT if check_range ( y + i , x + j , H - 1 , W - 1 ) : NEW_LINE INDENT NL . append ( ( y + i , x + j ) ) NEW_LINE DEDENT DEDENT DEDENT DEDENT NLD = collections . Counter ( NL ) NEW_LINE for v in NLD . values ( ) : NEW_LINE INDENT L [ v ] += 1 NEW_LINE L [ 0 ] -= 1 NEW_LINE DEDENT for v in L : NEW_LINE INDENT print ( v ) NEW_LINE DEDENT DEDENT solve ( ) NEW_LINE
T380
import java . util . * ; class Main { static ArrayList < Integer > [ ] map ; public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int x = sc . nextInt ( ) ; if ( x == 1 ) { System . out . println ( n - 2 ) ; System . exit ( 0 ) ; } map = new ArrayList [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) map [ i ] = new ArrayList < > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; map [ a ] . add ( b ) ; map [ b ] . add ( a ) ; } long ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int first = 1 , second = 0 ; for ( int w : map [ i ] ) { int [ ] cur = dfs ( w , i , x / 2 - 1 ) ; first += cur [ 0 ] ; second = Math . max ( second , cur [ 1 ] ) ; } if ( x % 2 == 1 ) first += second ; ans = Math . max ( ans , first ) ; } System . out . println ( n - ans ) ; } static int [ ] dfs ( int root , int from , int level ) { if ( level == 0 ) return new int [ ] { 1 , map [ root ] . size ( ) - 1 } ; int first = 1 , second = 0 ; for ( int w : map [ root ] ) { if ( w == from ) continue ; int [ ] cur = dfs ( w , root , level - 1 ) ; first += cur [ 0 ] ; second += cur [ 1 ] ; } return new int [ ] { first , second } ; } } Note : . / Main . java uses unchecked or unsafe operations . Note : Recompile with - Xlint : unchecked for details .
from collections import deque NEW_LINE n , k = ( int ( i ) for i in input ( ) . split ( ) ) NEW_LINE b = [ [ int ( i ) - 1 for i in input ( ) . split ( ) ] for i in range ( n - 1 ) ] NEW_LINE x , d , ans , c = [ [ ] for i in range ( n ) ] , [ [ ] for i in range ( n ) ] , n , 0 NEW_LINE for i , j in b : NEW_LINE INDENT x [ i ] . append ( j ) NEW_LINE x [ j ] . append ( i ) NEW_LINE DEDENT def f ( s ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT q , v = deque ( ) , [ 1 ] * n NEW_LINE v [ i ] = 0 NEW_LINE for j in x [ i ] : NEW_LINE INDENT q . append ( ( j , 1 , j ) ) NEW_LINE d [ i ] . append ( j ) NEW_LINE v [ j ] = 0 NEW_LINE DEDENT while q : NEW_LINE INDENT p = q . pop ( ) NEW_LINE if p [ 1 ] < s : NEW_LINE INDENT for j in x [ p [ 0 ] ] : NEW_LINE INDENT if v [ j ] : NEW_LINE INDENT q . append ( ( j , p [ 1 ] + 1 , p [ 2 ] ) ) NEW_LINE d [ i ] . append ( p [ 2 ] ) NEW_LINE v [ j ] = 0 NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT DEDENT if k > n // 2 : NEW_LINE INDENT for i in x : c = max ( len ( i ) , c ) NEW_LINE DEDENT if n - c + 1 <= k : ans = 0 NEW_LINE elif k == 1 : ans = n - 2 NEW_LINE else : NEW_LINE INDENT f ( k // 2 ) NEW_LINE if k % 2 : NEW_LINE INDENT for i , j in b : ans = min ( ans , n - len ( d [ i ] ) - len ( d [ j ] ) + d [ i ] . count ( j ) + d [ j ] . count ( i ) - 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( n ) : ans = min ( ans , n - len ( d [ i ] ) - 1 ) NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE
T381
import java . util . * ; class Main { static ArrayList < Integer > [ ] map ; public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int x = sc . nextInt ( ) ; if ( x == 1 ) { System . out . println ( n - 2 ) ; System . exit ( 0 ) ; } map = new ArrayList [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) map [ i ] = new ArrayList < > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; map [ a ] . add ( b ) ; map [ b ] . add ( a ) ; } long ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int first = 1 , second = 0 ; for ( int w : map [ i ] ) { int [ ] cur = dfs ( w , i , x / 2 - 1 ) ; first += cur [ 0 ] ; second = Math . max ( second , cur [ 1 ] ) ; } if ( x % 2 == 1 ) first += second ; ans = Math . max ( ans , first ) ; } System . out . println ( n - ans ) ; } static int [ ] dfs ( int root , int from , int level ) { if ( level == 0 ) return new int [ ] { 1 , map [ root ] . size ( ) - 1 } ; int first = 1 , second = 0 ; for ( int w : map [ root ] ) { if ( w == from ) continue ; int [ ] cur = dfs ( w , root , level - 1 ) ; first += cur [ 0 ] ; second += cur [ 1 ] ; } return new int [ ] { first , second } ; } } Note : . / Main . java uses unchecked or unsafe operations . Note : Recompile with - Xlint : unchecked for details .
n , k = map ( int , input ( ) . split ( ) ) NEW_LINE import sys NEW_LINE sys . setrecursionlimit ( 2000 ) NEW_LINE l = [ [ ] for i in range ( n ) ] NEW_LINE e = [ ] NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT a , b = map ( int , input ( ) . split ( ) ) NEW_LINE l [ a - 1 ] . append ( b - 1 ) NEW_LINE l [ b - 1 ] . append ( a - 1 ) NEW_LINE e . append ( [ a - 1 , b - 1 ] ) NEW_LINE DEDENT def first_search ( first , depth ) : NEW_LINE INDENT record = 0 NEW_LINE for i in l [ first ] : NEW_LINE INDENT temp = search ( first , i , depth - 1 ) NEW_LINE if record < temp : NEW_LINE INDENT record = temp NEW_LINE DEDENT DEDENT return record + 1 NEW_LINE DEDENT def search ( before , now , depth ) : NEW_LINE INDENT if depth <= 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans = 1 NEW_LINE for i in l [ now ] : NEW_LINE INDENT if before != i : NEW_LINE INDENT ans += search ( now , i , depth - 1 ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT DEDENT ret = 0 NEW_LINE if k % 2 == 0 : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT temp = search ( - 1 , i , k // 2 ) NEW_LINE if temp > ret : NEW_LINE INDENT ret = temp NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT for i in e : NEW_LINE INDENT temp = search ( i [ 0 ] , i [ 1 ] , k // 2 ) + search ( i [ 1 ] , i [ 0 ] , k // 2 ) NEW_LINE if temp > ret : NEW_LINE INDENT ret = temp NEW_LINE DEDENT DEDENT DEDENT print ( n - ret ) NEW_LINE
T382
import java . util . * ; class Main { static ArrayList < Integer > [ ] map ; public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int x = sc . nextInt ( ) ; if ( x == 1 ) { System . out . println ( n - 2 ) ; System . exit ( 0 ) ; } map = new ArrayList [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) map [ i ] = new ArrayList < > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; map [ a ] . add ( b ) ; map [ b ] . add ( a ) ; } long ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int first = 1 , second = 0 ; for ( int w : map [ i ] ) { int [ ] cur = dfs ( w , i , x / 2 - 1 ) ; first += cur [ 0 ] ; second = Math . max ( second , cur [ 1 ] ) ; } if ( x % 2 == 1 ) first += second ; ans = Math . max ( ans , first ) ; } System . out . println ( n - ans ) ; } static int [ ] dfs ( int root , int from , int level ) { if ( level == 0 ) return new int [ ] { 1 , map [ root ] . size ( ) - 1 } ; int first = 1 , second = 0 ; for ( int w : map [ root ] ) { if ( w == from ) continue ; int [ ] cur = dfs ( w , root , level - 1 ) ; first += cur [ 0 ] ; second += cur [ 1 ] ; } return new int [ ] { first , second } ; } } Note : . / Main . java uses unchecked or unsafe operations . Note : Recompile with - Xlint : unchecked for details .
import sys NEW_LINE from collections import defaultdict , deque NEW_LINE INF = float ( ' inf ' ) NEW_LINE MOD = 10 ** 9 + 7 NEW_LINE def LI ( ) : return [ int ( x ) for x in sys . stdin . readline ( ) . split ( ) ] NEW_LINE def LI_ ( ) : return [ int ( x ) - 1 for x in sys . stdin . readline ( ) . split ( ) ] NEW_LINE def LF ( ) : return [ float ( x ) for x in sys . stdin . readline ( ) . split ( ) ] NEW_LINE def LS ( ) : return sys . stdin . readline ( ) . split ( ) NEW_LINE def II ( ) : return int ( sys . stdin . readline ( ) ) NEW_LINE def SI ( ) : return input ( ) NEW_LINE def main ( ) : NEW_LINE INDENT n , k = LI ( ) NEW_LINE G = defaultdict ( list ) NEW_LINE AB = [ LI_ ( ) for _ in range ( n - 1 ) ] NEW_LINE for a , b in AB : NEW_LINE INDENT G [ a ] . append ( b ) NEW_LINE G [ b ] . append ( a ) NEW_LINE DEDENT def DFS ( ss : list , t : int ) : NEW_LINE INDENT q = deque ( ) NEW_LINE for s in ss : NEW_LINE INDENT q . append ( ( s , 0 ) ) NEW_LINE DEDENT visited = [ 0 ] * n NEW_LINE visited [ ss [ 0 ] ] = visited [ ss [ - 1 ] ] = 1 NEW_LINE while q : NEW_LINE INDENT v , d = q . pop ( ) NEW_LINE if d >= t : NEW_LINE INDENT continue NEW_LINE DEDENT for to in G [ v ] : NEW_LINE INDENT if visited [ to ] : NEW_LINE INDENT continue NEW_LINE DEDENT visited [ to ] = 1 NEW_LINE q . append ( ( to , d + 1 ) ) NEW_LINE DEDENT DEDENT return n - sum ( visited ) NEW_LINE DEDENT res = INF NEW_LINE if k % 2 : NEW_LINE INDENT for a , b in AB : NEW_LINE INDENT res = min ( res , DFS ( [ a , b ] , ( k - 1 ) // 2 ) ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT res = min ( res , DFS ( [ i ] , k // 2 ) ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT print ( main ( ) ) NEW_LINE
T383
import java . util . * ; class Main { static ArrayList < Integer > [ ] map ; public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int x = sc . nextInt ( ) ; if ( x == 1 ) { System . out . println ( n - 2 ) ; System . exit ( 0 ) ; } map = new ArrayList [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) map [ i ] = new ArrayList < > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; map [ a ] . add ( b ) ; map [ b ] . add ( a ) ; } long ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int first = 1 , second = 0 ; for ( int w : map [ i ] ) { int [ ] cur = dfs ( w , i , x / 2 - 1 ) ; first += cur [ 0 ] ; second = Math . max ( second , cur [ 1 ] ) ; } if ( x % 2 == 1 ) first += second ; ans = Math . max ( ans , first ) ; } System . out . println ( n - ans ) ; } static int [ ] dfs ( int root , int from , int level ) { if ( level == 0 ) return new int [ ] { 1 , map [ root ] . size ( ) - 1 } ; int first = 1 , second = 0 ; for ( int w : map [ root ] ) { if ( w == from ) continue ; int [ ] cur = dfs ( w , root , level - 1 ) ; first += cur [ 0 ] ; second += cur [ 1 ] ; } return new int [ ] { first , second } ; } } Note : . / Main . java uses unchecked or unsafe operations . Note : Recompile with - Xlint : unchecked for details .
import copy NEW_LINE N , K = ( int ( i ) for i in input ( ) . split ( ) ) NEW_LINE G = [ [ ] for i in range ( N ) ] NEW_LINE E = [ ] NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT A , B = ( int ( i ) for i in input ( ) . split ( ) ) NEW_LINE E . append ( ( A - 1 , B - 1 ) ) NEW_LINE G [ A - 1 ] . append ( B - 1 ) NEW_LINE G [ B - 1 ] . append ( A - 1 ) NEW_LINE DEDENT def DFS ( u , n , G ) : NEW_LINE INDENT q = [ u ] NEW_LINE v = [ 0 ] * N NEW_LINE d = [ 0 ] * N NEW_LINE while q : NEW_LINE INDENT u1 = q . pop ( ) NEW_LINE v [ u1 ] = 1 NEW_LINE if d [ u1 ] < n : NEW_LINE INDENT for u2 in G [ u1 ] : NEW_LINE INDENT if not v [ u2 ] : NEW_LINE INDENT d [ u2 ] = d [ u1 ] + 1 NEW_LINE q . append ( u2 ) NEW_LINE DEDENT DEDENT DEDENT DEDENT return sum ( v ) NEW_LINE DEDENT def DFS_E ( u , uu , n , G ) : NEW_LINE INDENT q = [ u , uu ] NEW_LINE v = [ 0 ] * N NEW_LINE d = [ 0 ] * N NEW_LINE while q : NEW_LINE INDENT u1 = q . pop ( ) NEW_LINE v [ u1 ] = 1 NEW_LINE if d [ u1 ] < n : NEW_LINE INDENT for u2 in G [ u1 ] : NEW_LINE INDENT if not v [ u2 ] and u2 != u : NEW_LINE INDENT d [ u2 ] = d [ u1 ] + 1 NEW_LINE q . append ( u2 ) NEW_LINE DEDENT DEDENT DEDENT DEDENT return sum ( v ) NEW_LINE DEDENT if K % 2 == 0 : NEW_LINE INDENT ans = 0 NEW_LINE for v in range ( N ) : NEW_LINE INDENT ans = max ( ans , DFS ( v , K // 2 , G ) ) NEW_LINE DEDENT print ( N - ans ) NEW_LINE DEDENT else : NEW_LINE INDENT ans = 0 NEW_LINE for u , v in E : NEW_LINE INDENT ans = max ( ans , DFS_E ( u , v , ( K - 1 ) // 2 , G ) ) NEW_LINE DEDENT print ( N - ans ) NEW_LINE DEDENT
T384
import java . util . * ; class Main { static ArrayList < Integer > [ ] map ; public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int x = sc . nextInt ( ) ; if ( x == 1 ) { System . out . println ( n - 2 ) ; System . exit ( 0 ) ; } map = new ArrayList [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) map [ i ] = new ArrayList < > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int a = sc . nextInt ( ) ; int b = sc . nextInt ( ) ; map [ a ] . add ( b ) ; map [ b ] . add ( a ) ; } long ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int first = 1 , second = 0 ; for ( int w : map [ i ] ) { int [ ] cur = dfs ( w , i , x / 2 - 1 ) ; first += cur [ 0 ] ; second = Math . max ( second , cur [ 1 ] ) ; } if ( x % 2 == 1 ) first += second ; ans = Math . max ( ans , first ) ; } System . out . println ( n - ans ) ; } static int [ ] dfs ( int root , int from , int level ) { if ( level == 0 ) return new int [ ] { 1 , map [ root ] . size ( ) - 1 } ; int first = 1 , second = 0 ; for ( int w : map [ root ] ) { if ( w == from ) continue ; int [ ] cur = dfs ( w , root , level - 1 ) ; first += cur [ 0 ] ; second += cur [ 1 ] ; } return new int [ ] { first , second } ; } } Note : . / Main . java uses unchecked or unsafe operations . Note : Recompile with - Xlint : unchecked for details .
from collections import deque NEW_LINE N , K = map ( int , input ( ) . split ( ) ) NEW_LINE es = [ tuple ( map ( lambda x : int ( x ) - 1 , input ( ) . split ( ) ) ) for i in range ( N - 1 ) ] NEW_LINE tr = [ [ ] for i in range ( N ) ] NEW_LINE for i , ( a , b ) in enumerate ( es ) : NEW_LINE INDENT tr [ a ] . append ( ( b , i ) ) NEW_LINE tr [ b ] . append ( ( a , i ) ) NEW_LINE DEDENT def reachable_points ( root_v , cant_visit_v ) : NEW_LINE INDENT visited = [ 0 ] * N NEW_LINE visited [ root_v ] = visited [ cant_visit_v ] = 1 NEW_LINE q = deque ( [ ( root_v , 0 ) ] ) NEW_LINE ret = 0 NEW_LINE while q : NEW_LINE INDENT v , dep = q . popleft ( ) NEW_LINE if dep == K // 2 : break NEW_LINE for to , _ in tr [ v ] : NEW_LINE INDENT if visited [ to ] : continue NEW_LINE ret += 1 NEW_LINE q . append ( ( to , dep + 1 ) ) NEW_LINE visited [ to ] = 1 NEW_LINE DEDENT DEDENT return ret NEW_LINE DEDENT def reachable_edges ( root_e ) : NEW_LINE INDENT a , b = es [ root_e ] NEW_LINE return reachable_points ( a , b ) + reachable_points ( b , a ) NEW_LINE DEDENT if K % 2 : NEW_LINE INDENT ans = N NEW_LINE for center_e in range ( N - 1 ) : NEW_LINE INDENT ans = min ( ans , N - 2 - reachable_edges ( center_e ) ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT else : NEW_LINE INDENT ans = N NEW_LINE for center_v in range ( N ) : NEW_LINE INDENT ans = min ( ans , N - 1 - reachable_points ( center_v , center_v ) ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT
T385
import java . util . * ; public class Main { public static void main ( String [ ] args ) { int INF = 100000000 ; Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int m = sc . nextInt ( ) ; int [ ] dp = new int [ n + 1 ] ; int [ ] a = new int [ m ] ; for ( int i = 0 ; i < m ; i ++ ) { a [ i ] = sc . nextInt ( ) ; } int [ ] cost = { 0 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; Arrays . sort ( a ) ; Arrays . fill ( dp , - INF ) ; dp [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = m - 1 ; j >= 0 ; j -- ) { if ( i - cost [ a [ j ] ] < 0 ) continue ; else dp [ i ] = Math . max ( dp [ i ] , dp [ i - cost [ a [ j ] ] ] + 1 ) ; } } StringBuilder ans = new StringBuilder ( ) ; while ( n != 0 ) { for ( int j = m - 1 ; j >= 0 ; j -- ) { if ( cost [ a [ j ] ] > n ) continue ; if ( dp [ n - cost [ a [ j ] ] ] == dp [ n ] - 1 ) { ans . append ( a [ j ] ) ; n -= cost [ a [ j ] ] ; break ; } } } System . out . println ( ans ) ; } }
import sys NEW_LINE def solve ( N : int , M : int , A : " List [ int ] " ) : NEW_LINE INDENT costs = [ 10 ** 18 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] NEW_LINE cand = [ ( i , costs [ i ] ) for i in A ] NEW_LINE cand . sort ( reverse = True ) NEW_LINE digits = [ - 1 * 10 ** 16 for i in range ( N + 1 ) ] NEW_LINE digits [ 0 ] = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT r = - 1 * 10 ** 16 NEW_LINE for c in cand : NEW_LINE INDENT if i - c [ 1 ] >= 0 : NEW_LINE INDENT r = max ( r , digits [ i - c [ 1 ] ] + 1 ) NEW_LINE DEDENT DEDENT digits [ i ] = r NEW_LINE DEDENT digit = digits [ N ] NEW_LINE lastCost = N NEW_LINE result = 0 NEW_LINE for i in range ( digit ) : NEW_LINE INDENT for c in cand : NEW_LINE INDENT if lastCost - c [ 1 ] >= 0 and digits [ lastCost - c [ 1 ] ] == digits [ lastCost ] - 1 : NEW_LINE INDENT result = result * 10 + c [ 0 ] NEW_LINE lastCost = lastCost - c [ 1 ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT print ( result ) NEW_LINE return NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT def iterate_tokens ( ) : NEW_LINE INDENT for line in sys . stdin : NEW_LINE INDENT for word in line . split ( ) : NEW_LINE INDENT yield word NEW_LINE DEDENT DEDENT DEDENT tokens = iterate_tokens ( ) NEW_LINE N = int ( next ( tokens ) ) NEW_LINE M = int ( next ( tokens ) ) NEW_LINE A = [ int ( next ( tokens ) ) for _ in range ( M ) ] NEW_LINE solve ( N , M , A ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT
T386
import java . util . * ; public class Main { public static void main ( String [ ] args ) { int INF = 100000000 ; Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int m = sc . nextInt ( ) ; int [ ] dp = new int [ n + 1 ] ; int [ ] a = new int [ m ] ; for ( int i = 0 ; i < m ; i ++ ) { a [ i ] = sc . nextInt ( ) ; } int [ ] cost = { 0 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; Arrays . sort ( a ) ; Arrays . fill ( dp , - INF ) ; dp [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = m - 1 ; j >= 0 ; j -- ) { if ( i - cost [ a [ j ] ] < 0 ) continue ; else dp [ i ] = Math . max ( dp [ i ] , dp [ i - cost [ a [ j ] ] ] + 1 ) ; } } StringBuilder ans = new StringBuilder ( ) ; while ( n != 0 ) { for ( int j = m - 1 ; j >= 0 ; j -- ) { if ( cost [ a [ j ] ] > n ) continue ; if ( dp [ n - cost [ a [ j ] ] ] == dp [ n ] - 1 ) { ans . append ( a [ j ] ) ; n -= cost [ a [ j ] ] ; break ; } } } System . out . println ( ans ) ; } }
N , M = map ( int , input ( ) . split ( ) ) NEW_LINE A = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE def num ( k ) : NEW_LINE INDENT costs = [ 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] NEW_LINE return costs [ k - 1 ] NEW_LINE DEDENT inf = 1000 NEW_LINE dp = [ - inf for _ in range ( N + 1 ) ] NEW_LINE dp [ 0 ] = 0 NEW_LINE cost_list = [ num ( k ) for k in A ] NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for cost in cost_list : NEW_LINE INDENT if i - cost < 0 : NEW_LINE INDENT pass NEW_LINE DEDENT else : NEW_LINE INDENT temp = dp [ i - cost ] + 1 NEW_LINE dp [ i ] = max ( dp [ i ] , temp ) NEW_LINE DEDENT DEDENT DEDENT ans = ' ' NEW_LINE many = N NEW_LINE for d in range ( dp [ N ] ) : NEW_LINE INDENT for k in sorted ( A , reverse = True ) : NEW_LINE INDENT if many - num ( k ) < 0 : NEW_LINE INDENT pass NEW_LINE DEDENT else : NEW_LINE INDENT if dp [ many - num ( k ) ] == dp [ many ] - 1 : NEW_LINE INDENT ans += str ( k ) NEW_LINE many -= num ( k ) NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT pass NEW_LINE DEDENT DEDENT DEDENT DEDENT print ( int ( ans ) ) NEW_LINE
T387
import java . util . * ; public class Main { public static void main ( String [ ] args ) { int INF = 100000000 ; Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int m = sc . nextInt ( ) ; int [ ] dp = new int [ n + 1 ] ; int [ ] a = new int [ m ] ; for ( int i = 0 ; i < m ; i ++ ) { a [ i ] = sc . nextInt ( ) ; } int [ ] cost = { 0 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; Arrays . sort ( a ) ; Arrays . fill ( dp , - INF ) ; dp [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = m - 1 ; j >= 0 ; j -- ) { if ( i - cost [ a [ j ] ] < 0 ) continue ; else dp [ i ] = Math . max ( dp [ i ] , dp [ i - cost [ a [ j ] ] ] + 1 ) ; } } StringBuilder ans = new StringBuilder ( ) ; while ( n != 0 ) { for ( int j = m - 1 ; j >= 0 ; j -- ) { if ( cost [ a [ j ] ] > n ) continue ; if ( dp [ n - cost [ a [ j ] ] ] == dp [ n ] - 1 ) { ans . append ( a [ j ] ) ; n -= cost [ a [ j ] ] ; break ; } } } System . out . println ( ans ) ; } }
def solve ( string ) : NEW_LINE INDENT n , m , * a = map ( int , string . split ( ) ) NEW_LINE needs = { i + 1 : n for i , n in enumerate ( map ( int , "2 ▁ 5 ▁ 5 ▁ 4 ▁ 5 ▁ 6 ▁ 3 ▁ 7 ▁ 6" . split ( ) ) ) } NEW_LINE if 2 in a and 5 in a : NEW_LINE INDENT a . remove ( 2 ) NEW_LINE DEDENT if 3 in a and 5 in a : NEW_LINE INDENT a . remove ( 3 ) NEW_LINE DEDENT if 2 in a and 3 in a : NEW_LINE INDENT a . remove ( 2 ) NEW_LINE DEDENT if 6 in a and 9 in a : NEW_LINE INDENT a . remove ( 6 ) NEW_LINE DEDENT b = sorted ( a , key = lambda x : needs [ x ] ) NEW_LINE index = 0 NEW_LINE base = str ( b [ 0 ] ) * ( n // needs [ b [ 0 ] ] ) NEW_LINE n %= needs [ b [ 0 ] ] NEW_LINE while n > 0 : NEW_LINE INDENT use = [ _b for _b in b if needs [ _b ] <= needs [ b [ 0 ] ] + n ] NEW_LINE if len ( use ) == 1 : NEW_LINE INDENT base = base [ : - 1 ] NEW_LINE n += needs [ b [ 0 ] ] NEW_LINE DEDENT elif max ( use ) == b [ 0 ] : NEW_LINE INDENT tmp_n = use [ - 1 ] NEW_LINE tmp_c = n // ( needs [ tmp_n ] - needs [ b [ 0 ] ] ) NEW_LINE base = base [ : - tmp_c ] + str ( tmp_n ) * tmp_c NEW_LINE n -= ( needs [ tmp_n ] - needs [ b [ 0 ] ] ) * tmp_c NEW_LINE DEDENT else : NEW_LINE INDENT tmp_n = max ( use ) NEW_LINE tmp_c = n // ( needs [ tmp_n ] - needs [ b [ 0 ] ] ) NEW_LINE base = base [ : index ] + str ( tmp_n ) * tmp_c + base [ index + tmp_c : ] NEW_LINE n -= ( needs [ tmp_n ] - needs [ b [ 0 ] ] ) * tmp_c NEW_LINE index += tmp_c NEW_LINE DEDENT DEDENT return base NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( solve ( ' \n ' . join ( [ input ( ) , input ( ) ] ) ) ) NEW_LINE DEDENT
T388
import java . util . * ; public class Main { public static void main ( String [ ] args ) { int INF = 100000000 ; Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int m = sc . nextInt ( ) ; int [ ] dp = new int [ n + 1 ] ; int [ ] a = new int [ m ] ; for ( int i = 0 ; i < m ; i ++ ) { a [ i ] = sc . nextInt ( ) ; } int [ ] cost = { 0 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; Arrays . sort ( a ) ; Arrays . fill ( dp , - INF ) ; dp [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = m - 1 ; j >= 0 ; j -- ) { if ( i - cost [ a [ j ] ] < 0 ) continue ; else dp [ i ] = Math . max ( dp [ i ] , dp [ i - cost [ a [ j ] ] ] + 1 ) ; } } StringBuilder ans = new StringBuilder ( ) ; while ( n != 0 ) { for ( int j = m - 1 ; j >= 0 ; j -- ) { if ( cost [ a [ j ] ] > n ) continue ; if ( dp [ n - cost [ a [ j ] ] ] == dp [ n ] - 1 ) { ans . append ( a [ j ] ) ; n -= cost [ a [ j ] ] ; break ; } } } System . out . println ( ans ) ; } }
import sys NEW_LINE ns = lambda : sys . stdin . readline ( ) . rstrip ( ) NEW_LINE ni = lambda : int ( ns ( ) ) NEW_LINE nm = lambda : map ( int , sys . stdin . readline ( ) . split ( ) ) NEW_LINE nms = lambda : map ( str , sys . stdin . readline ( ) . split ( ) ) NEW_LINE nl = lambda : list ( nm ( ) ) NEW_LINE n , m = nm ( ) NEW_LINE a = nl ( ) NEW_LINE lis = [ 0 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] NEW_LINE dp = [ - 1 for _ in range ( n + 1 ) ] NEW_LINE dp [ 0 ] = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in a : NEW_LINE INDENT if i - lis [ j ] >= 0 : NEW_LINE INDENT dp [ i ] = max ( dp [ i ] , dp [ i - lis [ j ] ] * 10 + j ) NEW_LINE DEDENT DEDENT DEDENT print ( dp [ n ] ) NEW_LINE
T389
import java . util . * ; public class Main { public static void main ( String [ ] args ) { int INF = 100000000 ; Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int m = sc . nextInt ( ) ; int [ ] dp = new int [ n + 1 ] ; int [ ] a = new int [ m ] ; for ( int i = 0 ; i < m ; i ++ ) { a [ i ] = sc . nextInt ( ) ; } int [ ] cost = { 0 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; Arrays . sort ( a ) ; Arrays . fill ( dp , - INF ) ; dp [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = m - 1 ; j >= 0 ; j -- ) { if ( i - cost [ a [ j ] ] < 0 ) continue ; else dp [ i ] = Math . max ( dp [ i ] , dp [ i - cost [ a [ j ] ] ] + 1 ) ; } } StringBuilder ans = new StringBuilder ( ) ; while ( n != 0 ) { for ( int j = m - 1 ; j >= 0 ; j -- ) { if ( cost [ a [ j ] ] > n ) continue ; if ( dp [ n - cost [ a [ j ] ] ] == dp [ n ] - 1 ) { ans . append ( a [ j ] ) ; n -= cost [ a [ j ] ] ; break ; } } } System . out . println ( ans ) ; } }
import sys NEW_LINE RESOURCE_DICT = dict ( [ ( k , v ) for k , v in zip ( range ( 1 , 10 ) , [ 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] ) ] ) NEW_LINE def get_input ( ) : NEW_LINE INDENT N , M = [ int ( _ ) for _ in input ( ) . split ( ) ] NEW_LINE A = [ int ( _ ) for _ in input ( ) . split ( ) ] NEW_LINE return N , M , A NEW_LINE DEDENT def solve ( N , M , A ) : NEW_LINE INDENT sorted_A = sorted ( A ) [ : : - 1 ] NEW_LINE dp = fill_dp ( N , sorted_A ) NEW_LINE array = find_array ( dp , N , sorted_A ) NEW_LINE return " " . join ( map ( str , array ) ) NEW_LINE DEDENT def fill_dp ( N , A ) : NEW_LINE INDENT dp = [ - 100 * N for _ in range ( N + 1 ) ] NEW_LINE dp [ 0 ] = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT fill ( A , dp , i , N ) NEW_LINE DEDENT return dp NEW_LINE DEDENT def fill ( A , dp , n , N ) : NEW_LINE INDENT cand = [ dp [ n - RESOURCE_DICT [ num ] ] + 1 for num in A if n - RESOURCE_DICT [ num ] >= 0 ] NEW_LINE dp [ n ] = max ( cand ) if len ( cand ) > 0 else - 100 * N NEW_LINE DEDENT def find_array ( dp , N , sorted_A ) : NEW_LINE INDENT array = [ ] NEW_LINE current_n = N NEW_LINE for _ in range ( dp [ N ] ) : NEW_LINE INDENT for number in sorted_A : NEW_LINE INDENT if current_n - RESOURCE_DICT [ number ] >= 0 and dp [ current_n - RESOURCE_DICT [ number ] ] == dp [ current_n ] - 1 : NEW_LINE INDENT array . append ( number ) NEW_LINE current_n -= RESOURCE_DICT [ number ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT return array NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N , M , A = get_input ( ) NEW_LINE ans = solve ( N , M , A ) NEW_LINE print ( ans ) NEW_LINE DEDENT
T390
import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStreamReader ; public class Main { static long t [ ] ; static long l [ ] ; static long h [ ] ; public static void main ( String [ ] args ) throws IOException { BufferedReader r = new BufferedReader ( new InputStreamReader ( System . in ) , 1 ) ; String s ; String sl [ ] ; int d [ ] = new int [ 10 ] ; int c [ ] = { 999999 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; for ( int i = 0 ; i <= 9 ; i ++ ) { d [ i ] = 999999 ; } s = r . readLine ( ) ; sl = s . split ( " ▁ " ) ; int n = Integer . parseInt ( sl [ 0 ] ) ; int m = Integer . parseInt ( sl [ 1 ] ) ; s = r . readLine ( ) ; sl = s . split ( " ▁ " ) ; for ( int i = 0 ; i < m ; i ++ ) { int v = Integer . parseInt ( sl [ i ] ) ; d [ v ] = c [ v ] ; } int dp [ ] = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { dp [ i ] = - 999999 ; for ( int j = 1 ; j <= 9 ; j ++ ) { if ( i - d [ j ] >= 0 && dp [ i - d [ j ] ] >= 0 ) { dp [ i ] = Math . max ( dp [ i ] , dp [ i - d [ j ] ] + 1 ) ; } } } int z = dp [ n ] ; int q = n ; while ( q > 0 ) { for ( int i = 9 ; 1 <= i ; i -- ) { if ( q - d [ i ] >= 0 && dp [ q - d [ i ] ] == dp [ q ] - 1 ) { System . out . print ( i ) ; q -= d [ i ] ; break ; } } } } }
import sys NEW_LINE def solve ( N : int , M : int , A : " List [ int ] " ) : NEW_LINE INDENT costs = [ 10 ** 18 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] NEW_LINE cand = [ ( i , costs [ i ] ) for i in A ] NEW_LINE cand . sort ( reverse = True ) NEW_LINE digits = [ - 1 * 10 ** 16 for i in range ( N + 1 ) ] NEW_LINE digits [ 0 ] = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT r = - 1 * 10 ** 16 NEW_LINE for c in cand : NEW_LINE INDENT if i - c [ 1 ] >= 0 : NEW_LINE INDENT r = max ( r , digits [ i - c [ 1 ] ] + 1 ) NEW_LINE DEDENT DEDENT digits [ i ] = r NEW_LINE DEDENT digit = digits [ N ] NEW_LINE lastCost = N NEW_LINE result = 0 NEW_LINE for i in range ( digit ) : NEW_LINE INDENT for c in cand : NEW_LINE INDENT if lastCost - c [ 1 ] >= 0 and digits [ lastCost - c [ 1 ] ] == digits [ lastCost ] - 1 : NEW_LINE INDENT result = result * 10 + c [ 0 ] NEW_LINE lastCost = lastCost - c [ 1 ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT print ( result ) NEW_LINE return NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT def iterate_tokens ( ) : NEW_LINE INDENT for line in sys . stdin : NEW_LINE INDENT for word in line . split ( ) : NEW_LINE INDENT yield word NEW_LINE DEDENT DEDENT DEDENT tokens = iterate_tokens ( ) NEW_LINE N = int ( next ( tokens ) ) NEW_LINE M = int ( next ( tokens ) ) NEW_LINE A = [ int ( next ( tokens ) ) for _ in range ( M ) ] NEW_LINE solve ( N , M , A ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT
T391
import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStreamReader ; public class Main { static long t [ ] ; static long l [ ] ; static long h [ ] ; public static void main ( String [ ] args ) throws IOException { BufferedReader r = new BufferedReader ( new InputStreamReader ( System . in ) , 1 ) ; String s ; String sl [ ] ; int d [ ] = new int [ 10 ] ; int c [ ] = { 999999 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; for ( int i = 0 ; i <= 9 ; i ++ ) { d [ i ] = 999999 ; } s = r . readLine ( ) ; sl = s . split ( " ▁ " ) ; int n = Integer . parseInt ( sl [ 0 ] ) ; int m = Integer . parseInt ( sl [ 1 ] ) ; s = r . readLine ( ) ; sl = s . split ( " ▁ " ) ; for ( int i = 0 ; i < m ; i ++ ) { int v = Integer . parseInt ( sl [ i ] ) ; d [ v ] = c [ v ] ; } int dp [ ] = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { dp [ i ] = - 999999 ; for ( int j = 1 ; j <= 9 ; j ++ ) { if ( i - d [ j ] >= 0 && dp [ i - d [ j ] ] >= 0 ) { dp [ i ] = Math . max ( dp [ i ] , dp [ i - d [ j ] ] + 1 ) ; } } } int z = dp [ n ] ; int q = n ; while ( q > 0 ) { for ( int i = 9 ; 1 <= i ; i -- ) { if ( q - d [ i ] >= 0 && dp [ q - d [ i ] ] == dp [ q ] - 1 ) { System . out . print ( i ) ; q -= d [ i ] ; break ; } } } } }
N , M = map ( int , input ( ) . split ( ) ) NEW_LINE A = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE def num ( k ) : NEW_LINE INDENT costs = [ 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] NEW_LINE return costs [ k - 1 ] NEW_LINE DEDENT inf = 1000 NEW_LINE dp = [ - inf for _ in range ( N + 1 ) ] NEW_LINE dp [ 0 ] = 0 NEW_LINE cost_list = [ num ( k ) for k in A ] NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for cost in cost_list : NEW_LINE INDENT if i - cost < 0 : NEW_LINE INDENT pass NEW_LINE DEDENT else : NEW_LINE INDENT temp = dp [ i - cost ] + 1 NEW_LINE dp [ i ] = max ( dp [ i ] , temp ) NEW_LINE DEDENT DEDENT DEDENT ans = ' ' NEW_LINE many = N NEW_LINE for d in range ( dp [ N ] ) : NEW_LINE INDENT for k in sorted ( A , reverse = True ) : NEW_LINE INDENT if many - num ( k ) < 0 : NEW_LINE INDENT pass NEW_LINE DEDENT else : NEW_LINE INDENT if dp [ many - num ( k ) ] == dp [ many ] - 1 : NEW_LINE INDENT ans += str ( k ) NEW_LINE many -= num ( k ) NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT pass NEW_LINE DEDENT DEDENT DEDENT DEDENT print ( int ( ans ) ) NEW_LINE
T392
import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStreamReader ; public class Main { static long t [ ] ; static long l [ ] ; static long h [ ] ; public static void main ( String [ ] args ) throws IOException { BufferedReader r = new BufferedReader ( new InputStreamReader ( System . in ) , 1 ) ; String s ; String sl [ ] ; int d [ ] = new int [ 10 ] ; int c [ ] = { 999999 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; for ( int i = 0 ; i <= 9 ; i ++ ) { d [ i ] = 999999 ; } s = r . readLine ( ) ; sl = s . split ( " ▁ " ) ; int n = Integer . parseInt ( sl [ 0 ] ) ; int m = Integer . parseInt ( sl [ 1 ] ) ; s = r . readLine ( ) ; sl = s . split ( " ▁ " ) ; for ( int i = 0 ; i < m ; i ++ ) { int v = Integer . parseInt ( sl [ i ] ) ; d [ v ] = c [ v ] ; } int dp [ ] = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { dp [ i ] = - 999999 ; for ( int j = 1 ; j <= 9 ; j ++ ) { if ( i - d [ j ] >= 0 && dp [ i - d [ j ] ] >= 0 ) { dp [ i ] = Math . max ( dp [ i ] , dp [ i - d [ j ] ] + 1 ) ; } } } int z = dp [ n ] ; int q = n ; while ( q > 0 ) { for ( int i = 9 ; 1 <= i ; i -- ) { if ( q - d [ i ] >= 0 && dp [ q - d [ i ] ] == dp [ q ] - 1 ) { System . out . print ( i ) ; q -= d [ i ] ; break ; } } } } }
def solve ( string ) : NEW_LINE INDENT n , m , * a = map ( int , string . split ( ) ) NEW_LINE needs = { i + 1 : n for i , n in enumerate ( map ( int , "2 ▁ 5 ▁ 5 ▁ 4 ▁ 5 ▁ 6 ▁ 3 ▁ 7 ▁ 6" . split ( ) ) ) } NEW_LINE if 2 in a and 5 in a : NEW_LINE INDENT a . remove ( 2 ) NEW_LINE DEDENT if 3 in a and 5 in a : NEW_LINE INDENT a . remove ( 3 ) NEW_LINE DEDENT if 2 in a and 3 in a : NEW_LINE INDENT a . remove ( 2 ) NEW_LINE DEDENT if 6 in a and 9 in a : NEW_LINE INDENT a . remove ( 6 ) NEW_LINE DEDENT b = sorted ( a , key = lambda x : needs [ x ] ) NEW_LINE index = 0 NEW_LINE base = str ( b [ 0 ] ) * ( n // needs [ b [ 0 ] ] ) NEW_LINE n %= needs [ b [ 0 ] ] NEW_LINE while n > 0 : NEW_LINE INDENT use = [ _b for _b in b if needs [ _b ] <= needs [ b [ 0 ] ] + n ] NEW_LINE if len ( use ) == 1 : NEW_LINE INDENT base = base [ : - 1 ] NEW_LINE n += needs [ b [ 0 ] ] NEW_LINE DEDENT elif max ( use ) == b [ 0 ] : NEW_LINE INDENT tmp_n = use [ - 1 ] NEW_LINE tmp_c = n // ( needs [ tmp_n ] - needs [ b [ 0 ] ] ) NEW_LINE base = base [ : - tmp_c ] + str ( tmp_n ) * tmp_c NEW_LINE n -= ( needs [ tmp_n ] - needs [ b [ 0 ] ] ) * tmp_c NEW_LINE DEDENT else : NEW_LINE INDENT tmp_n = max ( use ) NEW_LINE tmp_c = n // ( needs [ tmp_n ] - needs [ b [ 0 ] ] ) NEW_LINE base = base [ : index ] + str ( tmp_n ) * tmp_c + base [ index + tmp_c : ] NEW_LINE n -= ( needs [ tmp_n ] - needs [ b [ 0 ] ] ) * tmp_c NEW_LINE index += tmp_c NEW_LINE DEDENT DEDENT return base NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( solve ( ' \n ' . join ( [ input ( ) , input ( ) ] ) ) ) NEW_LINE DEDENT
T393
import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStreamReader ; public class Main { static long t [ ] ; static long l [ ] ; static long h [ ] ; public static void main ( String [ ] args ) throws IOException { BufferedReader r = new BufferedReader ( new InputStreamReader ( System . in ) , 1 ) ; String s ; String sl [ ] ; int d [ ] = new int [ 10 ] ; int c [ ] = { 999999 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; for ( int i = 0 ; i <= 9 ; i ++ ) { d [ i ] = 999999 ; } s = r . readLine ( ) ; sl = s . split ( " ▁ " ) ; int n = Integer . parseInt ( sl [ 0 ] ) ; int m = Integer . parseInt ( sl [ 1 ] ) ; s = r . readLine ( ) ; sl = s . split ( " ▁ " ) ; for ( int i = 0 ; i < m ; i ++ ) { int v = Integer . parseInt ( sl [ i ] ) ; d [ v ] = c [ v ] ; } int dp [ ] = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { dp [ i ] = - 999999 ; for ( int j = 1 ; j <= 9 ; j ++ ) { if ( i - d [ j ] >= 0 && dp [ i - d [ j ] ] >= 0 ) { dp [ i ] = Math . max ( dp [ i ] , dp [ i - d [ j ] ] + 1 ) ; } } } int z = dp [ n ] ; int q = n ; while ( q > 0 ) { for ( int i = 9 ; 1 <= i ; i -- ) { if ( q - d [ i ] >= 0 && dp [ q - d [ i ] ] == dp [ q ] - 1 ) { System . out . print ( i ) ; q -= d [ i ] ; break ; } } } } }
import sys NEW_LINE ns = lambda : sys . stdin . readline ( ) . rstrip ( ) NEW_LINE ni = lambda : int ( ns ( ) ) NEW_LINE nm = lambda : map ( int , sys . stdin . readline ( ) . split ( ) ) NEW_LINE nms = lambda : map ( str , sys . stdin . readline ( ) . split ( ) ) NEW_LINE nl = lambda : list ( nm ( ) ) NEW_LINE n , m = nm ( ) NEW_LINE a = nl ( ) NEW_LINE lis = [ 0 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] NEW_LINE dp = [ - 1 for _ in range ( n + 1 ) ] NEW_LINE dp [ 0 ] = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in a : NEW_LINE INDENT if i - lis [ j ] >= 0 : NEW_LINE INDENT dp [ i ] = max ( dp [ i ] , dp [ i - lis [ j ] ] * 10 + j ) NEW_LINE DEDENT DEDENT DEDENT print ( dp [ n ] ) NEW_LINE
T394
import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStreamReader ; public class Main { static long t [ ] ; static long l [ ] ; static long h [ ] ; public static void main ( String [ ] args ) throws IOException { BufferedReader r = new BufferedReader ( new InputStreamReader ( System . in ) , 1 ) ; String s ; String sl [ ] ; int d [ ] = new int [ 10 ] ; int c [ ] = { 999999 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; for ( int i = 0 ; i <= 9 ; i ++ ) { d [ i ] = 999999 ; } s = r . readLine ( ) ; sl = s . split ( " ▁ " ) ; int n = Integer . parseInt ( sl [ 0 ] ) ; int m = Integer . parseInt ( sl [ 1 ] ) ; s = r . readLine ( ) ; sl = s . split ( " ▁ " ) ; for ( int i = 0 ; i < m ; i ++ ) { int v = Integer . parseInt ( sl [ i ] ) ; d [ v ] = c [ v ] ; } int dp [ ] = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { dp [ i ] = - 999999 ; for ( int j = 1 ; j <= 9 ; j ++ ) { if ( i - d [ j ] >= 0 && dp [ i - d [ j ] ] >= 0 ) { dp [ i ] = Math . max ( dp [ i ] , dp [ i - d [ j ] ] + 1 ) ; } } } int z = dp [ n ] ; int q = n ; while ( q > 0 ) { for ( int i = 9 ; 1 <= i ; i -- ) { if ( q - d [ i ] >= 0 && dp [ q - d [ i ] ] == dp [ q ] - 1 ) { System . out . print ( i ) ; q -= d [ i ] ; break ; } } } } }
import sys NEW_LINE RESOURCE_DICT = dict ( [ ( k , v ) for k , v in zip ( range ( 1 , 10 ) , [ 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] ) ] ) NEW_LINE def get_input ( ) : NEW_LINE INDENT N , M = [ int ( _ ) for _ in input ( ) . split ( ) ] NEW_LINE A = [ int ( _ ) for _ in input ( ) . split ( ) ] NEW_LINE return N , M , A NEW_LINE DEDENT def solve ( N , M , A ) : NEW_LINE INDENT sorted_A = sorted ( A ) [ : : - 1 ] NEW_LINE dp = fill_dp ( N , sorted_A ) NEW_LINE array = find_array ( dp , N , sorted_A ) NEW_LINE return " " . join ( map ( str , array ) ) NEW_LINE DEDENT def fill_dp ( N , A ) : NEW_LINE INDENT dp = [ - 100 * N for _ in range ( N + 1 ) ] NEW_LINE dp [ 0 ] = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT fill ( A , dp , i , N ) NEW_LINE DEDENT return dp NEW_LINE DEDENT def fill ( A , dp , n , N ) : NEW_LINE INDENT cand = [ dp [ n - RESOURCE_DICT [ num ] ] + 1 for num in A if n - RESOURCE_DICT [ num ] >= 0 ] NEW_LINE dp [ n ] = max ( cand ) if len ( cand ) > 0 else - 100 * N NEW_LINE DEDENT def find_array ( dp , N , sorted_A ) : NEW_LINE INDENT array = [ ] NEW_LINE current_n = N NEW_LINE for _ in range ( dp [ N ] ) : NEW_LINE INDENT for number in sorted_A : NEW_LINE INDENT if current_n - RESOURCE_DICT [ number ] >= 0 and dp [ current_n - RESOURCE_DICT [ number ] ] == dp [ current_n ] - 1 : NEW_LINE INDENT array . append ( number ) NEW_LINE current_n -= RESOURCE_DICT [ number ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT return array NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N , M , A = get_input ( ) NEW_LINE ans = solve ( N , M , A ) NEW_LINE print ( ans ) NEW_LINE DEDENT
T395
import java . io . InputStream ; import java . io . PrintStream ; import java . util . HashMap ; import java . util . Map ; import java . util . Scanner ; public class Main { InputStream in = System . in ; PrintStream out = System . out ; int [ ] q = { 0 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; int [ ] a ; int compare ( String a , String b ) { if ( a == null ) { a = " " ; } if ( b == null ) { b = " " ; } int result = Integer . compare ( a . length ( ) , b . length ( ) ) ; if ( result == 0 ) { result = a . compareTo ( b ) ; } return result ; } String max ( String s1 , String s2 ) { if ( compare ( s1 , s2 ) > 0 ) { return s1 ; } else { return s2 ; } } Map < Integer , String > memo = new HashMap < > ( ) ; String dfs ( int n ) { if ( n == 0 ) { return " " ; } String x = memo . get ( n ) ; if ( x != null ) { return x ; } String ans = null ; for ( int i = 0 ; i < a . length ; i ++ ) { int newN = n - q [ a [ i ] ] ; if ( newN < 0 ) { continue ; } String tmp = dfs ( newN ) ; if ( tmp == null ) { continue ; } ans = max ( ans , a [ i ] + tmp ) ; } memo . put ( n , ans ) ; return ans ; } public void _main ( String [ ] args ) { Scanner sc = new Scanner ( in ) ; int N = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; a = new int [ M ] ; for ( int i = 0 ; i < M ; i ++ ) { a [ i ] = sc . nextInt ( ) ; } out . println ( dfs ( N ) ) ; sc . close ( ) ; } public static void main ( String [ ] args ) { new Main ( ) . _main ( args ) ; } }
import sys NEW_LINE def solve ( N : int , M : int , A : " List [ int ] " ) : NEW_LINE INDENT costs = [ 10 ** 18 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] NEW_LINE cand = [ ( i , costs [ i ] ) for i in A ] NEW_LINE cand . sort ( reverse = True ) NEW_LINE digits = [ - 1 * 10 ** 16 for i in range ( N + 1 ) ] NEW_LINE digits [ 0 ] = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT r = - 1 * 10 ** 16 NEW_LINE for c in cand : NEW_LINE INDENT if i - c [ 1 ] >= 0 : NEW_LINE INDENT r = max ( r , digits [ i - c [ 1 ] ] + 1 ) NEW_LINE DEDENT DEDENT digits [ i ] = r NEW_LINE DEDENT digit = digits [ N ] NEW_LINE lastCost = N NEW_LINE result = 0 NEW_LINE for i in range ( digit ) : NEW_LINE INDENT for c in cand : NEW_LINE INDENT if lastCost - c [ 1 ] >= 0 and digits [ lastCost - c [ 1 ] ] == digits [ lastCost ] - 1 : NEW_LINE INDENT result = result * 10 + c [ 0 ] NEW_LINE lastCost = lastCost - c [ 1 ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT print ( result ) NEW_LINE return NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT def iterate_tokens ( ) : NEW_LINE INDENT for line in sys . stdin : NEW_LINE INDENT for word in line . split ( ) : NEW_LINE INDENT yield word NEW_LINE DEDENT DEDENT DEDENT tokens = iterate_tokens ( ) NEW_LINE N = int ( next ( tokens ) ) NEW_LINE M = int ( next ( tokens ) ) NEW_LINE A = [ int ( next ( tokens ) ) for _ in range ( M ) ] NEW_LINE solve ( N , M , A ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT
T396
import java . io . InputStream ; import java . io . PrintStream ; import java . util . HashMap ; import java . util . Map ; import java . util . Scanner ; public class Main { InputStream in = System . in ; PrintStream out = System . out ; int [ ] q = { 0 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; int [ ] a ; int compare ( String a , String b ) { if ( a == null ) { a = " " ; } if ( b == null ) { b = " " ; } int result = Integer . compare ( a . length ( ) , b . length ( ) ) ; if ( result == 0 ) { result = a . compareTo ( b ) ; } return result ; } String max ( String s1 , String s2 ) { if ( compare ( s1 , s2 ) > 0 ) { return s1 ; } else { return s2 ; } } Map < Integer , String > memo = new HashMap < > ( ) ; String dfs ( int n ) { if ( n == 0 ) { return " " ; } String x = memo . get ( n ) ; if ( x != null ) { return x ; } String ans = null ; for ( int i = 0 ; i < a . length ; i ++ ) { int newN = n - q [ a [ i ] ] ; if ( newN < 0 ) { continue ; } String tmp = dfs ( newN ) ; if ( tmp == null ) { continue ; } ans = max ( ans , a [ i ] + tmp ) ; } memo . put ( n , ans ) ; return ans ; } public void _main ( String [ ] args ) { Scanner sc = new Scanner ( in ) ; int N = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; a = new int [ M ] ; for ( int i = 0 ; i < M ; i ++ ) { a [ i ] = sc . nextInt ( ) ; } out . println ( dfs ( N ) ) ; sc . close ( ) ; } public static void main ( String [ ] args ) { new Main ( ) . _main ( args ) ; } }
N , M = map ( int , input ( ) . split ( ) ) NEW_LINE A = list ( map ( int , input ( ) . split ( ) ) ) NEW_LINE def num ( k ) : NEW_LINE INDENT costs = [ 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] NEW_LINE return costs [ k - 1 ] NEW_LINE DEDENT inf = 1000 NEW_LINE dp = [ - inf for _ in range ( N + 1 ) ] NEW_LINE dp [ 0 ] = 0 NEW_LINE cost_list = [ num ( k ) for k in A ] NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for cost in cost_list : NEW_LINE INDENT if i - cost < 0 : NEW_LINE INDENT pass NEW_LINE DEDENT else : NEW_LINE INDENT temp = dp [ i - cost ] + 1 NEW_LINE dp [ i ] = max ( dp [ i ] , temp ) NEW_LINE DEDENT DEDENT DEDENT ans = ' ' NEW_LINE many = N NEW_LINE for d in range ( dp [ N ] ) : NEW_LINE INDENT for k in sorted ( A , reverse = True ) : NEW_LINE INDENT if many - num ( k ) < 0 : NEW_LINE INDENT pass NEW_LINE DEDENT else : NEW_LINE INDENT if dp [ many - num ( k ) ] == dp [ many ] - 1 : NEW_LINE INDENT ans += str ( k ) NEW_LINE many -= num ( k ) NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT pass NEW_LINE DEDENT DEDENT DEDENT DEDENT print ( int ( ans ) ) NEW_LINE
T397
import java . io . InputStream ; import java . io . PrintStream ; import java . util . HashMap ; import java . util . Map ; import java . util . Scanner ; public class Main { InputStream in = System . in ; PrintStream out = System . out ; int [ ] q = { 0 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; int [ ] a ; int compare ( String a , String b ) { if ( a == null ) { a = " " ; } if ( b == null ) { b = " " ; } int result = Integer . compare ( a . length ( ) , b . length ( ) ) ; if ( result == 0 ) { result = a . compareTo ( b ) ; } return result ; } String max ( String s1 , String s2 ) { if ( compare ( s1 , s2 ) > 0 ) { return s1 ; } else { return s2 ; } } Map < Integer , String > memo = new HashMap < > ( ) ; String dfs ( int n ) { if ( n == 0 ) { return " " ; } String x = memo . get ( n ) ; if ( x != null ) { return x ; } String ans = null ; for ( int i = 0 ; i < a . length ; i ++ ) { int newN = n - q [ a [ i ] ] ; if ( newN < 0 ) { continue ; } String tmp = dfs ( newN ) ; if ( tmp == null ) { continue ; } ans = max ( ans , a [ i ] + tmp ) ; } memo . put ( n , ans ) ; return ans ; } public void _main ( String [ ] args ) { Scanner sc = new Scanner ( in ) ; int N = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; a = new int [ M ] ; for ( int i = 0 ; i < M ; i ++ ) { a [ i ] = sc . nextInt ( ) ; } out . println ( dfs ( N ) ) ; sc . close ( ) ; } public static void main ( String [ ] args ) { new Main ( ) . _main ( args ) ; } }
def solve ( string ) : NEW_LINE INDENT n , m , * a = map ( int , string . split ( ) ) NEW_LINE needs = { i + 1 : n for i , n in enumerate ( map ( int , "2 ▁ 5 ▁ 5 ▁ 4 ▁ 5 ▁ 6 ▁ 3 ▁ 7 ▁ 6" . split ( ) ) ) } NEW_LINE if 2 in a and 5 in a : NEW_LINE INDENT a . remove ( 2 ) NEW_LINE DEDENT if 3 in a and 5 in a : NEW_LINE INDENT a . remove ( 3 ) NEW_LINE DEDENT if 2 in a and 3 in a : NEW_LINE INDENT a . remove ( 2 ) NEW_LINE DEDENT if 6 in a and 9 in a : NEW_LINE INDENT a . remove ( 6 ) NEW_LINE DEDENT b = sorted ( a , key = lambda x : needs [ x ] ) NEW_LINE index = 0 NEW_LINE base = str ( b [ 0 ] ) * ( n // needs [ b [ 0 ] ] ) NEW_LINE n %= needs [ b [ 0 ] ] NEW_LINE while n > 0 : NEW_LINE INDENT use = [ _b for _b in b if needs [ _b ] <= needs [ b [ 0 ] ] + n ] NEW_LINE if len ( use ) == 1 : NEW_LINE INDENT base = base [ : - 1 ] NEW_LINE n += needs [ b [ 0 ] ] NEW_LINE DEDENT elif max ( use ) == b [ 0 ] : NEW_LINE INDENT tmp_n = use [ - 1 ] NEW_LINE tmp_c = n // ( needs [ tmp_n ] - needs [ b [ 0 ] ] ) NEW_LINE base = base [ : - tmp_c ] + str ( tmp_n ) * tmp_c NEW_LINE n -= ( needs [ tmp_n ] - needs [ b [ 0 ] ] ) * tmp_c NEW_LINE DEDENT else : NEW_LINE INDENT tmp_n = max ( use ) NEW_LINE tmp_c = n // ( needs [ tmp_n ] - needs [ b [ 0 ] ] ) NEW_LINE base = base [ : index ] + str ( tmp_n ) * tmp_c + base [ index + tmp_c : ] NEW_LINE n -= ( needs [ tmp_n ] - needs [ b [ 0 ] ] ) * tmp_c NEW_LINE index += tmp_c NEW_LINE DEDENT DEDENT return base NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( solve ( ' \n ' . join ( [ input ( ) , input ( ) ] ) ) ) NEW_LINE DEDENT
T398
import java . io . InputStream ; import java . io . PrintStream ; import java . util . HashMap ; import java . util . Map ; import java . util . Scanner ; public class Main { InputStream in = System . in ; PrintStream out = System . out ; int [ ] q = { 0 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; int [ ] a ; int compare ( String a , String b ) { if ( a == null ) { a = " " ; } if ( b == null ) { b = " " ; } int result = Integer . compare ( a . length ( ) , b . length ( ) ) ; if ( result == 0 ) { result = a . compareTo ( b ) ; } return result ; } String max ( String s1 , String s2 ) { if ( compare ( s1 , s2 ) > 0 ) { return s1 ; } else { return s2 ; } } Map < Integer , String > memo = new HashMap < > ( ) ; String dfs ( int n ) { if ( n == 0 ) { return " " ; } String x = memo . get ( n ) ; if ( x != null ) { return x ; } String ans = null ; for ( int i = 0 ; i < a . length ; i ++ ) { int newN = n - q [ a [ i ] ] ; if ( newN < 0 ) { continue ; } String tmp = dfs ( newN ) ; if ( tmp == null ) { continue ; } ans = max ( ans , a [ i ] + tmp ) ; } memo . put ( n , ans ) ; return ans ; } public void _main ( String [ ] args ) { Scanner sc = new Scanner ( in ) ; int N = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; a = new int [ M ] ; for ( int i = 0 ; i < M ; i ++ ) { a [ i ] = sc . nextInt ( ) ; } out . println ( dfs ( N ) ) ; sc . close ( ) ; } public static void main ( String [ ] args ) { new Main ( ) . _main ( args ) ; } }
import sys NEW_LINE ns = lambda : sys . stdin . readline ( ) . rstrip ( ) NEW_LINE ni = lambda : int ( ns ( ) ) NEW_LINE nm = lambda : map ( int , sys . stdin . readline ( ) . split ( ) ) NEW_LINE nms = lambda : map ( str , sys . stdin . readline ( ) . split ( ) ) NEW_LINE nl = lambda : list ( nm ( ) ) NEW_LINE n , m = nm ( ) NEW_LINE a = nl ( ) NEW_LINE lis = [ 0 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] NEW_LINE dp = [ - 1 for _ in range ( n + 1 ) ] NEW_LINE dp [ 0 ] = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in a : NEW_LINE INDENT if i - lis [ j ] >= 0 : NEW_LINE INDENT dp [ i ] = max ( dp [ i ] , dp [ i - lis [ j ] ] * 10 + j ) NEW_LINE DEDENT DEDENT DEDENT print ( dp [ n ] ) NEW_LINE
T399
import java . io . InputStream ; import java . io . PrintStream ; import java . util . HashMap ; import java . util . Map ; import java . util . Scanner ; public class Main { InputStream in = System . in ; PrintStream out = System . out ; int [ ] q = { 0 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; int [ ] a ; int compare ( String a , String b ) { if ( a == null ) { a = " " ; } if ( b == null ) { b = " " ; } int result = Integer . compare ( a . length ( ) , b . length ( ) ) ; if ( result == 0 ) { result = a . compareTo ( b ) ; } return result ; } String max ( String s1 , String s2 ) { if ( compare ( s1 , s2 ) > 0 ) { return s1 ; } else { return s2 ; } } Map < Integer , String > memo = new HashMap < > ( ) ; String dfs ( int n ) { if ( n == 0 ) { return " " ; } String x = memo . get ( n ) ; if ( x != null ) { return x ; } String ans = null ; for ( int i = 0 ; i < a . length ; i ++ ) { int newN = n - q [ a [ i ] ] ; if ( newN < 0 ) { continue ; } String tmp = dfs ( newN ) ; if ( tmp == null ) { continue ; } ans = max ( ans , a [ i ] + tmp ) ; } memo . put ( n , ans ) ; return ans ; } public void _main ( String [ ] args ) { Scanner sc = new Scanner ( in ) ; int N = sc . nextInt ( ) ; int M = sc . nextInt ( ) ; a = new int [ M ] ; for ( int i = 0 ; i < M ; i ++ ) { a [ i ] = sc . nextInt ( ) ; } out . println ( dfs ( N ) ) ; sc . close ( ) ; } public static void main ( String [ ] args ) { new Main ( ) . _main ( args ) ; } }
import sys NEW_LINE RESOURCE_DICT = dict ( [ ( k , v ) for k , v in zip ( range ( 1 , 10 ) , [ 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] ) ] ) NEW_LINE def get_input ( ) : NEW_LINE INDENT N , M = [ int ( _ ) for _ in input ( ) . split ( ) ] NEW_LINE A = [ int ( _ ) for _ in input ( ) . split ( ) ] NEW_LINE return N , M , A NEW_LINE DEDENT def solve ( N , M , A ) : NEW_LINE INDENT sorted_A = sorted ( A ) [ : : - 1 ] NEW_LINE dp = fill_dp ( N , sorted_A ) NEW_LINE array = find_array ( dp , N , sorted_A ) NEW_LINE return " " . join ( map ( str , array ) ) NEW_LINE DEDENT def fill_dp ( N , A ) : NEW_LINE INDENT dp = [ - 100 * N for _ in range ( N + 1 ) ] NEW_LINE dp [ 0 ] = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT fill ( A , dp , i , N ) NEW_LINE DEDENT return dp NEW_LINE DEDENT def fill ( A , dp , n , N ) : NEW_LINE INDENT cand = [ dp [ n - RESOURCE_DICT [ num ] ] + 1 for num in A if n - RESOURCE_DICT [ num ] >= 0 ] NEW_LINE dp [ n ] = max ( cand ) if len ( cand ) > 0 else - 100 * N NEW_LINE DEDENT def find_array ( dp , N , sorted_A ) : NEW_LINE INDENT array = [ ] NEW_LINE current_n = N NEW_LINE for _ in range ( dp [ N ] ) : NEW_LINE INDENT for number in sorted_A : NEW_LINE INDENT if current_n - RESOURCE_DICT [ number ] >= 0 and dp [ current_n - RESOURCE_DICT [ number ] ] == dp [ current_n ] - 1 : NEW_LINE INDENT array . append ( number ) NEW_LINE current_n -= RESOURCE_DICT [ number ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT return array NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N , M , A = get_input ( ) NEW_LINE ans = solve ( N , M , A ) NEW_LINE print ( ans ) NEW_LINE DEDENT