using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net.Http.Headers;

namespace ConsoleApp1
{
    public static class AOC
    {
        public static List<int> first = new List<int>();
        public static List<int> second = new List<int>();
    }
    internal class Program
    {
        public static int Binariskeres(int[] tomb, int keresettertek)
        {
            int eleje = 0;
            int vege = tomb.Length - 1;
            while (eleje <= vege)
            {
                int i = (eleje + vege) / 2;
                if (tomb[i] == keresettertek)
                {
                    return i;
                }
                else if (tomb[i] < keresettertek)
                {
                    eleje = i + 1;
                }
                else if (tomb[i] > keresettertek)
                {
                    vege = i - 1;
                }
            }
            return -1;
        }

        public static int BinariskeresRekurziv(int[] tomb, int keresettertek, int eleje, int vege)
        {
            int mid = (eleje + vege) / 2;
            if (vege < 1)
            {
                return -1;
            }

            if (tomb[mid] == keresettertek) { return mid; }
            if (tomb[mid] > keresettertek)
            {
                return BinariskeresRekurziv(tomb, keresettertek, eleje, mid - 1);
            }

            return BinariskeresRekurziv(tomb, keresettertek, mid + 1, vege);
        }

        public static void Orai()
        {
            var tomb = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
            int index = Binariskeres(tomb, 8);
            int index2 = BinariskeresRekurziv(tomb, 1, 0, tomb.Length);
            int index3 = Binariskeres(tomb, 18); //-1, mert nincs ilyen 
                                                 //int index = Array.BinarySearch(tomb, 8);

            Console.WriteLine("A nyolcas indexe: {0}", index);
            Console.WriteLine("Az egyes indexe: {0}", index2);
            Console.WriteLine("Az 18 indexe: {0}", index3);
            Console.WriteLine();
        }

        public static void ReadingInput(string path)
        {
            FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
            StreamReader streamReader = new StreamReader(fileStream);

            string line = streamReader.ReadLine();
            while (line != null)
            {
                string[] parts = line.Split(new string[] { "   " }, StringSplitOptions.RemoveEmptyEntries);
                int value1 = Convert.ToInt32(parts[0]);
                int value2 = Convert.ToInt32(parts[1]);

                AOC.first.Add(value1);
                AOC.second.Add(value2);

                line = streamReader.ReadLine();
            }

            streamReader.Close();
            fileStream.Close();
        }

        public static void BubiSort()
        {
            int[] t = new int[] { 5,4,4,8,73,4,58,4,3,78,2,84,456,69};
            int n = t.Length;

            for (int i = 0; i < n; i++)
            {
                Console.WriteLine($"{t[i]}");
            }

            for (int i = n -1;i > 0; i--)
            {
                for (int j = 0; j < i; j++)
                {
                    if (t[j] > t[j+1])
                    {
                        int tmp = t[j + 1];
                        t[j+1] = t[j];
                        t[j] = tmp;
                    }
                }
            }
        }

        public static int[] Buborekrendezes(int[] bemenet)
        {
            int[] tomb = new int[bemenet.Length];
            Array.Copy(bemenet, tomb, bemenet.Length);

            var csere = true;

            for (int i = tomb.Length - 1; i >= 0 && csere; i--)
            {
                csere = false;
                for (int j = 0; j < i; j++)
                {
                    if (tomb[j] > tomb[j + 1])
                    {
                        int tmp = tomb[j];
                        tomb[j] = tomb[j + 1];
                        tomb[j + 1] = tmp;
                        csere = true ;
                    }
                }
            }
            return tomb;
        }

        public static void AOC_1(int[] array1, int[] array2)
        {
            List<int> differences = new List<int>();

            for (int i = 0; i < array1.Length; i++)
            {
                
                int diff = Math.Abs(array1[i] - array2[i]);
                differences.Add(diff);
                //Console.WriteLine(diff);
                
                //Console.WriteLine($"{array1[i]} - {array2[i]} = {diff}");
            }

            Console.WriteLine(differences.Sum());
        }

        static void Main(string[] args)
        {
            string path = $@"C:\Users\szabomarton\Desktop\C#\ProgaOra\20241212\input.txt";
            ReadingInput(path);

            AOC.first.Sort();
            AOC.second.Sort();

            AOC_1(AOC.first.ToArray(), AOC.second.ToArray());
            
        }
    }


}