

import functools
import math

import flax.linen as nn
import jax
import jax.numpy as jnp





def 							__a       (					__A ,       __A ,       __A ,       __A ,       __A = 4096	):
    SCREAMING_SNAKE_CASE__     ,		SCREAMING_SNAKE_CASE__     ,		SCREAMING_SNAKE_CASE__					     =						key.shape[-3:]
    SCREAMING_SNAKE_CASE__					     =						value.shape[-1]
    SCREAMING_SNAKE_CASE__					     =						min(__A ,       __A	)
    SCREAMING_SNAKE_CASE__					     =						query / jnp.sqrt(__A	)

    @functools.partial(jax.checkpoint ,       prevent_cse=__A	)
    def summarize_chunk(__A ,       __A ,       __A	):
        SCREAMING_SNAKE_CASE__					     =						jnp.einsum("...qhd,...khd->...qhk" ,       __A ,       __A ,       precision=__A	)

        SCREAMING_SNAKE_CASE__					     =						jnp.max(__A ,       axis=-1 ,       keepdims=__A	)
        SCREAMING_SNAKE_CASE__					     =						jax.lax.stop_gradient(__A	)
        SCREAMING_SNAKE_CASE__					     =						jnp.exp(attn_weights - max_score	)

        SCREAMING_SNAKE_CASE__					     =						jnp.einsum("...vhf,...qhv->...qhf" ,       __A ,       __A ,       precision=__A	)
        SCREAMING_SNAKE_CASE__					     =						jnp.einsum("...qhk->...qh" ,       __A	)

        return (exp_values, exp_weights.sum(axis=-1	), max_score)

    def chunk_scanner(__A	):
        # julienne key array
        SCREAMING_SNAKE_CASE__					     =						jax.lax.dynamic_slice(
            operand=__A ,       start_indices=[0] * (key.ndim - 3) + [chunk_idx, 0, 0] ,       slice_sizes=list(key.shape[:-3]	) + [key_chunk_size, num_heads, k_features] ,       )

        # julienne value array
        SCREAMING_SNAKE_CASE__					     =						jax.lax.dynamic_slice(
            operand=__A ,       start_indices=[0] * (value.ndim - 3) + [chunk_idx, 0, 0] ,       slice_sizes=list(value.shape[:-3]	) + [key_chunk_size, num_heads, v_features] ,       )

        return summarize_chunk(__A ,       __A ,       __A	)

    SCREAMING_SNAKE_CASE__     ,		SCREAMING_SNAKE_CASE__     ,		SCREAMING_SNAKE_CASE__					     =						jax.lax.map(f=__A ,       xs=jnp.arange(0 ,       __A ,       __A	)	)

    SCREAMING_SNAKE_CASE__					     =						jnp.max(__A ,       axis=0 ,       keepdims=__A	)
    SCREAMING_SNAKE_CASE__					     =						jnp.exp(chunk_max - global_max	)

    chunk_values *= jnp.expand_dims(__A ,       axis=-1	)
    chunk_weights *= max_diffs

    SCREAMING_SNAKE_CASE__					     =						chunk_values.sum(axis=0	)
    SCREAMING_SNAKE_CASE__					     =						jnp.expand_dims(__A ,       -1	).sum(axis=0	)

    return all_values / all_weights





def 							__a       (					__A ,       __A ,       __A ,       __A=jax.lax.Precision.HIGHEST ,       __A = 1024 ,       __A = 4096	):
    SCREAMING_SNAKE_CASE__     ,		SCREAMING_SNAKE_CASE__     ,		SCREAMING_SNAKE_CASE__					     =						query.shape[-3:]

    def chunk_scanner(__A ,       __A	):
        # julienne query array
        SCREAMING_SNAKE_CASE__					     =						jax.lax.dynamic_slice(
            operand=__A ,       start_indices=([0] * (query.ndim - 3)) + [chunk_idx, 0, 0] ,       slice_sizes=list(query.shape[:-3]	) + [min(__A ,       __A	), num_heads, q_features] ,       )

        return (
            chunk_idx + query_chunk_size,  # unused ignore it
            _query_chunk_attention(
                query=__A ,       key=__A ,       value=__A ,       precision=__A ,       key_chunk_size=__A	),
        )

    SCREAMING_SNAKE_CASE__     ,		SCREAMING_SNAKE_CASE__					     =						jax.lax.scan(
        f=__A ,       init=0 ,       xs=__A ,       length=math.ceil(num_q / query_chunk_size	)	)

    return jnp.concatenate(__A ,       axis=-3	)  # fuse the chunked result back







class    UpperCamelCase_			(  nn.Module			):


  """simple docstring"""
  SCREAMING_SNAKE_CASE__				: int
  SCREAMING_SNAKE_CASE__				: int										=      8
  SCREAMING_SNAKE_CASE__				: int										=      64
  SCREAMING_SNAKE_CASE__				: float										=      0.0
  SCREAMING_SNAKE_CASE__				: bool										=      False
  SCREAMING_SNAKE_CASE__				: jnp.dtype										=      jnp.floataa



  def      _UpperCamelCase    (	self    :				Optional[Any]	)					-> List[str]:
      SCREAMING_SNAKE_CASE__					     =						self.dim_head * self.heads
      SCREAMING_SNAKE_CASE__					     =						self.dim_head**-0.5

      # Weights were exported with old names {to_q, to_k, to_v, to_out}
      SCREAMING_SNAKE_CASE__					     =						nn.Dense(A_     ,  use_bias=A_     ,  dtype=self.dtype     ,  name="to_q"	)
      SCREAMING_SNAKE_CASE__					     =						nn.Dense(A_     ,  use_bias=A_     ,  dtype=self.dtype     ,  name="to_k"	)
      SCREAMING_SNAKE_CASE__					     =						nn.Dense(A_     ,  use_bias=A_     ,  dtype=self.dtype     ,  name="to_v"	)

      SCREAMING_SNAKE_CASE__					     =						nn.Dense(self.query_dim     ,  dtype=self.dtype     ,  name="to_out_0"	)
      SCREAMING_SNAKE_CASE__					     =						nn.Dropout(rate=self.dropout	)



  def      _UpperCamelCase    (	self    :				List[str]     ,  A_    :				int	)					-> Optional[Any]:
      SCREAMING_SNAKE_CASE__     ,		SCREAMING_SNAKE_CASE__     ,		SCREAMING_SNAKE_CASE__					     =						tensor.shape
      SCREAMING_SNAKE_CASE__					     =						self.heads
      SCREAMING_SNAKE_CASE__					     =						tensor.reshape(A_     ,  A_     ,  A_     ,  dim // head_size	)
      SCREAMING_SNAKE_CASE__					     =						jnp.transpose(A_     ,  (0, 2, 1, 3)	)
      SCREAMING_SNAKE_CASE__					     =						tensor.reshape(batch_size * head_size     ,  A_     ,  dim // head_size	)
      return tensor



  def      _UpperCamelCase    (	self    :				Dict     ,  A_    :				Tuple	)					-> Optional[int]:
      SCREAMING_SNAKE_CASE__     ,		SCREAMING_SNAKE_CASE__     ,		SCREAMING_SNAKE_CASE__					     =						tensor.shape
      SCREAMING_SNAKE_CASE__					     =						self.heads
      SCREAMING_SNAKE_CASE__					     =						tensor.reshape(batch_size // head_size     ,  A_     ,  A_     ,  A_	)
      SCREAMING_SNAKE_CASE__					     =						jnp.transpose(A_     ,  (0, 2, 1, 3)	)
      SCREAMING_SNAKE_CASE__					     =						tensor.reshape(batch_size // head_size     ,  A_     ,  dim * head_size	)
      return tensor







  def __call__(	self    :				Tuple     ,  A_    :				int     ,  A_    :				str=None     ,  A_    :				Optional[Any]=True	)					-> str:
      SCREAMING_SNAKE_CASE__					     =						hidden_states if context is None else context

      SCREAMING_SNAKE_CASE__					     =						self.query(A_	)
      SCREAMING_SNAKE_CASE__					     =						self.key(A_	)
      SCREAMING_SNAKE_CASE__					     =						self.value(A_	)

      SCREAMING_SNAKE_CASE__					     =						self.reshape_heads_to_batch_dim(A_	)
      SCREAMING_SNAKE_CASE__					     =						self.reshape_heads_to_batch_dim(A_	)
      SCREAMING_SNAKE_CASE__					     =						self.reshape_heads_to_batch_dim(A_	)

      if self.use_memory_efficient_attention:
          SCREAMING_SNAKE_CASE__					     =						query_states.transpose(1     ,  0     ,  2	)
          SCREAMING_SNAKE_CASE__					     =						key_states.transpose(1     ,  0     ,  2	)
          SCREAMING_SNAKE_CASE__					     =						value_states.transpose(1     ,  0     ,  2	)

          # this if statement create a chunk size for each layer of the unet
          # the chunk size is equal to the query_length dimension of the deepest layer of the unet

          SCREAMING_SNAKE_CASE__					     =						query_states.shape[-3]
          if flatten_latent_dim % 64 == 0:
              SCREAMING_SNAKE_CASE__					     =						int(flatten_latent_dim / 64	)
          elif flatten_latent_dim % 16 == 0:
              SCREAMING_SNAKE_CASE__					     =						int(flatten_latent_dim / 16	)
          elif flatten_latent_dim % 4 == 0:
              SCREAMING_SNAKE_CASE__					     =						int(flatten_latent_dim / 4	)
          else:
              SCREAMING_SNAKE_CASE__					     =						int(A_	)

          SCREAMING_SNAKE_CASE__					     =						jax_memory_efficient_attention(
              A_     ,  A_     ,  A_     ,  query_chunk_size=A_     ,  key_chunk_size=4096 * 4	)

          SCREAMING_SNAKE_CASE__					     =						hidden_states.transpose(1     ,  0     ,  2	)
      else:
          # compute attentions
          SCREAMING_SNAKE_CASE__					     =						jnp.einsum("b i d, b j d->b i j"     ,  A_     ,  A_	)
          SCREAMING_SNAKE_CASE__					     =						attention_scores * self.scale
          SCREAMING_SNAKE_CASE__					     =						nn.softmax(A_     ,  axis=2	)

          # attend to values
          SCREAMING_SNAKE_CASE__					     =						jnp.einsum("b i j, b j d -> b i d"     ,  A_     ,  A_	)

      SCREAMING_SNAKE_CASE__					     =						self.reshape_batch_dim_to_heads(A_	)
      SCREAMING_SNAKE_CASE__					     =						self.proj_attn(A_	)
      return self.dropout_layer(A_     ,  deterministic=A_	)







class    UpperCamelCase_			(  nn.Module			):


  """simple docstring"""
  SCREAMING_SNAKE_CASE__				: int
  SCREAMING_SNAKE_CASE__				: int
  SCREAMING_SNAKE_CASE__				: int
  SCREAMING_SNAKE_CASE__				: float										=      0.0
  SCREAMING_SNAKE_CASE__				: bool										=      False
  SCREAMING_SNAKE_CASE__				: jnp.dtype										=      jnp.floataa
  SCREAMING_SNAKE_CASE__				: bool										=      False



  def      _UpperCamelCase    (	self    :				Optional[Any]	)					-> List[Any]:
      # self attention (or cross_attention if only_cross_attention is True)
      SCREAMING_SNAKE_CASE__					     =						FlaxAttention(
          self.dim     ,  self.n_heads     ,  self.d_head     ,  self.dropout     ,  self.use_memory_efficient_attention     ,  dtype=self.dtype	)
      # cross attention
      SCREAMING_SNAKE_CASE__					     =						FlaxAttention(
          self.dim     ,  self.n_heads     ,  self.d_head     ,  self.dropout     ,  self.use_memory_efficient_attention     ,  dtype=self.dtype	)
      SCREAMING_SNAKE_CASE__					     =						FlaxFeedForward(dim=self.dim     ,  dropout=self.dropout     ,  dtype=self.dtype	)
      SCREAMING_SNAKE_CASE__					     =						nn.LayerNorm(epsilon=1E-5     ,  dtype=self.dtype	)
      SCREAMING_SNAKE_CASE__					     =						nn.LayerNorm(epsilon=1E-5     ,  dtype=self.dtype	)
      SCREAMING_SNAKE_CASE__					     =						nn.LayerNorm(epsilon=1E-5     ,  dtype=self.dtype	)
      SCREAMING_SNAKE_CASE__					     =						nn.Dropout(rate=self.dropout	)







  def __call__(	self    :				Any     ,  A_    :				List[str]     ,  A_    :				Union[str, Any]     ,  A_    :				List[str]=True	)					-> List[str]:
      # self attention
      SCREAMING_SNAKE_CASE__					     =						hidden_states
      if self.only_cross_attention:
          SCREAMING_SNAKE_CASE__					     =						self.attna(self.norma(A_	)     ,  A_     ,  deterministic=A_	)
      else:
          SCREAMING_SNAKE_CASE__					     =						self.attna(self.norma(A_	)     ,  deterministic=A_	)
      SCREAMING_SNAKE_CASE__					     =						hidden_states + residual

      # cross attention
      SCREAMING_SNAKE_CASE__					     =						hidden_states
      SCREAMING_SNAKE_CASE__					     =						self.attna(self.norma(A_	)     ,  A_     ,  deterministic=A_	)
      SCREAMING_SNAKE_CASE__					     =						hidden_states + residual

      # feed forward
      SCREAMING_SNAKE_CASE__					     =						hidden_states
      SCREAMING_SNAKE_CASE__					     =						self.ff(self.norma(A_	)     ,  deterministic=A_	)
      SCREAMING_SNAKE_CASE__					     =						hidden_states + residual

      return self.dropout_layer(A_     ,  deterministic=A_	)







class    UpperCamelCase_			(  nn.Module			):


  """simple docstring"""
  SCREAMING_SNAKE_CASE__				: int
  SCREAMING_SNAKE_CASE__				: int
  SCREAMING_SNAKE_CASE__				: int
  SCREAMING_SNAKE_CASE__				: int										=      1
  SCREAMING_SNAKE_CASE__				: float										=      0.0
  SCREAMING_SNAKE_CASE__				: bool										=      False
  SCREAMING_SNAKE_CASE__				: bool										=      False
  SCREAMING_SNAKE_CASE__				: jnp.dtype										=      jnp.floataa
  SCREAMING_SNAKE_CASE__				: bool										=      False



  def      _UpperCamelCase    (	self    :				int	)					-> List[Any]:
      SCREAMING_SNAKE_CASE__					     =						nn.GroupNorm(num_groups=32     ,  epsilon=1E-5	)

      SCREAMING_SNAKE_CASE__					     =						self.n_heads * self.d_head
      if self.use_linear_projection:
          SCREAMING_SNAKE_CASE__					     =						nn.Dense(A_     ,  dtype=self.dtype	)
      else:
          SCREAMING_SNAKE_CASE__					     =						nn.Conv(
              A_     ,  kernel_size=(1, 1)     ,  strides=(1, 1)     ,  padding="VALID"     ,  dtype=self.dtype     ,  )

      SCREAMING_SNAKE_CASE__					     =						[
          FlaxBasicTransformerBlock(
              A_     ,  self.n_heads     ,  self.d_head     ,  dropout=self.dropout     ,  only_cross_attention=self.only_cross_attention     ,  dtype=self.dtype     ,  use_memory_efficient_attention=self.use_memory_efficient_attention     ,  )
          for _ in range(self.depth	)
      ]

      if self.use_linear_projection:
          SCREAMING_SNAKE_CASE__					     =						nn.Dense(A_     ,  dtype=self.dtype	)
      else:
          SCREAMING_SNAKE_CASE__					     =						nn.Conv(
              A_     ,  kernel_size=(1, 1)     ,  strides=(1, 1)     ,  padding="VALID"     ,  dtype=self.dtype     ,  )

      SCREAMING_SNAKE_CASE__					     =						nn.Dropout(rate=self.dropout	)







  def __call__(	self    :				Optional[Any]     ,  A_    :				str     ,  A_    :				str     ,  A_    :				Optional[int]=True	)					-> List[Any]:
      SCREAMING_SNAKE_CASE__     ,		SCREAMING_SNAKE_CASE__     ,		SCREAMING_SNAKE_CASE__     ,		SCREAMING_SNAKE_CASE__					     =						hidden_states.shape
      SCREAMING_SNAKE_CASE__					     =						hidden_states
      SCREAMING_SNAKE_CASE__					     =						self.norm(A_	)
      if self.use_linear_projection:
          SCREAMING_SNAKE_CASE__					     =						hidden_states.reshape(A_     ,  height * width     ,  A_	)
          SCREAMING_SNAKE_CASE__					     =						self.proj_in(A_	)
      else:
          SCREAMING_SNAKE_CASE__					     =						self.proj_in(A_	)
          SCREAMING_SNAKE_CASE__					     =						hidden_states.reshape(A_     ,  height * width     ,  A_	)

      for transformer_block in self.transformer_blocks:
          SCREAMING_SNAKE_CASE__					     =						transformer_block(A_     ,  A_     ,  deterministic=A_	)

      if self.use_linear_projection:
          SCREAMING_SNAKE_CASE__					     =						self.proj_out(A_	)
          SCREAMING_SNAKE_CASE__					     =						hidden_states.reshape(A_     ,  A_     ,  A_     ,  A_	)
      else:
          SCREAMING_SNAKE_CASE__					     =						hidden_states.reshape(A_     ,  A_     ,  A_     ,  A_	)
          SCREAMING_SNAKE_CASE__					     =						self.proj_out(A_	)

      SCREAMING_SNAKE_CASE__					     =						hidden_states + residual
      return self.dropout_layer(A_     ,  deterministic=A_	)







class    UpperCamelCase_			(  nn.Module			):


  """simple docstring"""
  SCREAMING_SNAKE_CASE__				: int
  SCREAMING_SNAKE_CASE__				: float										=      0.0
  SCREAMING_SNAKE_CASE__				: jnp.dtype										=      jnp.floataa



  def      _UpperCamelCase    (	self    :				Tuple	)					-> Optional[Any]:
      # The second linear layer needs to be called
      # net_2 for now to match the index of the Sequential layer
      SCREAMING_SNAKE_CASE__					     =						FlaxGEGLU(self.dim     ,  self.dropout     ,  self.dtype	)
      SCREAMING_SNAKE_CASE__					     =						nn.Dense(self.dim     ,  dtype=self.dtype	)







  def __call__(	self    :				str     ,  A_    :				str     ,  A_    :				List[str]=True	)					-> Union[str, Any]:
      SCREAMING_SNAKE_CASE__					     =						self.net_a(A_     ,  deterministic=A_	)
      SCREAMING_SNAKE_CASE__					     =						self.net_a(A_	)
      return hidden_states







class    UpperCamelCase_			(  nn.Module			):


  """simple docstring"""
  SCREAMING_SNAKE_CASE__				: int
  SCREAMING_SNAKE_CASE__				: float										=      0.0
  SCREAMING_SNAKE_CASE__				: jnp.dtype										=      jnp.floataa



  def      _UpperCamelCase    (	self    :				Dict	)					-> str:
      SCREAMING_SNAKE_CASE__					     =						self.dim * 4
      SCREAMING_SNAKE_CASE__					     =						nn.Dense(inner_dim * 2     ,  dtype=self.dtype	)
      SCREAMING_SNAKE_CASE__					     =						nn.Dropout(rate=self.dropout	)







  def __call__(	self    :				Dict     ,  A_    :				int     ,  A_    :				Optional[Any]=True	)					-> Union[str, Any]:
      SCREAMING_SNAKE_CASE__					     =						self.proj(A_	)
      SCREAMING_SNAKE_CASE__     ,		SCREAMING_SNAKE_CASE__					     =						jnp.split(A_     ,  2     ,  axis=2	)
      return self.dropout_layer(hidden_linear * nn.gelu(A_	)     ,  deterministic=A_	)





def 							__a       (					__A ,       __A ,       __A	):
    SCREAMING_SNAKE_CASE__					     =						index
    SCREAMING_SNAKE_CASE__					     =						2 * index + 1
    SCREAMING_SNAKE_CASE__					     =						2 * index + 2
    if left_index < heap_size and unsorted[left_index] > unsorted[largest]:
        SCREAMING_SNAKE_CASE__					     =						left_index

    if right_index < heap_size and unsorted[right_index] > unsorted[largest]:
        SCREAMING_SNAKE_CASE__					     =						right_index

    if largest != index:
        SCREAMING_SNAKE_CASE__     ,		SCREAMING_SNAKE_CASE__					     =						unsorted[index], unsorted[largest]
        heapify(__A ,       __A ,       __A	)





def 							__a       (					__A	):
    SCREAMING_SNAKE_CASE__					     =						len(__A	)
    for i in range(n // 2 - 1 ,       -1 ,       -1	):
        heapify(__A ,       __A ,       __A	)
    for i in range(n - 1 ,       0 ,       -1	):
        SCREAMING_SNAKE_CASE__     ,		SCREAMING_SNAKE_CASE__					     =						unsorted[i], unsorted[0]
        heapify(__A ,       0 ,       __A	)
    return unsorted


if __name__ == "__main__":
  A__        =			input("Enter numbers separated by a comma:\n").strip()
  A__        =			[int(item) for item in user_input.split(",")]
  print(heap_sort(unsorted))


