#K66062. Maximum Shelf Book Value

    ID: 32337 Type: Default 1000ms 256MiB

Maximum Shelf Book Value

Maximum Shelf Book Value

You are given M distinct categories and K books. Each book has three attributes: its category number, a unique book index, and a value. Bob wants to display one book from each category on his shelf in order to maximize the total value of the shelf.

For each category, he can only choose one book – the one with the maximum value among all books in that category. Your task is to compute the maximum possible total value achieved by selecting the highest valued book from every category that has at least one book.

Input Format:

  • The first line contains two integers M and K, representing the number of categories and the number of books respectively.
  • The next K lines each contain three integers: the category number, the book index, and the value of the book.

Output Format:

  • Output a single integer, which is the maximum total value of books that can be displayed on the shelf.

Constraints:

  • 1 ≤ M, K ≤ 105
  • Book values are positive integers.

Note: It is guaranteed that the input will have at least one book for some categories.

Mathematical Formulation:

If we denote by \( B_i \) the set of book values in category \( i \), the answer is:

\[ \text{answer} = \sum_{i \in \{categories \, with\, books\}} \max_{v \in B_i} v \]

inputFormat

The input is read from standard input (stdin) and contains:

  1. The first line contains two space-separated integers: M (the number of categories) and K (the number of books).
  2. The next K lines each contain three space-separated integers: the category number, the book index, and the value of the book.

outputFormat

Output a single integer to the standard output (stdout) representing the maximum total value of the books that can be displayed on the shelf.

## sample
3 5
1 1 10
1 2 15
2 3 10
3 4 30
3 5 25
55