using System;
using static System.Console;
using static System.Math;
using System.IO;
using System.Collections.Generic;
namespace I_am_the_green_coder{
    class Inonoa{
        // なんか入力用の関数作ったけどまだテストしてないので使う前に確かめて
        static void Main(){
            long[] nk  = ILongs();
            long n = nk[0];
            long k = nk[1];
            long[][] td = ILongLongs(n);

            Array.Sort(td,Compare);
            //for(int i=0;i<n;i++){
            //    Console.Write(td[i][0]);
            //    Console.Write(" ");
            //    Console.WriteLine(td[i][1]);
            //}

            HashSet<long> sset = new HashSet<long>();
            bool[] kaburi = new bool[n];

            long ans = 0;
            long ansppoi = 0;
            long syurui = k;

            for(long i=0;i<k;i++){
                ans += td[i][1];
                ansppoi += td[i][1];
                if(kaburi[i] = !sset.Add(td[i][0])){
                    syurui --;
                }
            }
            ans += syurui * syurui;
            ansppoi += syurui * syurui;

            long imal = k-1;
            long imar = k;
            while(imal>-1&&imar<n){
                if(kaburi[imal]){
                    if(sset.Add(td[imar][0])){
                        ansppoi -= td[imal][1];
                        ansppoi += td[imar][1];
                        ansppoi += 2*syurui + 1;
                        syurui ++;
                        if(ans < ansppoi)ans = ansppoi;
                        imal --;
                        imar ++;
                        continue;
                    }else{
                        imar ++;
                        continue;
                    }
                }else{
                    imal --;
                    continue;
                }
            }
            Console.WriteLine(ans);
        }

        static int Compare(long[] a, long[] b){
            if(a[1]>b[1])return -1;
            if(a[1]<b[1])return 1;
            return 0;
        }

        static readonly long MOD = 1000000007;

        ///<summary>a_1, a_2, a_3, ...みたいなの</summary>
        static long[] ILongs(){
            string[] s = ReadLine().Split();
            long[] re = new long[s.Length];
            for(long i=0;i<s.Length;i++){
                re[i] = long.Parse(s[i]);
            }
            return re;
        }

        ///<summary>xooxoxoxxoxo...みたいなの</summary>
        static char[] IChars(){
            return ReadLine().ToCharArray();
        }

        ///<summary>n m とかをちょっとはやくかくだけ</summary>
        static string[] IStrings(){
            return ReadLine().Split();
        }

        ///<summary>n行の入力を取ったり</summary>
        static long[][] ILongLongs(long n){
            long[][] re = new long[n][];
            for(long i=0;i<n;i++){
                string[] s = ReadLine().Split();
                re[i] = new long[s.Length];
                for(long j=0;j<s.Length;j++){
                    re[i][j] = long.Parse(s[j]);
                }
            }
            return re;
        }

        ///<summary>n行の文字列を取ったり</summary>
        static char[][] ICharsChars(long n){
            char[][] re = new char[n][];
            for(long i=0;i<n;i++){
                re[i] = ReadLine().ToCharArray();
            }
            return re;
        }
    }
}


Measured memory_bytes: 28356