// moudryMustafa // spectrum // zoltan #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 int minY = 0; for (int y = 0; y < s; y++) { int tempMax = pMin[y*s]; int tempIndex = 0; for (int x = 0; x < s; x++) { if (pMin[y*s+x] > tempMax) { tempMax = pMin[y*s+x]; tempIndex = x; } } minY += tempMax; } // pro kazdy sloupec necham v radku jen nejvyssi cislo, jinak nuly int minX = 0; for (int x = 0; x < s; x++) { int tempMax = pMin[x]; int tempIndex = 0; for (int y = 0; y < s; y++) { if (pMin[y*s+x] > tempMax) { tempMax = pMin[y*s+x]; tempIndex = y; } } minX += tempMax; } int min = minX > minY ? minX : minY; cout << "Minimalni budova obsahuje " << min << " kostek, maximalni " << max << " kostek." << endl; } return 0; }