// zoltan chivay // marigold // triss // MOUDRY MUSTAFA KURVA #include using namespace std; int min (int a, int b) { return a < b ? a : b; } void printTable (int * table, int s) { for (int y = 0; y < s; y++) { for (int x = 0; x < s; x++) { cout << table[s*y + x] << " "; } cout << endl; } } int main (int argc, char ** argv) { int n; cin >> n; for (int i = 0; i < n; i++) { int s; cin >> s; int * pX = new int [s]; int * pY = new int [s]; for (int j = 0; j < s; j++) { cin >> pX[j]; } for (int j = 0; j < s; j++) { cin >> pY[j]; } // compute max int max = 0; for (int x = 0; x < s; x++) for (int y = 0; y < s; y++) { max += min (pX[x], pY[y]); } // compute min int * pMin = new int [s*s]; for (int j = 0; j < s*s; j++) pMin[j] = 0; // pro kazdy radek projdu kazdy sloupec for (int x = 0; x < s; x++) for (int y = 0; y < s; y++) { if (pY[y] <= pX[x]) { pMin[y*s+x] = pY[y]; } } // vypis tabulku // cout << "prvni pruchod:" << endl; //printTable (pMin, s); // pro kazdy sloupec projdu kazdy radek for (int x = 0; x < s; x++) for (int y = 0; y < s; y++) { if (pX[x] <= pY[y]) { pMin[y*s+x] = pX[x]; }else { pMin[y*s+x] = 0; } } //cout << "druhy pruchod:" << endl; //printTable (pMin, s); // pro kazdy radek necham ve sloupci jen nejvyssi cislo, jinak nuly for (int y = 0; y < s; y++) { int tempMax = pMin[y*s]; for (int x = 0; x < s; x++) { if (pMin[y*s+x] > tempMax) { tempMax = pMin[y*s+x]; } } for (int x = 0; x < s; x++) { if (pMin[y*s+x] < tempMax) { pMin[y*s+x] = 0; } } } int minVal = 0; for (int x = 0; x < s; x++) { int tempMax = pMin[x]; int tempCount = 1; for (int y = 1; y < s; y++) { if (pMin[y*s+x] > tempMax) { tempMax = pMin[y*s+x]; tempCount = 1; } else if (pMin[y*s+x] == tempCount) tempCount ++; } minVal += tempCount * tempMax; } cout << "Minimalni budova obsahuje " << minVal << " kostek, maximalni " << max << " kostek." << endl; } return 0; }