using System;
using System.Linq;

public class Program
{
    static void Main(string[] args)
    {
        string vards = "ginta";
        string uzvards = "varblane";

        int[,] dati = TabulasDati(vards, uzvards);
        
        // Uzzīmē tabulu
        TabulaGalva(vards);
        Tabula(dati);
        
        Console.WriteLine();
        
        // Uzzīmē otru tabulu
        int[,] reversed = TabulasMaina(dati);
        TabulaGalva(uzvards);
        Tabula(reversed);
    }

    // Zīmē horizontālu līniju
    static void TabulasLinija(int platums, int kolonas)
    {
        Console.Write("*" + string.Join("", 
            new int[kolonas + 3].Select(_ => new string('-', platums) + "*")));
        Console.WriteLine();
    }

    // Zīmē tabulas rindu ar datiem
    static void TabulasRinda(int[] dati, int max, int min, int sum, int platums)
    {
        string FormatNum(int num) => $" {num.ToString().PadRight(platums - 2)} |";
        
        Console.Write("|" + string.Join("", dati.Select(FormatNum)));
        Console.WriteLine($"{FormatNum(max)}{FormatNum(min)}{FormatNum(sum)}");
    }

    // Random skaitļu funkcija
    static int[,] TabulasDati(string vards, string uzvards)
    {
        var rand = new Random();
        int rindas = uzvards.Length, kolonas = vards.Length;
        var dati = new int[rindas, kolonas];
        
        for (int i = 0; i < rindas; i++)
            for (int j = 0; j < kolonas; j++)
                dati[i, j] = rand.Next(vards.Length, vards.Length + uzvards.Length + 1);
        
        return dati;
    }

    // Zīmē pirmo rindu
    static void TabulaGalva(string pirRinda)
    {
        const int platums = 5;
        
        TabulasLinija(platums, pirRinda.Length);
        
        Console.Write("|" + string.Join("", 
            pirRinda.Select(c => $" {c.ToString().PadRight(platums - 2)} |")));
        Console.WriteLine($" {"max".PadRight(platums - 2)} | {"min".PadRight(platums - 2)} | {"sum".PadRight(platums - 2)} |");
        
        TabulasLinija(platums, pirRinda.Length);
    }

    // Zīmē tabulu
    static void Tabula(int[,] dati)
    {
        const int platums = 5;
        int rindas = dati.GetLength(0), kolonas = dati.GetLength(1);
        
        for (int i = 0; i < rindas; i++)
        {
            int[] rinda = new int[kolonas];
            for (int j = 0; j < kolonas; j++)
                rinda[j] = dati[i, j];
            
            TabulasRinda(rinda, rinda.Max(), rinda.Min(), rinda.Sum(), platums);
            TabulasLinija(platums, kolonas);
        }
    }

    // Apmaina rindas un kolonas
    static int[,] TabulasMaina(int[,] dati)
    {
        int rindas = dati.GetLength(0), kolonas = dati.GetLength(1);
        var reversed = new int[kolonas, rindas];
        
        for (int i = 0; i < rindas; i++)
            for (int j = 0; j < kolonas; j++)
                reversed[j, i] = dati[i, j];
        
        return reversed;
    }
}