Problem:


Score : 400 points

Problem Statement

There are N pieces of sushi. Each piece has two parameters: "kind of topping" t_i and "deliciousness" d_i.
You are choosing K among these N pieces to eat.
Your "satisfaction" here will be calculated as follows:

The satisfaction is the sum of the "base total deliciousness" and the "variety bonus".

The base total deliciousness is the sum of the deliciousness of the pieces you eat.

The variety bonus is x*x, where x is the number of different kinds of toppings of the pieces you eat.

You want to have as much satisfaction as possible.
Find this maximum satisfaction.

Constraints

1 \leq K \leq N  \leq 10^5

1 \leq t_i \leq N

1 \leq d_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
t_1 d_1
t_2 d_2
.
.
.
t_N d_N

Output

Print the maximum satisfaction that you can obtain.

Sample Input 1

5 3
1 9
1 7
2 6
2 5
3 1

Sample Output 1

26

If you eat Sushi 1,2 and 3:

The base total deliciousness is 9+7+6=22.

The variety bonus is 2*2=4.

Thus, your satisfaction will be 26, which is optimal.

Sample Input 2

7 4
1 1
2 1
3 1
4 6
4 5
4 5
4 5

Sample Output 2

25

It is optimal to eat Sushi 1,2,3 and 4.

Sample Input 3

6 5
5 1000000000
2 990000000
3 980000000
6 970000000
6 960000000
4 950000000

Sample Output 3

4900000016

Note that the output may not fit into a 32-bit integer type.



Sample input:
5 3
1 9
1 7
2 6
2 5
3 1


Sample output:
26

Submission status: Runtime Error

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

s = []
for i in range(N):
  s.append(list(map(int, input().split())))

s2 = sorted(s ,key=lambda x: x[1], reverse=True)
s3 = sorted(s2,key=lambda x: x[0])

res = s2[:K]
p1 = sum([i[1] for i in res]) + len(set([i[0] for i in res]))**2

n1 = set([i[0] for i in s2])

res2 = [s3[0]]
res3 = []

for i in range(1,N):
    if s[i-1][0] != s[i][0]:
        res2.append(s[i])
    else:
        res3.append(s[i])

if len(res2) >= K:
    res2 = sorted(res2 ,key=lambda x: x[1], reverse=True)
    res2 = res2[:K]
    p2 = sum([i[1] for i in res2]) + len(set([i[0] for i in res2]))**2
else:
    res3 = sorted(res2 ,key=lambda x: x[1], reverse=True)
    res2.expend(res3[:K-len(res2)])
    p2 = sum([i[1] for i in res2]) + len(set([i[0] for i in res2]))**2
    

print(max(p1,p2))

Submission status: Runtime Error

Solution code:
import itertools
n,k=map(int,input().split())
t=[]
d=[]
l=[]
c=[]
a=0
max=0
b=0

for i in range(n):
  l.append(i)
m=list(itertools.combinations(l,k))

for i in range(n):
  a,b=map(int,input().split())
  t.append(a)
  d.append(b)
b=0
for i in range(n):
  for j in range(k):
    b+=d[m[i][j]]
    c.append(t[m[i][j]])

  b+=(len(set(c)))**2
  if b>max:
    max=b
  c=[]
  b=0
print(max)

Submission status: Runtime Error

Solution code:
import math
import itertools

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

dt = []
neta = []
bass_score = 0

for _ in range(N):
    tmp1,tmp2 = map(int, input().split())
    dt.append([tmp2,tmp1])


DT = dt.copy()
DT.sort(reverse=True)

for i in range(K):
    bass_score += DT[i][0]
    neta.append(DT[i][1])

bonus_score = len(set(neta))**2
test1 = bass_score + bonus_score

#----------------------------

DT = dt.copy()
neta = []
bass_score = 0
count = 0
for i in range(10**10):
    if DT[i][1] in neta:
        continue
    else:
        bass_score += DT[i][0]
        neta.append(DT[i][1])
        count += 1
        if count == K:
            break

bonus_score = len(set(neta))**2
test2 = bass_score + bonus_score

print(max(test1,test2))

Submission status: Runtime Error

Solution code:
import math
import itertools

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

dt = []
neta = []
bass_score = 0

for _ in range(N):
    tmp1,tmp2 = map(int, input().split())
    dt.append([tmp2,tmp1])


DT = dt.copy()
DT.sort(reverse=True)

for i in range(K):
    bass_score += DT[i][0]
    neta.append(DT[i][1])

bonus_score = len(set(neta))**2
test1 = bass_score + bonus_score

#----------------------------

dd = DT.copy()
neta = []
bass_score = 0
count = 0
for i in range(10**10):
    if DT[i][1] in neta:
        continue
    else:
        bass_score += DT[i][0]
        neta.append(DT[i][1])
        count += 1
        if count == K:
            break

bonus_score = len(set(neta))**2
test2 = bass_score + bonus_score

print(max(test1,test2))

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())

S=[[]for i in range(10**5+1)]
for i in range(N):
    a,b=map(int,input().split())
    S[a].append(b)
S.sort(reverse=True)
#print(S[:10])

k=0
o=0
E=[]
for i in range(len(S)):
    if S[i]==[]:
        break
    else:
        S[i].sort(reverse=True)
        k+=1
        o+=S[i][0]
        E.append(S[i][0])
        del S[i][0]
E.sort()
#print(S)
S.sort(reverse=True)
A=[]
if k>K:
    for i in range(k-K):
        A.append(E[i])
        o-=E[i]
    for i in range(k-K):
        del E[i]
        k-=1

for i in range(len(S)):
    if S[i]==[]:
        break
    else:
        for j in range(len(S[i])):
            A.append(S[i][j])
A.sort(reverse=True)
#print(A)
if K-k>0:
    for i in range(K-k):
        o+=A[i]
        #print(i)
    del A[:i+1]
    print(A)
    print(E)
for i in range(min(K,len(A))):
    if k*k+o<(k-1)**2+o-E[i]+A[i]:
        k=(k-1)
        o=o-E[i]+A[i]
    else:
        break
print(k*k+o)

Submission status: Runtime Error

Solution code:
def solve(N, K, tds):
    memos = {}
    netas = "0" * len(set([t[0] for t in tds]))

    def dfs(n, k, netas):
        if (n, k, netas) in memos:
            return memos[(n, k, netas)]

        if n == N or k == 0:
            memos[(n, k, netas)] = netas.count("1") ** 2
            return memos[(n, k, netas)]

        new_netas = netas[:tds[n][0] - 1] + "1" + netas[tds[n][0]:]
        eaten = dfs(n + 1, k - 1, new_netas) + tds[n][1]
        uneaten = dfs(n + 1, k, netas)

        memos[(n, k, netas)] = max(eaten, uneaten)
        return memos[(n, k, netas)]

    return dfs(0, K, netas)

if __name__ == "__main__":
    N, K = tuple(map(int, input().split(" ")))
    tds = [tuple(map(int, input().split(" "))) for _ in range(N)]
    print(solve(N, K, tds))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
items = []
neta = {}
for _ in range(N):
    t, d = tuple(map(int, input().split()))
    items.append((t, d))

    if t not in neta:
        neta[t] = d
    else:
        tmp = neta[t]
        if d > tmp:
            neta[t] = d

neta = sorted(neta.items(), key=lambda x: -x[1])
items = sorted(items, key=lambda x: -x[1])

ans1 = 0
select1 = set()
for i in range(K):
    t, d = items[i]
    select1.add(t)
    ans1 += d
ans1 += len(select1) ** 2

ans2 = 0
select2 = set()
cnt = 0
for k, v in neta:
    cnt += 1
    select2.add(k)
    ans2 += v
    if cnt == K:
        ans2 += cnt ** 2
        print(max(ans1, ans2))
        exit()
ans2 += cnt ** 2

remain = K - cnt
i = 1
sel = set()
while remain != 0:
    t, d = items[i]
    if t not in sel:
        ans2 += d
        sel.add(t)
        remain -= 1
        i += 1
    else:
        i += 1
        sel.add(t)
print(max(ans1, ans2))

Submission status: Runtime Error

Solution code:
import itertools
n,k = map(int,input().split())
su = [list(map(int,input().split())) for i in range(n)]
sushi = list(itertools.combinations(su,k))
p = []
t = []
d = []
for i in range(n):
  for j in range(k):
    t.append(sushi[i-1][j-1][0])
    d.append(sushi[i-1][j-1][1])
  T = len(list(set(t)))
  ans = sum(d) + T * T
  p.append(ans)
  t.clear()
  d.clear()
p.sort()
print(p[-1])

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
items = []
neta = {}
for _ in range(N):
    t, d = tuple(map(int, input().split()))
    items.append((t, d))

    if t not in neta:
        neta[t] = d
    else:
        tmp = neta[t]
        if d > tmp:
            neta[t] = d

neta = sorted(neta.items(), key=lambda x: -x[1])
items = sorted(items, key=lambda x: -x[1])

ans1 = 0
select1 = set()
for i in range(K):
    t, d = items[i]
    select1.add(t)
    ans1 += d
ans1 += len(select1) ** 2

ans2 = 0
select2 = set()
cnt = 0
for k, v in neta:
    cnt += 1
    select2.add(k)
    ans2 += v
    if cnt == K:
        ans2 += cnt ** 2
        print(max(ans1, ans2))
        exit()
ans2 += cnt ** 2
remain = K - cnt
i = 1
sel = set()
while remain != 0:
    t, d = items[i]
    if t not in sel:
        ans2 += d
        sel.add(t)
        remain -= 1
    else:
        i += 1
        sel.add(t)
print(max(ans1, ans2))

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())

S=[[]for i in range(10**5+1)]
for i in range(N):
    a,b=map(int,input().split())
    S[a].append(b)
S.sort(reverse=True)
#print(S)

k=0
o=0
E=[]
for i in range(len(S)):
    if S[i]==[]:
        break
    else:
        k+=1
        o+=S[i][0]
        E.append(S[i][0])
        del S[i][0]
E.sort()
#print(S)
S.sort(reverse=True)
A=[]
if k>K:
    for i in range(k-K):
        A.append(E[i])
        o-=E[i]
    for i in range(k-K):
        del E[i]
        k-=1

for i in range(len(S)):
    if S[i]==[]:
        break
    else:
        for j in range(len(S[i])):
            A.append(S[i][j])
A.sort(reverse=True)
#print(A)
if K-k>0:
    for i in range(K-k):
        o+=A[i]
        #print(i)
    del A[:i+1]
    print(A)
    print(E)
for i in range(min(K,len(A))):
    if k*k+o<(k-1)**2+o-E[i]+A[i]:
        k=(k-1)
        o=o-E[i]+A[i]
    else:
        break
print(k*k+o)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())

S=[[]for i in range(10**1+1)]
for i in range(N):
    a,b=map(int,input().split())
    S[a].append(b)
S.sort(reverse=True)
#print(S)

k=0
o=0
E=[]
for i in range(len(S)):
    if S[i]==[]:
        break
    else:
        k+=1
        o+=S[i][0]
        E.append(S[i][0])
        del S[i][0]
E.sort()
#print(S)
S.sort(reverse=True)
A=[]
if k>K:
    for i in range(k-K):
        A.append(E[i])
        o-=E[i]
    for i in range(k-K):
        del E[i]
        k-=1

for i in range(len(S)):
    if S[i]==[]:
        break
    else:
        for j in range(len(S[i])):
            A.append(S[i][j])
A.sort(reverse=True)
#print(A)
if K-k>0:
    for i in range(K-k):
        o+=A[i]
        #print(i)
    del A[:i+1]
    print(A)
    print(E)
for i in range(min(K,len(A))):
    if k*k+o<(k-1)**2+o-E[i]+A[i]:
        k=(k-1)
        o=o-E[i]+A[i]
    else:
        break
print(k*k+o)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
items = []
neta = {}
for _ in range(N):
    t, d = tuple(map(int, input().split()))
    items.append((t, d))

    if t not in neta:
        neta[t] = d
    else:
        tmp = neta[t]
        if d > tmp:
            neta[t] = d

neta = sorted(neta.items(), key=lambda x: -x[1])
items = sorted(items, key=lambda x: -x[1])

ans1 = 0
select1 = set()
for i in range(K):
    t, d = items[i]
    select1.add(t)
    ans1 += d
ans1 += len(select1) ** 2

ans2 = 0
select2 = set()
cnt = 0
for k, v in neta:
    cnt += 1
    select2.add(k)
    ans2 += v
    if cnt == K:
        ans2 += cnt ** 2
        print(max(ans1, ans2))
        exit()

remain = K - cnt
i = 1
sel = set()
while remain != 0:
    t, d = items[i]
    if t not in sel:
        ans2 += d
        sel.add(t)
        remain -= 1
    else:
        i += 1
        sel.add(t)
print(max(ans1, ans2))

Submission status: Runtime Error

Solution code:

def solve(N, K, tds):
    memos = {}
    netas = "0" * len(set([t[0] for t in tds]))

    def dfs(n, k, netas):
        if (n, k, netas) in memos:
            return memos[(n, k, netas)]

        if n == N or k == 0:
            memos[(n, k, netas)] = netas.count("1") ** 2
            return memos[(n, k, netas)]

        new_netas = netas[:tds[n][0] - 1] + "1" + netas[tds[n][0] + 1:]
        eaten = dfs(n + 1, k - 1, new_netas) + tds[n][1]
        uneaten = dfs(n + 1, k, netas)

        memos[(n, k, netas)] = max(eaten, uneaten)
        return memos[(n, k, netas)]

    return dfs(0, K, netas)

if __name__ == "__main__":
    N, K = tuple(map(int, input().split(" ")))
    tds = [tuple(map(int, input().split(" "))) for _ in range(N)]
    print(solve(N, K, tds))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
items = []
neta = {}
for _ in range(N):
    t, d = tuple(map(int, input().split()))
    items.append((t, d))

    if t not in neta:
        neta[t] = d
    else:
        tmp = neta[t]
        if d > tmp:
            neta[t] = d

neta = sorted(neta.items(), key=lambda x:-x[1])
items = sorted(items, key=lambda x: -x[1])


ans1 = 0
select1 = set()
for i in range(K):
    t, d = items[i]
    select1.add(t)
    ans1 += d
ans1 += len(select1) ** 2


ans2 = 0
select2 = set()
cnt = 0
for k, v in neta:
    cnt += 1
    select2.add(k)
    ans2 += v
    if cnt == K:
        ans2 += cnt ** 2
        print(max(ans1, ans2))
        exit()

remain = K - cnt
i = 1
sel = set()
while remain != 0:
    t, d = items[i]
    if t not in sel:
        ans2 += d
        sel.add(t)
    else:
        i += 1
        sel.add(t)
print(max(ans1, ans2))

Submission status: Runtime Error

Solution code:
import itertools
n,k = map(int,input().split())
su = [list(map(int,input().split())) for i in range(n)]
sushi = list(itertools.combinations(su,k))
p = []
t = []
d = []
for i in range(n):
  for j in range(k):
    t.clear()
    d.clear()
    t.append(sushi[i-1][j-1][0])
    d.append(sushi[i-1][j-1][1])
  T = len(list(set(t)))
  ans = sum(d) + T * T
  p.append(ans)
p.sort()
print(p[-1])

Submission status: Runtime Error

Solution code:
from heapq import heappush, heappop

N, K = map(int, input().split())
h = []
for i in range(N):
  t, d = map(int, input().split())
  heappush(h, (-d, t))

# initial
res = 0
sum = 0
q = []
used = {}
for i in range(K):
  (d, t) = heappop(h)
  if t not in used:
    used[t] = 1
  else:
    heappush(q, (-d, t))
  sum += -d
res = len(used)*len(used)+sum

# swap minimum
while q:
  (d, t) = heappop(h)
  if t not in used:
    used[t] = 1
    m, _ = heappop(q)
    sum = sum - m - d
  res = max(res, len(used)*len(used) + sum)
  
print(res)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())

S=[[]for i in range(10**1+1)]
for i in range(N):
    a,b=map(int,input().split())
    S[a].append(b)
S.sort(reverse=True)
#print(S)

k=0
o=0
E=[]
for i in range(len(S)):
    if S[i]==[]:
        break
    else:
        k+=1
        o+=S[i][0]
        E.append(S[i][0])
        del S[i][0]
E.sort()
#print(S)
S.sort(reverse=True)
A=[]
if k>K:
    for i in range(k-K):
        A.append(E[i])
    for i in range(k-K):
        del E[i]
        k-=1

for i in range(len(S)):
    if S[i]==[]:
        break
    else:
        for j in range(len(S[i])):
            A.append(S[i][j])
A.sort(reverse=True)
#print(A)
if K-k>0:
    for i in range(K-k):
        o+=A[i]
        #print(i)
    del A[:i+1]
    print(A)
    print(E)
for i in range(min(K,len(A))):
    if k*k+o<(k-1)**2+o-E[i]+A[i]:
        k=(k-1)
        o=o-E[i]+A[i]
    else:
        break
print(k*k+o)

Submission status: Runtime Error

Solution code:
def solve(N, K, tds):
    memos = {}
    def dfs(n, k, netas):
        if (n, k, netas) in memos:
            return memos[(n, k, netas)]

        if n == N or k == 0:
            memos[(n, k, netas)] = len(netas) ** 2
            return memos[(n, k, netas)]
        new_netas = set(netas)
        new_netas.add(tds[n][0])
        new_netas = tuple(sorted(new_netas))
        eaten = dfs(n + 1, k - 1, new_netas) + tds[n][1]

        uneaten = dfs(n + 1, k, netas)

        memos[(n, k, netas)] = max(eaten, uneaten)
        return memos[(n, k, netas)]

    return dfs(0, K, tuple())

if __name__ == "__main__":
    N, K = tuple(map(int, input().split(" ")))
    tds = [tuple(map(int, input().split(" "))) for _ in range(N)]
    print(solve(N, K, tds))

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
X = []
for i in range(N):
    t,d = map(int,input().split())
    X.append((d,t))

X.sort(reverse=True)

p,v = 0,set([])
pp = []
for i in range(K):
    p += X[i][0]
    if X[i][1] not in v:
        v.add(X[i][1])
    else:
        pp.append((X[i][0],X[i][1]))

Ans = p + len(v)**2
ans = Ans
for i in range(K,N):
    if X[i][1] not in v:
        ans = ans - pp[0][0] + X[i][0] - len(v)**2 + (len(v)+1)**2
        v.add(X[i][1])
        Ans = max(Ans,ans)
        pp = pp[1:]
    

print(Ans)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())

S=[[]for i in range(10**1+1)]
for i in range(N):
    a,b=map(int,input().split())
    S[a].append(b)
S.sort(reverse=True)
#print(S)

k=0
o=0
E=[]
for i in range(len(S)):
    if S[i]==[]:
        break
    else:
        k+=1
        o+=S[i][0]
        E.append(S[i][0])
        del S[i][0]
E.sort()
#print(S)
S.sort(reverse=True)
A=[]
for i in range(len(S)):
    if S[i]==[]:
        break
    else:
        for j in range(len(S[i])):
            A.append(S[i][j])
A.sort(reverse=True)
#print(A)
if K-k>0:
    for i in range(K-k):
        o+=A[i]
        #print(i)
    del A[:i+1]
    print(A)
    print(E)
for i in range(min(K,len(A))):
    if k*k+o<(k-1)**2+o-E[i]+A[i]:
        k=(k-1)
        o=o-E[i]+A[i]
    else:
        break
print(k*k+o)

Submission status: Runtime Error

Solution code:
IN = list(map(int,input().split()))
N = IN[0]
K = IN[1]

A = []

for i in range(N):
  IN = list(map(int,input().split()))
  A.append(IN)

A.sort(key=lambda x:-x[1])
ans1 = sum([a[1] for a in A[0:K]]) + len(set([a[0] for a in A[0:K]])) ** 2
ans2 = 0

j = -1
k = False
D = []
for i in range(K):
  while True:
    j += 1
    if j == N:
      j = 0
      k = True
    if k:
      ans2 += A[j][1]
      D.append(A[j][0])
      del A[j]
      break
    if not A[j][0] in D:
      ans2 += A[j][1]
      D.append(A[j][0])
      del A[j]
      break
ans2 += len(set(D)) ** 2

print(max(ans1,ans2))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
neta = [[] for i in range(N+1)]
for i in range(N):
    t, d = map(int, input().split())
    neta[t].append(d)
for i in range(N):
    neta[i].sort(reverse = True)
neta = [i for i in neta if i != []]
neta.sort(reverse = True)
kinds = len(neta)
maxneta = 0
for i in range(len(neta)):
    maxneta = max(maxneta, len(neta[i]))
anslist = []

kinds_max = min(kinds, K)
kinds_min = 1 + (K // maxneta)

firstnetasum = [0]
for i in range(kinds):
    firstnetasum.append(firstnetasum[i] + neta[i][0])
for i in range(kinds_min, kinds_max + 1):
    ans = firstnetasum[i]
    remain = []
    for j in range(i):
        try:
            remain += neta[j][1:]
        except IndexError:
            pass
    remain.sort(reverse = True)
    ans += sum(remain[:K-i])
    ans += i**2
    anslist.append(ans)
print(max(anslist))

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
li = [0 for i in range(n)]
lis = []
for i in range(n):
    a,b = map(int,input().split())
    if li[a-1] <= b:
        lis.append(li[a-1])
        li[a-1] = b
    else:
        lis.append(b)
lis.sort(reverse= True)
li.sort(reverse = True)
ans = 0
num = 0
cou = 0
co = 0

sushi = [0 for i in range(n)]

while li[num] != 0 and n > num:
    li[num] += 2 * num + 1
    num += 1
if m > num:
    ans = sum(li) + sum(lis[:m-num])
    cou = m-num
else:
    ans = sum(li[:num])
    cou = 0
nu = ans

for i in range(num-1):
    nu = nu - li[num-i-1]+lis[cou+i]
    ans = max(ans,nu)
print(ans)

Submission status: Runtime Error

Solution code:
ans = 0

def calc_final_data(suhsi_data, eat_data, sushi_num):
    global ans
    ans_kari = 0
    neta_hist = set()
    for i in range(sushi_num):
        if eat_data[i]:
            neta_hist.add(str(suhsi_data[i][0]))
            ans_kari += suhsi_data[i][1]
    ans_kari += len(neta_hist) * len(neta_hist)
    if ans_kari > ans:
        ans = ans_kari

def eat_sushi(suhsi_data, max_eat, eat_data_kari, index, eat_flg, sushi_num):
    eat_data = eat_data_kari.copy()
    if index == sushi_num or eat_data.count(1) == max_eat:
        calc_final_data(suhsi_data, eat_data, sushi_num)
        return
    if eat_flg:
        eat_data[index] = 1
    eat_sushi(suhsi_data, max_eat, eat_data, index+1, 0, sushi_num)
    eat_sushi(suhsi_data, max_eat, eat_data, index+1, 1, sushi_num)


def calc_eat(suhsi_data, max_eat, eat_data, sushi_num):
    eat_sushi(suhsi_data, max_eat, eat_data, 0, 0, sushi_num)
    eat_sushi(suhsi_data, max_eat, eat_data, 0, 1, sushi_num)

def print_ans():
    global ans
    print(ans)

def main():
    sushi_num, max_eat = list(map(int, input().split()))
    suhsi_data = [list(map(int, input().split())) for i in range(sushi_num)]
    eat_data = [0 for i in range(sushi_num)]
    calc_eat(suhsi_data, max_eat, eat_data, sushi_num)
    print_ans()

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
TD=[list(map(int,input().split())) for i in range(N)]
TD.sort(key=lambda x:x[1],reverse=True)

ANS=0

check=[0]*(10**5)
NOCHANGE=[]
NETA=[]

for i in range(K):
    ANS+=TD[i][1]
    NETA.append(TD[i][0])


for i in range(N):
    if check[TD[i][0]]==0:
        NOCHANGE.append(i)
        check[TD[i][0]]=1

NOCHANGE=set(NOCHANGE)
NETA=len(set(NETA))



NOWLIST=set(list(range(K)))
NOWLIST=sorted(list(NOWLIST-NOCHANGE))



NOCHANGE=sorted(list(NOCHANGE))
NOCHANSET=set(NOCHANGE)
ANS+=NETA**2

#print(NOCHANGE)
#print(NOWLIST)
#print(ANS)
#print(NETA)

for j in range(len(NOCHANGE)):
    if NOCHANGE[j]>K-1:
        break

#print(j)
i=K-1
L=len(NOCHANGE)
SCORE=ANS
while j<L:
    while i in NOCHANSET:
        i-=1

    if i<0:
        break


    SCORE+=TD[NOCHANGE[j]][1]-TD[i][1]-NETA**2+(NETA+1)**2
    if SCORE>ANS:
        ANS=SCORE
    #print(i,j,NETA,SCORE)
    NETA+=1
    j+=1
    i-=1


    #print(i,j)

print(ANS)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
li = [0 for i in range(n)]
lis = []
for i in range(n):
    a,b = map(int,input().split())
    if li[a-1] <= b:
        lis.append(li[a-1])
        li[a-1] = b
    else:
        lis.append(b)
lis.sort(reverse= True)
li.sort(reverse = True)
ans = 0
num = 0
cou = 0
co = 0

sushi = [0 for i in range(n)]

while li[num] != 0 and n > num:
    li[num] += 2 * num + 1
    num += 1

ans = sum(li[:num])
for i in range(num-2):
    ans += max(0, -li[num-(i+1)] + lis[i])
print(ans)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
N, K = map(int, input().split())
T = defaultdict(list)
for i in range(N):
    t, d = map(int, input().split())
    T[t].append(d)
S = []
U = []
for k, v in T.items():
    v.sort(reverse=1)
    U.append((v[0], k))
    for w in v[1:]:
        S.append((w, k))
S.sort(reverse=1)
U.sort(reverse=1)
D = defaultdict(int)
cnt = 0
D[U[0][1]] += 1
t = 1
cnt += 1
res = U[0][0]
S = S[:K-1]
for w, k in S:
    D[k] += 1
    if D[k] == 1:
        t += 1
    cnt += 1
    res += w
ans = 0
for v, k in U[1:]:
    D[k] += 1
    if D[k] == 1:
        t += 1
    cnt += 1
    res += v
    while cnt > K:
        cnt -= 1
        w, l = S.pop()
        D[l] -= 1
        if D[l] == 0:
            t -= 1
        res -= w
    print(t, res, cnt)
    ans = max(ans, t*t + res)
print(ans)