title: Minimum Cost Cut Board Squares
signature: fun minimumCostCutBoardSquares(x: ArrayList<Int>, y: ArrayList<Int>, m: Int, n: Int): Int {
test_code: package main
  
  import kotlin.text.*
  import kotlin.math.*
  
  
  // SOLUTION CODE
  // ===================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE
  // ===================================
  fun validateSolution(actual:Int, expected:Int):Boolean{
    return actual.equals(expected)
  }
  fun driver(x: ArrayList<Int>, y: ArrayList<Int>, m: Int, n: Int, expected:Int):String {
      try{
        if (validateSolution(PLACEHOLDER_FN_NAME(x, y, m, n), expected)) {
                return "PASSED"
        }
        return "FAILED"
      } catch (e: Exception){
        return e::class.simpleName as String
      }
  
  }
  
  fun main() {
      var result: String = ""
      result = driver(arrayListOf(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), arrayListOf(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)
      println("TEST-0...${result}")
      
      result = driver(arrayListOf(78, 66, 66, 52, -4, -52, -72, -74), arrayListOf(88, 84, 72, 30, -34, -40, -76, -78), 6, 7, 76)
      println("TEST-1...${result}")
      
      result = driver(arrayListOf(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), arrayListOf(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)
      println("TEST-2...${result}")
      
      result = driver(arrayListOf(97, 96, 89, 87, 83, 78, 77, 76, 67, 66, 63, 58, 54, 52, 48, 44, 38, 37, 28, 28, 19, 2), arrayListOf(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)
      println("TEST-3...${result}")
      
      result = driver(arrayListOf(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), arrayListOf(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)
      println("TEST-4...${result}")
      
      result = driver(arrayListOf(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), arrayListOf(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)
      println("TEST-5...${result}")
      
      result = driver(arrayListOf(75, 30), arrayListOf(39, 10), 1, 1, 153)
      println("TEST-6...${result}")
      
      result = driver(arrayListOf(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), arrayListOf(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)
      println("TEST-7...${result}")
      
      result = driver(arrayListOf(1, 0, 0, 0), arrayListOf(1, 1, 1, 0), 2, 2, 5)
      println("TEST-8...${result}")
      
      result = driver(arrayListOf(90, 81, 81, 74, 67, 40, 29, 25, 24, 13, 10), arrayListOf(75, 74, 58, 53, 51, 49, 45, 25, 23, 14, 7), 8, 10, 4117)
      println("TEST-9...${result}")
      
          
  }
  
  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;
  }