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
#include <bits/stdc++.h>
using namespace std;

#define ll long long

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int ABC[3];
  cin >> ABC[0] >> ABC[1] >> ABC[2];
  int abc[3];
  cin >> abc[0] >> abc[1] >> abc[2];
  vector<int> dp(ABC[2] + 1, INT_MAX);
  for (int i = 0; i <= 2; i++) {
    dp[abc[i]] = 0;
  }

  vector<vector<int>> m1;
  set<vector<int>> S;
  S.insert({abc[0], abc[1], abc[2]});
  queue<pair<vector<int>, int>> Q;
  Q.push({{abc[0], abc[1], abc[2]}, 0});

  while (!Q.empty()) {
    auto t = Q.front();
    Q.pop();
    for (int i = 0; i <= 2; i++) {
      for (int j = 0; j <= 2; j++) {
        if (i == j) {
          continue;
        }
        vector<int> temp(3);
        temp[0] = t.first[0];
        temp[1] = t.first[1];
        temp[2] = t.first[2];
        int k = min(ABC[j], temp[j] + temp[i]);
        temp[i] = max(0, temp[i] + temp[j] - ABC[j]);
        temp[j] = k;
        if (S.count(temp) == 0) {
          S.insert(temp);
          Q.push({temp, t.second + 1});
        }
        dp[temp[0]] = min(dp[temp[0]], t.second + 1);
        dp[temp[1]] = min(dp[temp[1]], t.second + 1);
        dp[temp[2]] = min(dp[temp[2]], t.second + 1);
      }
    }
  }
  for (int i = 0; i <= ABC[2]; i++) {
    cout << (dp[i] < INT_MAX ? dp[i] : -1) << " ";
  }
}