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
#include <stdio.h>
#include <stdlib.h>

int main()
{
	int n, i, j, k, tmp, s;
	int *tabl;

	scanf("%d", &n);
	tabl = malloc(n * sizeof(int));
	
	s = (1+n)*n/2; // liczba wszystkich c_ij
	scanf("%d", tabl);
	for (i = 1; i < n; i++)
	{
		scanf("%d", &tmp);
		for (j = 0; j < i; j++)
		{
			if (tmp <= tabl[j])
			{
				for (k = i; k > j; k--)
					tabl[k] = tabl[k-1];
				break;
			}
		}
		tabl[j] = tmp;
	}
	for (i = n; i < s; i++)
	{
		scanf("%d", &tmp);
		for (j = 0; j < n; j++)
		{
			if (tmp <= tabl[j])
			{
				for (k = n-1; k > j; k--)
					tabl[k] = tabl[k-1];
				tabl[j] = tmp;
				break;
			}
		}
	}
	s = 0;
	for (i = 0; i < n; i++) s += tabl[i];
	printf("%d\n", s);
	free(tabl);
	
	return 0;
}