Here is the contest link. All problems are from Codeforces' Problemset.
A. The Killer
Ilia calls every n minutes and artists arrive every m minutes.
We need to find how many minutes both happen at the same time within z minutes.
These are the multiples of lcm(n, m) up to z, so the answer is simply z // lcm(n, m).
Example:
If n = 2, m = 3, and z = 9,
then lcm(2, 3) = 6, and only minute 6 overlaps → answer = 1.
Complexity:
- Per test case: O(1)
from math import gcd
def solve():
n, m, z = list(map(int, input().split()))
lcm = n * m // gcd(n, m)
print(z // lcm)
if __name__ == "__main__":
solve()
B. Tired
At the start, spectators stand up one by one until k of them are standing.
After that, for each new spectator that stands, one from the start sits down — keeping k people standing at a time — until time n.
Then, as the wave ends, spectators sit down one by one.
So:
- If
t ≤ k, the number of standing spectators =t(still building up) - If
k < t ≤ n, allkare standing - If
t > n, the number decreases:k - (t - n)
Example:
For n = 10, k = 5, t = 12 → the wave is ending,
so standing = 5 - (12 - 10) = 3.
Complexity:
- Per test case: O(1)
def solve():
n,k,t = list(map(int,input().split()))
if t <= k:
print(t)
elif t <= n:
print(k)
else:
print(k - (t - n))
if __name__ == "__main__":
solve()
C. UUU
If some jobs are missing, reassign idlers from overrepresented jobs with the smallest persuasion times.
Each idler chooses one job, but some jobs might not be chosen by anyone. We must ensure all jobs 1..k are assigned, using the minimum total persuasion time.
Steps:
- Count how many idlers chose each job using a counter.
- The number of
missing jobs = k - number_of_unique_jobs. - Sort all (
persuasion_time,chosen_job) pairs by persuasion time. - Go through the sorted list for any job that has multiple idlers, persuade the cheapest ones until all missing jobs are covered.
Example:
n = 8, k = 7
a = [1, 1, 3, 1, 5, 3, 7, 1]
b = [5, 7, 4, 8, 1, 3, 5, 2]
Unique jobs = {1, 3, 5, 7} → missing 3 jobs.
Sorted by persuasion time → [(1,5), (2,1), (3,3), (4,3), (5,1), (5,7), (7,1), (8,1)]
We can persuade from overrepresented jobs (1 and 3) with smallest times → total = 10.
Complexity:
- Time Complexity: O(n log n)
- Space Complexity: O(n)
from collections import Counter
def solve():
n, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
counter = Counter(a)
remaining = k - len(counter)
pairs = []
for i in range(n):
pairs.append((b[i], a[i]))
pairs.sort()
ans = 0
for bb, aa in pairs:
if aa in counter and counter[aa] > 1 and remaining > 0:
ans += bb
counter[aa] -= 1
remaining -= 1
print(ans)
if __name__ == '__main__':
solve()
D. Instabram
- The total duration of all videos must be divisible by
k. - Try to build each post one by one — whenever the accumulated sum reaches the required equal duration, start a new post.
We need to divide the videos into exactly k contiguous groups such that the sum of durations in each group is equal.
Steps:
- Compute the total duration
total = sum(a).
Iftotal % k != 0, it’s impossible → print"No". - The target duration per post is
avg = total // k. - Traverse the list, accumulating durations until the sum equals
avg.
When it matches, record the size of this group and reset the counter.
If it ever exceedsavg, it’s impossible ("No"). - At the end, if exactly
kgroups were formed, print"Yes"and their sizes.
Example:
- Input:
6 3
3 3 1 4 1 6
Total = 18 → avg = 6
Groups: [3, 3], [1, 4, 1], [6]
- Output:
Yes
2 3 1
Complexity:
- Per test case: O(n)
- Space: O(1)
def solve():
n, k = map(int, input().split())
a = list(map(int, input().split()))
total = sum(a)
if total % k != 0:
print("No")
return
avg = total // k
s = 0
cnt = 0
ans = []
for x in a:
s += x
cnt += 1
if s == avg:
ans.append(cnt)
s = 0
cnt = 0
elif s > avg:
print("No")
return
if s != 0 or len(ans) != k:
print("No")
return
print("Yes")
print(*ans)
if __name__ == "__main__":
solve()
E. Enkoklsh
- Each week repeats every 7 days, and only some days have classes (
1s). - Compute how many full weeks you need first, then handle the remaining classes using a sliding window to find the shortest consecutive segment whose sum equals the remaining needed days.
Steps:
- Count how many classes happen in a full week →
total = sum(week). - Compute how many full weeks you can complete:
days = (k // total) * 7, then handle the leftoverneed = k % total. - If
need = 0, you already finish on a full week boundary. - Otherwise, duplicate the 7-day pattern (
week *= 2) and use a sliding window to find the smallest subarray with a sum =need. - Add this minimal segment length to
daysand print the total.
Example:k = 2, week = [0,1,0,0,0,0,0]
→ one class per week → need 2 weeks → 7*1 + 1 = 8 days.
Complexity:
- Time: O(14) ≈ O(1) per test (constant number of days, doubled array length = 14)
- Space: O(1)
import sys
input = sys.stdin.readline
def solve():
k = int(input()) - 1
week = list(map(int, input().split()))
total = sum(week)
days = 1 + (k // total) * 7
need = k % total + 1
week *= 2
left = curr = 0
min_len = 7
for right in range(len(week)):
curr += week[right]
while left < right and curr > need:
curr -= week[left]
left += 1
while left < right and week[left] == 0:
left += 1
if curr == need:
min_len = min(min_len, right - left)
print(days + min_len)
t = int(input())
for _ in range(t):
solve()
F. Division
Try to balance the three group sizes while ensuring that no subgroup contains both Assembler and C++ fans.
We are given three integers a, b, and c representing the number of Assembler, Basic, and C++ fans.
Assembler (A) and C++ (C) fans cannot be in the same group, but both can be paired with Basic (B) fans.
Idea: - Since A and C can’t share a group, we split them and then distribute B fans to balance group sizes.
- The heap ensures that B fans are always added to the smallest current group, keeping the largest group size minimal.
- In the end, a small adjustment balances out B fans if they make one group too large.
Complexity:
- Time: O(b log 3) ≈ O(b)
- Space: O(1)
from heapq import heapify, heappop, heappush
def solve():
a, b, c = map(int, input().split())
x = y = z = 0
if a > c:
z = c
x = (a + 1) // 2
y = a // 2
elif c > a:
z = (c + 1) // 2
x = a
y = c // 2
else:
x, y, z = a, 0, c
heap = [[x, 'a'], [y, 'b'], [z, 'c']]
heapify(heap)
base_b = y
while b > 0:
val, tag = heappop(heap)
val += 1
heappush(heap, [val, tag])
b -= 1
heap.sort(key=lambda x: x[1])
a, b, c = heap[0][0], heap[1][0], heap[2][0]
while b > base_b and b > max(a, c):
if a > c:
a, c = c, a
b -= 1
a += 1
print(max(a, b, c))
for _ in range(int(input())):
solve()
G.Coca Dola
Think of it as a subset selection where you want to minimize the number of chosen bottles while ensuring enough total capacity and maximizing already filled soda.
Let the total amount of soda be S = sum(a). We must keep some bottles so that their total capacity is at least S, and we want to minimize the number of bottles used. Among all such sets, we should maximize the total soda already present in those bottles to minimize pouring time. This can be solved using dynamic programming: for every possible total capacity, store the minimum number of bottles needed and the maximum soda already inside them. Iterate over all bottles and update these states. Finally, among all capacities ≥ S, choose the state with the fewest bottles, and among those, the one requiring the least pouring time S - kept_soda.
Complexity:
- Per test case: O(n * total_capacity)
- Space: O(total_capacity)
def solve():
n = int(input())
amount = list(map(int, input().split()))
capacity = list(map(int, input().split()))
total_soda = sum(amount)
total_capacity = sum(capacity)
dp = [(float('inf'), 0)] * (total_capacity + 1)
dp[0] = (0, 0)
for i in range(n):
a, b = amount[i], capacity[i]
new_dp = dp[:]
for cur_cap in range(total_capacity - b, -1, -1):
bottles, kept = dp[cur_cap]
new_cap = cur_cap + b
candidate = (bottles + 1, kept + a)
if (candidate[0] < new_dp[new_cap][0]) or \
(candidate[0] == new_dp[new_cap][0] and candidate[1] > new_dp[new_cap][1]):
new_dp[new_cap] = candidate
dp = new_dp
min_bottles = n
min_time = float('inf')
for cap in range(total_soda, total_capacity + 1):
bottles, kept = dp[cap]
if bottles < float('inf'):
time = total_soda - kept
if bottles < min_bottles or (bottles == min_bottles and time < min_time):
min_bottles, min_time = bottles, time
print(min_bottles, min_time)
for _ in range(int(input())):
solve()







