# frozen_string_literal: true class String # rubocop:todo Style/Documentation def to_gcp %w[G C P].map { chars.tally[_1] || 0 } end end def solve # rubocop:todo Metrics/AbcSize cpg = S.to_gcp.rotate m = Y.zip(cpg).map(&:min) m.sum * 3 + Y.zip(m) .map { _1 - _2 } .zip(cpg.zip(m).map { _1 - _2 }.rotate(-1)) .map(&:min) .sum end Y = gets.split.map(&:to_i) S = gets.chomp puts solve