C = (?a..?z).reduce({}){|s,e| s[e]=gets.to_i; s} # helloworld # hellloowrd hewrd = C[?h]*C[?e]*C[?w]*C[?r]*C[?d] o = C[?o] o = o/2*((o+1)/2) l = C[?l] bestl = 0 (1..(l-1)).each do |l1| l2 = l-l1 bestl = [bestl, l1*l2*(l2-1)/2].max end p hewrd*o*bestl