import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { private static int n; private static int m; private static int[][] sumPointArray; private static List[] takenPointArray; private static List[] givenPointArray; private static List sortedGoodsNumber; public static void main(String[] args) { Scanner cin = new Scanner(System.in); n = Integer.parseInt(cin.next()); sumPointArray = new int[n][2]; takenPointArray = new List[10]; givenPointArray = new List[10]; sortedGoodsNumber = new ArrayList(n); m = Integer.parseInt(cin.next()); for (int i = 0; i < m; i++) { int left = Integer.parseInt(cin.next()); int right = Integer.parseInt(cin.next()); int point = Integer.parseInt(cin.next()); sumPointArray[left][0] += point; if (takenPointArray[left] == null) { takenPointArray[left] = new ArrayList(); } int[] takenPoint = {right, point}; takenPointArray[left].add(takenPoint); sumPointArray[right][1] += point; if (givenPointArray[right] == null) { givenPointArray[right] = new ArrayList(); } int[] givenPoint = {left, point}; givenPointArray[right].add(givenPoint); } cin.close(); execute(); } private static void execute() { int totalPoint = 0; while (sortedGoodsNumber.size() < n) { int maxDiff = -1; int maxDiffIndex = -1; for (int i = 0; i < n; i++) { if (sortedGoodsNumber.contains(i)) { continue; } int diff = sumPointArray[i][0] - sumPointArray[i][1]; if (diff > maxDiff) { maxDiff = diff; maxDiffIndex = i; } } sortedGoodsNumber.add(maxDiffIndex); totalPoint += sumPointArray[maxDiffIndex][0]; for (int[] takenPoint : takenPointArray[maxDiffIndex]) { sumPointArray[takenPoint[0]][1] -= takenPoint[1]; } for (int[] givenPoint : givenPointArray[maxDiffIndex]) { sumPointArray[givenPoint[0]][0] -= givenPoint[1]; } } // System.out.println(sortedGoodsNumber); System.out.println(totalPoint); } }