Language: C++

using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Numerics;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using Debug = System.Diagnostics.Debug;
using System.Runtime.CompilerServices;
using static System.Math;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;

static class P
{
    static void Main()
    {
        //k種類の寿司を各種類毎に
        var nk = Console.ReadLine().Split().Select(int.Parse).ToArray();
        var n = nk[0];
        var k = nk[1];
        var grouped = Enumerable.Repeat(0, n).Select(_ => Console.ReadLine().Split().Select(int.Parse).ToArray()).GroupBy(x => x[0]).Select(x => x.Select(y => y[1]).OrderByDescending(y => y).ToArray()).OrderByDescending(x => x[0]).ToArray();
        var candidate = grouped.SelectMany(x => x.Skip(1)).OrderByDescending(x => x).ToArray();
        int candidatePtr = 0;
        long score = grouped.Take(k).Sum(x => (long)x.First());
        long variousCount = Min(grouped.Length, k);
        int groupPtr = (int)variousCount - 1;
        for (long i = variousCount + 1; i <= k; i++) score += candidate[candidatePtr++];
        while (0 <= groupPtr && candidatePtr < candidate.Length && variousCount * variousCount - (variousCount - 1) * (variousCount - 1) <= candidate[candidatePtr] - grouped[groupPtr][0])
        {
            variousCount--;
            score += candidate[candidatePtr++];
            score -= grouped[groupPtr--][0];
        }
        Console.WriteLine(score + variousCount * variousCount);
    }
}


Compiler output:
a.cc:1:7: error: expected nested-name-specifier before 'System'
    1 | using System;
      |       ^~~~~~
a.cc:2:7: error: expected nested-name-specifier before 'System'
    2 | using System.IO;
      |       ^~~~~~
a.cc:3:7: error: expected nested-name-specifier before 'System'
    3 | using System.Linq;
      |       ^~~~~~
a.cc:4:7: error: expected nested-name-specifier before 'System'
    4 | using System.Collections;
      |       ^~~~~~
a.cc:5:7: error: expected nested-name-specifier before 'System'
    5 | using System.Collections.Generic;
      |       ^~~~~~
a.cc:6:7: error: expected nested-name-specifier before 'System'
    6 | using System.Text;
      |       ^~~~~~
a.cc:7:7: error: expected nested-name-specifier before 'System'
    7 | using System.Numerics;
      |       ^~~~~~
a.cc:8:7: error: expected nested-name-specifier before 'System'
    8 | using System.Threading.Tasks;
      |       ^~~~~~
a.cc:9:7: error: expected nested-name-specifier before 'System'
    9 | using System.Text.RegularExpressions;
      |       ^~~~~~
a.cc:10:15: error: 'System' does not name a type
   10 | using Debug = System.Diagnostics.Debug;
      |               ^~~~~~
a.cc:11:7: error: expected nested-name-specifier before 'System'
   11 | using System.Runtime.CompilerServices;
      |       ^~~~~~
a.cc:12:7: error: expected nested-name-specifier before 'static'
   12 | using static System.Math;
      |       ^~~~~~
a.cc:13:27: error: 'System' does not name a type
   13 | using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;
      |                           ^~~~~~
a.cc:14:29: error: 'System' does not name a type
   14 | using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;
      |                             ^~~~~~
a.cc:39:2: error: expected ';' after class definition
   39 | }
      |  ^
      |  ;
a.cc: In static member function 'static void P::Main()':
a.cc:21:9: error: 'var' was not declared in this scope
   21 |         var nk = Console.ReadLine().Split().Select(int.Parse).ToArray();
      |         ^~~
a.cc:22:13: error: expected ';' before 'n'
   22 |         var n = nk[0];
      |             ^
a.cc:23:13: error: expected ';' before 'k'
   23 |         var k = nk[1];
      |             ^
a.cc:24:13: error: expected ';' before 'grouped'
   24 |         var grouped = Enumerable.Repeat(0, n).Select(_ => Console.ReadLine().Split().Select(int.Parse).ToArray()).GroupBy(x => x[0]).Select(x => x.Select(y => y[1]).OrderByDescending(y => y).ToArray()).OrderByDescending(x => x[0]).ToArray();
      |             ^~~~~~~
a.cc:25:13: error: expected ';' before 'candidate'
   25 |         var candidate = grouped.SelectMany(x => x.Skip(1)).OrderByDescending(x => x).ToArray();
      |             ^~~~~~~~~
a.cc:27:22: error: 'grouped' was not declared in this scope
   27 |         long score = grouped.Take(k).Sum(x => (long)x.First());
      |                      ^~~~~~~
a.cc:27:35: error: 'k' was not declared in this scope
   27 |         long score = grouped.Take(k).Sum(x => (long)x.First());
      |                                   ^
a.cc:27:42: error: 'x' was not declared in this scope
   27 |         long score = grouped.Take(k).Sum(x => (long)x.First());
      |                                          ^
a.cc:27:45: error: expected primary-expression before '>' token
   27 |         long score = grouped.Take(k).Sum(x => (long)x.First());
      |                                             ^
a.cc:28:29: error: 'Min' was not declared in this scope; did you mean 'Main'?
   28 |         long variousCount = Min(grouped.Length, k);
      |                             ^~~
      |                             Main
a.cc:30:63: error: 'candidate' was not declared in this scope; did you mean 'candidatePtr'?
   30 |         for (long i = variousCount + 1; i <= k; i++) score += candidate[candidatePtr++];
      |                                                               ^~~~~~~~~
      |                                                               candidatePtr
a.cc:31:48: error: 'candidate' was not declared in this scope; did you mean 'candidatePtr'?
   31 |         while (0 <= groupPtr && candidatePtr < candidate.Length && variousCount * variousCount - (variousCount - 1) * (variousCount - 1) <= candidate[candidatePtr] - grouped[groupPtr][0])
      |                                                ^~~~~~~~~
      |                                                candidatePtr
a.cc:37:9: error: 'Console' was not declared in this scope
   37 |         Console.WriteLine(score + variousCount * variousCount);
      |         ^~~~~~~
a.cc: At global scope:
a.cc:16:1: error: a storage class can only be specified for objects and functions
   16 | static class P
      | ^~~~~~