1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//package pa;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

public class kug
{
	public static void main(String[] args)
	{
		runalg();
	}

	private static void runalg()
	{
		if (fromFile)
		{
			setInFromFile();
		}
		else
		{
			setInFromSystemIn();
		}
		body();
	}

	private static final boolean	fromFile	= false;

	private static void setInFromFile()
	{
		File plik = new File(fileName);
		try
		{
			in = new Scanner(plik);
		}
		catch (IOException e)
		{
			System.exit(1);
		}
	}

	private static Scanner		in;
	private static final String	fileName	= "kug.txt";

	private static void setInFromSystemIn()
	{
		in = new Scanner(System.in);
	}

	private static void body()
	{
		cups = in.nextInt();
		circles = new boolean[cups];
		readCosts();
		int solution = solve();
		System.out.println(solution);
	}
	
	private static SortedSet<Integer> values()
	{
		SortedSet<Integer> result = new TreeSet<Integer>();
		for (Entry<Integer, Map<Integer, Integer>> entry : costs.entrySet())
		{
			Map<Integer, Integer> value1 = entry.getValue();
			for (Entry<Integer, Integer> entry2 : value1.entrySet())
			{
				Integer key = entry.getKey();
				Integer value = entry2.getKey();
				Integer toAdd = costs.get(key).get(value);
				result.add(toAdd);
			}
		}
		return result;
	}

	private static boolean[]	circles;

	private static void readCosts()
	{
		costs = new TreeMap<Integer, Map<Integer, Integer>>();
		for (int i = 1; i <= cups; i++)
		{
			TreeMap<Integer, Integer> subCost = readSubCosts(i, cups);
			costs.put(i, subCost);
		}

	}

	private static int											cups;

	private static Map<Integer, Map<Integer, Integer>>	costs;

	private static TreeMap<Integer, Integer> readSubCosts(int i, int cups)
	{
		TreeMap<Integer, Integer> subCost = new TreeMap<Integer, Integer>();
		for (int j = i; j <= cups; j++)
		{
			subCost.put(j, in.nextInt());
		}
		return subCost;
	}

	private static int solve()
	{
		int costAll = 0;
		SortedSet<Integer> values = values();
		for (Integer curr : values)
		{
			List<Entry<Integer, Integer>> positions = positions(curr);
			for (Entry<Integer, Integer> position : positions)
			{
				if (sum() != 5)
				{
					costAll += evaluateCost(position);
					//System.out.println("costAll="+costAll);
				}
				else
				{
					return costAll;
				}
			}
		}
		return costAll;
	}
	
	private static int evaluateCost(Entry<Integer, Integer> position)
	{
		Integer from = position.getKey();
		Integer to = position.getValue();
		int cost = cost(from, to);
		used.put(from, to);
		//System.out.println("cost("+from+", "+to+")="+cost);
		markCircles(from, to);
		List<Integer> list = getUnknown();
		for (Integer a : list)
		{
			for (Integer b : list)
			{
				if (!a.equals(b))
				{
					//System.out.println("a="+a+"; b="+b);
					boolean isUsed = isUsed(a, b);
					//System.out.println("isUsed="+isUsed);
					boolean circleA = circles[a-1];
					//System.out.println("circleA="+circleA);
					boolean circleB = circles[b-1];
					//System.out.println("circleB="+circleB);
					if (!circleA && !circleB && isUsed)
					{
						circles[a-1] = true;
						circles[b-1] = true;
					}
				}
			}
		}
		//System.out.println(Arrays.toString(circles));
		return cost;
	}
	
	private static void markCircles(int from, int to)
	{
		markOneCircle(from, to);
		markPairCircle(from, to);
		markTreeCircle(from, to);
		
	}
	
	private static void markOneCircle(int from, int to)
	{
		if (from == to)
		{
			circles[from] = true;
		}
	}
	
	private static void markPairCircle(int from, int to)
	{
		if (Math.abs(from-to) == 1 && (circles[from-1] || circles[to-1]))
		{
			if (circles[from-1])
			{
				circles[to-1] = true;
			}
			else
			{
				circles[from-1] = true;
			}
		}
	}
	
	private static void markTreeCircle(int from, int to)
	{
		if (Math.abs(from-to) == 2 && (isTrue(from) || isTrue(to) ))
		{
			//System.out.println("tu jestem2");
			circles[from] = true;
		}
	}
	private static Map<Integer, Integer> used = new TreeMap<Integer, Integer>();
	
	private static boolean isUsed(int from, int to)
	{
		boolean result = false;
		for (Entry<Integer, Integer> used : kug.used.entrySet())
		{
			Integer key = used.getKey();
			Integer val = used.getValue();
			if (key.equals(from) && val.equals(to))
			{
				result = true;
				break;
			}
		}
		return result;
	}
	
	private static List<Integer> getUnknown()
	{
		List<Integer> result = new ArrayList<Integer>();
		for (int i = 0; i < circles.length; i++)
		{
			boolean val = circles[i];
			if (!val)
			{
				result.add(i+1);
			}
		}
		return result;
	}
	private static boolean isTrue(int index)
	{
		boolean wartosc = circles[index-1];
		return wartosc;
	}
	
	private static int sum()
	{
		int sumaTrue = 0;
		for (boolean circle : circles)
		{
			if (circle)
			{
				sumaTrue += 1;
			}
		}
		return sumaTrue;
	}
	
	private static List<Entry<Integer, Integer>> positions(final int value)
	{
		List<Entry<Integer, Integer>> result = null;
		Warunek<Integer> warunek = new Warunek<Integer>()
		{
			public boolean zastosuj(Integer wartosc)
			{
				boolean wynik = wartosc == value;
				return wynik;
			}
		};
		result = uzyskajIndeksySpelniajaceWarunek(costs, warunek);
		return result;
	}
	
	private static int cost(int from, int to)
	{
		Map<Integer, Integer> subCost = costs.get(from);
		int result = subCost.get(to);
		return result;
	}
	
	public static <Dim1, Dim2, Val> List<Entry<Dim1, Dim2>> uzyskajIndeksySpelniajaceWarunek(Map<Dim1, Map<Dim2, Val>> macierz, Warunek<Val> warunek)
	{
		List<Entry<Dim1, Dim2>> result =  new ArrayList<Entry<Dim1, Dim2>>();
		for (Entry<Dim1, Map<Dim2, Val>> level1 : macierz.entrySet())
		{
			Map<Dim2, Val> index1 = level1.getValue();
			for (Entry<Dim2, Val> level2 : index1.entrySet())
			{
				Dim2 index2 = level2.getKey();
				if (warunek.zastosuj(level2.getValue()))
				{
					Entry<Dim1, Dim2> pair = new Para<Dim1, Dim2>(level1.getKey());
					pair.setValue(index2);
					result.add(pair);
				}
			}
		}
		return result;
	}
	public static interface Warunek<T>
	{
		boolean zastosuj(T dane);
	}
	
	public static class Para<T1, T2> implements Entry<T1, T2>
	{
		private T1 pierwsza;
		private T2 druga;
		
		public Para(T1 klucz)
		{
			pierwsza = klucz;
		}
		
		public Para(T1 klucz, T2 wartosc)
		{
			pierwsza = klucz;
			druga = wartosc;
		}

		public Para()
		{
		}

		@Override
		public T1 getKey() {
			return pierwsza;
		}

		@Override
		public T2 getValue() {
			return druga;
		}

		@Override
		public T2 setValue(T2 value) 
		{
			T2 poprzedniaWartosc = druga;
			druga = value;
			return poprzedniaWartosc;
		}
		
		public String toString()
		{
			return "( "+pierwsza+" , "+druga+" )";
		}
	}
}