package com.bluecup.org;
import java.util.Arrays;
import java.util.Scanner;
public class KM {
/**
* @param args
*/
static int n;
static int lx[] = new int[10];
static int ly[] = new int[10];
static int d[][] = new int[10 + 1][10 + 1];
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int value;
n = scan.nextInt();
for (int i = 1; i <= n; i++)
Arrays.fill(d[i], -1);
do {
int i = scan.nextInt();
int j = scan.nextInt();
value = scan.nextInt();
if (value > 0)
d[i][j] = value;
} while (value > 0);
lx_max();
Arrays.fill(ly, 0);
KM_match();
}
static boolean edge[][] = new boolean[10][10];
static boolean xmatch[] = new boolean[10];
static boolean ymatch[] = new boolean[10];
static int match[] = new int[10];
private static void KM_match() {
// TODO Auto-generated method stub
while (true) {
// 初始edge
// 初始match
// 匈牙利算法
// 是否退出
// 找最小顶标差值
// 减去值,调整顶标
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++)
if (d[i][j] == lx[i] + ly[j]) {
edge[i][j] = true;
} else
edge[i][j] = false;
}
for (int i = 1; i <= n; i++)
match[i] = -1;
int value = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
xmatch[j] = false;
ymatch[j] = false;
}
if (hungary(i)) {
value++;
} else {
xmatch[i] = true;
break;
}
}
if (value >= n)
break;
int ex;
ex = Integer.MAX_VALUE;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n && xmatch[i]; j++) {
if (!ymatch[j] && ex > (lx[i] + ly[j] - d[i][j])) {
ex = lx[i] + ly[j] - d[i][j];
}
}
}
for (int i = 1; i <= n; i++) {
if (xmatch[i]) {
lx[i] = lx[i] - ex;
}
if (ymatch[i]) {
ly[i] = ly[i] + ex;
}
}
}
int cost = 0;
for (int i = 1; i <= n; i++) {
cost += d[match[i]][i];
}
System.out.println(cost + "最大值");
}
private static boolean hungary(int x) {
// TODO Auto-generated method stub
for (int i = 1; i <= n; i++) {
if (!ymatch[i] && edge[x][i]) {
ymatch[i] = true;
if (match[i] == -1 || hungary(match[i])) {
match[i] = x;
return true;
}
if (match[i] != -1)
xmatch[match[i]] = true;
}
}
return false;
}
private static void lx_max() {
// TODO Auto-generated method stub
for (int i = 1; i <= n; i++) {
lx[i] = Integer.MIN_VALUE;
for (int j = 1; j <= n; j++) {
if (lx[i] < d[i][j])
lx[i] = d[i][j];
}
}
}
}