input: Write a function to implement a genetic algorithm for optimizing a given function.
  assert genetic_algorithm([1, 2, 3, 4, 5], 10, 10, 100) == ([5], 7)
  assert genetic_algorithm([1, 2, 3], 5, 5, 50) == ([3], 5)
  assert genetic_algorithm([2, 4, 6], 6, 8, 80) == ([6], 8)
output: import random
  
  def genetic_algorithm(values, population_size, generations, target_sum):
      def fitness(individual):
          return abs(target_sum - sum(individual))
  
      def mutate(individual):
          index = random.randint(0, len(individual) - 1)
          individual[index] = random.choice(values)
          return individual
  
      def crossover(parent1, parent2):
          index = random.randint(1, len(parent1) - 1)
          child1 = parent1[:index] + parent2[index:]
          child2 = parent2[:index] + parent1[index:]
          return child1, child2
  
      # Initialize population
      population = [[random.choice(values)] for _ in range(population_size)]
      
      for _ in range(generations):
          # Evaluate fitness
          population = sorted(population, key=fitness)
          
          # Select the best individuals
          population = population[:population_size // 2]
          
          # Create new population through crossover
          new_population = []
          while len(new_population) < population_size:
              parents = random.sample(population, 2)
              child1, child2 = crossover(parents[0], parents[1])
              new_population.extend([child1, child2])
          
          # Mutate individuals
          population = [mutate(ind) for ind in new_population]
      
      # Return the best individual
      best_individual = min(population, key=fitness)
      return best_individual, fitness(best_individual)