1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <bits/stdc++.h>

using namespace std;

struct TimedState
{
    array<int, 3> liters;
    int t;
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    array<int, 3> maxLiters, currLiters;
    for (int i = 0; i < 3; ++i)
        cin >> maxLiters[i];
    for (int i = 0; i < 3; ++i)
        cin >> currLiters[i];
    
    queue<TimedState> q;
    map<array<int, 3>, int> vis;
    
    q.push({currLiters, 0});
    while (!q.empty())
    {
        auto curr = q.front();
        q.pop();
        
        if (vis.count(curr.liters) && vis[curr.liters] <= curr.t)
            continue;
        vis[curr.liters] = curr.t;
        
        for (int from = 0; from < 3; ++from)
        {
            for (int to = 0; to < 3; ++to)
            {
                if (from == to)
                    continue;
                
                int pouredLiters = min(curr.liters[from], maxLiters[to] - curr.liters[to]);
                
                auto newState = curr;
                newState.liters[from] -= pouredLiters;
                newState.liters[to] += pouredLiters;
                ++newState.t;
                
                if (!vis.count(newState.liters) || newState.t < vis[newState.liters])
                    q.push(newState);
            }
        }
    }
    
    vector<int> res(maxLiters.back() + 1, numeric_limits<int>::max());
    for (const auto& v : vis)
    {
        for (const auto x : v.first)
            res[x] = min(res[x], v.second);
    }
    
    for (const auto x : res)
        cout << (x == numeric_limits<int>::max() ? -1 : x) << " ";
    cout << "\n";

    return 0;
}