from functools import reduce


def get_gpu_distribution(runs, available):
    """
    Finds how to distribute the available runs to perform the given number of runs.
    
    Parameters
    ----------
    runs : int
        number of reconstruction requested
        
    available : list
        list of available runs aligned with the GPU id list
        
    Returns
    -------
    distributed : list
        list of runs aligned with the GPU id list, the runs are equally distributed across the GPUs
    """
    all_avail = reduce((lambda x,y: x+y), available)
    distributed = [0] * len(available)
    sum_distr = 0
    while runs > sum_distr and all_avail > 0:
       # balance distribution
       for i in range(len(available)):
          if available[i] > 0:
             available[i] -= 1
             all_avail -= 1
             distributed[i] += 1
             sum_distr += 1
             if sum_distr == runs:
                break
    return distributed