Milk Mixing Simulation Algorithm

Given three buckets with capacities and initial milk amounts, perform 100 pouring operations in cyclic order (1→2, 2→3, 3→1, repeat). When pouring from bucket a to bucket b, transfer as much milk as possible until either bucket a is empty or bucket b is full.

Input Format

  • Line 1: Two integers c1, m1 (capacity and milk amount of bucket 1)
  • Line 2: Two integers c2, m2 (capactiy and milk amount of bucket 2)
  • Line 3: Two integers c3, m3 (capacity and milk amount of bucket 3)

All values are positive and ≤ 10^9.

Output Format

  • Three lines showing milk amounts in each bucket after 100 operations.

Example

Input:

10 3
11 4
12 5

Output:

0
10
2

Solution Approach

def pour_milk():
    capacities = list(map(int, input().split()))
    milk = list(map(int, input().split()))
    capacities.extend(list(map(int, input().split())))
    milk.extend(list(map(int, input().split())))
    
    for step in range(100):
        source = step % 3
        target = (source + 1) % 3
        
        transfer_amount = min(milk[source], capacities[target] - milk[target])
        milk[source] -= transfer_amount
        milk[target] += transfer_amount
    
    for amount in milk:
        print(amount)

Tags: simulation cyclic operations milk pouring algorithm

Posted on Sun, 26 Jul 2026 16:21:51 +0000 by WLC135