Under construction

Still being built. Things may change

Love Revolution

Interview

Time limit1sMemory limit1024 MB

Summary
Remove the cheapest set of unestablished relationships so that no cycle remains, keeping all established ones, and report the minimum total affection lost.
Level

Medium4 of 10

Topics
Minimum spanning tree, Greedy, Union-find
Solved
No attempts yet

Problem

Kiryeong, the student council president of Sinchon High School, has been worried about the tangled romances among students lately. Students cannot focus on their studies, so overall grades are falling. To ease their troubled minds, Kiryeong decided to take drastic action and force some of the romantic relationships to be given up. To minimize the fallout, the action must satisfy the following conditions: (1) A relationship that has already been established is not to be given up. (2) The romantic relationships must not form a KK-gon. A KK-gon (K≥3K \geq 3) means that for any KK distinct students A1,A2,⋯ ,AKA_1, A_2, \cdots, A_K, there is a romantic relationship between Ai−1A_{i-1} and AiA_i for every i≠1i \neq 1, and also between AKA_K and A1A_1. (3) If there are several ways to give up relationships, minimize the sum of the affection levels of the relationships given up. However, the number of students is very large, so it is hard to find a way that satisfies these conditions. When the conditions are satisfied, find the minimum sum of the affection levels of the relationships given up.

Input

The first line contains the number of students N(3≤N≤10 000)N(3 \leq N \leq 10\ 000) and the number of romantic relationships M(N≤M≤100 000)M(N \leq M \leq 100\ 000). The next MM lines each contain three integers aia_i, bib_i, cic_i and a status did_i, separated by spaces. (1≤ai<bi≤N;1≤ci≤10 000)(1 \leq a_i < b_i \leq N; 1 \leq c_i \leq 10\ 000) Students aia_i and bib_i have a relationship with affection level cic_i. If did_i is 1, the relationship is already established. If did_i is 0, it is not. Any two students are connected by one or more relationships. No pair of students has more than one relationship. No KK-gon made only of established relationships exists.

Output

Print the minimum sum of the affection levels of the relationships that are given up.

Examples2

  1. Example 1

    Input
    5 7
    1 3 10 0
    2 4 7 0
    1 2 6 0
    1 5 8 0
    2 5 13 1
    3 4 2 1
    4 5 6 0
    
    Expected output
    19
    
  2. Example 2

    Input
    4 6
    1 2 4 0
    1 4 1 0
    3 4 2 0
    2 3 5 0
    1 3 6 0
    2 4 3 0
    
    Expected output
    7