#require 'prime'
#require 'openssl'
MOD=10**9+7;  cnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)
def gs() gets.chomp end
def gi() gets.chomp.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end

def ar2(s1,s2,de=nil) Array.new(s1){Array.new(s2,de)} end
def ar3(s1,s2,s3,de=nil) Array.new(s1){Array.new(s2){Array.new(s3,de)}} end

def desc(ar) ar.sort!{|x,y| y<=>x} end
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end
def sum(ar) ar.inject(:+).to_i end
def ceil(a,b) (a.to_f/b).ceil end

def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end
def rui(ar) s=[0]; ar.size.times{|i| s<<s[-1]+ar[i]}; s end
def rev_rui(ar) s=[0]; ar.size.times.reverse_each{|i| s<<s[-1]+ar[i]}; s end

def puts_yesno(b) puts(b ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def pu(h) begin; require './../nemi_lib/debug.rb'; puts_val(h); rescue LoadError; end; end
########### ( ˘ω˘ )ｽﾔｧ… ###########

# Genetic Algorithm
class GA
  # ログ取りたい
  def initialize(time:2, item_length:10, item_size:10, range:0..100)
    @start_time = Time.now
    @time_limit = time - 0.1
    @item_size = item_size
    @item_length = item_length
    @range = range
    @superior = nil

    @items = generate
  end

  def execute
    evaluate
    while true
      change
      sudden_change
      evaluate
      @superior = @items[0] if !@superior || @superior.point.to_i < @items[0].point
      break unless in_time?
    end

    @superior
  end

  private

  # 初期値生成
  def generate
    items = []
    gene = Struct.new(:ar, :point)
    @item_size.times do |i|
      ar = []
      @item_length.times do |j|
        ar << rand(@range)
      end
      items << gene.new(ar, 0)
    end
    items
  end

  # 交叉
  def change
    gene = Struct.new(:ar, :point)
    l = @items.size/2
    l.times do
      pat1 = @items.shift.ar
      pat2 = @items.shift.ar
      lp = @item_length
      @items << gene.new((pat1[0..lp/2] + pat2[lp/2+1..-1]),0)
      @items << gene.new((pat2[0..lp/2] + pat1[lp/2+1..-1]),0)
    end
  end

  # 突然変異
  def sudden_change
    ratio = 5 #percent
    count = @item_length * @item_size * ratio / 100
    count.times do |i|
      target_i = rand(@item_size)
      pos_i = rand(@item_length)
      val = rand(@range)
      @items[target_i].ar[pos_i] = val
    end
  end

  # 評価
  def evaluate
    # TODO: 実装する
    # 例: 次の要素との差分の２乗が大きくなる方がいい
    
    @item_size.times do |i|
      prev=0
      sum=0
      @item_length.times do |j|
        sum+= (@items[i].ar[j]-prev)**2
        prev = @items[i].ar[j]
      end
      @items[i].point=sum
    end

    sort!
  end

  # pointに従ってソート
  def sort!
    @items.sort_by!{|a|-a.point}.to_a
  end

  def in_time?
    @time_limit> Time.now - @start_time
  end

end

class GAA < GA
  def initialize(time:2, item_length:100, item_size:10, range:1..26,c:,s:)
    @c=c
    @s=s
    # TODO いい書き方ありそう
    super(time:time, item_length:item_length, item_size:item_size, range:range)
  end

  def evaluate
    @item_size.times do |i|
      prev=0
      sum=0
      lasts={}
      @item_length.times do |day|
        con = @items[i].ar[day]
        lasts[con]=day
        sum+=@s[day][con-1]
        (1..26).each do |k|
          last=lasts[k].to_i
          sum-=@c[k-1]*(day-last)
        end
      end
      @items[i].point=sum
    end
    sort!
  end
end

d=gi
c=gsmi
s=d.times.map{gsmi}

ga=GAA.new(item_length:d,c:c,s:s)
ans=ga.execute
puts ans.ar

Measured memory_bytes: 16408