from collections import *

n, k = list(map(int, input().split()))

d, t = list(zip(*sorted(tuple(map(int, input().split()))[::-1] for _ in range(n))[::-1]))

c = Counter(t[:k])

x = len(c)

a = [sum(d[:k])]

for i in range(k)[::-1]:

    if c[t[i]] > 1:

        c[t[i]] -= 1

        a.append(a[-1] - d[i])

b = [0]

for i in range(k, n):

    if c[t[i]] <= 0:

        c[t[i]] += 1

        b.append(b[-1] + d[i])

r = 0

for i, (v, w) in enumerate(zip(a, b)):

    r = max(r, v+w+(x+i)**2)

print(r)
n, k =list(map(int, input().split()))

dt = sorted(tuple(map(int, input().split()))[::-1] for _ in range(n))[::-1]

r = v = 0

a = []

s = set()

for i, (d,t) in enumerate(dt, 1):

    if t in s:

        if i <= k:

            a.append(d)

            v += d

    else:

        if i > k:

            if not a:

                break

            v -= a.pop()

        v += d

        s.add(t)

    r = max(r, len(s)**2+v)

print(r)
from collections import *

n, k = list(map(int, input().split()))
d, t = list(
    zip(*sorted(tuple(map(int, input().split()))[::-1] for _ in range(n))[::-1])
)
c = Counter(t[:k])
x = len(c)
a = [sum(d[:k])]
for i in range(k)[::-1]:
    if c[t[i]] > 1:
        c[t[i]] -= 1
        a.append(a[-1] - d[i])
b = [0]
for i in range(k, n):
    if c[t[i]] <= 0:
        c[t[i]] += 1
        b.append(b[-1] + d[i])
r = 0
for i, (v, w) in enumerate(zip(a, b)):
    r = max(r, v + w + (x + i) ** 2)
print(r)

n, k = list(map(int, input().split()))
dt = sorted(tuple(map(int, input().split()))[::-1] for _ in range(n))[::-1]
r = v = 0
a = []
s = set()
for i, (d, t) in enumerate(dt, 1):
    if t in s:
        if i <= k:
            a.append(d)
            v += d
    else:
        if i > k:
            if not a:
                break
            v -= a.pop()
        v += d
        s.add(t)
    r = max(r, len(s) ** 2 + v)
print(r)

-from collections import * - -d, t = list( -    zip(*sorted(tuple(map(int, input().split()))[::-1] for _ in range(n))[::-1]) -) -c = Counter(t[:k]) -x = len(c) -a = [sum(d[:k])] -for i in range(k)[::-1]: -    if c[t[i]] > 1: -        c[t[i]] -= 1 -        a.append(a[-1] - d[i]) -b = [0] -for i in range(k, n): -    if c[t[i]] <= 0: -        c[t[i]] += 1 -        b.append(b[-1] + d[i]) -r = 0 -for i, (v, w) in enumerate(zip(a, b)): -    r = max(r, v + w + (x + i) ** 2) +dt = sorted(tuple(map(int, input().split()))[::-1] for _ in range(n))[::-1] +r = v = 0 +a = [] +s = set() +for i, (d, t) in enumerate(dt, 1): +    if t in s: +        if i <= k: +            a.append(d) +            v += d +    else: +        if i > k: +            if not a: +                break +            v -= a.pop() +        v += d +        s.add(t) +    r = max(r, len(s) ** 2 + v)