def compute_alignment_matrix(seq_x, seq_y, scoring_matrix, global_flag):
    """
    Takes as input two sequences seq_x and seq_y whose elements share a common
    alphabet with the scoring matrix scores. The function computes and
    returns the alignment matrix for seq_x and seq_y. If global_flag is True,
    each entry of the matrix can be negative. If global_flag is False,
    then we will force each entry to be positive.
    """
    rows = len(seq_x)
    cols = len(seq_y)
    alignment_matrix = [[0 for dummy_j in range(cols+1)] for dummy_i in range(rows+1)]

    for idx_i in range(1, rows+1):
        #print(idx_i-1), print(seq_x[idx_i-1])
        score = alignment_matrix[idx_i-1][0] + scoring_matrix[seq_x[idx_i-1]]['-']
        if global_flag:
            alignment_matrix[idx_i][0] = score
        else:
            alignment_matrix[idx_i][0] = max(0, score)

    for idx_j in range(1, cols+1):
        score = alignment_matrix[0][idx_j-1] + scoring_matrix['-'][seq_y[idx_j-1]]
        if global_flag:
            alignment_matrix[0][idx_j] = score
        else:
            alignment_matrix[0][idx_j] = max(0, score)

    for idx_i in range(1, rows+1):
        for idx_j in range(1, cols+1):
            score_i_1_j_1 = alignment_matrix[idx_i-1][idx_j-1] + scoring_matrix[seq_x[idx_i-1]][seq_y[idx_j-1]]
            score_i_j_1 = alignment_matrix[idx_i][idx_j-1] + scoring_matrix['-'][seq_y[idx_j-1]]
            score_i_1_j = alignment_matrix[idx_i-1][idx_j] + scoring_matrix[seq_x[idx_i-1]]['-']
            if global_flag:
                alignment_matrix[idx_i][idx_j] = max([score_i_1_j_1, score_i_j_1, score_i_1_j])
            else:
                alignment_matrix[idx_i][idx_j] = max([max(0, score_i_1_j_1), max(0, score_i_j_1), max(0, score_i_1_j)])

    return alignment_matrix