fn main [ var input := read_lazy(h[0]); var fs := true; var id := 0; var ids := empty(int); while len_greater_than(input, 0), input[0] >= '0', input[0] <= '9' do [ var num := input[0] - '0'; if fs then [ for i := 0 to num do ids +<= id; id += 1; ] else [ ids += fill(-1, num); ] fs := not fs; input := input[1 .. ]; ] var src := len(ids) - 1; while src >= 0 do [ if ids[src] < 0 then [ src -= 1; continue; ] var src1 := src; while src1 > 0, ids[src1 - 1] = ids[src] do src1 -= 1; var n := src - src1 + 1; var run := 0; for d := 0 to src1 do [ if ids[d] >= 0 then run := 0; else run += 1; var run_start := d - run + 1; if run = n then [ for i := 0 to n do [ ids[run_start + i] := ids[src1 + i]; ids[src1 + i] := -1; ] goto done; ] ] done: src := src1 - 1; ] var sum := 0; for i := 0 to len(ids) do if ids[i] >= 0 then sum += i * ids[i]; write(h[1], ntos(sum) + nl); xeval ids; ]