ri = lambda: int(eval(input()))

rl = lambda: list(map(int,input().split()))



N,K=rl()

td = [rl() for _ in range(N)]



from operator import itemgetter

key0 = itemgetter(0)

key1 = itemgetter(1)

td.sort(key=key1,reverse=True)



netas = set()

dup = []



for t,d in td[:K]:

    if t in netas:

        dup.append(d)

    else:

        netas.add(t)



score = len(netas)**2 + sum(map(key1,td[:K]))

ans = score

for t,d in td[K:]:

    if t not in netas and dup != []:

        netas.add(t)

        dup_ = dup.pop(-1)

        score += 2*len(netas)-1 + d - dup_

        ans = max(ans,score)

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

top = [0]*n

sub = []

for i in range(n):

    t,d = list(map(int,input().split()))

    t -=1

    if top[t]==0:

        top[t]=d

    else:

        if top[t]>=d:

            sub.append(d)

        else:

            sub.append(top[t])

            top[t]=d

 

top.sort()

top = top[::-1]

res = [0]

for i in range(n):

    if top[i]!=0:

        res.append(res[-1]+top[i])

    else:

        res.append(-1)

sub.sort()

sub = sub[::-1]

w = [0]

for i in range(len(sub)):

    w.append(w[-1]+sub[i])

tmp = 0

for i in range(1,k+1):

    if len(w)-1>=k-i and res[i]!=-1:

        tmp = max(res[i]+w[k-i]+i*i,tmp)

 

print(tmp)

    
ri = lambda: int(eval(input()))
rl = lambda: list(map(int, input().split()))
N, K = rl()
td = [rl() for _ in range(N)]
from operator import itemgetter

key0 = itemgetter(0)
key1 = itemgetter(1)
td.sort(key=key1, reverse=True)
netas = set()
dup = []
for t, d in td[:K]:
    if t in netas:
        dup.append(d)
    else:
        netas.add(t)
score = len(netas) ** 2 + sum(map(key1, td[:K]))
ans = score
for t, d in td[K:]:
    if t not in netas and dup != []:
        netas.add(t)
        dup_ = dup.pop(-1)
        score += 2 * len(netas) - 1 + d - dup_
        ans = max(ans, score)
print(ans)

n, k = list(map(int, input().split()))
top = [0] * n
sub = []
for i in range(n):
    t, d = list(map(int, input().split()))
    t -= 1
    if top[t] == 0:
        top[t] = d
    else:
        if top[t] >= d:
            sub.append(d)
        else:
            sub.append(top[t])
            top[t] = d
top.sort()
top = top[::-1]
res = [0]
for i in range(n):
    if top[i] != 0:
        res.append(res[-1] + top[i])
    else:
        res.append(-1)
sub.sort()
sub = sub[::-1]
w = [0]
for i in range(len(sub)):
    w.append(w[-1] + sub[i])
tmp = 0
for i in range(1, k + 1):
    if len(w) - 1 >= k - i and res[i] != -1:
        tmp = max(res[i] + w[k - i] + i * i, tmp)
print(tmp)

-ri = lambda: int(eval(input())) -rl = lambda: list(map(int, input().split())) -N, K = rl() -td = [rl() for _ in range(N)] -from operator import itemgetter - -key0 = itemgetter(0) -key1 = itemgetter(1) -td.sort(key=key1, reverse=True) -netas = set() -dup = [] -for t, d in td[:K]: -    if t in netas: -        dup.append(d) +n, k = list(map(int, input().split())) +top = [0] * n +sub = [] +for i in range(n): +    t, d = list(map(int, input().split())) +    t -= 1 +    if top[t] == 0: +        top[t] = d -        netas.add(t) -score = len(netas) ** 2 + sum(map(key1, td[:K])) -ans = score -for t, d in td[K:]: -    if t not in netas and dup != []: -        netas.add(t) -        dup_ = dup.pop(-1) -        score += 2 * len(netas) - 1 + d - dup_ -        ans = max(ans, score) -print(ans) +        if top[t] >= d: +            sub.append(d) +        else: +            sub.append(top[t]) +            top[t] = d +top.sort() +top = top[::-1] +res = [0] +for i in range(n): +    if top[i] != 0: +        res.append(res[-1] + top[i]) +    else: +        res.append(-1) +sub.sort() +sub = sub[::-1] +w = [0] +for i in range(len(sub)): +    w.append(w[-1] + sub[i]) +tmp = 0 +for i in range(1, k + 1): +    if len(w) - 1 >= k - i and res[i] != -1: +        tmp = max(res[i] + w[k - i] + i * i, tmp) +print(tmp)