Language: C++

#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
using ll = long long;
using pll = pair<ll, ll>;
#define FOR(i, begin, end) for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define VREP(s, ite) for (auto ite = s.begin(); ite != s.end(); ++ite)
#define FI first
#define SE second
//#define endl "\n"
#define ciosup  \
    cin.tie(0); \
    ios::sync_with_stdio(false);
#define eb emplace_back
#define vint vector<int>
constexpr ll INF = 1e9 + 7;
constexpr ll MOD = 1e9 + 7;

int main()
{
    ciosup;
    int n, k;
    cin >> n >> k;
    vector<pint> t(n);
    REP(i,n){
        cin >> t[i].SE >> t[i].FI;
    }
    sort(t.begin(), t.end());
    reverse(t.begin(), t.end());
    unordered_map<int, priority_queue<int, vector<int>, greater<int>>> sushi;
    ll ans = 0, kind = 0;
    REP(i, k)
    {
        if (!sushi.count(t[i].SE)){
            ++kind;
        }
        sushi[t[i].SE].emplace(t[i].FI);
        ans += t[i].FI;
    }
    ans += kind * kind;
    priority_queue<ll> ansque;
    ansque.emplace(ans);
    int exsushi = k - 1;
    FOR(i, k, n)
    {
        if (!sushi.count(t[i].SE)){
            while (exsushi >= 0 && sushi[t[exsushi].SE].size() <= 1){
                --exsushi;
            }
            if (exsushi == -1)
                break;
            ll ans2 = ans + t[i].FI - sushi[t[exsushi].SE].top() - kind * kind + (kind + 1) * (kind + 1);
            ansque.emplace(ans2);
            sushi[t[exsushi].SE].pop();
            sushi.emplace(t[i].SE);
            ans = ans2;
            ++kind;
        }
    }
    cout << ansque.top() << endl;
}

Compiler output:
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/c++allocator.h:33,
                 from /usr/include/c++/14/bits/allocator.h:46,
                 from /usr/include/c++/14/string:43,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/new_allocator.h: In instantiation of 'void std::__new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > >; _Args = {int&}; _Tp = std::__detail::_Hash_node<std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > >, false>]':
/usr/include/c++/14/bits/alloc_traits.h:575:17:   required from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > >; _Args = {int&}; _Tp = std::__detail::_Hash_node<std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > >, false>; allocator_type = std::allocator<std::__detail::_Hash_node<std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > >, false> >]'
  575 |           __a.construct(__p, std::forward<_Args>(__args)...);
      |           ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/hashtable_policy.h:2030:36:   required from 'std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type* std::__detail::_Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&& ...) [with _Args = {int&}; _NodeAlloc = std::allocator<std::__detail::_Hash_node<std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > >, false> >; __node_ptr = std::allocator<std::__detail::_Hash_node<std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > >, false> >::value_type*]'
 2030 |             __node_alloc_traits::construct(__alloc, __n->_M_valptr(),
      |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
 2031 |                                            std::forward<_Args>(__args)...);
      |                                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/hashtable.h:312:35:   required from 'std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::_Scoped_node::_Scoped_node(std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::__hashtable_alloc*, _Args&& ...) [with _Args = {int&}; _Key = int; _Value = std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > >; _Alloc = std::allocator<std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > > >; _ExtractKey = std::__detail::_Select1st; _Equal = std::equal_to<int>; _Hash = std::hash<int>; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<false, false, true>; std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::__hashtable_alloc = std::_Hashtable<int, std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > >, std::allocator<std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > > >, std::__detail::_Select1st, std::equal_to<int>, std::hash<int>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<false, false, true> >::__hashtable_alloc]'
  312 |             _M_node(__h->_M_allocate_node(std::forward<_Args>(__args)...))
      |                     ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/hashtable.h:2147:15:   required from 'std::pair<typename std::__detail::_Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::iterator, bool> std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::_M_emplace(std::true_type, _Args&& ...) [with _Args = {int&}; _Key = int; _Value = std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > >; _Alloc = std::allocator<std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > > >; _ExtractKey = std::__detail::_Select1st; _Equal = std::equal_to<int>; _Hash = std::hash<int>; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<false, false, true>; typename std::__detail::_Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::iterator = std::__detail::_Insert_base<int, std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > >, std::allocator<std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > > >, std::__detail::_Select1st, std::equal_to<int>, std::hash<int>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<false, false, true> >::iterator; typename _Traits::__constant_iterators = std::__detail::_Hashtable_traits<false, false, true>::__constant_iterators; std::true_type = std::true_type]'
 2147 |         _Scoped_node __node { this, std::forward<_Args>(__args)...  };
      |                      ^~~~~~
/usr/include/c++/14/bits/hashtable.h:1004:21:   required from 'std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::__ireturn_type std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::emplace(_Args&& ...) [with _Args = {int&}; _Key = int; _Value = std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > >; _Alloc = std::allocator<std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > > >; _ExtractKey = std::__detail::_Select1st; _Equal = std::equal_to<int>; _Hash = std::hash<int>; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<false, false, true>; __ireturn_type = std::_Hashtable<int, std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > >, std::allocator<std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > > >, std::__detail::_Select1st, std::equal_to<int>, std::hash<int>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<false, false, true> >::__ireturn_type]'
 1004 |         { return _M_emplace(__unique_keys{}, std::forward<_Args>(__args)...); }
      |                  ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/unordered_map.h:396:23:   required from 'std::pair<typename std::_Hashtable<_Key, std::pair<const _Key, _Val>, _Alloc, std::__detail::_Select1st, _Pred, _Hash, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<std::__not_<std::__and_<std::__is_fast_hash<_Hash>, std::__is_nothrow_invocable<const _Hash&, const _Key&> > >::value, false, true> >::iterator, bool> std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::emplace(_Args&& ...) [with _Args = {int&}; _Key = int; _Tp = std::priority_queue<int, std::vector<int>, std::greater<int> >; _Hash = std::hash<int>; _Pred = std::equal_to<int>; _Alloc = std::allocator<std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > > >; typename std::_Hashtable<_Key, std::pair<const _Key, _Val>, _Alloc, std::__detail::_Select1st, _Pred, _Hash, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<std::__not_<std::__and_<std::__is_fast_hash<_Hash>, std::__is_nothrow_invocable<const _Hash&, const _Key&> > >::value, false, true> >::iterator = std::__detail::_Insert_base<int, std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > >, std::allocator<std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > > >, std::__detail::_Select1st, std::equal_to<int>, std::hash<int>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<false, false, true> >::iterator]'
  396 |         { return _M_h.emplace(std::forward<_Args>(__args)...); }
      |                  ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:58:26:   required from here
   58 |             sushi.emplace(t[i].SE);
      |             ~~~~~~~~~~~~~^~~~~~~~~
/usr/include/c++/14/bits/new_allocator.h:191:11: error: no matching function for call to 'std::pair<const int, std::priority_queue<int, std::vector<int>, std::greater<int> > >::pair(int&)'
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51:
/usr/include/c++/14/bits/stl_pair.h:913:28: note: candidate: 'template<class _U1, class _U2, typename std::enable_if<(std::_PCC<((! std::is_same<const int, _U1>::value) || (! std::is_same<std::priority_queue<int, std::vector<int>, std::greater<int> >, _U2>::value)), const int, std::priority_queue<int, std::vector<int>, std::greater<int> > >::_MoveConstructiblePair<_U1, _U2>() && (! std::_PCC<((! std::is_same<const int, _U1>::value) || (! std::is_same<std::priority_queue<int, std::vector<int>, std::gr