fn main [ var numbers := map(list_break_to_lines(read_lazy(h[0])), ston); var all_seq2idx := empty(array(int16, [ 20, 20, 20, 20 ])); var all_idx2price := empty(list(int32)); for j := 0 to len(numbers) do [ var seq2idx := array_fill(int16, -1, [ 20, 20, 20, 20 ]); var idx2price := empty(int32); var last_seq := empty(int); var n := numbers[j]; for i := 0 to 2000 do [ var nn := n; nn xor= nn shl 6; nn and= 16777216 - 1; nn xor= nn shr 5; nn and= 16777216 - 1; nn xor= nn * 2048; nn and= 16777216 - 1; last_seq := last_seq + [ (nn mod 10 - n mod 10) + 10 ]; n := nn; idx2price +<= n mod 10; if len(last_seq) > 4 then last_seq := last_seq[1 .. ]; if len(last_seq) = 4 then [ if array_read(seq2idx, last_seq) = -1 then seq2idx := array_write(seq2idx, last_seq, i); ] ] all_seq2idx +<= seq2idx; all_idx2price +<= idx2price; ] var best_sum := 0; for a1 := 1 to 20 do [ fn get_best : int [ var best_sum := 0; for a2 := 1 to 20 do [ for a3 := 1 to 20 do [ for a4 := 1 to 20 do [ var seq := [ a1, a2, a3, a4 ]; var sum := 0; for j := 0 to len(numbers) do [ var idx := array_read(all_seq2idx[j], seq); if idx >= 0 then [ var price := all_idx2price[j][idx]; sum += price; ] ] best_sum := max(best_sum, sum); ] ] ] return best_sum; ] best_sum := max~spark(best_sum, get_best); ] write(h[1], ntos(best_sum) + nl); ]