Language: C++

#include <bits/stdc++.h>
using namespace std;

#define int long long
//#define uint __int128_t

#define pb push_back
#define fi first
#define se second
#define rep(i,s,n) for(int i = s;i<n;i++)
#define repe(i,s,n) for(int i = s;i<=n;i++)
#define rrep(i,s,n) for(int i = (n)-1;i>=(s);i--)
#define all(v) (v).begin(),(v).end()
#define chmin(a,b) a=min((a),(b))
#define chmax(a,b) a=max((a),(b))
#define low(a,b) lower_bound(all(a),(b))-(a).begin()
#define upp(a,b) upper_bound(all(a),(b))-(a).begin()
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0)
typedef long long ll;
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<vint> vvint;
typedef vector<pint>vpint;
typedef pair<pint,int> P1;
typedef pair<int,pint> P2;
typedef pair<pint,pint> PP;
static const ll maxLL = (ll)1 << 62;
const ll MOD=1000000007;
const ll INF=1e18;

int N,K;
vpint v;
int ans1=0,ans2=0;
int cntcnt[100010];
int cntcntcnt=0;
int memo[100010];

signed main() {
    IOS();
    //(1ll<<N)シフト演算のオーバーフローに気をつける
    //MOD同士の引き算に注意
    cin>>N>>K;
    v.resize(N);
    rep(i,0,N){
        int a,b;
        cin>>a>>b;
        v[i]=pint(b,a);
    }
    sort(all(v));
    reverse(all(v));
    
    vint used(100010,0);
    int x=0;
    rep(i,0,K){
        x+=v[i].fi;
        if(!used[v[i].se]){
            used[v[i].se]=1;
            cntcntcnt++;
        }
    }
    x+=cntcntcnt*cntcntcnt;
    
    ans1=0;
    vint used1(100010,0);
    int cnt=0;
    rep(i,0,N){
        if(!cntcnt[v[i].se]){
            used1[i]=1;
            cntcnt[v[i].se]=1;
            ans1+=v[i].fi;
            memo[cnt]=v[i].fi;
            cnt++;
        }
        if(cnt==K){
            break;
        }
    }
    sort(memo,memo+cnt);
    int memocnt=0;
    rep(i,0,K-cnt){
        if(!used1[i]){
            ans1+=v[i].fi;
            used1[i]=true;
        }
    }
    rep(i,0,N){
        if(used1[i])continue;
        if(ans1+cnt*cnt<ans1-memo[memocnt]+(cnt-1)*(cnt-1)+v[i].fi){
            ans1-=memo[memocnt];
            ans1+=v[i].fi;
            memocnt++;
            cnt--;
            cnt=max(1,cnt);
        }
    }
    cout<<max(x,cnt*cnt+ans1)<<endl;
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:94:20: error: no matching function for call to 'max(int, long long int&)'
   94 |             cnt=max(1,cnt);
      |                 ~~~^~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:94:20: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   94 |             cnt=max(1,cnt);
      |                 ~~~^~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:94:20: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   94 |             cnt=max(1,cnt);
      |                 ~~~^~~~~~~