Under construction

Still being built. Things may change

Yeongil Land

Interview

Time limit1sMemory limit1024 MB

Summary
Find the longest directed route from the gate that rides each of N rides (N up to 9) exactly once and returns to the gate.
Level

Medium5 of 10

Topics
Dynamic programming, Bit manipulation, Graph
Solved
No attempts yet

Problem

Yeongil Land is a theme park made up of one main gate and NN rides, each with an identification number. The gate is numbered 0, and the rides are numbered 1 through NN. Directed edges connect the gate and rides, or one ride and another. Two places may be joined by several edges, and an edge may start and end at the same place.

Jeongmin visits Yeongil Land, starts at the gate, and wants to know the longest time of a route that rides every ride exactly once and returns to the gate. The rides are so fascinating that Jeongmin cannot pass one without riding it, so each ride can be reached at most once. Also, the gate cannot be visited until every ride has been ridden, and ride time is ignored.

Find the longest time for the curious Jeongmin.

Input

The first line contains the number of rides NN. (1≤N≤91 \leq N \leq 9)

The second line contains the number of edges MM. (0≤M≤10 0000 \leq M \leq 10\,000)

Each of the next MM lines contains uiu_i, viv_i, and did_i, separated by spaces. This is an edge from the gate or ride uiu_i to the gate or ride viv_i, and it takes did_i to travel. The sum of all travel times is less than 2312^{31}. (0≤ui,vi≤N0 \leq u_i, v_i \leq N; 1≤di≤231−11 \leq d_i \leq 2^{31}-1)

Output

Print the longest time of a route that starts at the gate, rides every ride exactly once, and returns to the gate. If no such route exists, print -1.

Examples2

  1. Example 1

    Input
    2
    8
    0 1 18
    0 2 20
    0 2 15
    1 0 10
    1 2 15
    2 0 13
    2 2 10
    2 1 5
    
    Expected output
    46
    
  2. Example 2

    Input
    2
    4
    0 1 18
    0 2 20
    1 0 10
    1 2 15
    
    Expected output
    -1