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

typedef struct s_l_el {
   struct s_l_el *next;
   int i, j;
} lelem;

typedef struct s_list {
   lelem *items;
   int n;
} list;

typedef struct s_el {
   int i, j;
} int2;


void choose (
   int n,
   int **o, int **c, int **tmp_arr,
   int2 *lown, int *crash,
   list **lst,
   unsigned long long int *gmin,
   unsigned long long int sum,
   int i
) {
   int x, y;
   int lmin, lmx, lmy;
   lelem *el;

   int lsum;

   do {

      crash[i] = 0;
      //zrzekanie sie owna
      o[lown[i].i][lown[i].j] = -1;


      for (x=0; x < n; x++) {
         for (y=x; y < n; y++) {
            tmp_arr[x][y] = 0 + c[x][y];
         }
      }
      el = lst[i]->items;
      for (x=0; x < lst[i]->n; x++) {
         tmp_arr[el->i][el->j] += 1000000001;
         el = el->next;
      }

      //szukamy min w tablicy od 0,i do i,n
      lmin = 2000000002;

      for (x=0; x <= i; x++) {
         for (y=i; y < n; y++) {
            if ((tmp_arr[x][y] < lmin) && (o[x][y] == -1)) {
               lmin = tmp_arr[x][y];
               lmx = x;
               lmy = y;
            }
         }
      }

      // sprawdzanie min zajetych
      for (x=0; x < i; x++) {
         for (y=i; y < n; y++) {
            if ((tmp_arr[x][y] <= lmin) && (o[x][y] != -1)) {
               crash[o[x][y]]++;
            }
         }
      }
      // koniec sprawdzania :D

      // ownowanie
      lown[i].i = lmx;
      lown[i].j = lmy;
      o[lmx][lmy] = i;

      el = malloc(sizeof(lelem));
      el->next = lst[i]->items;
      el->i = lmx;
      el->j = lmy;

      lst[i]->items = el;
      lst[i]->n++;

      lsum = sum + c[lmx][lmy];

      if (lsum >= (*gmin)) {
         return;
      }
      if (i+1 == n) {
         (*gmin) = lsum;
      } else {
         choose (n, o, c, tmp_arr, lown, crash, lst, gmin, lsum, i+1);
      }

   } while (crash[i] > 0);
}


int main() {

   int n;
   scanf("%d", &n);

   int **owner    = malloc (sizeof(int*) * n);
   int **c        = malloc (sizeof(int*) * n);
   int **tmp_arr  = malloc (sizeof(int*) * n);
   int2 *lown     = malloc (sizeof(int2) * n);
   int *crash     = malloc (sizeof(int) * n);

   list **lst     = malloc (sizeof(list*) * n);


   int i, j, cij;


   for (i=0; i < n; i++) {
      lown[i].i   = 0;
      lown[i].j   = n;
      crash[i]    = 0;

      lst[i]      = malloc (sizeof(list));
      lst[i]->n   = 0;

      owner[i]    = malloc (sizeof(int) * (n+1));
      c[i]        = malloc (sizeof(int) * n);
      tmp_arr[i]  = malloc (sizeof(int) * n);
      for (j=i; j < n; j++) {
         scanf("%d", &cij);
         owner[i][j] = -1;
         c[i][j] = cij;
      }
   }

   unsigned long long int gmin = n * 1000000001;


   choose (n, owner, c, tmp_arr, lown, crash, lst, &gmin, 0, 0);

   printf("%llu", gmin);


   return 0;
}