




"""simple docstring"""




from abc import ABC, abstractmethod
from typing import List, Optional






class     __magic_name__							(       UpperCAmelCase__				):






				'''simple docstring'''





				def __init__(       self ):





							"""simple docstring"""
							# test for the above condition
							self.test()





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					0
							lowerCamelCase			      =					False
							while not completed:
										if counter == 1:
													self.reset()
										lowerCamelCase			      =					self.advance()
										if not self.does_advance(_a ):
													raise Exception(
													    """Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true.""" )

										lowerCamelCase      ,   lowerCamelCase      ,   lowerCamelCase			      =					self.update(_a )
										counter += 1

										if counter > 10_000:
													raise Exception("""update() does not fulfill the constraint.""" )

							if self.remaining() != 0:
										raise Exception("""Custom Constraint is not defined correctly.""" )





				@abstractmethod
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							raise NotImplementedError(
							    f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.' )





				@abstractmethod
				def    _lowerCAmelCase				(       self		, _a ):





							"""simple docstring"""
							raise NotImplementedError(
							    f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.' )





				@abstractmethod
				def    _lowerCAmelCase				(       self		, _a ):





							"""simple docstring"""
							raise NotImplementedError(
							    f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.' )





				@abstractmethod
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							raise NotImplementedError(
							    f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.' )





				@abstractmethod
				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							raise NotImplementedError(
							    f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.' )





				@abstractmethod
				def    _lowerCAmelCase				(       self		, _a=False ):





							"""simple docstring"""
							raise NotImplementedError(
							    f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.' )






class     __magic_name__							(       UpperCAmelCase__				):






				'''simple docstring'''





				def __init__(       self		, _a ):





							"""simple docstring"""
							super(_a		, self ).__init__()

							if not isinstance(_a		, _a ) or len(_a ) == 0:
										raise ValueError(f'`token_ids` has to be a non-empty list, but is {token_ids}.' )
							if any((not isinstance(_a		, _a ) or token_id < 0) for token_id in token_ids ):
										raise ValueError(f'Each list in `token_ids` has to be a list of positive integers, but is {token_ids}.' )

							lowerCamelCase			      =					token_ids

							lowerCamelCase			      =					len(self.token_ids )
							lowerCamelCase			      =					-1  # the index of the currently fulfilled step
							lowerCamelCase			      =					False





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							if self.completed:
										return None
							return self.token_ids[self.fulfilled_idx + 1]





				def    _lowerCAmelCase				(       self		, _a ):





							"""simple docstring"""
							if not isinstance(_a		, _a ):
										raise ValueError(f'`token_id` has to be an `int`, but is {token_id} of type {type(_a )}' )

							if self.completed:
										return False

							return token_id == self.token_ids[self.fulfilled_idx + 1]





				def    _lowerCAmelCase				(       self		, _a ):





							"""simple docstring"""
							if not isinstance(_a		, _a ):
										raise ValueError(f'`token_id` has to be an `int`, but is {token_id} of type {type(_a )}' )

							lowerCamelCase			      =					False
							lowerCamelCase			      =					False
							lowerCamelCase			      =					False

							if self.does_advance(_a ):
										self.fulfilled_idx += 1
										lowerCamelCase			      =					True
										if self.fulfilled_idx == (self.seqlen - 1):
													lowerCamelCase			      =					True
										lowerCamelCase			      =					completed
							else:
										# failed to make progress.
										lowerCamelCase			      =					True
										self.reset()
							return stepped, completed, reset





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					False
							lowerCamelCase			      =					0





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							return self.seqlen - (self.fulfilled_idx + 1)





				def    _lowerCAmelCase				(       self		, _a=False ):





							"""simple docstring"""
							lowerCamelCase			      =					PhrasalConstraint(self.token_ids )

							if stateful:
										lowerCamelCase			      =					self.seqlen
										lowerCamelCase			      =					self.fulfilled_idx
										lowerCamelCase			      =					self.completed

							return new_constraint






class     __magic_name__							:






				'''simple docstring'''





				def __init__(       self		, _a		, _a=True ):





							"""simple docstring"""
							lowerCamelCase			      =					max([len(_a ) for one in nested_token_ids] )

							lowerCamelCase			      =					{}
							for token_ids in nested_token_ids:
										lowerCamelCase			      =					root
										for tidx, token_id in enumerate(_a ):
													if token_id not in level:
																lowerCamelCase			      =					{}

													lowerCamelCase			      =					level[token_id]

							if no_subsets and self.has_subsets(_a		, _a ):
										raise ValueError(
										    """Each list in `nested_token_ids` can't be a complete subset of another list, but is"""
										    f' {nested_token_ids}.' )

							lowerCamelCase			      =					root





				def    _lowerCAmelCase				(       self		, _a ):





							"""simple docstring"""
							lowerCamelCase			      =					self.trie

							for current_token in current_seq:
										lowerCamelCase			      =					start[current_token]

							lowerCamelCase			      =					list(start.keys() )

							return next_tokens





				def    _lowerCAmelCase				(       self		, _a ):





							"""simple docstring"""
							lowerCamelCase			      =					self.next_tokens(_a )

							return len(_a ) == 0





				def    _lowerCAmelCase				(       self		, _a ):





							"""simple docstring"""
							lowerCamelCase			      =					list(root.values() )
							if len(_a ) == 0:
										return 1
							else:
										return sum([self.count_leaves(_a ) for nn in next_nodes] )





				def    _lowerCAmelCase				(       self		, _a		, _a ):





							"""simple docstring"""
							lowerCamelCase			      =					self.count_leaves(_a )
							return len(_a ) != leaf_count






class     __magic_name__							(       UpperCAmelCase__				):






				'''simple docstring'''





				def __init__(       self		, _a ):





							"""simple docstring"""
							super(_a		, self ).__init__()

							if not isinstance(_a		, _a ) or len(_a ) == 0:
										raise ValueError(f'`nested_token_ids` has to be a non-empty list, but is {nested_token_ids}.' )
							if any(not isinstance(_a		, _a ) for token_ids in nested_token_ids ):
										raise ValueError(f'`nested_token_ids` has to be a list of lists, but is {nested_token_ids}.' )
							if any(
							    any((not isinstance(_a		, _a ) or token_id < 0) for token_id in token_ids )
							    for token_ids in nested_token_ids ):
										raise ValueError(
										    f'Each list in `nested_token_ids` has to be a list of positive integers, but is {nested_token_ids}.' )

							lowerCamelCase			      =					DisjunctiveTrie(_a )
							lowerCamelCase			      =					nested_token_ids

							lowerCamelCase			      =					self.trie.max_height
							lowerCamelCase			      =					[]
							lowerCamelCase			      =					False





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					self.trie.next_tokens(self.current_seq )

							if len(_a ) == 0:
										return None
							else:
										return token_list





				def    _lowerCAmelCase				(       self		, _a ):





							"""simple docstring"""
							if not isinstance(_a		, _a ):
										raise ValueError(f'`token_id` is supposed to be type `int`, but is {token_id} of type {type(_a )}' )

							lowerCamelCase			      =					self.trie.next_tokens(self.current_seq )

							return token_id in next_tokens





				def    _lowerCAmelCase				(       self		, _a ):





							"""simple docstring"""
							if not isinstance(_a		, _a ):
										raise ValueError(f'`token_id` is supposed to be type `int`, but is {token_id} of type {type(_a )}' )

							lowerCamelCase			      =					False
							lowerCamelCase			      =					False
							lowerCamelCase			      =					False

							if self.does_advance(_a ):
										self.current_seq.append(_a )
										lowerCamelCase			      =					True
							else:
										lowerCamelCase			      =					True
										self.reset()

							lowerCamelCase			      =					self.trie.reached_leaf(self.current_seq )
							lowerCamelCase			      =					completed

							return stepped, completed, reset





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					False
							lowerCamelCase			      =					[]





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							if self.completed:
										# since this can be completed without reaching max height
										return 0
							else:
										return self.seqlen - len(self.current_seq )





				def    _lowerCAmelCase				(       self		, _a=False ):





							"""simple docstring"""
							lowerCamelCase			      =					DisjunctiveConstraint(self.token_ids )

							if stateful:
										lowerCamelCase			      =					self.seqlen
										lowerCamelCase			      =					self.current_seq
										lowerCamelCase			      =					self.completed

							return new_constraint






class     __magic_name__							:






				'''simple docstring'''





				def __init__(       self		, _a ):





							"""simple docstring"""
							lowerCamelCase			      =					constraints

							# max # of steps required to fulfill a given constraint
							lowerCamelCase			      =					max([c.seqlen for c in constraints] )
							lowerCamelCase			      =					len(_a )
							lowerCamelCase			      =					False

							self.init_state()





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					[]
							lowerCamelCase			      =					None
							lowerCamelCase			      =					[constraint.copy(stateful=_a ) for constraint in self.constraints]





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					0
							if self.inprogress_constraint:
										# extra points for having a constraint mid-fulfilled
										add += self.max_seqlen - self.inprogress_constraint.remaining()

							return (len(self.complete_constraints ) * self.max_seqlen) + add





				def    _lowerCAmelCase				(       self ):





							"""simple docstring"""
							lowerCamelCase			      =					[]
							if self.inprogress_constraint is None:
										for constraint in self.pending_constraints:  # "pending" == "unfulfilled yet"
													lowerCamelCase			      =					constraint.advance()
													if isinstance(_a		, _a ):
																token_list.append(_a )
													elif isinstance(_a		, _a ):
																token_list.extend(_a )
							else:
										lowerCamelCase			      =					self.inprogress_constraint.advance()
										if isinstance(_a		, _a ):
													token_list.append(_a )
										elif isinstance(_a		, _a ):
													token_list.extend(_a )

							if len(_a ) == 0:
										return None
							else:
										return token_list





				def    _lowerCAmelCase				(       self		, _a ):





							"""simple docstring"""
							self.init_state()

							if token_ids is not None:
										for token in token_ids:
													# completes or steps **one** constraint
													lowerCamelCase      ,   lowerCamelCase			      =					self.add(_a )

													# the entire list of constraints are fulfilled
													if self.completed:
																break





				def    _lowerCAmelCase				(       self		, _a ):





							"""simple docstring"""
							if not isinstance(_a		, _a ):
										raise ValueError(f'`token_id` should be an `int`, but is `{token_id}`.' )

							lowerCamelCase      ,   lowerCamelCase			      =					False, False

							if self.completed:
										lowerCamelCase			      =					True
										lowerCamelCase			      =					False
										return complete, stepped

							if self.inprogress_constraint is not None:
										# In the middle of fulfilling a constraint. If the `token_id` *does* makes an incremental progress to current
										# job, simply update the state

										lowerCamelCase      ,   lowerCamelCase      ,   lowerCamelCase			      =					self.inprogress_constraint.update(_a )
										if reset:
													# 1. If the next token breaks the progress, then we must restart.
													#     e.g. constraint = "I love pies" and sequence so far is "I love" but `token_id` == "books".

													#     But that doesn't mean we self.init_state(), since we only reset the state for this particular
													#     constraint, not the full list of constraints.

													self.pending_constraints.append(self.inprogress_constraint.copy(stateful=_a ) )
													lowerCamelCase			      =					None

										if complete:
													# 2. If the next token completes the constraint, move it to completed list, set
													#     inprogress to None. If there are no pending constraints either, then this full list of constraints
													#     is complete.

													self.complete_constraints.append(self.inprogress_constraint )
													lowerCamelCase			      =					None

													if len(self.pending_constraints ) == 0:
																# we're done!
																lowerCamelCase			      =					True

							else:
										# Not in the middle of fulfilling a constraint. So does this `token_id` helps us step towards any of our list
										# of constraints?

										for cidx, pending_constraint in enumerate(self.pending_constraints ):
													if pending_constraint.does_advance(_a ):
																lowerCamelCase      ,   lowerCamelCase      ,   lowerCamelCase			      =					pending_constraint.update(_a )

																if not stepped:
																			raise Exception(
																			    """`constraint.update(token_id)` is not yielding incremental progress, """
																			    """even though `constraint.does_advance(token_id)` is true.""" )

																if complete:
																			self.complete_constraints.append(_a )
																			lowerCamelCase			      =					None

																if not complete and stepped:
																			lowerCamelCase			      =					pending_constraint

																if complete or stepped:
																			# If we made any progress at all, then it's at least not a "pending constraint".

																			lowerCamelCase			      =					(
																			    self.pending_constraints[:cidx] + self.pending_constraints[cidx + 1 :]
																			)

																			if len(self.pending_constraints ) == 0 and self.inprogress_constraint is None:
																						# If there's no longer any pending after this and no inprogress either, then we must be
																						# complete.

																						lowerCamelCase			      =					True

																			break  # prevent accidentally stepping through multiple constraints with just one token.

							return complete, stepped





				def    _lowerCAmelCase				(       self		, _a=True ):





							"""simple docstring"""
							lowerCamelCase			      =					ConstraintListState(self.constraints )  # we actually never though self.constraints objects
							# throughout this process. So it's at initialization state.

							if stateful:
										lowerCamelCase			      =					[
										    constraint.copy(stateful=_a ) for constraint in self.complete_constraints
										]
										if self.inprogress_constraint is not None:
													lowerCamelCase			      =					self.inprogress_constraint.copy(stateful=_a )
										lowerCamelCase			      =					[constraint.copy() for constraint in self.pending_constraints]

							return new_state









"""simple docstring"""




def     a__ (				snake_case__				)						->					bool:
			lowerCamelCase			      =					(1 + 24 * n) ** 0.5
			return ((1 + root) / 6) % 1 == 0



def     a__ (				snake_case__ = 50_00				)						->					int:
			lowerCamelCase			      =					[(i * (3 * i - 1)) // 2 for i in range(1       , snake_case__				)]
			for i, pentagonal_i in enumerate(snake_case__				):
						for j in range(snake_case__       , len(snake_case__				)				):
									lowerCamelCase			      =					pentagonal_nums[j]
									lowerCamelCase			      =					pentagonal_i + pentagonal_j
									lowerCamelCase			      =					pentagonal_j - pentagonal_i
									if is_pentagonal(snake_case__				) and is_pentagonal(snake_case__				):
												return b

			return -1


if __name__ == "__main__":
					print(F"""{solution() = }""")



