Presents in Bankopolis
Contest: Tinkoff Challenge - Elimination Round
Difficulty rating: 2100
Tags: dp, graphs, shortest paths

Bankopolis is an incredible city in which all the n crossroads are located on a straight line and numbered from 1 to n along it. On each crossroad there is a bank office.

The crossroads are connected with m oriented bicycle lanes (the i-th lane goes from crossroad ui to crossroad vi), the difficulty of each of the lanes is known.

Oleg the bank client wants to gift happiness and joy to the bank employees. He wants to visit exactly k offices, in each of them he wants to gift presents to the employees.

The problem is that Oleg don't want to see the reaction on his gifts, so he can't use a bicycle lane which passes near the office in which he has already presented his gifts (formally, the i-th lane passes near the office on the x-th crossroad if and only if min(ui, vi) < x < max(ui, vi))). Of course, in each of the offices Oleg can present gifts exactly once. Oleg is going to use exactly k - 1 bicycle lane to move between offices. Oleg can start his path from any office and finish it in any office.

Oleg wants to choose such a path among possible ones that the total difficulty of the lanes he will use is minimum possible. Find this minimum possible total difficulty.

Input format:
The first line contains two integers n and k (1 ≤ n, k ≤ 80) — the number of crossroads (and offices) and the number of offices Oleg wants to visit.

The second line contains single integer m (0 ≤ m ≤ 2000) — the number of bicycle lanes in Bankopolis.

The next m lines contain information about the lanes.

The i-th of these lines contains three integers ui, vi and ci (1 ≤ ui, vi ≤ n, 1 ≤ ci ≤ 1000), denoting the crossroads connected by the i-th road and its difficulty.

Output format:
In the only line print the minimum possible total difficulty of the lanes in a valid path, or -1 if there are no valid paths.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first example Oleg visiting banks by path 1 → 6 → 2 → 4.

Path 1 → 6 → 2 → 7 with smaller difficulity is incorrect because crossroad 2 → 7 passes near already visited office on the crossroad 6.

In the second example Oleg can visit banks by path 4 → 1 → 3.

Example input:
7 4
4
1 6 2
6 2 2
2 4 2
2 7 1

Example output:
6

Example input:
4 3
4
2 1 2
1 3 2
3 4 2
4 1 1

Example output:
3

Editorial:
At any time Oleg can make correct move to crossroad from two segments: from crossroad v he can go to crossroads from segment [a;v - 1] or from segment [v + 1;b].
After we go from crossroad v to some crossroad u from segment [a;v - 1] there appear two new segments: [a;u - 1] and [u + 1;v - 1].
If we go from crossroad v to some crossroad u from segment [v + 1;b], then two new segments will be [v + 1;u - 1] and [u + 1;b].
Then you can think about dp solution like dp[v][a][b][cnt], and try each to, so a ≤ to ≤ b and recalc dp[to][a][v - 1][cnt + 1], dp[to][v + 1][b][cnt + 1].
Time complexity: O((n + m) * n2 * k).
But we can optimize this solution:
Notice that we don't need to know b if we want to go to the left and we don't need to know a if we want to go to the right.
Then we can make dp[v][go][cnt].
If v < go then we have a segment [v + 1;go], else [go;v - 1].
Then if v < go we need to recalc dp[to][v + 1][cnt + 1] and dp[to][go][cnt + 1], otherwise dp[to][go][cnt + 1] and dp[to][v - 1][cnt + 1].
Time complexity: O((n + m) * n * k).