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;

int main() {
  ios_base::sync_with_stdio(false);
  int A, B, C, a, b, c;
  cin >> A >> B >> C >> a >> b >> c;
  int sum = a + b + c;

  map<pair<int, int>, int> d;
  queue<pair<int, int>> q;
  
  auto relax = [&](int x, int y, int z, int distance) {
    auto id = make_pair(x, y);
    if (!d.count(id)) {
      d[id] = distance;
      q.push(id);
    }
  };

  vector<int> answer(max(A, max(B, C)) + 1, 1e9);

  relax(a, b, c, 0);
  while (!q.empty()) {
    tie(a, b) = q.front();
    int distance = d[q.front()];
    c = sum - (a + b);
    q.pop();

    answer[a] = min(answer[a], distance);
    answer[b] = min(answer[b], distance);
    answer[c] = min(answer[c], distance);

    int dab = min(a, B - b);
    relax(a - dab, b + dab, c, distance + 1);
    int dba = min(b, A - a);
    relax(a + dba, b - dba, c, distance + 1);
    int dac = min(a, C - c);
    relax(a - dac, b, c + dac, distance + 1);
    int dca = min(c, A - a);
    relax(a + dca, b, c - dca, distance + 1);
    int dbc = min(b, C - c);
    relax(a, b - dbc, c + dbc, distance + 1);
    int dcb = min(c, B - b);
    relax(a , b + dcb, c - dcb, distance + 1);
    
  }

  for (int i = 0; i <= C; ++i)
    if (answer[i] == 1e9)
      answer[i] = -1;

  cout << answer[0];
  for (int i = 1; i <= C; ++i)
    cout << ' ' << answer[i];
  cout << endl;
}