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: Wrong Answer

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
#  import numpy as np

sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]


def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    N, K = LI()
    T = []
    D = []
    TD = []
    [TD.append(LI()) for i in range(N)]
    # sort desc
    #  TD = np.array(TD)
    #  TD = TD[np.argsort(TD[:, 1])[::-1]]
    sorted(TD, key=lambda l:l[1], reverse=True)
    #  print(TD)

    # print(TD)
    # 種類のカウントをもつ
    #  print(max(TD, key=lambda x:x[1]))
    type_count_dict = {}
    for i in TD:
        if not type_count_dict.get(i[0]):
            type_count_dict[i[0]] = 0
    #  print(type_count_dict)

    S = TD[:K]
    #  print('S', S)
    for i in S:
        type_count_dict[i[0]] += 1

    #  print('type_count', type_count_dict)
    base_point = sum(i[1] for i in S)
    type_bonus = len(set([i[0] for i in S]))
    result = base_point + type_bonus**2
    results = [result]
    #  print('base_point, type_bonus, result', base_point, type_bonus**2, result)

    remaining_items = []
    done_types = []
    for i in TD[K:]:
        if i[0] in done_types:
            continue
        if type_count_dict[i[0]] == 0:
            remaining_items.append(i[1])
            done_types.append(i[0])
    #  print('remaining_items', remaining_items)

    for idx, s in enumerate(S[::-1]):
        if len(remaining_items) == 0:
            break
        if type_count_dict[s[0]] >= 2:
            # print('s', s)
            type_count_dict[s[0]] -= 1
            base_point += remaining_items[0] - s[1]
            #  S[len(S) - 1 - idx] = TD[remaining_items[0]]
            type_bonus += 1
            # print('base_point1', base_point)
            # print('base_point2', base_point)
            # print('type_bonus', type_bonus)
            del remaining_items[0]
            #  print(type_bonus)
            results.append(type_bonus**2 + base_point)

    print(max(results))
    
main()

Submission status: Wrong Answer

Solution code:
ri = lambda: int(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)

Submission status: Wrong Answer

Solution code:
ri = lambda: int(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)

Submission status: Wrong Answer

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

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

td_n = {}
for t,d in td:
    if t in td_n:
        td_n[t] += [d]
    else:
        td_n[t] = [d]
td = []
n = len(td_n)
for t,d in td_n.items():
    d.sort(reverse=True)
    td += [d]

#print(td)
td_1 = [t[0] for t in td]
td_1.sort(reverse=True)
td_rem = [d for t in td for d in t[1:]]
td_rem.sort(reverse=True)

ans = [0 for _ in range(n+1)]
sum_l = 0
for i in range(1,n+1):
    sum_l += len(td[i-1])
    if sum_l < K:
        continue
    else:
        ans[i] = i*i + sum(td_1[:i]) + sum(td_rem[:(K-i)])

print(max(ans))

Submission status: Wrong Answer

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

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

td_n = {}
for t,d in td:
    if t in td_n:
        td_n[t] += [d]
    else:
        td_n[t] = [d]
td = []
n = len(td_n)
for t,d in td_n.items():
    d.sort(reverse=True)
    td += [d]

#print(td)
td_1 = [t[0] for t in td]
td_rem = [d for t in td for d in t[1:]]
td_rem.sort(reverse=True)

ans = [0 for _ in range(n+1)]
sum_l = 0
for i in range(1,n+1):
    sum_l += len(td[i-1])
    if sum_l < K:
        continue
    else:
        ans[i] = i*i + sum(td_1[:i]) + sum(td_rem[:(K-i)])

print(max(ans))

Submission status: Wrong Answer

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

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

td_n = {}
for t,d in td:
    if t in td_n:
        td_n[t] += [d]
    else:
        td_n[t] = [d]
td = []
n = len(td_n)
for t,d in td_n.items():
    d.sort(reverse=True)
    td += [d]
td.sort(reverse=True, key=lambda x:len(x))

ans = [0 for _ in range(n+1)]
sum_l = 0
for i in range(1,n+1):
    sum_l += len(td[i-1])
    if sum_l < K:
        continue
    else:
        td_1 = [t[0] for t in td[:i]]
        td_ = [d for t in td[:i] for d in t[1:]]
        td_.sort(reverse=True)
        ans[i] = i*i + sum(td_1) + sum(td_[:(K-i)])
print(max(ans))

Submission status: Wrong Answer

Solution code:
import heapq

n, k = [int(x) for x in input().split()]

data = [[int(x) for x in input().split()] for _ in range(n)]
data.sort(reverse=True, key=lambda x: x[1])

eat = {}
noeat = {}

for x in data[0:k]:
  if x[0] in eat:
    #heapq.heappush(eat[x[0]], x[1])
    eat[x[0]].append(x[1])
  else:
    eat[x[0]] = [x[1]]
for k in eat:
  eat[k].sort()

for x in data[k:]:
  if x[0] in noeat:
    #heapq.heappush(noeat[x[0]], -x[1])
    noeat[x[0]].append(-x[1])
  else:
    noeat[x[0]] = [-x[1]]
for k in noeat:
  noeat[k].sort()

max_score = 0

while True:
  #print(eat)
  #print(noeat)
  score = 0
  for x in eat:
    score += sum(eat[x])
  score += len(eat)**2
  if max_score < score:
    max_score = score
  remove = None
  min_score1 = 10**12+1
  for x in eat:
    if 1 < len(eat[x]) and eat[x][0] < min_score1:
      remove = x
      min_score1 = eat[x][0]
  add = None
  max_score1 = 0
  for x in noeat:
    if max_score1 < -noeat[x][0] and not(x in eat):
      add = x
      max_score1 = -noeat[x][0]
  if remove == None or add == None:
    break
  else:
    eat[add] = [max_score1]
    heapq.heappop(eat[remove])
    if 1 < len(noeat[add]):
      heapq.heappop(noeat[add])
    else:
      noeat.pop(add)

print(max_score)

Submission status: Wrong Answer

Solution code:
import heapq

n, k = [int(x) for x in input().split()]

data = [[int(x) for x in input().split()] for _ in range(n)]
data.sort(reverse=True, key=lambda x: x[1])

eat = {}
noeat = {}

for x in data[0:k]:
  if x[0] in eat:
    heapq.heappush(eat[x[0]], x[1])
  else:
    eat[x[0]] = [x[1]]

for x in data[k:]:
  if x[0] in noeat:
    heapq.heappush(noeat[x[0]], -x[1])
  else:
    noeat[x[0]] = [-x[1]]

max_score = 0

while True:
  score = 0
  for x in eat:
    score += sum(eat[x])
  score += len(eat)**2
  if max_score < score:
    max_score = score
  remove = None
  min_score1 = 10**12+1
  for x in eat:
    if 1 < len(eat[x]) and eat[x][0] < min_score1:
      remove = x
      min_score1 = eat[x][0]
  add = None
  max_score1 = 0
  for x in noeat:
    if max_score1 < -noeat[x][0]:
      add = x
      max_score1 = -noeat[x][0]
  if remove == None or add == None:
    break
  else:
    eat[add] = [max_score1]
    heapq.heappop(eat[remove])
    if 1 < len(noeat[add]):
      heapq.heappop(noeat[add])
    else:
      noeat.pop(add)

print(max_score)

Submission status: Wrong Answer

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
import numpy as np

sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]


def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    N, K = LI()
    T = []
    D = []
    TD = []
    [TD.append(LI()) for i in range(N)]
    # sort desc
    TD = np.array(TD)
    TD = TD[np.argsort(TD[:, 1])[::-1]]

    # print(TD)
    # 種類のカウントをもつ
    type_count = [0 for i in range(max(TD[:, 0]) + 1)]

    S = TD[:K]
    # print('S', S)
    for i in S:
        type_count[i[0]] += 1

    # print('type_count', type_count)
    base_point = np.sum(S[:, 1])
    type_bonus = len([i for i in type_count if i != 0])
    result = base_point + type_bonus**2
    # print('base_point, type_bonus, result', base_point, type_bonus**2, result)

    remaining_items = []
    done_types = []
    for i in TD[K:]:
        if i[0] in done_types:
            continue
        if type_count[i[0]] == 0:
            remaining_items.append(i[1])
            done_types.append(i[0])
    # print('remaining_items', remaining_items)

    for idx, s in enumerate(S[::-1]):
        if len(remaining_items) == 0:
            break
        if type_count[s[0]] >= 2:
            # print('s', s)
            type_count[s[0]] -= 1
            base_point -= s[1]
            #  S[len(S) - 1 - idx] = TD[remaining_items[0]]
            type_bonus += 1
            # print('base_point1', base_point)
            base_point += remaining_items[0]
            # print('base_point2', base_point)
            # print('type_bonus', type_bonus)
            #  del remaining_items[0]
            result = max(result, type_bonus**2 + base_point)

    print(result)
    
main()

Submission status: Wrong Answer

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

tmp_list = TD[:K]
removable_list = []
S = set()
base_pt = 0
for t, d in tmp_list:
    if t in S:
        removable_list.append([t, d])
    else:
        S.add(t)
    base_pt += d
kind = len(S)
removable_list.sort(key=lambda x: x[1])

pt = [base_pt + kind*kind]
i = 0
for t, d in TD[K:]:
    if i >= len(removable_list):
        break
    if t in S:
        continue
    kind += 1
    base_pt += d - removable_list[i][1]
    pt.append(base_pt + kind*kind)
    i += 1
print(max(pt))

Submission status: Wrong Answer

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

tmp_list = TD[:K]
removable_list = []
S = set()
base_pt = 0
for t, d in tmp_list:
    if t in S:
        removable_list.append([t, d])
    else:
        S.add(t)
    base_pt += d
kind = len(S)
removable_list.sort(key=lambda x: x[1])

pt = [base_pt + kind*kind]
i = 0
if N != K:
    for t, d in TD[K:]:
        if i >= len(removable_list):
            break
        if t in S:
            continue
        kind += 1
        base_pt += d - removable_list[i][1]
        pt.append(base_pt + kind*kind)
        i += 1
print(max(pt))

Submission status: Wrong Answer

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

tmp_list = TD[:K]
removable_list = []
S = set()
base_pt = 0
for t, d in tmp_list:
    if t in S:
        removable_list.append([t, d])
    else:
        S.add(t)
    base_pt += d
kind = len(S)
removable_list.sort(key=lambda x: x[1])

pt = [base_pt + kind*kind]
i = 0
for t, d in TD[K:]:
    if i >= len(removable_list):
        break
    if t in S:
        continue
    kind += 1
    base_pt += d - removable_list[i][1]
    pt.append(base_pt + kind*kind)
    i += 1
print(max(pt))

Submission status: Wrong Answer

Solution code:
from collections import Counter

def calc_conf(_list):
    t, d = list(zip(*_list))
    return sum(d) + len(set(t)) ** 2

N, K = [int(i) for i in input().split()]
data_ls = list()
for i in range(N):
    data_ls.append([int(i) for i in input().split()])
data_ls = sorted(data_ls, key=lambda x: x[1], reverse=True)
base = data_ls[:K]
result = base
result_score = calc_conf(result)

t, d = list(zip(*result))
candidate_ls = list()
candidate_t_ls = list()
for i in data_ls[K:]:
    if i[0] not in candidate_t_ls:
        candidate_ls.append(i)
        candidate_t_ls.append(i[0])
duplicate_ls = list()
dupli_count = len(result) - len(set([i[0] for i in result]))
complete = list()
complete_t_ls = list()
for i in result:
    if i[0] not in complete_t_ls:
        complete.append(i)
        complete_t_ls.append(i[0])
    else:
        duplicate_ls.append(i)
assert dupli_count == len(duplicate_ls)
for i in range(dupli_count, -1, -1):
    candidate = complete + duplicate_ls[:i] + candidate_ls[:dupli_count-i]
    candidate_score = calc_conf(candidate)
    if result_score < candidate_score:
        result_score = candidate_score
        result = candidate
print(result_score)

Submission status: Wrong Answer

Solution code:
from collections import Counter

def calc_conf(_list):
    t, d = list(zip(*_list))
    return sum(d) + len(set(t)) ** 2

N, K = [int(i) for i in input().split()]
data_ls = list()
for i in range(N):
    data_ls.append([int(i) for i in input().split()])
data_ls = sorted(data_ls, key=lambda x: x[1], reverse=True)
base = data_ls[:K]
result = base
result_score = calc_conf(result)

t, d = list(zip(*result))
candidate_ls = list()
candidate_t_ls = list()
for i in data_ls[K:]:
    if i[0] not in candidate_t_ls:
        candidate_ls.append(i)
        candidate_t_ls.append(i[0])

ct = Counter([i[0] for i in result])
for value, count in ct.items():
    duplicate = [i for i in base if i[0] == value]
    base = [i for i in base if i[0] != value]
    for i in range(1,count):
        candidate = base + candidate_ls[:count - i] + duplicate[:i]
        candidate_score = calc_conf(candidate)
        if result_score < candidate_score:
            result_score = candidate_score
            result = base + candidate_ls[:i] + duplicate[count - i:]
            candidate_ls = candidate_ls[i:]
    base = result.copy()
print(result_score)

Submission status: Wrong Answer

Solution code:
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])
        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)

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

Solution code:
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
try:
    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)
except:
    print(mx)

Submission status: Wrong Answer

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

for x in range(0,N):
    TDlist.append(list(map(int,input().split())))

TDlist= sorted(TDlist, key = lambda x: x[1],reverse=True)
kinds = set()
seconds = []
answer = 0

for x in TDlist[:K]:
    if not x[0] in kinds:
        kinds.add(x[0])
    else:
        seconds.append(x[1])
    answer += x[1]

answer += len(kinds)**2
score = answer
max_score = answer

for x in TDlist[K:]:
    if seconds == []:
        break
    if x[0] in kinds:
        continue
    kinds.add(x[0])
    y = seconds.pop()
    score += (x[1] - y) + (len(kinds)*2 -1)
    max_score = max(score,answer)
    

answer = max(answer,max_score)

print(answer)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
from heapq import heapify, heappop

N, K = map(int, input().split())
sushi = [tuple(map(int, input().split())) for _ in range(N)]

sushi.sort(key=lambda x: (x[1], x[0]), reverse=True)

maxs = []
rems = []
kinds = set()
for k, g in groupby(sorted(sushi[:K], key=lambda x: x[0]), key=lambda x: x[0]):
    d = list(list(zip(*g))[1])
    maxs.append(d.pop(0))
    rems.extend(d)
    kinds.add(k)
heapify(rems)
ans = sum(maxs) + sum(rems) + len(kinds)**2

for k, g in groupby(sushi[:K], key=lambda x: x[0]):
    if not rems:
        break
    if k in kinds:
        continue
    d = list(list(zip(*g))[1])
    maxs.append(d.pop(0))
    heappop(rems)
    kinds.add(k)
    ans = max(ans, sum(maxs) + sum(rems) + len(kinds)**2)

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
from heapq import heapify, heappop

N, K = map(int, input().split())
sushi = [tuple(map(int, input().split())) for _ in range(N)]

srt = sorted(sushi, key=lambda x: x[1], reverse=True)
eat, non_eat = srt[:K], srt[K:]

maxs = []
rems = []
kinds = set()
for k, g in groupby(sorted(eat, key=lambda x: x[0]), key=lambda x: x[0]):
    d = list(list(zip(*g))[1])
    maxs.append(d.pop(0))
    rems.extend(d)
    kinds.add(k)
heapify(rems)
ans = sum(maxs) + sum(rems) + len(kinds)**2

for k, g in groupby(sorted(non_eat, key=lambda x: x[0]), key=lambda x: x[0]):
    if k in kinds:
        continue
    if not rems:
        break
    d = list(list(zip(*g))[1])
    maxs.append(d.pop(0))
    heappop(rems)
    kinds.add(k)
    ans = max(ans, sum(maxs) + sum(rems) + len(kinds)**2)

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
from heapq import heapify, heappop

N, K = map(int, input().split())
sushi = [tuple(map(int, input().split())) for _ in range(N)]

srt = sorted(sushi, key=lambda x: x[1], reverse=True)
eat, non_eat = srt[:K], srt[K:]

maxs = []
rems = []
kinds = set()
for k, g in groupby(sorted(eat, key=lambda x: x[0]), key=lambda x: x[0]):
    d = list(list(zip(*g))[1])
    maxs.append(d.pop(0))
    rems.extend(d)
    kinds.add(k)
heapify(rems)
ans = sum(maxs) + sum(rems) + len(kinds)**2

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq as hq
from collections import Counter
N,K = map(int,input().split())
t_d = []
for _ in range(N):
    t,d = map(int,input().split())
    t_d.append([t,d])
t_d = sorted(t_d, key=lambda x:-x[1])
kiso = 0
neta = Counter()
stack = []
for t,d in t_d[:K]:
    kiso += d
    neta[t] += 1
    hq.heappush(stack,[d,t])
ans = kiso + len(neta)**2
for t,d in t_d[K:]:
    if neta[t] > 0 : continue
    while(stack):
        d0,t0 = hq.heappop(stack)
        if neta[t0] > 1:
            kiso += d - d0
            neta[t0] -= 1
            neta[t] += 1
            ans = max(ans, kiso+len(neta)**2)
            hq.heappush(stack,[d0,t0])
            break
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq as hq
from collections import Counter
N,K = map(int,input().split())
t_d = []
for _ in range(N):
    t,d = map(int,input().split())
    t_d.append([t,d])
sorted(t_d, key=lambda x:-x[1])
kiso = 0
neta = Counter()
stack = []
for t,d in t_d[:K]:
    kiso += d
    neta[t] += 1
    stack.append((t,d))
ans = kiso + len(neta)**2
tmp = ans
for t,d in t_d[K:]:
    if t in neta : continue
    while(stack):
        t0,d0 = stack.pop()
        if neta[t0] > 1:
            tmp += d - d0 + 2*len(neta) + 1
            ans = max(ans,tmp)
            neta[t0] -= 1
            neta[t] += 1
            break
    else:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq as hq
from collections import Counter
N,K = map(int,input().split())
t_d = []
for _ in range(N):
    t,d = map(int,input().split())
    t_d.append([t,d])
sorted(t_d, key=lambda x:-x[1])
kiso = 0
neta = Counter()
stack = []
for t,d in t_d[:K]:
    kiso += d
    neta[t] += 1
    stack.append([d,t])
ans = kiso + len(neta)**2
tmp = ans
for t,d in t_d[K:]:
    if t in neta : continue
    while(stack):
        d0,t0 = stack.pop()
        if neta[t0] > 1:
            tmp += d - d0 + 2*len(neta) + 1
            ans = max(ans,tmp)
            neta[t0] -= 1
            neta[t] += 1
            break
    else:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq as hq
from collections import Counter
N,K = map(int,input().split())
t_d = []
for _ in range(N):
    t,d = map(int,input().split())
    t_d.append([t,d])
sorted(t_d, key=lambda x:-x[1])
kiso = 0
neta = Counter()
stack = []
for t,d in t_d[:K]:
    kiso += d
    neta[t] += 1
    hq.heappush(stack,[d,t])
ans = kiso + len(neta)**2
tmp = ans
for t,d in t_d[K:]:
    if t in neta : continue
    while(stack):
        d0,t0 = hq.heappop(stack)
        if neta[t0] > 1:
            tmp += d - d0 + 2*len(neta) + 1
            ans = max(ans,tmp)
            neta[t0] -= 1
            neta[t] += 1
            break
    else:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq as hq
from collections import Counter
N,K = map(int,input().split())
t_d = []
for _ in range(N):
    t,d = map(int,input().split())
    t_d.append([t,d])
sorted(t_d, key=lambda x:-x[1])
kiso = 0
neta = Counter()
stack = []
for t,d in t_d[:K]:
    kiso += d
    neta[t] += 1
    hq.heappush(stack,[d,t])
ans = kiso + len(neta)**2
for t,d in t_d[K:]:
    if t in neta : continue
    while(stack):
        d0,t0 = hq.heappop(stack)
        if neta[t0] > 1:
            kiso += d
            kiso -= d0
            ans = max(ans, kiso+len(neta)**2)
            neta[t0] -= 1
            neta[t] += 1
            break
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq as hq
from collections import Counter
N,K = map(int,input().split())
t_d = []
for _ in range(N):
    t,d = map(int,input().split())
    t_d.append([t,d])
sorted(t_d, key=lambda x:-x[1])
kiso = 0
neta = Counter()
stack = []
for t,d in t_d[:K]:
    kiso += d
    neta[t] += 1
    hq.heappush(stack,[d,t])
ans = kiso + len(neta)**2
for t,d in t_d[K:]:
    if t in neta : continue
    while(stack):
        d0,t0 = hq.heappop(stack)
        if neta[t0] > 1:
            kiso += d
            kiso -= d0
            neta[t0] -= 1
            neta[t] += 1
            ans = max(ans, kiso+len(neta)**2)
            break
    else:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq as hq
from collections import Counter
N,K = map(int,input().split())
t_d = []
for _ in range(N):
    t,d = map(int,input().split())
    t_d.append([t,d])
sorted(t_d, key=lambda x:-x[1])
kiso = 0
neta = Counter()
stack = []
for t,d in t_d[:K]:
    kiso += d
    neta[t] += 1
    hq.heappush(stack,[d,t])
ans = kiso + len(neta)**2
for t,d in t_d[K:]:
    if t in neta : continue
    while(stack):
        d0,t0 = hq.heappop(stack)
        if neta[t0] > 1:
            kiso += d
            kiso -= d0
            neta[t0] -= 1
            neta[t] += 1
            ans = max(ans, kiso+len(neta)**2)
            hq.heappush(stack,[d0,t0])
            break
    else:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq as hq
from collections import Counter
N,K = map(int,input().split())
t_d = []
for _ in range(N):
    t,d = map(int,input().split())
    t_d.append([t,d])
sorted(t_d, key=lambda x:-x[1])
kiso = 0
neta = Counter()
stack = []
for t,d in t_d[:K]:
    kiso += d
    neta[t] += 1
    hq.heappush(stack,[d,t])
ans = kiso + len(neta)**2
for t,d in t_d[K:]:
    if t in neta : continue
    while(stack):
        d0,t0 = hq.heappop(stack)
        if neta[t0] > 1:
            kiso += d
            kiso -= d0
            neta[t0] -= 1
            neta[t] += 1
            hq.heappush(stack,[d0,t0])
            break
    ans = max(ans, kiso+len(neta)**2)
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq as hq
from collections import Counter
N,K = map(int,input().split())
t_d = []
for _ in range(N):
    t,d = map(int,input().split())
    t_d.append([t,d])
sorted(t_d, key=lambda x:-x[1])
kiso = 0
neta = Counter()
stack = []
for t,d in t_d[:K]:
    kiso += d
    neta[t] += 1
    hq.heappush(stack,[d,t])
ans = kiso + len(neta)**2
for t,d in t_d[K:]:
    if neta[t] > 0 : continue
    while(stack):
        d0,t0 = hq.heappop(stack)
        if neta[t0] > 1:
            kiso += d - d0
            neta[t0] -= 1
            neta[t] += 1
            ans = max(ans, kiso+len(neta)**2)
            hq.heappush(stack,[d0,t0])
            break
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq as hq
from collections import Counter
N,K = map(int,input().split())
t_d = []
for _ in range(N):
    t,d = map(int,input().split())
    t_d.append([t,d])
sorted(t_d, key=lambda x:-x[1])
kiso = 0
neta = Counter()
stack = []
for t,d in t_d[:K]:
    kiso += d
    neta[t] += 1
    hq.heappush(stack,[d,t])
ans = kiso + len(neta)**2
for t,d in t_d[K:]:
    if neta[t] > 0 : continue
    while(stack):
        d0,t0 = hq.heappop(stack)
        if neta[t0] > 1:
            kiso += d - d0
            neta[t0] -= 1
            neta[t] += 1
            ans = max(ans, kiso+len(neta)**2)
            hq.heappush(stack,[d0,t0])
print(ans)

Submission status: Wrong Answer

Solution code:
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()

print(A)

for i in range(k):
    d,t = A[i]
    if t in st:
        X.append(d)
    else:
        Y.append(d)
        st.add(t)

print(X)

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)

Submission status: Wrong Answer

Solution code:
from  operator import itemgetter
import numpy as np

n, k = map(int, input().split())

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

#print(sushi)

sushi.sort(key=itemgetter(1), reverse=True)

#print(sushi)

sushi = np.array(sushi)

#print(sushi)

total = len(set(sushi[:,0]))

#print(total)

initial = len(set(sushi[:k, 0]))

#print(initial)

g = []

#print(initial, total)
for i in range(initial, total+1):
    check = [0 for i in range(n)]
    value = i * i
    types = []
    s = 0
    for j in range(i):
        while True:
            if sushi[s, 0] not in types:
                types.append(sushi[s, 0])
                value += sushi[s, 1]
                check[s] = 1
                s += 1
                break
            s += 1

    s = 0
    for j in range(k-i):
        while True:
            if check[s] == 0:
                value += sushi[s, 1]
                s += 1
                break
            s += 1
    
    g.append(value)

#print(g)
print(max(g))