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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int x;
int res[100005];
vector<ll> cap, now;
map<ll, int> dist;

ll hasz(vector<ll> &x) {
    return x[0] + (x[1] << 20LL) + (x[2] << 40LL);
}

vector<ll> strip(ll x) {
    vector<ll> res;
    for (int i = 1; i <= 3; i++) {
        res.push_back(x & ((1LL << 20LL) - 1));
        x >>= 20LL;
    }

    return res;
}
int32_t main() {
    for (int i = 0; i < 3; i++) cin >> x, cap.push_back(x);
    for (int i = 0; i < 3; i++) cin >> x, now.push_back(x);
    for (int i = 0; i <= cap[2]; i++) res[i] = -1;
    for (auto i : now) res[i] = 0;
    queue<pair<ll ,int>> q;
    q.push({hasz(now), 0});
    while (!q.empty()) {
        auto now = strip(q.front().first);
        int cur_dist = q.front().second;
        q.pop();
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (i == j) continue;
                int x = min(now[i], cap[j] - now[j]);
                now[i] -= x;
                now[j] += x;
                ll hugo = hasz(now);
                if (!dist.count(hugo)) {
                    dist[hugo] = cur_dist + 1;
                    if (res[now[i]] == -1)
                        res[now[i]] = cur_dist + 1;
                    if (res[now[j]] == -1)
                        res[now[j]] = cur_dist + 1;
                    
                    q.push({hugo, cur_dist + 1});
                }

                now[i] += x;
                now[j] -= x;
            }
        }
    }

    for (int i = 0; i <= cap[2]; i++) cout << res[i] << " ";
}