Benny and Some Magic

3.2

12 votes
Algorithms, Approved, Depth First Search, Graphs, Medium
Problem

Benny appeared in a Magic labyrinth. Labyrinth consists of rooms and doors. Doors are unidirectional, they can be used only in one direction. There are even doors from the room to itself. When passing through the door, the master of the labyrinth gives Benny a coin.

Benny can move through the doors as much as she wants. But the only goal of the game is to get the score as high as possible. If Benny doesn't use any door, her score is 0. Let the maximum value of coin earned by Benny is p and minimum value of coin earned by Benny is q, then the score is pq. Help Benny to learn, what is the maximum score she can achieve.

Input format

First line contains two integers n and m — number of rooms in Magic labyrinth and number of doors.

Next m lines contain description of doors. Each line contains three integers v, u and w, describing a door that can be used to go from room v to room u getting coin of value w.

Constraints

2n3105
1m3105
1v,un
0w109

Output format

Output single integer: maximum score which Benny can achieve.

Sample Input
6 8
1 2 9
2 3 7
3 1 1
2 4 5
3 4 4
4 5 6
5 6 3
6 4 10
Sample Output
9
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

One of the optimal paths is: 23124564. Which gives score equal to 101=9.

Editor Image

?