#include ifstream in("fastfood.in"); #define oo 1000000000 int n, n_k, d[300]; int dist[300][300], pos[300][300]; int best[300][40]; int last[300][40]; int i, j, l; int kase = 1; void computeVals() { int i,j, k, m; for (i=0; i> n >> n_k) { if (n == 0 && n_k == 0) return 0; cout << "Chain " << kase++ << endl; for (i = 0; i < n; i++) in >> d[i]; computeVals(); for (j = 0; j <= n_k; j++) best[0][j] = oo; for (i = 0; i < n; i++) { best[i][1] = dist[0][i]; last[i][1] = -1; for (j = 1; j < n_k; j++) { best[i][j + 1] = oo; for (l = 0; l < i; l++) if (best[i][j + 1] > best[l][j] + dist[l + 1][i]) { best[i][j + 1] = best[l][j] + dist[l + 1][i]; last[i][j + 1] = l; } } } /* for (i = 0; i < n; i++) for (j = 1; j <= n_k; j++) cout << i << ' ' << j << ':' << best[i][j] << ' ' << last[i][j] << endl; */ print(n - 1, n_k); cout << "Total distance sum = " << best[n - 1][n_k] << endl; cout << endl; } return 0; }