Language: C++

#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <numeric>

#define ull unsigned long long
#define ul unsigned long
#define ll long long
#define size_t unsigned long long

using namespace std;

// std::sort(&a[0], &a[4], [](auto& x, auto& y){return x[0] < y[0];});

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int n,k;
  std::cin >> n >> k;
  int td[n][2];
  for (int i = 0; i < n; i++) {
    std::cin >> td[i][0];
    std::cin >> td[i][1];
  }

  sort(&td[0], &td[n], [](auto& x, auto& y){return x[1] > y[1];});

  vector<int> chofuku;
  int dp[100001];
  int ans = 0;
  for (size_t i = 0; i < k; i++) {
    ans += td[i][0];
    dp[td[i][1]]++;
    if(dp[td[i][1]] > 1){
      chofuku.push_back(i);
    }
  }

  int x = (k - chofuku.size());
  for(int i = k; k < n; i++){
    if(dp[td[i][1]] == 0){
      if(ans + x*x < ans - td[chofuku[chofuku.size() - 1]][0] + td[i][0] + (x+1)*(x+1)){
        dp[td[i][1]]++;
        chofuku.pop_back();
        x += 1;
        ans = ans - td[chofuku[chofuku.size() - 1]][0] + td[i][0];
      }
    }
  }

  return 0;
}


Compiler output:
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_algo.h: In instantiation of 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(auto:1&, auto:2&)> >]':
/usr/include/c++/14/bits/stl_algo.h:1817:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(auto:1&, auto:2&)> >]'
 1817 |           std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1908:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(auto:1&, auto:2&)> >]'
 1908 |           std::__final_insertion_sort(__first, __last, __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4805:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = int (*)[2]; _Compare = main()::<lambda(auto:1&, auto:2&)>]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:30:7:   required from here
   30 |   sort(&td[0], &td[n], [](auto& x, auto& y){return x[1] > y[1];});
      |   ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1780:17: error: array must be initialized with a brace-enclosed initializer
 1780 |                 __val = _GLIBCXX_MOVE(*__i);
      |                 ^~~~~
/usr/include/c++/14/bits/stl_algo.h:1782:24: error: invalid array assignment
 1782 |               *__first = _GLIBCXX_MOVE(__val);
      |                        ^
In file included from /usr/include/c++/14/bits/stl_algo.h:61:
/usr/include/c++/14/bits/stl_heap.h: In instantiation of 'void std::__make_heap(_RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(auto:1&, auto:2&)> >]':
/usr/include/c++/14/bits/stl_algo.h:1593:23:   required from 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(auto:1&, auto:2&)> >]'
 1593 |       std::__make_heap(__first, __middle, __comp);
      |       ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1868:25:   required from 'void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(auto:1&, auto:2&)> >]'
 1868 |       std::__heap_select(__first, __middle, __last, __comp);
      |       ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1884:27:   required from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = int (*)[2]; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(auto:1&, auto:2&)> >]'
 1884 |               std::__partial_sort(__first, __last, __last, __comp);
      |               ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1905:25:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(auto:1&, auto:2&)> >]'
 1905 |           std::__introsort_loop(__first, __last,
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
 1906 |                                 std::__lg(__last - __first) * 2,
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1907 |                                 __comp);
      |                                 ~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4805:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = int (*)[2]; _Compare = main()::<lambda(auto:1&, auto:2&)>]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:30:7:   required from here
   30 |   sort(&td[0], &td[n], [](auto& x, auto& y){return x[1] > y[1];});
      |   ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_heap.h:355:22: error: array must be initialized with a brace-enclosed initializer
  355 |           _ValueType __value = _GLIBCXX_MOVE(*(__first + __parent));
      |                      ^~~~~~~
/usr/include/c++/14/bits/stl_heap.h: In instantiation of 'void std::__pop_heap(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare&) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(auto:1&, auto:2&)> >]':
/usr/include/c++/14/bits/stl_algo.h:1596:19:   required from 'void std::__heap_select(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(auto:1&, auto:2&)> >]'
 1596 |           std::__pop_heap(__first, __middle, __i, __comp);
      |           ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1868:25:   required from 'void std::__partial_sort(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(auto:1&, auto:2&)> >]'
 1868 |       std::__heap_select(__first, __middle, __last, __comp);
      |       ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1884:27:   required from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = int (*)[2]; _Size = long int; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(auto:1&, auto:2&)> >]'
 1884 |               std::__partial_sort(__first, __last, __last, __comp);
      |               ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1905:25:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = int (*)[2]; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(auto:1&, auto:2&)> >]'
 1905 |           std::__introsort_loop(__first, __last,
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
 1906 |                                 std::__lg(__last - __first) * 2,
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1907 |                                 __comp);
      |                                 ~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4805:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = int (*)[2]; _Compare = main()::<lambda(auto:1&, auto:2&)>]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:30:7:   required from here
   30 |   sort(&td[0], &td[n], [](auto& x, auto& y){return x[1] > y[1];});
      |   ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_heap.h:262:18: error: array must be initialized with a brace-enclosed initializer
  262 |       _ValueType __value = _GLIBCXX_MOVE(*__result);
      |                  ^~~~~~~
/usr/include/c++/14/bits/stl_heap.h:263:17: error: invalid array assignment
  263 |       *__result = _GLIBCXX_MOVE(*__first);
      |                 ^