Skip to content

Instantly share code, notes, and snippets.

@maehrm
Created February 14, 2026 05:26
Show Gist options
  • Select an option

  • Save maehrm/179caa1a5f460f0bc7bbebe2e52d2417 to your computer and use it in GitHub Desktop.

Select an option

Save maehrm/179caa1a5f460f0bc7bbebe2e52d2417 to your computer and use it in GitHub Desktop.
N, M, C = map(int, input().split())
A = list(map(int, input().split()))
# 位置ごとの人数をまとめる
cnt = {}
for a in A:
if a not in cnt:
cnt[a] = 0
cnt[a] += 1
points = sorted(cnt.items()) # (位置, 人数)
K = len(points)
cur = 0
r = 0
ans = 0
for i in range(K):
while cur < C:
cur += points[r][1]
r += 1
if r >= K:
r -= K
if i == 0:
ans += (M + points[0][0] - points[-1][0]) * cur
else:
ans += (points[i][0] - points[i - 1][0]) * cur
cur -= points[i][1]
print(ans)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment