提出 #76486384
ソースコード 拡げる
import sys
# 入力の高速化
input = sys.stdin.read
data = input().split()
N = int(data[0])
K = int(data[1])
M = int(data[2])
J = []
idx = 3
for _ in range(N):
c = int(data[idx])
v = int(data[idx + 1])
J.append((c, v))
idx += 2
# 価値が低い順(デフォルト)にソート。
# こうすると、J[-1] が一番価値が高くなります。
J.sort(key=lambda x: x[1])
vl = 0
used_colors = [False] * (N + 1)
used_gems = [False] * N # どの宝石を確保したかを記録するフラグ(popの代わり)
usedCounter = 0
# 1. M種類集めるフェーズ
# リストの末尾(価値が高い方)から見ていく
ptr = N - 1
while ptr >= 0 and usedCounter < M:
color, val = J[ptr]
if not used_colors[color]:
used_colors[color] = True
used_gems[ptr] = True # この宝石を確保したよ
usedCounter += 1
vl += val
ptr -= 1
# 2. 残りの枠を埋めるフェーズ
# もう一度上(末尾)から見て、まだ確保していない宝石を上から順に取る
remains_needed = K - usedCounter
ptr = N - 1
while ptr >= 0 and remains_needed > 0:
if not used_gems[ptr]: # まだ確保されていない、かつ残っている中で最高価値
vl += J[ptr][1]
remains_needed -= 1
ptr -= 1
print(vl)
提出情報
| 提出日時 | |
|---|---|
| 問題 | C - Variety |
| ユーザ | espre |
| 言語 | Python (PyPy 3.11-v7.3.20) |
| 得点 | 300 |
| コード長 | 1371 Byte |
| 結果 | AC |
| 実行時間 | 204 ms |
| メモリ | 179060 KiB |
ジャッジ結果
| セット名 | Sample | All | ||||
|---|---|---|---|---|---|---|
| 得点 / 配点 | 0 / 0 | 300 / 300 | ||||
| 結果 |
|
|
| セット名 | テストケース |
|---|---|
| Sample | sample-01.txt, sample-02.txt, sample-03.txt |
| All | 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, sample-01.txt, sample-02.txt, sample-03.txt |
| ケース名 | 結果 | 実行時間 | メモリ |
|---|---|---|---|
| 04.txt | AC | 45 ms | 79756 KiB |
| 05.txt | AC | 109 ms | 178888 KiB |
| 06.txt | AC | 112 ms | 178720 KiB |
| 07.txt | AC | 109 ms | 170776 KiB |
| 08.txt | AC | 47 ms | 79712 KiB |
| 09.txt | AC | 47 ms | 80348 KiB |
| 10.txt | AC | 55 ms | 90308 KiB |
| 11.txt | AC | 105 ms | 124804 KiB |
| 12.txt | AC | 195 ms | 179060 KiB |
| 13.txt | AC | 198 ms | 177420 KiB |
| 14.txt | AC | 199 ms | 177528 KiB |
| 15.txt | AC | 200 ms | 178084 KiB |
| 16.txt | AC | 198 ms | 177680 KiB |
| 17.txt | AC | 201 ms | 177820 KiB |
| 18.txt | AC | 198 ms | 177908 KiB |
| 19.txt | AC | 204 ms | 177524 KiB |
| 20.txt | AC | 202 ms | 178164 KiB |
| 21.txt | AC | 194 ms | 177640 KiB |
| 22.txt | AC | 108 ms | 174092 KiB |
| 23.txt | AC | 200 ms | 177636 KiB |
| sample-01.txt | AC | 48 ms | 79432 KiB |
| sample-02.txt | AC | 46 ms | 79668 KiB |
| sample-03.txt | AC | 44 ms | 79740 KiB |