Language: C++

#include <cstdio>
#include <climits>
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <stack>
#include <algorithm>
#include <iomanip>
#include <map>
#include <queue>
#include <functional>
#include <numeric>
#include <chrono>
#include <cstdlib>
using ll = long long;
using namespace std;

const ll MOD = 1e9 + 7;
const double pi = acos(-1);
#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define EPS 1e-4
// bool operator<(const pair<int, int> &a, const pair<int, int> &b)
// {
//     if (a.first == b.first)
//         return a.second < b.second;
//     return a.first < b.first;
// };

/*unsigned int xor128(void)
{
    static unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123;
    unsigned int t = (x ^ (x << 11));
    x = y;
    y = z;
    z = w;
    return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
unsigned int xor128rnd(unsigned int m)
{
    return xor128() % m;
}*/

bool operator<(const pair<int, int> &a, const pair<int, int> &b)
{
    if (a.first == b.first)
    {
        return a.second < b.second;
    }
    return a.first < b.first;
}

ll arr[100010];
bool tab_check[100010];
vector<pair<ll, ll>> v;
vector<vector<ll>> v1(100010);

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(8);

    int N, K;
    cin >> N >> K;

    ll ans = 0;
    for (int i = 0; i < N; ++i)
    {
        ll t, d;
        cin >> t >> d;
        t--;
        v.push_back({d, t});
        v1[t].push_back(d);

        tab_check[t] = false;
    }

    ll sum = 0, cnt = 0;
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());

       for (int i = 0; i < K; ++i)
    {
        sum += v[i].first;
        if (!tab_check[v[i].second])
        {
            tab_check[v[i].second] = true;
            cnt++;
        }
    }
    ans = sum + cnt * cnt;
    //cout << ans << endl;

    //vのK番目以降を見ていく

    ll idx = K - 1;
    for (int i = K; i < N; ++i)
    {
        if (!tab_check[v[i].second])
        {
            while (idx >= 0 && v1[idx].size() == 1)
                idx--;
            tab_check[v[i].second] = true;
            cnt++;
            //cout << cnt << endl;#include <cstdio>
#include <climits>
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <stack>
#include <algorithm>
#include <iomanip>
#include <map>
#include <queue>
#include <functional>
#include <numeric>
#include <chrono>
#include <cstdlib>
using ll = long long;
using namespace std;

const ll MOD = 1e9 + 7;
const double pi = acos(-1);
#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define EPS 1e-4
// bool operator<(const pair<int, int> &a, const pair<int, int> &b)
// {
//     if (a.first == b.first)
//         return a.second < b.second;
//     return a.first < b.first;
// };

/*unsigned int xor128(void)
{
    static unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123;
    unsigned int t = (x ^ (x << 11));
    x = y;
    y = z;
    z = w;
    return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
unsigned int xor128rnd(unsigned int m)
{
    return xor128() % m;
}*/

bool operator<(const pair<int, int> &a, const pair<int, int> &b)
{
    if (a.first == b.first)
    {
        return a.second < b.second;
    }
    return a.first < b.first;
}

ll arr[100010];
bool tab_check[100010];
vector<pair<ll, ll>> v;
vector<vector<ll>> v1(100010);

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    //cout << fixed << setprecision(8);

    int N, K;
    cin >> N >> K;

    ll ans = 0;
    for (int i = 0; i < N; ++i)
    {
        ll t, d;
        cin >> t >> d;
        t--;
        v.push_back({d, t});
        v1[t].push_back(d);

        tab_check[t] = false;
    }

    ll sum = 0, cnt = 0;
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());

    for (int i = 0; i < K; ++i)
    {
        sum += v[i].first;
        if (!tab_check[v[i].second])
        {
            tab_check[v[i].second] = true;
            cnt++;
        }
    }
    ans = sum + cnt * cnt;
    //cout << ans << endl;

    //vのK番目以降を見ていく

    ll idx = K - 1;
    for (int i = K; i < N; ++i)
    {
        if (!tab_check[v[i].second])
        {
            while (idx >= 0 && v1[idx].size() == 1)
                idx--;
            tab_check[v[i].second] = true;
            cnt++;
            //cout << cnt << endl;
            sum = sum - v[idx].first + v[i].first;
            //cout << sum << endl;
            ans = max(ans, sum + cnt * cnt);
            if (idx >= 1)
                idx--;
        }
    }
    cout << ans << endl;
    return 0;
}

            sum = sum - v[idx].first + v[i].first;
            //cout << sum << endl;
            ans = max(ans, sum + cnt * cnt);
            idx--;
        }
    }
    cout << ans << endl;
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:154:1: error: a function-definition is not allowed here before '{' token
  154 | {
      | ^
a.cc:167:9: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
  167 | int main()
      |         ^~
a.cc:167:9: note: remove parentheses to default-initialize a variable
  167 | int main()
      |         ^~
      |         --
a.cc:167:9: note: or replace parentheses with braces to value-initialize a variable
a.cc:168:1: error: a function-definition is not allowed here before '{' token
  168 | {
      | ^