import sys

input = sys.stdin.readline

from collections import *

from heapq import *



N, K = list(map(int, input().split()))

td = [tuple(map(int, input().split())) for _ in range(N)]

td.sort(key=lambda t: t[1], reverse=True)

d = defaultdict(list)

now = 0



for ti, di in td[:K]:

    d[ti].append(di)

    now += di



remove = []



for k in list(d.keys()):

    d[k].sort(reverse=True)

    

    for i in range(1, len(d[k])):

        heappush(remove, d[k][i])

    

s = set(list(d.keys()))

add = []



for ti, di in td:

    if ti in s:

        continue

    

    s.add(ti)

    heappush(add, -di)



x = len(d)

ans = now+x**2



while len(remove)>0 and len(add)>0:

    now -= heappop(remove)

    now -= heappop(add)

    x += 1

    ans = max(ans, now+x**2)



print(ans)
import sys

input = sys.stdin.readline

from collections import *



N, K = list(map(int, input().split()))

td = [tuple(map(int, input().split())) for _ in range(N)]

td.sort(key=lambda t: t[1], reverse=True)

d = defaultdict(list)

now = 0



for ti, di in td[:K]:

    d[ti].append(di)

    now += di



rem = []



for k in list(d.keys()):

    d[k].sort(reverse=True)

    

    for i in range(1, len(d[k])):

        rem.append(d[k][i])



add = []

s = set()



for ti, di in td[K:]:

    if ti not in d and ti not in s:

        add.append(di)

        s.add(ti)



rem.sort()

add.sort(reverse=True)

var = len(list(d.keys()))

ans = now+var**2



for i in range(min(len(rem), len(add))):

    now -= rem[i]

    now += add[i]

    ans = max(ans, now+(var+i+1)**2)



print(ans)
import sys

input = sys.stdin.readline
from collections import *
from heapq import *

N, K = list(map(int, input().split()))
td = [tuple(map(int, input().split())) for _ in range(N)]
td.sort(key=lambda t: t[1], reverse=True)
d = defaultdict(list)
now = 0
for ti, di in td[:K]:
    d[ti].append(di)
    now += di
remove = []
for k in list(d.keys()):
    d[k].sort(reverse=True)
    for i in range(1, len(d[k])):
        heappush(remove, d[k][i])
s = set(list(d.keys()))
add = []
for ti, di in td:
    if ti in s:
        continue
    s.add(ti)
    heappush(add, -di)
x = len(d)
ans = now + x**2
while len(remove) > 0 and len(add) > 0:
    now -= heappop(remove)
    now -= heappop(add)
    x += 1
    ans = max(ans, now + x**2)
print(ans)

import sys

input = sys.stdin.readline
from collections import *

N, K = list(map(int, input().split()))
td = [tuple(map(int, input().split())) for _ in range(N)]
td.sort(key=lambda t: t[1], reverse=True)
d = defaultdict(list)
now = 0
for ti, di in td[:K]:
    d[ti].append(di)
    now += di
rem = []
for k in list(d.keys()):
    d[k].sort(reverse=True)
    for i in range(1, len(d[k])):
        rem.append(d[k][i])
add = []
s = set()
for ti, di in td[K:]:
    if ti not in d and ti not in s:
        add.append(di)
        s.add(ti)
rem.sort()
add.sort(reverse=True)
var = len(list(d.keys()))
ans = now + var**2
for i in range(min(len(rem), len(add))):
    now -= rem[i]
    now += add[i]
    ans = max(ans, now + (var + i + 1) ** 2)
print(ans)

-from heapq import * -remove = [] +rem = [] -        heappush(remove, d[k][i]) -s = set(list(d.keys())) +        rem.append(d[k][i]) -for ti, di in td: -    if ti in s: -        continue -    s.add(ti) -    heappush(add, -di) -x = len(d) -ans = now + x**2 -while len(remove) > 0 and len(add) > 0: -    now -= heappop(remove) -    now -= heappop(add) -    x += 1 -    ans = max(ans, now + x**2) +s = set() +for ti, di in td[K:]: +    if ti not in d and ti not in s: +        add.append(di) +        s.add(ti) +rem.sort() +add.sort(reverse=True) +var = len(list(d.keys())) +ans = now + var**2 +for i in range(min(len(rem), len(add))): +    now -= rem[i] +    now += add[i] +    ans = max(ans, now + (var + i + 1) ** 2)