Problem p03148 (python)

Fixed submission:
n, k = map(int, input().split())
A = []
for _ in range(n):
    t, d = map(int, input().split())
    A.append((d, t))
A.sort(reverse=True)
X = []
Y = []
st = set()
for i in range(k):
    d, t = A[i]
    if t in st:
        X.append(d)
    else:
        Y.append(d)
        st.add(t)
x = sum(X)
y = sum(Y)
v = len(Y)
ans = x + y + v ** 2
for i in range(k, n):
    if not X:
        break
    d, t = A[i]
    if t in st:
        continue
    st.add(t)
    x -= X.pop()
    y += d
    v += 1
    ans = max(ans, x + y + v ** 2)
print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
A = []
for _ in range(n):
    t, d = map(int, input().split())
    A.append((d, t))
A.sort(reverse=True)
X = []
Y = []
st = set()
for d, t in A:
    if t in st:
        X.append(d)
    else:
        Y.append(d)
    st.add(t)
ans = 0
len_x = len(X)
len_y = len(Y)
if len_y >= k:
    a = 0
    b = len_y - 1
    x = 0
    y = sum(Y[:k])
else:
    a = k - len_y
    b = len_y - 1
    x = sum(X[:k - len_y])
    y = sum(Y)
ans = x + y + len_y ** 2
t = ans
for i in range(min(len_x, len_y, k)):
    x += X[a]
    a += 1
    y -= Y[b]
    b -= 1
    len_y -= 1
    t = x + y + len_y ** 2
    ans = max(ans, t)
print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
A = []
for _ in range(n):
    t, d = map(int, input().split())
    A.append((d, t))
A.sort(reverse=True)
X = []
Y = []
st = set()
for d, t in A:
    if t in st:
        X.append(d)
    else:
        Y.append(d)
    st.add(t)
ans = 0
len_x = len(X)
len_y = len(Y)
if len_y >= k:
    a = 0
    b = len_y - 1
    x = 0
    y = sum(Y[:k])
    N = min(k, len_x)
else:
    a = k - len_y
    b = len_y - 1
    x = sum(X[:k - len_y])
    y = sum(Y)
    N = min(k - len_y, len_x)
ans = x + y + len_y ** 2
t = ans
for i in range(N):
    x += X[a]
    a += 1
    y -= Y[b]
    b -= 1
    len_y -= 1
    t = x + y + len_y ** 2
    ans = max(ans, t)
print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
A = []
for _ in range(n):
    t, d = map(int, input().split())
    A.append((d, t))
A.sort(reverse=True)
X = []
Y = []
st = set()
for d, t in A:
    if t in st:
        X.append(d)
    else:
        Y.append(d)
    st.add(t)
ans = 0
len_y = len(Y)
if len_y >= k:
    a = 0
    b = len_y - 1
    x = 0
    y = sum(Y[:k])
else:
    a = k - len_y
    b = len_y - 1
    x = sum(X[:k - len_y])
    y = sum(Y)
ans = x + y + len_y ** 2
t = ans
for i in range(min(len(X), len_y, k - len_y + 1, k - 1)):
    x += X[a]
    a += 1
    y -= Y[b]
    b -= 1
    len_y -= 1
    t = x + y + len_y ** 2
    ans = max(ans, t)
print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
A = []
for _ in range(n):
    t, d = map(int, input().split())
    A.append((d, t))
A.sort(reverse=True)
X = []
Y = []
st = set()
for d, t in A:
    if t in st:
        X.append(d)
    else:
        Y.append(d)
    st.add(t)
ans = 0
len_y = len(Y)
if len_y >= k:
    a = 0
    b = len_y - 1
    x = 0
    y = sum(Y[:k])
else:
    a = k - len_y
    b = len_y - 1
    x = sum(X[:k - len_y])
    y = sum(Y)
ans = x + y + len_y ** 2
t = ans
for i in range(min(len(X), k - len_y + 1, k - 1)):
    x += X[a]
    a += 1
    y -= Y[b]
    b -= 1
    len_y -= 1
    t = x + y + len_y ** 2
    ans = max(ans, t)
print(ans)

Buggy submission (verdict: Runtime Error):
n, k = map(int, input().split())
A = []
for _ in range(n):
    t, d = map(int, input().split())
    A.append((d, t))
A.sort(reverse=True)
X = []
Y = []
st = set()
for d, t in A:
    if t in st:
        X.append(d)
    else:
        Y.append(d)
    st.add(t)
ans = 0
len_y = len(Y)
if len_y >= k:
    a = 0
    b = len_y - 1
    x = 0
    y = sum(Y[:k])
else:
    a = k - len_y
    b = len_y - 1
    x = sum(X[:k - len_y])
    y = sum(Y)
ans = x + y + len_y ** 2
t = ans
for i in range(min(len(X), k - 1)):
    x += X[a]
    a += 1
    y -= Y[b]
    b -= 1
    len_y -= 1
    t = x + y + len_y ** 2
    ans = max(ans, t)
print(ans)