input
stringlengths 20
127k
| target
stringlengths 20
119k
| problem_id
stringlengths 6
6
|
---|---|---|
# -*- coding: utf-8 -*-
import sys
from heapq import heapify, heappop
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7
N, K = MAP()
DT = []
tmp = [[] for i in range(N)]
for i in range(N):
t, d = MAP()
DT.append((d, t))
DT.sort(reverse=1)
que = []
D = {}
dcnt = 0
# ポイント上位K個をまず取る
for i in range(K):
d, t = DT[i]
dcnt += d
# 種類tが1つ目
if t not in D:
D[t] = d
# 2つ目以降なら、最大のものはDに残して他はキューへ
else:
if D[t] < d:
D[t], d = d, D[t]
que.append((d, t))
heapify(que)
tcnt = len(D)
ans = dcnt + tcnt**2
for i in range(K, N):
d2, t2 = DT[i]
# 既に持っている種類ならスキップ
if t2 in D:
continue
D[t2] = d2
tmp = []
# 全部1種類ずつならもう種類数を増やせないので終了
if not que:
break
d, t = heappop(que)
# この候補と交換してみる
dcnt -= d
dcnt += d2
tcnt += 1
ans = max(ans, dcnt + tcnt**2)
print(ans)
| # -*- coding: utf-8 -*-
import sys
def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7
N, K = MAP()
DT = []
tmp = [[] for i in range(N)]
for i in range(N):
t, d = MAP()
DT.append((d, t))
DT.sort(reverse=1)
A = []
D = {}
dcnt = 0
# ポイント上位K個をまず取る
for i in range(K):
d, t = DT[i]
dcnt += d
# 種類tが1つ目なら、確定で残す
if t not in D:
D[t] = d
# 2つ目以降なら、最大のものを残して他は交換候補へ
else:
if D[t] < d:
D[t], d = d, D[t]
A.append(d)
A.sort(reverse=1)
tcnt = len(D)
ans = dcnt + tcnt**2
for i in range(K, N):
d2, t2 = DT[i]
# 既に持っている種類ならスキップ
if t2 in D:
continue
D[t2] = d2
# 全部1種類ずつ(交換候補がもうない)なら、もう種類数を増やせないので終了
if not A:
break
d = A.pop()
# この候補と交換してみる
dcnt -= d
dcnt += d2
tcnt += 1
ans = max(ans, dcnt + tcnt**2)
print(ans)
| p03148 |
from operator import itemgetter
N, K = list(map(int, input().split()))
TD = [list(map(int, input().split())) for _ in range(N)]
TD.sort(key=itemgetter(1), reverse=True)
selected = TD[:K]
dlc = sum(map(itemgetter(1), selected))
variety = set(map(itemgetter(0), selected))
used = set()
overlap = [] # ダブっているネタの美味しさのリスト
for t, d in TD[:K]:
if t not in used:
used.add(t)
else:
overlap.append(d)
answer = dlc + len(variety)**2
for t, d in TD[K:]:
if not overlap:
break
if t in used:
continue
ov = overlap.pop()
dlc = dlc - ov + d
used.add(t)
answer = max(answer, dlc + len(used)**2)
print(answer)
| import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
N, K = lr()
TD = [lr() for _ in range(N)]
TD.sort(key=lambda x: x[1], reverse=True)
used = set()
overlap = []
for t, d in TD[:K]:
if t in used:
overlap.append(d)
else:
used.add(t)
delicious = sum([x[1] for x in TD[:K]])
answer = delicious + len(used) ** 2
for i in range(K, N):
if not overlap:
break
if TD[i][0] in used:
continue
delicious += (TD[i][1] - overlap.pop())
used.add(TD[i][0])
if answer < delicious + len(used)**2:
answer = delicious + len(used)**2
print(answer)
# 30 | p03148 |
from heapq import heappush, heappop, heapreplace
INF = 10 ** 9 + 5
N, K, *TD = list(map(int, open(0).read().split()))
E = [[] for _ in range(N)]
for t, d in zip(*[iter(TD)] * 2):
E[t - 1].append(d)
for e in E:
if e:
e.sort(reverse=True)
else:
e.append(-INF)
E.sort(key=lambda x: -x[0])
cur = 0
Q = []
for ei in E[:K]:
cur += ei[0]
for eij in ei[1:]:
heappush(Q, -eij)
for ei in E[K:]:
for eij in ei:
heappush(Q, -eij)
res = cur + K * K
for x in reversed(list(range(1, K))):
v, w = E[x][0], -Q[0]
if v < w:
heappop(Q)
cur += w
heappush(Q, -v)
cur -= v
res = max(res, cur + x * x)
print(res)
| from heapq import heappush, heapreplace
def main():
INF = 10 ** 9 + 5
N, K, *TD = list(map(int, open(0).read().split()))
E = [[] for _ in range(N)]
for t, d in zip(*[iter(TD)] * 2):
E[t - 1].append(d)
for e in E:
if e:
e.sort(reverse=True)
else:
e.append(-INF)
E.sort(key=lambda x: -x[0])
cur = 0
Q = []
for ei in E[:K]:
cur += ei[0]
for eij in ei[1:]:
heappush(Q, -eij)
for ei in E[K:]:
for eij in ei:
heappush(Q, -eij)
res = cur + K * K
for x in reversed(list(range(1, K))):
v, w = E[x][0], -Q[0]
if v < w:
heapreplace(Q, -v)
cur += w - v
res = max(res, cur + x * x)
print(res)
if __name__ == '__main__':
main()
| p03148 |
from heapq import heappush, heappop, heapreplace
def main():
INF = 10 ** 9 + 7
N, K, *TD = list(map(int, open(0).read().split()))
E = [[] for _ in range(N)]
for t, d in zip(*[iter(TD)] * 2):
E[t - 1].append(d)
for e in E:
if e:
e.sort(reverse=True)
else:
e.append(-INF)
E.sort(key=lambda x: -x[0])
cur = 0
Q = []
for i in range(K):
cur += E[i][0]
for j in range(1, len(E[i])):
heappush(Q, -E[i][j])
for i in range(K, N):
for eij in E[i]:
heappush(Q, -eij)
res = cur + K * K
for x in reversed(list(range(1, K))):
v, w = E[x][0], -Q[0]
if v < w:
heapreplace(Q, -v)
cur += w - v
tmp = cur + x * x
if res < tmp:
res = tmp
print(res)
if __name__ == '__main__':
main()
| from heapq import heappush, heapreplace
def main():
INF = 10 ** 9 + 5
N, K, *TD = list(map(int, open(0).read().split()))
E = [[] for _ in range(N)]
for t, d in zip(*[iter(TD)] * 2):
E[t - 1].append(d)
for e in E:
if e:
e.sort(reverse=True)
else:
e.append(-INF)
E.sort(key=lambda x: -x[0])
cur = 0
Q = []
for ei in E[:K]:
cur += ei[0]
for eij in ei[1:]:
heappush(Q, -eij)
for ei in E[K:]:
for eij in ei:
heappush(Q, -eij)
res = cur + K * K
for x in reversed(list(range(1, K))):
v, w = E[x][0], -Q[0]
if v < w:
heapreplace(Q, -v)
cur += w - v
tmp = cur + x * x
if res < tmp:
res = tmp
print(res)
if __name__ == '__main__':
main()
| p03148 |
import heapq as hq
from collections import deque
from copy import deepcopy
N, K = list(map(int, input().split()))
Sushi = [None for _ in range(N)]
TotalType = set()
for _ in range(N):
t, d = list(map(int, input().split()))
Sushi[_] = [d, t]
Sushi.sort(reverse = True)
ChosenType = set()
Double = []
hq.heapify(Double)
BasePoint = 0
for goodsushi in Sushi[:K]:
BasePoint += goodsushi[0]
if goodsushi[1] in ChosenType:
hq.heappush(Double, goodsushi)
else: ChosenType |= {goodsushi[1]}
NotChosen = deque(deepcopy(Sushi[K:]))
MaxHappiness = BasePoint + len(ChosenType)**2
while Double and NotChosen:
TrushPoint, TrushType = hq.heappop(Double)
BasePoint -= TrushPoint
while NotChosen:
addPoint, addType = NotChosen.popleft()
if addType not in ChosenType:
BasePoint += addPoint
ChosenType |= {addType}
break
MaxHappiness = max(MaxHappiness, BasePoint + len(ChosenType)**2)
print(MaxHappiness) | import sys
def solve():
input = sys.stdin.readline
N, K = list(map(int, input().split()))
S = []
for _ in range(N):
t, d = list(map(int, input().split()))
S.append((d, t))
S.sort(reverse = True)
upperKCount = 0
appeared = set()
maxPoint = 0
for i in range(K):
maxPoint += S[i][0]
if S[i][1] not in appeared:
upperKCount += 1
appeared |= {S[i][1]}
maxPoint += upperKCount ** 2
appeared = set()
typeMax = []
typeNum = 0
typeLower = []
others = 0
for d, t in S:
if t in appeared:
typeLower.append(d)
others += 1
else:
typeMax.append(d)
typeNum += 1
appeared |= {t}
typeMaxSum = [0] * (typeNum + 1)
typeLowerSum = [0] * (others + 1)
for i in range(typeNum): typeMaxSum[i+1] = typeMaxSum[i] + typeMax[i]
for i in range(others): typeLowerSum[i+1] = typeLowerSum[i] + typeLower[i]
for k in range(upperKCount + 1, min(typeNum, K) + 1):
maxPoint = max(maxPoint, k ** 2 + typeMaxSum[k] + typeLowerSum[K - k])
print(maxPoint)
return 0
if __name__ == "__main__":
solve() | p03148 |
def get_point(l):
deli = 0
kind = set()
for x in l:
kind = kind | set([x[0]])
deli += x[1]
point = deli + len(kind) * len(kind)
return point, kind
def remove_deplicated_kind(l, kind):
l = [[x[0], x[1]] for x in l if x[0] not in kind]
return l
def replace_data(curr, rest):
alt = [rest[0]] + curr[:-1]
replaced = curr[-1]
_, kind1 = get_point(curr)
_, kind2 = get_point(alt)
i = 0
while len(kind1) >= len(kind2):
temp = [replaced] + alt[:-1]
replaced = alt[-1]
alt = temp
_, kind2 = get_point(alt)
i += 1
if i >= len(curr):
break
return alt
n, k = list(map(int, input().split()))
l = []
for _ in range(n):
t, d = list(map(int, input().split()))
l.append([t, d])
l = sorted(l, key=lambda x: -x[1])
curr = l[:k]
rest = l[k:]
p, kind1 = get_point(curr)
ans = [p]
while len(rest) > 0:
curr = replace_data(curr, rest)
p, kind = get_point(curr)
rest = rest[1:]
rest = remove_deplicated_kind(rest, kind)
ans.append(p)
print((max(ans))) | import sys
def point(sushi, n, k):
sushi = sorted(sushi, key=lambda x:-x[1])
ate_t = set()
dpoint = 0
dup = []
for s in sushi[:k]:
t = s[0]
d = s[1]
if t in ate_t:
dup.append(d)
ate_t.add(t)
dpoint += d
ans = len(ate_t)**2 + dpoint
for s in sushi[k:]:
t = s[0]
d = s[1]
if len(dup) <= 0:
break
if t in ate_t:
continue
ate_t.add(t)
rm = dup.pop()
dpoint = dpoint - rm + d
res = len(ate_t)**2 + dpoint
ans = max(ans, res)
return ans
def main():
input = sys.stdin.readline
n, k = list(map(int, input().split()))
sushi = []
for _ in range(n):
t, d = list(map(int, input().split()))
sushi.append([t,d])
print((point(sushi, n, k)))
if __name__ == '__main__':
main() | p03148 |
N, K = list(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)) + 1):
C += lstA[i - 1]
D -= lstB[K - i]
tmp = C + D + i ** 2
ans = max(ans, tmp)
print(ans) | N, K = list(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 = set()
lstA = []
lstB = []
for item in sushi:
if item[0] in typ:
item[0] = 0
lstB.append(item[1])
else:
typ.add(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)) + 1):
C += lstA[i - 1]
D -= lstB[K - i]
tmp = C + D + i ** 2
ans = max(ans, tmp)
print(ans) | p03148 |
N,K = list(map(int,input().split()))
A = sorted([list(map(int,input().split())) for _ in range(N)],key=lambda x:x[1],reverse=True)
C = {}
tot1 = 0
for i in range(K):
t,d = A[i]
if t not in C:
C[t] = 0
C[t] += 1
tot1 += d
tot2 = len(C)
tot = tot1+tot2**2
cur = K
for i in range(K-1,-1,-1):
t,d = A[i]
if C[t]>1:
tot1 -= d
C[t] -= 1
j = cur
while j<N:
t1,d1 = A[j]
if t1 not in C:
C[t1]=1
tot1 += d1
tot2 += 1
if tot1+tot2**2>tot:
tot = tot1+tot2**2
cur = j+1
break
else:
j += 1
print(tot) | N,K = list(map(int,input().split()))
A = sorted([list(map(int,input().split())) for _ in range(N)],key=lambda x:x[1],reverse=True)
C = {}
tot1 = 0
for i in range(K):
t,d = A[i]
if t not in C:
C[t] = 0
C[t] += 1
tot1 += d
tot2 = len(C)
tot = tot1+tot2**2
cur = K
for i in range(K-1,-1,-1):
t,d = A[i]
if C[t]>1:
tot1 -= d
C[t] -= 1
j = cur
while j<N:
t1,d1 = A[j]
if t1 not in C:
C[t1]=1
tot1 += d1
tot2 += 1
if tot1+tot2**2>tot:
tot = tot1+tot2**2
cur = j+1
break
else:
j += 1
cur = j
print(tot) | p03148 |
import heapq
N, K = list(map(int, input().split()))
sushi = []
for i in range(N):
t, d = list(map(int, input().split()))
heapq.heappush(sushi, (-d, t))
total = 0
types = []
remain = []
for i in range(K):
d, t = heapq.heappop(sushi)
total -= d
if t not in types:
types.append(t)
else:
# 余りの寿司はremainにappend
heapq.heappush(remain, -d)
types_len = len(types)
ans = total + types_len ** 2
while sushi and remain:
d, t = heapq.heappop(sushi)
if t not in types:
types.append(t)
remain_d = heapq.heappop(remain)
total = total - d - remain_d
types_len += 1
ans = max(ans, total + types_len ** 2)
print(ans)
| import heapq
N, K = list(map(int, input().split()))
sushi = []
for i in range(N):
t, d = list(map(int, input().split()))
heapq.heappush(sushi, (-d, t))
total = 0
types = set()
remain = []
for i in range(K):
d, t = heapq.heappop(sushi)
total -= d
if t not in types:
types.add(t)
else:
# 余りの寿司はremainにappend
heapq.heappush(remain, -d)
types_len = len(types)
ans = total + types_len ** 2
while sushi and remain:
d, t = heapq.heappop(sushi)
if t not in types:
types.add(t)
remain_d = heapq.heappop(remain)
total = total - d - remain_d
types_len += 1
ans = max(ans, total + types_len ** 2)
print(ans)
| p03148 |
n,k=list(map(int, input().split()))
a=sorted([list(map(int, input().split())) for i in range(n)], key=lambda x:-x[1])
q=[]
ans=0
s=set()
for i in range(k):
ans+=a[i][1]
if a[i][0] in s:
q.append(a[i][1])
s.add(a[i][0])
ans+=len(s)**2
k1=k
ans1=ans
while k1<n and q:
if a[k1][0] not in s:
ans1=ans1+a[k1][1]-q.pop()-len(s)**2+(len(s)+1)**2
ans=max(ans,ans1)
s.add(a[k1][0])
k1+=1
print(ans)
| N, K = list(map(int, input().split()))
TD = sorted([list(map(int, input().split())) for i in range(N)], key=lambda x: -x[1])
q = []
ans = 0
s = set()
for i in range(K):
ans += TD[i][1]
if TD[i][0] in s:
q.append(TD[i][1])
s.add(TD[i][0])
ans += len(s) ** 2
k1 = K
ans1 = ans
while k1 < N and q:
if TD[k1][0] not in s:
ans1 = ans1 + TD[k1][1] - q.pop() - len(s) ** 2 + (len(s) + 1) ** 2
ans = max(ans, ans1)
s.add(TD[k1][0])
k1 += 1
print(ans) | p03148 |
from collections import defaultdict as dd
from heapq import heappop, heappush
N, K = list(map(int, input().split()))
l = [tuple(map(int, input().split())) for _ in range(N)]
l.sort(key=lambda x:-x[1])
used = set()
q1 = dd(list)
q2, q3 = [], []
score, kind = 0, 0
for t, d in l[:K]:
score += d
if t in used:
heappush(q3, d)
else:
used.add(t)
kind += 1
for t, d in l[K:]:
if not t in used:
heappush(q1[t], -d)
for t in list(q1.keys()):
d = heappop(q1[t])
heappush(q2, d)
ans = score + kind*kind
while q2 and q3:
d1 = heappop(q2)
d2 = heappop(q3)
score -= d1+d2
kind += 1
ans = max(ans, score + kind*kind)
print(ans) | from collections import defaultdict as dd
from heapq import heappop, heappush
N, K = list(map(int, input().split()))
l = [tuple(map(int, input().split())) for _ in range(N)]
l.sort(key=lambda x:-x[1])
used = set()
q1 = dd(list)
q2, q3 = [], []
score, kind = 0, 0
for t, d in l[:K]:
score += d
if t in used:
heappush(q3, d)
else:
used.add(t)
kind += 1
for t, d in l[K:]:
if not t in used:
heappush(q1[t], -d)
for t in list(q1.keys()):
d = heappop(q1[t])
heappush(q2, d)
ans = score + kind*kind
while q2 and q3:
score -= heappop(q2)+heappop(q3)
kind += 1
ans = max(ans, score + kind*kind)
print(ans) | p03148 |
from collections import defaultdict
from heapq import heappop, heappush
N, K = list(map(int, input().split()))
TD = []
for _ in range(N):
TD.append(tuple(map(int, input().split())))
TD.sort(key = lambda x:x[1], reverse=True)
TD += [(TD[-1][0], 0)]
D = defaultdict(list)
T = [0]*N
H = []
ans = 0
for i in range(K):
t, d = TD[i]
T[t-1] += 1
D[t].append(d)
ans += d
cnt = sum(1 for c in T if c >= 1)
ans += cnt**2
for t in range(1, N+1):
if T[t-1] >= 2:
heappush(H, (D[t].pop(), t))
T[t-1] -= 1
#print(ans, H, T, cnt)
for i in range(K, N):
if H:
t1, d1 = TD[i]
d2, t2 = heappop(H)
#print(t1, d1, t2, d2)
if T[t1-1] == 0 and d1 - d2 + 2*cnt + 1 >= 0:
ans += d1 - d2 + 2*cnt + 1
cnt += 1
T[t1-1] += 1
if T[t2-1] >= 2:
T[t2-1] -= 1
if T[t2-1] >= 2:
heappush(H, (D[t2].pop(), t2))
else:
heappush(H, (d2, t2))
#print(ans, H, T, cnt)
#元のやつとの組み合わせ...?
ans2 = 0
T2 = [0]*N
D2 = []
cnt2 = 0
for t, d in TD:
if cnt2 < K:
if T2[t-1]:
D2.append(d)
else:
T2[t-1] = 1
ans2 += d
cnt2 += 1
ans2 += sum(D2[:K-cnt2]) + sum(T2)**2
print((max(ans, ans2))) | from collections import defaultdict
from heapq import heappop, heappush
N, K = list(map(int, input().split()))
TD = []
for _ in range(N):
TD.append(tuple(map(int, input().split())))
TD.sort(key = lambda x:x[1], reverse=True)
TD += [(TD[-1][0], 0)]
D = defaultdict(list)
T = [0]*N
H = []
ans = [0]
for i in range(K):
t, d = TD[i]
T[t-1] += 1
D[t].append(d)
ans[0] += d
cnt = sum(1 for c in T if c >= 1)
ans[0] += cnt**2
for t, d in TD:
if T[t-1] >= 2:
heappush(H, (D[t].pop(), t))
T[t-1] -= 1
#print(ans, H, T, cnt)
for i in range(K, N):
if H:
t1, d1 = TD[i]
d2, t2 = heappop(H)
#print(t1, d1, t2, d2)
if T[t1-1] == 0:
ans.append(ans[-1] + d1 - d2 + 2*cnt + 1)
cnt += 1
T[t1-1] += 1
else:
heappush(H, (d2, t2))
#print(ans, H, T, cnt)
print((max(ans)))
#print(ans) | p03148 |
from collections import deque
n,k= list(map(int,input().split()))
td=[]
for i in range(n):
td.append(list(map(int,input().split())))
td.sort(key=lambda x:x[1],reverse=True)
select=[]
noselect=[]
cat=[]
for tdi in td:
if tdi[0] in cat:
noselect.append(tdi)
continue
select.append(tdi)
cat.append(tdi[0])
if len(select)==k:
break
i=0
while len(select) < k:
select.append(noselect[i])
i+=1
noselect=noselect[i:]
select.sort(key=lambda x:x[1])
select=deque(select)
#cat=list(set([tdi[0] for tdi in select]))
for no in noselect:
if no[0] not in cat:
continue
if (2 * len(cat)) - 1 < no[1] - select[0][1]:
select.popleft()
select.append(no)
cat=list(set([tdi[0] for tdi in select]))
else:
break
print((sum([tdi[1] for tdi in select])+(len(cat)**2)))
| n,k= list(map(int,input().split()))
td=[]
for _ in range(n):
td.append(list(map(int,input().split())))
td.sort(key=lambda x:x[1],reverse=True)
NEW=[1]*(n+1)
x=0
y=0
TOP=[]
SUB=[]
A=[]
for tdi in td[:k]:
if NEW[tdi[0]]==1:
NEW[tdi[0]]=0
TOP.append(tdi[1])
y+=1
else:
A.append(tdi[1])
x+=tdi[1]
for tdi in td[k:]:
if NEW[tdi[0]]==1:
SUB.append(tdi[1])
NEW[tdi[0]]=0
ans=x+y**2
for i in range(min(k-1,len(A),len(SUB))):
x -= A.pop()
x += SUB[i]
y += 1
ans = max(ans,x+y**2)
print(ans) | p03148 |
import queue
from collections import Counter
from operator import itemgetter
N, K = list(map(int, input().split()))
td = sorted([list(map(int, input().split())) for i in range(N)], key=itemgetter(1), reverse=True)
group = queue.PriorityQueue()
count = Counter()
ans = 0
for t, d in td[:K]:
group.put([d, t])
count[t] += 1
ans += d
ans += len(count) ** 2
s = queue.PriorityQueue()
s_count = Counter()
for t, d in td[K:]:
if not count[t] and not s_count[t]:
s.put([-d, t])
s_count[t] += 1
n = ans
x = len(count)
while group.qsize():
d, t = group.get()
if count[t] != 1:
count[t] -= 1
n -= (d + x**2)
d_, t_ = s.get()
count[t_] += 1
x += 1
n += (-d_ + x**2)
ans = max(ans, n)
print(ans) | import heapq
N, K = list(map(int, input().split()))
td = []
td_ = []
group = set()
total = 0
for i in range(N):
t, d = list(map(int, input().split()))
heapq.heappush(td, (-d, t))
for i in range(K):
d, t = heapq.heappop(td)
total -= d
if t not in group:
group.add(t)
else:
heapq.heappush(td_, -d)
ans = len(group)**2 + total
while td and td_:
d, t = heapq.heappop(td)
if t not in group:
group.add(t)
d_ = heapq.heappop(td_)
total = total - d_ - d
ans = max(ans, len(group)**2 + total)
print(ans) | p03148 |
n,k=list(map(int,input().split()))
a=sorted([list(map(int,input().split())) for i in range(n)],reverse=True,key=lambda x: x[1])
type=set([a[i][0] for i in range(k)])
dsum=sum([x[1] for x in a[:k]])
tnum=dict()
for i in [x[0] for x in a[:k]]:
if i not in list(tnum.keys()):
tnum[i]=1
else:tnum[i]+=1
ans=dsum+len(type)**2
sushi=a[-(n-k+1)::-1]
for i in a[k:]:
if i[0] not in type:
type.add(i[0])
for x in sushi[:]:
if tnum[x[0]]>1:
tnum[x[0]]-=1
dsum=dsum+i[1]-x[1]
ans=max(ans,dsum+len(type)**2)
break
sushi.remove(x)
print(ans)
| n, k = list(map(int, input().split()))
td = sorted([list(map(int, input().split())) for i in range(n)], reverse=True, key=lambda x: x[1])
type = set()
L = []
Sum = 0
for x in td[:k]:
Sum += x[1]
if x[0] not in type:
type.add(x[0])
else:
L.append(x[1])
L = L[::-1]
type_cnt = len(type)
ans = Sum + type_cnt ** 2
for x in td[k:]:
if len(L)==0:break
if x[0] not in type:
type.add(x[0])
type_cnt += 1
Sum = Sum - L.pop(0) + x[1]
ans = max(ans, Sum + type_cnt ** 2)
print(ans)
| p03148 |
N,K = list(map(int,input().split()))
s = [list(map(int,input().split())) for l in range(N)]
ans = []
s.sort(key = lambda x:x[1],reverse = True)
cnt = {}
A = 0
for i in range(K):
if s[i][0] not in cnt:
cnt[s[i][0]] = 1
else:
cnt[s[i][0]] += 1
ans.append(s[i])
A += s[i][1]
A += len(cnt)**2
ans.sort(key = lambda x:x[1])
for i in range(K,N):
if s[i][0] not in cnt:
for j in range(K):
if cnt[ans[j][0]] >= 2:
ans.pop(j)
ans.insert(0,s[i])
cnt[s[i][0]] = 1
temp = 0
for l in ans:
temp += l[1]
A = max(A,temp+len(cnt)**2)
break
else:
continue
if len(cnt) >= K:break
print(A) | N, K = [int(_) for _ in input().split()]
TD = [[int(_) for _ in input().split()] for i in range(N)]
# print(TD)
xs = sorted(TD, key=lambda x:-x[1])
#print(xs)
baset = 0
ts = set()
ds = []
for t, d in xs[:K]:
baset += d
if not t in ts:
ts.add(t)
else:
ds.append(d)
result = baset + len(ts)**2
r = result
for t, d in xs[K:]:
if not ds:
break
if t not in ts:
r += 2*len(ts) + 1
r += d - ds.pop()
ts.add(t)
result = max(r,result)
print(result) | p03148 |
import heapq as h;I=lambda:list(map(int,input().split()));n,k=I();z=[i*2+1for i in range(n)];s=sorted([I()for _ in[0]*n],key=lambda x:-x[1]);q=[];v=A=0;l=[0]*-~n
for a,b in s[:k]:
if l[a]:h.heappush(q,b)
else:A+=z[v];v+=1
l[a]=1;A+=b
T=A
for a,b in s[k:]:
if l[a]^1and q:l[a]=1;T+=b+z[v]-h.heappop(q);v+=1;A=max(A,T)
print(A) | import heapq as h;I=lambda:list(map(int,input().split()));n,k=I();z=[i*2+1for i in range(n)];s=sorted([I()for _ in[0]*n],key=lambda x:-x[1]);q=[];v=T=A=0;l=[0]*-~n
for i,(a,b)in enumerate(s):
if i<k:
if l[a]:h.heappush(q,b)
else:T+=z[v];v+=1
l[a]=1;T+=b
elif l[a]^1and q:l[a]=1;T+=b+z[v]-h.heappop(q);v+=1
A=max(A,T)
print(A) | p03148 |
import heapq as h;I=lambda:list(map(int,input().split()));n,k=I();z=[i*2+1for i in range(n)];s=sorted([I()for _ in[0]*n],key=lambda x:-x[1]);q=[];v=i=T=A=0;l=[0]*-~n
for a,b in s:
if i<k:
if l[a]:h.heappush(q,b)
else:T+=z[v];v+=1
l[a]=1;T+=b
elif l[a]^1and q:l[a]=1;T+=b+z[v]-h.heappop(q);v+=1
A=max(A,T);i+=1
print(A) | import heapq as h;I=lambda:list(map(int,input().split()));n,k=I();z=[i*2+1for i in range(n)];q=[];v=i=T=A=0;l=[0]*-~n
for a,b in sorted([I()for _ in[0]*n],key=lambda x:-x[1]):
if i<k:
if l[a]:h.heappush(q,b)
else:T+=z[v];v+=1
l[a]=1;T+=b
elif l[a]^1and q:l[a]=1;T+=b+z[v]-h.heappop(q);v+=1
A=max(A,T);i+=1
print(A) | p03148 |
import sys
input = sys.stdin.readline
from operator import itemgetter
import heapq
n, k = list(map(int, input().split()))
TD = sorted([list(map(int, input().split())) for _ in range(n)], reverse=True, key=itemgetter(1))
L = [[] for _ in range(n+1)]
P = []
a = 0
cnt = 0
F = [-float("inf")]*(n+1)
B = 0
for t, d in TD[:k]:
if B>>t & 1:
heapq.heappush(P, d)
else:
B |= 1 << t
a += 1
cnt += d
F[a] = cnt
for i in range(a, n):
if len(P) == 0:
break
while k < n and B>>TD[k][0] & 1:
k += 1
if k == n:
break
t, d = TD[k]
B |= 1 << t
F[i+1] = F[i] - heapq.heappop(P) + d
ans = 0
for i in range(a, n+1):
ans = max(ans, F[i] + i*i)
print(ans) | import sys
input = sys.stdin.readline
from operator import itemgetter
import heapq
n, k = list(map(int, input().split()))
TD = sorted([list(map(int, input().split())) for _ in range(n)], reverse=True, key=itemgetter(1))
L = [[] for _ in range(n+1)]
P = []
a = 0
cnt = 0
F = [-float("inf")]*(n+1)
for t, d in TD[:k]:
L[t].append(d)
if len(L[t]) == 1:
a += 1
cnt += d
F[a] = cnt
for i in range(1, n+1):
while len(L[i]) > 1:
heapq.heappush(P, L[i].pop())
now = k
for i in range(a, n):
if len(P) == 0:
break
while now < n and L[TD[now][0]]:
now += 1
if now == n:
break
t, d = TD[now]
L[t].append(d)
F[i+1] = F[i] - heapq.heappop(P) + d
ans = 0
for i in range(a, n+1):
ans = max(ans, F[i] + i*i)
print(ans) | p03148 |
def p_d():
from collections import defaultdict
from heapq import heappush, heappop
N, K = list(map(int, input().split()))
li = []
for _ in range(N):
t, d = list(map(int, input().split()))
li.append((t, d))
li.sort(key=lambda x: x[1], reverse=True)
sushi = defaultdict(list)
ans = 0
for i in range(K):
heappush(sushi[li[i][0]], li[i][1])
ans += li[i][1]
ans += len(list(sushi.keys())) ** 2
ans_list = [ans]
while True:
k, v = -1, 10 ** 9 + 1
for key in list(sushi.keys()):
if len(sushi[key]) > 1:
tmp = heappop(sushi[key])
if v > tmp:
if k != -1:
heappush(sushi[k], v)
k, v = key, tmp
else:
heappush(sushi[key], tmp)
if k == -1:
break
while K < N and li[K][0] in list(sushi.keys()):
K += 1
if K == N:
break
ans -= v
ans -= len(list(sushi.keys())) ** 2
heappush(sushi[li[K][0]], li[K][1])
ans += li[K][1]
ans += len(list(sushi.keys())) ** 2
ans_list.append(ans)
print((max(ans_list)))
if __name__ == '__main__':
p_d()
| def p_d():
N, K = list(map(int, input().split()))
inf = 10 ** 18
sushi = [[-inf] for _ in range(N)]
max_kind = -1
for _ in range(N):
t, d = list(map(int, input().split()))
sushi[t - 1].append(d)
if max_kind < t:
max_kind = t
for i in range(max_kind):
sushi[i].sort(reverse=True)
sushi.sort(key=lambda x: x[0], reverse=True)
que = []
res = 0
for i in range(K):
res += sushi[i][0]
for j in sushi[i][1:]:
que.append(j)
que.sort()
cur = res
res += K ** 2
for var in range(1, K)[::-1]:
a = que.pop()
b = sushi[var][0]
cur += a - b
res = max(res, cur + var ** 2)
print(res)
if __name__ == '__main__':
p_d()
| p03148 |
from collections import Counter
N,K = list(map(int,input().split()))
src = [tuple(map(int,input().split())) for i in range(N)]
src.sort(key=lambda x:-x[1])
selected = Counter()
ans = 0
stack = []
for t,d in src[:K]:
ans += d
selected[t] += 1
stack.append((t,d))
ans += len(selected)**2
tmp = ans
for t,d in src[K:]:
if t in selected: continue
while stack:
t0,d0 = stack.pop()
if selected[t0] > 1:
tmp += d - d0 + 2*len(selected)+1
ans = max(ans, tmp)
selected[t0] -= 1
selected[t] += 1
break
else:
break
print(ans) | N,K = list(map(int,input().split()))
TD = [tuple(map(int,input().split())) for i in range(N)]
TD.sort(key=lambda x:x[1])
import heapq
hq = []
heapq.heapify(hq)
selected = set()
ans = 0
for _ in range(K):
t,d = TD.pop()
if t in selected:
heapq.heappush(hq, d)
else:
selected.add(t)
ans += d
ans += len(selected)**2
tmp = ans
while TD:
t,d = TD.pop()
if t in selected: continue
selected.add(t)
if len(hq)==0: break
tmp -= heapq.heappop(hq)
tmp += d
tmp += len(selected)*2 - 1
ans = max(ans ,tmp)
print(ans) | p03148 |
from itertools import permutations
N, K = list(map(int, input().split()))
inputs = [[int(i) for i in input().split()] for _ in range(N)]
ans = 0
for items in permutations(inputs, K):
tmp = 0
sets = set()
for i in range(K):
tmp += items[i][1]
sets.add(items[i][0])
tmp += len(sets) * len(sets)
ans = max(ans, tmp)
print(ans)
| N, K = list(map(int, input().split()))
inputs = []
priority_queue = []
queue = []
sets = set()
for _ in range(N):
key, value = list(map(int, input().split()))
inputs.append((value, key))
inputs.sort(reverse=True) # valueで降順のソートをかけてる
for i in range(K):
value, key = inputs[i]
if key in sets:
queue.append(value)
else:
priority_queue.append(value)
sets.add(key)
sum_priority_queue = sum(priority_queue)
sum_queue = sum(queue)
kinds = len(sets)
ans = sum_priority_queue + sum_queue + kinds ** 2
for i in range(K, N):
if not queue:
break
value, key = inputs[i]
if key in sets:
continue
sets.add(key)
sum_queue -= queue.pop()
sum_priority_queue += value
kinds += 1
ans = max(ans, sum_priority_queue + sum_queue + kinds ** 2)
print(ans)
| p03148 |
import itertools
N, K = list(map(int, input().split()))
data = [list(map(int, input().split())) for _ in range(N)]
now = data[:K]
def count(x):
p = len(set([a[0] for a in x]))
point = p ** 2
p = sum([a[1] for a in x])
point += p
return point
p_max = count(now)
for i in list(itertools.combinations(data,K)):
if p_max < count(i):
p_max = count(i)
print(p_max)
| N, K = list(map(int, input().split()))
data = [list(map(int, input().split())) for _ in range(N)]
data = sorted(data, key=lambda x: x[1], reverse=1)
now = data[:K]
def count(x):
p = len(set([a[0] for a in x]))
point = p ** 2
p = sum([a[1] for a in x])
point += p
return point
p_max = count(now)
for i in data[K:]:
if not i[0] in [a[0] for a in now]:
for j in reversed(list(range(K))):
if [a[0] for a in now].count(now[j][0]) != 1:
now = now[:j]+now[j+1:]+[i]
break
if p_max < count(now):
p_max = count(now)
print(p_max)
| p03148 |
from collections import Counter
import heapq
n ,k = list(map(int, input().split()))
used = []
unused = []
for i in range(n):
t, d = list(map(int, input().split()))
unused.append((-d,t))
heapq.heapify(unused)
# 最初の集合
c = Counter()
ans = []
kiso = 0
for i in range(k):
d, t = heapq.heappop(unused)
kiso -= d
c[t] += 1
heapq.heappush(used,(-d, t))
bonus = len(c)
ans.append(kiso + bonus**2)
for i in range(k):
# いらないものを抜く
flag = False
atode = []
while used:
d, t = heapq.heappop(used)
if c[t] > 1:
c[t] -= 1
kiso -= d
flag = True
break
atode.append((d,t))
for i in atode:
heapq.heappush(used, i)
# 全部の種類が1つしかない場合
if not flag:break
# 種類を増やす
flag = False
atode = []
while unused:
d, t = heapq.heappop(unused)
if c[t]==0:
bonus+=1
kiso += -d
c[t] += 1
heapq.heappush(used, (-d, t))
flag = True
break
atode.append((d,t))
for i in atode:
heapq.heappush(unused, i)
# 種類を増やせなかった場合
if not flag:break
ans.append(kiso + bonus**2)
print((max(ans))) | #参考 https://atcoder.jp/contests/abc116/submissions/4059000
n, k = list(map(int,input().split()))
# 価値の大きい順にソート, おいしさが前に来るようにした
sushi = sorted(list(map(int, input().split()))[::-1] for i in range(n))[::-1]
s = set()
# 二個以上あるものの集合があれば良い,かつ最小順
q = []
kiso = 0
for i in range(k):
d, t = sushi[i]
if t in s:
q.append(d)
else:
s.add(t)
kiso += d
ans = kiso + len(s)**2
# 減らせる ∧ 増やせる
i = k
while i < n and q:
d, t = sushi[i]
if t not in s:
s.add(t)
kiso += -q.pop()+d
ans = max(ans, kiso+len(s)**2)
continue
i+=1
print(ans) | p03148 |
from heapq import heappush, heappop
N, K = list(map(int, input().split()))
TD = [list(map(int, input().split())) for _ in range(N)]
TD.sort(key=lambda x:x[1], reverse=True)
muptiple = set()
hp = []
for t, d in TD:
flg = 0
if t not in muptiple:
muptiple.add(t)
flg = 1
heappush(hp, (d*-1, flg))
ret = []
point = 0
vari = 0
total = 0
for i in range(K):
v = heappop(hp)
t = v[0]*-1
vari += v[1]
point += t
if not v[1]:
heappush(ret, t)
total = vari ** 2 + point
while len(hp) > 0 and len(ret) > 0:
v = heappop(hp)
if v[1] == 0:
continue
pre_t = heappop(ret)
t = v[0]*-1
point -= pre_t - t
vari += 1
tmp = vari ** 2 + point
total = max(total, tmp)
print(total) | from heapq import heappush, heappop
N, K = list(map(int, input().split()))
TD = [list(map(int, input().split())) for _ in range(N)]
TD.sort(key=lambda x:x[1], reverse=True)
muptiple = set()
for i in range(len(TD)):
flg = 0
if TD[i][0] not in muptiple:
muptiple.add(TD[i][0])
flg = 1
TD[i][0] = flg
TD.reverse()
ret = []
point = 0
vari = 0
total = 0
for i in range(K):
v = TD.pop()
vari += v[0]
point += v[1]
if not v[0]:
heappush(ret, v[1])
total = vari ** 2 + point
while len(TD) > 0 and len(ret) > 0:
v = TD.pop()
if v[0] == 0:
continue
pre_t = heappop(ret)
point -= pre_t - v[1]
vari += 1
tmp = vari ** 2 + point
total = max(total, tmp)
print(total) | p03148 |
import sys
input = sys.stdin.readline
N, K = list(map(int, input().split()))
sushi_array = []
sushi_count = [-1] * N
sushi_kind = 0
for i in range(N):
t, d = list(map(int, input().split()))
sushi_array.append([t - 1, d])
if sushi_count[t - 1] == -1:
sushi_count[t - 1] = 0
sushi_kind += 1
sushi_array = sorted(sushi_array, key=lambda x: -x[1])
# print(sushi_array, sushi_kind)
now_kind = 0
kiso = 0
for sushi in sushi_array[:K]:
kiso += sushi[1]
if sushi_count[sushi[0]] == 0:
now_kind += 1
sushi_count[sushi[0]] += 1
ans = kiso + now_kind * now_kind
add_index = K
remove_index = K - 1
now_kind += 1
while now_kind <= sushi_kind:
for sushi in sushi_array[add_index:]:
if sushi_count[sushi[0]] == 0:
kiso += sushi[1]
sushi_count[sushi[0]] = 1
break
add_index += 1
else:
break
for sushi in sushi_array[remove_index::-1]:
if sushi_count[sushi[0]] >= 1:
kiso -= sushi[1]
sushi_count[sushi[0]] -= 1
break
remove_index -= 1
else:
break
# print(kiso + now_kind * now_kind)
ans = max(ans, kiso + now_kind * now_kind)
now_kind += 1
print(ans) | import sys
input = sys.stdin.readline
N, K = list(map(int, input().split()))
sushi_array = []
sushi_count = [-1] * N
sushi_kind = 0
for i in range(N):
t, d = list(map(int, input().split()))
sushi_array.append([t - 1, d])
if sushi_count[t - 1] == -1:
sushi_count[t - 1] = 0
sushi_kind += 1
sushi_array = sorted(sushi_array, key=lambda x: -x[1])
# print(sushi_array, sushi_kind)
now_kind = 0
kiso = 0
can_remove = []
for sushi in sushi_array[:K]:
kiso += sushi[1]
if sushi_count[sushi[0]] == 0:
now_kind += 1
sushi_count[sushi[0]] = 1
else:
can_remove.append(sushi[1])
# print(sushi_count)
ans = kiso + now_kind * now_kind
add_index = K
remove_index = K - 1
# print(ans, kiso, now_kind)
now_kind += 1
for sushi in sushi_array[add_index:]:
if len(can_remove) == 0 or now_kind > sushi_kind:
break
if sushi_count[sushi[0]] == 0:
kiso += sushi[1] - can_remove.pop()
sushi_count[sushi[0]] = 1
# print(ans, kiso, now_kind)
ans = max(ans, kiso + now_kind * now_kind)
now_kind += 1
else:
continue
print(ans) | p03148 |
import sys
input = sys.stdin.readline
N, K = list(map(int, input().split()))
sushi_array = []
sushi_count = [-1] * N
sushi_kind = 0
for i in range(N):
t, d = list(map(int, input().split()))
sushi_array.append([t - 1, d])
if sushi_count[t - 1] == -1:
sushi_count[t - 1] = 0
sushi_kind += 1
sushi_array = sorted(sushi_array, key=lambda x: -x[1])
# print(sushi_array, sushi_kind)
now_kind = 0
kiso = 0
can_remove = []
for sushi in sushi_array[:K]:
kiso += sushi[1]
if sushi_count[sushi[0]] == 0:
now_kind += 1
sushi_count[sushi[0]] = 1
else:
can_remove.append(sushi[1])
# print(sushi_count)
ans = kiso + now_kind * now_kind
add_index = K
remove_index = K - 1
# print(ans, kiso, now_kind)
now_kind += 1
for sushi in sushi_array[add_index:]:
if len(can_remove) == 0 or now_kind > sushi_kind:
break
if sushi_count[sushi[0]] == 0:
kiso += sushi[1] - can_remove.pop()
sushi_count[sushi[0]] = 1
# print(ans, kiso, now_kind)
ans = max(ans, kiso + now_kind * now_kind)
now_kind += 1
else:
continue
print(ans) | import sys
input = sys.stdin.readline
N, K = list(map(int, input().split()))
sushi_array = []
sushi_count = [-1] * N
sushi_kind = 0
for i in range(N):
t, d = list(map(int, input().split()))
sushi_array.append([t - 1, d])
if sushi_count[t - 1] == -1:
sushi_count[t - 1] = 0
sushi_kind += 1
sushi_array = sorted(sushi_array, key=lambda x: -x[1])
# print(sushi_array, sushi_kind)
now_kind = 0
kiso = 0
for sushi in sushi_array[:K]:
kiso += sushi[1]
if sushi_count[sushi[0]] == 0:
now_kind += 1
sushi_count[sushi[0]] += 1
ans = kiso + now_kind * now_kind
add_index = K
remove_index = K - 1
now_kind += 1
while now_kind <= sushi_kind:
while add_index < N:
sushi = sushi_array[add_index]
# print(sushi)
add_index += 1
if sushi_count[sushi[0]] == 0:
kiso += sushi[1]
sushi_count[sushi[0]] = 1
break
else:
break
while remove_index >= 0:
sushi = sushi_array[remove_index]
# print(sushi)
remove_index -= 1
if sushi_count[sushi[0]] > 1:
kiso -= sushi[1]
sushi_count[sushi[0]] -= 1
break
else:
break
# print(ans,kiso,now_kind * now_kind)
ans = max(ans, kiso + now_kind * now_kind)
now_kind += 1
print(ans) | p03148 |
import collections
import heapq
import operator
import sys
input = sys.stdin.readline
ri = lambda: int(eval(input()))
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]
n, k = ril()
ls = rils(n)
ls.sort(key=operator.itemgetter(1), reverse=True)
counter = collections.Counter()
ls0 = []
ls1 = []
base = 0
for i, (t, d) in enumerate(ls):
if i < k:
heapq.heappush(ls0, (d, t))
counter[t] += 1
base += d
else:
if t not in counter:
heapq.heappush(ls1, (-d, t))
m = len(counter)
res = base + m**2
for i in range(k):
ok = False
while ls0:
d, t = heapq.heappop(ls0)
counter[t] -= 1
if counter[t] > 0:
base -= d
ok = True
break
if not ok:
break
ok = False
while ls1:
d, t = heapq.heappop(ls1)
if t not in counter:
counter[t] += 1
base += -d
ok = True
break
if not ok:
break
res = max(res, base + (m + i + 1)**2)
print(res) | import collections
import heapq
import operator
import sys
input = sys.stdin.readline
ri = lambda: int(eval(input()))
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]
n, k = ril()
ls = rils(n)
ls.sort(key=operator.itemgetter(1), reverse=True)
counter0 = collections.Counter()
counter1 = collections.Counter()
ls0 = []
ls1 = []
base = 0
for i, (t, d) in enumerate(ls):
if i < k:
if counter0[t] > 0:
heapq.heappush(ls0, (d, t))
counter0[t] += 1
base += d
else:
if t not in counter0 and t not in counter1:
heapq.heappush(ls1, (-d, t))
counter1[t] += 1
m = len(counter0)
res = base + m**2
for _ in range(k):
if len(ls0) == 0 or len(ls1) == 0:
break
d, _ = heapq.heappop(ls0)
base -= d
d, _ = heapq.heappop(ls1)
base += -d
m += 1
res = max(res, base + m**2)
print(res) | p03148 |
import collections
import heapq
import operator
import sys
input = sys.stdin.readline
ri = lambda: int(eval(input()))
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]
n, k = ril()
ls = rils(n)
ls.sort(key=operator.itemgetter(1), reverse=True)
counter0 = collections.Counter()
counter1 = collections.Counter()
ls0 = []
ls1 = []
base = 0
for i, (t, d) in enumerate(ls):
if i < k:
if counter0[t] > 0:
heapq.heappush(ls0, (d, t))
counter0[t] += 1
base += d
else:
if t not in counter0 and t not in counter1:
heapq.heappush(ls1, (-d, t))
counter1[t] += 1
m = len(counter0)
res = base + m**2
for _ in range(k):
if len(ls0) == 0 or len(ls1) == 0:
break
d, _ = heapq.heappop(ls0)
base -= d
d, _ = heapq.heappop(ls1)
base += -d
m += 1
res = max(res, base + m**2)
print(res) | import heapq
import operator
import sys
input = sys.stdin.readline
ri = lambda: int(eval(input()))
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]
n, k = ril()
ls = rils(n)
ls.sort(key=operator.itemgetter(1), reverse=True)
selected0 = set()
selected1 = set()
ls0 = []
ls1 = []
base = 0
for i, (t, d) in enumerate(ls):
if i < k:
if t in selected0:
heapq.heappush(ls0, (d, t))
selected0.add(t)
base += d
else:
if t not in selected0 and t not in selected1:
heapq.heappush(ls1, (-d, t))
selected1.add(t)
m = len(selected0)
res = base + m**2
for _ in range(k):
if len(ls0) == 0 or len(ls1) == 0:
break
d, _ = heapq.heappop(ls0)
base -= d
d, _ = heapq.heappop(ls1)
base += -d
m += 1
res = max(res, base + m**2)
print(res) | p03148 |
from collections import defaultdict
import heapq
def main():
N, K = list(map(int, input().split(' ')))
sushi_list = [list(map(int, input().split(' '))) for _ in range(N)] # list of (neta, oishisa)
sushi_list.sort(key=lambda x: x[1], reverse=True) # sort by oishisa
# check sushi with top K oishisa
top_k_neta_num = len(set([s[0] for s in sushi_list[:K]]))
top_k_oishisa = sum([s[1] for s in sushi_list[:K]])
if top_k_neta_num == K:
print((top_k_neta_num**2 + top_k_oishisa))
return
# check sushi of top_k_neta_num ~ K neta
top_k_sushi = defaultdict(list)
rest_sushi = defaultdict(list)
for i, s in enumerate(sushi_list):
if i < K:
heapq.heappush(top_k_sushi[s[0]], s[1])
else:
heapq.heappush(rest_sushi[s[0]], - s[1]) # max-> min
for neta in list(top_k_sushi.keys()):
if neta in rest_sushi:
del rest_sushi[neta]
max_point = top_k_neta_num**2 + top_k_oishisa
point = max_point
neta_num = top_k_neta_num
for _ in range(K - top_k_neta_num):
min_oishisa = 10**10
min_oishisa_neta = 0
for neta, oishisa in list(top_k_sushi.items()):
if len(oishisa) > 1 and oishisa[0] < min_oishisa:
min_oishisa = oishisa[0]
min_oishisa_neta = neta
new_max_oishisa = 0
new_max_oishisa_neta = 0
for neta, oishisa in list(rest_sushi.items()):
if len(oishisa) > 0 and - oishisa[0] > new_max_oishisa:
new_max_oishisa = - oishisa[0]
new_max_oishisa_neta = neta
# update top K sushi
if min_oishisa_neta > 0 and new_max_oishisa_neta > 0:
point += new_max_oishisa - min_oishisa + 2 * neta_num + 1
neta_num += 1
if point > max_point:
max_point = point
heapq.heappop(top_k_sushi[min_oishisa_neta])
heapq.heappush(top_k_sushi[new_max_oishisa_neta], new_max_oishisa)
del rest_sushi[new_max_oishisa_neta]
print(max_point)
if __name__ == '__main__':
main() | def main():
N, K = list(map(int, input().split()))
sushi_list = list()
for _ in range(N):
t, d = list(map(int, input().split()))
t -= 1
sushi_list.append((t, d))
sushi_list.sort(key=lambda s: s[1], reverse=True) # おいしさの降順でソート
# おいしさTop Kのすしをとるのを初期解とする
neta_flag_list = [0] * N
topk_extra_sushi_list = list() # Top Kのすしで、各ネタ最大のおいしさはないすし
additional_sushi_list = list() # Top Kのすしのネタ以外で、各ネタの最大のおいしさのすし
point_topk = 0
for k, sushi in enumerate(sushi_list):
if k < K:
if neta_flag_list[sushi[0]] == 1:
topk_extra_sushi_list.append(sushi)
point_topk += sushi[1]
else:
if neta_flag_list[sushi[0]] == 0:
additional_sushi_list.append(sushi)
neta_flag_list[sushi[0]] = 1
n_neta = K - len(topk_extra_sushi_list)
point_topk += n_neta ** 2
# その他のネタをt種類追加する場合を考えて、初期解と比較していく
d_extra, d_additional = 0, 0
ans = point_topk
n_additional_neta = min(K - n_neta, len(topk_extra_sushi_list), len(additional_sushi_list))
for t in range(1, n_additional_neta + 1):
n_cur_neta = n_neta + t
d_extra += topk_extra_sushi_list[-t][1]
d_additional += additional_sushi_list[t - 1][1]
point = point_topk - d_extra + d_additional + (n_cur_neta**2 - n_neta**2)
ans = max(ans, point)
print(ans)
if __name__ == '__main__':
main() | p03148 |
import collections
n, k = list(map(int, input().split()))
h = []
for i in range(n):
a, b = list(map(int, input().split()))
h.append((a,b))
h.sort(key=lambda x: -x[1])
fav=h[0:k]
tb={}
for t,d in sorted(h[k:], key=lambda x: (-x[0], -x[1])):
tb[t] = tb.get(t, []) + [d]
alt = []
for t,ds in list(tb.items()):
alt.append((t,max(ds)))
alt.sort(key=lambda x: -x[1])
# print("h: ",h)
# print("fav: ",fav)
# print("alt: ",alt)
var = len(set([x[0] for x in fav]))
bestscore = var**2 + sum([x[1] for x in fav])
while(len(alt) > 0):
c = collections.Counter([x[0] for x in fav])
temp = sorted([x for x in fav if c[x[0]] > 1], key=lambda x: -x[1])
if(len(temp) == 0):
break
while(len(alt) > 0):
t1, d1 = alt.pop(0)
if(c[t1] == 0):
break
if(c[t1] != 0):
break
t2, d2 = temp[-1]
fav.remove((t2,d2))
fav.append((t1, d1))
var += 1
bestscore = max(bestscore, var**2 + sum([x[1] for x in fav]))
# print("fav: ",fav)
# print("alt: ",alt)
print(bestscore) | n, k = list(map(int, input().split()))
h = []
for i in range(n):
t, d = list(map(int, input().split()))
h.append((t,d))
h.sort(key=lambda x: -x[1])
taste = 0
typen = 0
typecnt = {}
bad = []
for i in range(k):
typecnt[h[i][0]] = typecnt.get(h[i][0], 0) + 1
if(typecnt[h[i][0]] == 1):
typen += 1
else:
bad.append(h[i][1])
taste += h[i][1]
# print(h)
# print(typecnt)
# print(bad)
ans = taste + typen**2
for i in range(k, n):
if(len(bad) == 0):
break
cnt = typecnt.get(h[i][0], 0)
if(cnt != 0):
continue
else:
typecnt[h[i][0]] = cnt + 1
typen += 1
taste -= bad.pop()
taste += h[i][1]
ans = max(ans, taste + typen**2)
print(ans) | p03148 |
import itertools
N, K = list(map(int, input().split()))
menu = {}
for i in range(N):
t, d = list(map(int, input().split()))
menu[i] = (t, d)
def manzoku(s):
manzoku = sum([menu[i][1] for i in s]) + len(set([menu[i][0] for i in s]))**2
return manzoku
#K sushi
shushi = itertools.combinations(menu, K)
print((max([manzoku(c) for c in shushi])))
| N, K = list(map(int, input().split()))
menu_ = []
for i in range(N):
t, d = list(map(int, input().split()))
menu_.append((t, d))
menu = sorted(menu_, key = lambda x:x[1], reverse=True)
neta = set()
duplicates = []
for m in menu[:K]:
if m[0] in neta:
duplicates.append(m[1])
else:
neta.add(m[0])
p = sum([d for (t, d) in menu[:K]]) + len(neta)**2
ans = p
for m in menu[K:]:
if m[0] not in neta and duplicates:
neta.add(m[0])
duplicate = duplicates.pop()
p += (m[1] - duplicate) + 2 * len(neta) - 1
#print(ans, p)
ans = max(ans, p)
print(ans)
| p03148 |
N, K = list(map(int, input().split()))
nums = [[] for i in range(N)]
for i in range(N):
t, d = list(map(int, input().split()))
t -= 1
nums[t].append(d)
maxs = []
rest = []
for i in range(N):
nums[i].sort(reverse=True)
if len(nums[i]) > 0:
maxs.append(nums[i][0])
rest = rest + nums[i][1:]
maxs.sort(reverse=True)
if len(maxs) > K:
maxs = maxs[:K]
rest = rest + maxs[K:]
rest.sort(reverse=True)
other = []
idx = 0
while len(maxs)+len(other) < K:
other.append(rest[idx])
idx += 1
rest = rest[idx:]
types = len(maxs)
for i,r in enumerate(rest):
diff = types*types - pow(types-1, 2)
if diff < r-maxs[len(maxs)-1-i]:
maxs[len(maxs)-1-i] = r
types -= 1
else:
break
ans = types*types
for num in maxs:
ans += num
for num in other:
ans += num
print(ans)
| import sys
input = sys.stdin.readline
N, K = list(map(int, input().split()))
nums = [[] for i in range(N)]
for i in range(N):
t, d = list(map(int, input().split()))
t -= 1
nums[t].append(d)
maxs = []
rest = []
for i in range(N):
nums[i].sort(reverse=True)
if len(nums[i]) > 0:
maxs.append(nums[i][0])
for n in nums[i][1:]:
rest.append(n)
#rest = rest + nums[i][1:]
maxs.sort(reverse=True)
if len(maxs) > K:
maxs = maxs[:K]
for n in maxs[K:]:
rest.append(n)
#rest = rest + maxs[K:]
rest.sort(reverse=True)
other = []
idx = 0
while len(maxs)+len(other) < K:
other.append(rest[idx])
idx += 1
rest = rest[idx:]
types = len(maxs)
#print(maxs)
#print(rest)
for i,r in enumerate(rest):
diff = types*types - pow(types-1, 2)
if diff < r-maxs[len(maxs)-1-i]:
maxs[len(maxs)-1-i] = r
types -= 1
else:
break
ans = types*types
for num in maxs:
ans += num
for num in other:
ans += num
print(ans)
| p03148 |
import heapq
n,k = list(map(int,input().split()))
L = []
for i in range(n):
t,d = list(map(int,input().split()))
L.append([d,t])
L.sort()
ans = []
cur = [0]*n
heap = []
var = 0
for i in range(k):
if cur[L[n-i-1][1]-1] == 0:
ans.append(L[n-i-1][0])
cur[L[n-i-1][1]-1] = 1
var += 1
else:
heapq.heappush(heap, L[n-i-1])
temp = 0
for i in range(len(ans)):
temp += ans[i]
for i in range(len(heap)):
temp += heap[i][0]
temp += var**2
now = temp
for i in range(n-k):
if len(heap) != 0:
a = heapq.heappop(heap)
if cur[L[n-k-i-1][1]-1] == 0:
ans.append(L[n-k-i-1][0])
cur[L[n-k-i-1][1]-1] = 1
now -= var**2
now -= a[0]
var += 1
now += var**2
now += L[n-k-i-1][0]
temp = max(temp, now)
else:
heapq.heappush(heap, a)
print(temp)
| import heapq
n,k = list(map(int,input().split()))
L = []
for i in range(n):
t,d = list(map(int,input().split()))
L.append([d,t])
L.sort()
ans = []
cur = [0]*n
heap = []
var = 0
for i in range(k):
if cur[L[n-i-1][1]-1] == 0:
ans.append(L[n-i-1][0])
cur[L[n-i-1][1]-1] = 1
var += 1
else:
heapq.heappush(heap, L[n-i-1])
temp = 0
for i in range(len(ans)):
temp += ans[i]
for i in range(len(heap)):
temp += heap[i][0]
temp += var**2
now = temp
for i in range(n-k):
if len(heap) != 0:
a = heapq.heappop(heap)
if cur[L[n-k-i-1][1]-1] == 0:
cur[L[n-k-i-1][1]-1] = 1
now -= var**2
now -= a[0]
var += 1
now += var**2
now += L[n-k-i-1][0]
temp = max(temp, now)
else:
heapq.heappush(heap, a)
print(temp)
| p03148 |
import itertools
import heapq
N, K = [int(x) for x in input().split()]
orig_td = [tuple([int(x) for x in input().split()]) for _ in range(N)]
td = sorted(orig_td.copy(), key=lambda x: x[1], reverse=True)
INF = 10**9
# print(td)
def score(a):
kiso = sum([x[1] for x in a])
bonus = len(set([x[0] for x in a]))**2
return kiso + bonus
first = []
second = []
kind = set()
for i in range(K):
if td[i][0] not in kind:
first.append(td[i])
kind.add(td[i][0])
else:
second.append(td[i])
best_ans = sum([x[1] for x in first + second]) + len(first)**2
for k in range(K, N):
if len(second) == 0:
break
if td[k][0] in kind:
continue
a = second.pop(-1)
kind.add(td[k][0])
first.append(td[k])
ans = sum([x[1] for x in first + second]) + len(first)**2
# print(first, second, ans)
if best_ans < ans:
best_ans = ans
print(best_ans)
| N, K = [int(x) for x in input().split()]
td = sorted([[int(x) for x in input().split()] for _ in range(N)],
key=lambda x: x[1], reverse=True)
INF = 10**9
# print(td)
first = []
second = []
kind = set()
for i in range(K):
if td[i][0] not in kind:
first.append(td[i])
kind.add(td[i][0])
else:
second.append(td[i])
yummy = sum([x[1] for x in first + second])
best_ans = yummy + len(kind)**2
for k in range(K, N):
if len(second) == 0:
break
if td[k][0] in kind:
continue
a = second.pop(-1)
yummy -= a[1]
kind.add(td[k][0])
first.append(td[k])
yummy += td[k][1]
ans = yummy + len(kind)**2
# print(first, second, ans)
if best_ans < ans:
best_ans = ans
print(best_ans)
| p03148 |
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
from decimal import *
input = sys.stdin.readline
def inputInt(): return int(eval(input()))
def inputMap(): return list(map(int, input().split()))
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]
inf = float('inf')
mod = 1000000007
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
def main():
N,K = inputMap()
sushi_mi = []
sushi_ki = []
syu = [[] for i in range(N)]
for i in range(N):
t,d = inputMap()
t -= 1
syu[t].append(d)
for i in syu:
if len(i) > 0:
i.sort()
i = i[::-1]
for j,val in enumerate(i):
if j == 0:
sushi_mi.append(val)
else:
sushi_ki.append(val)
sushi_mi.sort()
sushi_mi = sushi_mi[::-1]
sushi_ki.sort()
sushi_ki = sushi_ki[::-1]
#print(sushi_mi)
#print(sushi_ki)
ans = 0
for i in range(1, min(len(sushi_mi), K)+1):
#print(min(len(sushi_mi), K+1)+1)
x = 0
tmp = 0
if K - i > len(sushi_ki):
continue
for j in range(0, i):
tmp += sushi_mi[j]
x += 1
for j in range(0, K - i):
tmp += sushi_ki[j]
oishi = tmp + x * x
#print(oishi)
if ans < oishi:
ans = oishi
print(ans)
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
main()
| # -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
from decimal import *
input = sys.stdin.readline
def inputInt(): return int(eval(input()))
def inputMap(): return list(map(int, input().split()))
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]
inf = float('inf')
mod = 1000000007
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
def main():
N,K = inputMap()
sushi_mii = []
sushi_kii = []
syu = [[] for i in range(N)]
for i in range(N):
t,d = inputMap()
t -= 1
syu[t].append(d)
for i in syu:
if len(i) > 0:
i.sort()
i = i[::-1]
for j,val in enumerate(i):
if j == 0:
sushi_mii.append(val)
else:
sushi_kii.append(val)
sushi_mii.sort()
sushi_mii = sushi_mii[::-1]
sushi_kii.sort()
sushi_kii = sushi_kii[::-1]
sushi_mi = []
sushi_ki = []
for i,val in enumerate(sushi_mii):
if i == 0:
sushi_mi.append(val)
else:
tmp = sushi_mi[i-1]
sushi_mi.append(tmp + val)
for i,val in enumerate(sushi_kii):
if i == 0:
sushi_ki.append(val)
else:
tmp = sushi_ki[i-1]
sushi_ki.append(tmp + val)
ans = 0
for i in range(1, min(len(sushi_mi), K)+1):
x = 0
tmp = 0
if K - i > len(sushi_ki):
continue
tmp += sushi_mi[i-1]
x = i
if K - i > 0:
tmp += sushi_ki[K - i-1]
oishi = tmp + x * x
if ans < oishi:
ans = oishi
print(ans)
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
main()
| p03148 |
from heapq import heappop, heappush
n, nTake = list(map(int, input().split()))
a = sorted([tuple(map(int, input().split())) for _ in range(n)], key=lambda x: (-x[1], x[0]))
totalTaste = 0
same = []
types = set()
for i in range(nTake):
type, taste = a[i]
totalTaste += taste
if type not in types:
types.add(type)
else:
heappush(same, taste)
rem = []
typesRem = set()
for i in range(nTake, n):
type, taste = a[i]
if type not in types and type not in typesRem:
typesRem.add(type)
heappush(rem, -taste)
ret = totalTaste + len(types) ** 2
curTotal = totalTaste
add = 1
while same and rem:
worst = heappop(same)
best = heappop(rem)
curTotal = curTotal - worst - best
ret = max(ret, curTotal + (len(types) + add) ** 2)
add += 1
print(ret) | n, nTake = list(map(int, input().split()))
a = sorted([tuple(map(int, input().split())) for _ in range(n)], key=lambda x: -x[1])
totalTaste = 0
same = []
types = set()
for i in range(nTake):
type, taste = a[i]
totalTaste += taste
if type not in types:
types.add(type)
else:
same.append(taste)
rem = []
typesRem = set()
for i in range(nTake, n):
type, taste = a[i]
if type not in types and type not in typesRem:
typesRem.add(type)
rem.append(taste)
same.sort()
rem.sort(key=lambda x: -x)
ret = totalTaste + len(types) ** 2
curTotal = totalTaste
for i in range(min(len(same), len(rem))):
curTotal = curTotal - same[i] + rem[i]
ret = max(ret, curTotal + (len(types) + i + 1) ** 2)
print(ret) | p03148 |
N, K = list(map(int, input().split()))
list0 = []
list1 = []
set1 = set()
td = []
for i in range(N):
t, d = list(map(int, input().split()))
td.append((t, d))
td.sort(key=lambda x: (x[0], -x[1]))
for t, d in td:
if t in set1:
list0.append(d)
else:
set1.add(t)
list1.append(d)
list0.sort(reverse=True)
list1.sort(reverse=True)
various = min(K, len(list1))
selected0 = []
selected1 = []
for i in range(various):
selected1.append(list1.pop(0))
if K > various:
for i in range(K - various):
selected0.append(list0.pop(0))
ans = sum(selected0) + sum(selected1) + various ** 2
mn = selected1[-1]
while list0 and list0[0] > mn:
mn = selected1.pop(-1)
selected0.append(list0.pop(0))
various -= 1
tmp = sum(selected0) + sum(selected1) + various ** 2
if tmp <= ans:
break
ans = tmp
print(ans)
| N, K = list(map(int, input().split()))
list0 = []
list1 = []
set1 = set()
td = []
for i in range(N):
t, d = list(map(int, input().split()))
td.append((t, d))
td.sort(key=lambda x: (x[0], -x[1]))
for t, d in td:
if t in set1:
list0.append(d)
else:
set1.add(t)
list1.append(d)
list0.sort(reverse=True)
list1.sort(reverse=True)
various = min(K, len(list1))
selected0 = []
selected1 = []
for i in range(various):
selected1.append(list1.pop(0))
if K > various:
for i in range(K - various):
selected0.append(list0.pop(0))
ans = sum(selected0) + sum(selected1) + various ** 2
mn = selected1[-1]
sum0 = sum(selected0)
sum1 = sum(selected1)
while list0 and list0[0] > mn:
sum0 += list0.pop(0)
sum1 -= selected1.pop(-1)
various -= 1
tmp = sum0 + sum1 + various ** 2
if tmp <= ans:
break
ans = tmp
print(ans)
| p03148 |
from collections import deque
n,k=list(map(int,input().split()))
td=sorted([list(map(int,input().split())) for i in range(n)],reverse=True,key=lambda x:x[1])
ans=0
kinds=dict()
for i in range(k):
ans+=td[i][1]
if td[i][0] in kinds:
kinds[td[i][0]].append(td[i][1])
else:
kinds[td[i][0]]=deque()
kinds[td[i][0]].append(td[i][1])
l=len(kinds)
ans+=(l**2)
ans_=ans
for i in range(k,n):
if td[i][0] not in kinds:
mi=[1000000000,1000000000]
for j in kinds:
if len(kinds[j])>1:
if mi[1]>kinds[j][-1]:
mi=[j,kinds[j][-1]]
if mi==[1000000000,1000000000]:
break
else:
ans=ans-l**2+(l+1)**2-mi[1]+td[i][1]
kinds[mi[0]].pop()
kinds[td[i][0]]=deque()
kinds[td[i][0]].append(td[i][1])
ans_=max(ans,ans_)
l+=1
print(ans_)
| n,k=list(map(int,input().split()))
td=sorted([list(map(int,input().split())) for i in range(n)],reverse=True,key=lambda x:x[1])
ans=0
kl=dict()
for i in range(k):
ans+=td[i][1]
if td[i][0] in kl:
kl[td[i][0]]+=1
else:
kl[td[i][0]]=1
l=len(kl)
ans+=(l**2)
ans_=ans
now=k-1
for i in range(k,n):
if td[i][0] not in kl:
#最小を求めるのをもっと早く
while now>=0:
if kl[td[now][0]]>1:
mi=td[now]
kl[mi[0]]-=1
now-=1
break
now-=1
if now==-1:
break
else:
ans=ans+2*l+1-mi[1]+td[i][1]
kl[td[i][0]]=1
ans_=max(ans,ans_)
l+=1
print(ans_)
| p03148 |
from operator import itemgetter
# 満足ポイントを求める関数
def f(a, b):
return len(a) ** 2 + sum(a) + sum(b)
n, k = list(map(int, input().split()))
sushi = []
for i in range(n):
sushi.append(list(map(int, input().split())))
sushi.sort(key=itemgetter(1), reverse=True)
s = []
s_change = []
types = []
# 集合Sの生成
for i in range(k):
if sushi[i][0] not in types:
s.append(sushi[i])
types.append(sushi[i][0])
else:
s_change.append(sushi[i])
s_change.sort(key=itemgetter(1))
#print(s)
#print(s_change)
m = []
# 交換用の集合の生成
for i in range(k, n):
if sushi[i][0] not in types:
m.append(sushi[i])
types.append(sushi[i][0])
#print(m)
#print([l[1] for l in s])
g = []
g.append(f([l[1] for l in s], [l[1] for l in s_change]))
# 集合gを求める
for i in range(min(len(s_change), len(m))):
s.append(m[i])
s_change.pop(0)
g.append(f([l[1] for l in s], [l[1] for l in s_change]))
print((max(g)))
| # 入力
n, k = list(map(int, input().split()))
A = [] # 寿司のネタの美味しさと種類を入れるリスト
for _ in range(n):
A.append(list(map(int, input().split())))
# 美味しさ順にソートする
A.sort(key=lambda x: x[1], reverse=True)
# 美味しさで貪欲にとる
X = [] # 同じ種類で他に美味しさの大きい寿司が存在する
Y = [] # 取った寿司の中の各々の種類で美味しさが最も大きい
st = set() # 種類
for i in range(k):
t, d = 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
t, d = 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) | p03148 |
print((sum(len(str(i))%2 for i in range(1, int(eval(input())) + 1))))
| n = int(eval(input()))
i = 1
r = 0
while 10 * i <= n + 1:
r += 9 * i
i *= 100
print((r + max(0, n + 1 - i)))
| p02952 |
from math import log10
n = int(eval(input()))
cnt = 0
for i in range(1,n+1):
if int(log10(i)+1) % 2 != 0 and i <= n:
cnt += 1
print(cnt) | from math import log10
n =int(eval(input()))
print((len([i for i in range(1, n+1) if int(log10(i)+1) % 2 != 0 and i <= n]))) | p02952 |
N = int(eval(input()))
count = 0
for i in range(1, N + 1):
num_dig = 0
ii = i
while ii > 0:
ii = int(ii / 10)
num_dig += 1
if num_dig % 2 != 0:
count += 1
print(count) | N = int(eval(input()))
count = 0
for i in range(1, N + 1):
if len(str(i)) % 2 != 0:
count += 1
print(count) | p02952 |
#!/usr/bin/env python3
n=int(eval(input()))
a=0
for i in range(1,n+1):
if len(str(i))%2>0:
a+=1
print(a)
| print((sum(len(str(-~i))%2>0 for i in range(int(eval(input()))))))
| p02952 |
n = int(eval(input()))
count = 0
for i in range(1,n+1):
if len(str(i)) % 2 == 1:
count += 1
print(count)
|
n = eval(input())
num = int(n)
ans = 0
for i in range(1,num+1):
if len(str(i)) % 2 == 1:
ans += 1
# print(i)
print(ans)
| p02952 |
N = int(eval(input()))
answer = 0
for x in range(1,N+1):
if len(str(x)) & 1:
answer += 1
print(answer) | import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
answer = sum(len(str(x))&1 for x in range(1,N+1))
print(answer) | p02952 |
N = int(eval(input()))
count = 0
for i in range(1, N+1):
s = str(i)
if len(s) % 2 == 1:
count += 1
print(count) | def main():
N = int(eval(input()))
ans = 0
for i in range(1, N+1):
s = str(i)
if(len(s) % 2 == 1): ans +=1
return ans
if __name__ == '__main__':
print((main())) | p02952 |
# 単純にすべてをまわして桁数を計算したのでよかったらしい
n = int(eval(input()))
ans = 0
for i in range(1, n+1):
if len(str(i)) % 2 == 1:
ans += 1
print(ans)
| n = eval(input())
digit_n = len(str(n))
n = int(n)
ans = 0
if digit_n % 2 == 0:
for i in range(digit_n // 2):
ans += 9 * 10 ** (i * 2)
else:
if digit_n != 1:
for i in range((digit_n - 1) // 2):
ans += 9 * 10 ** (i * 2)
hasuu = n - 10 ** ((i+1)*2)
ans += hasuu + 1
else:
ans = n
print(ans)
# 端数の計算が間違っていた
| p02952 |
# -*- coding: utf-8 -*-
def main():
n = int(eval(input()))
ans = 0
for i in range(1, n + 1):
if len(list(str(i))) % 2 == 1:
ans += 1
print(ans)
if __name__ == '__main__':
main()
| # -*- coding: utf-8 -*-
def count_digit(n: int) -> int:
digit = 0
while n:
digit += 1
n //= 10
return digit
def main():
n = int(eval(input()))
ans = 0
for i in range(1, n + 1):
if count_digit(i) % 2 == 1:
ans += 1
print(ans)
if __name__ == '__main__':
main()
| p02952 |
N = int(eval(input()))
i = 1
cnt = 0
while i <= N:
cnt += 1
i += 1
if len(str(i)) % 2 == 0:
i *= 10
print(cnt) | N = int(eval(input()))
cnt = 0
i = 0
for i in range(0, 5, 2):
m = 10 ** i
if len(str(m)) > len(str(N)):
break
M = int("9" * (i + 1))
if M > N:
M = N
cnt += M - m + 1
print(cnt) | p02952 |
n = int(eval(input()))
num = 0
for i in range(n):
if len(str(i+1))%2 == 1:
num += 1
print(num) | n = int(eval(input()))
cnt = 0
for i in range(1,n+1):
s = len(str(i))
if s%2 == 1:
cnt +=1
print(cnt) | p02952 |
# 2019-11-16 10:55:08(JST)
import sys
# import collections
import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np
def main():
n = int(sys.stdin.readline().rstrip())
digits = math.floor(math.log10(n))
if digits % 2 == 0:
ans = n
else:
ans = 10 ** digits - 1
for i in range(1, digits, 2):
ans -= 10 ** (i+1) - 1 - 10 ** i + 1
print(ans)
if __name__ == "__main__":
main()
| import sys
n = sys.stdin.readline().rstrip()
l = len(n)
def main():
cnt = 0
for i in range(0, l, 2):
if i < l-1:
cnt += 10 ** (i + 1) - 10 ** i
else:
cnt += int(n) - (10 ** i - 1)
return cnt
if __name__ == '__main__':
ans = main()
print(ans) | p02952 |
# ABC 136: B – Uneven Numbers
n = int(eval(input()))
odd_digits = 0
for i in range(1, n + 1):
if len(str(i)) % 2 != 0:
odd_digits += 1
print(odd_digits) | # ABC 136: B – Uneven Numbers
N = int(eval(input()))
count = 0
for i in range(1, N + 1):
if len(str(i)) % 2 == 1:
count += 1
print(count) | p02952 |
# ABC136b
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
n = int(eval(input()))
c = 0
for i in range(1, n+1):
if (len(str(i)) % 2 == 1):
c += 1
print(c) | def main():
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
# map(int, input().split())
n = int(eval(input()))
ans = 0
for i in range(1, n + 1):
if len(str(i)) % 2 == 1:
ans += 1
print(ans)
if __name__ == '__main__':
main()
| p02952 |
N = int(eval(input()))
cnt = 0
while N > 0:
s = str(N)
length = len(s)
if length % 2 == 0:
N -= 1
else:
cnt += 1
N -= 1
print(cnt) | N = int(eval(input()))
cnt = 0
for i in range(1, N + 1):
if len(str(i)) % 2 == 1:
cnt += 1
print(cnt) | p02952 |
n = int(eval(input()))
ans = 0
for i in range(1,n+1):
digit = 1
while i//10 > 0:
digit += 1
i /= 10
if digit%2 == 1:
ans += 1
print(ans) | n = int(eval(input()))
ans = 0
for i in range(1,n+1):
if len(str(i))%2 == 1:
ans += 1
print(ans) | p02952 |
n = int(eval(input()))
print((len(list([x for x in range(1,n+1) if len(str(x))%2 == 1])))) | n = int(eval(input()))
gen = (x for x in range(1,n+1) if len(str(x))%2 == 1)
print((len(list(gen)))) | p02952 |
a=0
for i in range(int(eval(input()))):
if len(str(i+1))%2==1:a+=1
print(a) | n=int(eval(input()))
ans=0
for i in range(1, n+1):
if len(str(i))%2 == 1:
ans+=1
print(ans) | p02952 |
n = int(eval(input()))
res = 0
for i in range(1, n + 1):
l = len(str(i))
res += l & 1
print(res) | n = int(eval(input()))
print((sum([len(str(x)) & 1 for x in range(1, n+1)]))) | p02952 |
N = int(eval(input()))
count = 0
for i in range(N):
if len(str(i + 1)) % 2 == 1:
count += 1
print(count) | N = int(eval(input()))
# count = 0
# for i in range(N):
# if len(str(i + 1)) % 2 == 1:
# count += 1
# print(count)
print((sum(1 for i in range(N) if len(str(i + 1)) % 2 == 1))) | p02952 |
N = int(eval(input()))
ans = 0
for i in range(1,N+1):
s = str(i)
if (len(s) % 2 != 0):
ans+=1
print(ans)
| n = int(eval(input()))
ans = 0
for i in range(1,n+1):
if len(str(i)) % 2 != 0:
ans += 1
print(ans)
| p02952 |
import math
n = int(eval(input()))
cnt = 0
for i in range(1, n + 1):
if math.floor(math.log10(i) + 1) % 2:
cnt += 1
print(cnt) | n = int(eval(input()))
cnt = 0
for i in range(1, n + 1):
if len(str(i)) % 2:
cnt += 1
print(cnt) | p02952 |
n = int(eval(input()))
cnt = 0
for i in range(1, n + 1):
if len(str(i)) % 2:
cnt += 1
print(cnt) | n = int(eval(input()))
print((sum(len(str(i + 1)) % 2 for i in range(n)))) | p02952 |
N = int(eval(input()))
s = 0
for i in range(1, N+1):
if len(str(i))%2 == 1:
s = s+1
print (s) | N = int(eval(input()))
s = len(str(N))
t = 0
for i in range(0,s//2):
t = t+9*10**(i*2)
if s%2 == 1:
t = t+N-10**(s-1)+1
print (t) | p02952 |
N = int(eval(input()))
cnt = 0
for i in range(1,N+1):
if len(str(i)) %2 != 0:
cnt+=1
print(cnt) | N = int(eval(input()))
cnt = 0
for i in range(1,N+1):
if len(str(i))%2==1:
cnt+=1
print(cnt) | p02952 |
N=int(eval(input()))
ans=0
for i in range (1,N+1):
if len(str(i))%2==1:
ans +=1
ans=int(ans)
print(ans) | N = int(eval(input()))
cnt=0
for i in range(1,N+1):
a = len(str(i))
if a%2==1:
cnt +=1
print(cnt)
| p02952 |
import math
def digits(n):
return int(math.log10(n))+1
def main():
n = int(eval(input()))
n_digit = digits(n)
ans = 0
if n_digit%2==0:
for i in range(n_digit-1,0,-2):
ans = ans + (pow(10,i)-pow(10,i-1))
else:
ans = n - pow(10,n_digit-1) + 1
for i in range(n_digit-2,0,-2):
ans = ans + (pow(10,i)-pow(10,i-1))
print(ans)
if __name__ == "__main__":
main()
| n = int(eval(input()))
import math
digit = int(math.log10(n))+1
ans = 0
if digit%2==0:
for i in range(digit-1,0,-1):
if i%2!=0: ans += (10**i-10**(i-1))
print(ans)
else:
ans = n-10**(digit-1)+1
for i in range(digit-2,0,-1):
if i%2!=0: ans += (10**i-10**(i-1))
print(ans)
| p02952 |
N = int(eval(input()))
ans = 0
for i in range(1, N + 1):
if len(str(i)) % 2 == 1:
ans += 1
print(ans) | #!/usr/bin/env python3
import sys
def solve(N: int):
result = 0
for i in range(1, N + 1):
if len(str(i)) % 2 != 0:
result += 1
print(result)
return
# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template)
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = int(next(tokens)) # type: int
solve(N)
if __name__ == "__main__":
main()
| p02952 |
n = eval(input(''))
n = int(n)
result = []
for i in range(1,n+1):
if len(str(i)) % 2 !=0:
result.append(i)
print((len(result))) | n = int(eval(input()))
result = []
for i in range(1,n+1):
if len(str(i)) % 2 != 0:
result.append(i)
print((len(result))) | p02952 |
N = int(eval(input()))
cnt = 0
for i in range(1,N+1):
if len(str(i))%2 == 1:
cnt += 1
print(cnt) | print((sum([1 for i in range(1,int(eval(input()))+1) if len(str(i))%2==1])))
| p02952 |
def f(x):
res = 0
while x > 0:
x //= 10
res += 1
return res
n = int(eval(input()))
ans = 0
for i in range(1, n + 1):
if f(i) % 2 == 1:
ans += 1
print(ans)
| n = int(eval(input()))
print((len(tuple([x for x in range(1, n + 1) if len(str(x)) % 2 == 1]))))
| p02952 |
N = int(eval(input()))
ans = sum(len(str(i)) % 2 == 1 for i in range(1, N + 1))
print(ans)
| N = int(eval(input()))
ans = min(9, N) + max(0, min(999, N) - 99) + max(0, min(99999, N) - 9999)
print(ans)
| p02952 |
N = int(eval(input()))
c = 0
for i in range(1,N + 1):
if len(str(i))%2 == 1:
c += 1
print(c)
| n=int(eval(input()))
c=0
for i in range(1,n+1):
if len(str(i))%2!=0:
c+=1
print(c)
| p02952 |
n=int(eval(input()))
c=0
for i in range(1,n+1):
if len(str(i))%2!=0:
c+=1
print(c)
| n=int(eval(input()))
print((sum(len(str(i))%2 for i in range(1,n+1)))) | p02952 |
a=int(eval(input()))
lis=[]
for i in range(1,a+1):
lis.append(str(i))
cnt=0
for i in lis:
if len(i)%2==1:
cnt+=1
print(cnt) | n=int(eval(input()))
ans=0
for i in range(1,n+1):
i=str(i)
if len(i)%2==1:
ans+=1
print(ans) | p02952 |
N = int(eval(input()))
count = 0
for i in range(1, N+1):
s = str(i)
if len(s) % 2 == 1:
count += 1
print(count) | def I(): return int(eval(input()))
N = I()
ans = 0
for i in range(1,N+1):
if len(str(i))%2==1:
ans += 1
print(ans)
| p02952 |
#!/usr/bin/env python
def main(N):
ans = 0
for i in range(1, N+1):
if len(str(i)) % 2 == 1:
ans += 1
return(ans)
if __name__ == '__main__':
N = int(eval(input()))
print((main(N)))
| #!/usr/bin/env python
def main():
N = int(eval(input()))
ans = 0
for i in range(1, N+1):
if len(str(i)) %2 == 1:
ans += 1
print(ans)
if __name__ == '__main__':
main()
| p02952 |
N = int(eval(input()))
count = 0
for i in range(1, N+1):
if len(str(i)) % 2 == 1:
count += 1
print(count) | def resolve():
N = int(input())
ans = 0
for i in range(1, N+1):
s = str(i)
if len(s)%2 == 0:
continue
ans += 1
return print(ans)
if __name__ == "__main__":
resolve()
| p02952 |
ans = 0
for i in range(1, int(eval(input()))+1):
if((1<=i and i<=9)or(i>=100 and i<=999)or(i>=10000 and i<=99999)):
ans += 1
print(ans) | ans = 0
for i in range(1, int(eval(input()))+1):
if((1<=i<=9)or(100<=i<=999)or(10000<=i<=99999)):
ans += 1
print(ans) | p02952 |
n_str = eval(input())
n_int = int(n_str)
# 1 ~ 50000
# 桁数が奇数= 1桁, 3桁, 5桁
# 1 ~ 9, 100 ~ 999,10000 ~ 99999の90909個しかないので全探索いけるか?
exs_list = []
for i in range(1, 100000):
if len(str(i)) % 2 == 1:
exs_list.append(i)
exs = {i: i for i in exs_list}
n_s = []
ans = 0
for i in range(1, n_int + 1):
if len(str(i)) % 2 == 0:
continue
if i in exs:
ans += 1
print(ans)
| def digit_sum(n):
# 各桁の和を求める
# 計算量: O(logN)
ans = 0
while n > 0:
ans += 1
n //= 10
return ans
n = int(eval(input()))
cnt = 0
for i in range(1, n+1):
cnt += digit_sum(i) % 2
print(cnt)
| p02952 |
N=int(eval(input()))
count=0
for i in range(1,N+1):
if len(str(i))%2==1:count+=1
print(count) | N=int(eval(input()))
print((sum(len(str(i))%2==1 for i in range(1,N+1)))) | p02952 |
N=int(eval(input()))
print((sum(len(str(i))%2==1 for i in range(1,N+1)))) | N=int(eval(input()))
print((sum(len(str(i))%2 for i in range(1,N+1)))) | p02952 |
n = int(eval(input()))
count= 0
for i in range(1,n+1):
if len(str(i)) == 1 or len(str(i)) == 3 or len(str(i)) == 5:
count+=1
print(count) | n = int(eval(input()))
count= 0
for i in range(1,n+1):
if len(str(i))%2 == 1:
count+=1
print(count) | p02952 |
print((sum(len(str(x+1))%2for x in range(int(eval(input())))))) | print(sum(len(str(x+1))%2for x in range(eval(input())))) | p02952 |
n = int(eval(input()))
def countKeta(num):
count = 1
while num / 10 >= 1:
count += 1
num = num // 10
return count
count = 0
for i in range(1, n+1):
if(countKeta(i) % 2 == 1):
count += 1
print(count) | n = int(eval(input()))
if n < 10:
print(n)
elif n < 100:
print((9))
elif n < 1000:
print((n-90))
elif n < 10000:
print((909))
elif n < 100000:
print((n-9090))
elif n < 1000000:
print((90909)) | p02952 |
N = int(eval(input()))
hako = []
ans = 0
for i in range(N):
hako.append(N-i)
for k in range(N):
nakami = hako[k]
if len(str(nakami))%2 >= 1:
ans += 1
print(ans)
| count = 0
S=int(eval(input()))
for i in range(1,S+1):
if len(str(i))%2==1:
count +=1
print(count)
| p02952 |