using System; using System.Collections.Generic; using System.Linq; namespace SortItems { class Program { static void Main(string[] args) { int[] P = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray(); int R = MainProcess(P[0],P[1]); Console.WriteLine(R); } private static int MainProcess(int N, int M) { int[][] itemlines = new int[M][]; for(int i = 0; i < M; i++) { itemlines[i] = Console.ReadLine().Split(' ').Select(s => int.Parse(s)).ToArray(); } int[] items = new int[N]; for(int i = 0; i < N; i++) { items[i] = i; } int R = 0; var perm = new Permutation(); foreach (var n in perm.Enumerate(items)) { int S = 0, idx0 = 0, idx1 = 0; for (int i = 0; i < M; i++) { for(int j = 0;j < N; j++) { if (itemlines[i][0] == n[j]) idx0 = j; if (itemlines[i][1] == n[j]) idx1 = j; } if(idx0 < idx1) { S += itemlines[i][2]; } } R = Math.Max(R, S); } return R; } } /* * Copy from http://qiita.com/gushwell/items/8780fc2b71f2182f36ac */ public class Permutation { public IEnumerable Enumerate(IEnumerable items) { if (items.Count() == 1) { yield return new T[] { items.First() }; yield break; } foreach (var item in items) { var leftside = new T[] { item }; var unused = items.Except(leftside); foreach (var rightside in Enumerate(unused)) { yield return leftside.Concat(rightside).ToArray(); } } } } }