Language: C++

/*
                                                     ``.-''!!!!!!!!!!!!!!!!!!!!!!!!!!!''-..`                                                     
                                            `.'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'.`                                            
                                     `.'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'.`                                     
                                `.!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.`                                
                            `'!!!!!!!!!!!!!!!!!!(((*****((!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'`                            
                        `'!!!!!!!!!!!+"u%$$####################$$%3""!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'`                        
                     .!!!!!!!!!!!!!"#############$$$$$$$$$############$!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.                     
                  `!!!!!!!!!!!!!!!!!"z%%3I"+(!!!!!!!!!!!!!!!!!!*"I3%%j(!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!`                  
                -!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!-                
              '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(*"""""IjjjjIt""""*(!!!!!!!!!!!!!!!!!!!!'              
            '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(I%$$##$$%"!!!!!!!!!!!!!!!!!!!!!!!!!!!3$##########################$$3""!!!!!!!!!!!!!!'            
          .!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"$############$(!!!!!!!!!!!!!!!!!!!!!!!($######$#########$3&%$$$##########$!!!!!!!!!!!!!!.          
         !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!t################(!!!!!!!!!!!!!!!!!!!!!!!!!!!!!I$###########%(!!!!!!("j%$%3*!!!!!!!!!!!!!!!!         
       `!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!$################"!!!!!!!!!!!!!!!!!!!!!!!!!!!!$###############"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!`       
      .!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!$##############%!!!!!!!!!!!!!!!!!!!!!!!!!!!!"################$!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.      
     `!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"$##########%*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!$###############"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!`     
     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!+""""*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!I$###########%(!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!     
    .!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"j3%%%z"*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.    
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!    
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!    
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!    
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(+"""+*(!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!    
    '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!+""""""""""*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'    
     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!""""""""""""*!!!!"$$#################$$$3j"+!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!     
     .!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"""""""""""""!!!u############################$$J"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.     
      '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"""""""""""""(!!!!!((!!!!!!!!!!!!!!(*""u%$########$!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'      
       '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!""""""""""""""*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!+t%$%3+!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'       
        .!!!!!!!!!!!!!!!!!!!!!!!!!!!!!("""""""""""""""!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!(((*+""""""+(!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.        
          !!!!!!!!!!!!!!!!!!!!!!!!!(""""""""""""""""+!!!!!!!!!!!!!!!!!!!!(((*+"""""""""""""""""""""""!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!          
           .!!!!!!!!!!!!!!!!!!!(+"""""""""""""""""(!!!!!!!(((*+"""""""""""""""""""""""""""""""""""""(!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.           
             .!!!!!!!!!!!!!!("""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""+*(((!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.             
               .!!!!!!!!!!("""""""""""""""""""""""""""""""""""""""""""""""""+*(((!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.               
                 `'!!!!!!"""""""""""""""""""""""""""""""""""""""""""""""""""(!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'`                 
                    .!!!""""""""""""""""""""""""""""""""""""""""""""""""""""""!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.                    
                       ."""""""""""""""""""""""""""""""""""""""""""""""""""""!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'`                       
                        !""""""""""""""""""""""""""""""""""""""""""""""""""(!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.                           
                        .""""""""""""""""""""""""""""""""""""""""""""""""""""!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'.                               
                         .*""""""""""""""""""""""""""""""""""""""""""""""""""!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'.                                    
                           !""""""""""""""""""""""""""""""""""""""""""""""(!!!!!!!!!!!!!!!!!!!!!!!!!'-.                                          
                             !(""""""""""""""""""""""""""""""""""""""""""""!!!!!!!!!!!!!!!!''.``                                                 
                               `!!""""""""""""""""""""""""""""""""""""""""!..`````                                                               
                                   .!!!*"""""""""""""""""""""""""+(!!!!!.                                                                        



  `!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'   `!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'   
  !$#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#3.  !$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"`  
  !$#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#3.  !$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"`  
  !$#$$$$$$$$$$$$$$$$$$$%!!!!!!!!!!!!!!!!!!!!!!!"$$$$$$$$$$$$$$$$$$#3.  !$$$$$$$$$$$$$$$$$$$3"(!!!-.```     ```.'!!!+I$$$$$$$$$$$$$$$$$$$$"`  
  !$$$$$$$$$$$$$$$$$$$$#3'                      !$$$$$$$$$$$$$$$$$$#3.  !$$$$$$$$$$$$$$$3!'`                           .!"$$$$$$$$$$$$$$$$"`  
  !$#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$%!            !$$$$$$$$$$$$$$$$$$#3.  !$$$$$$$$$$$$$"!                                  `!$$$$$$$$$$$$$$"`  
  !$#$$$$$$$$$$$$$$$$$$$$$$$$$$$$#$!            !$$$$$$$$$$$$$$$$$$#3.  !$$$$$$$$$$$$!                                      ."$$$$$$$$$$$$"`  
  !$#$$$$$$$$$$$$$$$$$$$$$$$$$$$$#$!            !$$$$$$$$$$$$$$$$$$#3.  !$$$$$$$$$$$!             .!"3%$$$$$%"(!             .I$$$$$$$$$$$"`  
  !$#$$$$$$$$$$$$$$$$$$$$$$$$$$$$#$!            !$$$$$$$$$$$$$$$$$$#3.  !$$$$$$$$$$u`           ."$$$$$$$$$$$$$$$!            !$$$$$$$$$$$"`  
  !$#$$$$$$$$$$$$$$$$$$$$$$$$$$$$#$!            !$$$$$$$$$$$$$$$$$$#3.  !$$$$$$$$$$!           `"$$$$$$$$$$$$$$$$$!           '%$$$$$$$$$$"`  
  !$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#$!            !$$$$$$$$$$$$$$$$$$#3.  !$$$$$$$$$$!           .&$$$$$$$$$$$$$$$$$!           .z$$$$$$$$$$"`  
  !$#$$$$$$$$$$$$$$$$$$$$$$$$$$$$#$!            !$$$$$$$$$$$$$$$$$$#3.  !$$$$$$$$$$(           ."$$$$$$$$$$$$$$$$$!           '%$$$$$$$$$$"`  
  !$#$$$$$$$$$$$$$$$$$$$$$$$$$$$$#$!            !$$$$$$$$$$$$$$$$$$#3.  !$$$$$$$$$$3`           !$$$$$$$$$$$$$$$$"`           !$$$$$$$$$$$"`  
  !$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#%'            !$$$$$$$$$$$$$$$$$$#3.  !$$$$$$$$$$$(            !%$$$$$$$$$$$$$*`           '&$$$$$$$$$$$"`  
  !$#$$$$$$$$$$$$$$$!!J$$$$$$$$$$"'             (#$$$$$$$$$$$$$$$$$#3.  !$$$$$$$$$$$$(`            '!"3%$$$%j"!`            '3$$$$$$$$$$$$"`  
  !$#$$$$$$$$$$$$$$$!   `'!!!!'.               !$$$$$$$$$$$$$$$$$$$#3.  !$$$$$$$$$$$$$u!                                  `($$$$$$$$$$$$$$"`  
  !$#$$$$$$$$$$$$$$$!                        '+$$$$$$$$$$$$$$$$$$$$#3.  !$$$$$$$$$$$$$$$3!.                             !"$$$$$$$$$$$$$$$$"`  
  !$$$$$$$$$$$$$$$$$I!'`                 .!(%$$$$$$$$$$$$$$$$$$$$$$#3.  !$$$$$$$$$$$$$$$$$$%"!!`                  `-!(I$$$$$$$$$$$$$$$$$$$"`  
  !$#$$$$$$$$$$$$$$$$$$$$%J""+((!!(*""j%$$$$$$$$$$$$$$$$$$$$$$$$$$$#3.  !$$$$$$$$$$$$$$$$$$$$$$$$%j""*((!!!((+""3$$$$$$$$$$$$$$$$$$$$$$$$$"`  
  !$#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#3.  !$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#"`  
  `''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''   `'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''-   
  '"zIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII3".  !#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#3.-!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!   
  !$################################################################%.  *BBBBBBBBBB%3333333333#BBBBBBBBW#''+(((((((!!!!!''''!!!!!((((((((*!`  
  !$###################$%$$$$$$$$$$$$$$$$$$$$$$$####################%.  *BBBBBBBBBB$%%%".     JBBBBBBBBW#''+((((!!`              `!!*((((+!`  
  !$##################$!                      .I####################%.  *BBBBBBBBBBBBBW#'     JBBBBBBBBW#''+((*!`     `!!!!!!`     `!*(((+!`  
  !$###################"!!!*!            .!(!!($####################%.  *BBBBBBBBBBBBBW#'     JBBBBBBBBW#''+((*!     -**(((((('     !*(((*!`  
  !$########################!            !$#########################%.  *BBBBBBBBBBBBBW#'     JBBBBBBBBW#''+((*!     '+((((((+!     !*(((+!`  
  !$########################!            !$#########################%.  *BBBBBBB#BBBBBW$-     JBBBBBBBBW#''+((*!`     !((((((!`    `!*(((*!`  
  !$########################!            !$#########################%.  *BBBBBBM$-!!((!`     !#WBBBBBBBW#''+((((!'      `..`      '!*((((*!`  
  !$########################!            !$#########################%.  *BBBBBBB#"!!-```.'!"$BBBBBBBBBBW#''+((((((!!!-.`    ``-!!((((((((*!`  
  !$########################!            !$#########################%.  !###############################$''!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!   
  !$########################!            !$#########################%.  !"""""""""""""""""""""""""""""""!.!"""""+*+**"""""'*u"tI"++*""I""J(`  
  !$########################!            !$#########################%.  !$$$$$$$$$t(((((((((((%$$$$$$$$#j.!$$$$3"*'  "$$$$!z#$(. `'-  '"##u`  
  !$########################!            !$#########################%.  !$$$$$$$$$3"*.     !""$$$$$$$$$#j.!$$$$$$%!  "$$$$!3#"` !###%. '%#j`  
  !$########################!            !$#########################%.  !$$$$$$$$$$#%'     !$$$$$$$$$$$#j.!$$$""j(. `"$$$$!z#$! `!I"!  !##u`  
  !$########################!            !$#########################%.  !$$$$$$$$$$#%'     !$$$$$$$$$$$#j.!$%%"!!!!!I%%%$3'I$$$3*!!!!"$$$#"`  
  !$###################$$$$#!            !$$$$$#####################%.  !$$$$$$$$$$#%'     !$$$$$$$$$$$#j-(####$3%%%&#####!!!!!-!!'!I!!!!I"`  
  !$##################$!                      .I####################%.  !$$$$$$$$$$#$'     !$$$$$$$$$$$#I'"BBBB#I  !#BBBWB!!*!!`!(!"*."(."J.  
  !$###################*!!!!!!!!!!!!!!!!!!!!!!!%####################%.  !$$$$$$$$$$$3'     !$$$$$$$$$$$#I'"BBBB@$` !#WBBBB!!"!!!(+!!I(+""I".  
  !$################################################################%.  !$$$$$$$$$+!!!!!!!!!!!z#$$$$$$$#I'"BBBBBz  !#BBBBB!"#"'!$$!!(!"I""z.  
  !3$%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%$t.  !%%33333333&&&&&&&&&&&333333333%".*####$j33zJ#####!*%"!!u%!!!!!+"*!`  
                                                                                                                                              

*/
#include<bits/stdc++.h>
using namespace std;
#define ALL(obj) (obj).begin(), (obj).end()
#define REP(i, n) for(int i = 0;i < (n);++i)

#define endl '\n'
#define pb emplace_back
#define mkp make_pair
#define p_q priority_queue
#define MOD 1000000007LL
#define INF 2000000000
#define PI 3.1415926

const int dx[]={1,0,-1,0}, dy[]={0,-1,0,1};
const int dx_8[]={1,1,0,-1,-1,-1,0,1}, dy_8[]={0,-1,-1,-1,0,1,1,1};

// typedef long long ll;
#define int long long

template <typename T> inline T GCD(T a,T b){T c;while(b!=0){c=a%b;a=b;b=c;}return a;}
template <typename T> inline T LCM(T a,T b){T c=GCD(a,b);a/=c;return a*b;}
template <typename T> inline T nCr(T a,T b){T i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
template <typename T> inline T nHr(T a,T b){return nCr(a+b-1,b);}

signed main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n, k, t[100000], d[100000]; cin >> n >> k;
	vector<pair<int, int> > vec;
	REP(i, n){
		cin >> t[i] >> d[i];
		vec.pb(mkp(d[i], t[i]));
	}
	sort(ALL(vec)); reverse(ALL(vec));

	int num = 0, type = 0; map<int, int> mp; vector<int> d;
	REP(i, k){
		num += vec[i].first;
    if(mp.count(vec[i].second) == 0){
      type++;
      mp[vec[i].second] = 1;
    }
    else d.pb(vec[i].first);
	}
	int ans = num + type*type;
	cerr << ans << endl;

  for(int i = k; i < n; i++){
    if(mp.count(vec[i].second) != 0) continue;
    if(d.size() == 0) break;

    num -= d.back(); d.pop_back();
    num += vec[i].first;
    mp[vec[i].second] = 1;
    type++;

    ans = max(ans, num+type*type);
  }
  cout << ans << endl;

	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:127:62: error: conflicting declaration 'std::vector<long long int> d'
  127 |         int num = 0, type = 0; map<int, int> mp; vector<int> d;
      |                                                              ^
a.cc:119:30: note: previous declaration as 'long long int d [100000]'
  119 |         int n, k, t[100000], d[100000]; cin >> n >> k;
      |                              ^
a.cc:97:12: error: request for member 'emplace_back' in 'd', which is of non-class type 'long long int [100000]'
   97 | #define pb emplace_back
      |            ^~~~~~~~~~~~
a.cc:134:12: note: in expansion of macro 'pb'
  134 |     else d.pb(vec[i].first);
      |            ^~
a.cc:141:10: error: request for member 'size' in 'd', which is of non-class type 'long long int [100000]'
  141 |     if(d.size() == 0) break;
      |          ^~~~
a.cc:143:14: error: request for member 'back' in 'd', which is of non-class type 'long long int [100000]'
  143 |     num -= d.back(); d.pop_back();
      |              ^~~~
a.cc:143:24: error: request for member 'pop_back' in 'd', which is of non-class type 'long long int [100000]'
  143 |     num -= d.back(); d.pop_back();
      |                        ^~~~~~~~