Language: C++

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Sushi{
    public:
        Sushi(int t = 0, int d = 0){
            kind = t;
            taste = d;
        }
        int kind;
        long taste;
};
void removeTop(vector<long>& heap){
    heap[0] = heap[heap.size() - 1];
    heap.pop_back();
    int i = 0;
    int n = heap.size() - 1;
    int max;
    while(2 * i + 1 <= n){ // 左の子が存在する
        if(heap[i] < heap[2 * i + 1]){ // 左の子のほうが大きい
            long temp = heap[i];
            if(2 * i + 2 <= n && heap[2 * i + 1] < heap[2 * i + 2]){ // 右の子が存在し、右の子のほうがより大きい
                heap[i] = heap[2 * i + 2];
                heap[2 * i + 2] = temp;
                i = 2 * i + 2;
            }
            else{ // 右の子が存在しないか、左の子のほうがより大きい
                heap[i] = heap[2 * i + 1];
                heap[2 * i + 1] = temp;
                i = 2 * i + 1;
            }
        }
        else if(2 * i + 2 <= n && heap[2 * i + 1] < heap[2 * i + 2]){ // 右の子が存在し、右の子のほうが大きい
            long temp = heap[i];
            heap[i] = heap[2 * i + 2];
            heap[2 * i + 2] = temp;
            i = 2 * i + 2;
        }
        else{
            break;
        }
    }
}
int main(void)
{
    bool debug = false;
    int N, K;
    cin >> N >> K;
    Sushi list[100000] = {};
    int t, d;
    for(int i = 0; i < N; i++){
        cin >> t >> d;
        list[i] = Sushi(t, d);
    }
    vector<vector<long>> eat(N);
    vector<vector<long>> noteat(N);
    sort(list, list+100000, [](const Sushi& x, const Sushi& y){return x.taste > y.taste;});
    long long result = 0;
    int kind = 0;
    Sushi loop;
    for(int i = 0; i < N; i++){
        loop = list[i];
        if(i < K){
            eat[loop.kind - 1].push_back(loop.taste);
            result += loop.taste;
            if(eat[loop.kind - 1].size() == 1){
                kind++;
            }
        }
        else{
            noteat[loop.kind - 1].push_back(loop.taste);
        }
    }
    result += kind * kind;
    long long temp = result;
    int min_index;
    long min;
    int max_index;
    long max;
    for(int i = kind + 1; i < N; i++){
        min = LONG_MAX;
        max = LONG_MIN;
        for(int j = 0; j < N; j++){ 
            if(eat[j].size() > 1 && min > eat[j][eat[j].size() - 1]){
                min_index = j;
                min = eat[j][eat[j].size() - 1];
            }
            else if(eat[j].size() == 0 && noteat[j].size() > 0 && max < noteat[j][0]){
                max_index = j;
                max = noteat[j][0];
            }
        }
        if(max == LONG_MIN){
            break;
        }
        temp += max - min;
        temp += (kind + 1) * (kind + 1) - kind * kind;
        eat[min_index].pop_back();
        eat[max_index].push_back(max);
        removeTop(noteat[max_index]);
        kind++;
        if(temp > result){
            result = temp;
        }
    }
    cout << result << "\n";
}

Compiler output:
a.cc: In function 'int main()':
a.cc:82:15: error: 'LONG_MAX' was not declared in this scope
   82 |         min = LONG_MAX;
      |               ^~~~~~~~
a.cc:4:1: note: 'LONG_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
    3 | #include <algorithm>
  +++ |+#include <climits>
    4 | using namespace std;
a.cc:83:15: error: 'LONG_MIN' was not declared in this scope
   83 |         max = LONG_MIN;
      |               ^~~~~~~~
a.cc:83:15: note: 'LONG_MIN' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'