Language: C++

#include <algorithm>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>

using namespace std;
typedef tuple<ll, ll> sushi;
typedef long long ll;
//貪欲法 Greedy

ll main() {
  ll N, K;
  cin >> N >> K;
  vector<sushi> V(N);

  for (ll i = 0; i < N; i++) {
    ll t, d;
    cin >> t >> d;
    t--;
    V[i] = sushi(d, t);  // tupleの代入の仕方はこれ dtの順番に注意。
  }

  sort(V.begin(), V.end());
  reverse(V.begin(), V.end());  //降順ソート

  vector<ll> cnt(N + 1, 0);  // 1-indexed
  priority_queue<sushi, vector<sushi>, greater<sushi>> PQ;
  // priority_queueで昇順にするにはこう

  ll ans = 0;
  ll tmpans = 0;

  for (ll i = 0; i < K; i++) {
    ll val = get<0>(V[i]);  // tupleの要素取得はこれ 0-indexed
    ll kind = get<1>(V[i]);
    tmpans += val;
    cnt[kind]++;
    PQ.push(V[i]);
  }

  ll count_num = 0;  //何種類の寿司を使ったか
  for (ll i = 0; i < N; i++) {
    if (cnt[i] > 0) count_num++;
  }

  ans += tmpans + count_num * count_num;

  for (ll i = K; i < N; i++) {
    ll val = get<0>(V[i]);
    ll kind = get<1>(V[i]);

    if (cnt[kind] > 0) continue;  //種類count_numが増えるかどうか判定

    while (!PQ.empty()) {
      ll qval = get<0>(PQ.top());
      ll qkind = get<1>(PQ.top());
      PQ.pop();

      if (cnt[qkind] == 1) continue;
      //入れ替えると結局種類数が減るので何もしない
      //一つしかないので次回から考慮する必要がないのでpushしない

      cnt[qkind]--;
      tmpans -= qval;
      cnt[kind]++;
      tmpans += val;
      count_num++;
      ans = max(ans, tmpans + count_num * count_num);

      break;
    }
  }

  cout << ans << endl;
}


Compiler output:
a.cc:8:15: error: 'll' was not declared in this scope
    8 | typedef tuple<ll, ll> sushi;
      |               ^~
a.cc:8:19: error: 'll' was not declared in this scope
    8 | typedef tuple<ll, ll> sushi;
      |                   ^~
a.cc:8:21: error: template argument 1 is invalid
    8 | typedef tuple<ll, ll> sushi;
      |                     ^
a.cc:8:21: error: template argument 2 is invalid
a.cc:12:1: error: '::main' must return 'int'
   12 | ll main() {
      | ^~
a.cc: In function 'int main()':
a.cc:21:22: error: expression list treated as compound expression in functional cast [-fpermissive]
   21 |     V[i] = sushi(d, t);  // tupleの代入の仕方はこれ dtの順番に注意。
      |                      ^
a.cc:35:20: error: no matching function for call to 'get<0>(__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&)'
   35 |     ll val = get<0>(V[i]);  // tupleの要素取得はこれ 0-indexed
      |              ~~~~~~^~~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_pair.h:1250:5: note: candidate: 'template<long unsigned int _Int, class _Tp1, class _Tp2> constexpr typename std::tuple_element<_Int, std::pair<_Tp1, _Tp2> >::type& std::get(pair<_Tp1, _Tp2>&)'
 1250 |     get(pair<_Tp1, _Tp2>& __in) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1250:5: note:   template argument deduction/substitution failed:
a.cc:35:20: note:   mismatched types 'std::pair<_Tp1, _Tp2>' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   35 |     ll val = get<0>(V[i]);  // tupleの要素取得はこれ 0-indexed
      |              ~~~~~~^~~~~~
/usr/include/c++/14/bits/stl_pair.h:1255:5: note: candidate: 'template<long unsigned int _Int, class _Tp1, class _Tp2> constexpr typename std::tuple_element<_Int, std::pair<_Tp1, _Tp2> >::type&& std::get(pair<_Tp1, _Tp2>&&)'
 1255 |     get(pair<_Tp1, _Tp2>&& __in) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1255:5: note:   template argument deduction/substitution failed:
a.cc:35:20: note:   mismatched types 'std::pair<_Tp1, _Tp2>' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   35 |     ll val = get<0>(V[i]);  // tupleの要素取得はこれ 0-indexed
      |              ~~~~~~^~~~~~
/usr/include/c++/14/bits/stl_pair.h:1260:5: note: candidate: 'template<long unsigned int _Int, class _Tp1, class _Tp2> constexpr const typename std::tuple_element<_Int, std::pair<_Tp1, _Tp2> >::type& std::get(const pair<_Tp1, _Tp2>&)'
 1260 |     get(const pair<_Tp1, _Tp2>& __in) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1260:5: note:   template argument deduction/substitution failed:
a.cc:35:20: note:   mismatched types 'const std::pair<_Tp1, _Tp2>' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   35 |     ll val = get<0>(V[i]);  // tupleの要素取得はこれ 0-indexed
      |              ~~~~~~^~~~~~
/usr/include/c++/14/bits/stl_pair.h:1265:5: note: candidate: 'template<long unsigned int _Int, class _Tp1, class _Tp2> constexpr const typename std::tuple_element<_Int, std::pair<_Tp1, _Tp2> >::type&& std::get(const pair<_Tp1, _Tp2>&&)'
 1265 |     get(const pair<_Tp1, _Tp2>&& __in) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1265:5: note:   template argument deduction/substitution failed:
a.cc:35:20: note:   mismatched types 'const std::pair<_Tp1, _Tp2>' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   35 |     ll val = get<0>(V[i]);  // tupleの要素取得はこれ 0-indexed
      |              ~~~~~~^~~~~~
In file included from /usr/include/c++/14/bits/memory_resource.h:47,
                 from /usr/include/c++/14/string:68,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:2:
/usr/include/c++/14/tuple:2445:5: note: candidate: 'template<long unsigned int __i, class ... _Elements> constexpr std::__tuple_element_t<__i, std::tuple<_Elements ...> >& std::get(tuple<_Elements ...>&)'
 2445 |     get(tuple<_Elements...>& __t) noexcept
      |     ^~~
/usr/include/c++/14/tuple:2445:5: note:   template argument deduction/substitution failed:
a.cc:35:20: note:   mismatched types 'std::tuple<_Elements ...>' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   35 |     ll val = get<0>(V[i]);  // tupleの要素取得はこれ 0-indexed
      |              ~~~~~~^~~~~~
/usr/include/c++/14/tuple:2451:5: note: candidate: 'template<long unsigned int __i, class ... _Elements> constexpr std::__tuple_element_t<__i, std::tuple<_Elements ...> >& std::get(const tuple<_Elements ...>&)'
 2451 |     get(const tuple<_Elements...>& __t) noexcept
      |     ^~~
/usr/include/c++/14/tuple:2451:5: note:   template argument deduction/substitution failed:
a.cc:35:20: note:   mismatched types 'const std::tuple<_Elements ...>' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   35 |     ll val = get<0>(V[i]);  // tupleの要素取得はこれ 0-indexed
      |              ~~~~~~^~~~~~
/usr/include/c++/14/tuple:2457:5: note: candidate: 'template<long unsigned int __i, class ... _Elements> constexpr std::__tuple_element_t<__i, std::tuple<_Elements ...> >&& std::get(tuple<_Elements ...>&&)'
 2457 |     get(tuple<_Elements...>&& __t) noexcept
      |     ^~~
/usr/include/c++/14/tuple:2457:5: note:   template argument deduction/substitution failed:
a.cc:35:20: note:   mismatched types 'std::tuple<_Elements ...>' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   35 |     ll val = get<0>(V[i]);  // tupleの要素取得はこれ 0-indexed
      |              ~~~~~~^~~~~~
/usr/include/c++/14/tuple:2466:5: note: candidate: 'template<long unsigned int __i, class ... _Elements> constexpr std::__tuple_element_t<__i, std::tuple<_Elements ...> >&& std::get(const tuple<_Elements ...>&&)'
 2466 |     get(const tuple<_Elements...>&& __t) noexcept
      |     ^~~
/usr/include/c++/14/tuple:2466:5: note:   template argument deduction/substitution failed:
a.cc:35:20: note:   mismatched types 'const std::tuple<_Elements ...>' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   35 |     ll val = get<0>(V[i]);  // tupleの要素取得はこれ 0-indexed
      |              ~~~~~~^~~~~~
/usr/include/c++/14/bits/stl_pair.h:138:5: note: candidate: 'template<long unsigned int _Int, class _Tp, long unsigned int _Nm> constexpr _Tp& std::get(array<_Tp, _Nm>&)'
  138 |     get(array<_Tp, _Nm>&) noexcept;
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:138:5: note:   template argument deduction/substitution failed:
a.cc:35:20: note:   mismatched types 'std::array<_Tp, _Nm>' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   35 |     ll val = get<0>(V[i]);  // tupleの要素取得はこれ 0-indexed
      |              ~~~~~~^~~~~~
/usr/include/c++/14/bits/stl_pair.h:142:5: note: candidate: 'template<long unsigned int _Int, class _Tp, long unsigned int _Nm> constexpr _Tp&& std::get(array<_Tp, _Nm>&&)'
  142 |     get(array<_Tp, _Nm>&&) noexcept;
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:142:5: note:   template argument deduction/substitution failed:
a.cc:35:20: note:   mismatched types 'std::array<_Tp, _Nm>' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   35 |     ll val = get<0>(V[i]);  // tupleの要素取得はこれ 0-indexed
      |              ~~~~~~^~~~~~
/usr/include/c++/14/bits/stl_pair.h:146:5: note: candidate: 'template<long unsigned int _Int, class _Tp, long unsigned int _Nm> constexpr const _Tp& std::get(const array<_Tp, _Nm>&)'
  146 |     get(const array<_Tp, _Nm>&) noexcept;
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:146:5: note:   template argument deduction/substitution failed:
a.cc:35:20: note:   mismatched types 'const std::array<_Tp, _Nm>' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   35 |     ll val = get<0>(V[i]);  // tupleの要素取得はこれ 0-indexed
      |              ~~~~~~^~~~~~
/usr/include/c++/14/bits/stl_pair.h:150:5: note: candidate: 'template<long unsigned int _Int, class _Tp, long unsigned int _Nm> constexpr const _Tp&& std::get(const array<_Tp, _Nm>&&)'
  150 |     get(const array<_Tp, _Nm>&&) noexcept;
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:150:5: note:   template argument deduction/substitution failed:
a.cc:35:20: note:   mismatched types 'const std::array<_Tp, _Nm>' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   35 |     ll val = get<0>(V[i]);  // tupleの要素取得はこれ 0-indexed
      |              ~~~~~~^~~~~~
/usr/include/c++/14/bits/stl_pair.h:1272:5: note: candidate: 'template<class _Tp, class _Up> constexpr _Tp& std::get(pair<_T1, _T2>&)'
 1272 |     get(pair<_Tp, _Up>& __p) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1272:5: note:   template argument deduction/substitution failed:
a.cc:35:20: error: type/value mismatch at argument 1 in template parameter list for 'template<class _Tp, class _Up> constexpr _Tp& std::get(pair<_T1, _T2>&)'
   35 |     ll val = get<0>(V[i]);  // tupleの要素取得はこれ 0-indexed
      |              ~~~~~~^~~~~~
a.cc:35:20: note:   expected a type, got '0'
/usr/include/c++/14/bits/stl_pair.h:1277:5: note: candidate: 'template<class _Tp, class _Up> constexpr const _Tp& std::get(const pair<_T1, _T2>&)'
 1277 |     get(const pair<_Tp, _Up>& __p) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1277:5: note:   template argument deduction/substitution failed:
a.cc:35:20: error: type/value mismatch at argument 1 in template parameter list for 'template<class _Tp, class _Up> constexpr const _Tp& std::get(const pair<_T1, _T2>&)'
   35 |