def simplify(seq, drop_zero):
    """
    Combine coefficients of terms with the same variable and optionally drop
    zero weights and sum up terms without a variable.
    """
    elements = []
    seen = {}
    rhs = 0
    for co, var in seq:
        if co == 0:
            continue

        if var is None:
            rhs -= co
        elif var not in seen:
            seen[var] = len(elements)
            elements.append((co, var))
        else:
            co_old, var_old = elements[seen[var]]
            assert var_old == var
            elements[seen[var]] = (co_old + co, var)

    if drop_zero:
        elements = [(co, var) for co, var in elements if co != 0]

    return rhs, elements