title: Minimum Cost Cut Board Squares
signature: function minimumCostCutBoardSquares($x, $y, $m, $n) {
test_code: <?php
  
  
  // SOLUTION CODE 
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  function validateSolution($actual, $expected){
      return serialize($actual) == serialize($expected);
  }
  
  function driver($x, $y, $m, $n, $expected){
      try{
        if (validateSolution(PLACEHOLDER_FN_NAME($x, $y, $m, $n), $expected)){
          return "PASSED";
        }
        return "FAILED";
      }
      catch (Exception $exception_obj) {
        return get_class($exception_obj);
      }
  }
  
  function main(){
      printf("TEST-0...%s\n",driver(
        array(98, 97, 93, 90, 90, 87, 87, 87, 83, 77, 75, 73, 70, 70, 69, 67, 67, 61, 60, 59, 58, 58, 56, 53, 52, 46, 46, 44, 40, 37, 34, 33, 33, 33, 32, 28, 26, 25, 25, 23, 22, 22, 20, 18, 16, 9, 9, 1), array(99, 97, 96, 95, 92, 91, 88, 87, 86, 77, 74, 73, 73, 72, 71, 70, 70, 67, 63, 62, 60, 59, 56, 55, 55, 55, 50, 47, 45, 39, 39, 37, 35, 30, 30, 29, 27, 25, 23, 20, 19, 17, 16, 13, 10, 9, 3, 2), 25, 27, 
        47899));
      
      printf("TEST-1...%s\n",driver(
        array(78, 66, 66, 52, -4, -52, -72, -74), array(88, 84, 72, 30, -34, -40, -76, -78), 6, 7, 
        76));
      
      printf("TEST-2...%s\n",driver(
        array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), array(1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 15, 19, 
        119));
      
      printf("TEST-3...%s\n",driver(
        array(97, 96, 89, 87, 83, 78, 77, 76, 67, 66, 63, 58, 54, 52, 48, 44, 38, 37, 28, 28, 19, 2), array(94, 92, 91, 83, 79, 75, 75, 72, 63, 63, 47, 40, 37, 36, 36, 33, 33, 29, 26, 19, 9, 5), 13, 14, 
        13164));
      
      printf("TEST-4...%s\n",driver(
        array(96, 90, 86, 78, 70, 32, 26, 26, 20, 16, 14, -2, -2, -4, -8, -10, -34, -44, -58, -60, -62, -62, -68, -72, -76, -78, -84), array(98, 92, 78, 66, 64, 50, 48, 42, 36, 26, 16, 16, 0, -4, -10, -18, -24, -32, -32, -48, -60, -62, -76, -82, -86, -98, -98), 15, 24, 
        -1650));
      
      printf("TEST-5...%s\n",driver(
        array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 25, 26, 
        341));
      
      printf("TEST-6...%s\n",driver(
        array(75, 30), array(39, 10), 1, 1, 
        153));
      
      printf("TEST-7...%s\n",driver(
        array(96, 78, 74, 70, 70, 62, 60, 58, 50, 46, 44, 40, 38, 38, 36, 32, 24, 22, 16, 14, 10, 0, -8, -10, -10, -18, -20, -36, -52, -54, -56, -56, -60, -60, -68, -82, -96), array(94, 92, 92, 90, 86, 80, 78, 70, 64, 62, 46, 46, 40, 40, 32, 6, 4, -6, -14, -18, -20, -22, -24, -28, -34, -42, -50, -56, -62, -66, -76, -86, -86, -90, -92, -94, -96), 19, 19, 
        14508));
      
      printf("TEST-8...%s\n",driver(
        array(1, 0, 0, 0), array(1, 1, 1, 0), 2, 2, 
        5));
      
      printf("TEST-9...%s\n",driver(
        array(90, 81, 81, 74, 67, 40, 29, 25, 24, 13, 10), array(75, 74, 58, 53, 51, 49, 45, 25, 23, 14, 7), 8, 10, 
        4117));
      
  }
  
  main();
  ?>
source_py: def minimum_cost_cut_board_squares(X, Y, m, n):
      res = 0
      X.sort(reverse=True)
      Y.sort(reverse=True)
      hzntl = 1
      vert = 1
      i = 0
      j = 0
      while ((i < m) and (j < n)):
          if (X[i] > Y[j]):
              res += (X[i] * vert)
              hzntl += 1
              i += 1
          else:
              res += (Y[j] * hzntl)
              vert += 1
              j += 1
      total = 0
      while (i < m):
          total += X[i]
          i += 1
      res += (total * vert)
      total = 0
      while (j < n):
          total += Y[j]
          j += 1
      res += (total * hzntl)
      return res
source_cpp: int minimum_cost_cut_board_squares(vector<int> X, vector<int> Y, int m, int n) {
    int res = 0;
    sort(X.begin(), X.end(), greater < int > ( ) );
    sort(Y.begin(), Y.end(), greater < int > ( ) );
    int hzntl = 1, vert = 1;
    int i = 0, j = 0;
    while ( i < m && j < n ) {
      if ( X [ i ] > Y [ j ] ) {
        res += X [ i ] * vert;
        hzntl ++;
        i ++;
      }
      else {
        res += Y [ j ] * hzntl;
        vert ++;
        j ++;
      }
    }
    int total = 0;
    while ( i < m ) total += X [ i ++ ];
    res += total * vert;
    total = 0;
    while ( j < n ) total += Y [ j ++ ];
    res += total * hzntl;
    return res;
  }