title: Schedule Jobs Server Gets Equal Load
signature: schedule_jobs_server_gets_equal_load <- function(a, b, n) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(a, b, n, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(a, b, n), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(4L, 9L, 16L, 18L, 20L, 23L, 24L, 25L, 25L, 26L, 29L, 30L, 35L, 40L, 41L, 43L, 44L, 46L, 53L, 53L, 56L, 56L, 58L, 60L, 62L, 70L, 80L, 80L, 80L, 82L, 86L, 90L, 92L, 92L, 95L), list(3L, 15L, 16L, 16L, 18L, 26L, 30L, 32L, 32L, 35L, 37L, 41L, 42L, 43L, 48L, 49L, 49L, 54L, 55L, 57L, 65L, 66L, 67L, 67L, 68L, 83L, 85L, 89L, 89L, 90L, 91L, 93L, 96L, 97L, 99L), 29L, 
          -1L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(-24L, 70L, -74L, -90L, 72L, 50L, -94L, 86L, -58L, -68L, 42L, 0L, 98L, -70L, -14L, -32L, 6L, 74L, 64L, -78L, 86L, -42L, -56L, 2L, -34L, -46L, 70L, -62L, 50L, -58L, -58L, 42L, 86L, 96L, -8L, 8L, -22L, -14L, -14L, 98L, 2L, 98L, -28L), list(-26L, 36L, 48L, 48L, -38L, -86L, 90L, -62L, 30L, -4L, 82L, 16L, 32L, -6L, 58L, 82L, -66L, -40L, 52L, -78L, 94L, -70L, -80L, -68L, -58L, -26L, 50L, -78L, -90L, -48L, -28L, 48L, 56L, 50L, 72L, -22L, -2L, 8L, -94L, 92L, -44L, -66L, -30L), 34L, 
          -1L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 13L, 
          -1L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(98L, 18L, 50L, 36L, 88L, 75L, 2L, 40L, 74L, 19L, 63L, 82L, 77L, 5L, 59L, 97L, 70L, 50L, 71L, 90L, 90L, 61L, 63L, 99L), list(93L, 25L, 16L, 42L, 55L, 61L, 69L, 68L, 95L, 28L, 40L, 90L, 1L, 86L, 76L, 40L, 13L, 47L, 71L, 4L, 64L, 54L, 84L, 45L), 16L, 
          -1L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-80L, -64L, -64L, -64L, -64L, -62L, -54L, -48L, -44L, -44L, -38L, -30L, -30L, -26L, -14L, -12L, -10L, -6L, -6L, 6L, 22L, 22L, 22L, 26L, 28L, 50L, 52L, 70L, 86L, 86L, 88L, 90L), list(-96L, -94L, -80L, -74L, -64L, -56L, -52L, -32L, -30L, -24L, -12L, -12L, -8L, -2L, 4L, 8L, 16L, 20L, 24L, 24L, 24L, 48L, 50L, 54L, 60L, 64L, 74L, 80L, 88L, 90L, 92L, 92L), 22L, 
          -1L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 1L), list(1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L), 20L, 
          -1L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(59L, 61L, 64L), list(22L, 59L, 85L), 1L, 
          81L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(98L, 92L, 28L, 42L, -74L, -36L, 40L, -8L, 32L, -22L, -70L, -22L, -56L, 74L, 6L, 6L, -62L, 46L, 34L, 2L), list(-62L, -84L, 72L, 60L, 10L, -18L, -44L, -22L, 14L, 0L, 76L, 72L, 96L, -28L, -24L, 52L, -74L, -30L, 16L, 66L), 18L, 
          -1L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 34L, 
          -1L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(72L, 97L, 79L, 21L, 83L, 2L, 31L, 59L, 6L, 11L, 79L, 97L), list(27L, 71L, 87L, 36L, 73L, 37L, 80L, 34L, 57L, 17L, 88L, 52L), 9L, 
          -1L
      )))
  
  }
  
  main()
source_py: def schedule_jobs_server_gets_equal_load(a, b, n):
      s = 0
      for i in range(0, n):
          s += (a[i] + b[i])
      if (n == 1):
          return (a[0] + b[0])
      if ((s % n) != 0):
          return (- 1)
      x = (s // n)
      for i in range(0, n):
          if (a[i] > x):
              return (- 1)
          if (i > 0):
              a[i] += b[(i - 1)]
              b[(i - 1)] = 0
          if (a[i] == x):
              continue
          y = (a[i] + b[i])
          if ((i + 1) < n):
              y += b[(i + 1)]
          if (y == x):
              a[i] = y
              b[i] = 0
              if ((i + 1) < n):
                  b[(i + 1)] = 0
              continue
          if ((a[i] + b[i]) == x):
              a[i] += b[i]
              b[i] = 0
              continue
          if (((i + 1) < n) and ((a[i] + b[(i + 1)]) == x)):
              a[i] += b[(i + 1)]
              b[(i + 1)] = 0
              continue
          return (- 1)
      for i in range(0, n):
          if (b[i] != 0):
              return (- 1)
      return x
source_cpp: int schedule_jobs_server_gets_equal_load(vector<int> a, vector<int> b, int n) {
    int i;
    long long int s = 0;
    for ( i = 0;
    i < n;
    i ++ ) s += ( a [ i ] + b [ i ] );
    if ( n == 1 ) return a [ 0 ] + b [ 0 ];
    if ( s % n != 0 ) return - 1;
    int x = s / n;
    for ( i = 0;
    i < n;
    i ++ ) {
      if ( a [ i ] > x ) return - 1;
      if ( i > 0 ) {
        a [ i ] += b [ i - 1 ];
        b [ i - 1 ] = 0;
      }
      if ( a [ i ] == x ) continue;
      int y = a [ i ] + b [ i ];
      if ( i + 1 < n ) y += b [ i + 1 ];
      if ( y == x ) {
        a [ i ] = y;
        b [ i ] = b [ i + 1 ] = 0;
        continue;
      }
      if ( a [ i ] + b [ i ] == x ) {
        a [ i ] += b [ i ];
        b [ i ] = 0;
        continue;
      }
      if ( i + 1 < n && a [ i ] + b [ i + 1 ] == x ) {
        a [ i ] += b [ i + 1 ];
        b [ i + 1 ] = 0;
        continue;
      }
      return - 1;
    }
    for ( i = 0;
    i < n;
    i ++ ) if ( b [ i ] != 0 ) return - 1;
    return x;
  }