Skip to content

USACO 2016 February Contest Bronze Division - Milk Pails#

Problem link: here

Solution Author: Stefan Dascalescu

Problem Solution#

We can use some math to try all variants of filling the big pile and pick the best one.

Source codes#

The source codes in C++ and Python can be seen below.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ifstream cin("pails.in");
    ofstream cout("pails.out");

    int x, y, m;
    cin >> x >> y >> m;

    int ans = m;
    for(int i = 0; i <= m/x; i++)
        ans = min(ans, (m - i * x) % y);

    cout << m - ans;
    return 0;
}
with open("pails.in", "r") as fin:
    data = fin.read().split()
x = int(data[0])
y = int(data[1])
m = int(data[2])
ans = m
for i in range(m // x + 1):
    ans = min(ans, (m - i * x) % y)
with open("pails.out", "w") as fout:
    fout.write(str(m - ans))