from operator import itemgetter



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

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



TD.sort(key=itemgetter(1), reverse=True)

selected = TD[:K]

dlc = sum(map(itemgetter(1), selected))

variety = set(map(itemgetter(0), selected))



used = set()

overlap = [] # ダブっているネタの美味しさのリスト

for t, d in TD[:K]:

    if t not in used:

        used.add(t)

    else:

        overlap.append(d)

answer = dlc + len(variety)**2



for t, d in TD[K:]:

    if not overlap:

        break

    if t in used:

        continue

    ov = overlap.pop()

    dlc = dlc - ov + d

    used.add(t)

    answer = max(answer, dlc + len(used)**2)



print(answer)

import sys



sr = lambda: sys.stdin.readline().rstrip()

ir = lambda: int(sr())

lr = lambda: list(map(int, sr().split()))



N, K = lr()

TD = [lr() for _ in range(N)]

TD.sort(key=lambda x: x[1], reverse=True)

used = set()

overlap = []

for t, d in TD[:K]:

    if t in used:

        overlap.append(d)

    else:

        used.add(t)



delicious = sum([x[1] for x in TD[:K]])

answer = delicious + len(used) ** 2

for i in range(K, N):

    if not overlap:

        break

    if TD[i][0] in used:

        continue

    delicious += (TD[i][1] -  overlap.pop())

    used.add(TD[i][0])

    if answer < delicious + len(used)**2:

        answer = delicious + len(used)**2



print(answer)

# 30
from operator import itemgetter

N, K = list(map(int, input().split()))
TD = [list(map(int, input().split())) for _ in range(N)]
TD.sort(key=itemgetter(1), reverse=True)
selected = TD[:K]
dlc = sum(map(itemgetter(1), selected))
variety = set(map(itemgetter(0), selected))
used = set()
overlap = []  # ダブっているネタの美味しさのリスト
for t, d in TD[:K]:
    if t not in used:
        used.add(t)
    else:
        overlap.append(d)
answer = dlc + len(variety) ** 2
for t, d in TD[K:]:
    if not overlap:
        break
    if t in used:
        continue
    ov = overlap.pop()
    dlc = dlc - ov + d
    used.add(t)
    answer = max(answer, dlc + len(used) ** 2)
print(answer)

import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
N, K = lr()
TD = [lr() for _ in range(N)]
TD.sort(key=lambda x: x[1], reverse=True)
used = set()
overlap = []
for t, d in TD[:K]:
    if t in used:
        overlap.append(d)
    else:
        used.add(t)
delicious = sum([x[1] for x in TD[:K]])
answer = delicious + len(used) ** 2
for i in range(K, N):
    if not overlap:
        break
    if TD[i][0] in used:
        continue
    delicious += TD[i][1] - overlap.pop()
    used.add(TD[i][0])
    if answer < delicious + len(used) ** 2:
        answer = delicious + len(used) ** 2
print(answer)
# 30

-from operator import itemgetter +import sys -N, K = list(map(int, input().split())) -TD = [list(map(int, input().split())) for _ in range(N)] -TD.sort(key=itemgetter(1), reverse=True) -selected = TD[:K] -dlc = sum(map(itemgetter(1), selected)) -variety = set(map(itemgetter(0), selected)) +sr = lambda: sys.stdin.readline().rstrip() +ir = lambda: int(sr()) +lr = lambda: list(map(int, sr().split())) +N, K = lr() +TD = [lr() for _ in range(N)] +TD.sort(key=lambda x: x[1], reverse=True) -overlap = []  # ダブっているネタの美味しさのリスト +overlap = [] -    if t not in used: +    if t in used: +        overlap.append(d) +    else: -    else: -        overlap.append(d) -answer = dlc + len(variety) ** 2 -for t, d in TD[K:]: +delicious = sum([x[1] for x in TD[:K]]) +answer = delicious + len(used) ** 2 +for i in range(K, N): -    if t in used: +    if TD[i][0] in used: -    ov = overlap.pop() -    dlc = dlc - ov + d -    used.add(t) -    answer = max(answer, dlc + len(used) ** 2) +    delicious += TD[i][1] - overlap.pop() +    used.add(TD[i][0]) +    if answer < delicious + len(used) ** 2: +        answer = delicious + len(used) ** 2 +# 30