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
#include <iostream>
#include <deque>
#include <map>

using namespace std;

int A, B, C, a, b, c;
int N;
int res[3*100000];
typedef tuple<int, int, int> t_node;
map<t_node, int> dist;
deque<t_node> q;
int V[3];

int main()
{
    cin >> V[0] >> V[1] >> V[2] >> a >> b >> c;
    N = a + b + c;
    for(int i=0; i<N; i++) res[i] = -1;
    
    q.push_back({a, b, c});
    dist[{a, b, c}] = 0;
    
    while(!q.empty()) {
        
        t_node curNode = q.front(); q.pop_front();
        auto [v0, v1, v2] = curNode;
        if(res[v0] == -1) res[v0] = dist[curNode];
        if(res[v1] == -1) res[v1] = dist[curNode];
        if(res[v2] == -1) res[v2] = dist[curNode];
        
        for(int b1 = 0; b1<3; b1++) for (int b2 = 0; b2<3; b2++) {
            if(b1 == b2) continue;
            int v[] = {v0, v1, v2};
            int d = min(v[b1], V[b2] - v[b2]);
            v[b1] -= d; v[b2] += d;
            t_node nextNode = {v[0], v[1], v[2]};
            if(!dist.count(nextNode)) {
                q.push_back(nextNode);
                dist[nextNode] = dist[curNode] + 1;
            }
        }
    }
    
    for(int n=0; n<N; n++) cout << res[n] << " ";
    
}