import heapq

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

z=[0,1]

for i in range(1,n):

    z.append(z[i]+2)

s=sorted([list(map(int,input().split()))for _ in range(n)],key=lambda x:-x[1])

q=[]

v=0

ans=0

l=[0]*-~n

for a in s[:k]:

    if not l[a[0]]:

        v+=1

        ans+=z[v]

    else:

        heapq.heappush(q,a[1])

    l[a[0]]=1

    ans+=a[1]

tem=ans

for a in s[k:]:

    if not q:break

    if l[a[0]]:continue

    l[a[0]]=1

    v+=1

    t=heapq.heappop(q)

    tem=tem-t+a[1]+z[v]

    ans=max(ans,tem)

print(ans)
import heapq as h;I=lambda:list(map(int,input().split()));n,k=I();z=[i*2+1for i in range(n)];s=sorted([I()for _ in[0]*n],key=lambda x:-x[1]);q=[];v=A=0;l=[0]*-~n

for a in s[:k]:

 if l[a[0]]:h.heappush(q,a[1])

 else:A+=z[v];v+=1

 l[a[0]]=1;A+=a[1]

T=A

for a in s[k:]:

 if not q:break

 if l[a[0]]^1:l[a[0]]=1;t=h.heappop(q);T=T-t+a[1]+z[v];v+=1;A=max(A,T)

print(A)
import heapq

n, k = list(map(int, input().split()))
z = [0, 1]
for i in range(1, n):
    z.append(z[i] + 2)
s = sorted([list(map(int, input().split())) for _ in range(n)], key=lambda x: -x[1])
q = []
v = 0
ans = 0
l = [0] * -~n
for a in s[:k]:
    if not l[a[0]]:
        v += 1
        ans += z[v]
    else:
        heapq.heappush(q, a[1])
    l[a[0]] = 1
    ans += a[1]
tem = ans
for a in s[k:]:
    if not q:
        break
    if l[a[0]]:
        continue
    l[a[0]] = 1
    v += 1
    t = heapq.heappop(q)
    tem = tem - t + a[1] + z[v]
    ans = max(ans, tem)
print(ans)

import heapq as h

I = lambda: list(map(int, input().split()))
n, k = I()
z = [i * 2 + 1 for i in range(n)]
s = sorted([I() for _ in [0] * n], key=lambda x: -x[1])
q = []
v = A = 0
l = [0] * -~n
for a in s[:k]:
    if l[a[0]]:
        h.heappush(q, a[1])
    else:
        A += z[v]
        v += 1
    l[a[0]] = 1
    A += a[1]
T = A
for a in s[k:]:
    if not q:
        break
    if l[a[0]] ^ 1:
        l[a[0]] = 1
        t = h.heappop(q)
        T = T - t + a[1] + z[v]
        v += 1
        A = max(A, T)
print(A)

-import heapq +import heapq as h -n, k = list(map(int, input().split())) -z = [0, 1] -for i in range(1, n): -    z.append(z[i] + 2) -s = sorted([list(map(int, input().split())) for _ in range(n)], key=lambda x: -x[1]) +I = lambda: list(map(int, input().split())) +n, k = I() +z = [i * 2 + 1 for i in range(n)] +s = sorted([I() for _ in [0] * n], key=lambda x: -x[1]) -v = 0 -ans = 0 +v = A = 0 -    if not l[a[0]]: +    if l[a[0]]: +        h.heappush(q, a[1]) +    else: +        A += z[v] -        ans += z[v] -    else: -        heapq.heappush(q, a[1]) -    ans += a[1] -tem = ans +    A += a[1] +T = A -    if l[a[0]]: -        continue -    l[a[0]] = 1 -    v += 1 -    t = heapq.heappop(q) -    tem = tem - t + a[1] + z[v] -    ans = max(ans, tem) -print(ans) +    if l[a[0]] ^ 1: +        l[a[0]] = 1 +        t = h.heappop(q) +        T = T - t + a[1] + z[v] +        v += 1 +        A = max(A, T) +print(A)