Language: C++


class ReversePopHeap
  attr_accessor :nodes, :size
  def initialize(n)
    @nodes = Array.new(n)
    @size = 0
  end

  def push(x)
    current_index = @size
    @size += 1
    while current_index > 0 do
      parent_index = (current_index - 1)>>1
      parent_value = @nodes[parent_index]
      break if parent_value >= x
      @nodes[current_index] = parent_value
      current_index = parent_index
    end
    @nodes[current_index] = x
  end

  def pop
    top = @nodes[0]
    current_node_index = 0
    current_node_value = @nodes[0] = @nodes[@size - 1]
    @nodes[@size - 1] = nil
    @size -= 1

    while
      child_node_index_r = (current_node_index + 1) << 1
      child_node_index_l = child_node_index_r - 1
      return top unless child_node_index_l < @size
      child_node_value_l = @nodes[child_node_index_l]
      child_node_value_r = @nodes[child_node_index_r]
      if child_node_value_r&.>child_node_value_l
        max_child_value = child_node_value_r
        max_child_index = child_node_index_r
      else
        max_child_value = child_node_value_l
        max_child_index = child_node_index_l
      end
      return top unless max_child_value > current_node_value
      change(current_node_index, max_child_index)
      current_node_index = max_child_index
    end
  end

  private

  def change(index_i, index_j)
    @nodes[index_i], @nodes[index_j] = @nodes[index_j], @nodes[index_i]
  end
end

n, m = gets.split.map(&:to_i)
rph = ReversePopHeap.new(n)
gets.split.map {|s| rph.push s.to_i}
sum = 0
m.times do |i|
  top = rph.pop
  rph.push(top/2)
  sum +- top
end
sum += rph.nodes[0...rph.size].inject(:+)
puts sum

Compiler output:
a.cc:5:5: error: stray '@' in program
    5 |     @nodes = Array.new(n)
      |     ^
a.cc:6:5: error: stray '@' in program
    6 |     @size = 0
      |     ^
a.cc:10:21: error: stray '@' in program
   10 |     current_index = @size
      |                     ^
a.cc:11:5: error: stray '@' in program
   11 |     @size += 1
      |     ^
a.cc:14:22: error: stray '@' in program
   14 |       parent_value = @nodes[parent_index]
      |                      ^
a.cc:16:7: error: stray '@' in program
   16 |       @nodes[current_index] = parent_value
      |       ^
a.cc:19:5: error: stray '@' in program
   19 |     @nodes[current_index] = x
      |     ^
a.cc:23:11: error: stray '@' in program
   23 |     top = @nodes[0]
      |           ^
a.cc:25:26: error: stray '@' in program
   25 |     current_node_value = @nodes[0] = @nodes[@size - 1]
      |                          ^
a.cc:25:38: error: stray '@' in program
   25 |     current_node_value = @nodes[0] = @nodes[@size - 1]
      |                                      ^
a.cc:25:45: error: stray '@' in program
   25 |     current_node_value = @nodes[0] = @nodes[@size - 1]
      |                                             ^
a.cc:26:5: error: stray '@' in program
   26 |     @nodes[@size - 1] = nil
      |     ^
a.cc:26:12: error: stray '@' in program
   26 |     @nodes[@size - 1] = nil
      |            ^
a.cc:27:5: error: stray '@' in program
   27 |     @size -= 1
      |     ^
a.cc:32:46: error: stray '@' in program
   32 |       return top unless child_node_index_l < @size
      |                                              ^
a.cc:33:28: error: stray '@' in program
   33 |       child_node_value_l = @nodes[child_node_index_l]
      |                            ^
a.cc:34:28: error: stray '@' in program
   34 |       child_node_value_r = @nodes[child_node_index_r]
      |                            ^
a.cc:51:5: error: stray '@' in program
   51 |     @nodes[index_i], @nodes[index_j] = @nodes[index_j], @nodes[index_i]
      |     ^
a.cc:51:22: error: stray '@' in program
   51 |     @nodes[index_i], @nodes[index_j] = @nodes[index_j], @nodes[index_i]
      |                      ^
a.cc:51:40: error: stray '@' in program
   51 |     @nodes[index_i], @nodes[index_j] = @nodes[index_j], @nodes[index_i]
      |                                        ^
a.cc:51:57: error: stray '@' in program
   51 |     @nodes[index_i], @nodes[index_j] = @nodes[index_j], @nodes[index_i]
      |                                                         ^
a.cc:64:18: error: too many decimal points in number
   64 | sum += rph.nodes[0...rph.size].inject(:+)
      |                  ^~~~~~~~~~~~
a.cc:3:17: error: found ':' in nested-name-specifier, expected '::'
    3 |   attr_accessor :nodes, :size
      |                 ^
      |                 ::
a.cc:3:3: error: 'attr_accessor' has not been declared
    3 |   attr_accessor :nodes, :size
      |   ^~~~~~~~~~~~~
a.cc:3:18: error: aggregate 'ReversePopHeap nodes' has incomplete type and cannot be defined
    3 |   attr_accessor :nodes, :size
      |                  ^~~~~
a.cc:3:25: error: expected unqualified-id before ':' token
    3 |   attr_accessor :nodes, :size
      |                         ^
a.cc:58:1: error: 'sum' does not name a type
   58 | sum = 0
      | ^~~