def print_solution(model, solver):
    """Prints the solution associated with solver.

  If solver has already had Solve() called on it, prints the solution. This
  includes each variable and its assignment, along with the objective function
  and its optimal value.
  If solver has not had Solve() called on it, or there is no feasible solution,
  this will probably crash.

  Args:
    model: A pywrapcp.CpModel object.
    solver: A pywrapcp.CpSolver object.

  Returns:
    Nothing, but prints the solution associated with solver.
  """
    model_proto = model.Proto()
    response_proto = solver.ResponseProto()
    variables_in_objective_map = {}
    maximization = False
    if model_proto.HasField('objective'):
        objective = model_proto.objective
        for i in range(len(objective.vars)):
            variables_in_objective_map[objective.vars[i]] = objective.coeffs[i]
        if objective.scaling_factor < 0.0:
            maximization = True
    variable_assignments = []
    variables_in_objective = []
    num_vars = len(model_proto.variables)
    for var_index in range(num_vars):
        if not model_proto.variables[var_index].name:
            continue
        variable_name = model_proto.variables[var_index].name
        if var_index in variables_in_objective_map:
            coefficient = variables_in_objective_map[var_index]
            if coefficient:
                if maximization:
                    coefficient *= -1
                if coefficient < 0:
                    variables_in_objective.append(' - {} * {}'.format(-
                        coefficient, variable_name))
                elif coefficient > 0:
                    variables_in_objective.append(' + {} * {}'.format(
                        coefficient, variable_name))
        variable_assignments.append('  {} = {}\n'.format(variable_name,
            response_proto.solution[var_index]))
    print(''.join(variable_assignments), end='')
    if variables_in_objective and variables_in_objective[0][1] == '+':
        variables_in_objective[0] = variables_in_objective[0][2:]
    print('{}:{}'.format('Maximize' if maximization else 'Minimize', ''.
        join(variables_in_objective)))
    print('Objective value: {}\n'.format(solver.ObjectiveValue()))
