Buggy python submission (verdict: Runtime Error):
import heapq
n,k=map(int,input().split())
data=[]
for i in range(n):
    t,d=map(int,input().split())
    data.append((-d,t))
data=sorted(data)
neta=set()
que1=[]
que2=[]
p,x=0,0
for i in range(k):
    if data[i][1] in neta:
        heapq.heappush(que1,data[i])
        x-=data[i][0]
    else:
        x-=data[i][0]
        p+=1
        neta.add(data[i][1])
for i in range(k,n):
    if data[i][1]in neta:
        pass
    else:
        heapq.heappush(que2,data[i])
mx=x+p*p
while que1:
    a=heapq.heappop(que1)
    b==None
    while que2:
        b=heapq.heappop(que2)
        if b[1] not in neta:
            break
        b=None
    if b==None:
        break
    x=x+a[0]-b[0]
    p+=1
    neta.add(b[1])
    mx=max(mx,x+p*p)
print(mx)


Fixed submission:
import heapq
n,k=map(int,input().split())
data=[]
for i in range(n):
    t,d=map(int,input().split())
    data.append((-d,t))
data=sorted(data)
neta=set()
que1=[]
p,x=0,0
for i in range(k):
    if data[i][1] in neta:
        heapq.heappush(que1,(-data[i][0],data[i][1]))
        x-=data[i][0]
    else:
        x-=data[i][0]
        p+=1
        neta.add(data[i][1])
mx=x+p*p
for i in range(k,n):
    if data[i][1] in neta:
        pass
    else:
        if que1:
            a=heapq.heappop(que1)
            x=x-a[0]-data[i][0]
            p+=1
            neta.add(data[i][1])
            mx=max(mx,x+p*p)
        else:
            break
print(mx)

Buggy python submission (verdict: Runtime Error):
import heapq
n,k=map(int,input().split())
data=[]
for i in range(n):
    t,d=map(int,input().split())
    data.append((-d,t))
data=sorted(data)
neta=set()
que1=[]
que2=[]
p,x=0,0
for i in range(k):
    if data[i][1] in neta:
        heapq.heappush(que1,data[i])
        x-=data[i][0]
    else:
        x-=data[i][0]
        p+=1
        neta.add(data[i][1])
for i in range(k,n):
    if data[i][1]in neta:
        pass
    else:
        heapq.heappush(que2,data[i])
mx=x+p*p
while que1:
    a=heapq.heappop(que1)
    while que2:
        b=heapq.heappop(que2)
        if b[1] not in neta:
            break
        b=None
    if b==None:
        break
    x=x+a[0]-b[0]
    p+=1
    neta.add(b[1])
    mx=max(mx,x+p*p)
print(mx)


Fixed submission:
import heapq
n,k=map(int,input().split())
data=[]
for i in range(n):
    t,d=map(int,input().split())
    data.append((-d,t))
data=sorted(data)
neta=set()
que1=[]
p,x=0,0
for i in range(k):
    if data[i][1] in neta:
        heapq.heappush(que1,(-data[i][0],data[i][1]))
        x-=data[i][0]
    else:
        x-=data[i][0]
        p+=1
        neta.add(data[i][1])
mx=x+p*p
for i in range(k,n):
    if data[i][1] in neta:
        pass
    else:
        if que1:
            a=heapq.heappop(que1)
            x=x-a[0]-data[i][0]
            p+=1
            neta.add(data[i][1])
            mx=max(mx,x+p*p)
        else:
            break
print(mx)

Buggy python submission (verdict: Runtime Error):
import heapq
n,k=map(int,input().split())
data=[]
for i in range(n):
    t,d=map(int,input().split())
    data.append((-d,t))
data=sorted(data)
neta=set()
que1=[]
que2=[]
p,x=0,0
for i in range(k):
    if data[i][1] in neta:
        heapq.heappush(que1,data[i])
        x-=data[i][0]
    else:
        x-=data[i][0]
        p+=1
        neta.add(data[i][1])
for i in range(k,n):
    if data[i][1]in neta:
        pass
    else:
        heapq.heappush(que2,data[i])
mx=x+p*p
tmp=x+p*p
while que1:
    a=heapq.heappop(que1)
    while que2:
        b=heapq.heappop(que2)
        if b[1] not in neta:
            break
    tmp=(tmp-p*p)+a[0]-b[0]+(p+1)**2
    p+=1
    neta.add(b[1])
    mx=max(mx,tmp)
print(mx)



Fixed submission:
import heapq
n,k=map(int,input().split())
data=[]
for i in range(n):
    t,d=map(int,input().split())
    data.append((-d,t))
data=sorted(data)
neta=set()
que1=[]
p,x=0,0
for i in range(k):
    if data[i][1] in neta:
        heapq.heappush(que1,(-data[i][0],data[i][1]))
        x-=data[i][0]
    else:
        x-=data[i][0]
        p+=1
        neta.add(data[i][1])
mx=x+p*p
for i in range(k,n):
    if data[i][1] in neta:
        pass
    else:
        if que1:
            a=heapq.heappop(que1)
            x=x-a[0]-data[i][0]
            p+=1
            neta.add(data[i][1])
            mx=max(mx,x+p*p)
        else:
            break
print(mx)

Buggy python submission (verdict: Runtime Error):
import heapq
n,k=map(int,input().split())
data=[]
for i in range(n):
    t,d=map(int,input().split())
    data.append((-d,t))
data=sorted(data)
neta=set()
que1=[]
que2=[]
p,x=0,0
for i in range(k):
    if data[i][1] in neta:
        heapq.heappush(que1,data[i])
        x-=data[i][0]
    else:
        x-=data[i][0]
        p+=1
        neta.add(data[i][1])
for i in range(k,n):
    if data[i][1]in neta:
        pass
    else:
        heapq.heappush(que2,data[i])
mx=x+p*p
tmp=x+p*p
while que1:
    a=heapq.heappop(que1)
    while True:
        b=heapq.heappop(que2)
        if b[1] not in neta:
            break
    tmp=(tmp-p*p)+a[0]-b[0]+(p+1)**2
    p+=1
    neta.add(b[1])
    mx=max(mx,tmp)
print(mx)



Fixed submission:
import heapq
n,k=map(int,input().split())
data=[]
for i in range(n):
    t,d=map(int,input().split())
    data.append((-d,t))
data=sorted(data)
neta=set()
que1=[]
p,x=0,0
for i in range(k):
    if data[i][1] in neta:
        heapq.heappush(que1,(-data[i][0],data[i][1]))
        x-=data[i][0]
    else:
        x-=data[i][0]
        p+=1
        neta.add(data[i][1])
mx=x+p*p
for i in range(k,n):
    if data[i][1] in neta:
        pass
    else:
        if que1:
            a=heapq.heappop(que1)
            x=x-a[0]-data[i][0]
            p+=1
            neta.add(data[i][1])
            mx=max(mx,x+p*p)
        else:
            break
print(mx)

Buggy python submission (verdict: Runtime Error):
import heapq
n,k=map(int,input().split())
data=[]
for i in range(n):
    t,d=map(int,input().split())
    data.append((-d,t))
data=sorted(data)
neta=set()
que1=[]
que2=[]
p,x=0,0
for i in range(k):
    if data[i][1] in neta:
        heapq.heappush(que1,data[i])
        x-=data[i][0]
    else:
        x-=data[i][0]
        p+=1
        neta.add(data[i][1])
for i in range(k,n):
    if data[i][1]in neta:
        pass
    else:
        heapq.heappush(que2,data[i])
mx=x+p*p
tmp=x+p*p
while que1:
    a=heapq.heappop(que1)
    while True:
        b=heapq.heappop(que2)
        if b[1] not in neta:
            break
    tmp=(tmp-p*p)+a[0]-b[0]+(p+1)**2
    p+=1
    neta.add(b[1])
    mx=max(mx,tmp)
print(mx)



Fixed submission:
import heapq
n,k=map(int,input().split())
data=[]
for i in range(n):
    t,d=map(int,input().split())
    data.append((-d,t))
data=sorted(data)
neta=set()
que1=[]
p,x=0,0
for i in range(k):
    if data[i][1] in neta:
        heapq.heappush(que1,(-data[i][0],data[i][1]))
        x-=data[i][0]
    else:
        x-=data[i][0]
        p+=1
        neta.add(data[i][1])
mx=x+p*p
for i in range(k,n):
    if data[i][1] in neta:
        pass
    else:
        if que1:
            a=heapq.heappop(que1)
            x=x-a[0]-data[i][0]
            p+=1
            neta.add(data[i][1])
            mx=max(mx,x+p*p)
        else:
            break
print(mx)