using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Library;
using static Library.Input;

namespace AtCoderTemplate
{
    class ProgramD
    {
        static void Main()
        {
            int n = NextInt, k = NextInt;
            var lists = Enumerable.Repeat(0, n).Select(_ => new List<int>()).ToArray();
            for (int i = 0; i < n; i++)
            {
                int t = NextInt - 1, d = NextInt;
                lists[t].Add(d);
            }
            for (int i = 0; i < n; i++)
            {
                lists[i].Sort((p, q) => q - p);
            }

            var maxArray = lists.Where(l => l.Any()).Select(l => l.First()).OrderByDescending(x => x).ToArray();
            var exArray = lists.SelectMany(l => l.Skip(1)).OrderByDescending(x => x).ToArray();
            var pq = new PriorityQueue<long>();
            long sum = 0;
            foreach (var item in maxArray.Take(k))
            {
                pq.Enqueue(item);
                sum += item;
            }
            
            long type = pq.Count;
            foreach (var item in exArray)
            {
                if (pq.Count < k)
                {
                    pq.Enqueue(item);
                    sum += item;
                }
                else if (sum + type * type < sum + item - pq.Peek() + (type - 1) * (type - 1))
                {
                    type--;
                    sum = sum - pq.Dequeue() + item;
                    pq.Enqueue(item);
                }
                else
                {
                    break;
                }
            }

            System.Console.WriteLine(sum + type * type);
        }
    }
}

namespace Library
{
    class Input
    {
        static IEnumerator<string> enumerator = new string[] { }.AsEnumerable().GetEnumerator();

        public static string Line => Console.ReadLine();

        public static string[] StrArr => Line.Split(' ');

        public static int NextInt => int.Parse(NextWord());

        public static long NextLong => long.Parse(NextWord());

        public static List<int> IntList => StrArr.Select(int.Parse).ToList();

        public static List<long> LongList => StrArr.Select(long.Parse).ToList();

        public static IEnumerable<long[]> TakeLine(int N)
        {
            return Enumerable.Repeat(0, N).Select(_ => Console.ReadLine().Split(' ').Select(long.Parse).ToArray());
        }

        public static string NextWord()
        {
            while (!enumerator.MoveNext())
            {
                enumerator = StrArr.AsEnumerable().GetEnumerator();
            }
            return enumerator.Current;
        }
    }
}

namespace Library
{
    public class PriorityQueue<T>
    {
        readonly List<T> _heap = new List<T>();
        readonly Comparison<T> _comparison;

        public PriorityQueue(Comparison<T> comparison)
        {
            _comparison = comparison;
        }

        public PriorityQueue() : this(Comparer<T>.Default.Compare) { }

        public PriorityQueue(IComparer<T> comparer) : this(comparer.Compare) { }

        public void Enqueue(T item)
        {
            _heap.Add(item);

            int childID = _heap.Count - 1;
            int parentID = (childID - 1) / 2;

            while (childID > 0 && _comparison(_heap[parentID], _heap[childID]) > 0)
            {
                Swap(parentID, childID);
                childID = parentID;
                parentID = (parentID - 1) / 2;
            }
        }

        public T Dequeue()
        {
            var first = _heap.First();
            _heap[0] = _heap.Last();
            _heap.RemoveAt(_heap.Count - 1);

            int parentID = 0;
            int childID = parentID * 2 + 2;
            if (childID >= _heap.Count || _comparison(_heap[childID], _heap[childID - 1]) > 0) childID--;

            while (childID < _heap.Count && _comparison(_heap[parentID], _heap[childID]) > 0)
            {
                Swap(parentID, childID);
                parentID = childID;
                childID = parentID * 2 + 2;
                if (childID >= _heap.Count || _comparison(_heap[childID], _heap[childID - 1]) > 0) childID--;
            }

            return first;
        }

        void Swap(int parent, int child)
        {
            var tmp = _heap[parent];
            _heap[parent] = _heap[child];
            _heap[child] = tmp;
        }

        public T Peek() { return _heap[0]; }
        public int Count => _heap.Count;
        public bool Any() { return _heap.Any(); }
        public List<T> Data => _heap;
    }
}


Measured memory_bytes: 35700