#![allow(non_snake_case)]

use std::cmp::min;
use std::io::Read;

const ALPHA_SIZE: usize = 26;

#[derive(Clone)]
struct State<'a> {
    D: usize,
    C: &'a Vec<i32>,
    S: &'a Vec<Vec<i32>>,
    actions: Vec<usize>
}


impl<'a> State<'a> {
    fn create(D: usize, C: &'a Vec<i32>, S: &'a Vec<Vec<i32>>, actions: Vec<usize>) -> State<'a> {
        assert!(actions.len() <= D);
        assert_eq!(S[0].len(), ALPHA_SIZE);
        return State { D, C, S, actions };
    }

    fn last_score(self: &Self) -> i32 {
        let mut score = 0;
        let mut last: Vec<i32> = vec![-1; ALPHA_SIZE];
        for d in 0..self.actions.len() {
            let diff = d as i32 - last[self.actions[d]];
            score += self.S[d][self.actions[d]];
            score -= diff * (diff - 1) / 2 * self.C[self.actions[d]];
            last[self.actions[d]] = d as i32;
        }
        for c in 0..ALPHA_SIZE {
            let diff = self.actions.len() as i32 - last[c];
            score -= diff * (diff - 1) / 2 * self.C[c]
        }
        score
    }

    fn change(self: &mut Self, d: usize, action: usize) {
        self.actions[d] = action;
    }

    fn add_action(self: &Self, action: usize) -> State<'a> {
        let mut actions = self.actions.clone();
        actions.push(action);
        return State {
            D: self.D,
            C: self.C,
            S: self.S,
            actions,
        };
    }
}

struct Solver {
    D: usize,
    C: Vec<i32>,
    S: Vec<Vec<i32>>,
}

impl Solver {
    fn create() -> Solver {
        let mut buffer = String::new();
        std::io::stdin().read_to_string(&mut buffer).unwrap();
        let mut iter = buffer.split_whitespace();
        let D = iter.next().unwrap().parse::<usize>().unwrap();

        let C: Vec<i32> = (0..ALPHA_SIZE)
            .map(|_| iter.next().unwrap().parse::<i32>().unwrap())
            .collect();
        let S: Vec<Vec<i32>> = (0..D)
            .map(|_| {
                (0..ALPHA_SIZE)
                    .map(|_| iter.next().unwrap().parse::<i32>().unwrap())
                    .collect()
            })
            .collect();
        Solver { D, C, S }
    }

    fn naive_greedy(self: &Self) -> Vec<usize> {
        let mut score = 0;
        let mut last: Vec<i32> = vec![-1; ALPHA_SIZE];
        let mut next_actions = vec![];
        for d in 0..self.D {
            let mut best_next_score = -1_000_000;
            let mut best_next_action = 0;
            for next_action in 0..ALPHA_SIZE {
                let mut next_score = score;
                next_score += self.S[d][next_action];
                for c in 0..ALPHA_SIZE {
                    if c != next_action {
                        next_score -= (d as i32 - last[c]) * self.C[c];
                    }
                }
                if next_score > best_next_score {
                    best_next_score = next_score;
                    best_next_action = next_action;
                }
            }

            score = best_next_score;
            // dbg!(score, best_next_action, &last, &self.C);
            last[best_next_action] = d as i32;
            next_actions.push(best_next_action);
        }
        next_actions
    }

    fn beam_search(self: &Self, beam_width: usize) -> Vec<usize> {
        let mut curr_states = vec![];
        let mut next_states = vec![];
        curr_states.push(State::create(self.D, &self.C, &self.S, vec![]));

        for d in 0..self.D {
            // dbg!(curr_states.len());
            {
                curr_states.sort_by_key(|s| -s.last_score());
                for i in 0..min(beam_width, curr_states.len()) {
                    let state = curr_states.get(i).unwrap();
                    for c in 0..ALPHA_SIZE {
                        next_states.push(state.add_action(c));
                    }
                }
            }
            curr_states.clear();
            {
                for state in &next_states {
                    curr_states.push(state.clone());
                }
            }
            next_states.clear();
        }

        curr_states.sort_by_key(|s| -s.last_score());
        return curr_states[0].actions.clone();
    }
}

fn main() {
    let solver = Solver::create();
    for action in solver.beam_search(10) {
        println!("{}", action + 1);
    }
}


Measured memory_bytes: 4972