#include <cstdio> #include <vector> using namespace std; vector<int> tab[2000]; long long int result; int questions; int main() { int n, c; scanf("%d", &n); result = 0; questions = 0; for (int i = 0; i < n; ++i) { tab[i].assign(n, 0); } for (int i = 0; i < n; ++i) { for (int j = i; j < n; ++j) { scanf("%d", &c); tab[i][j] = c; } } for (int k = 0; k <= n; ++k) { int bi = -1; int biv = 1000000000; int bj = -1; int bjv = 1000000000; for (int i = k; (i < n) && (biv > 0); ++i) { if (tab[k][i] < biv) { biv = tab[k][i]; bi = i; } } if (biv > 0) { for (int j = 0; (j < k) && (bjv > 0); ++j) { if (tab[j][k-1] < bjv) { bjv = tab[j][k-1]; bj = j; } } } if (biv > 0 && bjv > 0) { ++questions; if (biv <= bjv) { tab[k][bi] = -biv; result += biv; } else { tab[bjv][k-1] = -bjv; result += bjv; } } } while (questions < n) { int smallest = 1000000000; int bi = -1; int bj = -1; for (int i = 0; i < n; ++i) { for (int j = i; j < n; ++j) { if (tab[i][j] > 0 && tab[i][j] < smallest) { bj = j; bi = i; smallest = tab[i][j]; } } } tab[bi][bj] = -smallest; result += smallest; questions++; } printf ("%lld\n", result); return 0; }
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 | #include <cstdio> #include <vector> using namespace std; vector<int> tab[2000]; long long int result; int questions; int main() { int n, c; scanf("%d", &n); result = 0; questions = 0; for (int i = 0; i < n; ++i) { tab[i].assign(n, 0); } for (int i = 0; i < n; ++i) { for (int j = i; j < n; ++j) { scanf("%d", &c); tab[i][j] = c; } } for (int k = 0; k <= n; ++k) { int bi = -1; int biv = 1000000000; int bj = -1; int bjv = 1000000000; for (int i = k; (i < n) && (biv > 0); ++i) { if (tab[k][i] < biv) { biv = tab[k][i]; bi = i; } } if (biv > 0) { for (int j = 0; (j < k) && (bjv > 0); ++j) { if (tab[j][k-1] < bjv) { bjv = tab[j][k-1]; bj = j; } } } if (biv > 0 && bjv > 0) { ++questions; if (biv <= bjv) { tab[k][bi] = -biv; result += biv; } else { tab[bjv][k-1] = -bjv; result += bjv; } } } while (questions < n) { int smallest = 1000000000; int bi = -1; int bj = -1; for (int i = 0; i < n; ++i) { for (int j = i; j < n; ++j) { if (tab[i][j] > 0 && tab[i][j] < smallest) { bj = j; bi = i; smallest = tab[i][j]; } } } tab[bi][bj] = -smallest; result += smallest; questions++; } printf ("%lld\n", result); return 0; } |