title: Minimum Cost Cut Board Squares
signature: minimum_cost_cut_board_squares <- function(x, y, m, n) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(x, y, m, n, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(x, y, m, n), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(98L, 97L, 93L, 90L, 90L, 87L, 87L, 87L, 83L, 77L, 75L, 73L, 70L, 70L, 69L, 67L, 67L, 61L, 60L, 59L, 58L, 58L, 56L, 53L, 52L, 46L, 46L, 44L, 40L, 37L, 34L, 33L, 33L, 33L, 32L, 28L, 26L, 25L, 25L, 23L, 22L, 22L, 20L, 18L, 16L, 9L, 9L, 1L), list(99L, 97L, 96L, 95L, 92L, 91L, 88L, 87L, 86L, 77L, 74L, 73L, 73L, 72L, 71L, 70L, 70L, 67L, 63L, 62L, 60L, 59L, 56L, 55L, 55L, 55L, 50L, 47L, 45L, 39L, 39L, 37L, 35L, 30L, 30L, 29L, 27L, 25L, 23L, 20L, 19L, 17L, 16L, 13L, 10L, 9L, 3L, 2L), 25L, 27L, 
          47899L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(78L, 66L, 66L, 52L, -4L, -52L, -72L, -74L), list(88L, 84L, 72L, 30L, -34L, -40L, -76L, -78L), 6L, 7L, 
          76L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), list(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 15L, 19L, 
          119L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(97L, 96L, 89L, 87L, 83L, 78L, 77L, 76L, 67L, 66L, 63L, 58L, 54L, 52L, 48L, 44L, 38L, 37L, 28L, 28L, 19L, 2L), list(94L, 92L, 91L, 83L, 79L, 75L, 75L, 72L, 63L, 63L, 47L, 40L, 37L, 36L, 36L, 33L, 33L, 29L, 26L, 19L, 9L, 5L), 13L, 14L, 
          13164L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(96L, 90L, 86L, 78L, 70L, 32L, 26L, 26L, 20L, 16L, 14L, -2L, -2L, -4L, -8L, -10L, -34L, -44L, -58L, -60L, -62L, -62L, -68L, -72L, -76L, -78L, -84L), list(98L, 92L, 78L, 66L, 64L, 50L, 48L, 42L, 36L, 26L, 16L, 16L, 0L, -4L, -10L, -18L, -24L, -32L, -32L, -48L, -60L, -62L, -76L, -82L, -86L, -98L, -98L), 15L, 24L, 
          -1650L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), list(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), 25L, 26L, 
          341L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(75L, 30L), list(39L, 10L), 1L, 1L, 
          153L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(96L, 78L, 74L, 70L, 70L, 62L, 60L, 58L, 50L, 46L, 44L, 40L, 38L, 38L, 36L, 32L, 24L, 22L, 16L, 14L, 10L, 0L, -8L, -10L, -10L, -18L, -20L, -36L, -52L, -54L, -56L, -56L, -60L, -60L, -68L, -82L, -96L), list(94L, 92L, 92L, 90L, 86L, 80L, 78L, 70L, 64L, 62L, 46L, 46L, 40L, 40L, 32L, 6L, 4L, -6L, -14L, -18L, -20L, -22L, -24L, -28L, -34L, -42L, -50L, -56L, -62L, -66L, -76L, -86L, -86L, -90L, -92L, -94L, -96L), 19L, 19L, 
          14508L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(1L, 0L, 0L, 0L), list(1L, 1L, 1L, 0L), 2L, 2L, 
          5L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(90L, 81L, 81L, 74L, 67L, 40L, 29L, 25L, 24L, 13L, 10L), list(75L, 74L, 58L, 53L, 51L, 49L, 45L, 25L, 23L, 14L, 7L), 8L, 10L, 
          4117L
      )))
  
  }
  
  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;
  }