Language: C++

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

typedef long long ll;
typedef pair<ll, ll> P;

int N, K, t[100001], d[100001];

int main(){
    cin >> N >> K;
    for(int i=0;i<N;i++)    cin >> t[i] >> d[i];

    // (di, ti) のペア
    vector<P> p(N);
    for(int i=0;i<N;i++)    p[i] = P(d[i], t[i]);

    // di の降順にソート
    sort(p.begin(), p.end(), greater<P>());

    // di の大きいものから K 個選び、種類数をカウント
    vector<P> v(K);
    map<int, int> cnt;
    for(int i=0;i<K;i++){
        v[i] = P(p[i].first, p[i].second);
        cnt[ v[i].second ]++;
    }
    int cntt = 0;
    for(auto i : cnt){
        if(0 < i.second)   cntt++;
    }

    // 残りを価値の降順に詰めておく
    vector<P> nokori;
    for(int i=K;i<N;i++){
        if(cnt[ p[i].second ] == 0) nokori.push_back( P(p[i].first, p[i].second) );
    }

    // 価値を計算
    ll ans = cntt * cntt;
    for(int i=0;i<K;i++){
        ans += v[i].first;
    }

    // 選んだ K 個を価値の昇順にソート
    sort(v.begin(), v.end());

    ll res = ans;
    int i = 0, j = 0;
    while(i < v.size() && j < nokori.size()){
        while(i < v.size() && cnt[ v[i].second ] <= 1)   i++;
        if(i < v.size() && 1 < cnt[ v[i].second ] && cnt[ nokori[j].second ] == 0){
            cnt[ v[i].second ]--;
            cnt[ nokori[j].second ]++;
            cntt++;
            res += nokori[j].first - v[i].first + (cntt * 2 - 1);
            ans = max(ans, res);
        }
        if(cnt[ nokori[j] ] != 0)   j++;
    }

    cout << ans << endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:58:15: error: no match for 'operator[]' (operand types are 'std::map<int, int>' and '__gnu_cxx::__alloc_traits<std::allocator<std::pair<long long int, long long int> >, std::pair<long long int, long long int> >::value_type' {aka 'std::pair<long long int, long long int>'})
   58 |         if(cnt[ nokori[j] ] != 0)   j++;
      |               ^
In file included from /usr/include/c++/14/map:63,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:152,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_map.h:504:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = int; _Tp = int; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, int> >; mapped_type = int; key_type = int]'
  504 |       operator[](const key_type& __k)
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_map.h:504:34: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::pair<long long int, long long int> >, std::pair<long long int, long long int> >::value_type' {aka 'std::pair<long long int, long long int>'} to 'const std::map<int, int>::key_type&' {aka 'const int&'}
  504 |       operator[](const key_type& __k)
      |                  ~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_map.h:524:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](key_type&&) [with _Key = int; _Tp = int; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, int> >; mapped_type = int; key_type = int]'
  524 |       operator[](key_type&& __k)
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_map.h:524:29: note:   no known conversion for argument 1 from '__gnu_cxx::__alloc_traits<std::allocator<std::pair<long long int, long long int> >, std::pair<long long int, long long int> >::value_type' {aka 'std::pair<long long int, long long int>'} to 'std::map<int, int>::key_type&&' {aka 'int&&'}
  524 |       operator[](key_type&& __k)
      |                  ~~~~~~~~~~~^~~