using System;

namespace MatrixNameGame
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.UTF8;

            string firstName = Prompt("Ievadi savu vārdu: ");
            string lastName = Prompt("Ievadi savu uzvārdu: ");

            string altFirst = AlternatingCase(firstName);
            string altLast = AlternatingCase(lastName);

            int[,] originalMatrix = GenerateMatrix(firstName.Length + 1, lastName.Length + 2, firstName.Length, firstName.Length + lastName.Length);
            int[,] transposedMatrix = TransposeMatrix(originalMatrix, firstName.Length, lastName.Length);

            Console.WriteLine("\n--- Oriģinālā matrica ---");
            RenderMatrix(originalMatrix, altFirst, altLast);

            Console.WriteLine("\n--- Transponētā matrica ---");
            RenderMatrix(transposedMatrix, ReverseString(altLast), ReverseString(altFirst));
        }

        static string Prompt(string message)
        {
            Console.Write(message);
            return Console.ReadLine().Trim();
        }

        static string AlternatingCase(string text)
        {
            char[] result = new char[text.Length];
            for (int i = 0; i < text.Length; i++)
            {
                result[i] = (i % 2 == 0)
                    ? char.ToUpper(text[i])
                    : char.ToLower(text[i]);
            }
            return new string(result);
        }

        static string ReverseString(string text)
        {
            char[] chars = text.ToCharArray();
            Array.Reverse(chars);
            return new string(chars);
        }

        static int[,] GenerateMatrix(int cols, int rows, int min, int max)
        {
            int[,] matrix = new int[rows, cols];
            Random rnd = new Random();

            for (int r = 1; r < rows - 1; r++)
            {
                for (int c = 1; c < cols; c++)
                {
                    matrix[r, c] = rnd.Next(min, max + 1);
                }
            }
            return matrix;
        }

        static int[,] TransposeMatrix(int[,] original, int firstLen, int lastLen)
        {
            int[,] transposed = new int[firstLen + 2, lastLen + 1];

            for (int r = 1; r <= firstLen; r++)
            {
                for (int c = 1; c <= lastLen; c++)
                {
                    transposed[r, c] = original[c, r];
                }
            }
            return transposed;
        }

        static void RenderMatrix(int[,] matrix, string colHeaders, string rowHeaders)
        {
            int rows = matrix.GetLength(0);
            int cols = matrix.GetLength(1);

            // Header
            Console.Write("    ");
            foreach (char ch in colHeaders)
                Console.Write($" {ch,3} ");
            Console.WriteLine(" | SUM | MIN | MAX |");

            Console.WriteLine(new string('-', cols * 5 + 20));

            for (int r = 1; r < rows - 1; r++)
            {
                Console.Write($" {rowHeaders[rowHeaders.Length - r],2} |");
                int sum = 0, min = int.MaxValue, max = int.MinValue;

                for (int c = 1; c < cols; c++)
                {
                    int val = matrix[r, c];
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.Write($" {val,3} ");
                    Console.ResetColor();

                    sum += val;
                    if (val < min) min = val;
                    if (val > max) max = val;
                }
                Console.Write($"| {sum,3} | {min,3} | {max,3} |");
                Console.WriteLine();
            }

            Console.WriteLine(new string('-', cols * 5 + 20));

            // Column summaries
            Console.Write("SUM |");
            for (int c = 1; c < cols; c++)
            {
                int sum = 0;
                for (int r = 1; r < rows - 1; r++)
                    sum += matrix[r, c];
                Console.Write($" {sum,3} ");
            }
            Console.WriteLine();

            Console.Write("MIN |");
            for (int c = 1; c < cols; c++)
            {
                int min = int.MaxValue;
                for (int r = 1; r < rows - 1; r++)
                    if (matrix[r, c] < min) min = matrix[r, c];
                Console.Write($" {min,3} ");
            }
            Console.WriteLine();

            Console.Write("MAX |");
            for (int c = 1; c < cols; c++)
            {
                int max = int.MinValue;
                for (int r = 1; r < rows - 1; r++)
                    if (matrix[r, c] > max) max = matrix[r, c];
                Console.Write($" {max,3} ");
            }
            Console.WriteLine();
        }
    }
}
