import sys, itertools

#def input(): return sys.stdin.readline()

def inpl(): return [int(i) for i in input().split()]

N, K = inpl()

Sushi = []

Neta = set()

ans = 0

for _ in range(N):

    t, d = inpl()

    Sushi.append((d, t))

Sushi.sort(reverse = True)

Db = []

Nd = []

for i in range(K):

    d, t = Sushi[i]

    if t not in Neta:

        Neta.add(t)

        ans += d

    else:

        Db.append(d)

neta = len(Neta)

Db = [0] + list(itertools.accumulate(Db))

Db.reverse()

for i in range(N-K):

    d, t = Sushi[K+i]

    if t not in Neta:

        Neta.add(t)

        Nd.append(d)

Nd = [0] + list(itertools.accumulate(Nd))

print((ans + max([(neta + i)**2 + j + k for i, (j, k) in enumerate(zip(Db, Nd))])))
import sys, itertools

def input(): return sys.stdin.readline()

def inpl(): return [int(i) for i in input().split()]

N, K = inpl()

Sushi = []

Neta = set()

ans = 0

for _ in range(N):

    t, d = inpl()

    Sushi.append((d, t))

Sushi.sort(reverse = True)

Db = []

Nd = []

for i in range(K):

    d, t = Sushi[i]

    if t not in Neta:

        Neta.add(t)

        ans += d

    else:

        Db.append(d)

neta = len(Neta)

Db = [0] + list(itertools.accumulate(Db))

Db.reverse()

for i in range(N-K):

    d, t = Sushi[K+i]

    if t not in Neta:

        Neta.add(t)

        Nd.append(d)

Nd = [0] + list(itertools.accumulate(Nd))

print((ans + max([(neta + i)**2 + j + k for i, (j, k) in enumerate(zip(Db, Nd))])))
import sys, itertools

# def input(): return sys.stdin.readline()
def inpl():
    return [int(i) for i in input().split()]


N, K = inpl()
Sushi = []
Neta = set()
ans = 0
for _ in range(N):
    t, d = inpl()
    Sushi.append((d, t))
Sushi.sort(reverse=True)
Db = []
Nd = []
for i in range(K):
    d, t = Sushi[i]
    if t not in Neta:
        Neta.add(t)
        ans += d
    else:
        Db.append(d)
neta = len(Neta)
Db = [0] + list(itertools.accumulate(Db))
Db.reverse()
for i in range(N - K):
    d, t = Sushi[K + i]
    if t not in Neta:
        Neta.add(t)
        Nd.append(d)
Nd = [0] + list(itertools.accumulate(Nd))
print((ans + max([(neta + i) ** 2 + j + k for i, (j, k) in enumerate(zip(Db, Nd))])))

import sys, itertools


def input():
    return sys.stdin.readline()


def inpl():
    return [int(i) for i in input().split()]


N, K = inpl()
Sushi = []
Neta = set()
ans = 0
for _ in range(N):
    t, d = inpl()
    Sushi.append((d, t))
Sushi.sort(reverse=True)
Db = []
Nd = []
for i in range(K):
    d, t = Sushi[i]
    if t not in Neta:
        Neta.add(t)
        ans += d
    else:
        Db.append(d)
neta = len(Neta)
Db = [0] + list(itertools.accumulate(Db))
Db.reverse()
for i in range(N - K):
    d, t = Sushi[K + i]
    if t not in Neta:
        Neta.add(t)
        Nd.append(d)
Nd = [0] + list(itertools.accumulate(Nd))
print((ans + max([(neta + i) ** 2 + j + k for i, (j, k) in enumerate(zip(Db, Nd))])))

-# def input(): return sys.stdin.readline() + +def input(): +    return sys.stdin.readline() + +