#include #include using namespace std; int main() { ios::sync_with_stdio(false); int N; cin >> N; for (int NI = 0; NI < N; ++NI) { int K; cin >> K; int front[K], side[K]; for (int i = 0; i < K; ++i) cin >> front[i]; for (int i = 0; i < K; ++i) cin >> side[i]; unsigned int maxTiles = 0; for (int i = 0; i < K; ++i) { for (int j = 1; j <= front[i]; ++j) { for (int k = 0; k < K; ++k) if (side[k] >= j) ++maxTiles; } } int topo[K][K]; for (int i = 0; i < K; ++i) for (int j = 0; j < K; ++j) topo[i][j] = 0; int maxHeight = 0; for (int i = 0; i < K; ++i) if (side[i] > maxHeight) maxHeight = side[i]; bool sideDone[K]; bool frontDone[K]; int currentHeight = maxHeight; for (int i = 0; i < K; ++i) { frontDone[i] = false; sideDone[i] = false; } while (currentHeight != 0) { int x = -1; int y = -1; for (int i = 0; i < K; ++i) { if (side[i] == currentHeight && !sideDone[i]) x = i; if (front[i] == currentHeight && !frontDone[i]) y = i; } if (x != -1 && y != -1) { frontDone[y] = true; sideDone[x] = true; topo[x][y] = currentHeight; } else if (x == -1) { for (int i = 0; i < K; ++i) if (side[i] >= currentHeight) { frontDone[y] = true; sideDone[i] = true; topo[i][y] = currentHeight; break; } } else if(y == -1) { for (int i = 0; i < K; ++i) if (front[i] >= currentHeight) { frontDone[i] = true; sideDone[x] = true; topo[x][i] = currentHeight; break; } } bool allDone = true; for (int i = 0; i < K; ++i) { if (front[i] == currentHeight && frontDone[i] == false) { allDone = false; break; } if (side[i] == currentHeight && sideDone[i] == false) { allDone = false; break; } } if (allDone) { --currentHeight; } } int minTiles = 0; for (int i = 0; i < K; ++i) for (int j = 0; j < K; ++j) minTiles += topo[i][j]; cout << "Minimalni budova obsahuje " << minTiles << " kostek, maximalni " << maxTiles << " kostek." << endl; } return 0; }