def calculate_score(points, max_bytes, total_buttons, buttons, bytes):
    """Calculates the score for a level.

    points: the points assigned for solving the level (based on difficulty)
    max_bytes: the maximum number of bytes for the level
    total_buttons: the number of white buttons on the level
    buttons: the number of white buttons pressed
    bytes: the number of bytes actually used
    """
    if buttons == total_buttons and bytes <= max_bytes:
        return points * max_bytes // bytes
    assert buttons < total_buttons or bytes > max_bytes
    points_per_button = 0
    if bytes <= max_bytes:
        points_per_button = points // (2 * total_buttons)
    if max_bytes < bytes <= 2 * max_bytes:
        points_per_button = points * (2 * max_bytes - bytes) // (2 *
            max_bytes * total_buttons)
    return buttons * points_per_button
