#K46567. Maximum Magical Power Harvest

    ID: 28005 Type: Default 1000ms 256MiB

Maximum Magical Power Harvest

Maximum Magical Power Harvest

You are given two types of fruits: sun fruits and moon fruits. There are (n) sun fruits numbered from (0) to (n-1) and (m) moon fruits numbered from (0) to (m-1). You are also provided with (k) fruit pairs. Each fruit pair is represented by a triple ((a_i, b_i, p_i)) indicating that if sun fruit (a_i) is paired with moon fruit (b_i), a magical power of (p_i) will be harvested. Each fruit (both sun and moon) can only be used in at most one pairing. Your task is to determine the maximum total magical power that can be harvested by selecting a set of valid pairings.

Formally, select a set (S) of pairs such that no two pairs share the same sun fruit or the same moon fruit, and maximize (\sum_{(a,b,p) \in S} p).

inputFormat

The first line of input contains three integers (n), (m), and (k) representing the number of sun fruits, moon fruits, and fruit pairs respectively.

Each of the following (k) lines contains three integers (a_i), (b_i), and (p_i) describing a possible pairing between sun fruit (a_i) and moon fruit (b_i) with magical power (p_i).

outputFormat

Output a single integer which is the maximum total magical power that can be harvested by optimally pairing the fruits.## sample

3 3 4
0 0 10
0 1 15
1 0 20
1 2 25
40