package main import ( "fmt" "strconv" ) func scan() (N int, M int, scoreMap map[string]int) { scoreMap = map[string]int{} fmt.Scan(&N, &M) for i := 0; i < M; i++ { var item1, item2 string var score int fmt.Scan(&item1, &item2, &score) key := item1 + "," + item2 scoreMap[key] = score } return } func permutaions(arr []int) (res [][]int) { var helper func([]int, int) helper = func(arr []int, n int) { if n == 1 { tmp := make([]int, len(arr)) copy(tmp, arr) res = append(res, tmp) } else { for i := 0; i < n; i++ { helper(arr, n-1) if n%2 == 1 { tmp := arr[i] arr[i] = arr[n-1] arr[n-1] = tmp } else { tmp := arr[0] arr[0] = arr[n-1] arr[n-1] = tmp } } } } helper(arr, len(arr)) return res } func main() { N, _, scoreMap := scan() // N, _, scoreMap := 4, 9, map[string]int{ // "0,1": 1, // "0,2": 2, // "0,3": 3, // "1,2": 4, // "1,3": 5, // "2,3": 6, // "3,2": 100, // "2,1": 100, // "1,0": 100, // } var items []int for i := 0; i < N; i++ { items = append(items, i) } perms := permutaions(items) var maxScore int for _, per := range perms { tmpMaxScore := 0 for i := 1; i < len(per); i++ { key := strconv.Itoa(per[i-1]) + "," + strconv.Itoa(per[i]) tmpMaxScore += scoreMap[key] } if tmpMaxScore > maxScore { maxScore = tmpMaxScore } } fmt.Print(maxScore) }