
"""simple docstring"""






import gc
import unittest

import numpy as np
import torch

from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNetaDModel
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

from ..pipeline_params import UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS, UNCONDITIONAL_AUDIO_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()





class 				a__					(						A__   ,							unittest.TestCase       ):
     UpperCAmelCase__         =      DanceDiffusionPipeline
     UpperCAmelCase__         =      UNCONDITIONAL_AUDIO_GENERATION_PARAMS
     UpperCAmelCase__         =      PipelineTesterMixin.required_optional_params - {
         '''callback''',
         '''latents''',
         '''callback_steps''',
         '''output_type''',
         '''num_images_per_prompt''',
     }
     UpperCAmelCase__         =      UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS
     UpperCAmelCase__         =      False
     UpperCAmelCase__         =      False



     def  lowerCamelCase_						(							self :Union[str, Any]   ):







      '''simple docstring'''



      torch.manual_seed(0   )
      UpperCamelCase_     :					Dict   	=UNetaDModel(
          block_out_channels=(32, 32, 64)      ,  extra_in_channels=16      ,  sample_size=512      ,  sample_rate=16_000      ,  in_channels=2      ,  out_channels=2      ,  flip_sin_to_cos=_lowerCamelCase      ,  use_timestep_embedding=_lowerCamelCase      ,  time_embedding_type='fourier'      ,  mid_block_type='UNetMidBlock1D'      ,  down_block_types=('DownBlock1DNoSkip', 'DownBlock1D', 'AttnDownBlock1D')      ,  up_block_types=('AttnUpBlock1D', 'UpBlock1D', 'UpBlock1DNoSkip')      ,  )
      UpperCamelCase_     :					List[Any]   	=IPNDMScheduler()

      UpperCamelCase_     :					Dict   	={
          'unet': unet,
          'scheduler': scheduler,
      }
      return components



     def  lowerCamelCase_						(							self :List[str]      ,  _lowerCamelCase :Any      ,  _lowerCamelCase :List[Any]=0   ):







      '''simple docstring'''



      if str(_lowerCamelCase   ).startswith('mps'   ):
       UpperCamelCase_     :					Dict   	=torch.manual_seed(_lowerCamelCase   )
      else:
       UpperCamelCase_     :					Optional[int]   	=torch.Generator(device=_lowerCamelCase   ).manual_seed(_lowerCamelCase   )
      UpperCamelCase_     :					Union[str, Any]   	={
          'batch_size': 1,
          'generator': generator,
          'num_inference_steps': 4,
      }
      return inputs



     def  lowerCamelCase_						(							self :Dict   ):







      '''simple docstring'''



      UpperCamelCase_     :					List[str]   	='cpu'  # ensure determinism for the device-dependent torch.Generator
      UpperCamelCase_     :					Optional[int]   	=self.get_dummy_components()
      UpperCamelCase_     :					List[Any]   	=DanceDiffusionPipeline(**_lowerCamelCase   )
      UpperCamelCase_     :					Tuple   	=pipe.to(_lowerCamelCase   )
      pipe.set_progress_bar_config(disable=_lowerCamelCase   )

      UpperCamelCase_     :					List[str]   	=self.get_dummy_inputs(_lowerCamelCase   )
      UpperCamelCase_     :					Tuple   	=pipe(**_lowerCamelCase   )
      UpperCamelCase_     :					Union[str, Any]   	=output.audios

      UpperCamelCase_     :					Tuple   	=audio[0, -3:, -3:]

      assert audio.shape == (1, 2, components["unet"].sample_size)
      UpperCamelCase_     :					Tuple   	=np.array([-0.7265, 1.0000, -0.8388, 0.1175, 0.9498, -1.0000]   )
      assert np.abs(audio_slice.flatten() - expected_slice   ).max() < 1E-2



     @skip_mps
     def  lowerCamelCase_						(							self :Any   ):







      '''simple docstring'''



      return super().test_save_load_local()



     @skip_mps
     def  lowerCamelCase_						(							self :Any   ):







      '''simple docstring'''



      return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3E-3   )



     @skip_mps
     def  lowerCamelCase_						(							self :str   ):







      '''simple docstring'''



      return super().test_save_load_optional_components()



     @skip_mps
     def  lowerCamelCase_						(							self :Optional[int]   ):







      '''simple docstring'''



      return super().test_attention_slicing_forward_pass()




     def  lowerCamelCase_						(							self :List[Any]   ):







      '''simple docstring'''



      super().test_inference_batch_single_identical(expected_max_diff=3E-3   )





@slow
@require_torch_gpu
class 				a__					(						unittest.TestCase       ):



     def  lowerCamelCase_						(							self :List[str]   ):







      '''simple docstring'''



      super().tearDown()
      gc.collect()
      torch.cuda.empty_cache()



     def  lowerCamelCase_						(							self :Dict   ):







      '''simple docstring'''



      UpperCamelCase_     :					str   	=torch_device

      UpperCamelCase_     :					List[Any]   	=DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k'   )
      UpperCamelCase_     :					List[str]   	=pipe.to(_lowerCamelCase   )
      pipe.set_progress_bar_config(disable=_lowerCamelCase   )

      UpperCamelCase_     :					Optional[int]   	=torch.manual_seed(0   )
      UpperCamelCase_     :					Tuple   	=pipe(generator=_lowerCamelCase      ,  num_inference_steps=100      ,  audio_length_in_s=4.096   )
      UpperCamelCase_     :					Tuple   	=output.audios

      UpperCamelCase_     :					Optional[int]   	=audio[0, -3:, -3:]

      assert audio.shape == (1, 2, pipe.unet.sample_size)
      UpperCamelCase_     :					Dict   	=np.array([-0.0192, -0.0231, -0.0318, -0.0059, 0.0002, -0.0020]   )

      assert np.abs(audio_slice.flatten() - expected_slice   ).max() < 1E-2




     def  lowerCamelCase_						(							self :Any   ):







      '''simple docstring'''



      UpperCamelCase_     :					List[str]   	=torch_device

      UpperCamelCase_     :					List[str]   	=DanceDiffusionPipeline.from_pretrained('harmonai/maestro-150k'      ,  torch_dtype=torch.floataa   )
      UpperCamelCase_     :					Union[str, Any]   	=pipe.to(_lowerCamelCase   )
      pipe.set_progress_bar_config(disable=_lowerCamelCase   )

      UpperCamelCase_     :					Union[str, Any]   	=torch.manual_seed(0   )
      UpperCamelCase_     :					str   	=pipe(generator=_lowerCamelCase      ,  num_inference_steps=100      ,  audio_length_in_s=4.096   )
      UpperCamelCase_     :					str   	=output.audios

      UpperCamelCase_     :					Optional[Any]   	=audio[0, -3:, -3:]

      assert audio.shape == (1, 2, pipe.unet.sample_size)
      UpperCamelCase_     :					List[Any]   	=np.array([-0.0367, -0.0488, -0.0771, -0.0525, -0.0444, -0.0341]   )

      assert np.abs(audio_slice.flatten() - expected_slice   ).max() < 1E-2


"""simple docstring"""






def       A_  (				__lowercase						,	__lowercase						,	__lowercase	):
 if len(__lowercase	) != len(__lowercase	):
  raise ValueError('The length of profit and weight must be same.'	)
 if max_weight <= 0:
  raise ValueError('max_weight must greater than zero.'	)
 if any(p < 0 for p in profit	):
  raise ValueError('Profit can not be negative.'	)
 if any(w < 0 for w in weight	):
  raise ValueError('Weight can not be negative.'	)

 # List created to store profit gained for the 1kg in case of each weight
 # respectively.  Calculate and append profit/weight for each element.
 UpperCamelCase_     :					int   	=[p / w for p, w in zip(__lowercase						,	__lowercase	)]

 # Creating a copy of the list and sorting profit/weight in ascending order
 UpperCamelCase_     :					Optional[int]   	=sorted(__lowercase	)

 # declaring useful variables
 UpperCamelCase_     :					Optional[int]   	=len(__lowercase	)
 UpperCamelCase_     :					Optional[int]   	=0
 UpperCamelCase_     :					List[Any]   	=0
 UpperCamelCase_     :					Optional[int]   	=0

 # loop till the total weight do not reach max limit e.g. 15 kg and till i<length
 while limit <= max_weight and i < length:
  # flag value for encountered greatest element in sorted_profit_by_weight
  UpperCamelCase_     :					List[str]   	=sorted_profit_by_weight[length - i - 1]
  UpperCamelCase_     :					Optional[Any]   	=profit_by_weight.index(__lowercase	)
  UpperCamelCase_     :					str   	=-1

  # check if the weight encountered is less than the total weight
  # encountered before.
  if max_weight - limit >= weight[index]:
   limit += weight[index]
   # Adding profit gained for the given weight 1 ===
   # weight[index]/weight[index]
   gain += 1 * profit[index]
  else:
   # Since the weight encountered is greater than limit, therefore take the
   # required number of remaining kgs and calculate profit for it.
   # weight remaining / weight[index]
   gain += (max_weight - limit) / weight[index] * profit[index]
   break
  i += 1
 return gain


if __name__ == "__main__":
 print(
     'Input profits, weights, and then max_weight (all positive ints) separated by '
     'spaces.'
 )

 __SCREAMING_SNAKE_CASE							=     [int(x) for x in input('Input profits separated by spaces: ').split()]
 __SCREAMING_SNAKE_CASE							=     [int(x) for x in input('Input weights separated by spaces: ').split()]
 __SCREAMING_SNAKE_CASE							=     int(input('Max weight allowed: '))

 # Function Call
 calc_profit(profit, weight, max_weight)
