def match_duration(produced_tokens, produced_duration, produced_f0, target_duration):
    """
    >>> tokens = ['t'] * 4
    >>> F0s    = ['f0'] * 4
    >>> produced_duration = [1, 10, 10, 10]
    >>> match_duration(tokens, produced_duration, F0s, target_duration=100)
    (['t', 't', 't', 't'], [1, 10, 10, 10], ['f0', 'f0', 'f0', 'f0'])
    >>> match_duration(tokens, produced_duration, F0s, target_duration=5)
    (['t', 't'], [1, 4], ['f0', 'f0'])
    """
    if sum(produced_duration) <= target_duration:
        return produced_tokens, produced_duration, produced_f0

    running_duration = 0
    filtered_duration = []

    for next_tok_duration in produced_duration:
        if running_duration + next_tok_duration < target_duration:
            filtered_duration.append(next_tok_duration)
            running_duration += next_tok_duration
        else:
            to_add = target_duration - running_duration
            assert to_add <= next_tok_duration
            filtered_duration.append(to_add)
            break

    produced_duration = filtered_duration
    assert sum(produced_duration) == target_duration

    n_tok = len(filtered_duration)

    return produced_tokens[:n_tok], produced_duration, produced_f0[:n_tok]