package main import ( "fmt" ) func main() { type Score struct { i1, i2, s int } var n, m, i1, i2, s int _, _ = fmt.Scan(&n, &m) scores := make(map[int]Score) for i := 0; i < m; i++ { _, _ = fmt.Scan(&i1, &i2, &s) scores[i1*10+i2] = Score{i1, i2, s} } list := []int{0} total := 0 for i := 1; i < n; i++ { // 入れる数字 max := 0 maxJ := 0 for j := 0; j <= len(list); j++ { // 入れる位置 score := 0 for k := 0; k < len(list); k++ { // 入れた場合に得られるスコア計算 if k < j { score += scores[list[k]*10+i].s } else { score += scores[i*10+list[k]].s } } if max < score { max = score maxJ = j } } fmt.Println(list, i, max, maxJ, total) total += max switch maxJ { case 0: list = append([]int{i}, list...) case len(list): list = append(list, i) default: list = append(list[:maxJ-1], append([]int{i}, list[maxJ:]...)...) } } fmt.Println(total) }