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 copy
from collections import namedtuple

def first_true(iterable, default=None, pred=None):
    """Returns the first true value in the iterable.

    If no true value is found, returns *default*

    If *pred* is not None, returns the first item
    for which pred(item) is true.

    """
    # first_true([a,b,c], x) --> a or b or c or x
    # first_true([a,b], x, f) --> a if f(a) else b if f(b) else x
    return next(filter(pred, iterable), default)

Sushi = namedtuple('Sushi', ('t', 'd'))

n, k = map(int, input().split(' '))
all_sushi = []
for i in range(n):
    t, d = map(int, input().split(' '))
    all_sushi.append(Sushi(t=t, d=d))

all_sushi.sort(key=lambda e: e.d, reverse=True)

all_t = set(map(lambda e:e.t, all_sushi))

#
# Ignore types and select more delicious order.
#
selected_sushi = all_sushi[:k]
addable_sushi = all_sushi[k:]
cur_d = sum(map(lambda e:e.d, selected_sushi))
all_t = set(map(lambda e:e.t, all_sushi))
cur_t = set(map(lambda e:e.t, selected_sushi))
cur_t_len = len(cur_t)
cur_sc = cur_d + cur_t_len * cur_t_len
best_sc = cur_sc

#
# Prepare to add sushi.
#
add_t = all_t - cur_t
add_sushi = []
for t in add_t:
    s = next(filter(lambda e:e.t == t, addable_sushi), None)
    if s is not None:
        add_sushi.append(s)
add_sushi = sorted(add_sushi, key=lambda e:e.d, reverse=True)

#
# Try to decrement number of types.
#
removable_sushi = copy.copy(selected_sushi)
for t in cur_t:
    r = next(filter(lambda e:e.t == t, selected_sushi), None)
    removable_sushi.remove(r)

while (len(removable_sushi) > 0) and (len(add_sushi) > 0):
    r = removable_sushi[-1]
    removable_sushi = removable_sushi[:-1]
    a = add_sushi[0]
    add_sushi = add_sushi[1:]
    cur_sc -= r.d
    cur_sc += a.d
    cur_sc -= cur_t_len * cur_t_len
    cur_t_len += 1
    cur_sc += cur_t_len + cur_t_len
    if best_sc < cur_sc:
        best_sc = cur_sc

print(best_sc)

Submission status: Wrong Answer

Solution code:
import copy
from collections import namedtuple

def first_true(iterable, default=None, pred=None):
    """Returns the first true value in the iterable.

    If no true value is found, returns *default*

    If *pred* is not None, returns the first item
    for which pred(item) is true.

    """
    # first_true([a,b,c], x) --> a or b or c or x
    # first_true([a,b], x, f) --> a if f(a) else b if f(b) else x
    return next(filter(pred, iterable), default)

Sushi = namedtuple('Sushi', ('t', 'd'))

n, k = map(int, input().split(' '))
all_sushi = []
for i in range(n):
    t, d = map(int, input().split(' '))
    all_sushi.append(Sushi(t=t, d=d))

all_sushi.sort(key=lambda e: e.d, reverse=True)

all_t = set(map(lambda e:e.t, all_sushi))

#
# Ignore types and select more delicious order.
#
selected_sushi = all_sushi[:k]
addable_sushi = all_sushi[k:]
cur_d = sum(map(lambda e:e.d, selected_sushi))
all_t = set(map(lambda e:e.t, all_sushi))
cur_t = set(map(lambda e:e.t, selected_sushi))
cur_sc = cur_d + len(cur_t) ** 2
best_sc = cur_sc

#
# Prepare to add sushi.
#
add_t = all_t - cur_t
add_sushi = []
for t in add_t:
    s = next(filter(lambda e:e.t == t, addable_sushi), None)
    if s is not None:
        add_sushi.append(s)
add_sushi = sorted(add_sushi, key=lambda e:e.d, reverse=True)

#
# Try to decrement number of types.
#
removable_sushi = copy.copy(selected_sushi)
for t in cur_t:
    r = next(filter(lambda e:e.t == t, selected_sushi), None)
    removable_sushi.remove(r)

while (len(removable_sushi) > 0) and (len(add_sushi) > 0):
    r = removable_sushi[0]
    removable_sushi = removable_sushi[1:]
    a = add_sushi[0]
    add_sushi = add_sushi[1:]
    cur_sc -= r.d
    cur_sc += a.d
    cur_sc -= len(cur_t) ** 2
    cur_t |= {a.t}
    cur_sc += len(cur_t) ** 2
    if best_sc < cur_sc:
        best_sc = cur_sc

print(best_sc)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
sushi = []
for i in range(n):
    tmp = list(map(int, input().split()))
    sushi.append(tmp)
eaten = set() # 食べた種類を記憶(集合型)
chohuku = [] # 同じ種類で二回以上食べた寿司のスコアを記録
scores = [] # Sのスコアのリスト

sushi.sort(key=lambda x:x[1])
sushi.reverse() # sushiをおいしさの降順にソート

tmp = 0 # Sのおいしさの合計を保持
for i in range(k): # sushiを美味しい順にk個食べる
    if sushi[i][0] in eaten: # 同じ種類の寿司を食べていた場合
        chohuku.append(sushi[i][1])
    else:
        eaten.add(sushi[i][0]) # 食べた寿司の種類を記憶
    tmp += sushi[i][1] # 食べた寿司のおいしさをtmpに追加
chohuku.sort()
scores.append(tmp+len(eaten)**2) # 今のSのスコアを記憶

for i in range(k,n):
    if not sushi[i][0] in eaten and chohuku != []:
        tmp -= chohuku.pop()
        eaten.add(sushi[i][0])
        tmp += sushi[i][1]
        scores.append(tmp+len(eaten)**2)
        
print(max(scores))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
sushi = [list(map(int, input().split())) for _ in range(N)]
sushi = sorted(sushi, key=lambda x: x[1], reverse=True)
typ = []
lstA = []
lstB = []
for item in sushi:
    if item[0] in typ:
        item[0] = 0
        lstB.append(item[1])
    else:
        typ.append(item[0])
        item[0] = 1
        lstA.append(item[1])
ans = 0
neta = 0
for i in range(K):
    ans += sushi[i][1]
    if sushi[i][0] == 1:
        neta += 1
ans += neta ** 2
C = sum(lstA[:neta])
D = sum(lstB[:K - neta])
for i in range(neta + 1, min(K, len(lstA))):
    tmp = C + lstA[i] + D - lstB[K - i]
    tmp += i ** 2
    ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
sushi = [list(map(int, input().split())) for _ in range(N)]
sorted(sushi, key=lambda x: x[1])
sushi.reverse()
typ = []
lstA = []
lstB = []
for item in sushi:
    if item[0] in typ:
        item[0] = 0
        lstB.append(item[1])
    else:
        typ.append(item[0])
        item[0] = 1
        lstA.append(item[1])
ans = 0
neta = 0
for i in range(K):
    ans += sushi[i][1]
    if sushi[i][0] == 1:
        neta += 1
ans += neta ** 2
C = sum(lstA[:neta])
D = sum(lstB[:K - neta])
for i in range(neta + 1, min(K, len(lstA))):
    tmp = C + lstA[i] + D - lstB[K - i]
    tmp += i ** 2
    ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
sushi = [list(map(int, input().split())) for _ in range(N)]
sorted(sushi, key=lambda x: x[1])
typ = []
lstA = []
lstB = []
for item in sushi:
    if item[0] in typ:
        item[0] = 0
        lstB.append(item[1])
    else:
        typ.append(item[0])
        item[0] = 1
        lstA.append(item[1])
ans = 0
neta = 0
for i in range(K):
    ans += sushi[i][1]
    if sushi[i][0] == 1:
        neta += 1
ans += neta ** 2
C = sum(lstA[:neta])
D = sum(lstB[:K - neta])
for i in range(neta + 1, min(K, len(lstA))):
    tmp = C + lstA[i] + D - lstB[K - i]
    tmp += i ** 2
    ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
sushi = [list(map(int, input().split())) for _ in range(N)]
sorted(sushi, key=lambda x: x[1])
typ = []
lstA = []
lstB = []
for item in sushi:
    if item[0] in typ:
        item[0] = 0
        lstB.append(item[1])
    else:
        typ.append(item[0])
        item[0] = 1
        lstA.append(item[1])
ans = 0
neta = 0
for i in range(K):
    ans += sushi[i][1]
    if sushi[i][0] == 1:
        neta += 1
ans += neta ** 2
for i in range(neta + 1, min(K, len(lstA)) + 1):
    tmp = sum(lstA[:i]) + sum(lstB[:K - i])
    tmp += i ** 2
    ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
sushi = [list(map(int, input().split())) for _ in range(N)]
sorted(sushi, key=lambda x: x[1])
typ = []
lstA = []
lstB = []
for item in sushi:
    if item[0] in typ:
        item[0] = 0
        lstB.append(item[1])
    else:
        typ.append(item[0])
        item[0] = 1
        lstA.append(item[1])
ans = 0
neta = 0
for i in range(K):
    ans += sushi[i][1]
    if sushi[i][0] == 1:
        neta += 1
ans += neta ** 2
for i in range(neta + 1, min(K, len(lstA))):
    tmp = sum(lstA[0:i]) + sum(lstB[0:K - i])
    tmp += i ** 2
    ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
sushi = [list(map(int, input().split())) for _ in range(N)]
sorted(sushi, key=lambda x: x[1])
typ = []
lstA = []
lstB = []
for item in sushi:
    if item[0] in typ:
        item[0] = 0
        lstB.append(item[1])
    else:
        typ.append(item[0])
        item[0] = 1
        lstA.append(item[1])
ans = 0
neta = 0
for i in range(K):
    ans += sushi[i][1]
    if sushi[i][0] == 1:
        neta += 1
ans += neta ** 2
for i in range(neta + 1, min(K, len(lstA))):
    tmp = sum(lstA[0:i]) + sum(lstB[0:K - 1])
    tmp += i ** 2
    ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding utf-8 -*-

n, k = map(int, input().split())
sushi = [list(map(int, input().split())) for _ in range(n)]
sushi.sort(key = lambda x: x[1], reverse = True)    # sort by d

mp_max = 0
op = 0
taberu_ind = set()
pop_cands = []
t_taberu = set()
# greedy for delicious point
for i in range(k):
    op += sushi[i][1]

    t = sushi[i][0]
    if t_taberu.isdisjoint({t}):    # new type
        t_taberu.add(t)
    else:
        pop_cands.append(i)

    taberu_ind.add(i)
mp_max = op + len(t_taberu)**2

print(mp_max)

Submission status: Wrong Answer

Solution code:
class sushi:
  def __init__(self, t, d):
    self.t = t
    self.d = d

list = []
N, K = map(int, input().split())
for i in range(N):
  t, d = map(int, input().split())
  list.append(sushi(t, d))
  
sorted_list = sorted(list, key=lambda sushi:sushi.d, reverse=True)

t_list = []
rev_list = []
d_sum = 0
for i in range(K):
  if t_list.count(list[i].t) == 0:
    t_list.append(list[i].t)
  else:
    rev_list.append(i)
  d_sum += list[i].d

tmp = pow(len(t_list), 2) + d_sum

flag = 0
while len(rev_list) != 0:
  pointer = rev_list.pop()
  for i in range(K, N, 1):
    if t_list.count(list[i].t) == 0:
      l = len(t_list)
      key = pow(l + 1, 2) + list[i].d - pow(l, 2) - list[pointer].d
      if key > 0:
        tmp += key
      else:
        flag = 1
      break
  if flag:
    break

print(tmp)

Submission status: Wrong Answer

Solution code:
class sushi:
  def __init__(self, t, d):
    self.t = t
    self.d = d

list = []
N, K = map(int, input().split())
for i in range(N):
  t, d = map(int, input().split())
  list.append(sushi(t, d))
  
sorted_list = sorted(list, key=lambda sushi:sushi.d, reverse=True)

t_list = []
rev_list = []
d_sum = 0
for i in range(K):
  if t_list.count(list[i].t) == 0:
    t_list.append(list[i].t)
  else:
    rev_list.append(i)
  d_sum += list[i].d

tmp = pow(len(t_list), 2) + d_sum

flag = 0
while len(rev_list) != 0:
  pointer = rev_list.pop()
  for i in range(K, N, 1):
    if t_list.count(list[i].t) != 0:
      continue
    else:
      l = len(t_list)
      key = pow(l + 1, 2) + list[i].d - pow(l, 2) - list[pointer].d
      if key > 0:
        tmp += key
        break
      else:
        flag = 1
  if flag:
    break

print(tmp)

Submission status: Wrong Answer

Solution code:
#ABC116D-VariousSushi.py

from sys import stdin
#from numpy import sum

input = stdin.readline

N, K = list(map(int, input().split()))
List1, List2 = [], []
kind = set()
point1, point2 = 0, 0

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

INPUT.sort(key = lambda x: -x[1])

for ti, di in INPUT[:K]:
    point1 += di
    if ti in kind:
        List1.append(di)
    kind.add(ti)

point2 = len(kind)

Score = point1 + point2**2
Test = Score

List1POP = List1.pop

for ti, di in INPUT[K:]:
    if 0 >= len(List1):
        break
    if ti in kind:
        continue
    Test = Test - List1POP() + di + 2 * point2 + 1
    point2 += 1
    Score = max(Score, Test)

print(Score)

Submission status: Wrong Answer

Solution code:
#ABC116D-VariousSushi.py

from sys import stdin
#from numpy import sum

input = stdin.readline

N, K = list(map(int, input().split()))
List1, List2 = [], []
kind = set()
point1, point2 = 0, 0

INPUT = [list(map(int, input().split())) for _ in range(N)]

INPUT.sort(key = lambda x: -x[1])

for ti, di in INPUT[:K]:
    point1 += di
    if ti in kind:
        List1.append(di)
    kind.add(ti)

point2 = len(kind)

Score = point1 + point2**2
Test = Score

List1POP = List1.pop

for ti, di in INPUT[K:]:
    if 0 >= len(List1):
        break
    if ti in kind:
        continue
    Test = Test - List1POP() + di + 2 * point2 + 1
    point2 += 1
    Score = max(Score, Test)

print(Score)

Submission status: Wrong Answer

Solution code:
#ABC116D-VariousSushi.py

from sys import stdin

input = stdin.readline

N, K = list(map(int, input().split()))
List1, List2 = [], []
kind = set()
point1, point2 = 0, 0

INPUT = [list(map(int, input().split())) for _ in range(N)]

INPUT.sort(key = lambda x: -x[1])

for i in range(K):
    point1 += INPUT[i][1]
    if INPUT[i][0] in kind:
        List1.append(INPUT[i][1])
    kind.add(INPUT[i][0])

point2 = len(kind)

Score = point1 + point2**2
Test = Score

List1POP = List1.pop

for ti, di in INPUT[K:]:
    if 0 >= len(List1):
        break
    if ti in kind:
        continue
    Test = Test - List1POP() + di + 2 * point2 + 1
    point2 += 1
    Score = max(Score, Test)

print(Score)

Submission status: Wrong Answer

Solution code:
#ABC116D-VariousSushi.py

from sys import stdin
#from numpy import sum

input = stdin.readline

N, K = list(map(int, input().split()))
List1, List2 = [], []
kind = set()
point1, point2 = 0, 0

INPUT = [list(map(int, input().split())) for _ in range(N)]

INPUT = sorted(INPUT, key = lambda x: -x[1])

for i in range(K):
    point1 += INPUT[i][1]
    if INPUT[i][0] in kind:
        List1.append(INPUT[i][1])
    kind.add(INPUT[i][0])

point2 = len(kind)

Score = point1 + point2**2
Test = Score

for ti, di in INPUT[K:]:
    if 0 >= len(List1):
        break
    if ti in kind:
        continue
    Test = Test - List1.pop() + di + 2 * point2 + 1
    point2 += 1
    Score = max(Score, Test)

print(int(Score))

Submission status: Wrong Answer

Solution code:
N, K = input().split()
N = int(N)
K = int(K)
sushiList = []
for i in range(N):
    netaT, bimiD = input().split()
    netaT = int(netaT)
    bimiD = int(bimiD)

    sushiList.append((netaT, bimiD))

sushiList.sort(key=lambda x:x[1], reverse=True)
cache = []
count = 0
oisii = 0
for sushi in sushiList:
    if sushi[0] in cache:
        if K^2 < sushi[1]:
            oisii = oisii + sushi[1]
            count = count + 1
    else:
        cache.append(sushi[0])
        oisii = oisii + sushi[1]
        count = count + 1

    if count == K:
        break

oisii = oisii + len(cache) * len(cache)
print(oisii)

Submission status: Wrong Answer

Solution code:
#ABC116D-VariousSushi.py

import sys
import numpy as np

input = sys.stdin.readline

N, K = [int(n) for n in input().split()]
INPUT = [[None for i in range(2)] for j in range(N)]
List1, List2 = [], []
kind1, kind2 = [], []

for i in range(N):
    INPUT[i][0], INPUT[i][1] = [int(m) for m in input().split()]

INPUT = sorted(INPUT, key = lambda x: x[1], reverse=True)

for i in range(N):
    if INPUT[i][0] not in kind1:
        if len(List1) < K:
            List1.append(INPUT[i][1])
            kind1.append(INPUT[i][0])
    elif INPUT[i][0] not in kind2:
        List2.append(INPUT[i][1])
        kind2.append(INPUT[i][0])

List2.sort()

kindnumber = len(kind1)
k = len(List2)-(K-kindnumber)

Score = np.sum(List1) + np.sum(List2[k:]) + kindnumber ** 2
del List2[k:]
Test = Score

for i in range(kindnumber):
    if List2 == []:
        break
    Test = Test - List1.pop() + List2.pop() - 2 * kindnumber + 1
    kindnumber = kindnumber - 1
    if Test >= Score:
        Score = Test
    
print(int(Score))

Submission status: Wrong Answer

Solution code:
#ABC116D-VariousSushi.py

import numpy as np

INPUT1 = [int(n) for n in input().split()]

N, K = INPUT1[0], INPUT1[1]
Parameters = [[None for col in range(2)] for row in range(N)]
Score= 0
kind = []
kindnumber = 0

for i in range(N):
    INPUT2 = [int(m) for m in input().split()]
    Parameters[i] = [INPUT2[0], INPUT2[1]]

Parameters = sorted(Parameters, key=lambda x: x[1], reverse=True)

def SearchNew(List, K, kind, I):
    newkind = -1
    kindset = kind[:]
    oldkindnumber = 0
    newkindnumber = 0
    for i in range(len(List)-K-I):
        oldkindnumber = len(set(kindset))
        kindset.append(List[K+I+i][0])
        newkindnumber = len(set(kindset))
        if oldkindnumber != newkindnumber:
            newkind = K+I+i
            break
    return newkind

def SearchDouble(List, kind, J):
    result = -1
    testkind = kind[:]
    oldkindnumber = len(set(testkind))
    testkindPop = testkind.pop
    for i in range(J):
        testkindPop(J-1-i)
        newkindnumber = len(set(testkind))
        if oldkindnumber == newkindnumber:
            result = J-1-i
            break
        oldkindnumber = newkindnumber
    return result

kindAppend = kind.append

for i in range(K):
    kindAppend(Parameters[i][0])
kindnumber = len(set(kind))
Score = np.sum(Parameters[:K], axis=0)[1] + kindnumber**2

Test1, Test2 = 0, Score

I, J = 0, K

for t in Parameters:
    kindnumber = len(set(kind))
    i = SearchNew(Parameters, K, kind, I)
    j = SearchDouble(Parameters, kind, J)
    Parameters[i], Parameters[j] = Parameters[j], Parameters[i]
    kind[j] = Parameters[i][0]
    if i != -1 and j != -1:
        kindnumber += 1
    else:
        I, J = i, j
        break
    Test1 = Test2 - Parameters[i][1] + Parameters[j][1] + kindnumber * 2 - 1
    Test2 = Test1
    if Score <= Test2:
        Score = Test2

print(Score)

Submission status: Wrong Answer

Solution code:
#ABC116D-VariousSushi.py

import numpy as np

INPUT1 = [int(n) for n in input().split()]

N, K = INPUT1[0], INPUT1[1]
Parameters = [[None for col in range(2)] for row in range(N)]
Score= 0
kind = []
kindnumber = 0

for i in range(N):
    INPUT2 = [int(m) for m in input().split()]
    Parameters[i] = [INPUT2[0], INPUT2[1]]

Parameters = sorted(Parameters, key=lambda x: x[1], reverse=True)

def SearchNew(List, K, kind, I):
    newkind = -1
    kindset = kind[:]
    oldkindnumber = 0
    newkindnumber = 0
    for i in range(len(List)-K-I):
        oldkindnumber = len(set(kindset))
        kindset.append(List[K+I+i][0])
        newkindnumber = len(set(kindset))
        if oldkindnumber != newkindnumber:
            newkind = K+I+i
            break
    return newkind

def SearchDouble(List, K, kind, J):
    result = -1
    testkind = kind[:]
    oldkindnumber = len(set(testkind))
    testkindPop = testkind.pop
    for i in range(K-J):
        testkindPop(K-J-1-i)
        newkindnumber = len(set(testkind))
        if oldkindnumber == newkindnumber:
            result = K-J-1-i
            break
        oldkindnumber = newkindnumber
    return result

kindAppend = kind.append

for i in range(K):
    kindAppend(Parameters[i][0])
kindnumber = len(set(kind))
Score = np.sum(Parameters[:K], axis=0)[1] + kindnumber**2

Test1, Test2 = 0, Score

I, J = 0, 0

for t in Parameters:
    kindnumber = len(set(kind))
    i = SearchNew(Parameters, K, kind, I)
    j = SearchDouble(Parameters, K, kind, J)
    Parameters[i], Parameters[j] = Parameters[j], Parameters[i]
    kind[j] = Parameters[i][0]
    if i != -1 and j != -1:
        kindnumber += 1
    else:
        I, J = i, j
        break
    Test1 = Test2 - Parameters[i][1] + Parameters[j][1] + kindnumber * 2 - 1
    Test2 = Test1
    if Score <= Test2:
        Score = Test2

print(Score)

Submission status: Wrong Answer

Solution code:
#ABC116D-VariousSushi.py

import numpy as np

INPUT1 = [int(n) for n in input().split()]

N, K = INPUT1[0], INPUT1[1]
Parameters = [[None for col in range(2)] for row in range(N)]
Score= 0
kind = []
kindnumber = 0

for i in range(N):
    INPUT2 = [int(m) for m in input().split()]
    Parameters[i] = [INPUT2[0], INPUT2[1]]

Parameters = sorted(Parameters, key=lambda x: x[1], reverse=True)

def SearchNew(List, K, kind):
    newkind = -1
    kindset = kind[:]
    oldkindnumber = 0
    newkindnumber = 0
    for i in range(len(List)-K):
        oldkindnumber = len(set(kindset))
        kindset.append(List[K+i][0])
        newkindnumber = len(set(kindset))
        if oldkindnumber != newkindnumber:
            newkind = K+i
            break
    return newkind

def SearchDouble(List, K, kind):
    result = -1
    testkind = kind[:]
    oldkindnumber = len(set(testkind))
    testkindPop = testkind.pop
    for i in range(K):
        testkindPop(K-1-i)
        newkindnumber = len(set(testkind))
        if oldkindnumber == newkindnumber:
            result = K-1-i
            break
        oldkindnumber = newkindnumber
    return result

kindAppend = kind.append

for i in range(K):
    kindAppend(Parameters[i][0])
kindnumber = len(set(kind))
Score = np.sum(Parameters[:K], axis=0)[1] + kindnumber**2

Test1, Test2 = 0, Score

for t in Parameters:
    kindnumber = len(set(kind))
    i = SearchNew(Parameters, K, kind)
    j = SearchDouble(Parameters, K, kind)
    Parameters[i], Parameters[j] = Parameters[j], Parameters[i]
    kind[j] = Parameters[i][0]
    if i != -1 and j != -1:
        kindnumber += 1
    else:
        break
    Test1 = Test2 - Parameters[i][1] + Parameters[j][1] + kindnumber * 2 - 1
    Test2 = Test1
    if Score <= Test2:
        Score = Test2

print(Score)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
s = []
s2=[]#bakrtu
a=[0]#point
b=0#syurui
for i in range(n):
    t,d=map(int,input().split())
    s.append([t,d])
    s2.append(0)
l=sorted(s,key=lambda x:x[1])
l.reverse()
for i in range(k):
    a[0]+=l[i][1]
    s2[l[i][0]-1]+=1
    if s2[l[i][0]-1]==1:
        b+=1
m=1#koukann
for i in range(k-1,-1,-1):
    if s2[l[i][0]-1]!=1:
        a.append(0)
        for j in range(k,n):
            if s2[l[j][0]-1]==0:
                a[m]=a[m-1]-l[i][1]+l[j][1]
                s2[l[i][0]-1]-=1
                s2[l[j][0]-1]+=1
                m+=1
                break 
x=a[0]+b*b
for i in range(1,len(a)):
        y=a[i]+(b+i)**2
        if x<y:
                x=y

print(x)

Submission status: Wrong Answer

Solution code:
import copy

N, K = map(int,input().split())
T=[]
D=[]
A = [list(map(int, input().split())) for _ in range(N)] #t,d

# print(N,K,A)

dp=[[-1]*(K+1) for i in range(N+1)]
W = [1]*(N+1)
SYURUI=[[[]]*(K+1) for i in range(N+1)]

# print(dp)
# print(SYURUI)

# init
for i in range(N+1):
    dp[i][0]=0
for i in range(K+1):
    dp[0][i]=0

#loop
for i in range(1,N+1):
    for w in range(1,K+1):
        if w>=W[i-1]:
            syurui=A[i-1][0]
            oisisa_p=A[i-1][1]
            # print(syurui,oisisa_p)
            # print(i,w)
            # print(SYURUI[i-1][w-W[i-1]])
            # print(len(SYURUI[i-1][w-W[i-1]]))
            # print((len(SYURUI[i-1][w-W[i-1]]))**2)
            if(syurui in SYURUI[i-1][w-W[i-1]]):
                syurui_p = 0#(len(SYURUI[i-1][w-W[i-1]]))**2
            else:
                syurui_p = (len(SYURUI[i-1][w-W[i-1]])+1)**2 - (len(SYURUI[i-1][w-W[i-1]]))**2
            # print(syurui_p,oisisa_p)
            dp[i][w]=max(dp[i-1][w-W[i-1]]+syurui_p+oisisa_p,dp[i-1][w])
            taberu= dp[i-1][w-W[i-1]]+syurui_p+oisisa_p > dp[i-1][w]
            if taberu:
                SYURUI[i][w]=copy.deepcopy(SYURUI[i-1][w-W[i-1]])
                if(not (syurui in SYURUI[i-1][w-W[i-1]]) ):
                    SYURUI[i][w].append(syurui)
            else:
                SYURUI[i][w]=copy.deepcopy(SYURUI[i-1][w])
        else:
            dp[i][w]=dp[i-1][w]
    # print(dp)
    # print(SYURUI)
    # for i in range(N+1):
    #     print(dp[i])

print(dp[N][K])

Submission status: Wrong Answer

Solution code:
#ABC116D-VariousSushi.py

INPUT1 = [int(n) for n in input().split()]

N, K = INPUT1[0], INPUT1[1]
Parameters = []
Score, Test = 0, 0
flag1, flag2 = 0, 0
kind = []
kindnumber = 0

ParametersAppend = Parameters.append
for i in range(N):
    INPUT2 = [int(m) for m in input().split()]
    ParametersAppend([INPUT2[0], INPUT2[1]])

Parameters = sorted(Parameters, key=lambda x: x[1], reverse=True)

def binanrySearch(kind, target):
    testkind = kind[:]
    testkind.sort()
    left, right = 0, len(testkind)-1
    while(right >= left):
        mid = int((right + left) / 2)
        if testkind[mid] == target:
            return True
        elif testkind[mid] > target:
            right = mid - 1
        elif testkind[mid] < target:
            left = mid + 1
    return False

def SumScore(List, K, kindnumber):
    taste = 0
    for i in range(K):
        taste += List[i][1]
    return taste + kindnumber * kindnumber

def SearchNew(List, K, kind):
    newkind = -1
    for i in range(len(List)-K):
        if binanrySearch(kind, List[K+i][0]) == False:
            newkind = K+i
            break
    return newkind

def SearchDouble(List, K, kind):
    result = -1
    testkind = kind[:]
    testkindPop = testkind.pop
    for i in range(K):
        testkindPop(K-1-i)
        if binanrySearch(testkind, List[K-1-i][0]) == True:
            result = K-1-i
            break
    return result

kindAppend = kind.append

for i in range(K):
    kindAppend(Parameters[i][0])
kindnumber = len(set(kind))
Score = SumScore(Parameters, K, kindnumber)
kind.clear()

for t in Parameters:
    for i in range(K):
        kindAppend(Parameters[i][0])
    kindnumber = len(set(kind))
    i = SearchNew(Parameters, K, kind)
    j = SearchDouble(Parameters, K, kind)
    Parameters[i], Parameters[j] = Parameters[j], Parameters[i]
    if i != -1 and j != -1:
        kindnumber += 1
    else:
        break
    Test = Score - Parameters[i][1] + Parameters[j][1] + kindnumber**2
    if Score <= Test:
        Score = Test
    kind.clear()

print(Score)

Submission status: Wrong Answer

Solution code:
#ABC116D-VariousSushi.py

import copy

INPUT1 = [int(n) for n in input().split()]

N, K = INPUT1[0], INPUT1[1]
Score= 0
Box1 = [[0, 0, 0]]
Box2 = []
Box3 = []
FLAG = 0


for i in range(N):
    INPUT2 = [int(m) for m in input().split()]
    Box1.append([INPUT2[0], INPUT2[1], 1])
    Box2.append(INPUT2[0])

Box1 = sorted(Box1, key=lambda x: (x[2],x[1],x[0]), reverse=True)

def SumScore(List):
    taste = 0
    kind = []
    for i in range(len(List)):
        taste += List[i][1]
        kind.append(List[i][0])
    return taste + len(set(kind))**2

def putIntoBox(List, k, K): #List:Parameters, k:The number of kinds, K:The length of Output Box
    Box1_modified = copy.deepcopy(List)
    Box_output = []
    SetKind = []
    flag, flag2 = 0, 0
    length = 0
    i = 0
    while flag2 == 0:
        i = i % len(Box1_modified)
        if Box1_modified[i][0] not in SetKind and flag == 0 and Box1_modified[i][2] == 1:
            Box_output.append([Box1_modified[i][0], Box1_modified[i][1]])
            Box1_modified[i][2] = 0
            SetKind.append(Box1_modified[i][0])
            if len(SetKind) == k:
                flag = 1
                i = 0
        elif Box1_modified[i][0] in SetKind and flag == 1 and Box1_modified[i][2] == 1:
            Box_output.append([Box1_modified[i][0], Box1_modified[i][1]])
            Box1_modified[i][2] = 0

        if len(Box_output) == K:
            flag2 = 1
        elif Box1_modified[i][0] == 0:
            if length == len(Box_output):
                flag2 = 1
            length = len(Box_output)
        i += 1
    return SumScore(Box_output)

k_max = min(len(set(Box2)), K)

for k in range(k_max):
    Box3.append(putIntoBox(Box1, k, K))

Score = max(Box3)

print(Score)