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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define FORD(i,a,b) for(int i=a;i>=b;--i)
#define PB push_back
#define EB emplace_back
#define FI first
#define SE second
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define vvi vector<vi>
#define vll vector<ll>
#define vvll vector<vll>
#define vpii vector<pii>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ALL(X) (X).begin(),(X).end()
#ifndef DEBUG
#define endl (char)10
#endif
using namespace std;
using ll = long long;
using ld = long double;

template <class T>
istream& operator>> (istream& is, vector<T>& vec){
    FOR(i,0,vec.size()) is >> vec[i];
    return is;
}
template <class T>
ostream& operator<< (ostream& os, vector<T>& vec){
    for(auto& t : vec) os << t << " ";
    return os;
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    vi V(6);
    cin >> V;
    vi ans(V[2] + 1, -1);
    set<ll> seen;
    ans[V[3]] = ans[V[4]] = ans[V[5]] = 0;
    ll a = V[1] + 1;
    a *= V[2] + 1;
    ll b = V[2] + 1;
    ll zero = V[3] * a + V[4] * b + V[5];
    queue<pair<ll, int>> Q;
    Q.emplace(zero, 0);
    seen.insert(zero);
    vi pom(3);
    ll przelew;
    while(!Q.empty()){
        auto xx = Q.front();
        Q.pop();
        ll x = xx.FI;
        pom[0] = x / a;
        pom[1] = (x % a) / b;
        pom[2] = x % b;
        //cout << "Sciaglem " << xx.SE << " -> " << pom << endl;
        FOR(i,0,3){
            FOR(j,0,3){
                if (i == j) continue;
                przelew = min(pom[i], V[j] - pom[j]);
                pom[i] -= przelew;
                pom[j] += przelew;
                ll nx = pom[0] * a + pom[1] * b + pom[2];
                if (seen.count(nx) == 0){
                    seen.insert(nx);
                    Q.emplace(nx, xx.SE + 1);
                    FOR(k,0,3){
                        if(ans[pom[k]] == -1) ans[pom[k]] = xx.SE + 1;
                    }
                }
                pom[i] += przelew;
                pom[j] -= przelew;
            }
        }
    }
    cout << ans << endl;
}